From patchwork Fri Jun 28 21:03:10 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Roman Gushchin X-Patchwork-Id: 13716624 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 E64ECC2BD09 for ; Fri, 28 Jun 2024 21:03:56 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 1B45F6B00A0; Fri, 28 Jun 2024 17:03:56 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 165696B00A1; Fri, 28 Jun 2024 17:03:56 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id E834F6B00A2; Fri, 28 Jun 2024 17:03:55 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id C96956B00A0 for ; Fri, 28 Jun 2024 17:03:55 -0400 (EDT) Received: from smtpin17.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id 8514D120616 for ; Fri, 28 Jun 2024 21:03:55 +0000 (UTC) X-FDA: 82281524430.17.A313764 Received: from out-179.mta1.migadu.com (out-179.mta1.migadu.com [95.215.58.179]) by imf20.hostedemail.com (Postfix) with ESMTP id 726FC1C001B for ; Fri, 28 Jun 2024 21:03:53 +0000 (UTC) Authentication-Results: imf20.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=J4erkf2Y; spf=pass (imf20.hostedemail.com: domain of roman.gushchin@linux.dev designates 95.215.58.179 as permitted sender) smtp.mailfrom=roman.gushchin@linux.dev; dmarc=pass (policy=none) header.from=linux.dev ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1719608624; 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=xMDF+K4BIl7gcrHo2WUEmAHLFHOqrbM2Rc/7npZEcT8=; b=Ld7YH1ECIPoBZ9gMZ2lQzDneSho0m9cjtF+N0TEgLcoHsYXB4oe+ce1dwheHFY5dnYHRcp zK6yOaqCrntPyILT5qd/W0vo64aD6WcOOi+jvACxV555LQGM7sU5VVbDEf6K5JbT4cthK0 4R+r+FXRgMPqczk9KvEdM+iHKHaiG9I= ARC-Authentication-Results: i=1; imf20.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=J4erkf2Y; spf=pass (imf20.hostedemail.com: domain of roman.gushchin@linux.dev designates 95.215.58.179 as permitted sender) smtp.mailfrom=roman.gushchin@linux.dev; dmarc=pass (policy=none) header.from=linux.dev ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1719608624; a=rsa-sha256; cv=none; b=lvqMR38VsOsdE5dyWTU3MYQtc8er1geWYQd5Bxj4xLjh43oO00jLOswIBih/jmen4INmYX To91aAfEdKDbixPCIssM8Jh9C/UAbsrZSVaCgw6CnSSUAznAm8VQFPSwF6ApMEFGy7amdn Gk5cBKANz7+RQJ6yLgKirBwkEH0+JzM= X-Envelope-To: akpm@linux-foundation.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1719608631; 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=xMDF+K4BIl7gcrHo2WUEmAHLFHOqrbM2Rc/7npZEcT8=; b=J4erkf2YjUTiJUMZYB1J0+yCmRXUnc59RpTJ8yK14OwGb7VFouZgBj42vekY+DDYdgNdig Ex07/kd5xqqsR3UtmucYNSKqSFqszQLIeVJs8dybLz17eb6z3H7M2D1joHukOI44bfUF+G jCt42MhduBjyhLVrKK6fNNHBrWvoAFA= X-Envelope-To: linux-mm@kvack.org X-Envelope-To: linux-kernel@vger.kernel.org X-Envelope-To: hannes@cmpxchg.org X-Envelope-To: mhocko@kernel.org X-Envelope-To: shakeel.butt@linux.dev X-Envelope-To: muchun.song@linux.dev X-Envelope-To: roman.gushchin@linux.dev X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. From: Roman Gushchin To: Andrew Morton Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org, Johannes Weiner , Michal Hocko , Shakeel Butt , Muchun Song , Roman Gushchin Subject: [PATCH v1 2/9] mm: memcg: factor out legacy socket memory accounting code Date: Fri, 28 Jun 2024 21:03:10 +0000 Message-ID: <20240628210317.272856-3-roman.gushchin@linux.dev> In-Reply-To: <20240628210317.272856-1-roman.gushchin@linux.dev> References: <20240628210317.272856-1-roman.gushchin@linux.dev> MIME-Version: 1.0 X-Migadu-Flow: FLOW_OUT X-Rspamd-Server: rspam03 X-Rspam-User: X-Rspamd-Queue-Id: 726FC1C001B X-Stat-Signature: qa5rndj1zuj3eswbfca44crcuydzz1z9 X-HE-Tag: 1719608633-66197 X-HE-Meta: U2FsdGVkX1+hZ1GA/EmTboWCeyZ5JVq0pGtR7e7fuq/eDc1NGO307uhOrL8mZO5UJtM1YpAet3randYgaFf/48jqUcJ7RKTKdqD7hGt5caefpLXWOtB5Xk7VQReyIqs3/R+PCZOXVI/sWK0NU9ieFv0A8FdlDCjCBT7i3a8BVqeozISp7xnvRQZDbcuDcOnqDIPcMgNBSzlAGH1t52PfZGg+Jhdye0HadaBMcaAwSmxbVYRBeoeDDgo0820BzAL1GwQYms0Lws9pQoIOp//EAPaLay7W55CVfMQZGLAoZKCUcdIGeZHwxjgfO3iJAupUkYE42whSfe62ng/L0loD5cv7SyWuguRTz2A79GBRvV91OO0TMAJ+dNdP1GElnY3iG7cd+jnyDkZmNJaJNld0cApXmdxZXF0jNs9cF9x3db+Nw+w+/wsw8VEMPj1i2rz0kcvfTcBWLrmr8km2THGqJC/ap/6KG0FhU5dEMJ2+gkvH3sdaVrYjuONemXJ1fTE4l8keNcFB9YQ7huxh/VQXg2Dj8Les+grg47lhsXKhvKwUwWuW58ltKIXsx/s2CvDM+yg+pIcv3L1+UZBtXANyp0CGvC2EHSy/WuAzfhFTKqwUEhP50IylvaEIIpkD1+b34pNv9h8eNYMKGRU7wyVpp5RRlp+no7rvf/yupxPLM0rmvgMV5zkyx3eCxdIfavH47otqesjOfNS/aKGIHQzNCFvXk6j6X0Q51P8wunPx1kpP9ZIZC+Q+88r6du85RShOqLBoKKkQJKhwCsFesjjgjf1OCXvqxfTNghI8WE5AqVL3gJWsvApQGT2QApffyaWO5W7mN4S/1kWr00eK4secSilhuYyrtyRn+Maj6mk60wOkFmRIhAAlxwBMsxTm+iimxn1FuRCM3/OBxIWnMLsInQVY61tYniXMWZ9vjTsxVPYQFxhkjxIsTpp8kEdTMusPWy21Djd2dHfwNCaqT3z ZyK+oykV +CngD8Jid8Dmb6CrDo2+LE3t+IcaFQrP06bo9RmC0pQNo18mc6FXdohUOA/EvofrNtulLLNIR7nL/Uw0gR7NQJeLxxysfGOEnQ1uqVvwFP+3ZIpQ= 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: Move out the legacy cgroup v1 socket memory accounting code into mm/memcontrol-v1.c. This commit introduces three new functions: memcg1_tcpmem_active(), memcg1_charge_skmem() and memcg1_uncharge_skmem(), which contain all cgroup v1-specific code and become trivial if CONFIG_MEMCG_V1 isn't set. Note, that !!memcg->tcpmem_pressure check in mem_cgroup_under_socket_pressure() can't be easily moved into memcontrol-v1.h without including memcontrol-v1.h from memcontrol.h which isn't a good idea, so it's better to just #ifdef it. Signed-off-by: Roman Gushchin Acked-by: Shakeel Butt --- include/linux/memcontrol.h | 2 ++ mm/memcontrol-v1.c | 17 +++++++++++++++++ mm/memcontrol-v1.h | 16 ++++++++++++++++ mm/memcontrol.c | 22 +++++----------------- 4 files changed, 40 insertions(+), 17 deletions(-) diff --git a/include/linux/memcontrol.h b/include/linux/memcontrol.h index 796cfa842346..44ab6394c9ed 100644 --- a/include/linux/memcontrol.h +++ b/include/linux/memcontrol.h @@ -1650,8 +1650,10 @@ void mem_cgroup_sk_alloc(struct sock *sk); void mem_cgroup_sk_free(struct sock *sk); static inline bool mem_cgroup_under_socket_pressure(struct mem_cgroup *memcg) { +#ifdef CONFIG_MEMCG_V1 if (!cgroup_subsys_on_dfl(memory_cgrp_subsys)) return !!memcg->tcpmem_pressure; +#endif /* CONFIG_MEMCG_V1 */ do { if (time_before(jiffies, READ_ONCE(memcg->socket_pressure))) return true; diff --git a/mm/memcontrol-v1.c b/mm/memcontrol-v1.c index c73a0ff0cc39..c9b4c3e4797d 100644 --- a/mm/memcontrol-v1.c +++ b/mm/memcontrol-v1.c @@ -2925,6 +2925,23 @@ void memcg1_account_kmem(struct mem_cgroup *memcg, int nr_pages) } #endif /* CONFIG_MEMCG_KMEM */ +bool memcg1_charge_skmem(struct mem_cgroup *memcg, unsigned int nr_pages, + gfp_t gfp_mask) +{ + struct page_counter *fail; + + if (page_counter_try_charge(&memcg->tcpmem, nr_pages, &fail)) { + memcg->tcpmem_pressure = 0; + return true; + } + memcg->tcpmem_pressure = 1; + if (gfp_mask & __GFP_NOFAIL) { + page_counter_charge(&memcg->tcpmem, nr_pages); + return true; + } + return false; +} + static int __init memcg1_init(void) { int node; diff --git a/mm/memcontrol-v1.h b/mm/memcontrol-v1.h index 61620e2b0bf0..c8e5119223bb 100644 --- a/mm/memcontrol-v1.h +++ b/mm/memcontrol-v1.h @@ -108,6 +108,17 @@ void memcg1_check_events(struct mem_cgroup *memcg, int nid); void memcg1_stat_format(struct mem_cgroup *memcg, struct seq_buf *s); void memcg1_account_kmem(struct mem_cgroup *memcg, int nr_pages); +static inline bool memcg1_tcpmem_active(struct mem_cgroup *memcg) +{ + return memcg->tcpmem_active; +} +bool memcg1_charge_skmem(struct mem_cgroup *memcg, unsigned int nr_pages, + gfp_t gfp_mask); +static inline void memcg1_uncharge_skmem(struct mem_cgroup *memcg, unsigned int nr_pages) +{ + page_counter_uncharge(&memcg->tcpmem, nr_pages); +} + extern struct cftype memsw_files[]; extern struct cftype mem_cgroup_legacy_files[]; @@ -127,6 +138,11 @@ static inline void memcg1_check_events(struct mem_cgroup *memcg, int nid) {} static inline void memcg1_stat_format(struct mem_cgroup *memcg, struct seq_buf *s) {} static inline void memcg1_account_kmem(struct mem_cgroup *memcg, int nr_pages) {} +static inline bool memcg1_tcpmem_active(struct mem_cgroup *memcg) { return false; } +static inline bool memcg1_charge_skmem(struct mem_cgroup *memcg, unsigned int nr_pages, + gfp_t gfp_mask) { return true; } +static inline void memcg1_uncharge_skmem(struct mem_cgroup *memcg, unsigned int nr_pages) {} + extern struct cftype memsw_files[]; extern struct cftype mem_cgroup_legacy_files[]; #endif /* CONFIG_MEMCG_V1 */ diff --git a/mm/memcontrol.c b/mm/memcontrol.c index 802a077e2e2f..2c0605bbbb31 100644 --- a/mm/memcontrol.c +++ b/mm/memcontrol.c @@ -3788,7 +3788,7 @@ static void mem_cgroup_css_free(struct cgroup_subsys_state *css) if (cgroup_subsys_on_dfl(memory_cgrp_subsys) && !cgroup_memory_nosocket) static_branch_dec(&memcg_sockets_enabled_key); - if (!cgroup_subsys_on_dfl(memory_cgrp_subsys) && memcg->tcpmem_active) + if (!cgroup_subsys_on_dfl(memory_cgrp_subsys) && memcg1_tcpmem_active(memcg)) static_branch_dec(&memcg_sockets_enabled_key); #if defined(CONFIG_MEMCG_KMEM) @@ -5013,7 +5013,7 @@ void mem_cgroup_sk_alloc(struct sock *sk) memcg = mem_cgroup_from_task(current); if (mem_cgroup_is_root(memcg)) goto out; - if (!cgroup_subsys_on_dfl(memory_cgrp_subsys) && !memcg->tcpmem_active) + if (!cgroup_subsys_on_dfl(memory_cgrp_subsys) && !memcg1_tcpmem_active(memcg)) goto out; if (css_tryget(&memcg->css)) sk->sk_memcg = memcg; @@ -5039,20 +5039,8 @@ void mem_cgroup_sk_free(struct sock *sk) bool mem_cgroup_charge_skmem(struct mem_cgroup *memcg, unsigned int nr_pages, gfp_t gfp_mask) { - if (!cgroup_subsys_on_dfl(memory_cgrp_subsys)) { - struct page_counter *fail; - - if (page_counter_try_charge(&memcg->tcpmem, nr_pages, &fail)) { - memcg->tcpmem_pressure = 0; - return true; - } - memcg->tcpmem_pressure = 1; - if (gfp_mask & __GFP_NOFAIL) { - page_counter_charge(&memcg->tcpmem, nr_pages); - return true; - } - return false; - } + if (!cgroup_subsys_on_dfl(memory_cgrp_subsys)) + return memcg1_charge_skmem(memcg, nr_pages, gfp_mask); if (try_charge(memcg, gfp_mask, nr_pages) == 0) { mod_memcg_state(memcg, MEMCG_SOCK, nr_pages); @@ -5070,7 +5058,7 @@ bool mem_cgroup_charge_skmem(struct mem_cgroup *memcg, unsigned int nr_pages, void mem_cgroup_uncharge_skmem(struct mem_cgroup *memcg, unsigned int nr_pages) { if (!cgroup_subsys_on_dfl(memory_cgrp_subsys)) { - page_counter_uncharge(&memcg->tcpmem, nr_pages); + memcg1_uncharge_skmem(memcg, nr_pages); return; }