From patchwork Fri Sep 6 04:21:07 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yu Zhao X-Patchwork-Id: 13793201 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 09EBCCE7AAE for ; Fri, 6 Sep 2024 04:21:19 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 9B4A76B0082; Fri, 6 Sep 2024 00:21:18 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 965506B0088; Fri, 6 Sep 2024 00:21:18 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 82C266B0089; Fri, 6 Sep 2024 00:21:18 -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 642686B0082 for ; Fri, 6 Sep 2024 00:21:18 -0400 (EDT) Received: from smtpin23.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id 0DA43140E12 for ; Fri, 6 Sep 2024 04:21:18 +0000 (UTC) X-FDA: 82533013836.23.4ABBEA1 Received: from mail-yb1-f201.google.com (mail-yb1-f201.google.com [209.85.219.201]) by imf11.hostedemail.com (Postfix) with ESMTP id 4940D40011 for ; Fri, 6 Sep 2024 04:21:16 +0000 (UTC) Authentication-Results: imf11.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=HnJpyWOD; spf=pass (imf11.hostedemail.com: domain of 3O4PaZgYKCOgiejRKYQYYQVO.MYWVSXeh-WWUfKMU.YbQ@flex--yuzhao.bounces.google.com designates 209.85.219.201 as permitted sender) smtp.mailfrom=3O4PaZgYKCOgiejRKYQYYQVO.MYWVSXeh-WWUfKMU.YbQ@flex--yuzhao.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1725596427; 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=t8z7JhTPd5a0ay4gHmViQDV8ynpAlUlJ3UT1hxWkVyQ=; b=w4maggSjamimjUqcwUCABldGjwF9PRWXv+Xi80jEeoau3YMSqBs2wFH+qQl6dP1aBZaWTr XCuMjA6ZJQazd0bFZ8EemqfhzyYyQTeipGHIPrMpE5C7GDPZSxco/L+yZFWi/gR8qKbUYO 94Cq6W/sKJy87ij4cu2/3y5cd5P5Sc8= ARC-Authentication-Results: i=1; imf11.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=HnJpyWOD; spf=pass (imf11.hostedemail.com: domain of 3O4PaZgYKCOgiejRKYQYYQVO.MYWVSXeh-WWUfKMU.YbQ@flex--yuzhao.bounces.google.com designates 209.85.219.201 as permitted sender) smtp.mailfrom=3O4PaZgYKCOgiejRKYQYYQVO.MYWVSXeh-WWUfKMU.YbQ@flex--yuzhao.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1725596427; a=rsa-sha256; cv=none; b=6Hjw5XnIcr1N31M0HbWSCLFK3Ou6ZcaihfVpBVBB8S4g2Is1dpw9f0fin+EeRAz89hcH0x 79kQ6jREe2jDXRzWYlm3U7845FqPeEQlB9cu7JhtElaTog8pwNpX9PQoyhSpDhc7Lmrbnc cExmW+k2ass2WWXd5Tbi12DQ4SahzN4= Received: by mail-yb1-f201.google.com with SMTP id 3f1490d57ef6-e1d3255f852so1304731276.3 for ; Thu, 05 Sep 2024 21:21:15 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1725596475; x=1726201275; 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=t8z7JhTPd5a0ay4gHmViQDV8ynpAlUlJ3UT1hxWkVyQ=; b=HnJpyWODiE0C8HocmIIPO5jdGgZk2p11F7zbQ/QinUpPLRwqhuZFiAyqAfFV8gw8df vozgZ72ZJ86OH6QYwR+nkbcTjeKQVJUFAdDVZ8PuGlliqsG34DWBqaLG7bvvMp7B0CDB CU0KFBHMszU0zwYaCe5YGJR53m3/Ol3ZRsx+MsDthQ5COK6hcepwTzPva+DCG4l5vco2 HcRjcAacn5jrkmSpYQ8SN5duaAVeDnFpd5kinHxtzWYRR303Z7QFxRRvrbFxv5KnjN3s CzIChp+26zjUZEEwNpITfyqmz2f54pa5oH4HfzRXFbU403ykNqAilZqRwqXcQVQSMxXL kEUA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1725596475; x=1726201275; 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=t8z7JhTPd5a0ay4gHmViQDV8ynpAlUlJ3UT1hxWkVyQ=; b=KlCpcCKe2d40qA+S37OlFJDzVwEneAvW9qmNX7OVhT9DDE1Aw7yPAAdcf7crejYFbi tmODaNCl6X55BoQQ93OgnLPq+yheglhjuc/lUP9ZjwqjBhB8oeZkjl11kUYBmRZofC1l 8sPzmVE7Lqc1UMAD1fx0qS2DV9u4dnL3UjtDoiEvYz1h4Me1pUEJxRPtnpFq3C2ZuTHs pP8UJtyCKILZpk0revmuD5dd59xEY+pU7g3r/kI1JI+CZTpOWcT2DExTNpDjOjH+jZNX iH803OlVYFSNBWLtJLa2LPG9UgzkCFCn7F9ATFMqXDBymH8Q3/VFV78pKCjhMCiP3LZH BsnQ== X-Forwarded-Encrypted: i=1; AJvYcCV76UU2aHCOUj/tNQ+T3IQtss7ly4qufQTd4pjnH5DlYG87fa20xvVgKlhwn4j7Mw1Axb2CWgAU1w==@kvack.org X-Gm-Message-State: AOJu0YzsTP55xg9AwbfOzm0RPZWCIAlMdCe49aGpbd7crO4V7cG1+A4h 8+n4tmyMMuMJ/9/HqBE//cwOUGGhqiyy3gbZ54XyEstnj8tbscDrO8EEWoMSlQP8ug4zd+hwMeB ikA== X-Google-Smtp-Source: AGHT+IHnLZW3xX/3Rs6MjEAzTLo2oLLdlVk4kTRKTlbd52QR9KUnODr0nzvAIBIuE8r0TUpZnBgXzd5k3bg= X-Received: from yuzhao2.bld.corp.google.com ([2a00:79e0:2e28:6:5b7a:cdaf:9b3d:354a]) (user=yuzhao job=sendgmr) by 2002:a05:6902:2d43:b0:e16:4e62:8a17 with SMTP id 3f1490d57ef6-e1d3485d5cemr5514276.2.1725596475174; Thu, 05 Sep 2024 21:21:15 -0700 (PDT) Date: Thu, 5 Sep 2024 22:21:07 -0600 In-Reply-To: <20240906042108.1150526-1-yuzhao@google.com> Mime-Version: 1.0 References: <20240906042108.1150526-1-yuzhao@google.com> X-Mailer: git-send-email 2.46.0.469.g59c65b2a67-goog Message-ID: <20240906042108.1150526-2-yuzhao@google.com> Subject: [PATCH mm-unstable v2 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 , stable@vger.kernel.org X-Rspamd-Server: rspam03 X-Rspam-User: X-Rspamd-Queue-Id: 4940D40011 X-Stat-Signature: aagfm38pustoxwdczz6a5a9w8k4pzr4b X-HE-Tag: 1725596476-403832 X-HE-Meta: U2FsdGVkX18BWnZsU+l3spZ76pjOBtz2ObT8h3gUSH1za67Uo+57GGrfKbCfBleemcIT/9If9ik5NLJ/cb2VPrZkqXoc1MpdguCLiPwbdl7IP+KTlPmeHqnN1wwG7QMSSKWoKm7Bb1rKlp595oyKAjQb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XsLJVZ2EOcKPtNqogOxwLzPowKmOjyIvPY520BcbQKTF7hzmLqLP4mMgPwKfrHydnfQAUt9+e4MAaS+V61vlqGeSsNzsNT2Z0p1Q/O+Me9pBqdTxNiuyG0UkxCO+nqfrQznD07TLyk9lMLyv1cpagCAFrHxrsdvxN2ybcaGHaAZ6wOV5pOqAKKmhoKAGYyDh79NhU2emm/81oM/E0GlQZADCupOuKFUBJeU5Dwc0YvOrzApTCJNzFY1gl3bQpQRyhilcwta/LR7yEfT4pEDrc7kBPvZYxYZKrpUb1LOgJpQ3XMudD5ja39OA+spDWiHIARLhbPLi264aAGL0K+iURBmiM1zpRTm72CyH3hEDOmdSs= 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: The current assumption is that a large folio can only be split into order-0 folios. That is not the case for hugeTLB demotion, nor for THP split: see commit c010d47f107f ("mm: thp: split huge page to any lower order pages"). When a large folio is split into ones of a lower non-zero order, only the new head pages should be tagged. Tagging tail pages can cause imbalanced "calls" counters, since only head pages are untagged by pgalloc_tag_sub() and the "calls" 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 # echo 0 >/sys/kernel/mm/hugepages/hugepages-2048kB/nr_hugepages # 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 Cc: --- 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 fdc83b0c9e71..2a73efea02d7 100644 --- a/mm/huge_memory.c +++ b/mm/huge_memory.c @@ -3242,7 +3242,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 2a73753ecf9e..5c77defad295 100644 --- a/mm/hugetlb.c +++ b/mm/hugetlb.c @@ -3795,7 +3795,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 6b003f57965d..88113fdba956 100644 --- a/mm/page_alloc.c +++ b/mm/page_alloc.c @@ -2783,7 +2783,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); @@ -4981,7 +4981,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);