Received: by 2002:a05:7412:31a9:b0:e2:908c:2ebd with SMTP id et41csp4861534rdb; Fri, 15 Sep 2023 14:57:03 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFoOtzeA3FCqmdwJCYDl1FlBIo7OBEs4cMLb3J0mtiCrfOT6YyWhz6ilGRzeVm11F5rj99S X-Received: by 2002:a05:6a00:1805:b0:68b:ff3b:e14a with SMTP id y5-20020a056a00180500b0068bff3be14amr3178912pfa.18.1694815023599; Fri, 15 Sep 2023 14:57:03 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1694815023; cv=none; d=google.com; s=arc-20160816; b=EPrRYh4c4svPQp+lX7nA5Ha3BSeSPAKiWbvbHL8nvfGYsPXP1t8h/c23nFNTLqJtiu BNWJu5lvJkMkG7d+HCOeR4/mrFnJYA14iJlwMBIxDV2E+3NPcg+kC8RtI6igeyD/RM88 1u7LIM+EKv38E19JuCcD1TGKY8h6tP6jB8opPGyFunEFZjqfWfgc29xvgGrbUgUg0/h3 nzvFhvmbtNBn4HN1WmZvsD0pyvG9w1b3rd16xpiCFvVB05dJeHXn1r8Ic8sLkPaZOG38 RnWUvBZWpnSUjSjI7/+xVnw8xNPDGQ0UKJgEaPv4XuDArMvrK4F/djJWznAi6nwoKzFh k6RQ== 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=3Pka9W0lBex+AQwPauK3FVj6595pxpOZ2H5fxz/mNAA=; fh=Exu8p2J0bt/MoYkEByA/Woj5EWTdUoRsvJYmK88z1hY=; b=w6hO6eiul6IDPHtyLAIDCvSq4WQLMjRH/AMJy5fmizfSxrd/XFeZ10vYmweI1IJhBF q8lIyUEAsnyj1Q5PAg+ERKyWSR89A47fn2+CDVnWa+KjtGtH2StEoelBcCpikMCzCWdv jbyvwK7DnzD2kSH4TZC1b4vFU9Fi0jVPPyAiE9XnTB02clbK1OF/m5C5XcBhWtmDqYHb 9GUHWKUp1yF7J2N3IBf49rYwqniAnhknC4DpgLOfeb7RUo4cJrYkzA1EIgpLNFICq6e4 eiFRzq0Er9yeGAQbkfuRs/wIEIrbpgunTdlegZHjTwEXcUs6kQK66kmiBf8GaHTgPGLV gA4g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@infradead.org header.s=casper.20170209 header.b="dFE/BuzQ"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:2 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from agentk.vger.email (agentk.vger.email. [2620:137:e000::3:2]) by mx.google.com with ESMTPS id y1-20020a62ce01000000b0068a676c5f2csi3750725pfg.32.2023.09.15.14.57.02 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 15 Sep 2023 14:57:03 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:2 as permitted sender) client-ip=2620:137:e000::3:2; Authentication-Results: mx.google.com; dkim=pass header.i=@infradead.org header.s=casper.20170209 header.b="dFE/BuzQ"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:2 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by agentk.vger.email (Postfix) with ESMTP id ED5D7807FCFF; Fri, 15 Sep 2023 11:38:24 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at agentk.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236654AbjIOSh6 (ORCPT + 99 others); Fri, 15 Sep 2023 14:37:58 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42038 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236593AbjIOShY (ORCPT ); Fri, 15 Sep 2023 14:37:24 -0400 Received: from casper.infradead.org (casper.infradead.org [IPv6:2001:8b0:10b:1236::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D42151A5; Fri, 15 Sep 2023 11:37:16 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=casper.20170209; h=Content-Transfer-Encoding:MIME-Version: References:In-Reply-To:Message-Id:Date:Subject:Cc:To:From:Sender:Reply-To: Content-Type:Content-ID:Content-Description; bh=3Pka9W0lBex+AQwPauK3FVj6595pxpOZ2H5fxz/mNAA=; b=dFE/BuzQ/4dk84AB1Iba3+XdrC JXMu/iEd34lZucm5IbEtzAh+uiXBzC6JWIWXJ6L7G0viTTqwusion1sIbrNWTucYQR93SKyF/Zplk 9ahMcug8JjkZUPz4oWGKulmYYR/++Y385/j2XFDUIPdup9V4Z15DB41Yg2t3NuIfP7KsQttats7Uf BTf4Hpf7CqATNEDHv1XhExUH5g3e0/knqGKLDjxfObp04SXaNypzCk5zeSr3WHLluqDpuJylaIadD O7R+3Y3zcxD3M5re5XX7GAOJy7py560s5Kow44PupHQ/cHTyaIw+bxxmbsRClEkRGDt8tHkwSaLr/ Nmqa0TOw==; Received: from willy by casper.infradead.org with local (Exim 4.94.2 #2 (Red Hat Linux)) id 1qhDgk-00BMJ2-9B; Fri, 15 Sep 2023 18:37:10 +0000 From: "Matthew Wilcox (Oracle)" To: linux-kernel@vger.kernel.org Cc: "Matthew Wilcox (Oracle)" , linux-fsdevel@vger.kernel.org, linux-arch@vger.kernel.org, torvalds@linux-foundation.org, Nicholas Piggin Subject: [PATCH 15/17] mm: Add folio_xor_flags_has_waiters() Date: Fri, 15 Sep 2023 19:37:05 +0100 Message-Id: <20230915183707.2707298-16-willy@infradead.org> X-Mailer: git-send-email 2.37.1 In-Reply-To: <20230915183707.2707298-1-willy@infradead.org> References: <20230915183707.2707298-1-willy@infradead.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-0.9 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI, SPF_HELO_NONE,SPF_PASS autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on agentk.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 (agentk.vger.email [0.0.0.0]); Fri, 15 Sep 2023 11:38:25 -0700 (PDT) Optimise folio_end_read() by setting the uptodate bit at the same time we clear the unlock bit. This saves at least one memory barrier and one write-after-write hazard. Signed-off-by: Matthew Wilcox (Oracle) --- include/linux/page-flags.h | 19 +++++++++++++++++++ mm/filemap.c | 14 +++++++++++--- 2 files changed, 30 insertions(+), 3 deletions(-) diff --git a/include/linux/page-flags.h b/include/linux/page-flags.h index 5c02720c53a5..a88e64acebfe 100644 --- a/include/linux/page-flags.h +++ b/include/linux/page-flags.h @@ -692,6 +692,25 @@ TESTPAGEFLAG_FALSE(Ksm, ksm) u64 stable_page_flags(struct page *page); +/** + * folio_xor_flags_has_waiters - Change some folio flags. + * @folio: The folio. + * @mask: Bits set in this word will be changed. + * + * This must only be used for flags which are changed with the folio + * lock held. For example, it is unsafe to use for PG_dirty as that + * can be set without the folio lock held. It can also only be used + * on flags which are in the range 0-6 as some of the implementations + * only affect those bits. + * + * Return: Whether there are tasks waiting on the folio. + */ +static inline bool folio_xor_flags_has_waiters(struct folio *folio, + unsigned long mask) +{ + return xor_unlock_is_negative_byte(mask, folio_flags(folio, 0)); +} + /** * folio_test_uptodate - Is this folio up to date? * @folio: The folio. diff --git a/mm/filemap.c b/mm/filemap.c index 330e21da6863..8262b85593be 100644 --- a/mm/filemap.c +++ b/mm/filemap.c @@ -1499,7 +1499,7 @@ void folio_unlock(struct folio *folio) BUILD_BUG_ON(PG_waiters != 7); BUILD_BUG_ON(PG_locked > 7); VM_BUG_ON_FOLIO(!folio_test_locked(folio), folio); - if (xor_unlock_is_negative_byte(1 << PG_locked, folio_flags(folio, 0))) + if (folio_xor_flags_has_waiters(folio, 1 << PG_locked)) folio_wake_bit(folio, PG_locked); } EXPORT_SYMBOL(folio_unlock); @@ -1520,9 +1520,17 @@ EXPORT_SYMBOL(folio_unlock); */ void folio_end_read(struct folio *folio, bool success) { + unsigned long mask = 1 << PG_locked; + + /* Must be in bottom byte for x86 to work */ + BUILD_BUG_ON(PG_uptodate > 7); + VM_BUG_ON_FOLIO(!folio_test_locked(folio), folio); + VM_BUG_ON_FOLIO(folio_test_uptodate(folio), folio); + if (success) - folio_mark_uptodate(folio); - folio_unlock(folio); + mask |= 1 << PG_uptodate; + if (folio_xor_flags_has_waiters(folio, mask)) + folio_wake_bit(folio, PG_locked); } EXPORT_SYMBOL(folio_end_read); -- 2.40.1