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 8DCCAC433EF for ; Thu, 13 Jan 2022 17:55:35 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id DB59A6B0071; Thu, 13 Jan 2022 12:55:34 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id D642D6B0073; Thu, 13 Jan 2022 12:55:34 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id BDD3D6B0074; Thu, 13 Jan 2022 12:55:34 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0242.hostedemail.com [216.40.44.242]) by kanga.kvack.org (Postfix) with ESMTP id AB3336B0071 for ; Thu, 13 Jan 2022 12:55:34 -0500 (EST) Received: from smtpin18.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay01.hostedemail.com (Postfix) with ESMTP id 712F118209BD4 for ; Thu, 13 Jan 2022 17:55:34 +0000 (UTC) X-FDA: 79026016188.18.E178497 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by imf24.hostedemail.com (Postfix) with ESMTP id DC19C180004 for ; Thu, 13 Jan 2022 17:55:33 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1642096533; 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=Hb/USEUKkhn3s02KS0xUKc0vx42orgCcuSnwseHShRk=; b=gkx5aeZbFqb5AhViGi5KDHfbyN2GLUo4IGHEJ8cgZ95S2qlVUnVlKEb3dTmcw3yOEKHouo hm/OxEpr+G0eFLWB+LNbymq6SOd1V6UgmCVb81mt9ICdiMGQBo3CpNGtTzgbBy3McA9fxJ kHUFbrGp7x3BTx2Lts6gMYmH+QoppGE= Received: from mail-ed1-f72.google.com (mail-ed1-f72.google.com [209.85.208.72]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-202--hSwXq_ANOy-RS3_qhM_yg-1; Thu, 13 Jan 2022 12:55:30 -0500 X-MC-Unique: -hSwXq_ANOy-RS3_qhM_yg-1 Received: by mail-ed1-f72.google.com with SMTP id m8-20020a056402510800b003f9d22c4d48so6020038edd.21 for ; Thu, 13 Jan 2022 09:55:29 -0800 (PST) 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 :content-language:to:cc:references:from:organization:subject :in-reply-to:content-transfer-encoding; bh=Hb/USEUKkhn3s02KS0xUKc0vx42orgCcuSnwseHShRk=; b=YH9Mpwj223qB3Ul4qG1pH0spNqXmn0E5MrZ6iABio3b3afgr1QJW0MRigB5zIQDN7R +F4IOH/UEFkf6fhA6Uza6IiVcY+AUEY5tg+zMu9gZBc0ScnF31QW1RzinhYiZnfVEoBL cNJYHiGOstRa6X68OZRgG+TGjFDPUyUj5VdZoZpTyBkLgH/aRxb4FuW12szikaY7bYeH aZS0BU46ZZquFEbU7uR+1/oRSgaSkd+Yp6MxS7pWFFLakutwth673MMFWLp3nk8Apqz2 nMJJOWNLRuVxVrWZm9LDdq2NMFRe3BOMfgaKme0z5PM5AEoDRmZUSPGMvdGNdGzwNXAa sdDg== X-Gm-Message-State: AOAM530frPUwvn+n9sW5b6aW+fVcqP34hm+KXU0XNZlf6A8xagISPidZ 1+6QyXSKMqkIjL6DCnIClR+SzyNbObAJUa0SRnStsTJzK93KtHOsdzJt7vmE3T/Oenh8LLwI9Xp l7WVxxSd5tRs= X-Received: by 2002:a05:6402:40cf:: with SMTP id z15mr5184669edb.185.1642096529027; Thu, 13 Jan 2022 09:55:29 -0800 (PST) X-Google-Smtp-Source: ABdhPJy6o+NIPMdPbJERoH5Eo09Y5Ux7/iYWtpEKrIO6PgwTcNZgvFXP0WCgNbsuUjSVTZorr3sY1Q== X-Received: by 2002:a05:6402:40cf:: with SMTP id z15mr5184652edb.185.1642096528838; Thu, 13 Jan 2022 09:55:28 -0800 (PST) Received: from ?IPV6:2003:cb:c703:e200:8511:ed0f:ac2c:42f7? (p200300cbc703e2008511ed0fac2c42f7.dip0.t-ipconnect.de. [2003:cb:c703:e200:8511:ed0f:ac2c:42f7]) by smtp.gmail.com with ESMTPSA id g16sm1101134ejt.202.2022.01.13.09.55.28 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Thu, 13 Jan 2022 09:55:28 -0800 (PST) Message-ID: <0c44a89d-06a7-d0bb-e71e-7947d651f4d1@redhat.com> Date: Thu, 13 Jan 2022 18:55:27 +0100 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101 Thunderbird/91.4.0 To: Linus Torvalds Cc: Matthew Wilcox , Liang Zhang , Andrew Morton , Linux-MM , Linux Kernel Mailing List , wangzhigang17@huawei.com References: <20220113140318.11117-1-zhangliang5@huawei.com> <172ccfbb-7e24-db21-7d84-8c8d8c3805fd@redhat.com> From: David Hildenbrand Organization: Red Hat Subject: Re: [PATCH] mm: reuse the unshared swapcache page in do_wp_page 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 X-Rspamd-Server: rspam01 X-Rspamd-Queue-Id: DC19C180004 X-Stat-Signature: 8nukfzfisoy6qbqyc1o5ybs7da9mmfjx Authentication-Results: imf24.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=gkx5aeZb; spf=none (imf24.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-HE-Tag: 1642096533-851145 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.01.22 18:44, Linus Torvalds wrote: > On Thu, Jan 13, 2022 at 9:25 AM David Hildenbrand wrote: >> >> I might be missing something, but it's not only about whether we can remove >> the page from the swap cache, it's about whether we can reuse the page >> exclusively in a process with write access, avoiding a COW. And for that we >> have to check if it's mapped somewhere else already (readable). > > No. > > The "try to remove from swap cache" is one thing. That uses the swap count. However, reuse_swap_page() currently does multiple things, and that's part of the issue. > > The "see if we can reuse this page for COW" is a completely different > test, and that's the "page_count() == 1" one. > > The two should not be mixed up with each other. Just don't do it. > There's no reason - except for legacy confusion that should be > actively avoided and removed. > > IOW, the COW path would do > > trylock - copy if fails > try to remove from swap cache > if page_count() is now 1, we can reuse it I thought about that exact sequence as well. I remember stumbling over one of the other users of reuse_swap_page() that would require more thought -- do_swap_page(). There, we essentially do a COW before having the page mapped. (nasty) I'll give it more thought. > > Note how the "try to remove from swap cache" is entirely independent > of whether we then reuse it or not. > > And yes, we can have optimistic other tests - like not even bothering > to trylock if we can see that the page-count is so elevated that it > makes no difference and trying to remove from swap cache would be just > pointless extra work (both the removal itself, and then potentially > later re-additions). > > But those should be seen for what they are - not important for > semantics, only a "don't bother, this page has so many users that we > already know that removing the swapcache one doesn't make any > difference at all". Right. > > Now, it's possible that I'm missing something, but I think this kind > of clarity is very much what we should aim for. Clear rules, no mixing > of "can I COW this" with "can I remove this from the swap cache". I consider reuse_swap_page() at this point just absolutely nasty. While we're at it, is there a real reason we can't simplify to diff --git a/mm/memory.c b/mm/memory.c index e8e2144cbfa6..ab114a5862a0 100644 --- a/mm/memory.c +++ b/mm/memory.c @@ -3295,7 +3295,7 @@ static vm_fault_t do_wp_page(struct vm_fault *vmf) goto copy; if (!trylock_page(page)) goto copy; - if (PageKsm(page) || page_mapcount(page) != 1 || page_count(page) != 1) { + if (PageKsm(page) || page_count(page) != 1) { unlock_page(page); goto copy; Our page mapping has to hold a reference, so it seems unnecessary to check both. > > And now I need to start my travel nightmare, so I'll be effectively > offline for the next 24 hours ;( Happy traveling then :) No worries, I'll be working on all this more than 24 hours, especially PageAnonExclusive() that makes my head hurt when it comes to swap, but this discussion already helps a lot on how to eventually sort it all out. -- Thanks, David / dhildenb