Whamcloud - gitweb
LU-8760 lib: avoid unexpected out of order execution
[fs/lustre-release.git] / lustre / include / lustre_lib.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) 2007, 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  * lustre/include/lustre_lib.h
33  *
34  * Basic Lustre library routines.
35  */
36
37 #ifndef _LUSTRE_LIB_H
38 #define _LUSTRE_LIB_H
39
40 /** \defgroup lib lib
41  *
42  * @{
43  */
44
45 #include <libcfs/libcfs.h>
46 #include <lustre/lustre_idl.h>
47 #include <lustre_ver.h>
48 #include <uapi/linux/lustre_cfg.h>
49
50 /* target.c */
51 struct ptlrpc_request;
52 struct obd_export;
53 struct lu_target;
54 struct l_wait_info;
55 #include <lustre_ha.h>
56 #include <lustre_net.h>
57
58 #define LI_POISON 0x5a5a5a5a
59 #if BITS_PER_LONG > 32
60 # define LL_POISON 0x5a5a5a5a5a5a5a5aL
61 #else
62 # define LL_POISON 0x5a5a5a5aL
63 #endif
64 #define LP_POISON ((void *)LL_POISON)
65
66 #ifdef HAVE_SERVER_SUPPORT
67 int rev_import_init(struct obd_export *exp);
68 int target_handle_connect(struct ptlrpc_request *req);
69 int target_handle_disconnect(struct ptlrpc_request *req);
70 void target_destroy_export(struct obd_export *exp);
71 int target_handle_ping(struct ptlrpc_request *req);
72 void target_committed_to_req(struct ptlrpc_request *req);
73 void target_cancel_recovery_timer(struct obd_device *obd);
74 void target_stop_recovery_thread(struct obd_device *obd);
75 void target_cleanup_recovery(struct obd_device *obd);
76 int target_queue_recovery_request(struct ptlrpc_request *req,
77                                   struct obd_device *obd);
78 int target_bulk_io(struct obd_export *exp, struct ptlrpc_bulk_desc *desc,
79                    struct l_wait_info *lwi);
80 #endif
81
82 int target_pack_pool_reply(struct ptlrpc_request *req);
83 int do_set_info_async(struct obd_import *imp,
84                       int opcode, int version,
85                       size_t keylen, void *key,
86                       size_t vallen, void *val,
87                       struct ptlrpc_request_set *set);
88
89 void target_send_reply(struct ptlrpc_request *req, int rc, int fail_id);
90
91 /*
92  * l_wait_event is a flexible sleeping function, permitting simple caller
93  * configuration of interrupt and timeout sensitivity along with actions to
94  * be performed in the event of either exception.
95  *
96  * The first form of usage looks like this:
97  *
98  * struct l_wait_info lwi = LWI_TIMEOUT_INTR(timeout, timeout_handler,
99  *                                           intr_handler, callback_data);
100  * rc = l_wait_event(waitq, condition, &lwi);
101  *
102  * l_wait_event() makes the current process wait on 'waitq' until 'condition'
103  * is TRUE or a "killable" signal (SIGTERM, SIKGILL, SIGINT) is pending.  It
104  * returns 0 to signify 'condition' is TRUE, but if a signal wakes it before
105  * 'condition' becomes true, it optionally calls the specified 'intr_handler'
106  * if not NULL, and returns -EINTR.
107  *
108  * If a non-zero timeout is specified, signals are ignored until the timeout
109  * has expired.  At this time, if 'timeout_handler' is not NULL it is called.
110  * If it returns FALSE l_wait_event() continues to wait as described above with
111  * signals enabled.  Otherwise it returns -ETIMEDOUT.
112  *
113  * LWI_INTR(intr_handler, callback_data) is shorthand for
114  * LWI_TIMEOUT_INTR(0, NULL, intr_handler, callback_data)
115  *
116  * The second form of usage looks like this:
117  *
118  * struct l_wait_info lwi = LWI_TIMEOUT(timeout, timeout_handler);
119  * rc = l_wait_event(waitq, condition, &lwi);
120  *
121  * This form is the same as the first except that it COMPLETELY IGNORES
122  * SIGNALS.  The caller must therefore beware that if 'timeout' is zero, or if
123  * 'timeout_handler' is not NULL and returns FALSE, then the ONLY thing that
124  * can unblock the current process is 'condition' becoming TRUE.
125  *
126  * Another form of usage is:
127  * struct l_wait_info lwi = LWI_TIMEOUT_INTERVAL(timeout, interval,
128  *                                               timeout_handler);
129  * rc = l_wait_event(waitq, condition, &lwi);
130  * This is the same as previous case, but condition is checked once every
131  * 'interval' jiffies (if non-zero).
132  *
133  * Subtle synchronization point: this macro does *not* necessary takes
134  * wait-queue spin-lock before returning, and, hence, following idiom is safe
135  * ONLY when caller provides some external locking:
136  *
137  *             Thread1                            Thread2
138  *
139  *   l_wait_event(&obj->wq, ....);                                       (1)
140  *
141  *                                    wake_up(&obj->wq):                 (2)
142  *                                         spin_lock(&q->lock);          (2.1)
143  *                                         __wake_up_common(q, ...);     (2.2)
144  *                                         spin_unlock(&q->lock, flags); (2.3)
145  *
146  *   OBD_FREE_PTR(obj);                                                  (3)
147  *
148  * As l_wait_event() may "short-cut" execution and return without taking
149  * wait-queue spin-lock, some additional synchronization is necessary to
150  * guarantee that step (3) can begin only after (2.3) finishes.
151  *
152  * XXX nikita: some ptlrpc daemon threads have races of that sort.
153  *
154  */
155 static inline int back_to_sleep(void *arg)
156 {
157         return 0;
158 }
159
160 #define LWI_ON_SIGNAL_NOOP ((void (*)(void *))(-1))
161
162 struct l_wait_info {
163         cfs_duration_t lwi_timeout;
164         cfs_duration_t lwi_interval;
165         int            lwi_allow_intr;
166         int  (*lwi_on_timeout)(void *);
167         void (*lwi_on_signal)(void *);
168         void  *lwi_cb_data;
169 };
170
171 /* NB: LWI_TIMEOUT ignores signals completely */
172 #define LWI_TIMEOUT(time, cb, data)             \
173 ((struct l_wait_info) {                         \
174         .lwi_timeout    = time,                 \
175         .lwi_on_timeout = cb,                   \
176         .lwi_cb_data    = data,                 \
177         .lwi_interval   = 0,                    \
178         .lwi_allow_intr = 0                     \
179 })
180
181 #define LWI_TIMEOUT_INTERVAL(time, interval, cb, data)  \
182 ((struct l_wait_info) {                                 \
183         .lwi_timeout    = time,                         \
184         .lwi_on_timeout = cb,                           \
185         .lwi_cb_data    = data,                         \
186         .lwi_interval   = interval,                     \
187         .lwi_allow_intr = 0                             \
188 })
189
190 #define LWI_TIMEOUT_INTR(time, time_cb, sig_cb, data)   \
191 ((struct l_wait_info) {                                 \
192         .lwi_timeout    = time,                         \
193         .lwi_on_timeout = time_cb,                      \
194         .lwi_on_signal  = sig_cb,                       \
195         .lwi_cb_data    = data,                         \
196         .lwi_interval   = 0,                            \
197         .lwi_allow_intr = 0                             \
198 })
199
200 #define LWI_TIMEOUT_INTR_ALL(time, time_cb, sig_cb, data)       \
201 ((struct l_wait_info) {                                         \
202         .lwi_timeout    = time,                                 \
203         .lwi_on_timeout = time_cb,                              \
204         .lwi_on_signal  = sig_cb,                               \
205         .lwi_cb_data    = data,                                 \
206         .lwi_interval   = 0,                                    \
207         .lwi_allow_intr = 1                                     \
208 })
209
210 #define LWI_INTR(cb, data)  LWI_TIMEOUT_INTR(0, NULL, cb, data)
211
212 #define LUSTRE_FATAL_SIGS                                        \
213         (sigmask(SIGKILL) | sigmask(SIGINT) | sigmask(SIGTERM) | \
214          sigmask(SIGQUIT) | sigmask(SIGALRM))
215
216 /*
217  * Wait Queue
218  */
219 #ifndef HAVE___ADD_WAIT_QUEUE_EXCLUSIVE
220 static inline void __add_wait_queue_exclusive(wait_queue_head_t *q,
221                                               wait_queue_t *wait)
222 {
223         wait->flags |= WQ_FLAG_EXCLUSIVE;
224         __add_wait_queue(q, wait);
225 }
226 #endif /* HAVE___ADD_WAIT_QUEUE_EXCLUSIVE */
227
228 /**
229  * wait_queue_t of Linux (version < 2.6.34) is a FIFO list for exclusively
230  * waiting threads, which is not always desirable because all threads will
231  * be waken up again and again, even user only needs a few of them to be
232  * active most time. This is not good for performance because cache can
233  * be polluted by different threads.
234  *
235  * LIFO list can resolve this problem because we always wakeup the most
236  * recent active thread by default.
237  *
238  * NB: please don't call non-exclusive & exclusive wait on the same
239  * waitq if add_wait_queue_exclusive_head is used.
240  */
241 #define add_wait_queue_exclusive_head(waitq, link)              \
242 {                                                               \
243         unsigned long flags;                                    \
244                                                                 \
245         spin_lock_irqsave(&((waitq)->lock), flags);             \
246         __add_wait_queue_exclusive(waitq, link);                \
247         spin_unlock_irqrestore(&((waitq)->lock), flags);        \
248 }
249
250 /*
251  * wait for @condition to become true, but no longer than timeout, specified
252  * by @info.
253  */
254 #define __l_wait_event(wq, condition, info, ret, l_add_wait)                   \
255 do {                                                                           \
256         wait_queue_t __wait;                                                   \
257         cfs_duration_t __timeout = info->lwi_timeout;                          \
258         sigset_t   __blocked;                                              \
259         int   __allow_intr = info->lwi_allow_intr;                             \
260                                                                                \
261         ret = 0;                                                               \
262         if (condition)                                                         \
263                 break;                                                         \
264                                                                                \
265         init_waitqueue_entry(&__wait, current);                                \
266         l_add_wait(&wq, &__wait);                                              \
267                                                                                \
268         /* Block all signals (just the non-fatal ones if no timeout). */       \
269         if (info->lwi_on_signal != NULL && (__timeout == 0 || __allow_intr))   \
270                 __blocked = cfs_block_sigsinv(LUSTRE_FATAL_SIGS);              \
271         else                                                                   \
272                 __blocked = cfs_block_sigsinv(0);                              \
273                                                                                \
274         for (;;) {                                                             \
275                 set_current_state(TASK_INTERRUPTIBLE);                         \
276                                                                                \
277                 /* To guarantee that the condition check will be done */       \
278                 /* after setting the thread state as TASK_INTERRUPTIBLE. */    \
279                 /* Otherwise, out-of-order execution may cause some race. */   \
280                 /* Consider the following real execution order: */             \
281                                                                                \
282                 /* 1. Thread1 checks condition on CPU1, gets false. */         \
283                 /* 2. Thread2 sets condition on CPU2. */                       \
284                 /* 3. Thread2 calls wake_up() on CPU2 to wake the threads */   \
285                 /*    with state TASK_INTERRUPTIBLE | TASK_UNINTERRUPTIBLE. */ \
286                 /*    But the Thread1's state is TASK_RUNNING at that time. */ \
287                 /* 4. Thread1 sets its state as TASK_INTERRUPTIBLE on CPU1, */ \
288                 /*    then schedule. */                                        \
289                                                                                \
290                 /* If the '__timeout' variable is zero, the Thread1 will */    \
291                 /* have no chance to check the condition again. */             \
292                                                                                \
293                 /* Generally, the interval between out-of-ordered step1 and */ \
294                 /* step4 is very tiny, as to above step2 and step3 cannot */   \
295                 /* happen. On some degree, it can explain why we seldom hit */ \
296                 /* related trouble. But such race really exists, especially */ \
297                 /* consider that the step1 and step4 can be interruptible. */  \
298                 /* So add barrier to avoid Thread1 out-of-order execution. */  \
299                 smp_mb();                                                      \
300                                                                                \
301                 if (condition)                                                 \
302                         break;                                                 \
303                                                                                \
304                 if (__timeout == 0) {                                          \
305                         schedule();                                            \
306                 } else {                                                       \
307                         cfs_duration_t interval = info->lwi_interval?          \
308                                              min_t(cfs_duration_t,             \
309                                                  info->lwi_interval,__timeout):\
310                                              __timeout;                        \
311                         cfs_duration_t remaining = schedule_timeout(interval); \
312                         __timeout = cfs_time_sub(__timeout,                    \
313                                             cfs_time_sub(interval, remaining));\
314                         if (__timeout == 0) {                                  \
315                                 if (info->lwi_on_timeout == NULL ||            \
316                                     info->lwi_on_timeout(info->lwi_cb_data)) { \
317                                         ret = -ETIMEDOUT;                      \
318                                         break;                                 \
319                                 }                                              \
320                                 /* Take signals after the timeout expires. */  \
321                                 if (info->lwi_on_signal != NULL)               \
322                                     (void)cfs_block_sigsinv(LUSTRE_FATAL_SIGS);\
323                         }                                                      \
324                 }                                                              \
325                                                                                \
326                 if (condition)                                                 \
327                         break;                                                 \
328                 if (signal_pending(current)) {                                 \
329                         if (info->lwi_on_signal != NULL &&                     \
330                             (__timeout == 0 || __allow_intr)) {                \
331                                 if (info->lwi_on_signal != LWI_ON_SIGNAL_NOOP) \
332                                         info->lwi_on_signal(info->lwi_cb_data);\
333                                 ret = -EINTR;                                  \
334                                 break;                                         \
335                         }                                                      \
336                         /* We have to do this here because some signals */     \
337                         /* are not blockable - ie from strace(1).       */     \
338                         /* In these cases we want to schedule_timeout() */     \
339                         /* again, because we don't want that to return  */     \
340                         /* -EINTR when the RPC actually succeeded.      */     \
341                         /* the recalc_sigpending() below will deliver the */   \
342                         /* signal properly.                             */     \
343                         cfs_clear_sigpending();                                \
344                 }                                                              \
345         }                                                                      \
346                                                                                \
347         cfs_restore_sigs(__blocked);                                           \
348                                                                                \
349         set_current_state(TASK_RUNNING);                                       \
350         remove_wait_queue(&wq, &__wait);                                       \
351 } while (0)
352
353
354 #define l_wait_event(wq, condition, info)                       \
355 ({                                                              \
356         int                 __ret;                              \
357         struct l_wait_info *__info = (info);                    \
358                                                                 \
359         __l_wait_event(wq, condition, __info,                   \
360                        __ret, add_wait_queue);                  \
361         __ret;                                                  \
362 })
363
364 #define l_wait_event_exclusive(wq, condition, info)             \
365 ({                                                              \
366         int                 __ret;                              \
367         struct l_wait_info *__info = (info);                    \
368                                                                 \
369         __l_wait_event(wq, condition, __info,                   \
370                        __ret, add_wait_queue_exclusive);        \
371         __ret;                                                  \
372 })
373
374 #define l_wait_event_exclusive_head(wq, condition, info)        \
375 ({                                                              \
376         int                 __ret;                              \
377         struct l_wait_info *__info = (info);                    \
378                                                                 \
379         __l_wait_event(wq, condition, __info,                   \
380                        __ret, add_wait_queue_exclusive_head);   \
381         __ret;                                                  \
382 })
383
384 #define l_wait_condition(wq, condition)                         \
385 ({                                                              \
386         struct l_wait_info lwi = { 0 };                         \
387         l_wait_event(wq, condition, &lwi);                      \
388 })
389
390 #define l_wait_condition_exclusive(wq, condition)               \
391 ({                                                              \
392         struct l_wait_info lwi = { 0 };                         \
393         l_wait_event_exclusive(wq, condition, &lwi);            \
394 })
395
396 #define l_wait_condition_exclusive_head(wq, condition)          \
397 ({                                                              \
398         struct l_wait_info lwi = { 0 };                         \
399         l_wait_event_exclusive_head(wq, condition, &lwi);       \
400 })
401
402 /** @} lib */
403
404 #endif /* _LUSTRE_LIB_H */