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 629A4C433F5 for ; Thu, 20 Jan 2022 20:49:24 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id DCE4B6B007E; Thu, 20 Jan 2022 15:49:23 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id D7DE66B0080; Thu, 20 Jan 2022 15:49:23 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id C1E656B0081; Thu, 20 Jan 2022 15:49:23 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0137.hostedemail.com [216.40.44.137]) by kanga.kvack.org (Postfix) with ESMTP id B23256B007E for ; Thu, 20 Jan 2022 15:49:23 -0500 (EST) Received: from smtpin13.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay01.hostedemail.com (Postfix) with ESMTP id 77F7E181A349C for ; Thu, 20 Jan 2022 20:49:23 +0000 (UTC) X-FDA: 79051855806.13.DE78129 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by imf28.hostedemail.com (Postfix) with ESMTP id E6CD8C0009 for ; Thu, 20 Jan 2022 20:49:22 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1642711762; 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=oZAUYIHdJyn2g1T2J1YhyWMD7A9ST4VWWRZYZKMCJ4k=; b=DW+qtBp4xcfZSay97Vq4x7riD80OFI0vjBnHBxvBsrmNeC4waFm/99CNITYE47Ccu7f3kp rESc/BlFUXvZNJuDDsggr0IXrP+5T0h3EiYTm5O2Wn2OYKlPlt3stsKz1ecBsHtqQ0EiHf ajZx7HUULUUe3YhuLU919HLGj4YBnb4= Received: from mail-wm1-f71.google.com (mail-wm1-f71.google.com [209.85.128.71]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-372-oRmKKuJLPTSvjZMR1YTJxQ-1; Thu, 20 Jan 2022 15:49:21 -0500 X-MC-Unique: oRmKKuJLPTSvjZMR1YTJxQ-1 Received: by mail-wm1-f71.google.com with SMTP id s190-20020a1ca9c7000000b00347c6c39d9aso4812411wme.5 for ; Thu, 20 Jan 2022 12:49:21 -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=oZAUYIHdJyn2g1T2J1YhyWMD7A9ST4VWWRZYZKMCJ4k=; b=lIT5U9STPDt/xsbYkKFm9emQz8L/PoWq0ed2g7/fbxBpkl9yBVUAufSaehFtSUdTh6 3v4vWPzYWGyqlBJsQIjcgmt/WUI1lCL6z2CnHEIdjRFeEmGdxo0GDbRGJ3b8FKEhmye5 P02d1q1xDiWvMzPnGT5si/DbSvzMqTapwC4FRNUSZDxHmC/GOQvnPCkyFIy2TjMQJGb/ OspHpoTZhhUeMv6+kYgD+GZqorDXl4kU6SVTLh6IVMHh7Fnf7qkoniUeBphDvKAOISoN pg5aGUU2sHmdl1fpCzcqKtUbS2bR9p++XaumxwKX0aqgmOcDfsKgDB+hcfb9/YUUoFa4 wSrA== X-Gm-Message-State: AOAM531fYknmH40I9NiAVtaiwS0IKv4mbSfFVxNJthY7j8WdD8jwfsHC ZUKytKw/ELqYt/A8eBJPHJ6yrSaizkdFK+Uxlj01sdXP0i4ooVk71cv3xUzZyXGOtJbeGYmAjTS tooXIxudVECo= X-Received: by 2002:adf:cc8c:: with SMTP id p12mr707248wrj.677.1642711755256; Thu, 20 Jan 2022 12:49:15 -0800 (PST) X-Google-Smtp-Source: ABdhPJzZ8Wc0JJI1Fk/618MLE3Blo9++lHl/qGu3MZcPz8Nv6Qjvza0XRQqKDdnFvxuX4AWwRUetZw== X-Received: by 2002:adf:cc8c:: with SMTP id p12mr707233wrj.677.1642711755022; Thu, 20 Jan 2022 12:49:15 -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 bd20sm4111797wmb.38.2022.01.20.12.49.14 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Thu, 20 Jan 2022 12:49:14 -0800 (PST) Message-ID: <7a18f74f-9dc2-f23d-4f1c-c7a9217f8317@redhat.com> Date: Thu, 20 Jan 2022 21:49:13 +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: Nadav Amit Cc: Matthew Wilcox , "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> <42a9b72d-093e-c35c-f4b5-b321a666e67d@redhat.com> <288FB900-A688-4EDB-95C6-E63B6E0A15D1@gmail.com> From: David Hildenbrand Organization: Red Hat In-Reply-To: <288FB900-A688-4EDB-95C6-E63B6E0A15D1@gmail.com> X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Language: en-US Content-Type: text/plain; charset=UTF-8 X-Rspamd-Server: rspam05 X-Rspamd-Queue-Id: E6CD8C0009 X-Stat-Signature: r3hb1onjhpt3ggegwjd4wtaufeyzxin5 Authentication-Results: imf28.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=DW+qtBp4; spf=none (imf28.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: 1642711762-68354 Content-Transfer-Encoding: quoted-printable 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:46, Nadav Amit wrote: >=20 >=20 >> On Jan 20, 2022, at 12:37 PM, David Hildenbrand wro= te: >> >> 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 issu= e that I >>>>>>>> mentioned? [1] >>>> >>>>> +++ b/mm/memory.c >>>>> @@ -3291,19 +3291,28 @@ static vm_fault_t do_wp_page(struct vm_faul= t *vmf) >>>>> if (PageAnon(vmf->page)) { >>>>> struct page *page =3D vmf->page; >>>>> >>>>> - /* PageKsm() doesn't necessarily raise the page ref= count */ >>>>> - if (PageKsm(page) || page_count(page) !=3D 1) >>>>> + /* >>>>> + * PageKsm() doesn't necessarily raise the page ref= count. >>>>> + * >>>>> + * These checks are racy as long as we haven't lock= ed 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 + PageSwa= pCache(page)) >>>>> goto copy; >>>>> if (!trylock_page(page)) >>>>> goto copy; >>>>> - if (PageKsm(page) || page_mapcount(page) !=3D 1 || = page_count(page) !=3D 1) { >>>>> + if (PageSwapCache(page)) >>>>> + try_to_free_swap(page); >>>>> + if (PageKsm(page) || page_count(page) !=3D 1) { >>>>> unlock_page(page); >>>>> goto copy; >>>>> } >>>>> /* >>>>> - * Ok, we've got the only map reference, and the on= ly >>>>> - * 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 m= apping >>>>> + * 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 =3D 0; i < nops; i++) { >>>>> if (madvise((void *)p, PAGE_SIZE * npages, MADV_PAGEOUT)) { >>>>> perror("madvise"); >>>>> exit(-1); >>>>> } >>>>> >>>>> for (j =3D 0; j < npages; j++) { >>>>> c =3D p[j * PAGE_SIZE]; >>>>> c++; >>>>> time -=3D rdtscp(); >>>>> p[j * PAGE_SIZE] =3D c; >>>>> time +=3D rdtscp(); >>>>> } >>>>> } >>>>> >>>>> For this specific reproducer at least, the page lock seems to be th= e thingy that prohibits >>>>> reuse if I interpret the numbers correctly. We pass the initial pag= e_count() check. >>>>> >>>>> Haven't looked into the details, and I would be curious how that pe= rforms with actual >>>>> workloads, if we can reproduce similar behavior. >>>> >>>> I don't see how that patch addresses the lru issue. Wouldn't we nee= d >>>> 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 :) >> >=20 > I did hack something similar and it solved the problem, but I felt it i= s > a hack. If the thread is scheduled on another core, or if the write fau= lt > is triggered by another thread it wouldn=E2=80=99t work. Yes, it will not match easily. One question would be how often it would help in practice and if it would be worth the price. >=20 > If you look for a real-world workload that behaves similarly, you can t= ry > memcached with memory pressure and low latency device (I used=20 > pmem-emulated). This is the workload in which I encountered the issue > first. Yes, I agree. So PageAnonExclusive is our best bet ... hopefully. --=20 Thanks, David / dhildenb