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: 201435 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:b825:b0:106:860b:bbdd with SMTP id da37csp355941dyb; Thu, 15 Feb 2024 04:19:04 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCXHBP8hHm3T9J1LAScJGY73CQLFUwgDcx/eRhhVIpVOE28YE4ePpXMmGM23Iov0wUDA6xjcFkEEwZQT+rdhdiO+bErwvw== X-Google-Smtp-Source: AGHT+IEFk263y8b6pSPbxTQ4Pw5Q3qR65lwBF0bJqQe1VC757c/AfFiULHxrGlbJZ7OZmbQ6cqaU X-Received: by 2002:a05:6808:ece:b0:3c0:3767:b323 with SMTP id q14-20020a0568080ece00b003c03767b323mr1893807oiv.54.1707999543837; Thu, 15 Feb 2024 04:19:03 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1707999543; cv=pass; d=google.com; s=arc-20160816; b=dRNNDi6C8Uhgl5wEKED8qHw4lKYBUGc73MRvytu21p6B8NIqLOFAoiqVkcV05h/Gil dk1QJ33uIAMmhWxZNIehkv+WNOnPft7kVmg3FEvaE9Tf5nk/MT+MFX2J5qnwHJyEsTQB 52T8JxieJVOBphIhvsyQRUA3mtm+hrpeR0xGkY/V2hXjLo8HRYKVRbD8KznZvg9XnW4/ eqBgNfJ+AFXolc0rDf6MTPqTB3JIvyDgSUi4Y4oE25alKaSbYP3RzvEv0/U/+36vwpBL uALafAKZE93d0WX3SD3GBL+5YWUCkVn3udrj8iFk6fTyqen6N2UsmYQlgoIq/mUbDRgC DV2w== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from; bh=lbpD6M3yiauchTnc5b8hwmaJxEY6qpI+eNSGlv7SoLM=; fh=2FwJMiZjh7JNs0IOfHdUsjppsJNoMkV8ot/xk2oRsPU=; b=kgO2FHRDv8CoA9AqCBIjkwZuHM3zoVqy0VoqrUzWqTOTJyHXUwPOdkiLFozlWca1qU CewQxBllz1FnBsBHr32veqvhX4xZ3N/jTWo6xZfuHTyhh/OhiBpEyR+Gc+E44Fkjzmgf bXgar/jEInJ2LvQDPTBsrdO2rYMmVJLuH22f0rgYPqAs2cOXaERFYz+CdpWxs0H7/a+T D9ogIKB/wNn0hEubdNmYmJ00HOvr7F8spdXRDaFS7WI9+2K+7OdTdwd3IiahlbKUyE+h 4iSxNrYUCcOeWLtSJ/SYDaAl7UO6Tyq407HquHDCViMWZugQJxfT0LEF2MuWgYJUxwi+ rvpQ==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; arc=pass (i=1 spf=pass spfdomain=arm.com dmarc=pass fromdomain=arm.com); spf=pass (google.com: domain of linux-kernel+bounces-66874-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-66874-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=arm.com Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [147.75.199.223]) by mx.google.com with ESMTPS id r11-20020a0ccc0b000000b0068f2a156fb1si278248qvk.111.2024.02.15.04.19.03 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 15 Feb 2024 04:19:03 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-66874-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) client-ip=147.75.199.223; Authentication-Results: mx.google.com; arc=pass (i=1 spf=pass spfdomain=arm.com dmarc=pass fromdomain=arm.com); spf=pass (google.com: domain of linux-kernel+bounces-66874-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-66874-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=arm.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ny.mirrors.kernel.org (Postfix) with ESMTPS id 9C96A1C21EA2 for ; Thu, 15 Feb 2024 12:19:03 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id C01F212DDA2; Thu, 15 Feb 2024 12:18:13 +0000 (UTC) Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by smtp.subspace.kernel.org (Postfix) with ESMTP id A90D713FF5 for ; Thu, 15 Feb 2024 12:18:10 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=217.140.110.172 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707999492; cv=none; b=lpjhcgBbegZBDH6fvMtC1EhPz8qOc7jO1N3ynHk0xONRDvXGUi0ONlMDOAACjdjNqeg7B1RWzoHVXgem5pZs8d5zeD4fKpDjl2Qva0O6KF59P3dD9tf7ItzbbbmrSqcFELNsVK0wSQ15KQRlWucM/tQzD0X+ZeWFnHLjZop8n8g= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707999492; c=relaxed/simple; bh=wChUY2n7KOLoXl9tar4y7xF2JR66QrvSAd+bls/t2rw=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=pEK2N4GPEX6aELJ8f3hci9ZihDyw5PbQWsAisJcLZ+VvdKZ73ov2Izxa9BW8L9nl9CXO6Wr5pQgQH7lEYJ7LM6N/1tuGp16FczQeQLh0OJ+ENkZSf9iQ3m79N3KJ/bVEeynpDGkGIJLi71Qr+wYDry8JdjAb9BtryV9a/hNZnsQ= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=arm.com; spf=pass smtp.mailfrom=arm.com; arc=none smtp.client-ip=217.140.110.172 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=arm.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=arm.com 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> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1790967330159555083 X-GMAIL-MSGID: 1790967330159555083 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 --- 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; }