[PATCH V2] eventfd: move 'eventfd-count' printing out of spinlock

From: wenyang . linux
Date: Mon Oct 02 2023 - 11:26:48 EST


From: Wen Yang <wenyang.linux@xxxxxxxxxxx>

When printing eventfd->count, interrupts will be disabled and a spinlock
will be obtained, competing with eventfd_write().

In a single CPU container, one process frequently calls eventfd_write()
and another process frequently prints eventfd->count, as follows:

Tasks: 372 total, 3 running, 369 sleeping, 0 stopped, 0 zombie
%Cpu(s): 1.8 us, 4.5 sy, 0.0 ni, 92.5 id, 0.0 wa, 0.0 hi, 1.2 si, 0.0 st
MiB Mem : 13595.9 total, 4627.0 free, 2536.6 used, 6432.2 buff/cache
MiB Swap: 2048.0 total, 2048.0 free, 0.0 used. 10727.2 avail Mem

PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND P
1893151 test 20 0 2364 896 896 R 49.2 0.0 6:03.49 a.out 3
1893080 test 20 0 244748 5376 4224 R 48.8 0.0 6:52.34 test_zmq 3

# ./perf stat -e 'probe:eventfd_write' -I 10000
# time counts unit events
10.004950650 396,394 probe:eventfd_write
20.015871354 396,403 probe:eventfd_write
30.026916173 397,624 probe:eventfd_write
40.037815761 396,363 probe:eventfd_write
50.048964887 396,168 probe:eventfd_write
60.059711824 396,656 probe:eventfd_write
70.065021841 397,286 probe:eventfd_write
80.072792225 397,476 probe:eventfd_write
90.083883162 394,014 probe:eventfd_write

By moving the "eventfd-count" print out of the spinlock and merging
multiple seq_printf() into one, it could improve a bit.
After applying this patch, the competition has been slightly alleviated,
and eventfd_writ() has more opportunities to be executed, as follows:

# ./perf stat -e 'probe:eventfd_write' -I 10000
# time counts unit events
10.010058911 397,550 probe:eventfd_write
20.021065452 397,771 probe:eventfd_write
30.032054749 397,509 probe:eventfd_write
40.034853809 397,605 probe:eventfd_write
50.045754449 396,926 probe:eventfd_write
60.056373938 396,867 probe:eventfd_write
70.067280837 397,542 probe:eventfd_write
80.078232498 397,011 probe:eventfd_write
90.089215771 397,311 probe:eventfd_write

Signed-off-by: Wen Yang <wenyang.linux@xxxxxxxxxxx>
Cc: Alexander Viro <viro@xxxxxxxxxxxxxxxxxx>
Cc: Jens Axboe <axboe@xxxxxxxxx>
Cc: Christian Brauner <brauner@xxxxxxxxxx>
Cc: Christoph Hellwig <hch@xxxxxx>
Cc: Dylan Yudaken <dylany@xxxxxx>
Cc: David Woodhouse <dwmw@xxxxxxxxxxxx>
Cc: Matthew Wilcox <willy@xxxxxxxxxxxxx>
Cc: Eric Biggers <ebiggers@xxxxxxxxxx>
Cc: linux-fsdevel@xxxxxxxxxxxxxxx
Cc: linux-kernel@xxxxxxxxxxxxxxx
---
fs/eventfd.c | 13 +++++++++----
1 file changed, 9 insertions(+), 4 deletions(-)

diff --git a/fs/eventfd.c b/fs/eventfd.c
index 8aa36cd37351..3b893f21923d 100644
--- a/fs/eventfd.c
+++ b/fs/eventfd.c
@@ -295,13 +295,18 @@ static ssize_t eventfd_write(struct file *file, const char __user *buf, size_t c
static void eventfd_show_fdinfo(struct seq_file *m, struct file *f)
{
struct eventfd_ctx *ctx = f->private_data;
+ unsigned long long cnt;

spin_lock_irq(&ctx->wqh.lock);
- seq_printf(m, "eventfd-count: %16llx\n",
- (unsigned long long)ctx->count);
+ cnt = ctx->count;
spin_unlock_irq(&ctx->wqh.lock);
- seq_printf(m, "eventfd-id: %d\n", ctx->id);
- seq_printf(m, "eventfd-semaphore: %d\n",
+
+ seq_printf(m,
+ "eventfd-count: %16llx\n"
+ "eventfd-id: %d\n"
+ "eventfd-semaphore: %d\n",
+ cnt,
+ ctx->id,
!!(ctx->flags & EFD_SEMAPHORE));
}
#endif
--
2.25.1