From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-oo1-f74.google.com (mail-oo1-f74.google.com [209.85.161.74]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id E36BE8494 for ; Sun, 26 Jan 2025 07:02:10 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.161.74 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1737874932; cv=none; b=PA2W79EsLo7OYqfJuDP0Mtie+6QY4UbOMmYOu7wT+osLa3nW7DS7/fVnRARh6YP3W1mIPNMwDhHU6wWI8JVbor5arWl0WKayEmcpmhPsqJSVE/YZTMmROT1oTQVZDHm0psciz0LwvJxZl6jMksaHuI93Xh8DiwghoWEE9hjbl4E= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1737874932; c=relaxed/simple; bh=wBWiYh5veVoVf8FAth+ZLP6npO9w2Ew8T8vOGr0QFOM=; h=Date:Mime-Version:Message-ID:Subject:From:To:Cc:Content-Type; b=Jbd42i7qXWHVeGAuYt4HOIzMsxOQCU1J3QeAeOYTgd+RhxU9R9uxEY28LXiCbbDrM/fGpKvAYNzBLl42hlEwWCv9zn2cY89gQJH02ZlYl6qScPLlz4s795FT0kdGVgrhsc4vagMqj35Ogv51H73pMVh9DPJBDQVk4xGn/XU7ncw= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--surenb.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=k4L5zGsK; arc=none smtp.client-ip=209.85.161.74 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--surenb.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="k4L5zGsK" Received: by mail-oo1-f74.google.com with SMTP id 006d021491bc7-5f70b0fad64so1130285eaf.1 for ; Sat, 25 Jan 2025 23:02:10 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1737874930; x=1738479730; darn=vger.kernel.org; h=cc:to:from:subject:message-id:mime-version:date:from:to:cc:subject :date:message-id:reply-to; bh=Gjibp4HHI09y13Cb+jMTPU7WntuA24yRPhNo2MxmLn0=; b=k4L5zGsKxqD6yCl7xEg8k+w5GQooqMQ0ICBxvhs++Y9jiCeGaNo+pgGfDvoE4ygiOm GGh50XmAAetylzctZLIWNB7P+np9yCYigmXZGDHqvg+ZTyVv+w4KnBEUUQoca2seM4B3 HxyaTmB6WeIaMpW6QNsTo3UHYz4OTXrg2Ae+S8LMcNjd1AzVI7gxVL7a2Etas/HWFhtx u+FLjIMpl5pf2ZvALRdQzLwIup34e+WqBnRLPynSe6wPuz1SRz0BNO7+2tioaQ+7SsdH avA1KHkU8fKnZs0VX+y1aoXjB7PQUX5AjCKmebzmsIYLnmTbcS4HV3E2q2Wuwn94hhw8 Hcrw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1737874930; x=1738479730; h=cc:to:from:subject:message-id:mime-version:date:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=Gjibp4HHI09y13Cb+jMTPU7WntuA24yRPhNo2MxmLn0=; b=XUqDNqXiVC+ljMvf5QivALW1ayKnVHXyxBa77aQ4m1pQrLHQxp4mt//UlFWMry95NQ 3pzORcwm1wBm7TGVgKuuP2vDkFXtuV462PwYylbcgwBWviFVBhIUWyKRevPOsp+YyFCl vF2hGMEsQVRDy2zp6LJfIhln03P25lqNuuiAZhu3bvDI7st0TCFUTUP/q4CR8QGENlNf EpbhK+D3rPB6fngYo8XPNbrYJPUxtERIwe5RwSKSbfVCLuyD6XRQM0CHkpkTei8UM3Cr 1KBjTfW84Fs2g+HzddumFUyZqvzWiYdK+EeO0wJiLKrYfTlxiGDq5hJCMfkYEsT8TgWP NaAQ== X-Forwarded-Encrypted: i=1; AJvYcCWdNqt7ZUFYNgOed7knyRSHaABfTFFO4YddXf1KcGdLAw2FnwufL9foixjr51bM5r5CNCCa52lHGrBB5+8=@vger.kernel.org X-Gm-Message-State: AOJu0YwhTSsnVJm22QkytuQONvUxZc/039v0lnySZ7Bvt0oM3jtRkr54 xHZ3o78NeKIKQqwUDSV8TKvC68WVhnHl3SM1sd7+JSrzjArHNqSzqSnq+CvZACR2h5H7qbA7hGO +bg== X-Google-Smtp-Source: AGHT+IFx729Tm/RHbxsPZVc8EhlfE/xBJUd+ZMgbbEChjn50lBiPKyouSgWjqxGyD7UokRCh6tCkDMiH8jU= X-Received: from oacpz5.prod.google.com ([2002:a05:6871:e485:b0:29e:147:e258]) (user=surenb job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6870:4e0d:b0:29e:2f3a:761a with SMTP id 586e51a60fabf-2b1c0a349f5mr20640727fac.20.1737874929840; Sat, 25 Jan 2025 23:02:09 -0800 (PST) Date: Sat, 25 Jan 2025 23:02:04 -0800 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 X-Mailer: git-send-email 2.48.1.262.g85cc9f2d1e-goog Message-ID: <20250126070206.381302-1-surenb@google.com> Subject: [PATCH 1/3] mm: avoid extra mem_alloc_profiling_enabled() checks From: Suren Baghdasaryan To: akpm@linux-foundation.org Cc: kent.overstreet@linux.dev, vbabka@suse.cz, 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 Content-Type: text/plain; charset="UTF-8" Refactor code to avoid extra mem_alloc_profiling_enabled() checks inside pgalloc_tag_get() function which is often called after that check was already done. Signed-off-by: Suren Baghdasaryan --- include/linux/pgalloc_tag.h | 35 +++++++++++++++++++---------------- lib/alloc_tag.c | 6 +++--- mm/page_alloc.c | 3 +-- 3 files changed, 23 insertions(+), 21 deletions(-) diff --git a/include/linux/pgalloc_tag.h b/include/linux/pgalloc_tag.h index 3469c4b20105..4a82b6b4820e 100644 --- a/include/linux/pgalloc_tag.h +++ b/include/linux/pgalloc_tag.h @@ -205,28 +205,32 @@ static inline void pgalloc_tag_sub(struct page *page, unsigned int nr) } } -static inline struct alloc_tag *pgalloc_tag_get(struct page *page) +/* Should be called only if mem_alloc_profiling_enabled() */ +static inline struct alloc_tag *__pgalloc_tag_get(struct page *page) { struct alloc_tag *tag = NULL; - - 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_check(&ref); - if (ref.ct) - tag = ct_to_alloc_tag(ref.ct); - put_page_tag_ref(handle); - } + union pgtag_ref_handle handle; + union codetag_ref ref; + + if (get_page_tag_ref(page, &ref, &handle)) { + alloc_tag_sub_check(&ref); + if (ref.ct) + tag = ct_to_alloc_tag(ref.ct); + put_page_tag_ref(handle); } return tag; } -static inline void pgalloc_tag_sub_pages(struct alloc_tag *tag, unsigned int nr) +static inline void pgalloc_tag_sub_pages(struct page *page, unsigned int nr) { - if (mem_alloc_profiling_enabled() && tag) + 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); } @@ -241,8 +245,7 @@ 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 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) {} +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/lib/alloc_tag.c b/lib/alloc_tag.c index 19b45617bdcf..1d893e313614 100644 --- a/lib/alloc_tag.c +++ b/lib/alloc_tag.c @@ -174,7 +174,7 @@ void pgalloc_tag_split(struct folio *folio, int old_order, int new_order) if (!mem_alloc_profiling_enabled()) return; - tag = pgalloc_tag_get(&folio->page); + tag = __pgalloc_tag_get(&folio->page); if (!tag) return; @@ -200,10 +200,10 @@ void pgalloc_tag_swap(struct folio *new, struct folio *old) if (!mem_alloc_profiling_enabled()) return; - tag_old = pgalloc_tag_get(&old->page); + tag_old = __pgalloc_tag_get(&old->page); if (!tag_old) return; - tag_new = pgalloc_tag_get(&new->page); + tag_new = __pgalloc_tag_get(&new->page); if (!tag_new) return; diff --git a/mm/page_alloc.c b/mm/page_alloc.c index 6e469c7ef9a4..55ed2f245f80 100644 --- a/mm/page_alloc.c +++ b/mm/page_alloc.c @@ -4832,12 +4832,11 @@ void __free_pages(struct page *page, unsigned int order) { /* get PageHead before we drop reference */ int head = PageHead(page); - struct alloc_tag *tag = pgalloc_tag_get(page); if (put_page_testzero(page)) free_frozen_pages(page, order); else if (!head) { - pgalloc_tag_sub_pages(tag, (1 << order) - 1); + pgalloc_tag_sub_pages(page, (1 << order) - 1); while (order-- > 0) free_frozen_pages(page + (1 << order), order); } base-commit: a227a25fe3fdd08f743a20a6bf6367a47b20c125 -- 2.48.1.262.g85cc9f2d1e-goog