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 7F2ABECAAD1 for ; Wed, 31 Aug 2022 22:18:55 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id AB1746B0072; Wed, 31 Aug 2022 18:18:54 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id A61226B0073; Wed, 31 Aug 2022 18:18:54 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 928F58D0001; Wed, 31 Aug 2022 18:18:54 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id 825936B0072 for ; Wed, 31 Aug 2022 18:18:54 -0400 (EDT) Received: from smtpin13.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id 4C9E18016C for ; Wed, 31 Aug 2022 22:18:54 +0000 (UTC) X-FDA: 79861303788.13.56852AA Received: from mail-pj1-f42.google.com (mail-pj1-f42.google.com [209.85.216.42]) by imf13.hostedemail.com (Postfix) with ESMTP id EB2A62001D for ; Wed, 31 Aug 2022 22:18:53 +0000 (UTC) Received: by mail-pj1-f42.google.com with SMTP id j9-20020a17090a3e0900b001fd9568b117so640079pjc.3 for ; Wed, 31 Aug 2022 15:18:53 -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=zyw/eC7l+o0wMdEAj5KqnO8fiNMT30lG6RjAzbV/PlI=; b=D7USHTEKx2MfvljGpX6xGf2pTM5VkJjMsd3VggokKrV1LeFyRXxiWRO4COzTDubFMG er0g1EidEOBeT5cqmSI8NUvo95w32KuI/0Lhnu6fF25hgEq0qgxrH2UdKg6H12iHeLAf TrzBt0cxLfZ8kgKA8OzYFt4SpcyQKjmEdN+6moy7ptNHJ05v8TwNlFOrt0ZxrvoqK85B Fy5zQaBZjQQfLWZJUI0KRXG0pofaL1+qwZzgnPQMEWxbFAYCEhrcTHllCA1gHqAywr2v mEFKY0B5FOqEJ8K6P4obm6ZpzWA2URDg9744QYiK96Zs0dafADtYrGNENu7mcXXvnY1H Fdzg== 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=zyw/eC7l+o0wMdEAj5KqnO8fiNMT30lG6RjAzbV/PlI=; b=yICmtKVn151nlwGc4G/JaSKT2QbK6NljfBupksGDxkZ2H/YuHJbL3P8gLnXYjfo5h5 VlpfhFAj3SnFdeGEK3+UfbE2fGDXvrtVvvRDrvXogZNANe9q8R5u1/MJqESArtEvDVhj w+QBSoMi4ugk0UumJ0t6xCqEh/TWy02me8rrLUxES5oNyRduaoAIgk+WQl/S49a0eG3B 5KQ0Lw77lhF8WrpnCBN6sIiYcpMhu4eo7gA5fbqwH/hmgfVCCPH0ZaYvLX7vQKVeT5ae KR2RRcHZ2zAh6hkPom3NhlvWADM/cxxZGaHYJmaDqJO7O03YbtNVY2yQfxUcHPkmNRAi rqqw== X-Gm-Message-State: ACgBeo0e3W9oCcRp5aJwTQTRtFCCaO7BPrILQLzag6tvkDRlwy6wqmw4 AtmnfUtlyaHGwDtHH43ioDS2jsWmgxVPpYgc/53H5LhJlvw= X-Google-Smtp-Source: AA6agR6mmpBDP7RNimLqgx2iOpt52CKVLdlqOaNx9iqhiZbQ3acLhnOkYWzujon4aia2O8hVvJiDZ0C3gmtMf8BZ834= X-Received: by 2002:a17:90b:1b49:b0:1fd:dccc:91f7 with SMTP id nv9-20020a17090b1b4900b001fddccc91f7mr5332349pjb.200.1661984333042; Wed, 31 Aug 2022 15:18:53 -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: Wed, 31 Aug 2022 15:18:40 -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-Authentication-Results: i=1; imf13.hostedemail.com; dkim=pass header.d=gmail.com header.s=20210112 header.b=D7USHTEK; spf=pass (imf13.hostedemail.com: domain of shy828301@gmail.com designates 209.85.216.42 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=1661984333; a=rsa-sha256; cv=none; b=IvUJICvwjvcjp4unpxMaHVErj38FZ7QDOgGo+tB7hhY+ZINmeFndEa8caJwgwCDfvLhQ1x qbS/lEKmjJn2uNrXLXukR88jzS86VqXexAeS7P25SZshckP25oVCqzxCUoYhdCAYJ8y8Pz QrSo+5E9YndnIMOkUnJbnWn2QhjkVX0= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1661984333; 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=zyw/eC7l+o0wMdEAj5KqnO8fiNMT30lG6RjAzbV/PlI=; b=PVyif9wtFn/r+0t05vtGMJ1f7SgiwXzH0s0au9w8TsdLaAu9Z2MjREIMH5FzlSs90qyYFI vkkPR8jSXJOwINsvHmGMN2iE6TXniyFmc14MKHl0+KlR8hkIJ2Ng1VCO5qbwXup5DOp9YW fiLH4GAiLKJLtwR8OfH697GcPj/iYSo= Authentication-Results: imf13.hostedemail.com; dkim=pass header.d=gmail.com header.s=20210112 header.b=D7USHTEK; spf=pass (imf13.hostedemail.com: domain of shy828301@gmail.com designates 209.85.216.42 as permitted sender) smtp.mailfrom=shy828301@gmail.com; dmarc=pass (policy=none) header.from=gmail.com X-Rspam-User: X-Stat-Signature: eg3p4yfyk1i9deee3m8mubt4quy3yxme X-Rspamd-Queue-Id: EB2A62001D X-Rspamd-Server: rspam09 X-HE-Tag: 1661984333-140292 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 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. > > pmdp_collapse_flush() is actually just called by khugepaged, so arch > specific implementation should not be a problem and we avoid making > gup fast more complicated. > > > > > -- > > Thanks, > > > > David / dhildenb > >