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 73119C433EF for ; Thu, 13 Jan 2022 15:04:30 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id D3D726B0073; Thu, 13 Jan 2022 10:04:29 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id CC5A46B0075; Thu, 13 Jan 2022 10:04:29 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id B16E86B0078; Thu, 13 Jan 2022 10:04:29 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0104.hostedemail.com [216.40.44.104]) by kanga.kvack.org (Postfix) with ESMTP id 9BB876B0073 for ; Thu, 13 Jan 2022 10:04:29 -0500 (EST) Received: from smtpin03.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay05.hostedemail.com (Postfix) with ESMTP id 53EA21813ABB1 for ; Thu, 13 Jan 2022 15:04:29 +0000 (UTC) X-FDA: 79025585058.03.010691E Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by imf29.hostedemail.com (Postfix) with ESMTP id 9A37D12001C for ; Thu, 13 Jan 2022 15:04:26 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1642086265; 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=cRTsijcpTTyHraoR+V3O1Mi16FiVJpK3PKf++RerMpw=; b=d3ltDT23Hr3d02is4vn6Ot7ZvTbYpSlt40h7ZN9G8K/5rFuAvwX7m4PqpDNLrrKU/V6ovn /Wj98DLPTE2/9Q+LRaaK8c6VdiMP83j+xZjqHGduwmeq8cwRijGJSzcIP8tqdDIzU+zNcm Cgejy7lqwNGodz/gXVUAz3tfXnnOk58= Received: from mail-ed1-f69.google.com (mail-ed1-f69.google.com [209.85.208.69]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-407--nZRczdkPsqS9POblVQKJw-1; Thu, 13 Jan 2022 10:04:24 -0500 X-MC-Unique: -nZRczdkPsqS9POblVQKJw-1 Received: by mail-ed1-f69.google.com with SMTP id h1-20020aa7cdc1000000b0040042dd2fe4so4329363edw.17 for ; Thu, 13 Jan 2022 07:04:24 -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:subject :content-language:to:cc:references:from:organization:in-reply-to :content-transfer-encoding; bh=cRTsijcpTTyHraoR+V3O1Mi16FiVJpK3PKf++RerMpw=; b=XUiA1Fq4Hr3IMUYx9Y8ce2o037be+zi65YsBTjSLQOjnGDl/UOa+WDHHzNnEiimNoE kVh4flFysbrIH1d1x2/+Yr6bKcsd86tph5XS6SDpe0LAoEcmpj/vyDbQ1mas4mr/0/S+ ggsu0NLPtngJdzx8+Ah+xu5UJH0T2D3jRf5RQgsQffu8c3WeHZzzOUCFJh3c+FEiw53+ rVR7HWdLAxeg4cj5+K5CWBdWfsO5wVCOqInfiyN5i6+j+FID2rGROZJSRJ8LB7WooiHN BJj3ZSDaN+l6cVRjGrjM3Nssod9rIkjWyZHp4wBGFN3yCrNyP8Lz8jhmcBnULpmnmcg4 rBdg== X-Gm-Message-State: AOAM530eBGzyJNTm/+Sd2VrFbW8cwMvCaFdH1O5w/3gCIQazWu63UdAt ILxhGkKBlfvzIa8Go7DHnFYpKGsVNU+VvoF93tVrMUZEd/Ml0P6scd2i+uWVS1DqvbCt/8T9Yd3 LxBfNaLIFrP4= X-Received: by 2002:a17:907:a411:: with SMTP id sg17mr3970384ejc.386.1642086263418; Thu, 13 Jan 2022 07:04:23 -0800 (PST) X-Google-Smtp-Source: ABdhPJwjs3sI3lBKoJfQelsLOUNn03wgAPiabACCHXkb0MaN1/2AMIGOdqbV88alKCvlLM5aoh0wdg== X-Received: by 2002:a17:907:a411:: with SMTP id sg17mr3970358ejc.386.1642086263121; Thu, 13 Jan 2022 07:04:23 -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 d27sm953104ejd.117.2022.01.13.07.04.22 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Thu, 13 Jan 2022 07:04:22 -0800 (PST) Message-ID: <79e04824-73d5-c69f-64fb-f67051f4e124@redhat.com> Date: Thu, 13 Jan 2022 16:04:21 +0100 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101 Thunderbird/91.4.0 Subject: Re: [PATCH] mm: reuse the unshared swapcache page in do_wp_page To: Matthew Wilcox Cc: Liang Zhang , 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 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-Queue-Id: 9A37D12001C X-Stat-Signature: a9zeig3rzqpo6yeuksbk7sy1c535jupz Authentication-Results: imf29.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=d3ltDT23; dmarc=pass (policy=none) header.from=redhat.com; spf=none (imf29.hostedemail.com: domain of david@redhat.com has no SPF policy when checking 170.10.129.124) smtp.mailfrom=david@redhat.com X-Rspamd-Server: rspam08 X-HE-Tag: 1642086266-23739 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 16:02, Matthew Wilcox wrote: > On Thu, Jan 13, 2022 at 03:46:54PM +0100, David Hildenbrand wrote: >> 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? > > Unfortunately the last digit is missing from your "3.", but I Sorry, == 2. > think you're absolutely right; we need to check swapcount. So > once reuse_swap_page() checks page_count instead of mapcount, we'll > be good? > That's something I've been thinking of. Either get rid of reuse_swap_page() completely or make it obey the same rules everywhere. It's highly inconsistent how we handle COW. -- Thanks, David / dhildenb