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 ABB91C433F5 for ; Thu, 20 Jan 2022 20:07:21 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 1077E6B0073; Thu, 20 Jan 2022 15:07:21 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 0B7706B007B; Thu, 20 Jan 2022 15:07:21 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id EC3056B007D; Thu, 20 Jan 2022 15:07:20 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0108.hostedemail.com [216.40.44.108]) by kanga.kvack.org (Postfix) with ESMTP id DE72D6B0073 for ; Thu, 20 Jan 2022 15:07:20 -0500 (EST) Received: from smtpin14.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay03.hostedemail.com (Postfix) with ESMTP id A714B8248076 for ; Thu, 20 Jan 2022 20:07:20 +0000 (UTC) X-FDA: 79051749840.14.D1E8410 Received: from casper.infradead.org (casper.infradead.org [90.155.50.34]) by imf28.hostedemail.com (Postfix) with ESMTP id BD5F8C006A for ; Thu, 20 Jan 2022 20:07:19 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=casper.20170209; h=In-Reply-To:Content-Type:MIME-Version: References:Message-ID:Subject:Cc:To:From:Date:Sender:Reply-To: Content-Transfer-Encoding:Content-ID:Content-Description; bh=B3+5396UaXvFEYhBocmsmHaWNgzX8iVJrkcpnR2FF40=; b=nx0iaBmPPuLI2/9GOonlnPUSEq EjWGaxtTNM6+8VbIeQAvX0LFJiPXBZbrZ1B4JFvGcNdcHTYt5wT0LE/lMp33cwXQrKfIbSj3E+UxZ Wx1HAtlu8CDzAp/1baDqEy+kggiFLdHMOWnD06RsHjYJeNBv+GZ1XynH0yF/Fnbp5yvbMDCPsHzp4 eBbnl6Jf5HYMiPtqiyx/ztodvX2TN6qgtC80jf68vikKwyexUZoXlVV4K/n9ZGFvLTB8YKqIZM74N S0KuXfXftssDpaPGglmGRIwAKUz2baSxvDcU7OCWasjbcacAmFIaq8Ygnm4U1/EnPGDa8uIZI42Le xw7/ST2g==; Received: from willy by casper.infradead.org with local (Exim 4.94.2 #2 (Red Hat Linux)) id 1nAdi9-00Eivm-3a; Thu, 20 Jan 2022 20:07:09 +0000 Date: Thu, 20 Jan 2022 20:07:09 +0000 From: Matthew Wilcox To: David Hildenbrand Cc: Nadav Amit , "zhangliang (AG)" , Andrew Morton , Linux-MM , Linux Kernel Mailing List , wangzhigang17@huawei.com, Linus Torvalds Subject: Re: [PATCH] mm: reuse the unshared swapcache page in do_wp_page Message-ID: References: <9cd7eee2-91fd-ddb8-e47d-e8585e5baa05@redhat.com> <747ff31c-6c9e-df6c-f14d-c43aa1c77b4a@redhat.com> <8931808d-db61-0f06-ceb3-f48a83b1f74c@redhat.com> <6225EAFF-B323-4DC5-AC4C-885B29ED7261@gmail.com> <9071d5a8-ed2d-5cf5-5526-43fe7dd377ec@redhat.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <9071d5a8-ed2d-5cf5-5526-43fe7dd377ec@redhat.com> Authentication-Results: imf28.hostedemail.com; dkim=pass header.d=infradead.org header.s=casper.20170209 header.b=nx0iaBmP; dmarc=none; spf=none (imf28.hostedemail.com: domain of willy@infradead.org has no SPF policy when checking 90.155.50.34) smtp.mailfrom=willy@infradead.org X-Stat-Signature: z1ijx7gse4ucuw7h368rffwthik14mnb X-Rspamd-Queue-Id: BD5F8C006A X-Rspamd-Server: rspam12 X-HE-Tag: 1642709239-454948 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 Thu, Jan 20, 2022 at 08:55:12PM +0100, David Hildenbrand wrote: > >>> David, does any of it regards the lru_cache_add() reference issue that I > >>> mentioned? [1] > +++ b/mm/memory.c > @@ -3291,19 +3291,28 @@ static vm_fault_t do_wp_page(struct vm_fault *vmf) > if (PageAnon(vmf->page)) { > struct page *page = vmf->page; > > - /* PageKsm() doesn't necessarily raise the page refcount */ > - if (PageKsm(page) || page_count(page) != 1) > + /* > + * PageKsm() doesn't necessarily raise the page refcount. > + * > + * These checks are racy as long as we haven't locked the page; > + * they are a pure optimization to avoid trying to lock the page > + * and trying to free the swap cache when there is little hope > + * it will actually result in a refcount of 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 (PageSwapCache(page)) > + try_to_free_swap(page); > + if (PageKsm(page) || page_count(page) != 1) { > unlock_page(page); > goto copy; > } > /* > - * Ok, we've got the only map reference, and the only > - * page count reference, and the page is locked, > - * it's dark out, and we're wearing sunglasses. Hit it. > + * Ok, we've got the only page reference from our mapping > + * and the page is locked, it's dark out, and we're wearing > + * sunglasses. Hit it. > */ > unlock_page(page); > wp_page_reuse(vmf); > > > I added some vmstats that monitor various paths. After one run of > ./forceswap 2 1000000 1 > I'm left with a rough delta (including some noise) of > anon_wp_copy_count 1799 > anon_wp_copy_count_early 1 > anon_wp_copy_lock 983396 > anon_wp_reuse 0 > > The relevant part of your reproducer is > > for (i = 0; i < nops; i++) { > if (madvise((void *)p, PAGE_SIZE * npages, MADV_PAGEOUT)) { > perror("madvise"); > exit(-1); > } > > for (j = 0; j < npages; j++) { > c = p[j * PAGE_SIZE]; > c++; > time -= rdtscp(); > p[j * PAGE_SIZE] = c; > time += rdtscp(); > } > } > > For this specific reproducer at least, the page lock seems to be the thingy that prohibits > reuse if I interpret the numbers correctly. We pass the initial page_count() check. > > Haven't looked into the details, and I would be curious how that performs with actual > workloads, if we can reproduce similar behavior. I don't see how that patch addresses the lru issue. Wouldn't we need something like ... if (!PageLRU(page)) lru_add_drain_all();