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 77364C433EF for ; Wed, 13 Apr 2022 09:39:05 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id A03BE6B0072; Wed, 13 Apr 2022 05:39:04 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 9B32A6B0073; Wed, 13 Apr 2022 05:39:04 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 8A3136B0074; Wed, 13 Apr 2022 05:39:04 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (relay.hostedemail.com [64.99.140.28]) by kanga.kvack.org (Postfix) with ESMTP id 7C53C6B0072 for ; Wed, 13 Apr 2022 05:39:04 -0400 (EDT) Received: from smtpin18.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id 4474F21F94 for ; Wed, 13 Apr 2022 09:39:04 +0000 (UTC) X-FDA: 79351357008.18.B4F4A25 Received: from szxga02-in.huawei.com (szxga02-in.huawei.com [45.249.212.188]) by imf14.hostedemail.com (Postfix) with ESMTP id E4D07100005 for ; Wed, 13 Apr 2022 09:39:02 +0000 (UTC) Received: from canpemm500002.china.huawei.com (unknown [172.30.72.54]) by szxga02-in.huawei.com (SkyGuard) with ESMTP id 4Kdctt2xVrzgYgD; Wed, 13 Apr 2022 17:37:10 +0800 (CST) Received: from [10.174.177.76] (10.174.177.76) by canpemm500002.china.huawei.com (7.192.104.244) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.24; Wed, 13 Apr 2022 17:38:59 +0800 Subject: Re: [PATCH v2 1/8] mm/swap: remember PG_anon_exclusive via a swp pte bit To: David Hildenbrand CC: linux-kernel , Linux-MM References: <20220329164329.208407-1-david@redhat.com> <20220329164329.208407-2-david@redhat.com> <28142e3e-2556-0ca2-7ac5-7420ef862259@huawei.com> <374d2be1-e13d-e605-ff80-b9d5eee4c40e@redhat.com> From: Miaohe Lin Message-ID: <3b9c6cc6-c5f5-8a8d-0b0f-9ca903cfab20@huawei.com> Date: Wed, 13 Apr 2022 17:38:59 +0800 User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:78.0) Gecko/20100101 Thunderbird/78.6.0 MIME-Version: 1.0 In-Reply-To: <374d2be1-e13d-e605-ff80-b9d5eee4c40e@redhat.com> Content-Type: text/plain; charset="utf-8" Content-Language: en-US Content-Transfer-Encoding: 7bit X-Originating-IP: [10.174.177.76] X-ClientProxiedBy: dggems701-chm.china.huawei.com (10.3.19.178) To canpemm500002.china.huawei.com (7.192.104.244) X-CFilter-Loop: Reflected X-Rspam-User: Authentication-Results: imf14.hostedemail.com; dkim=none; spf=pass (imf14.hostedemail.com: domain of linmiaohe@huawei.com designates 45.249.212.188 as permitted sender) smtp.mailfrom=linmiaohe@huawei.com; dmarc=pass (policy=quarantine) header.from=huawei.com X-Rspamd-Server: rspam03 X-Rspamd-Queue-Id: E4D07100005 X-Stat-Signature: 6ejuerr4m7an3rza66sh37z6c1wcr5jj X-HE-Tag: 1649842742-273472 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 2022/4/13 17:30, David Hildenbrand wrote: > On 13.04.22 10:58, Miaohe Lin wrote: >> On 2022/3/30 0:43, David Hildenbrand wrote: >>> Currently, we clear PG_anon_exclusive in try_to_unmap() and forget about >> ... >>> diff --git a/mm/memory.c b/mm/memory.c >>> index 14618f446139..9060cc7f2123 100644 >>> --- a/mm/memory.c >>> +++ b/mm/memory.c >>> @@ -792,6 +792,11 @@ copy_nonpresent_pte(struct mm_struct *dst_mm, struct mm_struct *src_mm, >>> &src_mm->mmlist); >>> spin_unlock(&mmlist_lock); >>> } >>> + /* Mark the swap entry as shared. */ >>> + if (pte_swp_exclusive(*src_pte)) { >>> + pte = pte_swp_clear_exclusive(*src_pte); >>> + set_pte_at(src_mm, addr, src_pte, pte); >>> + } >>> rss[MM_SWAPENTS]++; >>> } else if (is_migration_entry(entry)) { >>> page = pfn_swap_entry_to_page(entry); >>> @@ -3559,6 +3564,7 @@ vm_fault_t do_swap_page(struct vm_fault *vmf) >>> struct page *page = NULL, *swapcache; >>> struct swap_info_struct *si = NULL; >>> rmap_t rmap_flags = RMAP_NONE; >>> + bool exclusive = false; >>> swp_entry_t entry; >>> pte_t pte; >>> int locked; >>> @@ -3724,6 +3730,46 @@ vm_fault_t do_swap_page(struct vm_fault *vmf) >>> BUG_ON(!PageAnon(page) && PageMappedToDisk(page)); >>> BUG_ON(PageAnon(page) && PageAnonExclusive(page)); >>> >>> + /* >>> + * Check under PT lock (to protect against concurrent fork() sharing >>> + * the swap entry concurrently) for certainly exclusive pages. >>> + */ >>> + if (!PageKsm(page)) { >>> + /* >>> + * Note that pte_swp_exclusive() == false for architectures >>> + * without __HAVE_ARCH_PTE_SWP_EXCLUSIVE. >>> + */ >>> + exclusive = pte_swp_exclusive(vmf->orig_pte); >>> + if (page != swapcache) { >>> + /* >>> + * We have a fresh page that is not exposed to the >>> + * swapcache -> certainly exclusive. >>> + */ >>> + exclusive = true; >>> + } else if (exclusive && PageWriteback(page) && >>> + !(swp_swap_info(entry)->flags & SWP_STABLE_WRITES)) { >> >> Really sorry for late respond and a newbie question. IIUC, if SWP_STABLE_WRITES is set, >> it means concurrent page modifications while under writeback is not supported. For these >> problematic swap backends, exclusive marker is dropped. So the above if statement is to >> filter out these problematic swap backends which have SWP_STABLE_WRITES set. If so, the >> above check should be && (swp_swap_info(entry)->flags & SWP_STABLE_WRITES)), i.e. no "!". >> Or am I miss something? > > Oh, thanks for your careful eyes! > > Indeed, SWP_STABLE_WRITES indicates that the backend *requires* stable > writes, meaning, we must not modify the page while writeback is active. > > So if and only if that is set, we must drop the exclusive marker. > > This essentially corresponds to previous reuse_swap_page() logic: > > bool reuse_swap_page(struct page *page) > { > ... > if (!PageWriteback(page)) { > ... > } else { > ... > if (p->flags & SWP_STABLE_WRITES) { > spin_unlock(&p->lock); > return false; > } > ... > } > > Fortunately, this only affects such backends. For backends without > SWP_STABLE_WRITES, the current code is simply sub-optimal. > > > So yes, this has to be > > } else if (exclusive && PageWriteback(page) && > (swp_swap_info(entry)->flags & SWP_STABLE_WRITES)) { > I am glad that my question helps. :) > > Let me try finding a way to test this, the tests I was running so far > were apparently not using a backend with SWP_STABLE_WRITES. > That will be really helpful. Many thanks for your hard work!