From patchwork Thu Feb 15 12:17:54 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ryan Roberts X-Patchwork-Id: 13558302 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 kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 6A931C48BF0 for ; Thu, 15 Feb 2024 12:18:14 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 2906B8D0013; Thu, 15 Feb 2024 07:18:13 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 1F03D8D0001; Thu, 15 Feb 2024 07:18:13 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 0435B8D0013; Thu, 15 Feb 2024 07:18:12 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id DC4D88D0001 for ; Thu, 15 Feb 2024 07:18:12 -0500 (EST) Received: from smtpin02.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 78803A0FC9 for ; Thu, 15 Feb 2024 12:18:12 +0000 (UTC) X-FDA: 81793940424.02.26BC93B Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by imf13.hostedemail.com (Postfix) with ESMTP id BA04520010 for ; Thu, 15 Feb 2024 12:18:10 +0000 (UTC) Authentication-Results: imf13.hostedemail.com; dkim=none; dmarc=pass (policy=none) header.from=arm.com; spf=pass (imf13.hostedemail.com: domain of ryan.roberts@arm.com designates 217.140.110.172 as permitted sender) smtp.mailfrom=ryan.roberts@arm.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1707999490; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=lbpD6M3yiauchTnc5b8hwmaJxEY6qpI+eNSGlv7SoLM=; b=UWKmB8eKAz5df8eJOO96QghArtFLKkXxIzxLClf6EYqMWthlV6bqtNkH7xaSYt1/hTCrcV e/UtC0yasFqc5VwEg9k0JyppFRWzDrym6eiWiXKdbZF3QUf2YQm/qzZ6SUEsXuTcfEU+YB y/l+kuL36qLSouNeh6V0l1wpd+3DwwQ= ARC-Authentication-Results: i=1; imf13.hostedemail.com; dkim=none; dmarc=pass (policy=none) header.from=arm.com; spf=pass (imf13.hostedemail.com: domain of ryan.roberts@arm.com designates 217.140.110.172 as permitted sender) smtp.mailfrom=ryan.roberts@arm.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1707999490; a=rsa-sha256; cv=none; b=mcrEAlsgxjdzeQbNFkHIUMwyc2JnDDp1FtG9l2H3+rrGOR4b+0hEQ8IJl66usM/ubBGIUK 3is9hrf6JKm9kZAZS4y/e312cHA4tH3K2yD7XpsusCCZHHYFq1wQP30VXf6+uCCsBRCqlY 1l/lwjx2sGMqTT+IXI2fePEeJ9mdWuY= Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id B279B1480; Thu, 15 Feb 2024 04:18:50 -0800 (PST) Received: from e125769.cambridge.arm.com (e125769.cambridge.arm.com [10.1.196.26]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id 16F853F766; Thu, 15 Feb 2024 04:18:07 -0800 (PST) From: Ryan Roberts To: David Hildenbrand , Mark Rutland , Catalin Marinas , Will Deacon , Alexander Shishkin , Jiri Olsa , Ian Rogers , Adrian Hunter , Andrew Morton , Muchun Song Cc: Ryan Roberts , linux-arm-kernel@lists.infradead.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org Subject: [RFC PATCH v1 2/4] mm/gup: Use ptep_get_lockless_norecency() Date: Thu, 15 Feb 2024 12:17:54 +0000 Message-Id: <20240215121756.2734131-3-ryan.roberts@arm.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20240215121756.2734131-1-ryan.roberts@arm.com> References: <20240215121756.2734131-1-ryan.roberts@arm.com> MIME-Version: 1.0 X-Rspamd-Queue-Id: BA04520010 X-Rspam-User: X-Rspamd-Server: rspam05 X-Stat-Signature: jyfxn1rwjtezwn7c4dm48mma51wo6zpt X-HE-Tag: 1707999490-39397 X-HE-Meta: U2FsdGVkX19EzX/XdVAlpE9KBBGukW7vjZ1Ei6wMHdVO0vpnlMMJjAndDeCzbJ/RCMRKmN4q6PNGTNkShyFDGBsKeap0hS8jCdE26AGYOoJ2OTivMTrvvGvo9lQ1X2aSmtqz7fTxZp2dWS+gLbr0MMYkMg9HTnWealPnLXC2PVNYkAUKJKw0NCW+aD8NNsPzRlnSWYV2ep/XvZ85v2cUQDymkzYr0sMzIctKKaXWIUlspKB9q1Ge38z+IPnrSsniJZ+OEtbpVvWt78GTQmIafcYSABpBWmi23aWQMU8xVJNUVdJTGBL/e8jYhg6VXDiIiWSFEoXCOtXbHdbEikmNNgbPKgWM6+qj1by3ddml4uQ9cqWnOg0BcaFEYuzOItdK1aS+UhmNs3Op3jE+hOqQStN8AL90iYoE0WB1djq7hx3GJTmKhxboCCv3NEfnCCI6ZNamWSKhhsyU1tEC0+A9dMsNh/Lb27xwXU1+xRATPIqMvqmkS6Qp2ykWgQwch9CurbEDiYrrYVmnv4k1mqqSlC2TFt0FtrwJyLWbEoRukxtJ9idBYJOvDi/ilefsCHsJ/9eyXxT7kMQmZatjaOkFtxqL8y7FTrVxDhyyln6d3yieNV5Tm3x4/ky8d+SfUB0rQ1I9LdusU/kHypgIv05QtMm87JmXZEhhnLuUgIkjSffiE7vV7V18va+SGW2nTvAyoaMCRZh6/DgsvVZLlzTOV+N+Q1Oz0cfPG/dsqewHQF709MilfO+cAYVF8E4H+jVi6cmAjJQ6cQKVaIYTS40XhmB9QCu1lo1hz4PvRyQvZU2ffaUGpZAp2nPT0v+Pwlo8kfzJNirpQwQc7KM28aIr+b3pXLK+9u2z4E6zMpGui+8aeo7OCcPxNH9lso79oH8Aix0pvXC9jeW5pkrcb14t/pYbLxSKiu3Zq/8Pf3pkJMUDkCDqCmAPZZPe1roHv4t9+9WS3FNHOlFykV+iaYN Ks501R1P HGDRJlAKbtHF1A2lyMreI6aMxYKL1Ew2kjxuUNu/vCbjmQzhwm7KbFxXl8xWsi9JgkeBb9PKKhNaw6/vUekfMG1WLT04vq22KXJIDD5Bd5eeaZDxuUoK9Key9pjxKaXGrgMwinQNxRHO1Vm8USrKx/BncuELSn4gPKhEYaZ+o0WFZOo7qDuvJmy2BYpGCkqVQ/qy8fpaKSL0ckWA= X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: Gup needs to read ptes locklessly, so it uses ptep_get_lockless(). However, the returned access and dirty bits are unimportant so let's switch over to ptep_get_lockless_norecency(). The wrinkle is that gup needs to check that the pte hasn't changed once it has pinned the folio following this model: pte = ptep_get_lockless_norecency(ptep) ... if (!pte_same(pte, ptep_get_lockless(ptep))) // RACE! ... And now that pte may not contain correct access and dirty information, the pte_same() comparison could spuriously fail. So let's introduce a new pte_same_norecency() helper which will ignore the access and dirty bits when doing the comparison. Note that previously, ptep_get() was being used for the comparison; this is technically incorrect because the PTL is not held. I've also converted the comparison to use the preferred pmd_same() helper instead of doing a raw value comparison. As a side-effect, this new approach removes the possibility of concurrent read/write to the page causing a spurious fast gup failure, because the access and dirty bits are no longer used in the comparison. Signed-off-by: Ryan Roberts Acked-by: David Hildenbrand --- include/linux/pgtable.h | 18 ++++++++++++++++++ mm/gup.c | 7 ++++--- 2 files changed, 22 insertions(+), 3 deletions(-) -- 2.25.1 diff --git a/include/linux/pgtable.h b/include/linux/pgtable.h index 9dd40fdbd825..8123affa8baf 100644 --- a/include/linux/pgtable.h +++ b/include/linux/pgtable.h @@ -936,6 +936,24 @@ static inline int pte_same(pte_t pte_a, pte_t pte_b) } #endif +/** + * pte_same_norecency - Compare pte_a and pte_b, ignoring young and dirty bits, + * if the ptes are present. + * + * @pte_a: First pte to compare. + * @pte_b: Second pte to compare. + * + * Returns 1 if the ptes match, else 0. + */ +static inline int pte_same_norecency(pte_t pte_a, pte_t pte_b) +{ + if (pte_present(pte_a)) + pte_a = pte_mkold(pte_mkclean(pte_a)); + if (pte_present(pte_b)) + pte_b = pte_mkold(pte_mkclean(pte_b)); + return pte_same(pte_a, pte_b); +} + #ifndef __HAVE_ARCH_PTE_UNUSED /* * Some architectures provide facilities to virtualization guests diff --git a/mm/gup.c b/mm/gup.c index df83182ec72d..0f96d0a5ec09 100644 --- a/mm/gup.c +++ b/mm/gup.c @@ -2576,7 +2576,7 @@ static int gup_pte_range(pmd_t pmd, pmd_t *pmdp, unsigned long addr, if (!ptep) return 0; do { - pte_t pte = ptep_get_lockless(ptep); + pte_t pte = ptep_get_lockless_norecency(ptep); struct page *page; struct folio *folio; @@ -2617,8 +2617,9 @@ static int gup_pte_range(pmd_t pmd, pmd_t *pmdp, unsigned long addr, goto pte_unmap; } - if (unlikely(pmd_val(pmd) != pmd_val(*pmdp)) || - unlikely(pte_val(pte) != pte_val(ptep_get(ptep)))) { + if (unlikely(!pmd_same(pmd, *pmdp)) || + unlikely(!pte_same_norecency(pte, + ptep_get_lockless_norecency(ptep)))) { gup_put_folio(folio, 1, flags); goto pte_unmap; }