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.
30 * Copyright (c) 2012, Intel Corporation.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
37 #define DEBUG_SUBSYSTEM S_LNET
39 #include <libcfs/libcfs.h>
42 cfs_mem_cache_t *cfs_page_t_slab = NULL;
43 cfs_mem_cache_t *cfs_page_p_slab = NULL;
45 cfs_page_t * virt_to_page(void * addr)
48 pg = cfs_mem_cache_alloc(cfs_page_t_slab, 0);
55 memset(pg, 0, sizeof(cfs_page_t));
56 pg->addr = (void *)((__u64)addr & (~((__u64)PAGE_SIZE-1)));
58 cfs_atomic_set(&pg->count, 1);
59 set_bit(PG_virt, &(pg->flags));
66 * To allocate the cfs_page_t and also 1 page of memory
69 * flags: the allocation options
72 * pointer to the cfs_page_t strcture in success or
73 * NULL in failure case
79 cfs_atomic_t libcfs_total_pages;
81 cfs_page_t * cfs_alloc_page(int flags)
84 pg = cfs_mem_cache_alloc(cfs_page_t_slab, 0);
91 memset(pg, 0, sizeof(cfs_page_t));
92 pg->addr = cfs_mem_cache_alloc(cfs_page_p_slab, 0);
93 cfs_atomic_set(&pg->count, 1);
96 if (cfs_is_flag_set(flags, CFS_ALLOC_ZERO)) {
97 memset(pg->addr, 0, CFS_PAGE_SIZE);
99 cfs_atomic_inc(&libcfs_total_pages);
101 cfs_enter_debugger();
102 cfs_mem_cache_free(cfs_page_t_slab, pg);
111 * To free the cfs_page_t including the page
114 * pg: pointer to the cfs_page_t strcture
122 void cfs_free_page(cfs_page_t *pg)
125 ASSERT(pg->addr != NULL);
126 ASSERT(cfs_atomic_read(&pg->count) <= 1);
128 if (!test_bit(PG_virt, &pg->flags)) {
129 cfs_mem_cache_free(cfs_page_p_slab, pg->addr);
130 cfs_atomic_dec(&libcfs_total_pages);
132 cfs_enter_debugger();
134 cfs_mem_cache_free(cfs_page_t_slab, pg);
137 int cfs_mem_is_in_cache(const void *addr, const cfs_mem_cache_t *kmem)
139 KdPrint(("cfs_mem_is_in_cache: not implemented. (should maintain a"
140 "chain to keep all allocations traced.)\n"));
146 * To allocate memory from system pool
149 * nr_bytes: length in bytes of the requested buffer
150 * flags: flags indiction
153 * NULL: if there's no enough memory space in system
154 * the address of the allocated memory in success.
157 * This operation can be treated as atomic.
161 cfs_alloc(size_t nr_bytes, u_int32_t flags)
165 /* Ignore the flags: always allcoate from NonPagedPool */
166 ptr = ExAllocatePoolWithTag(NonPagedPool, nr_bytes, 'Lufs');
167 if (ptr != NULL && (flags & CFS_ALLOC_ZERO)) {
168 memset(ptr, 0, nr_bytes);
172 cfs_enter_debugger();
180 * To free the sepcified memory to system pool
183 * addr: pointer to the buffer to be freed
189 * This operation can be treated as atomic.
200 * To allocate large block of memory from system pool
203 * nr_bytes: length in bytes of the requested buffer
206 * NULL: if there's no enough memory space in system
207 * the address of the allocated memory in success.
214 cfs_alloc_large(size_t nr_bytes)
216 return cfs_alloc(nr_bytes, 0);
221 * To free the sepcified memory to system pool
224 * addr: pointer to the buffer to be freed
234 cfs_free_large(void *addr)
241 * cfs_mem_cache_create
242 * To create a SLAB cache
245 * name: name string of the SLAB cache to be created
246 * size: size in bytes of SLAB entry buffer
247 * offset: offset in the page
248 * flags: SLAB creation flags
251 * The poitner of cfs_memory_cache structure in success.
252 * NULL pointer in failure case.
255 * 1, offset won't be used here.
256 * 2, it could be better to induce a lock to protect the access of the
257 * SLAB structure on SMP if there's not outside lock protection.
258 * 3, parameters C/D are removed.
262 cfs_mem_cache_create(
269 cfs_mem_cache_t * kmc = NULL;
271 /* The name of the SLAB could not exceed 20 chars */
273 if (name && strlen(name) >= 20) {
277 /* Allocate and initialize the SLAB strcture */
279 kmc = cfs_alloc (sizeof(cfs_mem_cache_t), 0);
285 memset(kmc, 0, sizeof(cfs_mem_cache_t));
289 strcpy(&kmc->name[0], name);
292 /* Initialize the corresponding LookAside list */
294 ExInitializeNPagedLookasideList(
309 * cfs_mem_cache_destroy
310 * To destroy the unused SLAB cache
313 * kmc: the SLAB cache to be destroied.
316 * 0: in success case.
317 * 1: in failure case.
323 int cfs_mem_cache_destroy (cfs_mem_cache_t * kmc)
327 ExDeleteNPagedLookasideList(&(kmc->npll));
335 * cfs_mem_cache_alloc
336 * To allocate an object (LookAside entry) from the SLAB
339 * kmc: the SLAB cache to be allocated from.
340 * flags: flags for allocation options
343 * object buffer address: in success case.
344 * NULL: in failure case.
350 void *cfs_mem_cache_alloc(cfs_mem_cache_t * kmc, int flags)
354 buf = ExAllocateFromNPagedLookasideList(&(kmc->npll));
361 * To free an object (LookAside entry) to the SLAB cache
364 * kmc: the SLAB cache to be freed to.
365 * buf: the pointer to the object to be freed.
374 void cfs_mem_cache_free(cfs_mem_cache_t * kmc, void * buf)
376 ExFreeToNPagedLookasideList(&(kmc->npll), buf);
379 spinlock_t shrinker_guard = {0};
380 CFS_LIST_HEAD(shrinker_hdr);
381 cfs_timer_t shrinker_timer = {0};
383 struct cfs_shrinker * cfs_set_shrinker(int seeks, shrink_callback cb)
385 struct cfs_shrinker * s = (struct cfs_shrinker *)
386 cfs_alloc(sizeof(struct cfs_shrinker), CFS_ALLOC_ZERO);
391 spin_lock(&shrinker_guard);
392 cfs_list_add(&s->list, &shrinker_hdr);
393 spin_unlock(&shrinker_guard);
399 void cfs_remove_shrinker(struct cfs_shrinker *s)
401 struct cfs_shrinker *tmp;
402 spin_lock(&shrinker_guard);
404 cfs_list_for_each_entry_typed(tmp, &shrinker_hdr,
405 struct cfs_shrinker, list) {
407 cfs_list_del(&tmp->list);
412 cfs_list_del(&s->list);
414 spin_unlock(&shrinker_guard);
418 /* time ut test proc */
419 void shrinker_timer_proc(ulong_ptr_t arg)
421 struct cfs_shrinker *s;
422 spin_lock(&shrinker_guard);
424 cfs_list_for_each_entry_typed(s, &shrinker_hdr,
425 struct cfs_shrinker, list) {
426 s->cb(s->nr, __GFP_FS);
428 spin_unlock(&shrinker_guard);
429 cfs_timer_arm(&shrinker_timer, 300);
432 int start_shrinker_timer()
434 /* initialize shriner timer */
435 cfs_timer_init(&shrinker_timer, shrinker_timer_proc, NULL);
437 /* start the timer to trigger in 5 minutes */
438 cfs_timer_arm(&shrinker_timer, 300);
443 void stop_shrinker_timer()
445 /* cancel the timer */
446 cfs_timer_disarm(&shrinker_timer);
447 cfs_timer_done(&shrinker_timer);