From patchwork Sat Feb 1 23:18:02 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Suren Baghdasaryan X-Patchwork-Id: 13956399 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 14D19C0218A for ; Sat, 1 Feb 2025 23:18:14 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 98DCE6B0088; Sat, 1 Feb 2025 18:18:13 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 8F1E46B0089; Sat, 1 Feb 2025 18:18:13 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 7426E6B008A; Sat, 1 Feb 2025 18:18:13 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id 4C1F86B0088 for ; Sat, 1 Feb 2025 18:18:13 -0500 (EST) Received: from smtpin08.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 07C8CB21F5 for ; Sat, 1 Feb 2025 23:18:13 +0000 (UTC) X-FDA: 83072941266.08.FC178A4 Received: from mail-pj1-f74.google.com (mail-pj1-f74.google.com [209.85.216.74]) by imf09.hostedemail.com (Postfix) with ESMTP id 3621B140008 for ; Sat, 1 Feb 2025 23:18:10 +0000 (UTC) Authentication-Results: imf09.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=AcXptLa7; spf=pass (imf09.hostedemail.com: domain of 3saueZwYKCMY463qzns00sxq.o0yxuz69-yyw7mow.03s@flex--surenb.bounces.google.com designates 209.85.216.74 as permitted sender) smtp.mailfrom=3saueZwYKCMY463qzns00sxq.o0yxuz69-yyw7mow.03s@flex--surenb.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=1738451891; 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=REEyzR4VwcXBG/MiulZy61h2cqis6B2iwbKUK+/Z7ro=; b=16TGjUDJhGdCc3Xdbc5tkqXG0cik/ScMtLvLtC60hZAXnsn3jeJ9IIappTzIgMbmDxNx6a wn2uvf88G6frUlzj2+6bV0fNeVcd3d6IyhmoG+zuRFuMJGJgLCjyrc3atCambcnQeDdCr7 rXonVeNAlHkwqE51h7sTiT4xIpuJyTA= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1738451891; a=rsa-sha256; cv=none; b=RR/9e1Wz6YcdViYzheTjfjsin7hDxB0JA2LN/ZYmGkR7YvDelmM8q6YTOCe8c+TN95AIn0 v0DlK+dJ1RutP9ivbbpW/080zmZFRpJZg5mbLjpCtZgb3U/vbXhm42O18R8rWqktwX9EEU uDnclTp+TWj+hSyWH7fO6ZnNWZzPdkk= ARC-Authentication-Results: i=1; imf09.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=AcXptLa7; spf=pass (imf09.hostedemail.com: domain of 3saueZwYKCMY463qzns00sxq.o0yxuz69-yyw7mow.03s@flex--surenb.bounces.google.com designates 209.85.216.74 as permitted sender) smtp.mailfrom=3saueZwYKCMY463qzns00sxq.o0yxuz69-yyw7mow.03s@flex--surenb.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com Received: by mail-pj1-f74.google.com with SMTP id 98e67ed59e1d1-2eebfd6d065so8996801a91.3 for ; Sat, 01 Feb 2025 15:18:10 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1738451890; x=1739056690; 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=REEyzR4VwcXBG/MiulZy61h2cqis6B2iwbKUK+/Z7ro=; b=AcXptLa72a2pLuG37EDmKfBj/wdHxjLDgeH7x17Txb3y0/lJ473iy/WzbhEwsoQk08 HbQXCEsIb7kY3P43TVOlp0bQUAGoA1lIiKkrtb6kImugMhy3gVbF7rd9E7yHJFJLlop9 t46fj9cj5JiULwbSHtdVyVpeEoZJ1UiMuS+tVGLQVT5rIx4vr2Rk0YrRzRKqfAYwKlCL h0U7qy2Tk0OZgdtz5Oook999+dGXBvTWWslBDeslSVo5ylDjifarHeInHo/ZPhJRPFFP 01IrD2KTLFtG3IhZTPeubUZYunx0ethlbqeNsSJGO+zE5c8cuReUHtOQ2+zuJchHRjGe pp6Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1738451890; x=1739056690; 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=REEyzR4VwcXBG/MiulZy61h2cqis6B2iwbKUK+/Z7ro=; b=IP5VqOUWSUM7UvusOwydnsB5r6nP/MMCW5TgA9g50HA+n3VZFpBR0r2XUCxrP2+ogm DzGkaQ+C1KMjFCoLGNN9aZY5AkJ2QOCF1lrC+9qGv2koFjzpp9DxUz3vqjKbIEbjTXBi dkAVQ+l7DDEEXESU0Yd8iL7tb1im5iuUuDflhwFrWeAXJT0vg/t/euDmrMsEXRF8y5DB P3/zMY1McDBuk1XLC6/LC7EuLSZRtlWYmmrtSYuhjYbecLHWHRLMIp+qF2dUGktoSaOJ NkSjw41CAn/cDHEFr7ULdqtqHpXxdj+ZNnn6eNMUyGVUag2giE5eJYAEpT6SarT6PgiE EYNw== X-Forwarded-Encrypted: i=1; AJvYcCVf3VcEBGpp/uhkqzlFmJkUtc6SIWcMBbwBsIjMCkrbfBmrGPre2fwA++oegYdn2b+9JR+Pv9bQ3A==@kvack.org X-Gm-Message-State: AOJu0YyZCuYnrK1CIjS+BqChyLNtvayRCUFqS1JADOmfIE1MLqBCOJHe iAZg1LkkOy8LQgFUW/bXukGnnHnx1ZLt4qMdk0QLDzSVTeiYKSy8Z6cLsktf2OT7iZ97dj6rp+q rvQ== X-Google-Smtp-Source: AGHT+IH3XLGWaDaT1t9/Cp7o6c+RF7ry2yIsdfJHITI/fssp3IRkvyZrbgiqnj5YWj1ktwSp36mfOaC0fRY= X-Received: from pfwo11.prod.google.com ([2002:a05:6a00:1bcb:b0:72a:bc54:8507]) (user=surenb job=prod-delivery.src-stubby-dispatcher) by 2002:aa7:930b:0:b0:726:8366:40ca with SMTP id d2e1a72fcca58-72fd0bbd6dcmr22976159b3a.1.1738451889825; Sat, 01 Feb 2025 15:18:09 -0800 (PST) Date: Sat, 1 Feb 2025 15:18:02 -0800 In-Reply-To: <20250201231803.2661189-1-surenb@google.com> Mime-Version: 1.0 References: <20250201231803.2661189-1-surenb@google.com> X-Mailer: git-send-email 2.48.1.362.g079036d154-goog Message-ID: <20250201231803.2661189-3-surenb@google.com> Subject: [PATCH v2 3/3] alloc_tag: uninline code gated by mem_alloc_profiling_key in page allocator From: Suren Baghdasaryan To: akpm@linux-foundation.org Cc: kent.overstreet@linux.dev, vbabka@suse.cz, rostedt@goodmis.org, peterz@infradead.org, yuzhao@google.com, minchan@google.com, shakeel.butt@linux.dev, souravpanda@google.com, pasha.tatashin@soleen.com, 00107082@163.com, quic_zhenhuah@quicinc.com, surenb@google.com, linux-mm@kvack.org, linux-kernel@vger.kernel.org X-Stat-Signature: dio44dqu4yjwdb5mjhj93k94fjiij58b X-Rspam-User: X-Rspamd-Queue-Id: 3621B140008 X-Rspamd-Server: rspam03 X-HE-Tag: 1738451890-555478 X-HE-Meta: U2FsdGVkX18iM23ipA9mrvUxZ0Xq+//bmZeXa8OLXrr41we4PTdcyQkkUm+xDH3kYn9p0U4GdHBvWpJEz3XAByKqxfJ/DYc69R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01CNvOf9vlPRzPH8TxHEu4BKP+QiKvjilyvxGAo7LKg3hMXbwwAVSFx9G2f31tpPhukMDA5j85PSIDKW2tqZcazYmTx12w+Wa3hJsAwc82j5mrIypgNkrOQRisICu+SFzc7hYQT8boQmrBXtbVyZgLhozJC8R4Xv3SNGzarUc0OnjOZ6000359T6SK3X2XZhkemfCAXTOoLj9aMzFnbszAnselDk2RX43OXF52tjJ8CTTe4H82fT+HqiV5LEV8ol638iNrTEV3977YwOOkiNlyZTBcQ7qh/H5aG5UjhZF3mGCSKHkkSwfwOGpmblv5F4T6uCc7XcLKu0v6Z9GQXe20ry2nbcwbJJ278JS1r/T5BgZnYAwIXOmAGZqq3Y+Qa0QGBZSxQFEPtu7W/YpLUYiiGgfZc2SrjDoE3uA07tN6eIAvgqO0N5T0tJF08elN5aQ6oDk= X-Bogosity: Ham, tests=bogofilter, spamicity=0.000013, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: When a sizable code section is protected by a disabled static key, that code gets into the instruction cache even though it's not executed and consumes the cache, increasing cache misses. This can be remedied by moving such code into a separate uninlined function. On a Pixel6 phone, page allocation profiling overhead measured with CONFIG_MEM_ALLOC_PROFILING=y and profiling disabled is: baseline modified Big core 4.93% 1.53% Medium core 4.39% 1.41% Little core 1.02% 0.36% This improvement comes at the expense of the configuration when profiling gets enabled, since there is now an additional function call. The overhead from this additional call on Pixel6 is: Big core 0.24% Middle core 0.63% Little core 1.1% However this is negligible when compared with the overall overhead of the memory allocation profiling when it is enabled. On x86 this patch does not make noticeable difference because the overhead with mem_alloc_profiling_key disabled is much lower (under 1%) to start with, so any improvement is less visible and hard to distinguish from the noise. The overhead from additional call when profiling is enabled is also within noise levels. Signed-off-by: Suren Baghdasaryan Reviewed-by: Shakeel Butt --- Changes since v1 [1]: - Removed inline_if_mem_alloc_prof and uninlined the gated code unconditionally, per Steven Rostedt and Vlastimil Babka - Updated changelog to include overhead when profiling is enabled [1] https://lore.kernel.org/all/20250126070206.381302-3-surenb@google.com/ include/linux/pgalloc_tag.h | 60 +++------------------------- mm/page_alloc.c | 78 +++++++++++++++++++++++++++++++++++++ 2 files changed, 83 insertions(+), 55 deletions(-) diff --git a/include/linux/pgalloc_tag.h b/include/linux/pgalloc_tag.h index 4a82b6b4820e..c74077977830 100644 --- a/include/linux/pgalloc_tag.h +++ b/include/linux/pgalloc_tag.h @@ -162,47 +162,13 @@ static inline void update_page_tag_ref(union pgtag_ref_handle handle, union code } } -static inline void clear_page_tag_ref(struct page *page) -{ - if (mem_alloc_profiling_enabled()) { - union pgtag_ref_handle handle; - union codetag_ref ref; - - if (get_page_tag_ref(page, &ref, &handle)) { - set_codetag_empty(&ref); - update_page_tag_ref(handle, &ref); - put_page_tag_ref(handle); - } - } -} - -static inline void pgalloc_tag_add(struct page *page, struct task_struct *task, - unsigned int nr) -{ - if (mem_alloc_profiling_enabled()) { - union pgtag_ref_handle handle; - union codetag_ref ref; - - if (get_page_tag_ref(page, &ref, &handle)) { - alloc_tag_add(&ref, task->alloc_tag, PAGE_SIZE * nr); - update_page_tag_ref(handle, &ref); - put_page_tag_ref(handle); - } - } -} +/* Should be called only if mem_alloc_profiling_enabled() */ +void __clear_page_tag_ref(struct page *page); -static inline void pgalloc_tag_sub(struct page *page, unsigned int nr) +static inline void clear_page_tag_ref(struct page *page) { - if (mem_alloc_profiling_enabled()) { - union pgtag_ref_handle handle; - union codetag_ref ref; - - if (get_page_tag_ref(page, &ref, &handle)) { - alloc_tag_sub(&ref, PAGE_SIZE * nr); - update_page_tag_ref(handle, &ref); - put_page_tag_ref(handle); - } - } + if (mem_alloc_profiling_enabled()) + __clear_page_tag_ref(page); } /* Should be called only if mem_alloc_profiling_enabled() */ @@ -222,18 +188,6 @@ static inline struct alloc_tag *__pgalloc_tag_get(struct page *page) return tag; } -static inline void pgalloc_tag_sub_pages(struct page *page, unsigned int nr) -{ - struct alloc_tag *tag; - - if (!mem_alloc_profiling_enabled()) - return; - - tag = __pgalloc_tag_get(page); - if (tag) - this_cpu_sub(tag->counters->bytes, PAGE_SIZE * nr); -} - void pgalloc_tag_split(struct folio *folio, int old_order, int new_order); void pgalloc_tag_swap(struct folio *new, struct folio *old); @@ -242,10 +196,6 @@ void __init alloc_tag_sec_init(void); #else /* CONFIG_MEM_ALLOC_PROFILING */ 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_sub_pages(struct page *page, unsigned int nr) {} static inline void alloc_tag_sec_init(void) {} static inline void pgalloc_tag_split(struct folio *folio, int old_order, int new_order) {} static inline void pgalloc_tag_swap(struct folio *new, struct folio *old) {} diff --git a/mm/page_alloc.c b/mm/page_alloc.c index b7e3b45183ed..16dfcf7ade74 100644 --- a/mm/page_alloc.c +++ b/mm/page_alloc.c @@ -1041,6 +1041,84 @@ static void kernel_init_pages(struct page *page, int numpages) kasan_enable_current(); } +#ifdef CONFIG_MEM_ALLOC_PROFILING + +/* Should be called only if mem_alloc_profiling_enabled() */ +void __clear_page_tag_ref(struct page *page) +{ + union pgtag_ref_handle handle; + union codetag_ref ref; + + if (get_page_tag_ref(page, &ref, &handle)) { + set_codetag_empty(&ref); + update_page_tag_ref(handle, &ref); + put_page_tag_ref(handle); + } +} + +/* Should be called only if mem_alloc_profiling_enabled() */ +static noinline +void __pgalloc_tag_add(struct page *page, struct task_struct *task, + unsigned int nr) +{ + union pgtag_ref_handle handle; + union codetag_ref ref; + + if (get_page_tag_ref(page, &ref, &handle)) { + alloc_tag_add(&ref, task->alloc_tag, PAGE_SIZE * nr); + update_page_tag_ref(handle, &ref); + put_page_tag_ref(handle); + } +} + +static inline void pgalloc_tag_add(struct page *page, struct task_struct *task, + unsigned int nr) +{ + if (mem_alloc_profiling_enabled()) + __pgalloc_tag_add(page, task, nr); +} + +/* Should be called only if mem_alloc_profiling_enabled() */ +static noinline +void __pgalloc_tag_sub(struct page *page, unsigned int nr) +{ + union pgtag_ref_handle handle; + union codetag_ref ref; + + if (get_page_tag_ref(page, &ref, &handle)) { + alloc_tag_sub(&ref, PAGE_SIZE * nr); + update_page_tag_ref(handle, &ref); + put_page_tag_ref(handle); + } +} + +static inline void pgalloc_tag_sub(struct page *page, unsigned int nr) +{ + if (mem_alloc_profiling_enabled()) + __pgalloc_tag_sub(page, nr); +} + +static inline void pgalloc_tag_sub_pages(struct page *page, unsigned int nr) +{ + struct alloc_tag *tag; + + if (!mem_alloc_profiling_enabled()) + return; + + tag = __pgalloc_tag_get(page); + if (tag) + this_cpu_sub(tag->counters->bytes, PAGE_SIZE * nr); +} + +#else /* CONFIG_MEM_ALLOC_PROFILING */ + +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_sub_pages(struct page *page, unsigned int nr) {} + +#endif /* CONFIG_MEM_ALLOC_PROFILING */ + __always_inline bool free_pages_prepare(struct page *page, unsigned int order) {