1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
4 * Copyright (C) 2002 Cluster File Systems, Inc.
5 * Author: Liang Zhen <liangzhen@clusterfs.com>
6 * Nikita Danilov <nikita@clusterfs.com>
8 * This file is part of Lustre, http://www.lustre.org.
10 * Lustre is free software; you can redistribute it and/or
11 * modify it under the terms of version 2 of the GNU General Public
12 * License as published by the Free Software Foundation.
14 * Lustre is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with Lustre; if not, write to the Free Software
21 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 * Darwin porting library
24 * Make things easy to port
26 #define DEBUG_SUBSYSTEM S_LNET
28 #include <mach/mach_types.h>
30 #include <sys/malloc.h>
32 #include <libcfs/libcfs.h>
33 #include "darwin-internal.h"
35 #if CFS_INDIVIDUAL_ZONE
36 extern zone_t zinit( vm_size_t, vm_size_t, vm_size_t, const char *);
37 extern void * zalloc(zone_t zone);
38 extern void *zalloc_noblock(zone_t zone);
39 extern void zfree(zone_t zone, void *addr);
42 struct list_head *z_nob; /* Pointer to z_link */
43 struct list_head z_link; /* Do NOT access it directly */
46 static struct cfs_zone_nob cfs_zone_nob;
47 static spinlock_t cfs_zone_guard;
49 cfs_mem_cache_t *mem_cache_find(const char *name, size_t objsize)
51 cfs_mem_cache_t *walker = NULL;
53 LASSERT(cfs_zone_nob.z_nob != NULL);
55 spin_lock(&cfs_zone_guard);
56 list_for_each_entry(walker, cfs_zone_nob.z_nob, mc_link) {
57 if (!strcmp(walker->mc_name, name) && \
58 walker->mc_size == objsize)
61 spin_unlock(&cfs_zone_guard);
67 * our wrapper around kern/zalloc.c:zinit()
69 * Creates copy of name and calls zinit() to do real work. Needed because zone
70 * survives kext unloading, so that @name cannot be just static string
71 * embedded into kext image.
73 cfs_mem_cache_t *mem_cache_create(vm_size_t objsize, const char *name)
75 cfs_mem_cache_t *mc = NULL;
78 MALLOC(mc, cfs_mem_cache_t *, sizeof(cfs_mem_cache_t), M_TEMP, M_WAITOK|M_ZERO);
80 CERROR("cfs_mem_cache created fail!\n");
84 cname = _MALLOC(strlen(name) + 1, M_TEMP, M_WAITOK);
85 LASSERT(cname != NULL);
86 mc->mc_cache = zinit(objsize, (KMEM_MAX_ZONE * objsize), 0, strcpy(cname, name));
87 mc->mc_size = objsize;
88 CFS_INIT_LIST_HEAD(&mc->mc_link);
89 strncpy(mc->mc_name, name, 1 + strlen(name));
93 void mem_cache_destroy(cfs_mem_cache_t *mc)
96 * zone can NOT be destroyed after creating,
97 * so just keep it in list.
99 * We will not lost a zone after we unload
100 * libcfs, it can be found by from libcfs.zone
105 #define mem_cache_alloc(mc) zalloc((mc)->mc_cache)
107 # define mem_cache_alloc_nb(mc) zalloc((mc)->mc_cache)
109 /* XXX Liang: Tiger doesn't export zalloc_noblock() */
110 # define mem_cache_alloc_nb(mc) zalloc_noblock((mc)->mc_cache)
112 #define mem_cache_free(mc, p) zfree((mc)->mc_cache, p)
114 #else /* !CFS_INDIVIDUAL_ZONE */
117 mem_cache_find(const char *name, size_t objsize)
122 cfs_mem_cache_t *mem_cache_create(vm_size_t size, const char *name)
124 cfs_mem_cache_t *mc = NULL;
126 MALLOC(mc, cfs_mem_cache_t *, sizeof(cfs_mem_cache_t), M_TEMP, M_WAITOK|M_ZERO);
128 CERROR("cfs_mem_cache created fail!\n");
131 mc->mc_cache = OSMalloc_Tagalloc(name, OSMT_DEFAULT);
136 void mem_cache_destroy(cfs_mem_cache_t *mc)
138 OSMalloc_Tagfree(mc->mc_cache);
142 #define mem_cache_alloc(mc) OSMalloc((mc)->mc_size, (mc)->mc_cache)
143 #define mem_cache_alloc_nb(mc) OSMalloc_noblock((mc)->mc_size, (mc)->mc_cache)
144 #define mem_cache_free(mc, p) OSFree(p, (mc)->mc_size, (mc)->mc_cache)
146 #endif /* !CFS_INDIVIDUAL_ZONE */
149 cfs_mem_cache_create (const char *name,
150 size_t objsize, size_t off, unsigned long arg1)
154 mc = mem_cache_find(name, objsize);
157 mc = mem_cache_create(objsize, name);
161 int cfs_mem_cache_destroy (cfs_mem_cache_t *cachep)
163 mem_cache_destroy(cachep);
167 void *cfs_mem_cache_alloc (cfs_mem_cache_t *cachep, int flags)
171 /* zalloc_canblock() is not exported... Emulate it. */
172 if (flags & CFS_ALLOC_ATOMIC) {
173 result = (void *)mem_cache_alloc_nb(cachep);
175 LASSERT(get_preemption_level() == 0);
176 result = (void *)mem_cache_alloc(cachep);
178 if (result != NULL && (flags & CFS_ALLOC_ZERO))
179 memset(result, 0, cachep->mc_size);
184 void cfs_mem_cache_free (cfs_mem_cache_t *cachep, void *objp)
186 mem_cache_free(cachep, objp);
189 /* ---------------------------------------------------------------------------
192 * --------------------------------------------------------------------------- */
198 static unsigned int raw_pages = 0;
199 static cfs_mem_cache_t *raw_page_cache = NULL;
201 static struct xnu_page_ops raw_page_ops;
202 static struct xnu_page_ops *page_ops[XNU_PAGE_NTYPES] = {
203 [XNU_PAGE_RAW] = &raw_page_ops
206 #if defined(LIBCFS_DEBUG)
207 static int page_type_is_valid(cfs_page_t *page)
209 LASSERT(page != NULL);
210 return 0 <= page->type && page->type < XNU_PAGE_NTYPES;
213 static int page_is_raw(cfs_page_t *page)
215 return page->type == XNU_PAGE_RAW;
219 static struct xnu_raw_page *as_raw(cfs_page_t *page)
221 LASSERT(page_is_raw(page));
222 return list_entry(page, struct xnu_raw_page, header);
225 static void *raw_page_address(cfs_page_t *pg)
227 return (void *)as_raw(pg)->virtual;
230 static void *raw_page_map(cfs_page_t *pg)
232 return (void *)as_raw(pg)->virtual;
235 static void raw_page_unmap(cfs_page_t *pg)
239 static struct xnu_page_ops raw_page_ops = {
240 .page_map = raw_page_map,
241 .page_unmap = raw_page_unmap,
242 .page_address = raw_page_address
245 extern int get_preemption_level(void);
247 struct list_head page_death_row;
248 spinlock_t page_death_row_phylax;
250 static void raw_page_finish(struct xnu_raw_page *pg)
253 if (pg->virtual != NULL)
254 cfs_mem_cache_free(raw_page_cache, pg->virtual);
258 void raw_page_death_row_clean(void)
260 struct xnu_raw_page *pg;
262 spin_lock(&page_death_row_phylax);
263 while (!list_empty(&page_death_row)) {
264 pg = container_of(page_death_row.next,
265 struct xnu_raw_page, link);
267 spin_unlock(&page_death_row_phylax);
269 spin_lock(&page_death_row_phylax);
271 spin_unlock(&page_death_row_phylax);
275 void free_raw_page(struct xnu_raw_page *pg)
277 if (!atomic_dec_and_test(&pg->count))
280 * kmem_free()->vm_map_remove()->vm_map_delete()->lock_write() may
281 * block. (raw_page_done()->upl_abort() can block too) On the other
282 * hand, cfs_free_page() may be called in non-blockable context. To
283 * work around this, park pages on global list when cannot block.
285 if (get_preemption_level() > 0) {
286 spin_lock(&page_death_row_phylax);
287 list_add(&pg->link, &page_death_row);
288 spin_unlock(&page_death_row_phylax);
291 raw_page_death_row_clean();
295 cfs_page_t *cfs_alloc_page(u_int32_t flags)
297 struct xnu_raw_page *page;
300 * XXX nikita: do NOT call libcfs_debug_msg() (CDEBUG/ENTRY/EXIT)
301 * from here: this will lead to infinite recursion.
304 page = cfs_alloc(sizeof *page, flags);
306 page->virtual = cfs_mem_cache_alloc(raw_page_cache, flags);
307 if (page->virtual != NULL) {
309 page->header.type = XNU_PAGE_RAW;
310 atomic_set(&page->count, 1);
316 return page != NULL ? &page->header : NULL;
319 void cfs_free_page(cfs_page_t *pages)
321 free_raw_page(as_raw(pages));
324 void cfs_get_page(cfs_page_t *p)
326 atomic_inc(&as_raw(p)->count);
329 int cfs_put_page_testzero(cfs_page_t *p)
331 return atomic_dec_and_test(&as_raw(p)->count);
334 int cfs_page_count(cfs_page_t *p)
336 return atomic_read(&as_raw(p)->count);
340 * Generic page operations
343 void *cfs_page_address(cfs_page_t *pg)
346 * XXX nikita: do NOT call libcfs_debug_msg() (CDEBUG/ENTRY/EXIT)
347 * from here: this will lead to infinite recursion.
349 LASSERT(page_type_is_valid(pg));
350 return page_ops[pg->type]->page_address(pg);
353 void *cfs_kmap(cfs_page_t *pg)
355 LASSERT(page_type_is_valid(pg));
356 return page_ops[pg->type]->page_map(pg);
359 void cfs_kunmap(cfs_page_t *pg)
361 LASSERT(page_type_is_valid(pg));
362 return page_ops[pg->type]->page_unmap(pg);
365 void xnu_page_ops_register(int type, struct xnu_page_ops *ops)
367 LASSERT(0 <= type && type < XNU_PAGE_NTYPES);
368 LASSERT(ops != NULL);
369 LASSERT(page_ops[type] == NULL);
371 page_ops[type] = ops;
374 void xnu_page_ops_unregister(int type)
376 LASSERT(0 <= type && type < XNU_PAGE_NTYPES);
377 LASSERT(page_ops[type] != NULL);
379 page_ops[type] = NULL;
383 * Portable memory allocator API
385 #ifdef HAVE_GET_PREEMPTION_LEVEL
386 extern int get_preemption_level(void);
388 #define get_preemption_level() (0)
391 void *cfs_alloc(size_t nr_bytes, u_int32_t flags)
396 if (flags & CFS_ALLOC_ATOMIC) {
399 LASSERT(get_preemption_level() == 0);
403 if (flags & CFS_ALLOC_ZERO)
406 return _MALLOC(nr_bytes, M_TEMP, mflags);
409 void cfs_free(void *addr)
411 return _FREE(addr, M_TEMP);
414 void *cfs_alloc_large(size_t nr_bytes)
416 LASSERT(get_preemption_level() == 0);
417 return _MALLOC(nr_bytes, M_TEMP, M_WAITOK);
420 void cfs_free_large(void *addr)
422 LASSERT(get_preemption_level() == 0);
423 return _FREE(addr, M_TEMP);
427 * Lookup cfs_zone_nob by sysctl.zone, if it cannot be
428 * found (first load of * libcfs since boot), allocate
429 * sysctl libcfs.zone.
431 int cfs_mem_init(void)
433 #if CFS_INDIVIDUAL_ZONE
437 len = sizeof(struct cfs_zone_nob);
438 rc = sysctlbyname("libcfs.zone",
439 (void *)&cfs_zone_nob, &len, NULL, 0);
441 /* zone_nob is not register in libcfs_sysctl */
442 struct cfs_zone_nob *nob;
443 struct sysctl_oid *oid;
445 assert(cfs_sysctl_isvalid());
447 nob = _MALLOC(sizeof(struct cfs_zone_nob),
448 M_TEMP, M_WAITOK | M_ZERO);
449 CFS_INIT_LIST_HEAD(&nob->z_link);
450 nob->z_nob = &nob->z_link;
451 oid = cfs_alloc_sysctl_struct(NULL, OID_AUTO, CTLFLAG_RD | CTLFLAG_KERN,
452 "zone", nob, sizeof(struct cfs_zone_nob));
457 sysctl_register_oid(oid);
459 cfs_zone_nob.z_nob = nob->z_nob;
461 spin_lock_init(&cfs_zone_guard);
463 CFS_INIT_LIST_HEAD(&page_death_row);
464 spin_lock_init(&page_death_row_phylax);
465 raw_page_cache = cfs_mem_cache_create("raw-page", CFS_PAGE_SIZE, 0, 0);
469 void cfs_mem_fini(void)
471 raw_page_death_row_clean();
472 spin_lock_done(&page_death_row_phylax);
473 cfs_mem_cache_destroy(raw_page_cache);
475 #if CFS_INDIVIDUAL_ZONE
476 cfs_zone_nob.z_nob = NULL;
477 spin_lock_done(&cfs_zone_guard);