Skip to content

Commit

Permalink
sched/headers: fix up header file dependency on <linux/sched/signal.h>
Browse files Browse the repository at this point in the history
The scheduler header file split and cleanups ended up exposing a few
nasty header file dependencies, and in particular it showed how we in
<linux/wait.h> ended up depending on "signal_pending()", which now comes
from <linux/sched/signal.h>.

That's a very subtle and annoying dependency, which already caused a
semantic merge conflict (see commit e58bc92 "Pull overlayfs updates
from Miklos Szeredi", which added that fixup in the merge commit).

It turns out that we can avoid this dependency _and_ improve code
generation by moving the guts of the fairly nasty helper #define
__wait_event_interruptible_locked() to out-of-line code.  The code that
includes the signal_pending() check is all in the slow-path where we
actually go to sleep waiting for the event anyway, so using a helper
function is the right thing to do.

Using a helper function is also what we already did for the non-locked
versions, see the "__wait_event*()" macros and the "prepare_to_wait*()"
set of helper functions.

We might want to try to unify all these macro games, we have a _lot_ of
subtly different wait-event loops.  But this is the minimal patch to fix
the annoying header dependency.

Acked-by: Ingo Molnar <[email protected]>
Signed-off-by: Linus Torvalds <[email protected]>
  • Loading branch information
torvalds committed Mar 8, 2017
1 parent ec3b93a commit bd0f9b3
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 21 deletions.
31 changes: 10 additions & 21 deletions include/linux/wait.h
Original file line number Diff line number Diff line change
Expand Up @@ -620,30 +620,19 @@ do { \
__ret; \
})

extern int do_wait_intr(wait_queue_head_t *, wait_queue_t *);
extern int do_wait_intr_irq(wait_queue_head_t *, wait_queue_t *);

#define __wait_event_interruptible_locked(wq, condition, exclusive, irq) \
#define __wait_event_interruptible_locked(wq, condition, exclusive, fn) \
({ \
int __ret = 0; \
int __ret; \
DEFINE_WAIT(__wait); \
if (exclusive) \
__wait.flags |= WQ_FLAG_EXCLUSIVE; \
do { \
if (likely(list_empty(&__wait.task_list))) \
__add_wait_queue_tail(&(wq), &__wait); \
set_current_state(TASK_INTERRUPTIBLE); \
if (signal_pending(current)) { \
__ret = -ERESTARTSYS; \
__ret = fn(&(wq), &__wait); \
if (__ret) \
break; \
} \
if (irq) \
spin_unlock_irq(&(wq).lock); \
else \
spin_unlock(&(wq).lock); \
schedule(); \
if (irq) \
spin_lock_irq(&(wq).lock); \
else \
spin_lock(&(wq).lock); \
} while (!(condition)); \
__remove_wait_queue(&(wq), &__wait); \
__set_current_state(TASK_RUNNING); \
Expand Down Expand Up @@ -676,7 +665,7 @@ do { \
*/
#define wait_event_interruptible_locked(wq, condition) \
((condition) \
? 0 : __wait_event_interruptible_locked(wq, condition, 0, 0))
? 0 : __wait_event_interruptible_locked(wq, condition, 0, do_wait_intr))

/**
* wait_event_interruptible_locked_irq - sleep until a condition gets true
Expand All @@ -703,7 +692,7 @@ do { \
*/
#define wait_event_interruptible_locked_irq(wq, condition) \
((condition) \
? 0 : __wait_event_interruptible_locked(wq, condition, 0, 1))
? 0 : __wait_event_interruptible_locked(wq, condition, 0, do_wait_intr_irq))

/**
* wait_event_interruptible_exclusive_locked - sleep exclusively until a condition gets true
Expand Down Expand Up @@ -734,7 +723,7 @@ do { \
*/
#define wait_event_interruptible_exclusive_locked(wq, condition) \
((condition) \
? 0 : __wait_event_interruptible_locked(wq, condition, 1, 0))
? 0 : __wait_event_interruptible_locked(wq, condition, 1, do_wait_intr))

/**
* wait_event_interruptible_exclusive_locked_irq - sleep until a condition gets true
Expand Down Expand Up @@ -765,7 +754,7 @@ do { \
*/
#define wait_event_interruptible_exclusive_locked_irq(wq, condition) \
((condition) \
? 0 : __wait_event_interruptible_locked(wq, condition, 1, 1))
? 0 : __wait_event_interruptible_locked(wq, condition, 1, do_wait_intr_irq))


#define __wait_event_killable(wq, condition) \
Expand Down
39 changes: 39 additions & 0 deletions kernel/sched/wait.c
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,45 @@ long prepare_to_wait_event(wait_queue_head_t *q, wait_queue_t *wait, int state)
}
EXPORT_SYMBOL(prepare_to_wait_event);

/*
* Note! These two wait functions are entered with the
* wait-queue lock held (and interrupts off in the _irq
* case), so there is no race with testing the wakeup
* condition in the caller before they add the wait
* entry to the wake queue.
*/
int do_wait_intr(wait_queue_head_t *wq, wait_queue_t *wait)
{
if (likely(list_empty(&wait->task_list)))
__add_wait_queue_tail(wq, wait);

set_current_state(TASK_INTERRUPTIBLE);
if (signal_pending(current))
return -ERESTARTSYS;

spin_unlock(&wq->lock);
schedule();
spin_lock(&wq->lock);
return 0;
}
EXPORT_SYMBOL(do_wait_intr);

int do_wait_intr_irq(wait_queue_head_t *wq, wait_queue_t *wait)
{
if (likely(list_empty(&wait->task_list)))
__add_wait_queue_tail(wq, wait);

set_current_state(TASK_INTERRUPTIBLE);
if (signal_pending(current))
return -ERESTARTSYS;

spin_unlock_irq(&wq->lock);
schedule();
spin_lock_irq(&wq->lock);
return 0;
}
EXPORT_SYMBOL(do_wait_intr_irq);

/**
* finish_wait - clean up after waiting in a queue
* @q: waitqueue waited on
Expand Down

0 comments on commit bd0f9b3

Please sign in to comment.