From patchwork Thu Aug 29 16:56:07 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Hildenbrand X-Patchwork-Id: 13783457 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 0A9C6C87FC8 for ; Thu, 29 Aug 2024 16:57:35 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 9ADFB6B0096; Thu, 29 Aug 2024 12:57:34 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 95E526B0098; Thu, 29 Aug 2024 12:57:34 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 825CD6B0099; Thu, 29 Aug 2024 12:57:34 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id 6465C6B0096 for ; Thu, 29 Aug 2024 12:57:34 -0400 (EDT) Received: from smtpin12.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id E70D8C0E83 for ; Thu, 29 Aug 2024 16:57:33 +0000 (UTC) X-FDA: 82505889186.12.7B06AB3 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by imf24.hostedemail.com (Postfix) with ESMTP id 36D5018001B for ; Thu, 29 Aug 2024 16:57:32 +0000 (UTC) Authentication-Results: imf24.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b="UCnj8Bt/"; dmarc=pass (policy=none) header.from=redhat.com; spf=pass (imf24.hostedemail.com: domain of david@redhat.com designates 170.10.133.124 as permitted sender) smtp.mailfrom=david@redhat.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1724950586; a=rsa-sha256; cv=none; b=WADUe0BiFHppTIwX3d5IN/qx1Cvugrm7xK1tK9b6HlyHYXtW/RRYy+YEilSr7lymV7S4EB yceIglpTUN1wrK7GKGwmra3JSZQBjVeYuhe+pgNg3zcuFhflorT0Q/BLmkLOURuSdaUM7L v0QjhCOdlv2H6BmBuCiuIhGgkktQi+8= ARC-Authentication-Results: i=1; imf24.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b="UCnj8Bt/"; dmarc=pass (policy=none) header.from=redhat.com; spf=pass (imf24.hostedemail.com: domain of david@redhat.com designates 170.10.133.124 as permitted sender) smtp.mailfrom=david@redhat.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1724950586; 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-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=EpD7Yg2+jtoN65+dYA8OeTl4VXhFFh1pjoJ0uEfzgHA=; b=tJPMeyumebZZS75re3cvncPcuBxvi+1OKTgTlyizLd9DF79I6Z2YqL9+c/CAeapb7hmD1F Yalb5IL1vwPJVMAl4sKZbjvebcyofUXoJr2GcZm9LoDl+VaQnTzBT1ZueKaxNbT0p+JJ69 PpAC4WTdpwz2NqOYH6qoiCDMI2QnODs= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1724950651; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=EpD7Yg2+jtoN65+dYA8OeTl4VXhFFh1pjoJ0uEfzgHA=; b=UCnj8Bt/cL41sar+sV8WwnsSF+yecqYSKH1X45z3ny5FdSJV0U+8DpfNL7f+jr5fnxFw0x X4sFwJvD6yhF0ZtdpEDQ/WxBU5VTz8NUHtLqs+yXLuimoJFCbeqmnpFhsrZOllGW+X54f2 uOfuJ+KH/f0rL0GyCl94F0TWgUpkAuM= Received: from mx-prod-mc-02.mail-002.prod.us-west-2.aws.redhat.com (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-62-X3BqNIGuM3SJk-RFpxTW0Q-1; Thu, 29 Aug 2024 12:57:27 -0400 X-MC-Unique: X3BqNIGuM3SJk-RFpxTW0Q-1 Received: from mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.12]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-02.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id E4CFD18EA8EA; Thu, 29 Aug 2024 16:57:24 +0000 (UTC) Received: from t14s.redhat.com (unknown [10.39.193.245]) by mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id DDA2A1955F21; Thu, 29 Aug 2024 16:57:16 +0000 (UTC) From: David Hildenbrand To: linux-kernel@vger.kernel.org Cc: linux-mm@kvack.org, cgroups@vger.kernel.org, x86@kernel.org, linux-fsdevel@vger.kernel.org, David Hildenbrand , Andrew Morton , "Matthew Wilcox (Oracle)" , Tejun Heo , Zefan Li , Johannes Weiner , =?utf-8?q?Michal_Koutn=C3=BD?= , Jonathan Corbet , Andy Lutomirski , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen Subject: [PATCH v1 04/17] mm: let _folio_nr_pages overlay memcg_data in first tail page Date: Thu, 29 Aug 2024 18:56:07 +0200 Message-ID: <20240829165627.2256514-5-david@redhat.com> In-Reply-To: <20240829165627.2256514-1-david@redhat.com> References: <20240829165627.2256514-1-david@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.0 on 10.30.177.12 X-Rspamd-Queue-Id: 36D5018001B X-Rspam-User: X-Rspamd-Server: rspam05 X-Stat-Signature: ug1x9tn94nrk8e415h1isoefqzwk3td4 X-HE-Tag: 1724950652-715404 X-HE-Meta: U2FsdGVkX1/Y/Ho6G2WoBVoO1sSSncTvmnUiSMpADXDcyi/WZKk2I2rat/ZSNO26DpeLPejXNviBXHp1bjHsJBP2Krs0jxphVNg06Ay52x6piei6dzaF0Qy9spQUSnTtSKKxSNDtZUCcBRCKQxgo8uxDEUX4WqrCrstm27OU/WzirImqGAyl7nfCf1U6oyqOQQt08bqgdD77GyXKw5nClZwG/BSVfDqcqIElqFP6tPjMs+MeeqaEl70OiR16F6MLHTXDdIh7NB9EQP4W6bVoyRP99eDjPqoespSdnQEMogw8rURxxXeJRRv0zaM2EKEwMEx+MecCEDVbAqH30vWC32LkDf9MPPcmIHiEc4QNzMfASiSA3BDrOdDoc566HAK87sBqF42Q/eBgC+UaxXpp//GX1R5iF9adv60t4qOD7txBioOhsJZ3DLGoeDl0WwqX8ku5A+B3vwYjCalcUAAY0mHHm/dkoxyN13521EhK2zI7+pRtA3JZuj94xVn0bQRSp8nyGJKwRrUA35ocXEUQ2M91Pf/GPPDNk1nPRa7LqsqmdlLVlRu1vIDKkxsDASWPhC726zN9I9Mb8/2xYzq5wZzkhR6aN7ozLcnJYOWpr1BaNSHYRLdZfR2QeaPgBZHhB6tVmnnfBBZhaMu868leyzWg1U50m/VNSlmklO94wG/JzXkXJG/zjoFFWWzKknWq3SiAsUE+vCvzf1BVsFVIMLL9tIujIUUuGboPRZIN2D+PnP0AWGJ2stGlmFeIUmAfb5jSMWdE0fNMRIQM927kPv1Ma3kpWShTK7RZlgHaHgZnxNpTZOc1g2BknAc+bgvtjKqOw2YIeGSN5ZEXMFgYmdE3P4nQ9dcCmyHDKRjfyZXo94+WtMnNCDwvqwFTc+08a2A1VjFmIwAtKxSCl76r7SAr+xqAQBtImQg/Ov5UUgo/kCabuTHdYvP6I0TNd0NcKgKuO04XfmVHKiYTPuF 2Y1khH3Y n+kwTlneD6uUiya36ND1JOtZ889Pg2R1uLRB/BNAHLlkN/END8DSz0Fr9L7lK1nTSZR3bF4gcfDjD9ITw3EK6f9DYa8yyCD7vQOYCjEO6m9nbrZ/9vyaH5UGmLKHa+QkUBBDesSIYHcpPmKbZaWOihkEHhOSeSflvuHeM0R9jKsy4zL7Sc/3Eb/4F+3WvRqpCP/RmzqN2T/6waP3HPBQviqrYd3uip7URvhMR1aqfvgYg9dUhCO47vDpYBSJ6LqUfcCBkNkK4mWYoVVOecOzWgpZK1VeLXg6IFLpxbuoErEdmwJA/mAtELhdl2J2tU2M4Vbuyu0tSCUr0mLCceFLmKHmsKvyIR0T+jBTlGWcrt5tJCAKUtd0kpGDbqA== 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: List-Subscribe: List-Unsubscribe: Let's free up some more of the "unconditionally available on 64BIT" space in order-1 folios by letting _folio_nr_pages overlay memcg_data in the first tail page (second folio page). Consequently, we have the optimization now whenever we have CONFIG_MEMCG, independent of 64BIT. We have to make sure that page->memcg on tail pages does not return "surprises". page_memcg_check() already properly refuses PageTail(). Let's do that earlier in print_page_owner_memcg() to avoid printing wrong "Slab cache page" information. No other code should touch that field on tail pages of compound pages. Reset the "_nr_pages" to 0 when splitting folios, or when freeing them back to the buddy (to avoid false page->memcg_data "bad page" reports). Note that in __split_huge_page(), folio_nr_pages() would stop working already as soon as we start messing with the subpages. Most kernel configs should have at least CONFIG_MEMCG enabled, even if disabled at runtime. 64byte "struct memmap" is what we usually have on 64BIT. While at it, rename "_folio_nr_pages" to "_nr_pages". Signed-off-by: David Hildenbrand Reviewed-by: Kirill A. Shutemov --- include/linux/mm.h | 4 ++-- include/linux/mm_types.h | 30 ++++++++++++++++++++++-------- mm/huge_memory.c | 8 ++++++++ mm/internal.h | 4 ++-- mm/page_alloc.c | 6 +++++- mm/page_owner.c | 2 +- 6 files changed, 40 insertions(+), 14 deletions(-) diff --git a/include/linux/mm.h b/include/linux/mm.h index fa8b6ce54235c..98411e53da916 100644 --- a/include/linux/mm.h +++ b/include/linux/mm.h @@ -1078,8 +1078,8 @@ static inline unsigned int folio_large_order(const struct folio *folio) static inline long folio_large_nr_pages(const struct folio *folio) { -#ifdef CONFIG_64BIT - return folio->_folio_nr_pages; +#ifdef NR_PAGES_IN_LARGE_FOLIO + return folio->_nr_pages; #else return 1L << folio_large_order(folio); #endif diff --git a/include/linux/mm_types.h b/include/linux/mm_types.h index 6e3bdf8e38bca..480548552ea54 100644 --- a/include/linux/mm_types.h +++ b/include/linux/mm_types.h @@ -283,6 +283,11 @@ typedef struct { unsigned long val; } swp_entry_t; +#if defined(CONFIG_MEMCG) || defined(CONFIG_SLAB_OBJ_EXT) +/* We have some extra room after the refcount in tail pages. */ +#define NR_PAGES_IN_LARGE_FOLIO +#endif + /** * struct folio - Represents a contiguous set of bytes. * @flags: Identical to the page flags. @@ -305,7 +310,7 @@ typedef struct { * @_large_mapcount: Do not use directly, call folio_mapcount(). * @_nr_pages_mapped: Do not use outside of rmap and debug code. * @_pincount: Do not use directly, call folio_maybe_dma_pinned(). - * @_folio_nr_pages: Do not use directly, call folio_nr_pages(). + * @_nr_pages: Do not use directly, call folio_nr_pages(). * @_hugetlb_subpool: Do not use directly, use accessor in hugetlb.h. * @_hugetlb_cgroup: Do not use directly, use accessor in hugetlb_cgroup.h. * @_hugetlb_cgroup_rsvd: Do not use directly, use accessor in hugetlb_cgroup.h. @@ -366,13 +371,20 @@ struct folio { unsigned long _flags_1; unsigned long _head_1; /* public: */ - atomic_t _large_mapcount; - atomic_t _entire_mapcount; - atomic_t _nr_pages_mapped; - atomic_t _pincount; -#ifdef CONFIG_64BIT - unsigned int _folio_nr_pages; -#endif + union { + struct { + atomic_t _large_mapcount; + atomic_t _entire_mapcount; + atomic_t _nr_pages_mapped; + atomic_t _pincount; + }; + unsigned long _usable_1[4]; + }; + atomic_t _mapcount_1; + atomic_t _refcount_1; +#ifdef NR_PAGES_IN_LARGE_FOLIO + unsigned int _nr_pages; +#endif /* NR_PAGES_IN_LARGE_FOLIO */ /* private: the union with struct page is transitional */ }; struct page __page_1; @@ -424,6 +436,8 @@ FOLIO_MATCH(_last_cpupid, _last_cpupid); offsetof(struct page, pg) + sizeof(struct page)) FOLIO_MATCH(flags, _flags_1); FOLIO_MATCH(compound_head, _head_1); +FOLIO_MATCH(_mapcount, _mapcount_1); +FOLIO_MATCH(_refcount, _refcount_1); #undef FOLIO_MATCH #define FOLIO_MATCH(pg, fl) \ static_assert(offsetof(struct folio, fl) == \ diff --git a/mm/huge_memory.c b/mm/huge_memory.c index 15418ffdd3774..28d12573fcf8c 100644 --- a/mm/huge_memory.c +++ b/mm/huge_memory.c @@ -3171,6 +3171,14 @@ static void __split_huge_page(struct page *page, struct list_head *list, int order = folio_order(folio); unsigned int nr = 1 << order; + /* + * Reset any memcg data overlay in the tail pages. folio_nr_pages() + * is unreliable after this point. + */ +#ifdef NR_PAGES_IN_LARGE_FOLIO + folio->_nr_pages = 0; +#endif + /* complete memcg works before add pages to LRU */ split_page_memcg(head, order, new_order); diff --git a/mm/internal.h b/mm/internal.h index 97d6b94429ebd..f627fd2200464 100644 --- a/mm/internal.h +++ b/mm/internal.h @@ -625,8 +625,8 @@ static inline void folio_set_order(struct folio *folio, unsigned int order) return; folio->_flags_1 = (folio->_flags_1 & ~0xffUL) | order; -#ifdef CONFIG_64BIT - folio->_folio_nr_pages = 1U << order; +#ifdef NR_PAGES_IN_LARGE_FOLIO + folio->_nr_pages = 1U << order; #endif } diff --git a/mm/page_alloc.c b/mm/page_alloc.c index c2ffccf9d2131..e276cbaf97054 100644 --- a/mm/page_alloc.c +++ b/mm/page_alloc.c @@ -1077,8 +1077,12 @@ __always_inline bool free_pages_prepare(struct page *page, if (unlikely(order)) { int i; - if (compound) + if (compound) { page[1].flags &= ~PAGE_FLAGS_SECOND; +#ifdef NR_PAGES_IN_LARGE_FOLIO + ((struct folio *)page)->_nr_pages = 0; +#endif + } for (i = 1; i < (1 << order); i++) { if (compound) bad += free_tail_page_prepare(page, page + i); diff --git a/mm/page_owner.c b/mm/page_owner.c index 2d6360eaccbb6..a409e2561a8fd 100644 --- a/mm/page_owner.c +++ b/mm/page_owner.c @@ -507,7 +507,7 @@ static inline int print_page_owner_memcg(char *kbuf, size_t count, int ret, rcu_read_lock(); memcg_data = READ_ONCE(page->memcg_data); - if (!memcg_data) + if (!memcg_data || PageTail(page)) goto out_unlock; if (memcg_data & MEMCG_DATA_OBJEXTS)