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.
36 * libcfs/libcfs/darwin/darwin-mem.c
38 * Author: Liang Zhen <liangzhen@clusterfs.com>
39 * Author: Nikita Danilov <nikita@clusterfs.com>
41 #define DEBUG_SUBSYSTEM S_LNET
43 #include <mach/mach_types.h>
45 #include <sys/malloc.h>
47 #include <libcfs/libcfs.h>
48 #include "darwin-internal.h"
50 #if CFS_INDIVIDUAL_ZONE
51 extern zone_t zinit( vm_size_t, vm_size_t, vm_size_t, const char *);
52 extern void * zalloc(zone_t zone);
53 extern void *zalloc_noblock(zone_t zone);
54 extern void zfree(zone_t zone, void *addr);
57 struct list_head *z_nob; /* Pointer to z_link */
58 struct list_head z_link; /* Do NOT access it directly */
61 static struct cfs_zone_nob cfs_zone_nob;
62 static spinlock_t cfs_zone_guard;
64 struct kmem_cache *mem_cache_find(const char *name, size_t objsize)
66 struct kmem_cache *walker = NULL;
68 LASSERT(cfs_zone_nob.z_nob != NULL);
70 spin_lock(&cfs_zone_guard);
71 list_for_each_entry(walker, cfs_zone_nob.z_nob, mc_link) {
72 if (!strcmp(walker->mc_name, name) && \
73 walker->mc_size == objsize)
76 spin_unlock(&cfs_zone_guard);
82 * our wrapper around kern/zalloc.c:zinit()
84 * Creates copy of name and calls zinit() to do real work. Needed because zone
85 * survives kext unloading, so that @name cannot be just static string
86 * embedded into kext image.
88 struct kmem_cache *mem_cache_create(vm_size_t objsize, const char *name)
90 struct kmem_cache *mc = NULL;
93 MALLOC(mc, struct kmem_cache *, sizeof(struct kmem_cache), M_TEMP, M_WAITOK|M_ZERO);
95 CERROR("cfs_mem_cache created fail!\n");
99 cname = _MALLOC(strlen(name) + 1, M_TEMP, M_WAITOK);
100 LASSERT(cname != NULL);
101 mc->mc_cache = zinit(objsize, (KMEM_MAX_ZONE * objsize), 0, strcpy(cname, name));
102 mc->mc_size = objsize;
103 INIT_LIST_HEAD(&mc->mc_link);
104 strncpy(mc->mc_name, name, 1 + strlen(name));
108 void mem_cache_destroy(struct kmem_cache *mc)
111 * zone can NOT be destroyed after creating,
112 * so just keep it in list.
114 * We will not lost a zone after we unload
115 * libcfs, it can be found by from libcfs.zone
120 #define mem_cache_alloc(mc) zalloc((mc)->mc_cache)
122 # define mem_cache_alloc_nb(mc) zalloc((mc)->mc_cache)
124 /* XXX Liang: Tiger doesn't export zalloc_noblock() */
125 # define mem_cache_alloc_nb(mc) zalloc_noblock((mc)->mc_cache)
127 #define mem_cache_free(mc, p) zfree((mc)->mc_cache, p)
129 #else /* !CFS_INDIVIDUAL_ZONE */
132 mem_cache_find(const char *name, size_t objsize)
137 struct kmem_cache *mem_cache_create(vm_size_t size, const char *name)
139 struct kmem_cache *mc = NULL;
141 MALLOC(mc, struct kmem_cache *, sizeof(struct kmem_cache), M_TEMP, M_WAITOK|M_ZERO);
143 CERROR("cfs_mem_cache created fail!\n");
146 mc->mc_cache = OSMalloc_Tagalloc(name, OSMT_DEFAULT);
151 void mem_cache_destroy(struct kmem_cache *mc)
153 OSMalloc_Tagfree(mc->mc_cache);
157 #define mem_cache_alloc(mc) OSMalloc((mc)->mc_size, (mc)->mc_cache)
158 #define mem_cache_alloc_nb(mc) OSMalloc_noblock((mc)->mc_size, (mc)->mc_cache)
159 #define mem_cache_free(mc, p) OSFree(p, (mc)->mc_size, (mc)->mc_cache)
161 #endif /* !CFS_INDIVIDUAL_ZONE */
164 kmem_cache_create(const char *name, size_t objsize, size_t off,
165 unsigned long arg1, void *ctro)
167 struct kmem_cache *mc;
169 mc = mem_cache_find(name, objsize);
172 mc = mem_cache_create(objsize, name);
176 kmem_cache_destroy (struct kmem_cache *cachep)
178 mem_cache_destroy(cachep);
182 void *kmem_cache_alloc (struct kmem_cache *cachep, int flags)
186 /* zalloc_canblock() is not exported... Emulate it. */
187 if (flags & GFP_ATOMIC) {
188 result = (void *)mem_cache_alloc_nb(cachep);
190 LASSERT(get_preemption_level() == 0);
191 result = (void *)mem_cache_alloc(cachep);
193 if (result != NULL && (flags & __GFP_ZERO))
194 memset(result, 0, cachep->mc_size);
199 void kmem_cache_free (struct kmem_cache *cachep, void *objp)
201 mem_cache_free(cachep, objp);
204 /* ---------------------------------------------------------------------------
207 * --------------------------------------------------------------------------- */
213 static unsigned int raw_pages;
214 static struct kmem_cache *raw_page_cache;
216 static struct xnu_page_ops raw_page_ops;
217 static struct xnu_page_ops *page_ops[XNU_PAGE_NTYPES] = {
218 [XNU_PAGE_RAW] = &raw_page_ops
221 #if defined(LIBCFS_DEBUG)
222 static int page_type_is_valid(struct page *page)
224 LASSERT(page != NULL);
225 return 0 <= page->type && page->type < XNU_PAGE_NTYPES;
228 static int page_is_raw(struct page *page)
230 return page->type == XNU_PAGE_RAW;
234 static struct xnu_raw_page *as_raw(struct page *page)
236 LASSERT(page_is_raw(page));
237 return list_entry(page, struct xnu_raw_page, header);
240 static void *raw_page_address(struct page *pg)
242 return (void *)as_raw(pg)->virtual;
245 static void *raw_page_map(struct page *pg)
247 return (void *)as_raw(pg)->virtual;
250 static void raw_page_unmap(struct page *pg)
254 static struct xnu_page_ops raw_page_ops = {
255 .page_map = raw_page_map,
256 .page_unmap = raw_page_unmap,
257 .page_address = raw_page_address
260 extern int get_preemption_level(void);
262 struct list_head page_death_row;
263 spinlock_t page_death_row_phylax;
265 static void raw_page_finish(struct xnu_raw_page *pg)
268 if (pg->virtual != NULL)
269 kmem_cache_free(raw_page_cache, pg->virtual);
273 void raw_page_death_row_clean(void)
275 struct xnu_raw_page *pg;
277 spin_lock(&page_death_row_phylax);
278 while (!list_empty(&page_death_row)) {
279 pg = container_of(page_death_row.next,
280 struct xnu_raw_page, link);
282 spin_unlock(&page_death_row_phylax);
284 spin_lock(&page_death_row_phylax);
286 spin_unlock(&page_death_row_phylax);
290 void free_raw_page(struct xnu_raw_page *pg)
292 if (!atomic_dec_and_test(&pg->count))
295 * kmem_free()->vm_map_remove()->vm_map_delete()->lock_write() may
296 * block. (raw_page_done()->upl_abort() can block too) On the other
297 * hand, __free_page() may be called in non-blockable context. To
298 * work around this, park pages on global list when cannot block.
300 if (get_preemption_level() > 0) {
301 spin_lock(&page_death_row_phylax);
302 list_add(&pg->link, &page_death_row);
303 spin_unlock(&page_death_row_phylax);
306 raw_page_death_row_clean();
310 struct page *alloc_page(u_int32_t flags)
312 struct xnu_raw_page *page;
315 * XXX nikita: do NOT call libcfs_debug_msg() (CDEBUG/ENTRY/EXIT)
316 * from here: this will lead to infinite recursion.
319 page = kmalloc(sizeof *page, flags);
321 page->virtual = kmem_cache_alloc(raw_page_cache, flags);
322 if (page->virtual != NULL) {
324 page->header.type = XNU_PAGE_RAW;
325 atomic_set(&page->count, 1);
331 return page != NULL ? &page->header : NULL;
334 void __free_page(struct page *pages)
336 free_raw_page(as_raw(pages));
339 void get_page(struct page *p)
341 atomic_inc(&as_raw(p)->count);
344 int cfs_put_page_testzero(struct page *p)
346 return atomic_dec_and_test(&as_raw(p)->count);
349 int page_count(struct page *p)
351 return atomic_read(&as_raw(p)->count);
355 * Generic page operations
358 void *page_address(struct page *pg)
361 * XXX nikita: do NOT call libcfs_debug_msg() (CDEBUG/ENTRY/EXIT)
362 * from here: this will lead to infinite recursion.
364 LASSERT(page_type_is_valid(pg));
365 return page_ops[pg->type]->page_address(pg);
368 void *kmap(struct page *pg)
370 LASSERT(page_type_is_valid(pg));
371 return page_ops[pg->type]->page_map(pg);
374 void kunmap(struct page *pg)
376 LASSERT(page_type_is_valid(pg));
377 page_ops[pg->type]->page_unmap(pg);
380 void xnu_page_ops_register(int type, struct xnu_page_ops *ops)
382 LASSERT(0 <= type && type < XNU_PAGE_NTYPES);
383 LASSERT(ops != NULL);
384 LASSERT(page_ops[type] == NULL);
386 page_ops[type] = ops;
389 void xnu_page_ops_unregister(int type)
391 LASSERT(0 <= type && type < XNU_PAGE_NTYPES);
392 LASSERT(page_ops[type] != NULL);
394 page_ops[type] = NULL;
398 * Portable memory allocator API
400 #ifdef HAVE_GET_PREEMPTION_LEVEL
401 extern int get_preemption_level(void);
403 #define get_preemption_level() (0)
406 void *kmalloc(size_t nr_bytes, u_int32_t flags)
411 if (flags & GFP_ATOMIC) {
414 LASSERT(get_preemption_level() == 0);
418 if (flags & __GFP_ZERO)
421 return _MALLOC(nr_bytes, M_TEMP, mflags);
424 void kfree(void *addr)
426 return _FREE(addr, M_TEMP);
429 void *vmalloc(size_t nr_bytes)
431 LASSERT(get_preemption_level() == 0);
432 return _MALLOC(nr_bytes, M_TEMP, M_WAITOK);
435 void vfree(void *addr)
437 LASSERT(get_preemption_level() == 0);
438 return _FREE(addr, M_TEMP);
442 * Lookup cfs_zone_nob by sysctl.zone, if it cannot be
443 * found (first load of * libcfs since boot), allocate
444 * sysctl libcfs.zone.
446 int cfs_mem_init(void)
448 #if CFS_INDIVIDUAL_ZONE
452 len = sizeof(struct cfs_zone_nob);
453 rc = sysctlbyname("libcfs.zone",
454 (void *)&cfs_zone_nob, &len, NULL, 0);
456 /* zone_nob is not register in libcfs_sysctl */
457 struct cfs_zone_nob *nob;
458 struct sysctl_oid *oid;
460 assert(cfs_sysctl_isvalid());
462 nob = _MALLOC(sizeof(struct cfs_zone_nob),
463 M_TEMP, M_WAITOK | M_ZERO);
464 INIT_LIST_HEAD(&nob->z_link);
465 nob->z_nob = &nob->z_link;
466 oid = cfs_alloc_sysctl_struct(NULL, OID_AUTO, CTLFLAG_RD | CTLFLAG_KERN,
467 "zone", nob, sizeof(struct cfs_zone_nob));
472 sysctl_register_oid(oid);
474 cfs_zone_nob.z_nob = nob->z_nob;
476 spin_lock_init(&cfs_zone_guard);
478 INIT_LIST_HEAD(&page_death_row);
479 spin_lock_init(&page_death_row_phylax);
480 raw_page_cache = kmem_cache_create("raw-page", PAGE_CACHE_SIZE,
485 void cfs_mem_fini(void)
487 raw_page_death_row_clean();
488 spin_lock_done(&page_death_row_phylax);
489 kmem_cache_destroy(raw_page_cache);
491 #if CFS_INDIVIDUAL_ZONE
492 cfs_zone_nob.z_nob = NULL;
493 spin_lock_done(&cfs_zone_guard);