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 8228DC433F5 for ; Thu, 13 Jan 2022 14:47:02 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id A1C376B0072; Thu, 13 Jan 2022 09:47:01 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 9CC406B0073; Thu, 13 Jan 2022 09:47:01 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 86B0C6B0074; Thu, 13 Jan 2022 09:47:01 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0115.hostedemail.com [216.40.44.115]) by kanga.kvack.org (Postfix) with ESMTP id 790F36B0072 for ; Thu, 13 Jan 2022 09:47:01 -0500 (EST) Received: from smtpin26.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay03.hostedemail.com (Postfix) with ESMTP id 19CBC827FC6E for ; Thu, 13 Jan 2022 14:47:01 +0000 (UTC) X-FDA: 79025541000.26.0671CC1 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by imf13.hostedemail.com (Postfix) with ESMTP id A2B7120002 for ; Thu, 13 Jan 2022 14:47:00 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1642085219; 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=lSDQWCPWVjF9NlavrTbv8hqhD4ANM1MfQ/emEeuv7nY=; b=Z4oHyidz56YcYiK8A921UGx17BiJUdeqGwhYaaYpEchab1zFzRwdMwqyPyRAKwTulVYRtf dtP/LvvsZg8tHexk27bP6IaILvbrYD91PZ5lLoY/5LmDlWa8uWyb2gCntfVWP4agnN1iAH FEBYAIxBcNl5S+MgFvqGsC8hpiVyh70= Received: from mail-ed1-f71.google.com (mail-ed1-f71.google.com [209.85.208.71]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-91-gSOhD51uPd2mpx7QWYaLlA-1; Thu, 13 Jan 2022 09:46:58 -0500 X-MC-Unique: gSOhD51uPd2mpx7QWYaLlA-1 Received: by mail-ed1-f71.google.com with SMTP id y18-20020a056402271200b003fa16a5debcso5536822edd.14 for ; Thu, 13 Jan 2022 06:46:57 -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=lSDQWCPWVjF9NlavrTbv8hqhD4ANM1MfQ/emEeuv7nY=; b=gAcGnP5/xx+FB8d3eqdrhNJ/N6TrOAs/a/CI5Af03AwoLN79DQ4Y9I6+pk6Tx/Q9bi aDT/jNoccK3SfxqNBgyzCvOFRW5g2bF8iA7bd2h/1EEoIpS06RbY2Dj5wSSxB6fdqwcT qLPS5nMvdJrY5lp+jAK3Qzx5SMyc3kAGOAl5tFqcZh5Hrz78TdtrZJC/z8kIwRVYzVRC j1Y7/mosUtNJ0isQPTBkG8YdznRHePG7fbskydUwQC9gPkmxWL71637B2RGW/BdgGJIS MHGsEZzs+QtySo96gDxsCKJNZgQ+ycI4CTAWkTyP24VAo+n+wm1A+EC++c+1hik2WqrS GooQ== X-Gm-Message-State: AOAM531I8GEf+R6RO0WM81jzSaSfhXf0WprVy5tvHPgKGlXq1StcIA0v KQhaPYrgNfzVcfVDG09BvjMfK2hxqZCnK96udWo0KsPrGpRgKwcppRbNR55P0RxbcErnf9zOl+c n4NZV42wCYvY= X-Received: by 2002:a17:906:36da:: with SMTP id b26mr3560779ejc.213.1642085217039; Thu, 13 Jan 2022 06:46:57 -0800 (PST) X-Google-Smtp-Source: ABdhPJz/38BHGBrb6Tyf8n/r/vXXRORwnLjRqkhkzTt2dZhCdq1sN4eIg2NQzE+YI3p2tvbF891lHA== X-Received: by 2002:a17:906:36da:: with SMTP id b26mr3560766ejc.213.1642085216813; Thu, 13 Jan 2022 06:46:56 -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 g9sm938257ejo.222.2022.01.13.06.46.55 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Thu, 13 Jan 2022 06:46:56 -0800 (PST) Message-ID: Date: Thu, 13 Jan 2022 15:46:54 +0100 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101 Thunderbird/91.4.0 To: Matthew Wilcox , Liang Zhang Cc: akpm@linux-foundation.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org, wangzhigang17@huawei.com, Linus Torvalds References: <20220113140318.11117-1-zhangliang5@huawei.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 Authentication-Results: imf13.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=Z4oHyidz; dmarc=pass (policy=none) header.from=redhat.com; spf=none (imf13.hostedemail.com: domain of david@redhat.com has no SPF policy when checking 170.10.129.124) smtp.mailfrom=david@redhat.com X-Stat-Signature: wbsza4m6jqtc78mkqdgxogojj95mgyze X-Rspamd-Queue-Id: A2B7120002 X-Rspamd-Server: rspam12 X-HE-Tag: 1642085220-433076 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 15:39, Matthew Wilcox wrote: > On Thu, Jan 13, 2022 at 10:03:18PM +0800, Liang Zhang wrote: >> In current implementation, process's read requestions will fault in pages >> with WP flags in PTEs. Next, if process emit a write requestion will go >> into do_wp_page() and copy data to a new allocated page from the old one >> due to refcount > 1 (page table mapped and swapcache), which could be >> result in performance degradation. In fact, this page is exclusively owned >> by this process and the duplication from old to a new allocated page is >> really unnecessary. >> >> So In this situation, these unshared pages can be reused by its process. > > Let's bring Linus in on this, but I think this reintroduces all of the > mapcount problems that we've been discussing recently. > > How about this as an alternative? > > +++ b/mm/memory.c > @@ -3291,11 +3291,11 @@ static vm_fault_t do_wp_page(struct vm_fault *vmf) > struct page *page = vmf->page; > > /* PageKsm() doesn't necessarily raise the page refcount */ > - if (PageKsm(page) || page_count(page) != 1) > + if (PageKsm(page) || page_count(page) != 1 + PageSwapCache(page)) > goto copy; > if (!trylock_page(page)) > goto copy; > - if (PageKsm(page) || page_mapcount(page) != 1 || page_count(page) != 1) { > + if (PageKsm(page) || page_mapcount(page) != 1 || page_count(page) != 1 + PageSwapCache(page)) { > unlock_page(page); > goto copy; > } Funny, I was staring at swap reuse code as I received this mail ... because if we're not using reuse_swap_page() here anymore, we shouldn't really be reusing it anywhere for consistency, most prominently in do_swap_page() when we handle vmf->flags & FAULT_FLAG_WRITE just similarly as we do here ... And that's where things get hairy and I am still trying to figure out all of the details. Regarding above: If the page is swapped out in multiple processes but was only faulted into the current process R/O, and then we try to write: 1. Still in the swapcache: PageSwapCache() 2. Mapped only by one process: page_mapcount(page) == 1 3. Reference from one page table and the swap cache: page_count(page) == But other processes could read-fault on the swapcache page, no? I think we'd really have to check against the swapcount as well ... essentially reuse_swap_page(), no? -- Thanks, David / dhildenb