From mboxrd@z Thu Jan 1 00:00:00 1970 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 838A2C38145 for ; Tue, 6 Sep 2022 19:11:43 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id ED5288D0001; Tue, 6 Sep 2022 15:11:42 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id E85F56B0073; Tue, 6 Sep 2022 15:11:42 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id D74D58D0001; Tue, 6 Sep 2022 15:11:42 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id C933A6B0072 for ; Tue, 6 Sep 2022 15:11:42 -0400 (EDT) Received: from smtpin05.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id A30EA120370 for ; Tue, 6 Sep 2022 19:11:42 +0000 (UTC) X-FDA: 79882604844.05.B8F0021 Received: from mail-pf1-f180.google.com (mail-pf1-f180.google.com [209.85.210.180]) by imf09.hostedemail.com (Postfix) with ESMTP id 5F225140083 for ; Tue, 6 Sep 2022 19:11:42 +0000 (UTC) Received: by mail-pf1-f180.google.com with SMTP id c198so2472934pfc.13 for ; Tue, 06 Sep 2022 12:11:42 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date; bh=w5HACyk7qZtfk4k6sLXm47Jz7SohkcpzYCPZm4Qovhw=; b=diUvTLiw+h79hhqSYAnJNwSPRkd1iPWX5fdPVlO7qXvE+qUW5IlY4N+riprFgHXr6k PdunL/+mWBLAV7lPq10sadvcrEh8ttEBQEoBE4IMpHCLvW+0wkoHbrZJYpBY2c1DDua1 hgXJxgzCWGdvghNgyyutLEQpTzP+RXjqv3cKk9dk4k0O4K5GjHXXr4ezFJcTAziXG0b6 +WqoqlBz7VXUWFPlByKTBIfNsWU91CO3LzTtufPsmKHryaRKFvnyJS7Wf/m0Sp+UcCc1 nu/FlDyM07OiWHysKErZlR4CRE19EhFNurevpzExh4vfi7iKocpCJRhFl2AqZBntKi5C uwJA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc:subject:date; bh=w5HACyk7qZtfk4k6sLXm47Jz7SohkcpzYCPZm4Qovhw=; b=z4fafayO0iGKPd4h4Rh1yOwmAQE9HVRzRWQOTFUXiKZUWUcaqwcw3p6HtvHxljz4l+ l+xS/d3Sgw6HQ+ChPc3YSrTNXxXSJz8OC8DhmaTot4pzF/wJbbd57I1qBq8jUKfxSI0S OuvizNxCiVYh9r1k0uTgzMyXzSKv2Y9iCGDg9LIQQjZyunKC9X5PyGoo16/KkKs0FFm/ UoK0UC/89zpzqtpZspjdyQMcXJdLVDwFRD5BqOwbOhdK6Ldu1DBSDWfD8QB0IDxFgFtg cWpZ3AE3eSR9R5IfKWmLIBl/Z0jzvGS69lzaKDLTIE7cfDo2nfK/jxPYUPi2NXooKrUc nY9g== X-Gm-Message-State: ACgBeo2AsMlBzRqC7CJ2Z0Tf02uxGg8gPnCpdGuCw5BkrL5BFVOWXn3y JDFCUmnN132SFCPkbFlFhcwp0V+ysl5oqLHqV0AJ762m X-Google-Smtp-Source: AA6agR5SVGBUB2XMa2QyGadf0meSM6bevz7OIIXSFjdapIWgrivobT834pCj5HZSTWw4Gui+MtV3s8RzZeJeCyGeFXg= X-Received: by 2002:a65:6a05:0:b0:42c:87a0:ea77 with SMTP id m5-20020a656a05000000b0042c87a0ea77mr84260pgu.75.1662491501469; Tue, 06 Sep 2022 12:11:41 -0700 (PDT) MIME-Version: 1.0 References: <20220901222707.477402-1-shy828301@gmail.com> <4516a349-49cb-fd7b-176a-f1a9479906d9@redhat.com> <40486dbb-9f19-6fa6-d46d-99d2b033883d@redhat.com> In-Reply-To: <40486dbb-9f19-6fa6-d46d-99d2b033883d@redhat.com> From: Yang Shi Date: Tue, 6 Sep 2022 12:11:28 -0700 Message-ID: Subject: Re: [PATCH] mm: gup: fix the fast GUP race against THP collapse To: David Hildenbrand Cc: Jason Gunthorpe , John Hubbard , peterx@redhat.com, kirill.shutemov@linux.intel.com, hughd@google.com, akpm@linux-foundation.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org Content-Type: text/plain; charset="UTF-8" ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1662491502; 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-type:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=w5HACyk7qZtfk4k6sLXm47Jz7SohkcpzYCPZm4Qovhw=; b=xrMpSCOgTE/onksbNvHgaaBm8pibQ20yPl38bRxva20Q6vTnBmC9GrLwMG56OnTEpP3M7w JqEU15uKPtxTfeBEPx4SgfB1ObrOQrhXuXMCV27P+c3/4AED4O7Etoo2llxjBj6oyIbeMJ nKDEhdHkwXpXjXnp1DLWybFKvn1gChI= ARC-Authentication-Results: i=1; imf09.hostedemail.com; dkim=pass header.d=gmail.com header.s=20210112 header.b=diUvTLiw; spf=pass (imf09.hostedemail.com: domain of shy828301@gmail.com designates 209.85.210.180 as permitted sender) smtp.mailfrom=shy828301@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1662491502; a=rsa-sha256; cv=none; b=Hed+sRF/q/ahaLz9eoZ0/WE3UfkLcjy4ksrPZX43rLg9Mc5N6dTY1FQEwesPuSLfuYMJTd iThPh+ZuyToJ2xOOY2Pn2FRJ65NccFvjkwPlxL4Pd8l8OuQFVNDkGD0UoqVYXJ75sjQVy6 5Mi4COYXknNXzzgHKIaYR0URsluaQmo= X-Rspamd-Queue-Id: 5F225140083 Authentication-Results: imf09.hostedemail.com; dkim=pass header.d=gmail.com header.s=20210112 header.b=diUvTLiw; spf=pass (imf09.hostedemail.com: domain of shy828301@gmail.com designates 209.85.210.180 as permitted sender) smtp.mailfrom=shy828301@gmail.com; dmarc=pass (policy=none) header.from=gmail.com X-Rspamd-Server: rspam02 X-Rspam-User: X-Stat-Signature: wsqn85pmmjmq6aj6jt5i4qg15ajdn4zb X-HE-Tag: 1662491502-133289 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: On Tue, Sep 6, 2022 at 7:44 AM David Hildenbrand wrote: > > On 06.09.22 16:30, Jason Gunthorpe wrote: > > On Tue, Sep 06, 2022 at 03:57:30PM +0200, David Hildenbrand wrote: > > > >>> READ_ONCE primarily is a marker that the data being read is unstable > >>> and that the compiler must avoid all instability when reading it. eg > >>> in this case the compiler could insanely double read the value, even > >>> though the 'if' requires only a single read. This would result in > >>> corrupt calculation. > >> > >> As we have a full memory barrier + compile barrier, the compiler might > >> indeed do double reads and all that stuff. BUT, it has to re-read after we > >> incremented the refcount, and IMHO that's the important part to detect the > >> change. > > > > Yes, it is important, but it is not the only important part. > > > > The compiler still has to exectute "if (*a != b)" *correctly*. > > > > This is what READ_ONCE is for. It doesn't set order, it doesn't > > implement a barrier, it tells the compiler that '*a' is unstable data > > and the compiler cannot make assumptions based on the idea that > > reading '*a' multiple times will always return the same value. > > > > If the compiler makes those assumptions then maybe even though 'if (*a > > != b)' is the reality, it could mis-compute '*a == b'. You enter into > > undefined behavior here. > > > > Though it is all very unlikely, the general memory model standard is > > to annotate with READ_ONCE. > > The only thing I could see going wrong in the comparison once the stars > alingn would be something like the following: > > if (*a != b) > > implemented as > > if ((*a).lower != b.lower && (*a).higher != b.higher) > > > This could only go wrong if we have more than one change such that: > > Original: > > *a = 0x00000000ffffffffull; > > > First modification: > *a = 0xffffffffffffffffull; > > Second modification: > *a = 0x00000000eeeeeeeeull; IIUC this is typically a 32-bit thing. > > > If we race with both modifications, we could see that ffffffff matches, > and could see that 00000000 matches as well. > > > So I agree that we should change it, but not necessarily as an urgent > fix and not necessarily in this patch. It's best to adjust all gup_* > functions in one patch. > > ... I do wonder if we want to reuse ptep_get_lockless() instead of the > READ_ONCE(). CONFIG_GUP_GET_PTE_LOW_HIGH is confusing. > > -- > Thanks, > > David / dhildenb >