Whamcloud - gitweb
b=22375 Add walk_stack callback for dump_trace.
[fs/lustre-release.git] / libcfs / libcfs / linux / linux-mem.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  * GPL HEADER START
5  *
6  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 only,
10  * as published by the Free Software Foundation.
11  *
12  * This program is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * General Public License version 2 for more details (a copy is included
16  * in the LICENSE file that accompanied this code).
17  *
18  * You should have received a copy of the GNU General Public License
19  * version 2 along with this program; If not, see
20  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
21  *
22  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23  * CA 95054 USA or visit www.sun.com if you need additional information or
24  * have any questions.
25  *
26  * GPL HEADER END
27  */
28 /*
29  * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
30  * Use is subject to license terms.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  */
36 #define DEBUG_SUBSYSTEM S_LNET
37
38 #include <linux/mm.h>
39 #include <linux/vmalloc.h>
40 #include <linux/slab.h>
41 #include <linux/highmem.h>
42 #include <libcfs/libcfs.h>
43
44 static unsigned int cfs_alloc_flags_to_gfp(u_int32_t flags)
45 {
46         unsigned int mflags = 0;
47
48         if (flags & CFS_ALLOC_ATOMIC)
49                 mflags |= __GFP_HIGH;
50         else
51                 mflags |= __GFP_WAIT;
52         if (flags & CFS_ALLOC_NOWARN)
53                 mflags |= __GFP_NOWARN;
54         if (flags & CFS_ALLOC_IO)
55                 mflags |= __GFP_IO;
56         if (flags & CFS_ALLOC_FS)
57                 mflags |= __GFP_FS;
58         if (flags & CFS_ALLOC_HIGH)
59                 mflags |= __GFP_HIGH;
60         return mflags;
61 }
62
63 void *
64 cfs_alloc(size_t nr_bytes, u_int32_t flags)
65 {
66         void *ptr = NULL;
67
68         ptr = kmalloc(nr_bytes, cfs_alloc_flags_to_gfp(flags));
69         if (ptr != NULL && (flags & CFS_ALLOC_ZERO))
70                 memset(ptr, 0, nr_bytes);
71         return ptr;
72 }
73
74 void
75 cfs_free(void *addr)
76 {
77         kfree(addr);
78 }
79
80 void *
81 cfs_alloc_large(size_t nr_bytes)
82 {
83         return vmalloc(nr_bytes);
84 }
85
86 void
87 cfs_free_large(void *addr)
88 {
89         vfree(addr);
90 }
91
92 cfs_page_t *cfs_alloc_page(unsigned int flags)
93 {
94         /*
95          * XXX nikita: do NOT call portals_debug_msg() (CDEBUG/ENTRY/EXIT)
96          * from here: this will lead to infinite recursion.
97          */
98         return alloc_page(cfs_alloc_flags_to_gfp(flags));
99 }
100
101 void cfs_free_page(cfs_page_t *page)
102 {
103         __free_page(page);
104 }
105
106 cfs_mem_cache_t *
107 cfs_mem_cache_create (const char *name, size_t size, size_t offset,
108                       unsigned long flags)
109 {
110 #ifdef HAVE_KMEM_CACHE_CREATE_DTOR
111         return kmem_cache_create(name, size, offset, flags, NULL, NULL);
112 #else
113         return kmem_cache_create(name, size, offset, flags, NULL);
114 #endif
115 }
116
117 int
118 cfs_mem_cache_destroy (cfs_mem_cache_t * cachep)
119 {
120 #ifdef HAVE_KMEM_CACHE_DESTROY_INT
121         return kmem_cache_destroy(cachep);
122 #else
123         kmem_cache_destroy(cachep);
124         return 0;
125 #endif
126 }
127
128 void *
129 cfs_mem_cache_alloc(cfs_mem_cache_t *cachep, int flags)
130 {
131         return kmem_cache_alloc(cachep, cfs_alloc_flags_to_gfp(flags));
132 }
133
134 void
135 cfs_mem_cache_free(cfs_mem_cache_t *cachep, void *objp)
136 {
137         return kmem_cache_free(cachep, objp);
138 }
139
140 /**
141  * Returns true if \a addr is an address of an allocated object in a slab \a
142  * kmem. Used in assertions. This check is optimistically imprecise, i.e., it
143  * occasionally returns true for the incorrect addresses, but if it returns
144  * false, then the addresses is guaranteed to be incorrect.
145  */
146 int cfs_mem_is_in_cache(const void *addr, const cfs_mem_cache_t *kmem)
147 {
148 #ifdef CONFIG_SLAB
149         struct page *page;
150
151         /*
152          * XXX Copy of mm/slab.c:virt_to_cache(). It won't work with other
153          * allocators, like slub and slob.
154          */
155         page = virt_to_page(addr);
156         if (unlikely(PageCompound(page)))
157                 page = (struct page *)page->private;
158         return PageSlab(page) && ((void *)page->lru.next) == kmem;
159 #else
160         return 1;
161 #endif
162 }
163 EXPORT_SYMBOL(cfs_mem_is_in_cache);
164
165
166 EXPORT_SYMBOL(cfs_alloc);
167 EXPORT_SYMBOL(cfs_free);
168 EXPORT_SYMBOL(cfs_alloc_large);
169 EXPORT_SYMBOL(cfs_free_large);
170 EXPORT_SYMBOL(cfs_alloc_page);
171 EXPORT_SYMBOL(cfs_free_page);
172 EXPORT_SYMBOL(cfs_mem_cache_create);
173 EXPORT_SYMBOL(cfs_mem_cache_destroy);
174 EXPORT_SYMBOL(cfs_mem_cache_alloc);
175 EXPORT_SYMBOL(cfs_mem_cache_free);