Whamcloud - gitweb
LU-17705 ptlrpc: replace synchronize_rcu() with rcu_barrier()
[fs/lustre-release.git] / libcfs / include / libcfs / libcfs_private.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.gnu.org/licenses/gpl-2.0.html
19  *
20  * GPL HEADER END
21  */
22 /*
23  * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Use is subject to license terms.
25  *
26  * Copyright (c) 2011, 2014, Intel Corporation.
27  */
28 /*
29  * This file is part of Lustre, http://www.lustre.org/
30  * Lustre is a trademark of Sun Microsystems, Inc.
31  *
32  * libcfs/include/libcfs/libcfs_private.h
33  *
34  * Various defines for libcfs.
35  *
36  */
37
38 #ifndef __LIBCFS_PRIVATE_H__
39 #define __LIBCFS_PRIVATE_H__
40
41 #ifndef DEBUG_SUBSYSTEM
42 # define DEBUG_SUBSYSTEM S_UNDEFINED
43 #endif
44
45 #include <linux/slab.h>
46 #include <linux/vmalloc.h>
47
48 #ifdef LIBCFS_DEBUG
49
50 /*
51  * When this is on, LASSERT macro includes check for assignment used instead
52  * of equality check, but doesn't have unlikely(). Turn this on from time to
53  * time to make test-builds. This shouldn't be on for production release.
54  */
55 #define LASSERT_CHECKED (0)
56
57 #if LASSERT_CHECKED
58 /*
59  * Assertion.
60  *
61  * Strange construction with empty "then" clause is used to trigger compiler
62  * warnings on the assertions of the form LASSERT(a = b);
63  *
64  * "warning: suggest parentheses around assignment used as truth value"
65  *
66  * requires -Wall. Unfortunately this rules out use of likely/unlikely.
67  */
68 #define LASSERTF(cond, fmt, ...)                                        \
69 do {                                                                    \
70         if (cond)                                                       \
71                 ;                                                       \
72         else {                                                          \
73                 LIBCFS_DEBUG_MSG_DATA_DECL(__msg_data, D_EMERG, NULL);  \
74                 libcfs_debug_msg(&__msg_data,                           \
75                                  "ASSERTION( %s ) failed: " fmt, #cond, \
76                                  ## __VA_ARGS__);                       \
77                 lbug_with_loc(&__msg_data);                             \
78         }                                                               \
79 } while (0)
80
81 #define LASSERT(cond) LASSERTF(cond, "\n")
82
83 #else /* !LASSERT_CHECKED */
84
85 #define LASSERTF(cond, fmt, ...)                                        \
86 do {                                                                    \
87         if (unlikely(!(cond))) {                                        \
88                 LIBCFS_DEBUG_MSG_DATA_DECL(__msg_data, D_EMERG, NULL);  \
89                 libcfs_debug_msg(&__msg_data,                           \
90                                  "ASSERTION( %s ) failed: " fmt, #cond, \
91                                  ## __VA_ARGS__);                       \
92                 lbug_with_loc(&__msg_data);                             \
93         }                                                               \
94 } while (0)
95
96 #define LASSERT(cond) LASSERTF(cond, "\n")
97 #endif /* !LASSERT_CHECKED */
98 #else /* !LIBCFS_DEBUG */
99 /* sizeof is to use expression without evaluating it. */
100 # define LASSERT(e) ((void)sizeof!!(e))
101 # define LASSERTF(cond, ...) ((void)sizeof!!(cond))
102 #endif /* !LIBCFS_DEBUG */
103
104 #ifdef CONFIG_LUSTRE_DEBUG_EXPENSIVE_CHECK
105 /**
106  * This is for more expensive checks that one doesn't want to be enabled all
107  * the time. LINVRNT() has to be explicitly enabled by --enable-invariants
108  * configure option.
109  */
110 # define LINVRNT(exp) LASSERT(exp)
111 #else
112 # define LINVRNT(exp) ((void)sizeof!!(exp))
113 #endif
114
115 void __noreturn lbug_with_loc(struct libcfs_debug_msg_data *msg);
116
117 #define LBUG()                                                          \
118 do {                                                                    \
119         LIBCFS_DEBUG_MSG_DATA_DECL(msgdata, D_EMERG, NULL);             \
120         lbug_with_loc(&msgdata);                                        \
121 } while(0)
122
123 /*
124  * Memory
125  */
126 #ifdef LIBCFS_DEBUG
127
128 extern atomic_t libcfs_kmemory;
129
130 # define libcfs_kmem_inc(ptr, size)             \
131 do {                                            \
132         atomic_add(size, &libcfs_kmemory);      \
133 } while (0)
134
135 # define libcfs_kmem_dec(ptr, size)             \
136 do {                                            \
137         atomic_sub(size, &libcfs_kmemory);      \
138 } while (0)
139
140 # define libcfs_kmem_read()                     \
141         atomic_read(&libcfs_kmemory)
142
143 #else
144 # define libcfs_kmem_inc(ptr, size) do {} while (0)
145 # define libcfs_kmem_dec(ptr, size) do {} while (0)
146 # define libcfs_kmem_read()     (0)
147 #endif /* LIBCFS_DEBUG */
148
149 #ifndef LIBCFS_VMALLOC_SIZE
150 #define LIBCFS_VMALLOC_SIZE        (2 << PAGE_SHIFT) /* 2 pages */
151 #endif
152
153 #define LIBCFS_ALLOC_PRE(size, mask)                                        \
154 do {                                                                        \
155         LASSERT(!in_interrupt() ||                                          \
156                 ((size) <= LIBCFS_VMALLOC_SIZE &&                           \
157                  ((mask) & GFP_ATOMIC)) != 0);                      \
158 } while (0)
159
160 #define LIBCFS_ALLOC_POST(ptr, size)                                        \
161 do {                                                                        \
162         if (unlikely((ptr) == NULL)) {                                      \
163                 CERROR("LNET: out of memory at %s:%d (tried to alloc '"     \
164                        #ptr "' = %d)\n", __FILE__, __LINE__, (int)(size));  \
165                 CERROR("LNET: %d total bytes allocated by lnet\n",          \
166                        libcfs_kmem_read());                                 \
167         } else {                                                            \
168                 libcfs_kmem_inc((ptr), (size));                             \
169                 CDEBUG(D_MALLOC, "alloc '" #ptr "': %d at %p (tot %d).\n",  \
170                        (int)(size), (ptr), libcfs_kmem_read());             \
171         }                                                                   \
172 } while (0)
173
174 /**
175  * allocate memory with GFP flags @mask
176  * The allocated memory is zeroed-out.
177  */
178 #define LIBCFS_ALLOC_GFP(ptr, size, mask)                                   \
179 do {                                                                        \
180         LIBCFS_ALLOC_PRE((size), (mask));                                   \
181         (ptr) = (size) <= LIBCFS_VMALLOC_SIZE ?                             \
182                 kzalloc((size), (mask)) : vzalloc(size);                    \
183         LIBCFS_ALLOC_POST((ptr), (size));                                   \
184 } while (0)
185
186 /**
187  * default allocator
188  */
189 #define LIBCFS_ALLOC(ptr, size) \
190         LIBCFS_ALLOC_GFP(ptr, size, GFP_NOFS)
191
192 /**
193  * non-sleeping allocator
194  */
195 #define LIBCFS_ALLOC_ATOMIC(ptr, size) \
196         LIBCFS_ALLOC_GFP(ptr, size, GFP_ATOMIC)
197
198 /**
199  * allocate memory for specified CPU partition
200  *   \a cptab != NULL, \a cpt is CPU partition id of \a cptab
201  *   \a cptab == NULL, \a cpt is HW NUMA node id
202  * The allocated memory is zeroed-out.
203  */
204 #define LIBCFS_CPT_ALLOC_GFP(ptr, cptab, cpt, size, mask)                   \
205 do {                                                                        \
206         LIBCFS_ALLOC_PRE((size), (mask));                                   \
207         (ptr) = (size) <= LIBCFS_VMALLOC_SIZE ?                             \
208                 cfs_cpt_malloc((cptab), (cpt), (size), (mask) | __GFP_ZERO) : \
209                 cfs_cpt_vzalloc((cptab), (cpt), (size));                    \
210         LIBCFS_ALLOC_POST((ptr), (size));                                   \
211 } while (0)
212
213 /** default numa allocator */
214 #define LIBCFS_CPT_ALLOC(ptr, cptab, cpt, size)                             \
215         LIBCFS_CPT_ALLOC_GFP(ptr, cptab, cpt, size, GFP_NOFS)
216
217 #define LIBCFS_FREE(ptr, size)                                          \
218 do {                                                                    \
219         int s = (size);                                                 \
220         if (unlikely((ptr) == NULL)) {                                  \
221                 CERROR("LIBCFS: free NULL '" #ptr "' (%d bytes) at "    \
222                        "%s:%d\n", s, __FILE__, __LINE__);               \
223                 break;                                                  \
224         }                                                               \
225         libcfs_kmem_dec((ptr), s);                                      \
226         CDEBUG(D_MALLOC, "kfreed '" #ptr "': %d at %p (tot %d).\n",     \
227                s, (ptr), libcfs_kmem_read());                           \
228         if (unlikely(s > LIBCFS_VMALLOC_SIZE))                          \
229                 vfree(ptr);                                             \
230         else                                                            \
231                 kfree(ptr);                                             \
232 } while (0)
233
234 /******************************************************************************/
235
236 void libcfs_debug_dumplog(void);
237 int libcfs_debug_init(unsigned long bufsize);
238 int libcfs_debug_cleanup(void);
239 int libcfs_debug_clear_buffer(void);
240 int libcfs_debug_mark_buffer(const char *text);
241
242 /*
243  * allocate a variable array, returned value is an array of pointers.
244  * Caller can specify length of array by count.
245  */
246 void *cfs_array_alloc(int count, unsigned int size);
247 void  cfs_array_free(void *vars);
248
249 #define LASSERT_ATOMIC_ENABLED          (1)
250
251 #if LASSERT_ATOMIC_ENABLED
252
253 /** assert value of @a is equal to @v */
254 #define LASSERT_ATOMIC_EQ(a, v)                         \
255         LASSERTF(atomic_read(a) == v, "value: %d\n", atomic_read((a)));
256
257 /** assert value of @a is unequal to @v */
258 #define LASSERT_ATOMIC_NE(a, v)                         \
259         LASSERTF(atomic_read(a) != v, "value: %d\n", atomic_read((a)));
260
261 /** assert value of @a is little than @v */
262 #define LASSERT_ATOMIC_LT(a, v)                         \
263         LASSERTF(atomic_read(a) < v, "value: %d\n", atomic_read((a)));
264
265 /** assert value of @a is little/equal to @v */
266 #define LASSERT_ATOMIC_LE(a, v)                         \
267         LASSERTF(atomic_read(a) <= v, "value: %d\n", atomic_read((a)));
268
269 /** assert value of @a is great than @v */
270 #define LASSERT_ATOMIC_GT(a, v)                         \
271         LASSERTF(atomic_read(a) > v, "value: %d\n", atomic_read((a)));
272
273 /** assert value of @a is great/equal to @v */
274 #define LASSERT_ATOMIC_GE(a, v)                         \
275         LASSERTF(atomic_read(a) >= v, "value: %d\n", atomic_read((a)));
276
277 /** assert value of @a is great than @v1 and little than @v2 */
278 #define LASSERT_ATOMIC_GT_LT(a, v1, v2)                 \
279 do {                                                    \
280         int __v = atomic_read(a);                       \
281         LASSERTF(__v > v1 && __v < v2, "value: %d\n", __v);\
282 } while (0)
283
284 /** assert value of @a is great than @v1 and little/equal to @v2 */
285 #define LASSERT_ATOMIC_GT_LE(a, v1, v2)                 \
286 do {                                                    \
287         int __v = atomic_read(a);                       \
288         LASSERTF(__v > v1 && __v <= v2, "value: %d\n", __v);\
289 } while (0)
290
291 /** assert value of @a is great/equal to @v1 and little than @v2 */
292 #define LASSERT_ATOMIC_GE_LT(a, v1, v2)                 \
293 do {                                                    \
294         int __v = atomic_read(a);                       \
295         LASSERTF(__v >= v1 && __v < v2, "value: %d\n", __v);\
296 } while (0)
297
298 /** assert value of @a is great/equal to @v1 and little/equal to @v2 */
299 #define LASSERT_ATOMIC_GE_LE(a, v1, v2)                         \
300 do {                                                            \
301         int __v = atomic_read(a);                               \
302         LASSERTF(__v >= v1 && __v <= v2, "value: %d\n", __v);   \
303 } while (0)
304
305 #else /* !LASSERT_ATOMIC_ENABLED */
306
307 #define LASSERT_ATOMIC_EQ(a, v)                 do {} while (0)
308 #define LASSERT_ATOMIC_NE(a, v)                 do {} while (0)
309 #define LASSERT_ATOMIC_LT(a, v)                 do {} while (0)
310 #define LASSERT_ATOMIC_LE(a, v)                 do {} while (0)
311 #define LASSERT_ATOMIC_GT(a, v)                 do {} while (0)
312 #define LASSERT_ATOMIC_GE(a, v)                 do {} while (0)
313 #define LASSERT_ATOMIC_GT_LT(a, v1, v2)         do {} while (0)
314 #define LASSERT_ATOMIC_GT_LE(a, v1, v2)         do {} while (0)
315 #define LASSERT_ATOMIC_GE_LT(a, v1, v2)         do {} while (0)
316 #define LASSERT_ATOMIC_GE_LE(a, v1, v2)         do {} while (0)
317
318 #endif /* LASSERT_ATOMIC_ENABLED */
319
320 #define LASSERT_ATOMIC_ZERO(a)                  LASSERT_ATOMIC_EQ(a, 0)
321 #define LASSERT_ATOMIC_POS(a)                   LASSERT_ATOMIC_GT(a, 0)
322
323 #define CFS_ALLOC_PTR(ptr)      LIBCFS_ALLOC(ptr, sizeof(*(ptr)));
324 #define CFS_ALLOC_PTR_ARRAY(ptr, count)                 \
325         LIBCFS_ALLOC(ptr, (count) * sizeof(*(ptr)))
326
327 #define CFS_FREE_PTR(ptr)       LIBCFS_FREE(ptr, sizeof(*(ptr)));
328 #define CFS_FREE_PTR_ARRAY(ptr, count)                  \
329         LIBCFS_FREE(ptr, (count) * sizeof(*(ptr)))
330
331 /* implication */
332 #define ergo(a, b) (!(a) || (b))
333 /* logical equivalence */
334 #define equi(a, b) (!!(a) == !!(b))
335
336 #define MKSTR(ptr) ((ptr))? (ptr) : ""
337
338 static inline size_t cfs_size_round4(size_t val)
339 {
340         return (val + 3) & (~0x3);
341 }
342
343 #ifndef HAVE_CFS_SIZE_ROUND
344 static inline size_t cfs_size_round(size_t val)
345 {
346         return (val + 7) & (~0x7);
347 }
348 #define HAVE_CFS_SIZE_ROUND
349 #endif
350
351 static inline size_t cfs_size_round16(size_t val)
352 {
353         return (val + 0xf) & (~0xf);
354 }
355
356 static inline size_t cfs_size_round32(size_t val)
357 {
358         return (val + 0x1f) & (~0x1f);
359 }
360
361 static inline size_t cfs_size_round0(size_t val)
362 {
363         if (!val)
364                 return 0;
365         return (val + 1 + 7) & (~0x7);
366 }
367
368 static inline size_t cfs_round_strlen(char *fset)
369 {
370         return cfs_size_round(strlen(fset) + 1);
371 }
372
373 #endif