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 1A396C433F5 for ; Thu, 20 Jan 2022 20:37:42 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 6625F6B007D; Thu, 20 Jan 2022 15:37:42 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 610EE6B007E; Thu, 20 Jan 2022 15:37:42 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 4B2B26B0080; Thu, 20 Jan 2022 15:37:42 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0183.hostedemail.com [216.40.44.183]) by kanga.kvack.org (Postfix) with ESMTP id 3C7C56B007D for ; Thu, 20 Jan 2022 15:37:42 -0500 (EST) Received: from smtpin16.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay02.hostedemail.com (Postfix) with ESMTP id E37FB95276 for ; Thu, 20 Jan 2022 20:37:41 +0000 (UTC) X-FDA: 79051826322.16.5B5385A Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by imf26.hostedemail.com (Postfix) with ESMTP id 84635140010 for ; Thu, 20 Jan 2022 20:37:40 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1642711060; 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=uGxwqBCvgZoPCkZZ5FkImKlvcovv546VMc/LVt9w2tc=; b=BiZUAVlW9tPLYeNZsIMUjBkAoHmilK2qendKRybJ8nsWdT2G/RvpENuCTA+lK7VFGl6AiU gW+8YlstnEauEz0Qen6j6XzKE8aPAhW19hIcxR6XRG47jcd7qtrnPvU9O7auK+bkB42IXU zi7qqUfnTPn/QJioSnG/afFmQD7brtA= 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-185-NHNZ2VF6MKi5MQAQtfpXTg-1; Thu, 20 Jan 2022 15:37:37 -0500 X-MC-Unique: NHNZ2VF6MKi5MQAQtfpXTg-1 Received: by mail-ed1-f71.google.com with SMTP id c11-20020a056402120b00b0040321cea9d4so6953262edw.23 for ; Thu, 20 Jan 2022 12:37:36 -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:from:to:cc:references:organization:in-reply-to :content-transfer-encoding; bh=uGxwqBCvgZoPCkZZ5FkImKlvcovv546VMc/LVt9w2tc=; b=UQRChcH4iI3fjkBI13yRBUNnTsXDMSlP3jebSzcqcBnIG65sBlNdubFdpf0Cid8nQR v3eIxfFgs2nQNELDouZH/2D+m7/3vSdcrfM89VNKGqQpIWewh2x+F13igr9C+p1Qq+Fa JdEMTrK+QjW/oDPyJt3lvpf1L2ELXtMO785uKmBZFwAe1Mf8RowUc+PYeM4J8ULMKQbl ht38RBibkEbIdETDQ/1CXIJM25gAzZIbfsTUikq7Ouu7CsAip+RJvoGD/xHlxKKGAVe5 H3CUoyw61BCwVk00Qi+1w7f+oZxyu32MDcKI1QZbr+qLqdk5cajRSOMFzlpEZWKG/zc8 tJJA== X-Gm-Message-State: AOAM531LNUZ8jlmGTVRMYQrGL5KmFsUEM3NhkzWh5YmbGSAFOIg53N8w /Ea/mquqxXpo466mNylBZ0XlEXQBMtQDCM80PMGBPScqCpf297/HddxBwb/JUNUDhF7bQ9fduso FYC89+vafL5M= X-Received: by 2002:a05:6402:1104:: with SMTP id u4mr917727edv.24.1642711056023; Thu, 20 Jan 2022 12:37:36 -0800 (PST) X-Google-Smtp-Source: ABdhPJzYE4Lp9kOI/G+mmLmeVOSnWXs3gIZZIBO5XD6ezueSA34Nf0afMXtekWQmU16Ku3t5x3mhlA== X-Received: by 2002:a05:6402:1104:: with SMTP id u4mr917710edv.24.1642711055820; Thu, 20 Jan 2022 12:37:35 -0800 (PST) Received: from ?IPV6:2003:cb:c70e:5800:eeb:dae2:b1c0:f5d1? (p200300cbc70e58000eebdae2b1c0f5d1.dip0.t-ipconnect.de. [2003:cb:c70e:5800:eeb:dae2:b1c0:f5d1]) by smtp.gmail.com with ESMTPSA id v14sm1399715ejy.77.2022.01.20.12.37.34 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Thu, 20 Jan 2022 12:37:34 -0800 (PST) Message-ID: <42a9b72d-093e-c35c-f4b5-b321a666e67d@redhat.com> Date: Thu, 20 Jan 2022 21:37:34 +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 From: David Hildenbrand To: Matthew Wilcox Cc: Nadav Amit , "zhangliang (AG)" , Andrew Morton , Linux-MM , Linux Kernel Mailing List , wangzhigang17@huawei.com, Linus Torvalds 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> 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: 84635140010 X-Stat-Signature: kxknyhbqjr9sxnwa3nck94ishowj4c8w Authentication-Results: imf26.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=BiZUAVlW; dmarc=pass (policy=none) header.from=redhat.com; spf=none (imf26.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: rspam03 X-HE-Tag: 1642711060-647617 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 20.01.22 21:09, David Hildenbrand wrote: > On 20.01.22 21:07, Matthew Wilcox wrote: >> 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(); >> lru_add_drain_all() takes a mutex ... best we can do I guess is drain the local CPU using lru_add_drain(). I'll go play with it and see what breaks :) -- Thanks, David / dhildenb