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 00F6AECAAD1 for ; Thu, 1 Sep 2022 17:40:45 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 6996980033; Thu, 1 Sep 2022 13:40:45 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 6488F8000D; Thu, 1 Sep 2022 13:40:45 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 5373080033; Thu, 1 Sep 2022 13:40:45 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id 416708000D for ; Thu, 1 Sep 2022 13:40:45 -0400 (EDT) Received: from smtpin09.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 0E833A08E8 for ; Thu, 1 Sep 2022 17:40:45 +0000 (UTC) X-FDA: 79864231650.09.996560E Received: from mail-pg1-f173.google.com (mail-pg1-f173.google.com [209.85.215.173]) by imf26.hostedemail.com (Postfix) with ESMTP id 8B50614006C for ; Thu, 1 Sep 2022 17:40:44 +0000 (UTC) Received: by mail-pg1-f173.google.com with SMTP id bh13so17044637pgb.4 for ; Thu, 01 Sep 2022 10:40:44 -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=b0H9B0FRwbPM5Cz+4QjPTjCFU3PLC04eS989QmgK6OY=; b=SLXbEaDl+m0WNW1mdPDM1JtgiXI2fq5Ahl2hAk+1/2KblOlCwZQ27c5OI3iOPG0aeV jC73JvdnF0WDCP0VTMr7X/+IkartYastE/3OjD4t5EyGGynnNHX5TQco0hAHHE6zk7/G 7njhGCkfe6HH19n8JFQo2ce+Eg/V2GnSL9aEKHrN6du2K9QBSB16Hxx8esgENcDuGuxG ZJKJhsbWYspdEHAQIHKfo8D4Mtjmxd7CSz0Qtp5icSOBzjQRD1UxNka1xGvbZCQD2k1n 8tS73TtJOA+1XWXPO3Dru+oORzI1CqlyMUyBAKCr6zNVRYwirwpv1Zs8XAMOs8leAGGl u8Uw== 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=b0H9B0FRwbPM5Cz+4QjPTjCFU3PLC04eS989QmgK6OY=; b=TMt7Fr7K0th5HlWQueiONz33Fvs/4AOQy4sCVSjFelNBXl9lKd3xVuW4EncPWsGKMd jJQHDsXgHMBln/uizVZdr+c/hcwqsC7/Mj8tjG3Rdp3ct5lq62tDPx0I7HKcZSJWdLcf 7WW5c3qLal8Nt5Roq1bB2e5MyAQFPNCVVyxHEm4I+kaztqxsE2GSLDQ0CYup92iyKjXK ZpuPj88OEgOTXeXEjfXmqqWDtshfqmmLRxmh+wHqn9s3bW/XXtQkbhKInnJR8ddjuGRl KtQ5mUwbX/PcvHxt++Oc79MBmCz3AX17MUJUqcJZgnGCIpdyBZKk3WGnIIEKusVThsbD Mnxg== X-Gm-Message-State: ACgBeo24C7RZu2I9mEywwFXLLLY6JdfPmR79Sublv0dvUthubrtXwcaj aS8pEvBfJ8lfo1FXjkW6JcYPPMeEUOTnv67bhrV79kkO X-Google-Smtp-Source: AA6agR70Xdh8a0e1gFvCSjjLJRyRMUbqTHe5xl2eniat52M1jOZH+rSLT5JNyscu9FMW2Gi3BF+0eQ9CMccIMC7cVus= X-Received: by 2002:a63:de01:0:b0:42b:31f7:b6ea with SMTP id f1-20020a63de01000000b0042b31f7b6eamr26540720pgg.587.1662054043593; Thu, 01 Sep 2022 10:40:43 -0700 (PDT) MIME-Version: 1.0 References: <20220831083024.37138-1-david@redhat.com> <4845ae71-b7dd-1707-ebc3-2eb3521e7fa0@redhat.com> <94c3217d-df73-2b6b-21f0-95baf117c584@redhat.com> In-Reply-To: From: Yang Shi Date: Thu, 1 Sep 2022 10:40:30 -0700 Message-ID: Subject: Re: [PATCH v1] mm/ksm: update stale comment in write_protect_page() To: David Hildenbrand Cc: linux-kernel@vger.kernel.org, linux-mm@kvack.org, Andrew Morton , Jason Gunthorpe , John Hubbard , Andrea Arcangeli , Hugh Dickins , Peter Xu , "Kirill A. Shutemov" 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=1662054044; 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=b0H9B0FRwbPM5Cz+4QjPTjCFU3PLC04eS989QmgK6OY=; b=7ofje0QJYjEjGjKpRnygfhrbxgJYl3Zy31oXLwRXf5aP81kisOzUwBgT2SmlYF/i8kj2K3 6BdYvfRxUwXpTxZItC+aXseKMu6n0F9TUCog5l+azpUhstt0YAKgRFwjU3cJTat0aVaIdT 933e173qaP2Sfq0BVPcDlNMjpsVWzs8= ARC-Authentication-Results: i=1; imf26.hostedemail.com; dkim=pass header.d=gmail.com header.s=20210112 header.b=SLXbEaDl; spf=pass (imf26.hostedemail.com: domain of shy828301@gmail.com designates 209.85.215.173 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=1662054044; a=rsa-sha256; cv=none; b=Pj9FYzuaOBV5fXxx1xEGetuxLvSEAa6K4Ttae4GXe14ABA+GxzZx4KEfmkr/fajShacT3P CoPDGqatbmKoyfiQxm0CmwOYblYjFm7hOGTgomM5Qf6+YMzv6C0DTtsVIzCq0EkWOZoAKv l2MDOw5NJ4B1tNwoJvoRmctzMJJcz0g= Authentication-Results: imf26.hostedemail.com; dkim=pass header.d=gmail.com header.s=20210112 header.b=SLXbEaDl; spf=pass (imf26.hostedemail.com: domain of shy828301@gmail.com designates 209.85.215.173 as permitted sender) smtp.mailfrom=shy828301@gmail.com; dmarc=pass (policy=none) header.from=gmail.com X-Rspamd-Server: rspam11 X-Stat-Signature: 5e1q9coqrqerosdc781pp64hh7rsg96o X-Rspamd-Queue-Id: 8B50614006C X-Rspam-User: X-HE-Tag: 1662054044-580507 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 Wed, Aug 31, 2022 at 11:58 PM David Hildenbrand wrote: > > On 01.09.22 00:18, Yang Shi wrote: > > On Wed, Aug 31, 2022 at 12:43 PM Yang Shi wrote: > >> > >> On Wed, Aug 31, 2022 at 12:36 PM David Hildenbrand wrote: > >>> > >>> On 31.08.22 21:34, Yang Shi wrote: > >>>> On Wed, Aug 31, 2022 at 12:15 PM David Hildenbrand wrote: > >>>>> > >>>>> On 31.08.22 21:08, Yang Shi wrote: > >>>>>> On Wed, Aug 31, 2022 at 11:29 AM David Hildenbrand wrote: > >>>>>>> > >>>>>>> On 31.08.22 19:55, Yang Shi wrote: > >>>>>>>> On Wed, Aug 31, 2022 at 1:30 AM David Hildenbrand wrote: > >>>>>>>>> > >>>>>>>>> The comment is stale, because a TLB flush is no longer sufficient and > >>>>>>>>> required to synchronize against concurrent GUP-fast. This used to be true > >>>>>>>>> in the past, whereby a TLB flush would have implied an IPI on architectures > >>>>>>>>> that support GUP-fast, resulting in GUP-fast that disables local interrupts > >>>>>>>>> from completing before completing the flush. > >>>>>>>> > >>>>>>>> Hmm... it seems there might be problem for THP collapse IIUC. THP > >>>>>>>> collapse clears and flushes pmd before doing anything on pte and > >>>>>>>> relies on interrupt disable of fast GUP to serialize against fast GUP. > >>>>>>>> But if TLB flush is no longer sufficient, then we may run into the > >>>>>>>> below race IIUC: > >>>>>>>> > >>>>>>>> CPU A CPU B > >>>>>>>> THP collapse fast GUP > >>>>>>>> > >>>>>>>> gup_pmd_range() <-- see valid pmd > >>>>>>>> > >>>>>>>> gup_pte_range() <-- work on pte > >>>>>>>> clear pmd and flush TLB > >>>>>>>> __collapse_huge_page_isolate() > >>>>>>>> isolate page <-- before GUP bump refcount > >>>>>>>> > >>>>>>>> pin the page > >>>>>>>> __collapse_huge_page_copy() > >>>>>>>> copy data to huge page > >>>>>>>> clear pte (don't flush TLB) > >>>>>>>> Install huge pmd for huge page > >>>>>>>> > >>>>>>>> return the obsolete page > >>>>>>> > >>>>>>> Hm, the is_refcount_suitable() check runs while the PTE hasn't been > >>>>>>> cleared yet. And we don't check if the PMD changed once we're in > >>>>>>> gup_pte_range(). > >>>>>> > >>>>>> Yes > >>>>>> > >>>>>>> > >>>>>>> The comment most certainly should be stale as well -- unless there is > >>>>>>> some kind of an implicit IPI broadcast being done. > >>>>>>> > >>>>>>> 2667f50e8b81 mentions: "The RCU page table free logic coupled with an > >>>>>>> IPI broadcast on THP split (which is a rare event), allows one to > >>>>>>> protect a page table walker by merely disabling the interrupts during > >>>>>>> the walk." > >>>>>>> > >>>>>>> I'm not able to quickly locate that IPI broadcast -- maybe there is one > >>>>>>> being done here (in collapse) as well? > >>>>>> > >>>>>> The TLB flush may call IPI. I'm supposed it is arch dependent, right? > >>>>>> Some do use IPI, some may not. > >>>>> > >>>>> Right, and the whole idea of the RCU GUP-fast was to support > >>>>> architectures that don't do it. x86-64 does it. IIRC, powerpc doesn't do > >>>>> it -- but maybe it does so for PMDs? > >>>> > >>>> It looks powerpc does issue IPI for pmd flush. But arm64 doesn't IIRC. > >>>> > >>>> So maybe we should implement pmdp_collapse_flush() for those arches to > >>>> issue IPI. > >>> > >>> ... or find another way to detect and handle this in GUP-fast? > >>> > >>> Not sure if, for handling PMDs, it could be sufficient to propagate the > >>> pmdp pointer + value and double check that the values didn't change. > >> > >> Should work too, right before pinning the page. > > > > I actually mean the same place for checking pte. So, something like: > > > > diff --git a/mm/gup.c b/mm/gup.c > > index 5abdaf487460..2b0703403902 100644 > > --- a/mm/gup.c > > +++ b/mm/gup.c > > @@ -2392,7 +2392,8 @@ static int gup_pte_range(pmd_t pmd, unsigned > > long addr, unsigned long end, > > goto pte_unmap; > > } > > > > - if (unlikely(pte_val(pte) != pte_val(*ptep))) { > > + if (unlikely(pmd_val(pmd) != pmd_val(*pmdp)) || > > + unlikely(pte_val(pte) != pte_val(*ptep))) { > > gup_put_folio(folio, 1, flags); > > goto pte_unmap; > > } > > > > It doesn't build, just shows the idea. > > Exactly what I had in mind. We should add a comment spelling out that > this is for handling huge PMD collapse. Yeah, I will prepare a patch soon. > > > -- > Thanks, > > David / dhildenb >