4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License version 2 for more details (a copy is included
14 * in the LICENSE file that accompanied this code).
16 * You should have received a copy of the GNU General Public License
17 * version 2 along with this program; If not, see
18 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
20 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21 * CA 95054 USA or visit www.sun.com if you need additional information or
27 * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
28 * Use is subject to license terms.
31 * This file is part of Lustre, http://www.lustre.org/
32 * Lustre is a trademark of Sun Microsystems, Inc.
35 #define DEBUG_SUBSYSTEM S_LNET
37 #include <libcfs/libcfs.h>
40 cfs_mem_cache_t *cfs_page_t_slab = NULL;
41 cfs_mem_cache_t *cfs_page_p_slab = NULL;
43 cfs_page_t * virt_to_page(void * addr)
46 pg = cfs_mem_cache_alloc(cfs_page_t_slab, 0);
53 memset(pg, 0, sizeof(cfs_page_t));
54 pg->addr = (void *)((__u64)addr & (~((__u64)PAGE_SIZE-1)));
56 cfs_atomic_set(&pg->count, 1);
57 set_bit(PG_virt, &(pg->flags));
64 * To allocate the cfs_page_t and also 1 page of memory
67 * flags: the allocation options
70 * pointer to the cfs_page_t strcture in success or
71 * NULL in failure case
77 cfs_atomic_t libcfs_total_pages;
79 cfs_page_t * cfs_alloc_page(int flags)
82 pg = cfs_mem_cache_alloc(cfs_page_t_slab, 0);
89 memset(pg, 0, sizeof(cfs_page_t));
90 pg->addr = cfs_mem_cache_alloc(cfs_page_p_slab, 0);
91 cfs_atomic_set(&pg->count, 1);
94 if (cfs_is_flag_set(flags, CFS_ALLOC_ZERO)) {
95 memset(pg->addr, 0, CFS_PAGE_SIZE);
97 cfs_atomic_inc(&libcfs_total_pages);
100 cfs_mem_cache_free(cfs_page_t_slab, pg);
109 * To free the cfs_page_t including the page
112 * pg: pointer to the cfs_page_t strcture
120 void cfs_free_page(cfs_page_t *pg)
123 ASSERT(pg->addr != NULL);
124 ASSERT(cfs_atomic_read(&pg->count) <= 1);
126 if (!test_bit(PG_virt, &pg->flags)) {
127 cfs_mem_cache_free(cfs_page_p_slab, pg->addr);
128 cfs_atomic_dec(&libcfs_total_pages);
130 cfs_enter_debugger();
132 cfs_mem_cache_free(cfs_page_t_slab, pg);
135 int cfs_mem_is_in_cache(const void *addr, const cfs_mem_cache_t *kmem)
137 KdPrint(("cfs_mem_is_in_cache: not implemented. (should maintain a"
138 "chain to keep all allocations traced.)\n"));
144 * To allocate memory from system pool
147 * nr_bytes: length in bytes of the requested buffer
148 * flags: flags indiction
151 * NULL: if there's no enough memory space in system
152 * the address of the allocated memory in success.
155 * This operation can be treated as atomic.
159 cfs_alloc(size_t nr_bytes, u_int32_t flags)
163 /* Ignore the flags: always allcoate from NonPagedPool */
164 ptr = ExAllocatePoolWithTag(NonPagedPool, nr_bytes, 'Lufs');
165 if (ptr != NULL && (flags & CFS_ALLOC_ZERO)) {
166 memset(ptr, 0, nr_bytes);
170 cfs_enter_debugger();
178 * To free the sepcified memory to system pool
181 * addr: pointer to the buffer to be freed
187 * This operation can be treated as atomic.
198 * To allocate large block of memory from system pool
201 * nr_bytes: length in bytes of the requested buffer
204 * NULL: if there's no enough memory space in system
205 * the address of the allocated memory in success.
212 cfs_alloc_large(size_t nr_bytes)
214 return cfs_alloc(nr_bytes, 0);
219 * To free the sepcified memory to system pool
222 * addr: pointer to the buffer to be freed
232 cfs_free_large(void *addr)
239 * cfs_mem_cache_create
240 * To create a SLAB cache
243 * name: name string of the SLAB cache to be created
244 * size: size in bytes of SLAB entry buffer
245 * offset: offset in the page
246 * flags: SLAB creation flags
249 * The poitner of cfs_memory_cache structure in success.
250 * NULL pointer in failure case.
253 * 1, offset won't be used here.
254 * 2, it could be better to induce a lock to protect the access of the
255 * SLAB structure on SMP if there's not outside lock protection.
256 * 3, parameters C/D are removed.
260 cfs_mem_cache_create(
267 cfs_mem_cache_t * kmc = NULL;
269 /* The name of the SLAB could not exceed 20 chars */
271 if (name && strlen(name) >= 20) {
275 /* Allocate and initialize the SLAB strcture */
277 kmc = cfs_alloc (sizeof(cfs_mem_cache_t), 0);
283 memset(kmc, 0, sizeof(cfs_mem_cache_t));
287 strcpy(&kmc->name[0], name);
290 /* Initialize the corresponding LookAside list */
292 ExInitializeNPagedLookasideList(
307 * cfs_mem_cache_destroy
308 * To destroy the unused SLAB cache
311 * kmc: the SLAB cache to be destroied.
314 * 0: in success case.
315 * 1: in failure case.
321 int cfs_mem_cache_destroy (cfs_mem_cache_t * kmc)
325 ExDeleteNPagedLookasideList(&(kmc->npll));
333 * cfs_mem_cache_alloc
334 * To allocate an object (LookAside entry) from the SLAB
337 * kmc: the SLAB cache to be allocated from.
338 * flags: flags for allocation options
341 * object buffer address: in success case.
342 * NULL: in failure case.
348 void *cfs_mem_cache_alloc(cfs_mem_cache_t * kmc, int flags)
352 buf = ExAllocateFromNPagedLookasideList(&(kmc->npll));
359 * To free an object (LookAside entry) to the SLAB cache
362 * kmc: the SLAB cache to be freed to.
363 * buf: the pointer to the object to be freed.
372 void cfs_mem_cache_free(cfs_mem_cache_t * kmc, void * buf)
374 ExFreeToNPagedLookasideList(&(kmc->npll), buf);
377 spinlock_t shrinker_guard = {0};
378 CFS_LIST_HEAD(shrinker_hdr);
379 cfs_timer_t shrinker_timer = {0};
381 struct cfs_shrinker * cfs_set_shrinker(int seeks, shrink_callback cb)
383 struct cfs_shrinker * s = (struct cfs_shrinker *)
384 cfs_alloc(sizeof(struct cfs_shrinker), CFS_ALLOC_ZERO);
389 spin_lock(&shrinker_guard);
390 cfs_list_add(&s->list, &shrinker_hdr);
391 spin_unlock(&shrinker_guard);
397 void cfs_remove_shrinker(struct cfs_shrinker *s)
399 struct cfs_shrinker *tmp;
400 spin_lock(&shrinker_guard);
402 cfs_list_for_each_entry_typed(tmp, &shrinker_hdr,
403 struct cfs_shrinker, list) {
405 cfs_list_del(&tmp->list);
410 cfs_list_del(&s->list);
412 spin_unlock(&shrinker_guard);
416 /* time ut test proc */
417 void shrinker_timer_proc(ulong_ptr_t arg)
419 struct cfs_shrinker *s;
420 spin_lock(&shrinker_guard);
422 cfs_list_for_each_entry_typed(s, &shrinker_hdr,
423 struct cfs_shrinker, list) {
424 s->cb(s->nr, __GFP_FS);
426 spin_unlock(&shrinker_guard);
427 cfs_timer_arm(&shrinker_timer, 300);
430 int start_shrinker_timer()
432 /* initialize shriner timer */
433 cfs_timer_init(&shrinker_timer, shrinker_timer_proc, NULL);
435 /* start the timer to trigger in 5 minutes */
436 cfs_timer_arm(&shrinker_timer, 300);
441 void stop_shrinker_timer()
443 /* cancel the timer */
444 cfs_timer_disarm(&shrinker_timer);
445 cfs_timer_done(&shrinker_timer);