From patchwork Tue Sep 3 21:36:48 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yu Zhao X-Patchwork-Id: 13789329 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 D8680CD3440 for ; Tue, 3 Sep 2024 21:37:02 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 0CC718D016E; Tue, 3 Sep 2024 17:37:01 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 07B0C8D01E4; Tue, 3 Sep 2024 17:37:01 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id E0C238D016E; Tue, 3 Sep 2024 17:37:00 -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 B48168D01E4 for ; Tue, 3 Sep 2024 17:37:00 -0400 (EDT) Received: from smtpin29.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id 5E6CB80955 for ; Tue, 3 Sep 2024 21:37:00 +0000 (UTC) X-FDA: 82524737400.29.D4711F3 Received: from mail-yw1-f201.google.com (mail-yw1-f201.google.com [209.85.128.201]) by imf23.hostedemail.com (Postfix) with ESMTP id 8326D14000B for ; Tue, 3 Sep 2024 21:36:58 +0000 (UTC) Authentication-Results: imf23.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=b7C+dYRz; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf23.hostedemail.com: domain of 3eYHXZgYKCBgMIN5yC4CC492.0CA96BIL-AA8Jy08.CF4@flex--yuzhao.bounces.google.com designates 209.85.128.201 as permitted sender) smtp.mailfrom=3eYHXZgYKCBgMIN5yC4CC492.0CA96BIL-AA8Jy08.CF4@flex--yuzhao.bounces.google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1725399348; a=rsa-sha256; cv=none; b=A0c3lsrzuNTbbGMr4i6cFqu0+LoSNl+mOnkNLKJu1/yYh7j/noKPNmFIIFrMR8zNmJv4Cs HGRhtdSb6VlQzdcS/aayKg3SCh71AnBeWAlC23z33K8EhRnbwF/RqOb5aXNcxR5XGQwddK fZLhgmyKpnOznTJbG1WX9cmUfLRl5bA= ARC-Authentication-Results: i=1; imf23.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=b7C+dYRz; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf23.hostedemail.com: domain of 3eYHXZgYKCBgMIN5yC4CC492.0CA96BIL-AA8Jy08.CF4@flex--yuzhao.bounces.google.com designates 209.85.128.201 as permitted sender) smtp.mailfrom=3eYHXZgYKCBgMIN5yC4CC492.0CA96BIL-AA8Jy08.CF4@flex--yuzhao.bounces.google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1725399348; 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=Z/7NuEBRfCNoI3kJSS18ZDURR7hz7WK5gt1vcDxkEbA=; b=rdzL7g3HA/+xJW6JWDYnIQRrA24pwVVKAnFR174RoNQDoGUHxRIGcPT5V7SZ2nRV95uRGa X5uM0JFQJrdESI6DdIsN4EZN/QeVxaFZmZM3JteDxCOmIDDXFpkvu5lm6jp30RCaWVaNI9 K69LrzNCUyIMafy7/lQrizddMjjonfg= Received: by mail-yw1-f201.google.com with SMTP id 00721157ae682-6d54ab222fcso2929967b3.1 for ; Tue, 03 Sep 2024 14:36:58 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1725399417; x=1726004217; darn=kvack.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=Z/7NuEBRfCNoI3kJSS18ZDURR7hz7WK5gt1vcDxkEbA=; b=b7C+dYRzNJYHIGrGFlipjry5g0CSBWgeo1hvUTTilmpVgyI1sceMW4ilaBbEz6mYEU /PurP7aHN4IF6/xw1J/K13jYU12SHUn8y+Ki8nnQIt0mCKmvEs7dTOcK7HlO0D2CTav9 yIVlycA0Ty07AXKppsoRrmEecRlJwf1sVhDH8H/XuXrBbzCf14PUaaQ01FROgZQVhQgE c8pNVL7v4owY+veWXKAVYjrneuSgtrEmGIhXi7635/vmoZeoRyCFBAYrQWrCbFEYHJk/ yOjWhdEVMLtCRr1fnvu+DnIkOQio8IeLCX+0i8Td/GRgyK15HlrOc698J0KpFkHBDBL+ E5Gw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1725399417; x=1726004217; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=Z/7NuEBRfCNoI3kJSS18ZDURR7hz7WK5gt1vcDxkEbA=; b=Cmmg0XvfnrauxheZwatwK++Ao3GM9k3cr2Rlpy61TS2YaOcjZkqZGloJrU5++T+VMt 3u5gsdIZmtEoecBI9uULndGnfv4M5174ijLXWQs7BtJErC8g+x0kdH3v2Md2VG3+Uudc PFmaMi3n9MFK4EocNiZ5it3ZHNtCvVP+b6TXKvu6YdX6FkSjTG779s7+HeoYP/FRM0yP wnTkQVRFQrqBrf8tI1FAvAbfRKK5Wcn24/ZTtJuvat6p1DTZ/WDBRWRu4cVm+tAh6MLa v+muI83K5DRZam9VK2svBZMStVhJ8FxmXLv+Vy1MBvFBb7/D0RooY/coCU67PRvm3jf2 uFyA== X-Forwarded-Encrypted: i=1; AJvYcCWyWSTPLaF6NyAZc7WpaOgtvQgF5bgArLLIemmyfnKhNAtY8BIUPXh/YxigwaHIxLT1OKUmqJNRBQ==@kvack.org X-Gm-Message-State: AOJu0YzuszWz09SLWVgji1+tCkMAexo4d9QY5VdB9LkcLEP3FJuRVix3 iEchnA2H0xS8B0/DfV/lqdBbu2ObVpQ85cRAUDNdpH9K8SOK5qz9O+6IAdi9hSn5Zcb/Gb8IqPY iHQ== X-Google-Smtp-Source: AGHT+IGGSeNgfu3VxAiHe7OfPaoKscE8qEOTQ+EoyIisWRyArXESs2P4m407AuhgVZ//yoQWwR8kUho664U= X-Received: from yuzhao2.bld.corp.google.com ([2a00:79e0:2e28:6:bb58:3d85:3f9c:c6c6]) (user=yuzhao job=sendgmr) by 2002:a25:8592:0:b0:e0e:c9bc:3206 with SMTP id 3f1490d57ef6-e1d0e78ad12mr315276.5.1725399417227; Tue, 03 Sep 2024 14:36:57 -0700 (PDT) Date: Tue, 3 Sep 2024 15:36:48 -0600 In-Reply-To: <20240903213649.3566695-1-yuzhao@google.com> Mime-Version: 1.0 References: <20240903213649.3566695-1-yuzhao@google.com> X-Mailer: git-send-email 2.46.0.469.g59c65b2a67-goog Message-ID: <20240903213649.3566695-2-yuzhao@google.com> Subject: [PATCH mm-unstable v1 2/3] mm/codetag: fix pgalloc_tag_split() From: Yu Zhao To: Andrew Morton , Kent Overstreet , Suren Baghdasaryan Cc: Muchun Song , linux-mm@kvack.org, linux-kernel@vger.kernel.org, Yu Zhao X-Rspamd-Queue-Id: 8326D14000B X-Rspam-User: X-Rspamd-Server: rspam05 X-Stat-Signature: d96cugr17ww6mrs3do8mx7s8frk8hjkh X-HE-Tag: 1725399418-778737 X-HE-Meta: U2FsdGVkX18n9B9ZpQHfAFWjahz32c9a1CFLzpm+KY3he1q+H58jlLJNeL3NY8Nyiv0bcWm2ivPQQyPtiIuF2Ac2kCanuk/j9r5ibZpXuL6RlBq41AOwzGTf734bXZGudumcl8+veCZTVSpDGMh+L94+VbRtQj5qvHpjgdxYu1PlGpt33M5iNOwJeoNU0rm15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I3fihCK7dRmaBEk89p55bVYjhUsjgCSSNt2U/Vr6oGL5kkCe/dzpABHzc69mWt16P0sSRPxXcLXMcvegCH/r4R48jrH2WwY+/x/kntiAtafAv4vlOQphkyEZnVDQwV3DGCEBopyfYQgwRYci81c5Cicwl4bYmAYOPy89Pxfr7oZPrDWmUTe3Dv1p7+54W/ko+KQuYAzr+BKBUzIPGEQub6k1qTKS6RYO/FJQATJ7evZTtCvsrVRPHa6D7mN8zM+LbMjedHh4BfmWccojD/eBIb5ooWHarJuvtdnDDt6wtty98hethZiiirEFPFR+MjRQ== 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: Only tag the new head pages when splitting one large folio to multiple ones of a lower order. Tagging tail pages can cause imbalanced "calls" counters, since only head pages are untagged by pgalloc_tag_sub() and reference counts on tail pages are leaked, e.g., # echo 2048kB >/sys/kernel/mm/hugepages/hugepages-1048576kB/demote_size # echo 700 >/sys/kernel/mm/hugepages/hugepages-1048576kB/nr_hugepages # time echo 700 >/sys/kernel/mm/hugepages/hugepages-1048576kB/demote # grep alloc_gigantic_folio /proc/allocinfo Before this patch: 0 549427200 mm/hugetlb.c:1549 func:alloc_gigantic_folio real 0m2.057s user 0m0.000s sys 0m2.051s After this patch: 0 0 mm/hugetlb.c:1549 func:alloc_gigantic_folio real 0m1.711s user 0m0.000s sys 0m1.704s Not tagging tail pages also improves the splitting time, e.g., by about 15% when demoting 1GB hugeTLB folios to 2MB ones, as shown above. Fixes: be25d1d4e822 ("mm: create new codetag references during page splitting") Signed-off-by: Yu Zhao Acked-by: Suren Baghdasaryan --- include/linux/mm.h | 30 ++++++++++++++++++++++++++++++ include/linux/pgalloc_tag.h | 31 ------------------------------- mm/huge_memory.c | 2 +- mm/hugetlb.c | 2 +- mm/page_alloc.c | 4 ++-- 5 files changed, 34 insertions(+), 35 deletions(-) diff --git a/include/linux/mm.h b/include/linux/mm.h index b31d4bdd65ad..a07e93adb8ad 100644 --- a/include/linux/mm.h +++ b/include/linux/mm.h @@ -4137,4 +4137,34 @@ void vma_pgtable_walk_end(struct vm_area_struct *vma); int reserve_mem_find_by_name(const char *name, phys_addr_t *start, phys_addr_t *size); +#ifdef CONFIG_MEM_ALLOC_PROFILING +static inline void pgalloc_tag_split(struct folio *folio, int old_order, int new_order) +{ + int i; + struct alloc_tag *tag; + unsigned int nr_pages = 1 << new_order; + + if (!mem_alloc_profiling_enabled()) + return; + + tag = pgalloc_tag_get(&folio->page); + if (!tag) + return; + + for (i = nr_pages; i < (1 << old_order); i += nr_pages) { + union codetag_ref *ref = get_page_tag_ref(folio_page(folio, i)); + + if (ref) { + /* Set new reference to point to the original tag */ + alloc_tag_ref_set(ref, tag); + put_page_tag_ref(ref); + } + } +} +#else /* !CONFIG_MEM_ALLOC_PROFILING */ +static inline void pgalloc_tag_split(struct folio *folio, int old_order, int new_order) +{ +} +#endif /* CONFIG_MEM_ALLOC_PROFILING */ + #endif /* _LINUX_MM_H */ diff --git a/include/linux/pgalloc_tag.h b/include/linux/pgalloc_tag.h index 207f0c83c8e9..59a3deb792a8 100644 --- a/include/linux/pgalloc_tag.h +++ b/include/linux/pgalloc_tag.h @@ -80,36 +80,6 @@ static inline void pgalloc_tag_sub(struct page *page, unsigned int nr) } } -static inline void pgalloc_tag_split(struct page *page, unsigned int nr) -{ - int i; - struct page_ext *first_page_ext; - struct page_ext *page_ext; - union codetag_ref *ref; - struct alloc_tag *tag; - - if (!mem_alloc_profiling_enabled()) - return; - - first_page_ext = page_ext = page_ext_get(page); - if (unlikely(!page_ext)) - return; - - ref = codetag_ref_from_page_ext(page_ext); - if (!ref->ct) - goto out; - - tag = ct_to_alloc_tag(ref->ct); - page_ext = page_ext_next(page_ext); - for (i = 1; i < nr; i++) { - /* Set new reference to point to the original tag */ - alloc_tag_ref_set(codetag_ref_from_page_ext(page_ext), tag); - page_ext = page_ext_next(page_ext); - } -out: - page_ext_put(first_page_ext); -} - static inline struct alloc_tag *pgalloc_tag_get(struct page *page) { struct alloc_tag *tag = NULL; @@ -142,7 +112,6 @@ static inline void clear_page_tag_ref(struct page *page) {} static inline void pgalloc_tag_add(struct page *page, struct task_struct *task, unsigned int nr) {} static inline void pgalloc_tag_sub(struct page *page, unsigned int nr) {} -static inline void pgalloc_tag_split(struct page *page, unsigned int nr) {} static inline struct alloc_tag *pgalloc_tag_get(struct page *page) { return NULL; } static inline void pgalloc_tag_sub_pages(struct alloc_tag *tag, unsigned int nr) {} diff --git a/mm/huge_memory.c b/mm/huge_memory.c index 0993dfe9ae94..aa8a4c938ba9 100644 --- a/mm/huge_memory.c +++ b/mm/huge_memory.c @@ -3244,7 +3244,7 @@ static void __split_huge_page(struct page *page, struct list_head *list, /* Caller disabled irqs, so they are still disabled here */ split_page_owner(head, order, new_order); - pgalloc_tag_split(head, 1 << order); + pgalloc_tag_split(folio, order, new_order); /* See comment in __split_huge_page_tail() */ if (folio_test_anon(folio)) { diff --git a/mm/hugetlb.c b/mm/hugetlb.c index 3faf5aad142d..a8624c07d8bf 100644 --- a/mm/hugetlb.c +++ b/mm/hugetlb.c @@ -3778,7 +3778,7 @@ static long demote_free_hugetlb_folios(struct hstate *src, struct hstate *dst, list_del(&folio->lru); split_page_owner(&folio->page, huge_page_order(src), huge_page_order(dst)); - pgalloc_tag_split(&folio->page, 1 << huge_page_order(src)); + pgalloc_tag_split(folio, huge_page_order(src), huge_page_order(dst)); for (i = 0; i < pages_per_huge_page(src); i += pages_per_huge_page(dst)) { struct page *page = folio_page(folio, i); diff --git a/mm/page_alloc.c b/mm/page_alloc.c index c242d61fc4fd..13ce8e8899ed 100644 --- a/mm/page_alloc.c +++ b/mm/page_alloc.c @@ -2822,7 +2822,7 @@ void split_page(struct page *page, unsigned int order) for (i = 1; i < (1 << order); i++) set_page_refcounted(page + i); split_page_owner(page, order, 0); - pgalloc_tag_split(page, 1 << order); + pgalloc_tag_split(page_folio(page), order, 0); split_page_memcg(page, order, 0); } EXPORT_SYMBOL_GPL(split_page); @@ -5020,7 +5020,7 @@ static void *make_alloc_exact(unsigned long addr, unsigned int order, struct page *last = page + nr; split_page_owner(page, order, 0); - pgalloc_tag_split(page, 1 << order); + pgalloc_tag_split(page_folio(page), order, 0); split_page_memcg(page, order, 0); while (page < --last) set_page_refcounted(last);