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
48 * When this is on, LASSERT macro includes check for assignment used instead
49 * of equality check, but doesn't have unlikely(). Turn this on from time to
50 * time to make test-builds. This shouldn't be on for production release.
52 #define LASSERT_CHECKED (0)
58 * Strange construction with empty "then" clause is used to trigger compiler
59 * warnings on the assertions of the form LASSERT(a = b);
61 * "warning: suggest parentheses around assignment used as truth value"
63 * requires -Wall. Unfortunately this rules out use of likely/unlikely.
65 #define LASSERTF(cond, fmt, ...) \
70 LIBCFS_DEBUG_MSG_DATA_DECL(__msg_data, D_EMERG, NULL); \
71 libcfs_debug_msg(&__msg_data, \
72 "ASSERTION( %s ) failed: " fmt, #cond, \
74 lbug_with_loc(&__msg_data); \
78 #define LASSERT(cond) LASSERTF(cond, "\n")
80 #else /* !LASSERT_CHECKED */
82 #define LASSERTF(cond, fmt, ...) \
84 if (unlikely(!(cond))) { \
85 LIBCFS_DEBUG_MSG_DATA_DECL(__msg_data, D_EMERG, NULL); \
86 libcfs_debug_msg(&__msg_data, \
87 "ASSERTION( %s ) failed: " fmt, #cond, \
89 lbug_with_loc(&__msg_data); \
93 #define LASSERT(cond) LASSERTF(cond, "\n")
94 #endif /* !LASSERT_CHECKED */
95 #else /* !LIBCFS_DEBUG */
96 /* sizeof is to use expression without evaluating it. */
97 # define LASSERT(e) ((void)sizeof!!(e))
98 # define LASSERTF(cond, ...) ((void)sizeof!!(cond))
99 #endif /* !LIBCFS_DEBUG */
101 #ifdef CONFIG_LUSTRE_DEBUG_EXPENSIVE_CHECK
103 * This is for more expensive checks that one doesn't want to be enabled all
104 * the time. LINVRNT() has to be explicitly enabled by --enable-invariants
107 # define LINVRNT(exp) LASSERT(exp)
109 # define LINVRNT(exp) ((void)sizeof!!(exp))
112 #define KLASSERT(e) LASSERT(e)
114 void lbug_with_loc(struct libcfs_debug_msg_data *) __attribute__((noreturn));
118 LIBCFS_DEBUG_MSG_DATA_DECL(msgdata, D_EMERG, NULL); \
119 lbug_with_loc(&msgdata); \
127 extern atomic_t libcfs_kmemory;
129 # define libcfs_kmem_inc(ptr, size) \
131 atomic_add(size, &libcfs_kmemory); \
134 # define libcfs_kmem_dec(ptr, size) \
136 atomic_sub(size, &libcfs_kmemory); \
139 # define libcfs_kmem_read() \
140 atomic_read(&libcfs_kmemory)
143 # define libcfs_kmem_inc(ptr, size) do {} while (0)
144 # define libcfs_kmem_dec(ptr, size) do {} while (0)
145 # define libcfs_kmem_read() (0)
146 #endif /* LIBCFS_DEBUG */
148 #ifndef LIBCFS_VMALLOC_SIZE
149 #define LIBCFS_VMALLOC_SIZE (2 << PAGE_SHIFT) /* 2 pages */
152 #define LIBCFS_ALLOC_PRE(size, mask) \
154 LASSERT(!in_interrupt() || \
155 ((size) <= LIBCFS_VMALLOC_SIZE && \
156 ((mask) & GFP_ATOMIC)) != 0); \
159 #define LIBCFS_ALLOC_POST(ptr, size) \
161 if (unlikely((ptr) == NULL)) { \
162 CERROR("LNET: out of memory at %s:%d (tried to alloc '" \
163 #ptr "' = %d)\n", __FILE__, __LINE__, (int)(size)); \
164 CERROR("LNET: %d total bytes allocated by lnet\n", \
165 libcfs_kmem_read()); \
167 libcfs_kmem_inc((ptr), (size)); \
168 CDEBUG(D_MALLOC, "alloc '" #ptr "': %d at %p (tot %d).\n", \
169 (int)(size), (ptr), libcfs_kmem_read()); \
174 * allocate memory with GFP flags @mask
175 * The allocated memory is zeroed-out.
177 #define LIBCFS_ALLOC_GFP(ptr, size, mask) \
179 LIBCFS_ALLOC_PRE((size), (mask)); \
180 (ptr) = (size) <= LIBCFS_VMALLOC_SIZE ? \
181 kzalloc((size), (mask)) : vzalloc(size); \
182 LIBCFS_ALLOC_POST((ptr), (size)); \
188 #define LIBCFS_ALLOC(ptr, size) \
189 LIBCFS_ALLOC_GFP(ptr, size, GFP_NOFS)
192 * non-sleeping allocator
194 #define LIBCFS_ALLOC_ATOMIC(ptr, size) \
195 LIBCFS_ALLOC_GFP(ptr, size, GFP_ATOMIC)
198 * allocate memory for specified CPU partition
199 * \a cptab != NULL, \a cpt is CPU partition id of \a cptab
200 * \a cptab == NULL, \a cpt is HW NUMA node id
201 * The allocated memory is zeroed-out.
203 #define LIBCFS_CPT_ALLOC_GFP(ptr, cptab, cpt, size, mask) \
205 LIBCFS_ALLOC_PRE((size), (mask)); \
206 (ptr) = (size) <= LIBCFS_VMALLOC_SIZE ? \
207 cfs_cpt_malloc((cptab), (cpt), (size), (mask) | __GFP_ZERO) : \
208 cfs_cpt_vzalloc((cptab), (cpt), (size)); \
209 LIBCFS_ALLOC_POST((ptr), (size)); \
212 /** default numa allocator */
213 #define LIBCFS_CPT_ALLOC(ptr, cptab, cpt, size) \
214 LIBCFS_CPT_ALLOC_GFP(ptr, cptab, cpt, size, GFP_NOFS)
216 #define LIBCFS_FREE(ptr, size) \
219 if (unlikely((ptr) == NULL)) { \
220 CERROR("LIBCFS: free NULL '" #ptr "' (%d bytes) at " \
221 "%s:%d\n", s, __FILE__, __LINE__); \
224 libcfs_kmem_dec((ptr), s); \
225 CDEBUG(D_MALLOC, "kfreed '" #ptr "': %d at %p (tot %d).\n", \
226 s, (ptr), libcfs_kmem_read()); \
227 if (unlikely(s > LIBCFS_VMALLOC_SIZE)) \
233 /******************************************************************************/
237 void libcfs_debug_dumpstack(struct task_struct *tsk);
238 void libcfs_debug_dumplog(void);
239 int libcfs_debug_init(unsigned long bufsize);
240 int libcfs_debug_cleanup(void);
241 int libcfs_debug_clear_buffer(void);
242 int libcfs_debug_mark_buffer(const char *text);
245 * allocate a variable array, returned value is an array of pointers.
246 * Caller can specify length of array by count.
248 void *cfs_array_alloc(int count, unsigned int size);
249 void cfs_array_free(void *vars);
251 #define LASSERT_ATOMIC_ENABLED (1)
253 #if LASSERT_ATOMIC_ENABLED
255 /** assert value of @a is equal to @v */
256 #define LASSERT_ATOMIC_EQ(a, v) \
258 LASSERTF(atomic_read(a) == v, \
259 "value: %d\n", atomic_read((a))); \
262 /** assert value of @a is unequal to @v */
263 #define LASSERT_ATOMIC_NE(a, v) \
265 LASSERTF(atomic_read(a) != v, \
266 "value: %d\n", atomic_read((a))); \
269 /** assert value of @a is little than @v */
270 #define LASSERT_ATOMIC_LT(a, v) \
272 LASSERTF(atomic_read(a) < v, \
273 "value: %d\n", atomic_read((a))); \
276 /** assert value of @a is little/equal to @v */
277 #define LASSERT_ATOMIC_LE(a, v) \
279 LASSERTF(atomic_read(a) <= v, \
280 "value: %d\n", atomic_read((a))); \
283 /** assert value of @a is great than @v */
284 #define LASSERT_ATOMIC_GT(a, v) \
286 LASSERTF(atomic_read(a) > v, \
287 "value: %d\n", atomic_read((a))); \
290 /** assert value of @a is great/equal to @v */
291 #define LASSERT_ATOMIC_GE(a, v) \
293 LASSERTF(atomic_read(a) >= v, \
294 "value: %d\n", atomic_read((a))); \
297 /** assert value of @a is great than @v1 and little than @v2 */
298 #define LASSERT_ATOMIC_GT_LT(a, v1, v2) \
300 int __v = atomic_read(a); \
301 LASSERTF(__v > v1 && __v < v2, "value: %d\n", __v); \
304 /** assert value of @a is great than @v1 and little/equal to @v2 */
305 #define LASSERT_ATOMIC_GT_LE(a, v1, v2) \
307 int __v = atomic_read(a); \
308 LASSERTF(__v > v1 && __v <= v2, "value: %d\n", __v); \
311 /** assert value of @a is great/equal to @v1 and little than @v2 */
312 #define LASSERT_ATOMIC_GE_LT(a, v1, v2) \
314 int __v = atomic_read(a); \
315 LASSERTF(__v >= v1 && __v < v2, "value: %d\n", __v); \
318 /** assert value of @a is great/equal to @v1 and little/equal to @v2 */
319 #define LASSERT_ATOMIC_GE_LE(a, v1, v2) \
321 int __v = atomic_read(a); \
322 LASSERTF(__v >= v1 && __v <= v2, "value: %d\n", __v); \
325 #else /* !LASSERT_ATOMIC_ENABLED */
327 #define LASSERT_ATOMIC_EQ(a, v) do {} while (0)
328 #define LASSERT_ATOMIC_NE(a, v) do {} while (0)
329 #define LASSERT_ATOMIC_LT(a, v) do {} while (0)
330 #define LASSERT_ATOMIC_LE(a, v) do {} while (0)
331 #define LASSERT_ATOMIC_GT(a, v) do {} while (0)
332 #define LASSERT_ATOMIC_GE(a, v) do {} while (0)
333 #define LASSERT_ATOMIC_GT_LT(a, v1, v2) do {} while (0)
334 #define LASSERT_ATOMIC_GT_LE(a, v1, v2) do {} while (0)
335 #define LASSERT_ATOMIC_GE_LT(a, v1, v2) do {} while (0)
336 #define LASSERT_ATOMIC_GE_LE(a, v1, v2) do {} while (0)
338 #endif /* LASSERT_ATOMIC_ENABLED */
340 #define LASSERT_ATOMIC_ZERO(a) LASSERT_ATOMIC_EQ(a, 0)
341 #define LASSERT_ATOMIC_POS(a) LASSERT_ATOMIC_GT(a, 0)
343 #define CFS_ALLOC_PTR(ptr) LIBCFS_ALLOC(ptr, sizeof (*(ptr)));
344 #define CFS_FREE_PTR(ptr) LIBCFS_FREE(ptr, sizeof (*(ptr)));
346 /** Compile-time assertion.
348 * Check an invariant described by a constant expression at compile time by
349 * forcing a compiler error if it does not hold. \a cond must be a constant
350 * expression as defined by the ISO C Standard:
352 * 6.8.4.2 The switch statement
354 * [#3] The expression of each case label shall be an integer
355 * constant expression and no two of the case constant
356 * expressions in the same switch statement shall have the same
357 * value after conversion...
360 #define CLASSERT(cond) do {switch (1) {case (cond): case 0: break; } } while (0)
363 #define ergo(a, b) (!(a) || (b))
364 /* logical equivalence */
365 #define equi(a, b) (!!(a) == !!(b))
367 /* what used to be in portals_lib.h */
369 # define MIN(a,b) (((a)<(b)) ? (a): (b))
372 # define MAX(a,b) (((a)>(b)) ? (a): (b))
375 #define MKSTR(ptr) ((ptr))? (ptr) : ""
377 static inline size_t cfs_size_round4(size_t val)
379 return (val + 3) & (~0x3);
382 #ifndef HAVE_CFS_SIZE_ROUND
383 static inline size_t cfs_size_round(size_t val)
385 return (val + 7) & (~0x7);
387 #define HAVE_CFS_SIZE_ROUND
390 static inline size_t cfs_size_round16(size_t val)
392 return (val + 0xf) & (~0xf);
395 static inline size_t cfs_size_round32(size_t val)
397 return (val + 0x1f) & (~0x1f);
400 static inline size_t cfs_size_round0(size_t val)
404 return (val + 1 + 7) & (~0x7);
407 static inline size_t cfs_round_strlen(char *fset)
409 return cfs_size_round(strlen(fset) + 1);
412 extern struct cfs_psdev_ops libcfs_psdev_ops;
413 extern struct miscdevice libcfs_dev;
414 extern struct cfs_wi_sched *cfs_sched_rehash;