Received: by 2002:ab2:6857:0:b0:1ef:ffd0:ce49 with SMTP id l23csp3466246lqp; Tue, 26 Mar 2024 09:49:51 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCWhjTFDxdg8ETRruH+7gE9O2KjdB7qvrukLAieRpls7WrxN73Tt04AwOEGlEh4mL59UPIV0RxVI5FM6+VN64Gah6w1J6otA5sq1nU/cqw== X-Google-Smtp-Source: AGHT+IEYndJRFnCYWFzaRC1RHRWAWCc9robemrd44lJppz9bAiI1KjVFbZzC5AvXeox+gR+k4jl/ X-Received: by 2002:a05:6808:eca:b0:3c3:7ba2:1579 with SMTP id q10-20020a0568080eca00b003c37ba21579mr12300295oiv.24.1711471790810; Tue, 26 Mar 2024 09:49:50 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1711471790; cv=pass; d=google.com; s=arc-20160816; b=nSicz6T4FqJvM0Ejh/CC2HSQl77k0F3UqBRz+4dtNoXM9/rnZWahil58H4xja43qSB qVie4mr/UB+0e7YXwCLY99kt8pqmms6CdSUfUvhN/wYMoL4QavPntkt0KXZ9ZByckD3b pCR1omFnM7glmFfVgreNXWEPGEIzMgT0zIWD28Hvt7BmzE9YnJoELaSLPzScyUHMMFAP sL+FtIHcQPiC9FAuozJPqlctT5CEIVBgJB0vrh2neU1KZ9OulA0z2MXwv4czKCXugPxJ WU7sfjOSCuOcyppNB6rLhEGtwUzS1tdgvNAPLn1KYEWV0zw8tmZLBVswuxQx+DBZBN5k E+aA== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:in-reply-to:from:references:cc:to :content-language:subject:user-agent:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:date:message-id; bh=dulVK/FwNF20i7tg1KimhCC74HYS54dkpyuQZQJsLiQ=; fh=5ElpXvGotM0/udMhnvi0GjXf9RQ5OKrboCm54T8RE10=; b=f5cPqIfxFEpLICwcSJRCqWMgzjXYKRTkPHbemFVVP7L5GCfOO/NzyZmdpLN4deXq8o KJPDpj46WRyslHTURTrzX8GDkzO9g12bkdgINWbpM0u1zw8qF3EU7kXU4V1lEa0oBsVP mW24KsrHYE/L/HiW7azho3MFzuxapgl4fpt0wKYKTmHZyD4SZdb3xfSMt9rlltDR0o+j J6W4qNlRpxufHTJD7NcQ5Mtzs7XwCFuxfU/ewTRnCGr5QkCx6Kx/7LNO9CR0gqnIGicM 4VpoOd/H9eV5LQdzUgCN82dpSi8ftnKnG16f7cIKO1Kx73uxTMNiJhKte3OCUCbWPfvX t+aA==; 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-119508-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-119508-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=arm.com Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [147.75.199.223]) by mx.google.com with ESMTPS id g3-20020ac87d03000000b004316180b1aesi3326279qtb.391.2024.03.26.09.49.50 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 26 Mar 2024 09:49:50 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-119508-linux.lists.archive=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-119508-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-119508-linux.lists.archive=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 8488F1C65197 for ; Tue, 26 Mar 2024 16:49:50 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 295B81CD2D; Tue, 26 Mar 2024 16:48:52 +0000 (UTC) Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by smtp.subspace.kernel.org (Postfix) with ESMTP id DE43A1C69C for ; Tue, 26 Mar 2024 16:48:49 +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=1711471731; cv=none; b=DQyqMj7CcgTqRhilmSxzX+NkpiVuTAGvxeltiDNP71wluLWh/cl47VHB8xZSFhO5UUAc6RuQ1VYpkTRz13P+nxK02WGOp9ac3ygaLUQyEEAo1EuVZyuQCh2DsSOBRxilKIYSKUZRMRSOnSbYcDuY1blcIft2ucGbHFR5esN7UNA= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711471731; c=relaxed/simple; bh=dOBYFG2CFVG88RBbN6IiQLjRxxljLiWi7mMX9/e5kiM=; h=Message-ID:Date:MIME-Version:Subject:To:Cc:References:From: In-Reply-To:Content-Type; b=SzO5nGFvKRylEKNaJeIK0kdWflY7FEE1QmG79HSc1T1WT4dDB1nldvupVmd4qA8FtMZfG1JVQXS1ROI/YPYX4lE6r6Y3OrVcfrEZixy5rgmbzeDko4kX4xuJqT+DLHPMlm81DLZZYQn8ghdsUkyLa3T9olytLLVh7Y4SFGWIfFg= 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 E5E172F4; Tue, 26 Mar 2024 09:49:22 -0700 (PDT) Received: from [10.1.29.179] (XHFQ2J9959.cambridge.arm.com [10.1.29.179]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id 69D4D3F694; Tue, 26 Mar 2024 09:48:47 -0700 (PDT) Message-ID: <79ade347-419a-4c9e-84db-def06ec5f36a@arm.com> Date: Tue, 26 Mar 2024 16:48:45 +0000 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [RFC PATCH v1 2/4] mm/gup: Use ptep_get_lockless_norecency() Content-Language: en-GB To: David Hildenbrand , Mark Rutland , Catalin Marinas , Will Deacon , Alexander Shishkin , Jiri Olsa , Ian Rogers , Adrian Hunter , Andrew Morton , Muchun Song Cc: linux-arm-kernel@lists.infradead.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org References: <20240215121756.2734131-1-ryan.roberts@arm.com> <20240215121756.2734131-3-ryan.roberts@arm.com> <5d80c368-7ce7-4a44-9cd7-aee3e1c9182b@redhat.com> From: Ryan Roberts In-Reply-To: <5d80c368-7ce7-4a44-9cd7-aee3e1c9182b@redhat.com> Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit On 26/03/2024 16:30, David Hildenbrand wrote: > On 15.02.24 13:17, Ryan Roberts wrote: >> 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 >> --- > > [...] > >>   #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; > > We pass the pte into pte_access_permitted(). It would be good to mention that > you checked all implementations. TBH, I hadn't; I decided that since the "inaccurate access/dirty bits" was only possible on arm64, then only arm64's implementation needed checking. But given your comment, I just had a quick look at all impls. I didn't spot any problems where any impl needs the access/dirty bits. I'll add this to the commit log. > > Acked-by: David Hildenbrand >