]> git.ipfire.org Git - thirdparty/linux.git/commitdiff
wait: avoid spurious calls to prepare_to_wait_event() in ___wait_event()
authorMateusz Guzik <mjguzik@gmail.com>
Mon, 3 Mar 2025 23:04:09 +0000 (00:04 +0100)
committerChristian Brauner <brauner@kernel.org>
Tue, 4 Mar 2025 08:46:07 +0000 (09:46 +0100)
In vast majority of cases the condition determining whether the thread
can proceed is true after the first wake up.

However, even in that case the thread ends up calling into
prepare_to_wait_event() again, suffering a spurious irq + lock trip.

Then it calls into finish_wait() to unlink itself.

Note that in case of a pending signal the work done by
prepare_to_wait_event() gets ignored even without the change.

pre-check the condition after waking up instead.

Stats gathared during a kernel build:
bpftrace -e 'kprobe:prepare_to_wait_event,kprobe:finish_wait \
 { @[probe] = count(); }'

@[kprobe:finish_wait]: 392483
@[kprobe:prepare_to_wait_event]: 778690

As in calls to prepare_to_wait_event() almost double calls to
finish_wait(). This evens out with the patch.

Signed-off-by: Mateusz Guzik <mjguzik@gmail.com>
Link: https://lore.kernel.org/r/20250303230409.452687-4-mjguzik@gmail.com
Signed-off-by: Christian Brauner <brauner@kernel.org>
include/linux/wait.h

index 6d90ad97440876082512beeb0a59d2e09d082f23..3503fe822e38b710f0ae688995a8d8d6de62a588 100644 (file)
@@ -316,6 +316,9 @@ extern void init_wait_entry(struct wait_queue_entry *wq_entry, int flags);
                }                                                               \
                                                                                \
                cmd;                                                            \
+                                                                               \
+               if (condition)                                                  \
+                       break;                                                  \
        }                                                                       \
        finish_wait(&wq_head, &__wq_entry);                                     \
 __out: __ret;                                                                  \