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 0A742C433F5 for ; Wed, 13 Apr 2022 12:31:13 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 636FF6B0072; Wed, 13 Apr 2022 08:31:12 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 5E58F6B0073; Wed, 13 Apr 2022 08:31:12 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 45FFC6B0074; Wed, 13 Apr 2022 08:31:12 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0030.hostedemail.com [216.40.44.30]) by kanga.kvack.org (Postfix) with ESMTP id 3557B6B0072 for ; Wed, 13 Apr 2022 08:31:12 -0400 (EDT) Received: from smtpin27.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay01.hostedemail.com (Postfix) with ESMTP id D0D6E1828D83A for ; Wed, 13 Apr 2022 12:31:11 +0000 (UTC) X-FDA: 79351790742.27.757D480 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by imf21.hostedemail.com (Postfix) with ESMTP id 5429F1C0004 for ; Wed, 13 Apr 2022 12:31:11 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1649853070; 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=bOgoRxfzCzI5q33umdhDYElMBhuyEs/gGqSfqtPLkH4=; b=MSQ43mqgLRpcYMWq+AIe32kOE5lHN6xJD1RdeFmiFSHeloJkdSKXMLDyMgvPxY4/THKXT7 f92VkpXJjzLj+GIPU3IivbOLVnrh6Jln06JRp1Oay3coeaqXtjNm22nBWU9re3xr+wGEVh l9SKXfPfipX+J8eABNyRWhpoq9yKG1I= Received: from mail-wm1-f72.google.com (mail-wm1-f72.google.com [209.85.128.72]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-304-IHIoTzpRNzuPlZgGFzFxcA-1; Wed, 13 Apr 2022 08:31:07 -0400 X-MC-Unique: IHIoTzpRNzuPlZgGFzFxcA-1 Received: by mail-wm1-f72.google.com with SMTP id az27-20020a05600c601b00b0038ff021c8a4so140961wmb.1 for ; Wed, 13 Apr 2022 05:31:07 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:message-id:date:mime-version:user-agent:subject :content-language:to:cc:references:from:organization:in-reply-to :content-transfer-encoding; bh=bOgoRxfzCzI5q33umdhDYElMBhuyEs/gGqSfqtPLkH4=; b=KP5YHkIXM+ipJ6+OifwQssXLO37SznwH1RlQ1Fv+0dRrjkh2cg7c7JlHG4Z9KzMcEO GgCf7aJ+wvGoFKFTLjLeHNwoxY+q6L0L6y/MmEzyObrmbPUet8wNdBmDw6L3TNn03Mv0 OlzX6WOc+P8nAsPIAn5h34TqLsGWhxXQI1eqAw2sJ8JZugBz4tV3It8Td0t9L3K+fNS8 6XAwk9EuetU7Ib6ytMFyeEJvzXWCpWcEtAayzJQ/tzx2nIZxeB30zwCXvu3HU146NdU4 IqJ1macZ5sm7y9NjUmVaeLYPKiIU4bAUGBf63sfrGmgQwQ2QFTfkteC8fgDGmowOIgJw isAA== X-Gm-Message-State: AOAM5338JWLUyKXXo86IxjP8zfL4QJpXbd4fnBha3AIXnG68BfsHMJ+q eBWSOETssNkJEhK94sAvBdYECtlAkAJpY+I+ZmUhIi3fCeoKdrXiv9EIa1qB3+pfrBpySruqtoP M6NDHY8Z9ajA= X-Received: by 2002:adf:e0c5:0:b0:206:1ba3:26aa with SMTP id m5-20020adfe0c5000000b002061ba326aamr32484212wri.645.1649853066491; Wed, 13 Apr 2022 05:31:06 -0700 (PDT) X-Google-Smtp-Source: ABdhPJw/66NqVU7vWO4e1lJW3L4YvkXKeRrNtgBHQuEpQdeZpBwqU+kp1AOvAjnVhG5i/3ySErOLjA== X-Received: by 2002:adf:e0c5:0:b0:206:1ba3:26aa with SMTP id m5-20020adfe0c5000000b002061ba326aamr32484202wri.645.1649853066236; Wed, 13 Apr 2022 05:31:06 -0700 (PDT) Received: from ?IPV6:2003:cb:c704:5800:1078:ebb9:e2c3:ea8c? (p200300cbc70458001078ebb9e2c3ea8c.dip0.t-ipconnect.de. [2003:cb:c704:5800:1078:ebb9:e2c3:ea8c]) by smtp.gmail.com with ESMTPSA id g7-20020a5d64e7000000b00204a13925dcsm34138874wri.11.2022.04.13.05.31.05 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Wed, 13 Apr 2022 05:31:05 -0700 (PDT) Message-ID: Date: Wed, 13 Apr 2022 14:31:05 +0200 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101 Thunderbird/91.6.2 Subject: Re: [PATCH v2 1/8] mm/swap: remember PG_anon_exclusive via a swp pte bit To: Miaohe Lin Cc: linux-kernel , Linux-MM , Minchan Kim 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> <3b9c6cc6-c5f5-8a8d-0b0f-9ca903cfab20@huawei.com> From: David Hildenbrand Organization: Red Hat In-Reply-To: <3b9c6cc6-c5f5-8a8d-0b0f-9ca903cfab20@huawei.com> 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 X-Stat-Signature: x3gr8zzuboctkuj3umq4ubqzpaj3xax6 Authentication-Results: imf21.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=MSQ43mqg; spf=none (imf21.hostedemail.com: domain of david@redhat.com has no SPF policy when checking 170.10.133.124) smtp.mailfrom=david@redhat.com; dmarc=pass (policy=none) header.from=redhat.com X-Rspam-User: X-Rspamd-Server: rspam08 X-Rspamd-Queue-Id: 5429F1C0004 X-HE-Tag: 1649853071-608044 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 13.04.22 11:38, Miaohe Lin wrote: > 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! > FWIW, I tried with zram, which sets SWP_STABLE_WRITES ... but, it seems to always do a synchronous writeback, so it cannot really trigger this code path. commit f05714293a591038304ddae7cb0dd747bb3786cc Author: Minchan Kim Date: Tue Jan 10 16:58:15 2017 -0800 mm: support anonymous stable page mentions "During developemnt for zram-swap asynchronous writeback,"; maybe that can be activated somehow? Putting Minchan on CC. -- Thanks, David / dhildenb