From patchwork Mon Oct 2 15:13:02 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Wen Yang X-Patchwork-Id: 13406374 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 05BC5E784BD for ; Mon, 2 Oct 2023 15:26:47 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S238077AbjJBP0s (ORCPT ); Mon, 2 Oct 2023 11:26:48 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46082 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S237928AbjJBP0r (ORCPT ); Mon, 2 Oct 2023 11:26:47 -0400 Received: from out203-205-221-202.mail.qq.com (out203-205-221-202.mail.qq.com [203.205.221.202]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B0726A9; Mon, 2 Oct 2023 08:26:43 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=foxmail.com; s=s201512; t=1696260400; bh=HdkrU/Yo/BYxGx0eytoTbyvF4iLbWStQw9vUbY4Xghw=; h=From:To:Cc:Subject:Date; b=GlL6mRpT7MmdmgVq6Okdz9omjEHFLagjUFK+XPAMD3z3ThAkg05O2C0x294TZv0GK q1zkahtYhsvsARkWo2CV2L3Adl6h9sfIFch4qYmdXERaGq7Kwt4SJRzKrO2wlG6QHQ ZFBeRMvOmWp1eg5wC+3+zXNOvkplIttPeKfPEwFY= Received: from localhost.localdomain ([240e:430:cc3:362a:ac65:29f0:b0a3:8952]) by newxmesmtplogicsvrsza12-0.qq.com (NewEsmtp) with SMTP id 3461804C; Mon, 02 Oct 2023 23:13:06 +0800 X-QQ-mid: xmsmtpt1696259586tyiyz6z58 Message-ID: X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u+z4IHvEH3Bifp84EW0L/ByLsoj5B+z2uWbg== X-QQ-XMRINFO: Mp0Kj//9VHAxr69bL5MkOOs= From: wenyang.linux@foxmail.com To: Alexander Viro , Jens Axboe , Christian Brauner Cc: Wen Yang , Christoph Hellwig , Dylan Yudaken , David Woodhouse , Matthew Wilcox , Eric Biggers , linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH V2] eventfd: move 'eventfd-count' printing out of spinlock Date: Mon, 2 Oct 2023 23:13:02 +0800 X-OQ-MSGID: <20231002151302.3829-1-wenyang.linux@foxmail.com> X-Mailer: git-send-email 2.25.1 MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-fsdevel@vger.kernel.org From: Wen Yang 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 Cc: Alexander Viro Cc: Jens Axboe Cc: Christian Brauner Cc: Christoph Hellwig Cc: Dylan Yudaken Cc: David Woodhouse Cc: Matthew Wilcox Cc: Eric Biggers Cc: linux-fsdevel@vger.kernel.org Cc: linux-kernel@vger.kernel.org --- 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