From patchwork Thu Sep 21 07:57:55 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Max Kellermann X-Patchwork-Id: 142986 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:612c:172:b0:3f2:4152:657d with SMTP id h50csp5190992vqi; Thu, 21 Sep 2023 15:58:02 -0700 (PDT) X-Google-Smtp-Source: AGHT+IEncLUr/+EgN565yIqsjy4cNVR99+nIyyPNccuMhV9sQ9VTbSDisg/PliWblQ6gUFv6c8Si X-Received: by 2002:a05:6808:234d:b0:3ad:29a4:f560 with SMTP id ef13-20020a056808234d00b003ad29a4f560mr6204991oib.34.1695337082066; Thu, 21 Sep 2023 15:58:02 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1695337082; cv=none; d=google.com; s=arc-20160816; b=C3eKuQ2UwCt7RZ8fqyG7iYF5U0/urwaDItc8xEO5+0HhUF4HGRbplQkCKbMHVjmdfW g3tGJlLtLLxskFo3fMq3fZglUni0Je9E5VYNPULWSlvcir/5fl8LrCfJcJo52OmtXDD5 JYBhZbWew5dGapIGgRcqjNhTwDOdZV6T+BY4fPoSBiIgGVFiP87wGbLPjBx5SNk+jl1n Ydl0P+RnqJyo61eSeVxGUj3YI2SeBkEIFWjVpH3LPhsXV6s/W8J9DQiOIUH7GO0exhXi q0va0HwvwI4I7peN4ADtWHlkdjbH0n9HdjUK+tc7jQFx0TrgnApLFcfwBgGNV2AXVv4y W/xQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=Ln8pAXov2i3W9jm1L6DymijJbQvZk3PVbO+zm8QE4Tk=; fh=/mmUFS5s71yRsPeN08HF9rYndG2tJREJfMy47/3Il/c=; b=Ll8NYY+684+txbN+v/1vXMktk+QH20y7yW6EXjF2iXt1L5sUA441g7WYGopo3Sb2lt J04iSyQxsUDzD5H8iHu0fea/ewH8HQuoWPq8yHaTkthOkTrqRh5VwECjOv/wzEEAxMYv jNXvDf7TfjhZL+08NA2+OCOIGW+GJycmL4neOL7dwYV0jipzVAujx7KxDJCWDS4pTtP6 Jpr0/CsNQWfOW3g8mh6YkRZ/nZ4YF930bwRWZERPuTW9k2C9uYxXe5HE5l3XyYbAYu9d tEluZiwlGaSnnh+/KkwXmBbr0aSf4UxC4VDhMWhvEEecamjrS9TY5+RzpFnMvwEwQU1f 3mcw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@ionos.com header.s=google header.b=S373tAqM; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.31 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=ionos.com Received: from morse.vger.email (morse.vger.email. [23.128.96.31]) by mx.google.com with ESMTPS id k124-20020a636f82000000b0056b0b30d05csi2258897pgc.245.2023.09.21.15.58.01 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 21 Sep 2023 15:58:02 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.31 as permitted sender) client-ip=23.128.96.31; Authentication-Results: mx.google.com; dkim=pass header.i=@ionos.com header.s=google header.b=S373tAqM; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.31 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=ionos.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by morse.vger.email (Postfix) with ESMTP id 3EB4983470F9; Thu, 21 Sep 2023 12:37:38 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at morse.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230394AbjIUThS (ORCPT + 28 others); Thu, 21 Sep 2023 15:37:18 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54638 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230385AbjIUThB (ORCPT ); Thu, 21 Sep 2023 15:37:01 -0400 Received: from mail-ej1-x635.google.com (mail-ej1-x635.google.com [IPv6:2a00:1450:4864:20::635]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 49C41FBB33 for ; Thu, 21 Sep 2023 12:12:33 -0700 (PDT) Received: by mail-ej1-x635.google.com with SMTP id a640c23a62f3a-9ad8bba8125so172000466b.3 for ; Thu, 21 Sep 2023 12:12:33 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ionos.com; s=google; t=1695323552; x=1695928352; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=Ln8pAXov2i3W9jm1L6DymijJbQvZk3PVbO+zm8QE4Tk=; b=S373tAqMm0OSLe8ccVaqtclKSPf62bjkwoM5pxazLkqmEKdxhANXGyVBOnJ7cl67qI uKgX5YBDYRB52PJeAvKAnzl47FChzi1EfnmrOlgAm0hTPJ2WXUBzPYbAoQSoHMtczQqL 4Pl7/deynA/1c9WoEHSFX+9WsBnSFV7mzyi6deZkdVOWpmB1NNL3nLt1IxON6QR5hnb5 Nye9kUbKCHU1XH+0eVRxFe0P9XIVUpEqRO4MF+RjURkWSWTurYab3OeWtoIKVXvTZ7EE 52kqwsVEGbb8+UC0oLuVKRHSm3WnFm5c9pxyfg6lazHyYOlmSYQrPNt63lwWPUy0QKOO domQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1695323552; x=1695928352; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=Ln8pAXov2i3W9jm1L6DymijJbQvZk3PVbO+zm8QE4Tk=; b=YaYHJXVwHgS2LbUv+i3zzpKgKpnjCBNNySNSD/PwLDMC6CmzQhQaiVIUXCysVHfVKU VgyErS/Qi2VCyKr3WwD5ZuzEarecTHrPlXMhCSpzhZ9UyQUf06Yot7kW56QfjS5ziuNZ mqs7EGG7qRsf/RRLCxvWnvqbRLFkCbSWxiJp1Xwbow94fHtq76sVv9rQbYmNtmT7ZuB1 u1l5ksi0MdiivoAkfCRm10UOPlowMgGi5psjDiQhFE8smawWkwttSWrU0RItCLotevxA 4LPlSyCihl/rYRqjKdbk6kjApX2YC5WB/qvMejus5E2xuIElYsQtvPmE0OEgyXoEE/pd JhIg== X-Gm-Message-State: AOJu0YynWRxy//oE23VqrT5VZltlx/O2lsfP5StkclBZQGLrd+Mzf38a dfONiusWyToYWSXvA+6587uUVg2m/zLnc0l/Rvc= X-Received: by 2002:a7b:c3cf:0:b0:402:f536:2d3e with SMTP id t15-20020a7bc3cf000000b00402f5362d3emr4452684wmj.14.1695283084637; Thu, 21 Sep 2023 00:58:04 -0700 (PDT) Received: from heron.intern.cm-ag (p200300dc6f209c00529a4cfffe3dd983.dip0.t-ipconnect.de. [2003:dc:6f20:9c00:529a:4cff:fe3d:d983]) by smtp.gmail.com with ESMTPSA id v4-20020a05600c214400b003fef19bb55csm1151252wml.34.2023.09.21.00.58.04 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 21 Sep 2023 00:58:04 -0700 (PDT) From: Max Kellermann To: viro@zeniv.linux.org.uk, brauner@kernel.org, howells@redhat.com Cc: linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org, Max Kellermann Subject: [PATCH 4/4] fs/pipe: use spinlock in pipe_read() only if there is a watch_queue Date: Thu, 21 Sep 2023 09:57:55 +0200 Message-Id: <20230921075755.1378787-4-max.kellermann@ionos.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230921075755.1378787-1-max.kellermann@ionos.com> References: <20230921075755.1378787-1-max.kellermann@ionos.com> MIME-Version: 1.0 X-Spam-Status: No, score=-0.8 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI, SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on morse.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (morse.vger.email [0.0.0.0]); Thu, 21 Sep 2023 12:37:38 -0700 (PDT) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1777689776273566473 X-GMAIL-MSGID: 1777689776273566473 If there is no watch_queue, holding the pipe mutex is enough to prevent concurrent writes, and we can avoid the spinlock. O_NOTIFICATION_QUEUE is an exotic and rarely used feature, and of all the pipes that exist at any given time, only very few actually have a watch_queue, therefore it appears worthwile to optimize the common case. This patch does not optimize pipe_resize_ring() where the spinlocks could be avoided as well; that does not seem like a worthwile optimization because this function is not called often. Related commits: - commit 8df441294dd3 ("pipe: Check for ring full inside of the spinlock in pipe_write()") - commit b667b8673443 ("pipe: Advance tail pointer inside of wait spinlock in pipe_read()") - commit 189b0ddc2451 ("pipe: Fix missing lock in pipe_resize_ring()") Signed-off-by: Max Kellermann --- fs/pipe.c | 32 ++++++++++++++++++++++++++------ 1 file changed, 26 insertions(+), 6 deletions(-) diff --git a/fs/pipe.c b/fs/pipe.c index 939def02c18c..da557eff9560 100644 --- a/fs/pipe.c +++ b/fs/pipe.c @@ -322,14 +322,34 @@ pipe_read(struct kiocb *iocb, struct iov_iter *to) if (!buf->len) { pipe_buf_release(pipe, buf); - spin_lock_irq(&pipe->rd_wait.lock); + + if (pipe_has_watch_queue(pipe)) { + /* if the pipe has a + * watch_queue, we need + * additional protection by + * the spinlock because + * notifications get posted + * with only this spinlock, no + * mutex + */ + + spin_lock_irq(&pipe->rd_wait.lock); #ifdef CONFIG_WATCH_QUEUE - if (buf->flags & PIPE_BUF_FLAG_LOSS) - pipe->note_loss = true; + if (buf->flags & PIPE_BUF_FLAG_LOSS) + pipe->note_loss = true; #endif - tail++; - pipe->tail = tail; - spin_unlock_irq(&pipe->rd_wait.lock); + tail++; + pipe->tail = tail; + spin_unlock_irq(&pipe->rd_wait.lock); + } else { + /* without a watch_queue, we + * can simply increment the + * tail without the spinlock - + * the mutex is enough + */ + + pipe->tail = ++tail; + } } total_len -= chars; if (!total_len)