1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
4 * Copyright (C) 2001, 2002 Cluster File Systems, Inc.
6 * This file is part of Lustre, http://www.lustre.org.
8 * Lustre is free software; you can redistribute it and/or
9 * modify it under the terms of version 2 of the GNU General Public
10 * License as published by the Free Software Foundation.
12 * Lustre is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with Lustre; if not, write to the Free Software
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26 #include <libcfs/kp30.h>
27 #include <lprocfs_status.h>
29 /* global variables */
30 extern struct lprocfs_stats *obd_memory;
33 OBD_MEMORY_PAGES_STAT = 1,
37 extern unsigned int obd_fail_loc;
38 extern unsigned int obd_fail_val;
39 extern unsigned int obd_debug_peer_on_timeout;
40 extern unsigned int obd_dump_on_timeout;
41 extern unsigned int obd_dump_on_eviction;
42 extern unsigned int obd_timeout; /* seconds */
43 #define PING_INTERVAL max(obd_timeout / 4, 1U)
44 #define RECONNECT_INTERVAL max(obd_timeout / 10, 10U)
45 extern unsigned int ldlm_timeout;
46 extern unsigned int obd_health_check_timeout;
47 extern unsigned int obd_sync_filter;
48 extern unsigned int obd_max_dirty_pages;
49 extern atomic_t obd_dirty_pages;
50 extern cfs_waitq_t obd_race_waitq;
51 extern int obd_race_state;
53 /* Timeout definitions */
54 #define LDLM_TIMEOUT_DEFAULT 20
55 #define OBD_TIMEOUT_DEFAULT 100
56 #define HEALTH_CHECK_COEF 3 / 2
57 #define HEALTH_CHECK_TIMEOUT_DEFAULT (OBD_TIMEOUT_DEFAULT * HEALTH_CHECK_COEF)
58 #define HEALTH_CHECK_TIMEOUT (obd_timeout * HEALTH_CHECK_COEF)
60 #define OBD_FAIL_MDS 0x100
61 #define OBD_FAIL_MDS_HANDLE_UNPACK 0x101
62 #define OBD_FAIL_MDS_GETATTR_NET 0x102
63 #define OBD_FAIL_MDS_GETATTR_PACK 0x103
64 #define OBD_FAIL_MDS_READPAGE_NET 0x104
65 #define OBD_FAIL_MDS_READPAGE_PACK 0x105
66 #define OBD_FAIL_MDS_SENDPAGE 0x106
67 #define OBD_FAIL_MDS_REINT_NET 0x107
68 #define OBD_FAIL_MDS_REINT_UNPACK 0x108
69 #define OBD_FAIL_MDS_REINT_SETATTR 0x109
70 #define OBD_FAIL_MDS_REINT_SETATTR_WRITE 0x10a
71 #define OBD_FAIL_MDS_REINT_CREATE 0x10b
72 #define OBD_FAIL_MDS_REINT_CREATE_WRITE 0x10c
73 #define OBD_FAIL_MDS_REINT_UNLINK 0x10d
74 #define OBD_FAIL_MDS_REINT_UNLINK_WRITE 0x10e
75 #define OBD_FAIL_MDS_REINT_LINK 0x10f
76 #define OBD_FAIL_MDS_REINT_LINK_WRITE 0x110
77 #define OBD_FAIL_MDS_REINT_RENAME 0x111
78 #define OBD_FAIL_MDS_REINT_RENAME_WRITE 0x112
79 #define OBD_FAIL_MDS_OPEN_NET 0x113
80 #define OBD_FAIL_MDS_OPEN_PACK 0x114
81 #define OBD_FAIL_MDS_CLOSE_NET 0x115
82 #define OBD_FAIL_MDS_CLOSE_PACK 0x116
83 #define OBD_FAIL_MDS_CONNECT_NET 0x117
84 #define OBD_FAIL_MDS_CONNECT_PACK 0x118
85 #define OBD_FAIL_MDS_REINT_NET_REP 0x119
86 #define OBD_FAIL_MDS_DISCONNECT_NET 0x11a
87 #define OBD_FAIL_MDS_GETSTATUS_NET 0x11b
88 #define OBD_FAIL_MDS_GETSTATUS_PACK 0x11c
89 #define OBD_FAIL_MDS_STATFS_PACK 0x11d
90 #define OBD_FAIL_MDS_STATFS_NET 0x11e
91 #define OBD_FAIL_MDS_GETATTR_NAME_NET 0x11f
92 #define OBD_FAIL_MDS_PIN_NET 0x120
93 #define OBD_FAIL_MDS_UNPIN_NET 0x121
94 #define OBD_FAIL_MDS_ALL_REPLY_NET 0x122
95 #define OBD_FAIL_MDS_ALL_REQUEST_NET 0x123
96 #define OBD_FAIL_MDS_SYNC_NET 0x124
97 #define OBD_FAIL_MDS_SYNC_PACK 0x125
98 #define OBD_FAIL_MDS_DONE_WRITING_NET 0x126
99 #define OBD_FAIL_MDS_DONE_WRITING_PACK 0x127
100 #define OBD_FAIL_MDS_ALLOC_OBDO 0x128
101 #define OBD_FAIL_MDS_PAUSE_OPEN 0x129
102 #define OBD_FAIL_MDS_STATFS_LCW_SLEEP 0x12a
103 #define OBD_FAIL_MDS_OPEN_CREATE 0x12b
104 #define OBD_FAIL_MDS_OST_SETATTR 0x12c
105 #define OBD_FAIL_MDS_QUOTACHECK_NET 0x12d
106 #define OBD_FAIL_MDS_QUOTACTL_NET 0x12e
107 #define OBD_FAIL_MDS_CLIENT_ADD 0x12f
108 #define OBD_FAIL_MDS_GETXATTR_NET 0x130
109 #define OBD_FAIL_MDS_GETXATTR_PACK 0x131
110 #define OBD_FAIL_MDS_SETXATTR_NET 0x132
111 #define OBD_FAIL_MDS_SETXATTR 0x133
112 #define OBD_FAIL_MDS_SETXATTR_WRITE 0x134
113 #define OBD_FAIL_MDS_FS_SETUP 0x135
114 #define OBD_FAIL_MDS_RESEND 0x136
115 #define OBD_FAIL_MDS_IS_SUBDIR_NET 0x137
116 #define OBD_FAIL_MDS_IS_SUBDIR_PACK 0x138
117 #define OBD_FAIL_MDS_SET_INFO_NET 0x139
118 #define OBD_FAIL_MDS_WRITEPAGE_NET 0x13a
119 #define OBD_FAIL_MDS_WRITEPAGE_PACK 0x13b
120 #define OBD_FAIL_MDS_LLOG_CREATE_FAILED 0x13c
121 #define OBD_FAIL_MDS_OSC_PRECREATE 0x13d
122 #define OBD_FAIL_MDS_LOV_SYNC_RACE 0x13e
124 #define OBD_FAIL_OST 0x200
125 #define OBD_FAIL_OST_CONNECT_NET 0x201
126 #define OBD_FAIL_OST_DISCONNECT_NET 0x202
127 #define OBD_FAIL_OST_GET_INFO_NET 0x203
128 #define OBD_FAIL_OST_CREATE_NET 0x204
129 #define OBD_FAIL_OST_DESTROY_NET 0x205
130 #define OBD_FAIL_OST_GETATTR_NET 0x206
131 #define OBD_FAIL_OST_SETATTR_NET 0x207
132 #define OBD_FAIL_OST_OPEN_NET 0x208
133 #define OBD_FAIL_OST_CLOSE_NET 0x209
134 #define OBD_FAIL_OST_BRW_NET 0x20a
135 #define OBD_FAIL_OST_PUNCH_NET 0x20b
136 #define OBD_FAIL_OST_STATFS_NET 0x20c
137 #define OBD_FAIL_OST_HANDLE_UNPACK 0x20d
138 #define OBD_FAIL_OST_BRW_WRITE_BULK 0x20e
139 #define OBD_FAIL_OST_BRW_READ_BULK 0x20f
140 #define OBD_FAIL_OST_SYNC_NET 0x210
141 #define OBD_FAIL_OST_ALL_REPLY_NET 0x211
142 #define OBD_FAIL_OST_ALL_REQUESTS_NET 0x212
143 #define OBD_FAIL_OST_LDLM_REPLY_NET 0x213
144 #define OBD_FAIL_OST_BRW_PAUSE_BULK 0x214
145 #define OBD_FAIL_OST_ENOSPC 0x215
146 #define OBD_FAIL_OST_EROFS 0x216
147 #define OBD_FAIL_OST_ENOENT 0x217
148 #define OBD_FAIL_OST_QUOTACHECK_NET 0x218
149 #define OBD_FAIL_OST_QUOTACTL_NET 0x219
150 #define OBD_FAIL_OST_CHECKSUM_RECEIVE 0x21a
151 #define OBD_FAIL_OST_CHECKSUM_SEND 0x21b
152 #define OBD_FAIL_OST_BRW_SIZE 0x21c
153 #define OBD_FAIL_OST_DROP_REQ 0x21d
154 #define OBD_FAIL_OST_SETATTR_CREDITS 0x21e
155 #define OBD_FAIL_OST_HOLD_WRITE_RPC 0x21f
156 #define OBD_FAIL_OST_BRW_WRITE_BULK2 0x220
158 #define OBD_FAIL_LDLM 0x300
159 #define OBD_FAIL_LDLM_NAMESPACE_NEW 0x301
160 #define OBD_FAIL_LDLM_ENQUEUE 0x302
161 #define OBD_FAIL_LDLM_CONVERT 0x303
162 #define OBD_FAIL_LDLM_CANCEL 0x304
163 #define OBD_FAIL_LDLM_BL_CALLBACK 0x305
164 #define OBD_FAIL_LDLM_CP_CALLBACK 0x306
165 #define OBD_FAIL_LDLM_GL_CALLBACK 0x307
166 #define OBD_FAIL_LDLM_ENQUEUE_EXTENT_ERR 0x308
167 #define OBD_FAIL_LDLM_ENQUEUE_INTENT_ERR 0x309
168 #define OBD_FAIL_LDLM_CREATE_RESOURCE 0x30a
169 #define OBD_FAIL_LDLM_ENQUEUE_BLOCKED 0x30b
170 #define OBD_FAIL_LDLM_REPLY 0x30c
171 #define OBD_FAIL_LDLM_RECOV_CLIENTS 0x30d
172 #define OBD_FAIL_LDLM_ENQUEUE_OLD_EXPORT 0x30e
173 #define OBD_FAIL_LDLM_GLIMPSE 0x30f
174 #define OBD_FAIL_LDLM_CANCEL_RACE 0x310
175 #define OBD_FAIL_LDLM_CANCEL_EVICT_RACE 0x311
177 #define OBD_FAIL_OSC 0x400
178 #define OBD_FAIL_OSC_BRW_READ_BULK 0x401
179 #define OBD_FAIL_OSC_BRW_WRITE_BULK 0x402
180 #define OBD_FAIL_OSC_LOCK_BL_AST 0x403
181 #define OBD_FAIL_OSC_LOCK_CP_AST 0x404
182 #define OBD_FAIL_OSC_MATCH 0x405
183 #define OBD_FAIL_OSC_BRW_PREP_REQ 0x406
184 #define OBD_FAIL_OSC_SHUTDOWN 0x407
185 #define OBD_FAIL_OSC_CHECKSUM_RECEIVE 0x408
186 #define OBD_FAIL_OSC_CHECKSUM_SEND 0x409
187 #define OBD_FAIL_OSC_BRW_PREP_REQ2 0x40a
189 #define OBD_FAIL_PTLRPC 0x500
190 #define OBD_FAIL_PTLRPC_ACK 0x501
191 #define OBD_FAIL_PTLRPC_RQBD 0x502
192 #define OBD_FAIL_PTLRPC_BULK_GET_NET 0x503
193 #define OBD_FAIL_PTLRPC_BULK_PUT_NET 0x504
194 #define OBD_FAIL_PTLRPC_DROP_RPC 0x505
195 #define OBD_FAIL_PTLRPC_DELAY_SEND 0x506
196 #define OBD_FAIL_PTLRPC_DELAY_RECOV 0x507
198 #define OBD_FAIL_OBD_PING_NET 0x600
199 #define OBD_FAIL_OBD_LOG_CANCEL_NET 0x601
200 #define OBD_FAIL_OBD_LOGD_NET 0x602
201 #define OBD_FAIL_OBD_QC_CALLBACK_NET 0x603
202 #define OBD_FAIL_OBD_DQACQ 0x604
204 #define OBD_FAIL_TGT_REPLY_NET 0x700
205 #define OBD_FAIL_TGT_CONN_RACE 0x701
206 #define OBD_FAIL_TGT_FORCE_RECONNECT 0x702
207 #define OBD_FAIL_TGT_DELAY_CONNECT 0x703
208 #define OBD_FAIL_TGT_DELAY_RECONNECT 0x704
209 #define OBD_FAIL_TGT_DELAY_PRECREATE 0x705
211 #define OBD_FAIL_MDC_REVALIDATE_PAUSE 0x800
212 #define OBD_FAIL_MDC_ENQUEUE_PAUSE 0x801
214 #define OBD_FAIL_MGS 0x900
215 #define OBD_FAIL_MGS_ALL_REQUEST_NET 0x901
216 #define OBD_FAIL_MGS_ALL_REPLY_NET 0x902
217 #define OBD_FAIL_MGC_PROCESS_LOG 0x903
218 #define OBD_FAIL_MGS_SLOW_REQUEST_NET 0x904
219 #define OBD_FAIL_MGS_SLOW_TARGET_REG 0x905
221 #define OBD_FAIL_QUOTA_QD_COUNT_32BIT 0xa00
223 #define OBD_FAIL_LPROC_REMOVE 0xb00
225 #define OBD_FAIL_SEQ 0x1000
226 #define OBD_FAIL_SEQ_QUERY_NET 0x1001
228 #define OBD_FAIL_FLD 0x1100
229 #define OBD_FAIL_FLD_QUERY_NET 0x1101
231 #define OBD_FAIL_SEC_CTX 0x1200
232 #define OBD_FAIL_SEC_CTX_INIT_NET 0x1201
233 #define OBD_FAIL_SEC_CTX_INIT_CONT_NET 0x1202
234 #define OBD_FAIL_SEC_CTX_FINI_NET 0x1203
236 /* Failure injection control */
237 #define OBD_FAIL_MASK_SYS 0x0000FF00
238 #define OBD_FAIL_MASK_LOC (0x000000FF | OBD_FAIL_MASK_SYS)
239 #define OBD_FAIL_ONCE 0x80000000
240 #define OBD_FAILED 0x40000000
242 /* The following flags aren't made to be combined */
243 #define OBD_FAIL_SKIP 0x20000000 /* skip N then fail */
244 #define OBD_FAIL_SOME 0x10000000 /* fail N times */
245 #define OBD_FAIL_RAND 0x08000000 /* fail 1/N of the time */
246 #define OBD_FAIL_USR1 0x04000000 /* user flag */
248 static inline int obd_fail_check(__u32 id)
250 static int count = 0;
251 if (likely((obd_fail_loc & OBD_FAIL_MASK_LOC) !=
252 (id & OBD_FAIL_MASK_LOC)))
255 if ((obd_fail_loc & (OBD_FAILED | OBD_FAIL_ONCE)) ==
256 (OBD_FAILED | OBD_FAIL_ONCE)) {
257 count = 0; /* paranoia */
261 if (obd_fail_loc & OBD_FAIL_RAND) {
262 unsigned int ll_rand(void);
263 if (obd_fail_val < 2)
265 if (ll_rand() % obd_fail_val > 0)
269 if (obd_fail_loc & OBD_FAIL_SKIP) {
271 if (count < obd_fail_val)
276 /* Overridden by FAIL_ONCE */
277 if (obd_fail_loc & OBD_FAIL_SOME) {
279 if (count >= obd_fail_val) {
281 /* Don't fail anymore */
282 obd_fail_loc |= OBD_FAIL_ONCE;
286 obd_fail_loc |= OBD_FAILED;
287 /* Handle old checks that OR in this */
288 if (id & OBD_FAIL_ONCE)
289 obd_fail_loc |= OBD_FAIL_ONCE;
294 #define OBD_FAIL_CHECK(id) \
297 if (unlikely(obd_fail_loc && (_ret_ = obd_fail_check(id)))) { \
298 CERROR("*** obd_fail_loc=%x ***\n", id); \
303 /* deprecated - just use OBD_FAIL_CHECK */
304 #define OBD_FAIL_CHECK_ONCE OBD_FAIL_CHECK
306 #define OBD_FAIL_RETURN(id, ret) \
308 if (unlikely(obd_fail_loc && obd_fail_check(id))) { \
309 CERROR("*** obd_fail_return=%x rc=%d ***\n", id, ret); \
314 #define OBD_FAIL_TIMEOUT(id, secs) \
316 if (unlikely(obd_fail_loc && (_ret_ = obd_fail_check(id)))) { \
317 CERROR("obd_fail_timeout id %x sleeping for %d secs\n", \
319 set_current_state(TASK_UNINTERRUPTIBLE); \
320 cfs_schedule_timeout(CFS_TASK_UNINT, \
321 cfs_time_seconds(secs)); \
322 set_current_state(TASK_RUNNING); \
323 CERROR("obd_fail_timeout id %x awake\n", (id)); \
328 #define OBD_FAIL_TIMEOUT_MS(id, ms) \
330 if (unlikely(obd_fail_loc && (_ret_ = obd_fail_check(id)))) { \
331 CERROR("obd_fail_timeout id %x sleeping for %d ms\n", \
333 set_current_state(TASK_UNINTERRUPTIBLE); \
334 cfs_schedule_timeout(CFS_TASK_UNINT, \
335 cfs_time_seconds(ms)/1000); \
336 set_current_state(TASK_RUNNING); \
337 CERROR("obd_fail_timeout id %x awake\n", (id)); \
343 /* The idea here is to synchronise two threads to force a race. The
344 * first thread that calls this with a matching fail_loc is put to
345 * sleep. The next thread that calls with the same fail_loc wakes up
346 * the first and continues. */
347 #define OBD_RACE(id) \
349 if (unlikely(obd_fail_loc && obd_fail_check(id))) { \
350 obd_race_state = 0; \
351 CERROR("obd_race id %x sleeping\n", (id)); \
352 OBD_SLEEP_ON(obd_race_waitq, obd_race_state != 0); \
353 CERROR("obd_fail_race id %x awake\n", (id)); \
354 } else if ((obd_fail_loc & OBD_FAIL_MASK_LOC) == \
355 ((id) & OBD_FAIL_MASK_LOC)) { \
356 CERROR("obd_fail_race id %x waking\n", (id)); \
357 obd_race_state = 1; \
358 wake_up(&obd_race_waitq); \
362 /* sigh. an expedient fix until OBD_RACE is fixed up */
363 #define OBD_RACE(foo) do {} while(0)
366 #define fixme() CDEBUG(D_OTHER, "FIXME\n");
368 extern atomic_t libcfs_kmemory;
371 #define obd_memory_add(size) \
372 lprocfs_counter_add(obd_memory, OBD_MEMORY_STAT, (long)(size))
373 #define obd_memory_sub(size) \
374 lprocfs_counter_sub(obd_memory, OBD_MEMORY_STAT, (long)(size))
375 #define obd_memory_sum() \
376 lprocfs_stats_collector(obd_memory, OBD_MEMORY_STAT, \
377 LPROCFS_FIELDS_FLAGS_SUM)
378 #define obd_pages_add(order) \
379 lprocfs_counter_add(obd_memory, OBD_MEMORY_PAGES_STAT, \
380 (long)(1 << (order)))
381 #define obd_pages_sub(order) \
382 lprocfs_counter_sub(obd_memory, OBD_MEMORY_PAGES_STAT, \
383 (long)(1 << (order)))
384 #define obd_pages_sum() \
385 lprocfs_stats_collector(obd_memory, OBD_MEMORY_PAGES_STAT, \
386 LPROCFS_FIELDS_FLAGS_SUM)
388 extern void obd_update_maxusage(void);
389 extern __u64 obd_memory_max(void);
390 extern __u64 obd_pages_max(void);
394 extern __u64 obd_alloc;
395 extern __u64 obd_pages;
397 extern __u64 obd_max_alloc;
398 extern __u64 obd_max_pages;
400 static inline void obd_memory_add(long size)
403 if (obd_alloc > obd_max_alloc)
404 obd_max_alloc = obd_alloc;
407 static inline void obd_memory_sub(long size)
412 static inline void obd_pages_add(int order)
414 obd_pages += 1<< order;
415 if (obd_pages > obd_max_pages)
416 obd_max_pages = obd_pages;
419 static inline void obd_pages_sub(int order)
421 obd_pages -= 1<< order;
424 #define obd_memory_sum() (obd_alloc)
425 #define obd_pages_sum() (obd_pages)
427 #define obd_memory_max() (obd_max_alloc)
428 #define obd_pages_max() (obd_max_pages)
432 #if defined (CONFIG_DEBUG_MEMORY) && defined(__KERNEL__)
434 #define OBD_MT_WRONG_SIZE (1 << 0)
435 #define OBD_MT_ALREADY_FREED (1 << 1)
436 #define OBD_MT_LOC_LEN 128
438 struct obd_mem_track {
439 struct hlist_node mt_hash;
440 char mt_loc[OBD_MT_LOC_LEN];
446 void lvfs_memdbg_show(void);
447 void lvfs_memdbg_insert(struct obd_mem_track *mt);
448 void lvfs_memdbg_remove(struct obd_mem_track *mt);
449 struct obd_mem_track *lvfs_memdbg_find(void *ptr);
451 int lvfs_memdbg_check_insert(struct obd_mem_track *mt);
452 struct obd_mem_track *lvfs_memdbg_check_remove(void *ptr);
454 static inline struct obd_mem_track *
455 __new_mem_track(void *ptr, int size,
456 char *file, int line)
458 struct obd_mem_track *mt;
460 mt = kmalloc(sizeof(*mt), GFP_KERNEL);
464 snprintf(mt->mt_loc, sizeof(mt->mt_loc) - 1,
465 "%s:%d", file, line);
474 __free_mem_track(struct obd_mem_track *mt)
480 __get_mem_track(void *ptr, int size,
481 char *file, int line)
483 struct obd_mem_track *mt;
485 mt = __new_mem_track(ptr, size, file, line);
487 CWARN("Can't allocate new memory track\n");
491 if (!lvfs_memdbg_check_insert(mt))
492 __free_mem_track(mt);
498 __put_mem_track(void *ptr, int size,
499 char *file, int line)
501 struct obd_mem_track *mt;
503 if (unlikely(!(mt = lvfs_memdbg_check_remove(ptr)))) {
504 CWARN("Ptr 0x%p is not allocated. Attempt to free "
505 "not allocated memory at %s:%d\n", ptr,
510 if (unlikely(mt->mt_size != size)) {
511 if (!(mt->mt_flags & OBD_MT_ALREADY_FREED)) {
512 mt->mt_flags |= (OBD_MT_WRONG_SIZE |
513 OBD_MT_ALREADY_FREED);
515 CWARN("Freeing memory chunk (at 0x%p) of "
516 "different size than allocated "
517 "(%d != %d) at %s:%d, allocated at %s\n",
518 ptr, mt->mt_size, size, file, line,
522 __free_mem_track(mt);
528 #define get_mem_track(ptr, size, file, line) \
529 __get_mem_track((ptr), (size), (file), (line))
531 #define put_mem_track(ptr, size, file, line) \
532 __put_mem_track((ptr), (size), (file), (line))
534 #else /* !CONFIG_DEBUG_MEMORY */
536 #define get_mem_track(ptr, size, file, line) \
539 #define put_mem_track(ptr, size, file, line) \
541 #endif /* !CONFIG_DEBUG_MEMORY */
543 #define OBD_DEBUG_MEMUSAGE (1)
545 #if OBD_DEBUG_MEMUSAGE
546 #define OBD_ALLOC_POST(ptr, size, name) \
547 obd_memory_add(size); \
548 get_mem_track((ptr), (size), __FILE__, __LINE__); \
549 CDEBUG(D_MALLOC, name " '" #ptr "': %d at %p.\n", \
552 #define OBD_FREE_PRE(ptr, size, name) \
554 put_mem_track((ptr), (size), __FILE__, __LINE__); \
555 obd_memory_sub(size); \
556 CDEBUG(D_MALLOC, name " '" #ptr "': %d at %p.\n", \
558 POISON(ptr, 0x5a, size)
560 #else /* !OBD_DEBUG_MEMUSAGE */
562 #define OBD_ALLOC_POST(ptr, size, name) ((void)0)
563 #define OBD_FREE_PRE(ptr, size, name) ((void)0)
565 #endif /* !OBD_DEBUG_MEMUSAGE */
567 #if defined(LUSTRE_UTILS) /* this version is for utils only */
568 #define OBD_ALLOC_GFP(ptr, size, gfp_mask) \
570 (ptr) = cfs_alloc(size, (gfp_mask)); \
571 if (unlikely((ptr) == NULL)) { \
572 CERROR("kmalloc of '" #ptr "' (%d bytes) failed at %s:%d\n", \
573 (int)(size), __FILE__, __LINE__); \
575 memset(ptr, 0, size); \
576 CDEBUG(D_MALLOC, "kmalloced '" #ptr "': %d at %p\n", \
580 #else /* this version is for the kernel and liblustre */
581 #define OBD_ALLOC_GFP(ptr, size, gfp_mask) \
583 (ptr) = cfs_alloc(size, (gfp_mask)); \
584 if (unlikely((ptr) == NULL)) { \
585 CERROR("kmalloc of '" #ptr "' (%d bytes) failed\n", \
587 CERROR(LPU64" total bytes allocated by Lustre, %d by LNET\n", \
588 obd_memory_sum(), atomic_read(&libcfs_kmemory)); \
590 memset(ptr, 0, size); \
591 OBD_ALLOC_POST(ptr, size, "kmalloced"); \
596 #ifndef OBD_ALLOC_MASK
597 # define OBD_ALLOC_MASK CFS_ALLOC_IO
600 #define OBD_ALLOC(ptr, size) OBD_ALLOC_GFP(ptr, size, OBD_ALLOC_MASK)
601 #define OBD_ALLOC_WAIT(ptr, size) OBD_ALLOC_GFP(ptr, size, CFS_ALLOC_STD)
602 #define OBD_ALLOC_PTR(ptr) OBD_ALLOC(ptr, sizeof *(ptr))
603 #define OBD_ALLOC_PTR_WAIT(ptr) OBD_ALLOC_WAIT(ptr, sizeof *(ptr))
606 # define OBD_VMALLOC(ptr, size) OBD_ALLOC(ptr, size)
608 # define OBD_VMALLOC(ptr, size) \
610 (ptr) = cfs_alloc_large(size); \
611 if (unlikely((ptr) == NULL)) { \
612 CERROR("vmalloc of '" #ptr "' (%d bytes) failed\n", \
614 CERROR(LPU64" total bytes allocated by Lustre, %d by LNET\n", \
615 obd_memory_sum(), atomic_read(&libcfs_kmemory)); \
617 memset(ptr, 0, size); \
618 OBD_ALLOC_POST(ptr, size, "vmalloced"); \
623 #ifdef CONFIG_DEBUG_SLAB
624 #define POISON(ptr, c, s) do {} while (0)
625 #define POISON_PTR(ptr) ((void)0)
627 #define POISON(ptr, c, s) memset(ptr, c, s)
628 #define POISON_PTR(ptr) (ptr) = (void *)0xdeadbeef
632 #define POISON_PAGE(page, val) do { memset(kmap(page), val, CFS_PAGE_SIZE); \
633 kunmap(page); } while (0)
635 #define POISON_PAGE(page, val) do { } while (0)
639 #define OBD_FREE(ptr, size) \
641 OBD_FREE_PRE(ptr, size, "kfreed"); \
648 # ifdef HAVE_CALL_RCU_PARAM
649 # define my_call_rcu(rcu, cb) call_rcu(rcu, cb, rcu)
651 # define my_call_rcu(rcu, cb) call_rcu(rcu, cb)
654 # define my_call_rcu(rcu, cb) (cb)(rcu)
657 #define OBD_FREE_RCU_CB(ptr, size, handle, free_cb) \
659 struct portals_handle *__h = (handle); \
661 __h->h_ptr = (ptr); \
662 __h->h_size = (size); \
663 __h->h_free_cb = (void (*)(void *, size_t))(free_cb); \
664 my_call_rcu(&__h->h_rcu, class_handle_free_cb); \
667 #define OBD_FREE_RCU(ptr, size, handle) OBD_FREE_RCU_CB(ptr, size, handle, NULL)
670 #define OBD_FREE(ptr, size) ((void)(size), free((ptr)))
671 #define OBD_FREE_RCU(ptr, size, handle) (OBD_FREE(ptr, size))
672 #define OBD_FREE_RCU_CB(ptr, size, handle, cb) ((*(cb))(ptr, size))
673 #endif /* ifdef __KERNEL__ */
676 # define OBD_VFREE(ptr, size) OBD_FREE(ptr, size)
678 # define OBD_VFREE(ptr, size) \
680 OBD_FREE_PRE(ptr, size, "vfreed"); \
681 cfs_free_large(ptr); \
686 /* we memset() the slab object to 0 when allocation succeeds, so DO NOT
687 * HAVE A CTOR THAT DOES ANYTHING. its work will be cleared here. we'd
688 * love to assert on that, but slab.c keeps kmem_cache_s all to itself. */
689 #define OBD_SLAB_ALLOC(ptr, slab, type, size) \
691 LASSERT(!in_interrupt()); \
692 (ptr) = cfs_mem_cache_alloc(slab, (type)); \
693 if (unlikely((ptr) == NULL)) { \
694 CERROR("slab-alloc of '"#ptr"' (%d bytes) failed\n", \
696 CERROR(LPU64" total bytes allocated by Lustre, %d by LNET\n", \
697 obd_memory_sum(), atomic_read(&libcfs_kmemory)); \
699 memset(ptr, 0, size); \
700 OBD_ALLOC_POST(ptr, size, "slab-alloced"); \
704 #define OBD_FREE_PTR(ptr) OBD_FREE(ptr, sizeof *(ptr))
706 #define OBD_SLAB_FREE(ptr, slab, size) \
708 OBD_FREE_PRE(ptr, size, "slab-freed"); \
709 cfs_mem_cache_free(slab, ptr); \
713 #define OBD_SLAB_ALLOC_PTR(ptr, slab) \
714 OBD_SLAB_ALLOC((ptr), (slab), CFS_ALLOC_STD, sizeof *(ptr))
715 #define OBD_SLAB_FREE_PTR(ptr, slab) \
716 OBD_SLAB_FREE((ptr), (slab), sizeof *(ptr))
718 #define KEY_IS(str) (keylen >= strlen(str) && strncmp(key, str, keylen) == 0)
720 /* Wrapper for contiguous page frame allocation */
721 #define OBD_PAGES_ALLOC(ptr, order, gfp_mask) \
723 (ptr) = cfs_alloc_pages(gfp_mask, order); \
724 if (unlikely((ptr) == NULL)) { \
725 CERROR("alloc_pages of '" #ptr "' %d page(s) / "LPU64" bytes "\
726 "failed\n", (int)(1 << (order)), \
727 (__u64)((1 << (order)) << CFS_PAGE_SHIFT)); \
728 CERROR(LPU64" total bytes and "LPU64" total pages " \
729 "("LPU64" bytes) allocated by Lustre, " \
730 "%d total bytes by LNET\n", \
732 obd_pages_sum() << CFS_PAGE_SHIFT, \
734 atomic_read(&libcfs_kmemory)); \
736 obd_pages_add(order); \
737 CDEBUG(D_MALLOC, "alloc_pages '" #ptr "': %d page(s) / " \
738 LPU64" bytes at %p.\n", \
739 (int)(1 << (order)), \
740 (__u64)((1 << (order)) << CFS_PAGE_SHIFT), ptr); \
744 #define OBD_PAGE_ALLOC(ptr, gfp_mask) \
745 OBD_PAGES_ALLOC(ptr, 0, gfp_mask)
747 #define OBD_PAGES_FREE(ptr, order) \
750 obd_pages_sub(order); \
751 CDEBUG(D_MALLOC, "free_pages '" #ptr "': %d page(s) / "LPU64" bytes " \
753 (int)(1 << (order)), (__u64)((1 << (order)) << CFS_PAGE_SHIFT),\
755 __cfs_free_pages(ptr, order); \
756 (ptr) = (void *)0xdeadbeef; \
759 #define OBD_PAGE_FREE(ptr) OBD_PAGES_FREE(ptr, 0)
761 #if defined(__linux__)
762 #include <linux/obd_support.h>
763 #elif defined(__APPLE__)
764 #include <darwin/obd_support.h>
765 #elif defined(__WINNT__)
766 #include <winnt/obd_support.h>
768 #error Unsupported operating system.