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 5CEA7C001B3 for ; Thu, 22 Jun 2023 02:36:34 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 7AFE08D0002; Wed, 21 Jun 2023 22:36:33 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 75E368D0001; Wed, 21 Jun 2023 22:36:33 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 5FEF28D0002; Wed, 21 Jun 2023 22:36:33 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id 520298D0001 for ; Wed, 21 Jun 2023 22:36:33 -0400 (EDT) Received: from smtpin14.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id 084B01A0A9C for ; Thu, 22 Jun 2023 02:36:33 +0000 (UTC) X-FDA: 80928820266.14.7B58616 Received: from mail-yw1-f176.google.com (mail-yw1-f176.google.com [209.85.128.176]) by imf10.hostedemail.com (Postfix) with ESMTP id 39DCFC0002 for ; Thu, 22 Jun 2023 02:36:30 +0000 (UTC) Authentication-Results: imf10.hostedemail.com; dkim=pass header.d=google.com header.s=20221208 header.b=O3tP+byB; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf10.hostedemail.com: domain of hughd@google.com designates 209.85.128.176 as permitted sender) smtp.mailfrom=hughd@google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1687401390; h=from:from:sender: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: in-reply-to:in-reply-to:references:references:dkim-signature; bh=v95kJuIKZOvwX2VyzbcG9p4WcOSZqpPbckNEDTbMqbw=; b=jpnIUhMnIjL/ZDNI+EypIxiNaeyinK1tufCKz9/9S/SmRZyXgB9m9SDSLocdemioexuWBM oV6l6Aed9i43UB5suh28+QPXzhY7uppQpcCbAMng6KDFDmgNCrQolFlYCw6ECBy4vx0OKd zrbcFSn8OCVLkuClU+TPpyr4pkUygOM= ARC-Authentication-Results: i=1; imf10.hostedemail.com; dkim=pass header.d=google.com header.s=20221208 header.b=O3tP+byB; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf10.hostedemail.com: domain of hughd@google.com designates 209.85.128.176 as permitted sender) smtp.mailfrom=hughd@google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1687401390; a=rsa-sha256; cv=none; b=sri4aAqQcI1zxurCAhfGykcV6smFS22m7CsPslcAWLSpI0oH8GZ+fpM+B0g/1l77XTxhbX QIYZuixETnvXLfCHF7/uZBnlbc46c0h88ezZO/GmXjeNtoS55qpGTNN1IUDVgLlyQGUneG rtbqtAKgY/q9Tatf2o1/b3ZhDc1XKnU= Received: by mail-yw1-f176.google.com with SMTP id 00721157ae682-57012b2973eso74553607b3.2 for ; Wed, 21 Jun 2023 19:36:29 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1687401389; x=1689993389; h=mime-version:references:message-id:in-reply-to:subject:cc:to:from :date:from:to:cc:subject:date:message-id:reply-to; bh=v95kJuIKZOvwX2VyzbcG9p4WcOSZqpPbckNEDTbMqbw=; b=O3tP+byBHyqrVuzTTEu8WRLDc/8A9uAf+TdWk4s+4iRKfQ5KOSa7bvLkBzTYKr0W8M j3sRCXur+WjZ9BeHD+FncbDp1prKZ/qXqo9vRO5qA8AQSnrPS9w22qXk+xnyoij9+QY4 Acm9ERBt0JMUlTv5p8Jp0yNYMVl5d341AvRtjPFDRxez8WAwHgXzS6SBOEkhSkDpkgzA FicxTEqozjAq1pC+B+ZdlXfR0aFh6Inz+lQvgqOR+F9yGjspH8iPGFbJTwEd3pnnSvO5 lsJm1UV477OB7Dih1OhkfIqmAM0XwW9HFmiv+LGL2kzkOO0DnreoAdIjNdFX9CO4XaoS SsqQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1687401389; x=1689993389; h=mime-version:references:message-id:in-reply-to:subject:cc:to:from :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=v95kJuIKZOvwX2VyzbcG9p4WcOSZqpPbckNEDTbMqbw=; b=itl7n3pJYhJWU9vg3INhJDwdwWVjsWQzGNQGTampeinrGgRIuD1jnwLv0Xb57fPQds /lp7FsoMnJI/ytCJrQd64Hs/15Pjk/1Wzo7trN2rj2zNAMo92dTCjTtMK/CWlFPcegOW 0EJZb0BVC9iVIiPGHXRTF6GCaWURUQPpGVd01108ZzLDFy2AtP3L+DgSFqngBiDaJaCq jPVbPv0SEXMQBSM6oSfa/Q1GLRpYxxp3XYV/suDrV1ntBa9stLjKAL1M+l5EXC4HotwM TnE6J7Lx4PQ0Wv6Zlod2yUc/J0PostKJ/OkU+i2wGI5oz8hlwCfNKUZrcrlz+Q7exRTp 41nA== X-Gm-Message-State: AC+VfDxORJJB73cOzjOqVmC9qSp5MmZuQwJhl9DLNH6uMvk4fD/J2cd5 pbQxeIKdLPMr7fI9BrvwXXy/xA== X-Google-Smtp-Source: ACHHUZ7nNc0KOSzPxWDeH97+s3XtTQiSXAf5QSy+8GGNP+KxVnc2GWHN4WSyWChzR7EoY+E3ZvtVZQ== X-Received: by 2002:a0d:e6d3:0:b0:56d:ffa:f3b0 with SMTP id p202-20020a0de6d3000000b0056d0ffaf3b0mr14905560ywe.52.1687401389086; Wed, 21 Jun 2023 19:36:29 -0700 (PDT) Received: from ripple.attlocal.net (172-10-233-147.lightspeed.sntcca.sbcglobal.net. [172.10.233.147]) by smtp.gmail.com with ESMTPSA id e65-20020a0dc244000000b0056cffe97a11sm1564690ywd.13.2023.06.21.19.36.23 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 21 Jun 2023 19:36:27 -0700 (PDT) Date: Wed, 21 Jun 2023 19:36:11 -0700 (PDT) From: Hugh Dickins X-X-Sender: hugh@ripple.attlocal.net To: Jason Gunthorpe cc: Hugh Dickins , Andrew Morton , Gerald Schaefer , Vasily Gorbik , Mike Kravetz , Mike Rapoport , "Kirill A. Shutemov" , Matthew Wilcox , David Hildenbrand , Suren Baghdasaryan , Qi Zheng , Yang Shi , Mel Gorman , Peter Xu , Peter Zijlstra , Will Deacon , Yu Zhao , Alistair Popple , Ralph Campbell , Ira Weiny , Steven Price , SeongJae Park , Lorenzo Stoakes , Huang Ying , Naoya Horiguchi , Christophe Leroy , Zack Rusin , Axel Rasmussen , Anshuman Khandual , Pasha Tatashin , Miaohe Lin , Minchan Kim , Christoph Hellwig , Song Liu , Thomas Hellstrom , Russell King , "David Sc. Miller" , Michael Ellerman , "Aneesh Kumar K.V" , Heiko Carstens , Christian Borntraeger , Claudio Imbrenda , Alexander Gordeev , Jann Horn , Vishal Moola , Vlastimil Babka , linux-arm-kernel@lists.infradead.org, sparclinux@vger.kernel.org, linuxppc-dev@lists.ozlabs.org, linux-s390@vger.kernel.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org Subject: Re: [PATCH v2 05/12] powerpc: add pte_free_defer() for pgtables sharing page In-Reply-To: Message-ID: References: <54cb04f-3762-987f-8294-91dafd8ebfb0@google.com> <5cd9f442-61da-4c3d-eca-b7f44d22aa5f@google.com> <2ad8b6cf-692a-ff89-ecc-586c20c5e07f@google.com> MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII X-Rspamd-Server: rspam09 X-Rspamd-Queue-Id: 39DCFC0002 X-Stat-Signature: k7k9pmpiaebeaaa5cerea6c8r73n8se1 X-Rspam-User: X-HE-Tag: 1687401390-213903 X-HE-Meta: U2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AzIZ+txFkp52Hbb75WD2ClPwf22LeGfY1GH073r6DWjYFO4b7JyVlUtuXjiMRmI4jw3HbwLvp6gKb3i0X9Q7A/HVKYUw/1a9OlB+e06osQzfWWj60HX36vOoOfYYi27KqAufDCESDO2ZXZlq8UKzZrJKeDMN+nRrVUrlEy2DjmubB0KZeUQzhqU6tFbYAn+6/Zz4vhPCsLauFgXJDOBetEnVfsEzan0lUL+mqZ7odu/25grvIDZid0bjm1YGHgJdh2p0BN55QcIJWyct8SoJuCxT+jbRhybh/WX60lVQLY7qqsD0ZA8QB1p7/7gXGxIA926rdjenZF0M7J4rgq2wqi9Q+38ThMDKRPa6eK9ND6yy49A9FVd/NV0PowzQW2walyLsFsvJ7YQuDbA5Qsh0hyrfIbDXNHqzAfDJvgCXateniQvQuxu8E41VEVG7cOca167MBP6nLpOM5vRa6IAj5c4PgTth+85cIIULLldX+19XV56F1C2p1XD4HduzvL12KGZibpVBkE3TDQ8Qu9ClePeRtw37Tvbp3w== 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 Tue, 20 Jun 2023, Jason Gunthorpe wrote: > On Tue, Jun 20, 2023 at 12:54:25PM -0700, Hugh Dickins wrote: > > On Tue, 20 Jun 2023, Jason Gunthorpe wrote: > > > On Tue, Jun 20, 2023 at 12:47:54AM -0700, Hugh Dickins wrote: > > > > Add powerpc-specific pte_free_defer(), to call pte_free() via call_rcu(). > > > > pte_free_defer() will be called inside khugepaged's retract_page_tables() > > > > loop, where allocating extra memory cannot be relied upon. This precedes > > > > the generic version to avoid build breakage from incompatible pgtable_t. > > > > > > > > This is awkward because the struct page contains only one rcu_head, but > > > > that page may be shared between PTE_FRAG_NR pagetables, each wanting to > > > > use the rcu_head at the same time: account concurrent deferrals with a > > > > heightened refcount, only the first making use of the rcu_head, but > > > > re-deferring if more deferrals arrived during its grace period. > > > > > > You didn't answer my question why we can't just move the rcu to the > > > actual free page? > > > > I thought that I had answered it, perhaps not to your satisfaction: > > > > https://lore.kernel.org/linux-mm/9130acb-193-6fdd-f8df-75766e663978@google.com/ > > > > My conclusion then was: > > Not very good reasons: good enough, or can you supply a better patch? > > Oh, I guess I didn't read that email as answering the question.. > > I was saying to make pte_fragment_free() unconditionally do the > RCU. It is the only thing that uses the page->rcu_head, and it means > PPC would double RCU the final free on the TLB path, but that is > probably OK for now. This means pte_free_defer() won't do anything > special on PPC as PPC will always RCU free these things, this address > the defer concern too, I think. Overall it is easier to reason about. > > I looked at fixing the TLB stuff to avoid the double rcu but quickly > got scared that ppc was using a kmem_cache to allocate other page > table sizes so there is not a reliable struct page to get a rcu_head > from. This looks like the main challenge for ppc... We'd have to teach > the tlb code to not do its own RCU stuff for table levels that the > arch is already RCU freeing - and that won't get us to full RCU > freeing on PPC. Sorry for being so dense all along: yes, your way is unquestionably much better than mine. I guess I must have been obsessive about keeping pte_free_defer()+pte_free_now() "on the outside", as they were on x86, and never perceived how much easier it is with a small tweak inside pte_fragment_free(); and never reconsidered it since. But I'm not so keen on the double-RCU, extending this call_rcu() to all the normal cases, while still leaving the TLB batching in place: here is the replacement patch I'd prefer us to go forward with now. Many thanks! [PATCH v3 05/12] powerpc: add pte_free_defer() for pgtables sharing page Add powerpc-specific pte_free_defer(), to free table page via call_rcu(). pte_free_defer() will be called inside khugepaged's retract_page_tables() loop, where allocating extra memory cannot be relied upon. This precedes the generic version to avoid build breakage from incompatible pgtable_t. This is awkward because the struct page contains only one rcu_head, but that page may be shared between PTE_FRAG_NR pagetables, each wanting to use the rcu_head at the same time. But powerpc never reuses a fragment once it has been freed: so mark the page Active in pte_free_defer(), before calling pte_fragment_free() directly; and there call_rcu() to pte_free_now() when last fragment is freed and the page is PageActive. Suggested-by: Jason Gunthorpe Signed-off-by: Hugh Dickins --- arch/powerpc/include/asm/pgalloc.h | 4 ++++ arch/powerpc/mm/pgtable-frag.c | 29 ++++++++++++++++++++++++++--- 2 files changed, 30 insertions(+), 3 deletions(-) diff --git a/arch/powerpc/include/asm/pgalloc.h b/arch/powerpc/include/asm/pgalloc.h index 3360cad78ace..3a971e2a8c73 100644 --- a/arch/powerpc/include/asm/pgalloc.h +++ b/arch/powerpc/include/asm/pgalloc.h @@ -45,6 +45,10 @@ static inline void pte_free(struct mm_struct *mm, pgtable_t ptepage) pte_fragment_free((unsigned long *)ptepage, 0); } +/* arch use pte_free_defer() implementation in arch/powerpc/mm/pgtable-frag.c */ +#define pte_free_defer pte_free_defer +void pte_free_defer(struct mm_struct *mm, pgtable_t pgtable); + /* * Functions that deal with pagetables that could be at any level of * the table need to be passed an "index_size" so they know how to diff --git a/arch/powerpc/mm/pgtable-frag.c b/arch/powerpc/mm/pgtable-frag.c index 20652daa1d7e..0c6b68130025 100644 --- a/arch/powerpc/mm/pgtable-frag.c +++ b/arch/powerpc/mm/pgtable-frag.c @@ -106,6 +106,15 @@ pte_t *pte_fragment_alloc(struct mm_struct *mm, int kernel) return __alloc_for_ptecache(mm, kernel); } +static void pte_free_now(struct rcu_head *head) +{ + struct page *page; + + page = container_of(head, struct page, rcu_head); + pgtable_pte_page_dtor(page); + __free_page(page); +} + void pte_fragment_free(unsigned long *table, int kernel) { struct page *page = virt_to_page(table); @@ -115,8 +124,22 @@ void pte_fragment_free(unsigned long *table, int kernel) BUG_ON(atomic_read(&page->pt_frag_refcount) <= 0); if (atomic_dec_and_test(&page->pt_frag_refcount)) { - if (!kernel) - pgtable_pte_page_dtor(page); - __free_page(page); + if (kernel) + __free_page(page); + else if (TestClearPageActive(page)) + call_rcu(&page->rcu_head, pte_free_now); + else + pte_free_now(&page->rcu_head); } } + +#ifdef CONFIG_TRANSPARENT_HUGEPAGE +void pte_free_defer(struct mm_struct *mm, pgtable_t pgtable) +{ + struct page *page; + + page = virt_to_page(pgtable); + SetPageActive(page); + pte_fragment_free((unsigned long *)pgtable, 0); +} +#endif /* CONFIG_TRANSPARENT_HUGEPAGE */ -- 2.35.3