Whamcloud - gitweb
1d0f03d3f4118eb0624241295f8c2d720eefdf30
[fs/lustre-release.git] / libcfs / include / libcfs / linux / linux-mem.h
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
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.
9  *
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).
15  *
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
19  *
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
22  * have any questions.
23  *
24  * GPL HEADER END
25  */
26 /*
27  * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
28  * Use is subject to license terms.
29  *
30  * Copyright (c) 2011, Whamcloud, Inc.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  *
36  * libcfs/include/libcfs/linux/linux-mem.h
37  *
38  * Basic library routines.
39  */
40
41 #ifndef __LIBCFS_LINUX_CFS_MEM_H__
42 #define __LIBCFS_LINUX_CFS_MEM_H__
43
44 #ifndef __LIBCFS_LIBCFS_H__
45 #error Do not #include this file directly. #include <libcfs/libcfs.h> instead
46 #endif
47
48 #ifndef __KERNEL__
49 #error This include is only for kernel use.
50 #endif
51
52 #include <linux/mm.h>
53 #include <linux/vmalloc.h>
54 #include <linux/pagemap.h>
55 #include <linux/slab.h>
56 #ifdef HAVE_MM_INLINE
57 # include <linux/mm_inline.h>
58 #endif
59
60 typedef struct page                     cfs_page_t;
61 #define CFS_PAGE_SIZE                   PAGE_CACHE_SIZE
62 #define CFS_PAGE_SHIFT                  PAGE_CACHE_SHIFT
63 #define CFS_PAGE_MASK                   (~((__u64)CFS_PAGE_SIZE-1))
64
65 #define cfs_num_physpages               num_physpages
66
67 #define cfs_copy_from_user(to, from, n) copy_from_user(to, from, n)
68 #define cfs_copy_to_user(to, from, n)   copy_to_user(to, from, n)
69
70 static inline void *cfs_page_address(cfs_page_t *page)
71 {
72         /*
73          * XXX nikita: do NOT call portals_debug_msg() (CDEBUG/ENTRY/EXIT)
74          * from here: this will lead to infinite recursion.
75          */
76         return page_address(page);
77 }
78
79 static inline void *cfs_kmap(cfs_page_t *page)
80 {
81         return kmap(page);
82 }
83
84 static inline void cfs_kunmap(cfs_page_t *page)
85 {
86         kunmap(page);
87 }
88
89 static inline void cfs_get_page(cfs_page_t *page)
90 {
91         get_page(page);
92 }
93
94 static inline int cfs_page_count(cfs_page_t *page)
95 {
96         return page_count(page);
97 }
98
99 #define cfs_page_index(p)       ((p)->index)
100
101 #define cfs_page_pin(page) page_cache_get(page)
102 #define cfs_page_unpin(page) page_cache_release(page)
103
104 /*
105  * Memory allocator
106  * XXX Liang: move these declare to public file
107  */
108 extern void *cfs_alloc(size_t nr_bytes, u_int32_t flags);
109 extern void  cfs_free(void *addr);
110
111 extern void *cfs_alloc_large(size_t nr_bytes);
112 extern void  cfs_free_large(void *addr);
113
114 extern cfs_page_t *cfs_alloc_page(unsigned int flags);
115 extern void cfs_free_page(cfs_page_t *page);
116
117 #define cfs_memory_pressure_get() (current->flags & PF_MEMALLOC)
118 #define cfs_memory_pressure_set() do { current->flags |= PF_MEMALLOC; } while (0)
119 #define cfs_memory_pressure_clr() do { current->flags &= ~PF_MEMALLOC; } while (0)
120
121 #if BITS_PER_LONG == 32
122 /* limit to lowmem on 32-bit systems */
123 #define CFS_NUM_CACHEPAGES \
124         min(cfs_num_physpages, 1UL << (30 - CFS_PAGE_SHIFT) * 3 / 4)
125 #else
126 #define CFS_NUM_CACHEPAGES cfs_num_physpages
127 #endif
128
129 /*
130  * In Linux there is no way to determine whether current execution context is
131  * blockable.
132  */
133 #define CFS_ALLOC_ATOMIC_TRY   CFS_ALLOC_ATOMIC
134
135 /*
136  * SLAB allocator
137  * XXX Liang: move these declare to public file
138  */
139 typedef struct kmem_cache cfs_mem_cache_t;
140 extern cfs_mem_cache_t * cfs_mem_cache_create (const char *, size_t, size_t, unsigned long);
141 extern int cfs_mem_cache_destroy ( cfs_mem_cache_t * );
142 extern void *cfs_mem_cache_alloc ( cfs_mem_cache_t *, int);
143 extern void cfs_mem_cache_free ( cfs_mem_cache_t *, void *);
144 extern int cfs_mem_is_in_cache(const void *addr, const cfs_mem_cache_t *kmem);
145
146 #define CFS_DECL_MMSPACE                mm_segment_t __oldfs
147 #define CFS_MMSPACE_OPEN \
148         do { __oldfs = get_fs(); set_fs(get_ds());} while(0)
149 #define CFS_MMSPACE_CLOSE               set_fs(__oldfs)
150
151 #define CFS_SLAB_HWCACHE_ALIGN          SLAB_HWCACHE_ALIGN
152 #define CFS_SLAB_KERNEL                 SLAB_KERNEL
153 #define CFS_SLAB_NOFS                   SLAB_NOFS
154
155 /*
156  * NUMA allocators
157  *
158  * NB: we will rename these functions in a separate patch:
159  * - rename cfs_alloc to cfs_malloc
160  * - rename cfs_alloc/free_page to cfs_page_alloc/free
161  * - rename cfs_alloc/free_large to cfs_vmalloc/vfree
162  */
163 extern void *cfs_cpt_malloc(struct cfs_cpt_table *cptab, int cpt,
164                             size_t nr_bytes, unsigned int flags);
165 extern void *cfs_cpt_vmalloc(struct cfs_cpt_table *cptab, int cpt,
166                              size_t nr_bytes);
167 extern cfs_page_t *cfs_page_cpt_alloc(struct cfs_cpt_table *cptab,
168                                       int cpt, unsigned int flags);
169 extern void *cfs_mem_cache_cpt_alloc(cfs_mem_cache_t *cachep,
170                                      struct cfs_cpt_table *cptab,
171                                      int cpt, unsigned int flags);
172
173 /*
174  * Shrinker
175  */
176 #define cfs_shrinker    shrinker
177
178 #ifdef HAVE_SHRINK_CONTROL
179 # define SHRINKER_ARGS(sc, nr_to_scan, gfp_mask)  \
180                        struct shrinker *shrinker, \
181                        struct shrink_control *sc
182 # define shrink_param(sc, var) ((sc)->var)
183 #else
184 # ifdef HAVE_SHRINKER_WANT_SHRINK_PTR
185 #  define SHRINKER_ARGS(sc, nr_to_scan, gfp_mask)  \
186                         struct shrinker *shrinker, \
187                         int nr_to_scan, gfp_t gfp_mask
188 # else
189 #  define SHRINKER_ARGS(sc, nr_to_scan, gfp_mask)  \
190                         int nr_to_scan, gfp_t gfp_mask
191 # endif
192 # define shrink_param(sc, var) (var)
193 #endif
194
195 #ifdef HAVE_REGISTER_SHRINKER
196 typedef int (*cfs_shrinker_t)(SHRINKER_ARGS(sc, nr_to_scan, gfp_mask));
197
198 static inline
199 struct cfs_shrinker *cfs_set_shrinker(int seek, cfs_shrinker_t func)
200 {
201         struct shrinker *s;
202
203         s = kmalloc(sizeof(*s), GFP_KERNEL);
204         if (s == NULL)
205                 return (NULL);
206
207         s->shrink = func;
208         s->seeks = seek;
209
210         register_shrinker(s);
211
212         return s;
213 }
214
215 static inline
216 void cfs_remove_shrinker(struct cfs_shrinker *shrinker)
217 {
218         if (shrinker == NULL)
219                 return;
220
221         unregister_shrinker(shrinker);
222         kfree(shrinker);
223 }
224 #else
225 typedef shrinker_t              cfs_shrinker_t;
226 #define cfs_set_shrinker(s, f)  set_shrinker(s, f)
227 #define cfs_remove_shrinker(s)  remove_shrinker(s)
228 #endif
229
230 #define CFS_DEFAULT_SEEKS                 DEFAULT_SEEKS
231 #endif /* __LINUX_CFS_MEM_H__ */