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 2CED6ECAAD1 for ; Wed, 31 Aug 2022 16:31:32 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 9B91B940009; Wed, 31 Aug 2022 12:31:31 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 96913940007; Wed, 31 Aug 2022 12:31:31 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 80985940009; Wed, 31 Aug 2022 12:31:31 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id 72B63940007 for ; Wed, 31 Aug 2022 12:31:31 -0400 (EDT) Received: from smtpin23.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id 43A2640794 for ; Wed, 31 Aug 2022 16:31:31 +0000 (UTC) X-FDA: 79860428382.23.815B941 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by imf26.hostedemail.com (Postfix) with ESMTP id CA1FD140058 for ; Wed, 31 Aug 2022 16:31:30 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1661963490; h=from:from: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:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=/pmEe4DRaxxJzQ4pMfZASLYe1HuEIzWuGYKBiRUg9UA=; b=BI71CG25FPxWjFTTiTU1PE2JQAgYE2WnpgabPHl8piJOPJ7MUVIUBdTQlGscK6RAu/7PkH XhjmvZDif8xlQJRtT5mH+8KBD+xJk3aySUq1DPl8s+69pZ8WbT54tD2ZNDG8WYmqlF6rsR OmzrloM9Oyg7HCHKDhMqKqApo6cdcas= Received: from mail-wm1-f69.google.com (mail-wm1-f69.google.com [209.85.128.69]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_128_GCM_SHA256) id us-mta-492-znigSFubMXeQsBRpJzZGBQ-1; Wed, 31 Aug 2022 12:31:26 -0400 X-MC-Unique: znigSFubMXeQsBRpJzZGBQ-1 Received: by mail-wm1-f69.google.com with SMTP id r83-20020a1c4456000000b003a7b679981cso7498541wma.6 for ; Wed, 31 Aug 2022 09:31:26 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:in-reply-to:subject:organization:from :references:cc:to:content-language:user-agent:mime-version:date :message-id:x-gm-message-state:from:to:cc; bh=/pmEe4DRaxxJzQ4pMfZASLYe1HuEIzWuGYKBiRUg9UA=; b=2wLGJTMc0zqwSP6/ukN1BVmrEL3xTMR6oykSG/u68PdgcBNW/kwilRGBkzZJ3BCBvk mTrfH+1tOJpxEwM4uhuzkjbWZ/FJYl37x86X7b2gsVQhEng62625hKZUAMgeDtTFbrPe KCTOXmwE4ftSxugfthBQUoTaOUjf6C0a1tvaSZmQXtQGGlVRlqtLSuT9wpCxZ81FrdRr ZXKBDDSmaBWzfy/lWgSgRCJVNOmv1SMpAAajZ9+AFercpLX8Q7yQMR0aQchsUOpIHjz0 SD5q8yLMIUX8+kR0b96M6FIc6d3LwBd2QvwqQ69tOJGrypTVzeD4WkhLnSBEbcjrmDn3 cQng== X-Gm-Message-State: ACgBeo0U+ewbnNiW/OzkPcSqHk60KC1dWTeZQ6mmvYBL+6KUKvwH/kC5 PaMhsVoAXi0kvWtjp7Y1Fn77fD2rUwYbWT2Qh7nknYWG1Sd6d58OJ/kkru0bXgy6YT6btXmYBJr /N+Hojn4vO0U= X-Received: by 2002:a5d:5a9a:0:b0:226:e840:c551 with SMTP id bp26-20020a5d5a9a000000b00226e840c551mr3386345wrb.679.1661963484979; Wed, 31 Aug 2022 09:31:24 -0700 (PDT) X-Google-Smtp-Source: AA6agR4GhqhvTHnUNFTq03uLhSghi7ZyuZ9Tj26NYwUfm6CzIdyk2zRN+Fo1MwvIvsXbKf0v1GUyow== X-Received: by 2002:a5d:5a9a:0:b0:226:e840:c551 with SMTP id bp26-20020a5d5a9a000000b00226e840c551mr3386326wrb.679.1661963484637; Wed, 31 Aug 2022 09:31:24 -0700 (PDT) Received: from ?IPV6:2003:cb:c706:2900:1613:4308:aca3:2786? (p200300cbc706290016134308aca32786.dip0.t-ipconnect.de. [2003:cb:c706:2900:1613:4308:aca3:2786]) by smtp.gmail.com with ESMTPSA id bg15-20020a05600c3c8f00b003a4f08495b7sm2855208wmb.34.2022.08.31.09.31.23 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Wed, 31 Aug 2022 09:31:24 -0700 (PDT) Message-ID: <4d067a99-1112-3b3d-bedf-35c1124904fd@redhat.com> Date: Wed, 31 Aug 2022 18:31:23 +0200 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101 Thunderbird/91.12.0 To: Peter Xu Cc: John Hubbard , Jason Gunthorpe , linux-kernel@vger.kernel.org, linux-mm@kvack.org, Andrew Morton , Mel Gorman , "Matthew Wilcox (Oracle)" , Andrea Arcangeli , Hugh Dickins , Alistair Popple 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> <9a4fe603-950e-785b-6281-2e309256463f@nvidia.com> <68b38ac4-c680-b694-21a9-1971396d63b9@redhat.com> From: David Hildenbrand Organization: Red Hat Subject: Re: [PATCH v1 2/3] mm/gup: use gup_can_follow_protnone() also in GUP-fast In-Reply-To: X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Language: en-US Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1661963490; a=rsa-sha256; cv=none; b=V28l9oLHz6hy6IQKfa3U5vmRvZZabNNPDxnu9QIBEh0Lb5c1Wzr3Zf9MFu57oPcBtDSYLw bwW/LjfJ3eaNGrc4aV7SOLg2zcaSJexz8Yc4DXa/ShYcGtkBchEZRnd+MyTSykPMWFZRZS xQGt9Q00QRGD3WCql81+pAcFJfgDeYA= ARC-Authentication-Results: i=1; imf26.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=BI71CG25; spf=pass (imf26.hostedemail.com: domain of david@redhat.com designates 170.10.133.124 as permitted sender) smtp.mailfrom=david@redhat.com; dmarc=pass (policy=none) header.from=redhat.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1661963490; 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:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=/pmEe4DRaxxJzQ4pMfZASLYe1HuEIzWuGYKBiRUg9UA=; b=fYr3u4CaV+t3WPEVss6PV/gG0Marm1yQ70d9ZvlXkwahaCAEvxG4slWIWkM31bExDEdwGr MZOrBPBkIHYt34aOlsZTP0iyYlBAWcWae33ZkoSJi+wy9dZ/+BBjlEP9n/vf6+O9X2F5k4 oMLjb+DXDDnXgeENgYjXTKrTManj2II= X-Rspam-User: X-Stat-Signature: ehzggqx3p39x3b3kqj5jg98p6z9ehi8j X-Rspamd-Server: rspam08 X-Rspamd-Queue-Id: CA1FD140058 Authentication-Results: imf26.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=BI71CG25; spf=pass (imf26.hostedemail.com: domain of david@redhat.com designates 170.10.133.124 as permitted sender) smtp.mailfrom=david@redhat.com; dmarc=pass (policy=none) header.from=redhat.com X-HE-Tag: 1661963490-358128 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: [...] >> + /* >> + * We have to make sure that while we clear PageAnonExclusive, that >> + * the page is not pinned and that concurrent GUP-fast won't succeed in >> + * concurrently pinning the page. >> + * >> + * Conceptually, GUP-fast pinning code of anon pages consists of: >> + * (1) Read the PTE >> + * (2) Pin the mapped page >> + * (3) Check if the PTE changed by re-reading it; back off if so. >> + * (4) Check if PageAnonExclusive is not set; back off if so. >> + * >> + * Conceptually, PageAnonExclusive clearing code consists of: >> + * (1) Clear PTE >> + * (2) Check if the page is pinned; back off if so. >> + * (3) Clear PageAnonExclusive >> + * (4) Restore PTE (optional) >> + * >> + * In GUP-fast, we have to make sure that (2),(3) and (4) happen in >> + * the right order. Memory order between (2) and (3) is handled by >> + * GUP-fast, independent of PageAnonExclusive. >> + * >> + * When clearing PageAnonExclusive(), we have to make sure that (1), >> + * (2), (3) and (4) happen in the right order. >> + * >> + * Note that (4) has to happen after (3) in both cases to handle the >> + * corner case whereby the PTE is restored to the original value after >> + * clearing PageAnonExclusive and while GUP-fast might not detect the >> + * PTE change, it will detect the PageAnonExclusive change. >> + * >> + * We assume that there might not be a memory barrier after >> + * clearing/invalidating the PTE (1) and before restoring the PTE (4), >> + * so we use explicit ones here. >> + * >> + * These memory barriers are paired with memory barriers in GUP-fast >> + * code, including gup_must_unshare(). >> + */ >> + >> + /* Clear/invalidate the PTE before checking for PINs. */ >> + if (IS_ENABLED(CONFIG_HAVE_FAST_GUP)) >> + smp_mb(); > > Wondering whether this could be smp_mb__before_atomic(). We'll read via atomic_read(). That's a non-RMW operation. smp_mb__before_atomic() only applies to RMW (Read Modify Write) operations. I have an updated patch with improved description/comments, that includes the following explanation/example and showcases how the two barrier pairs interact: Thread 0 (KSM) Thread 1 (GUP-fast) (B1) Read the PTE # (B2) skipped without FOLL_WRITE (A1) Clear PTE smb_mb() (A2) Check pinned (B3) Pin the mapped page smb_mb() (A3) Clear PageAnonExclusive smb_wmb() (A4) Restore PTE (B4) Check if the PTE changed smb_rmb() (B5) Check PageAnonExclusive > >> + >> + if (unlikely(page_maybe_dma_pinned(page))) >> + return -EBUSY; >> ClearPageAnonExclusive(page); >> + >> + /* Clear PageAnonExclusive() before eventually restoring the PTE. */ >> + if (IS_ENABLED(CONFIG_HAVE_FAST_GUP)) >> + smp_mb__after_atomic(); >> return 0; >> } >> >> diff --git a/mm/huge_memory.c b/mm/huge_memory.c >> index e9414ee57c5b..2aef8d76fcf2 100644 >> --- a/mm/huge_memory.c >> +++ b/mm/huge_memory.c >> @@ -2140,6 +2140,8 @@ static void __split_huge_pmd_locked(struct vm_area_struct *vma, pmd_t *pmd, >> * >> * In case we cannot clear PageAnonExclusive(), split the PMD >> * only and let try_to_migrate_one() fail later. >> + * >> + * See page_try_share_anon_rmap(): invalidate PMD first. >> */ >> anon_exclusive = PageAnon(page) && PageAnonExclusive(page); >> if (freeze && anon_exclusive && page_try_share_anon_rmap(page)) >> @@ -3177,6 +3179,7 @@ int set_pmd_migration_entry(struct page_vma_mapped_walk *pvmw, >> flush_cache_range(vma, address, address + HPAGE_PMD_SIZE); >> pmdval = pmdp_invalidate(vma, address, pvmw->pmd); >> >> + /* See page_try_share_anon_rmap(): invalidate PMD first. */ >> anon_exclusive = PageAnon(page) && PageAnonExclusive(page); >> if (anon_exclusive && page_try_share_anon_rmap(page)) { >> set_pmd_at(mm, address, pvmw->pmd, pmdval); >> diff --git a/mm/ksm.c b/mm/ksm.c >> index d7526c705081..971cf923c0eb 100644 >> --- a/mm/ksm.c >> +++ b/mm/ksm.c >> @@ -1091,6 +1091,7 @@ static int write_protect_page(struct vm_area_struct *vma, struct page *page, >> goto out_unlock; >> } >> >> + /* See page_try_share_anon_rmap(): clear PTE first. */ >> if (anon_exclusive && page_try_share_anon_rmap(page)) { >> set_pte_at(mm, pvmw.address, pvmw.pte, entry); >> goto out_unlock; >> diff --git a/mm/migrate_device.c b/mm/migrate_device.c >> index 27fb37d65476..47e955212f15 100644 >> --- a/mm/migrate_device.c >> +++ b/mm/migrate_device.c >> @@ -193,20 +193,16 @@ static int migrate_vma_collect_pmd(pmd_t *pmdp, >> bool anon_exclusive; >> pte_t swp_pte; >> > > flush_cache_page() missing here? Hmm, wouldn't that already be missing on the !anon path right now? > > Better copy Alistair too when post formally since this will have a slight > conflict with the other thread. Yes, I'll give him a heads-up right away: full patch in https://lkml.kernel.org/r/68b38ac4-c680-b694-21a9-1971396d63b9@redhat.com Thanks for having a look Peter1 -- Thanks, David / dhildenb