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.gnu.org/licenses/gpl-2.0.html
23 * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Use is subject to license terms.
26 * Copyright (c) 2011, 2014, Intel Corporation.
29 * This file is part of Lustre, http://www.lustre.org/
30 * Lustre is a trademark of Sun Microsystems, Inc.
32 * libcfs/include/libcfs/libcfs_private.h
34 * Various defines for libcfs.
38 #ifndef __LIBCFS_PRIVATE_H__
39 #define __LIBCFS_PRIVATE_H__
41 #ifndef DEBUG_SUBSYSTEM
42 # define DEBUG_SUBSYSTEM S_UNDEFINED
50 * When this is on, LASSERT macro includes check for assignment used instead
51 * of equality check, but doesn't have unlikely(). Turn this on from time to
52 * time to make test-builds. This shouldn't be on for production release.
54 #define LASSERT_CHECKED (0)
60 * Strange construction with empty "then" clause is used to trigger compiler
61 * warnings on the assertions of the form LASSERT(a = b);
63 * "warning: suggest parentheses around assignment used as truth value"
65 * requires -Wall. Unfortunately this rules out use of likely/unlikely.
67 #define LASSERTF(cond, fmt, ...) \
72 LIBCFS_DEBUG_MSG_DATA_DECL(__msg_data, D_EMERG, NULL); \
73 libcfs_debug_msg(&__msg_data, \
74 "ASSERTION( %s ) failed: " fmt, #cond, \
76 lbug_with_loc(&__msg_data); \
80 #define LASSERT(cond) LASSERTF(cond, "\n")
82 #else /* !LASSERT_CHECKED */
84 #define LASSERTF(cond, fmt, ...) \
86 if (unlikely(!(cond))) { \
87 LIBCFS_DEBUG_MSG_DATA_DECL(__msg_data, D_EMERG, NULL); \
88 libcfs_debug_msg(&__msg_data, \
89 "ASSERTION( %s ) failed: " fmt, #cond, \
91 lbug_with_loc(&__msg_data); \
95 #define LASSERT(cond) LASSERTF(cond, "\n")
96 #endif /* !LASSERT_CHECKED */
97 #else /* !LIBCFS_DEBUG */
98 /* sizeof is to use expression without evaluating it. */
99 # define LASSERT(e) ((void)sizeof!!(e))
100 # define LASSERTF(cond, ...) ((void)sizeof!!(cond))
101 #endif /* !LIBCFS_DEBUG */
103 #ifdef CONFIG_LUSTRE_DEBUG_EXPENSIVE_CHECK
105 * This is for more expensive checks that one doesn't want to be enabled all
106 * the time. LINVRNT() has to be explicitly enabled by --enable-invariants
109 # define LINVRNT(exp) LASSERT(exp)
111 # define LINVRNT(exp) ((void)sizeof!!(exp))
114 #define KLASSERT(e) LASSERT(e)
116 void lbug_with_loc(struct libcfs_debug_msg_data *) __attribute__((noreturn));
120 LIBCFS_DEBUG_MSG_DATA_DECL(msgdata, D_EMERG, NULL); \
121 lbug_with_loc(&msgdata); \
124 extern atomic_t libcfs_kmemory;
130 # define libcfs_kmem_inc(ptr, size) \
132 atomic_add(size, &libcfs_kmemory); \
135 # define libcfs_kmem_dec(ptr, size) \
137 atomic_sub(size, &libcfs_kmemory); \
140 # define libcfs_kmem_read() \
141 atomic_read(&libcfs_kmemory)
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 */
149 #ifndef LIBCFS_VMALLOC_SIZE
150 #define LIBCFS_VMALLOC_SIZE (2 << PAGE_SHIFT) /* 2 pages */
153 #define LIBCFS_ALLOC_PRE(size, mask) \
155 LASSERT(!in_interrupt() || \
156 ((size) <= LIBCFS_VMALLOC_SIZE && \
157 ((mask) & GFP_ATOMIC)) != 0); \
160 #define LIBCFS_ALLOC_POST(ptr, size) \
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()); \
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()); \
175 * allocate memory with GFP flags @mask
176 * The allocated memory is zeroed-out.
178 #define LIBCFS_ALLOC_GFP(ptr, size, mask) \
180 LIBCFS_ALLOC_PRE((size), (mask)); \
181 (ptr) = (size) <= LIBCFS_VMALLOC_SIZE ? \
182 kzalloc((size), (mask)) : vzalloc(size); \
183 LIBCFS_ALLOC_POST((ptr), (size)); \
189 #define LIBCFS_ALLOC(ptr, size) \
190 LIBCFS_ALLOC_GFP(ptr, size, GFP_NOFS)
193 * non-sleeping allocator
195 #define LIBCFS_ALLOC_ATOMIC(ptr, size) \
196 LIBCFS_ALLOC_GFP(ptr, size, GFP_ATOMIC)
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.
204 #define LIBCFS_CPT_ALLOC_GFP(ptr, cptab, cpt, size, mask) \
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)); \
213 /** default numa allocator */
214 #define LIBCFS_CPT_ALLOC(ptr, cptab, cpt, size) \
215 LIBCFS_CPT_ALLOC_GFP(ptr, cptab, cpt, size, GFP_NOFS)
217 #define LIBCFS_FREE(ptr, size) \
220 if (unlikely((ptr) == NULL)) { \
221 CERROR("LIBCFS: free NULL '" #ptr "' (%d bytes) at " \
222 "%s:%d\n", s, __FILE__, __LINE__); \
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)) \
234 /******************************************************************************/
236 /* htonl hack - either this, or compile with -O2. Stupid byteorder/generic.h */
237 #if defined(__GNUC__) && (__GNUC__ >= 2) && !defined(__OPTIMIZE__)
238 #define ___htonl(x) __cpu_to_be32(x)
239 #define ___htons(x) __cpu_to_be16(x)
240 #define ___ntohl(x) __be32_to_cpu(x)
241 #define ___ntohs(x) __be16_to_cpu(x)
242 #define htonl(x) ___htonl(x)
243 #define ntohl(x) ___ntohl(x)
244 #define htons(x) ___htons(x)
245 #define ntohs(x) ___ntohs(x)
248 void libcfs_debug_dumpstack(struct task_struct *tsk);
249 void libcfs_run_upcall(char **argv);
250 void libcfs_run_lbug_upcall(struct libcfs_debug_msg_data *);
251 void libcfs_debug_dumplog(void);
252 int libcfs_debug_init(unsigned long bufsize);
253 int libcfs_debug_cleanup(void);
254 int libcfs_debug_clear_buffer(void);
255 int libcfs_debug_mark_buffer(const char *text);
257 #else /* !__KERNEL__ */
261 # define LASSERT(e) assert(e)
262 # define LASSERTF(cond, ...) \
265 CERROR(__VA_ARGS__); \
268 # define LBUG() assert(0)
269 # ifdef CONFIG_LUSTRE_DEBUG_EXPENSIVE_CHECK
270 # define LINVRNT(exp) LASSERT(exp)
272 # define LINVRNT(exp) ((void)sizeof!!(exp))
275 # define LASSERT(e) ((void)sizeof!!(e))
276 # define LASSERTF(cond, ...) ((void)sizeof!!(cond))
277 # define LBUG() ((void)(0))
278 # define LINVRNT(exp) ((void)sizeof!!(exp))
279 # endif /* LIBCFS_DEBUG */
280 # define KLASSERT(e) ((void)0)
281 # define printk printf
282 #define LIBCFS_ALLOC_GFP(ptr, size, mask) \
284 (ptr) = calloc(1, size); \
286 # define LIBCFS_FREE(ptr, size) do { free(ptr); } while((size) - (size))
287 # define LIBCFS_ALLOC(ptr, size) \
288 LIBCFS_ALLOC_GFP(ptr, size, 0)
289 # define LIBCFS_CPT_ALLOC_GFP(ptr, cptab, cpt, size, mask) \
290 LIBCFS_ALLOC(ptr, size)
291 # define LIBCFS_CPT_ALLOC(ptr, cptab, cpt, size) \
292 LIBCFS_ALLOC(ptr, size)
294 void libcfs_debug_dumplog(void);
295 int libcfs_debug_init(unsigned long bufsize);
296 int libcfs_debug_cleanup(void);
298 #define libcfs_debug_dumpstack(tsk) ((void)0)
301 * Generic compiler-dependent macros required for kernel
302 * build go below this comment. Actual compiler/compiler version
303 * specific implementations come from the above header files
305 #define likely(x) __builtin_expect(!!(x), 1)
306 #define unlikely(x) __builtin_expect(!!(x), 0)
311 * allocate a variable array, returned value is an array of pointers.
312 * Caller can specify length of array by count.
314 void *cfs_array_alloc(int count, unsigned int size);
315 void cfs_array_free(void *vars);
317 #define LASSERT_ATOMIC_ENABLED (1)
319 #if LASSERT_ATOMIC_ENABLED
321 /** assert value of @a is equal to @v */
322 #define LASSERT_ATOMIC_EQ(a, v) \
324 LASSERTF(atomic_read(a) == v, \
325 "value: %d\n", atomic_read((a))); \
328 /** assert value of @a is unequal to @v */
329 #define LASSERT_ATOMIC_NE(a, v) \
331 LASSERTF(atomic_read(a) != v, \
332 "value: %d\n", atomic_read((a))); \
335 /** assert value of @a is little than @v */
336 #define LASSERT_ATOMIC_LT(a, v) \
338 LASSERTF(atomic_read(a) < v, \
339 "value: %d\n", atomic_read((a))); \
342 /** assert value of @a is little/equal to @v */
343 #define LASSERT_ATOMIC_LE(a, v) \
345 LASSERTF(atomic_read(a) <= v, \
346 "value: %d\n", atomic_read((a))); \
349 /** assert value of @a is great than @v */
350 #define LASSERT_ATOMIC_GT(a, v) \
352 LASSERTF(atomic_read(a) > v, \
353 "value: %d\n", atomic_read((a))); \
356 /** assert value of @a is great/equal to @v */
357 #define LASSERT_ATOMIC_GE(a, v) \
359 LASSERTF(atomic_read(a) >= v, \
360 "value: %d\n", atomic_read((a))); \
363 /** assert value of @a is great than @v1 and little than @v2 */
364 #define LASSERT_ATOMIC_GT_LT(a, v1, v2) \
366 int __v = atomic_read(a); \
367 LASSERTF(__v > v1 && __v < v2, "value: %d\n", __v); \
370 /** assert value of @a is great than @v1 and little/equal to @v2 */
371 #define LASSERT_ATOMIC_GT_LE(a, v1, v2) \
373 int __v = atomic_read(a); \
374 LASSERTF(__v > v1 && __v <= v2, "value: %d\n", __v); \
377 /** assert value of @a is great/equal to @v1 and little than @v2 */
378 #define LASSERT_ATOMIC_GE_LT(a, v1, v2) \
380 int __v = atomic_read(a); \
381 LASSERTF(__v >= v1 && __v < v2, "value: %d\n", __v); \
384 /** assert value of @a is great/equal to @v1 and little/equal to @v2 */
385 #define LASSERT_ATOMIC_GE_LE(a, v1, v2) \
387 int __v = atomic_read(a); \
388 LASSERTF(__v >= v1 && __v <= v2, "value: %d\n", __v); \
391 #else /* !LASSERT_ATOMIC_ENABLED */
393 #define LASSERT_ATOMIC_EQ(a, v) do {} while (0)
394 #define LASSERT_ATOMIC_NE(a, v) do {} while (0)
395 #define LASSERT_ATOMIC_LT(a, v) do {} while (0)
396 #define LASSERT_ATOMIC_LE(a, v) do {} while (0)
397 #define LASSERT_ATOMIC_GT(a, v) do {} while (0)
398 #define LASSERT_ATOMIC_GE(a, v) do {} while (0)
399 #define LASSERT_ATOMIC_GT_LT(a, v1, v2) do {} while (0)
400 #define LASSERT_ATOMIC_GT_LE(a, v1, v2) do {} while (0)
401 #define LASSERT_ATOMIC_GE_LT(a, v1, v2) do {} while (0)
402 #define LASSERT_ATOMIC_GE_LE(a, v1, v2) do {} while (0)
404 #endif /* LASSERT_ATOMIC_ENABLED */
406 #define LASSERT_ATOMIC_ZERO(a) LASSERT_ATOMIC_EQ(a, 0)
407 #define LASSERT_ATOMIC_POS(a) LASSERT_ATOMIC_GT(a, 0)
409 #define CFS_ALLOC_PTR(ptr) LIBCFS_ALLOC(ptr, sizeof (*(ptr)));
410 #define CFS_FREE_PTR(ptr) LIBCFS_FREE(ptr, sizeof (*(ptr)));
412 /** Compile-time assertion.
414 * Check an invariant described by a constant expression at compile time by
415 * forcing a compiler error if it does not hold. \a cond must be a constant
416 * expression as defined by the ISO C Standard:
418 * 6.8.4.2 The switch statement
420 * [#3] The expression of each case label shall be an integer
421 * constant expression and no two of the case constant
422 * expressions in the same switch statement shall have the same
423 * value after conversion...
426 #define CLASSERT(cond) do {switch (1) {case (cond): case 0: break; } } while (0)
429 #define ergo(a, b) (!(a) || (b))
430 /* logical equivalence */
431 #define equi(a, b) (!!(a) == !!(b))
433 /* what used to be in portals_lib.h */
435 # define MIN(a,b) (((a)<(b)) ? (a): (b))
438 # define MAX(a,b) (((a)>(b)) ? (a): (b))
441 #define MKSTR(ptr) ((ptr))? (ptr) : ""
443 static inline size_t cfs_size_round4(size_t val)
445 return (val + 3) & (~0x3);
448 #ifndef HAVE_CFS_SIZE_ROUND
449 static inline size_t cfs_size_round(size_t val)
451 return (val + 7) & (~0x7);
453 #define HAVE_CFS_SIZE_ROUND
456 static inline size_t cfs_size_round16(size_t val)
458 return (val + 0xf) & (~0xf);
461 static inline size_t cfs_size_round32(size_t val)
463 return (val + 0x1f) & (~0x1f);
466 static inline size_t cfs_size_round0(size_t val)
470 return (val + 1 + 7) & (~0x7);
473 static inline size_t cfs_round_strlen(char *fset)
475 return cfs_size_round(strlen(fset) + 1);
478 #define LOGL(var,len,ptr) \
481 memcpy((char *)ptr, (const char *)var, len); \
482 ptr += cfs_size_round(len); \
485 #define LOGU(var,len,ptr) \
488 memcpy((char *)var, (const char *)ptr, len); \
489 ptr += cfs_size_round(len); \
492 extern struct cfs_psdev_ops libcfs_psdev_ops;
493 extern struct miscdevice libcfs_dev;
494 extern struct cfs_wi_sched *cfs_sched_rehash;