Received: by 2002:a05:6358:45e:b0:b5:b6eb:e1f9 with SMTP id 30csp4562420rwe; Tue, 30 Aug 2022 12:24:11 -0700 (PDT) X-Google-Smtp-Source: AA6agR5Orhd3ESNwqbqxNvLH6YBTIxprW44K/exXH4sQIUpeFCuEqjOV+1tjY/TYkpRTNAQW5vFg X-Received: by 2002:a17:90b:23d0:b0:1fd:af26:cee9 with SMTP id md16-20020a17090b23d000b001fdaf26cee9mr14885528pjb.23.1661887450779; Tue, 30 Aug 2022 12:24:10 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1661887450; cv=pass; d=google.com; s=arc-20160816; b=lwhvVxvUFLrwppuL3WEBqTLe1FvAQ5VQukyygv9Gfu1oOCEuT5wok2yXxo0Tbx1CzD DvEv9IQHsrSionSIMbn+brdtK+mjZksi5XLEbJX8iMBolazYPUE+iJc58yBF9Py0FBYU ACmCLs8kwKbexv/pLM5RqgtBm91T/jG79IFy6T+1/YjAZDEyhFlsbpEc4/XDiFzq41Ih I+GEuzFZ5NVKd7+Vp6r3oprU0X+qmcYJvlIG5LNUi7Rh0DuQAg8FY2iB+eQIGGoPInRd xwvkElp0jXN3m4Rhz9wF5AVSSvT2j21M3oYVrnOkURg3v1H9p6PBO7QUrTQHO2xvdsE7 e44A== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:mime-version:content-transfer-encoding :in-reply-to:from:references:cc:to:content-language:subject :user-agent:date:message-id:dkim-signature; bh=PfDmqC6/l5nfFD3k0fOLSX2Uisjw3URDtzJVihSqCpA=; b=Q/lGXGDXiXjXeqnheRVzipvfA9boMG5yPl1e1F/A0cQBviRxcv6ArUKcMZxYrSyDHp HfTMYeNW7YhCQZN1NIKVxCIISmjpSrjHKcdD1dlAB/dn1Ak1LQOutIK1a5cO3lN9v3P8 6L9Bgg7wsOJSI3D0DjFsQAmEszcoCPFGENSlGNDe0NgU+KgzhI1ojlOYvb1iIeH66jv5 U/p5kaoDhiDtykMOV1+7xXkxOpV9/WX1J6n/zo1t53v3zX1lU2BCn/DYYp0dBcIO3XIQ Dh9gc3uWpY32uLtKSBtL+c3z86tLSWgSy8bss7sMUk54kLGi7hjOCrwmavoXkrZyxVfA b+PA== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@Nvidia.com header.s=selector2 header.b=A7TctpHS; arc=pass (i=1 spf=pass spfdomain=nvidia.com dkim=pass dkdomain=nvidia.com dmarc=pass fromdomain=nvidia.com); spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=nvidia.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id t191-20020a6381c8000000b0042e8690962csi2202313pgd.548.2022.08.30.12.23.57; Tue, 30 Aug 2022 12:24:10 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@Nvidia.com header.s=selector2 header.b=A7TctpHS; arc=pass (i=1 spf=pass spfdomain=nvidia.com dkim=pass dkdomain=nvidia.com dmarc=pass fromdomain=nvidia.com); spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=nvidia.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231249AbiH3TSv (ORCPT + 99 others); Tue, 30 Aug 2022 15:18:51 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51524 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229607AbiH3TSr (ORCPT ); Tue, 30 Aug 2022 15:18:47 -0400 Received: from NAM12-MW2-obe.outbound.protection.outlook.com (mail-mw2nam12on2072.outbound.protection.outlook.com [40.107.244.72]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1736B61B3C for ; Tue, 30 Aug 2022 12:18:46 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=hDrE1+fDvA7uVbXxzlaIXTpwZ1VIaotFQutVsV07yIOOFsiUVRoJKA/SagEWY2hBRllvjeFsKOLA2m6HMcdIP2S84OcHF1hoVGM1V987x/iyTAyRLE7FBb8KwA4rhuMWUHDOwYfOAP+7fh2xidd0+JW5//NPty2BPpImfT5lYJaMCcTMnaznqvTXgvrYO3sYJ5aa/kOqNGuMZ+kP/s3CGBSQbvk1VwzZXJ/oA5+WHBcOK6JK8P7mqa/gL78hRc1Pk6zMkzi5HyJhiP7i2SIg8flaTCwa3kj4BJguCOTJFM99S1xW+sntOIrsiZIssGYavMB+pJcLQz5U59KCi54u7g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=PfDmqC6/l5nfFD3k0fOLSX2Uisjw3URDtzJVihSqCpA=; b=aXP5yTxFCpL+SylrwGARy+A/GsJ0ZU8WV/JpviNQPCpP10VVrvUEy8hBIu25P4LqCdBA9u3RbeeW9VAhYCoBP77PLQXdi3ELVVCW5H1LW6AzKpYI5qrnBZA2WXyUA+ucSCdUArf0WGziUSoVmfaUgTgylEoMCj4iZ2Z9VqlUuf+HhRtC9zIwJUl54kecbapTA/S+9hM7dpCmyrIiyKTPGfOaa5jxP3Vp5jtE0H7Z2ovaDUqcz/W4Jz0mHBZcEv+AJzytr2WKUL2sj0RXcaMzgfL5xe1OPbkspOAgnGtUp7eJoTXUUXZXEcrVdPiGTCA2NTKBSqi+GkODQJL/LkYHMA== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=nvidia.com; dmarc=pass action=none header.from=nvidia.com; dkim=pass header.d=nvidia.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=Nvidia.com; s=selector2; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=PfDmqC6/l5nfFD3k0fOLSX2Uisjw3URDtzJVihSqCpA=; b=A7TctpHS88ApI0eLDUzbVFO34upo2AkMcbzuUxihk7rogCiZ3BsfQmag6zxcMwy12UM0NjEkBrXpPi7+Wy3yTEZh9HBIHqjqK/6BfgL4rNqj+nLTpHFdBHQUCeh/S+C3FARCMugx6T2GGNppx7770GPIFR8Y056NgcOGafQUNqvi8/PCumsk06t8H8+NHjRoxb4GtSprGsI18ebGAAbDzVdEtN4jpuZ8otZYVUewhybOGWVT4KRi/1pawhD1zNW3GMzXdhGfEDVdBb7yVbYZ2HmIkIa8jFB/hqkil0fUjqpYarWz8b1GZ6Txr36/4/AcfXOL1VWKayw1YkggrphG5A== Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=nvidia.com; Received: from BY5PR12MB4130.namprd12.prod.outlook.com (2603:10b6:a03:20b::16) by SA1PR12MB5614.namprd12.prod.outlook.com (2603:10b6:806:228::7) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5546.21; Tue, 30 Aug 2022 19:18:44 +0000 Received: from BY5PR12MB4130.namprd12.prod.outlook.com ([fe80::508d:221c:9c9e:e1a5]) by BY5PR12MB4130.namprd12.prod.outlook.com ([fe80::508d:221c:9c9e:e1a5%9]) with mapi id 15.20.5588.010; Tue, 30 Aug 2022 19:18:44 +0000 Message-ID: <9a4fe603-950e-785b-6281-2e309256463f@nvidia.com> Date: Tue, 30 Aug 2022 12:18:41 -0700 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.2.0 Subject: Re: [PATCH v1 2/3] mm/gup: use gup_can_follow_protnone() also in GUP-fast Content-Language: en-US To: David Hildenbrand , Jason Gunthorpe Cc: linux-kernel@vger.kernel.org, linux-mm@kvack.org, Andrew Morton , Mel Gorman , "Matthew Wilcox (Oracle)" , Andrea Arcangeli , Hugh Dickins , Peter Xu References: <20220825164659.89824-1-david@redhat.com> <20220825164659.89824-3-david@redhat.com> <1892f6de-fd22-0e8b-3ff6-4c8641e1c68e@redhat.com> <2e20c90d-4d1f-dd83-aa63-9d8d17021263@redhat.com> <9ce3aaaa-71a6-5a81-16a3-36e6763feb91@redhat.com> From: John Hubbard In-Reply-To: <9ce3aaaa-71a6-5a81-16a3-36e6763feb91@redhat.com> Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit X-ClientProxiedBy: SJ0PR13CA0022.namprd13.prod.outlook.com (2603:10b6:a03:2c0::27) To BY5PR12MB4130.namprd12.prod.outlook.com (2603:10b6:a03:20b::16) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: 49fb9cd5-fb03-4a05-67de-08da8abc7472 X-MS-TrafficTypeDiagnostic: SA1PR12MB5614:EE_ X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: fVe35gElqc/JMVv+UhVnOl9rqwNeakaUxlB4dxuVRFyNBRgoqekef/i5IHn1WIyQen0r9IXUYcFL1HtbCRuEC8vMlrHK/mZIAJDLCP24/hrN3RNmd0juFer/XJuucrXH6WXlmXZ/MnHTDZ0cMC2aoIXHulqnemxkCqtlArKG4S0ksQUF+GYtPa6N0wOEMRzBkHe06BDLq8+VY+iUGH84V22MhaGTAXlV8CVe7uXRTb0ZY2bF+5cLY1qY9m5ouCC09+HSl2ftv6DxVNdWTJt8ViYB/TzVF+4n8uJlKr5EKqOzpnXmI987DJmWSolM0uh1TxrVMzTHSa5B8z5QmBMaC9NXK5+UVltCTTIpo5TjeS4jwcfnjSQZ4FJDn1RlccgR200JNqy8nNf701LVIqq34Xm5786FOOQxJSC469W6ygj8+W+yMjdBZlilBrmp08GLsrZtSzVjGsdhX7n2w+BBvpb2i9WGVlSu7DeoHVYDDGVHZnJR6Ei9p4ifq66yl3dv9g0bH8S4PRMaqaUq5oEbx+Ld8MZw+0tb29X6ZZcsZoy8Um0LEzu1JlCvFGAd9VRScYh0m89klKGBP5c3x6HvyO+sKOJM2hP0AS7dDGAF4f3oTWYLtnxyw50YbjFp6CStd0oRl5Jc2ZEe3T37cLNZSTlCroFkrMnEX162VfALQCq03oQpiEk3kq6JNwjDxzomFCboffAhqLPORFJGo6QM38iW0gCD9yim7d/Dn2sEFqiU4B/zrRJXVUe1z9Emsl6osFZtusKWOilAK2BhbP+trFFTEyC3zGH9RxKeRNgpsBMNCoxYY3GGlicRd4ZXj8EfmdNBAc5XcInVw3BXdSaoVbMigpnGsU4LFG5rznrl5TlU9OgFowCoKDXiH7A8kptg X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:BY5PR12MB4130.namprd12.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230016)(4636009)(136003)(346002)(376002)(366004)(39860400002)(396003)(38100700002)(6512007)(41300700001)(26005)(54906003)(83380400001)(186003)(2616005)(6486002)(966005)(8936002)(31696002)(5660300002)(2906002)(478600001)(36756003)(31686004)(6666004)(6636002)(6506007)(66476007)(86362001)(66946007)(66556008)(110136005)(53546011)(316002)(4326008)(8676002)(43740500002)(45980500001);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: =?utf-8?B?VllmS1c2SmtIYVg4ZnoyMDhKMW95QnovcXc0aGk0QWZaakg3NmVxaVFFQXNJ?= =?utf-8?B?SzdVbktuej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HRz?= =?utf-8?B?RUtPUFhMelpJTDdSelVrNUg0VnVuVG53dHIvbmNWczRwOGI4Uy9POW9XcWtP?= =?utf-8?B?NjhLa09mU2JTdFdUcUxEemlRM1pJc1FEV0tsZmFMcHR2VU5ZTHJEem96OUtZ?= =?utf-8?B?a2U3K1BzUmZZallwM1UzdUROWDFrcHlIYUdlQ0tDZUlQK2pUb2VkYjFnVzU4?= =?utf-8?B?eDJNWHNkeXhiY0dnT1BZZlRMMk9MdmJOaERJbUE1R1FqU1BJYzViRG1mM0dR?= =?utf-8?Q?Ix7y9NGJANpfDQQrOP5R7N55R?= X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: 49fb9cd5-fb03-4a05-67de-08da8abc7472 X-MS-Exchange-CrossTenant-AuthSource: BY5PR12MB4130.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 30 Aug 2022 19:18:44.0048 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 43083d15-7273-40c1-b7db-39efd9ccc17a X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: f8B4h4X+fWuolyPVYZ/pia2zqlaRVnM0NU/KXoEquznmjGt/fFrUB5K4jDBMGhTVcnCMgVV0OjDOayniORgMyQ== X-MS-Exchange-Transport-CrossTenantHeadersStamped: SA1PR12MB5614 X-Spam-Status: No, score=-1.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FORGED_SPF_HELO, NICE_REPLY_A,RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2,SPF_HELO_PASS, SPF_NONE,T_SCC_BODY_TEXT_LINE autolearn=no autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 8/30/22 11:53, David Hildenbrand wrote: > Good, I managed to attract the attention of someone who understands that machinery :) > > While validating whether GUP-fast and PageAnonExclusive code work correctly, > I started looking at the whole RCU GUP-fast machinery. I do have a patch to > improve PageAnonExclusive clearing (I think we're missing memory barriers to > make it work as expected in any possible case), but I also stumbled eventually > over a more generic issue that might need memory barriers. > > Any thoughts whether I am missing something or this is actually missing > memory barriers? > It's actually missing memory barriers. In fact, others have had that same thought! [1] :) In that 2019 thread, I recall that this got dismissed because of a focus on the IPI-based aspect of gup fast synchronization (there was some hand waving, perhaps accurate waving, about memory barriers vs. CPU interrupts). But now the RCU (non-IPI) implementation is more widely used than it used to be, the issue is clearer. > > From ce8c941c11d1f60cea87a3e4d941041dc6b79900 Mon Sep 17 00:00:00 2001 > From: David Hildenbrand > Date: Mon, 29 Aug 2022 16:57:07 +0200 > Subject: [PATCH] mm/gup: update refcount+pincount before testing if the PTE > changed > > mm/ksm.c:write_protect_page() has to make sure that no unknown > references to a mapped page exist and that no additional ones with write > permissions are possible -- unknown references could have write permissions > and modify the page afterwards. > > Conceptually, mm/ksm.c:write_protect_page() consists of: > (1) Clear/invalidate PTE > (2) Check if there are unknown references; back off if so. > (3) Update PTE (e.g., map it R/O) > > Conceptually, GUP-fast code consists of: > (1) Read the PTE > (2) Increment refcount/pincount of the mapped page > (3) Check if the PTE changed by re-reading it; back off if so. > > To make sure GUP-fast won't be able to grab additional references after > clearing the PTE, but will properly detect the change and back off, we > need a memory barrier between updating the recount/pincount and checking > if it changed. > > try_grab_folio() doesn't necessarily imply a memory barrier, so add an > explicit smp_mb__after_atomic() after the atomic RMW operation to > increment the refcount and pincount. > > ptep_clear_flush() used to clear the PTE and flush the TLB should imply > a memory barrier for flushing the TLB, so don't add another one for now. > > PageAnonExclusive handling requires further care and will be handled > separately. > > Fixes: 2667f50e8b81 ("mm: introduce a general RCU get_user_pages_fast()") > Signed-off-by: David Hildenbrand > --- > mm/gup.c | 17 +++++++++++++++++ > 1 file changed, 17 insertions(+) > > diff --git a/mm/gup.c b/mm/gup.c > index 5abdaf487460..0008b808f484 100644 > --- a/mm/gup.c > +++ b/mm/gup.c > @@ -2392,6 +2392,14 @@ static int gup_pte_range(pmd_t pmd, unsigned long addr, unsigned long end, > goto pte_unmap; > } > > + /* > + * Update refcount/pincount before testing for changed PTE. This > + * is required for code like mm/ksm.c:write_protect_page() that > + * wants to make sure that a page has no unknown references > + * after clearing the PTE. > + */ > + smp_mb__after_atomic(); > + > if (unlikely(pte_val(pte) != pte_val(*ptep))) { > gup_put_folio(folio, 1, flags); > goto pte_unmap; > @@ -2577,6 +2585,9 @@ static int gup_hugepte(pte_t *ptep, unsigned long sz, unsigned long addr, > if (!folio) > return 0; > > + /* See gup_pte_range(). */ Don't we usually also identify what each mb pairs with, in the comments? That would help. > + smp_mb__after_atomic(); > + > if (unlikely(pte_val(pte) != pte_val(*ptep))) { > gup_put_folio(folio, refs, flags); > return 0; > @@ -2643,6 +2654,9 @@ static int gup_huge_pmd(pmd_t orig, pmd_t *pmdp, unsigned long addr, > if (!folio) > return 0; > > + /* See gup_pte_range(). */ > + smp_mb__after_atomic(); > + > if (unlikely(pmd_val(orig) != pmd_val(*pmdp))) { > gup_put_folio(folio, refs, flags); > return 0; > @@ -2683,6 +2697,9 @@ static int gup_huge_pud(pud_t orig, pud_t *pudp, unsigned long addr, > if (!folio) > return 0; > > + /* See gup_pte_range(). */ > + smp_mb__after_atomic(); > + > if (unlikely(pud_val(orig) != pud_val(*pudp))) { > gup_put_folio(folio, refs, flags); > return 0; [1] https://lore.kernel.org/lkml/9465df76-0229-1b44-5646-5cced1bc1718@nvidia.com/ thanks, -- John Hubbard NVIDIA