1 // SPDX-License-Identifier: GPL-2.0
5 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 only,
9 * as published by the Free Software Foundation.
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License version 2 for more details (a copy is included
15 * in the LICENSE file that accompanied this code).
17 * You should have received a copy of the GNU General Public License
18 * version 2 along with this program; If not, see
19 * http://www.gnu.org/licenses/gpl-2.0.html
24 * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
25 * Use is subject to license terms.
27 * Copyright (c) 2011, 2012, Intel Corporation.
30 * This file is part of Lustre, http://www.lustre.org/
31 * Lustre is a trademark of Sun Microsystems, Inc.
33 * libcfs/include/libcfs/libcfs_private.h
35 * Various defines for libcfs.
39 #ifndef __LIBCFS_PRIVATE_H__
40 #define __LIBCFS_PRIVATE_H__
42 #ifndef DEBUG_SUBSYSTEM
43 # define DEBUG_SUBSYSTEM S_UNDEFINED
47 * When this is on, LASSERT macro includes check for assignment used instead
48 * of equality check, but doesn't have unlikely(). Turn this on from time to
49 * time to make test-builds. This shouldn't be on for production release.
51 #define LASSERT_CHECKED (0)
53 #define LASSERTF(cond, fmt, ...) \
55 if (unlikely(!(cond))) { \
56 LIBCFS_DEBUG_MSG_DATA_DECL(__msg_data, D_EMERG, NULL); \
57 libcfs_debug_msg(&__msg_data, \
58 "ASSERTION( %s ) failed: " fmt, #cond, \
60 lbug_with_loc(&__msg_data); \
64 #define LASSERT(cond) LASSERTF(cond, "\n")
66 #ifdef CONFIG_LUSTRE_DEBUG_EXPENSIVE_CHECK
68 * This is for more expensive checks that one doesn't want to be enabled all
69 * the time. LINVRNT() has to be explicitly enabled by
70 * CONFIG_LUSTRE_DEBUG_EXPENSIVE_CHECK option.
72 # define LINVRNT(exp) LASSERT(exp)
74 # define LINVRNT(exp) ((void)sizeof !!(exp))
77 #define KLASSERT(e) LASSERT(e)
79 void __noreturn lbug_with_loc(struct libcfs_debug_msg_data *msg);
83 LIBCFS_DEBUG_MSG_DATA_DECL(msgdata, D_EMERG, NULL); \
84 lbug_with_loc(&msgdata); \
87 #define LIBCFS_ALLOC_POST(ptr, size) \
89 if (unlikely(!(ptr))) { \
90 CERROR("LNET: out of memory at %s:%d (tried to alloc '" \
91 #ptr "' = %d)\n", __FILE__, __LINE__, (int)(size)); \
93 memset((ptr), 0, (size)); \
100 #define LIBCFS_ALLOC(ptr, size) \
102 LASSERT(!in_interrupt()); \
103 (ptr) = kvmalloc((size), GFP_NOFS); \
104 LIBCFS_ALLOC_POST((ptr), (size)); \
108 * non-sleeping allocator
110 #define LIBCFS_ALLOC_ATOMIC(ptr, size) \
112 (ptr) = kmalloc((size), GFP_ATOMIC); \
113 LIBCFS_ALLOC_POST(ptr, size); \
117 * allocate memory for specified CPU partition
118 * \a cptab != NULL, \a cpt is CPU partition id of \a cptab
119 * \a cptab == NULL, \a cpt is HW NUMA node id
121 #define LIBCFS_CPT_ALLOC(ptr, cptab, cpt, size) \
123 LASSERT(!in_interrupt()); \
124 (ptr) = kvmalloc_node((size), GFP_NOFS, cfs_cpt_spread_node(cptab, cpt)); \
125 LIBCFS_ALLOC_POST((ptr), (size)); \
128 #define LIBCFS_FREE(ptr, size) \
130 if (unlikely(!(ptr))) { \
131 CERROR("LIBCFS: free NULL '" #ptr "' (%d bytes) at " \
132 "%s:%d\n", (int)(size), __FILE__, __LINE__); \
138 /******************************************************************************/
140 void libcfs_debug_dumplog(void);
141 int libcfs_debug_init(unsigned long bufsize);
142 int libcfs_debug_cleanup(void);
143 int libcfs_debug_clear_buffer(void);
144 int libcfs_debug_mark_buffer(const char *text);
147 * allocate a variable array, returned value is an array of pointers.
148 * Caller can specify length of array by count.
150 void *cfs_array_alloc(int count, unsigned int size);
151 void cfs_array_free(void *vars);
153 #define LASSERT_ATOMIC_ENABLED (1)
155 #if LASSERT_ATOMIC_ENABLED
157 /** assert value of @a is equal to @v */
158 #define LASSERT_ATOMIC_EQ(a, v) \
159 LASSERTF(atomic_read(a) == v, "value: %d\n", atomic_read((a)))
161 /** assert value of @a is unequal to @v */
162 #define LASSERT_ATOMIC_NE(a, v) \
163 LASSERTF(atomic_read(a) != v, "value: %d\n", atomic_read((a)))
165 /** assert value of @a is little than @v */
166 #define LASSERT_ATOMIC_LT(a, v) \
167 LASSERTF(atomic_read(a) < v, "value: %d\n", atomic_read((a)))
169 /** assert value of @a is little/equal to @v */
170 #define LASSERT_ATOMIC_LE(a, v) \
171 LASSERTF(atomic_read(a) <= v, "value: %d\n", atomic_read((a)))
173 /** assert value of @a is great than @v */
174 #define LASSERT_ATOMIC_GT(a, v) \
175 LASSERTF(atomic_read(a) > v, "value: %d\n", atomic_read((a)))
177 /** assert value of @a is great/equal to @v */
178 #define LASSERT_ATOMIC_GE(a, v) \
179 LASSERTF(atomic_read(a) >= v, "value: %d\n", atomic_read((a)))
181 /** assert value of @a is great than @v1 and little than @v2 */
182 #define LASSERT_ATOMIC_GT_LT(a, v1, v2) \
184 int __v = atomic_read(a); \
185 LASSERTF(__v > v1 && __v < v2, "value: %d\n", __v); \
188 /** assert value of @a is great than @v1 and little/equal to @v2 */
189 #define LASSERT_ATOMIC_GT_LE(a, v1, v2) \
191 int __v = atomic_read(a); \
192 LASSERTF(__v > v1 && __v <= v2, "value: %d\n", __v); \
195 /** assert value of @a is great/equal to @v1 and little than @v2 */
196 #define LASSERT_ATOMIC_GE_LT(a, v1, v2) \
198 int __v = atomic_read(a); \
199 LASSERTF(__v >= v1 && __v < v2, "value: %d\n", __v); \
202 /** assert value of @a is great/equal to @v1 and little/equal to @v2 */
203 #define LASSERT_ATOMIC_GE_LE(a, v1, v2) \
205 int __v = atomic_read(a); \
206 LASSERTF(__v >= v1 && __v <= v2, "value: %d\n", __v); \
209 #else /* !LASSERT_ATOMIC_ENABLED */
211 #define LASSERT_ATOMIC_EQ(a, v) do {} while (0)
212 #define LASSERT_ATOMIC_NE(a, v) do {} while (0)
213 #define LASSERT_ATOMIC_LT(a, v) do {} while (0)
214 #define LASSERT_ATOMIC_LE(a, v) do {} while (0)
215 #define LASSERT_ATOMIC_GT(a, v) do {} while (0)
216 #define LASSERT_ATOMIC_GE(a, v) do {} while (0)
217 #define LASSERT_ATOMIC_GT_LT(a, v1, v2) do {} while (0)
218 #define LASSERT_ATOMIC_GT_LE(a, v1, v2) do {} while (0)
219 #define LASSERT_ATOMIC_GE_LT(a, v1, v2) do {} while (0)
220 #define LASSERT_ATOMIC_GE_LE(a, v1, v2) do {} while (0)
222 #endif /* LASSERT_ATOMIC_ENABLED */
224 #define LASSERT_ATOMIC_ZERO(a) LASSERT_ATOMIC_EQ(a, 0)
225 #define LASSERT_ATOMIC_POS(a) LASSERT_ATOMIC_GT(a, 0)
227 #define CFS_ALLOC_PTR(ptr) LIBCFS_ALLOC(ptr, sizeof(*(ptr)))
228 #define CFS_FREE_PTR(ptr) LIBCFS_FREE(ptr, sizeof(*(ptr)))
230 /* max value for numeric network address */
231 #define MAX_NUMERIC_VALUE 0xffffffff
234 #define ergo(a, b) (!(a) || (b))
235 /* logical equivalence */
236 #define equi(a, b) (!!(a) == !!(b))
238 #ifndef HAVE_CFS_SIZE_ROUND
239 static inline size_t cfs_size_round(int val)
241 return round_up(val, 8);
244 #define HAVE_CFS_SIZE_ROUND