From patchwork Thu Aug 15 15:54:02 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Shakeel Butt X-Patchwork-Id: 13764970 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 06623C52D7C for ; Thu, 15 Aug 2024 15:54:40 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 92E636B015C; Thu, 15 Aug 2024 11:54:39 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 8DD886B015D; Thu, 15 Aug 2024 11:54:39 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 7A4AC6B015E; Thu, 15 Aug 2024 11:54:39 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id 578046B015C for ; Thu, 15 Aug 2024 11:54:39 -0400 (EDT) Received: from smtpin08.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 0D11FA8F2C for ; Thu, 15 Aug 2024 15:54:39 +0000 (UTC) X-FDA: 82454927478.08.A23251D Received: from out-188.mta1.migadu.com (out-188.mta1.migadu.com [95.215.58.188]) by imf20.hostedemail.com (Postfix) with ESMTP id 3D5601C0022 for ; Thu, 15 Aug 2024 15:54:37 +0000 (UTC) Authentication-Results: imf20.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=Kx2KlLkN; spf=pass (imf20.hostedemail.com: domain of shakeel.butt@linux.dev designates 95.215.58.188 as permitted sender) smtp.mailfrom=shakeel.butt@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=1723737241; 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:references:dkim-signature; bh=DjpAiVrd2YJQXzgNMIISiRWFMgGrAObcvytxXAsoS+o=; b=zI/RO6zZ3XxOLwsUzqZEZFXp86/Z6c/Rjvm7F5TKX0z8u1D6u2a449jM/Xb/i/aMUtyoje sS9yr/wKZpRJHrXvIqaqtipg/1CpxDvkxDI0eGMTfteO0Pm9/x9DU4lJiLn4EDNhxgmOOn Bta+XeBbvf+pH6+MVfBuQR4DgJ5VH1s= ARC-Authentication-Results: i=1; imf20.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=Kx2KlLkN; spf=pass (imf20.hostedemail.com: domain of shakeel.butt@linux.dev designates 95.215.58.188 as permitted sender) smtp.mailfrom=shakeel.butt@linux.dev; dmarc=pass (policy=none) header.from=linux.dev ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1723737241; a=rsa-sha256; cv=none; b=LhGC8Pj5CxO6ffqFvUkJpHZ7JKCtfquMwo51Lmt/R5gQ9ckd4mijUqR7zvo87jB/kdN59h I+g7/EQCjO9K+OeKPzwdZ5PV6U/I4J+EdIJ/gdjIRI9Wf6ShxhfHK7iHgC+7tEwKh4QdgG viNe7sXPBqXRYzXSuE1w+2PgyLow0Ic= X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1723737275; 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; bh=DjpAiVrd2YJQXzgNMIISiRWFMgGrAObcvytxXAsoS+o=; b=Kx2KlLkNtiaJWRRx1Lpiwnl0vG51HtNZvOmfAwA7VxHKPaUsVKh74zukXNwa6JqfNawSrb Ai6f4bTwh+vYS7bZvva9tO+v/MbyrUBhtVtoR8lGaNnb5x/BIVKBRnPamna0xBLDdeHYq7 NR3qdWieoaGRvtDMrqCvMlV6YN0AyZk= From: Shakeel Butt To: Andrew Morton Cc: Johannes Weiner , Michal Hocko , Roman Gushchin , Muchun Song , "T . J . Mercier" , linux-mm@kvack.org, linux-kernel@vger.kernel.org, Meta kernel team , cgroups@vger.kernel.org, Matthew Wilcox , Michal Hocko Subject: [PATCH v2] memcg: replace memcg ID idr with xarray Date: Thu, 15 Aug 2024 08:54:02 -0700 Message-ID: <20240815155402.3630804-1-shakeel.butt@linux.dev> MIME-Version: 1.0 X-Migadu-Flow: FLOW_OUT X-Rspamd-Server: rspam03 X-Rspam-User: X-Rspamd-Queue-Id: 3D5601C0022 X-Stat-Signature: uupdp5gepyb9xu4iobfswqwpjmui1bmj X-HE-Tag: 1723737277-870397 X-HE-Meta: U2FsdGVkX19P/jJ9j1R39WWBCQvnoYeCu4Y+b829V4mlp5O2IODcc4cEya/094FZFy3svhhy6WGASYLVcIoWweH6iUi7qlXoZTAGpLXWy7+DtQShA9PjW5L9/4uuhA+UyGOQXGzv8+AssP39bFKznvuzwPMvuWkjH5+6+ZKK26PaBog9axWX0RRUFY/3nHcF6/63EkrqQjShVHo/gZnj5ocVf8Ox+tG/8C2lePBr7dvgQAigFYLmm4Yrzfcu1LbGvL4kw35b7r6kNXdUSgelwZ0/XQrwY2IoqwI14HsfTpQ5d7mSezH1N2dC/lCEdnMZPfsrwNRdrt8ykhd9+xFB33TX8pSeo/2PosJWdNa+zq3gVC8zg4QOeLtL+We7nct0BIkr60/OihZu3OtxksQIFzOXCudSHysrOGJK1kwJvqv2D8m6Yfn75uyes+Wg5lOvd6/dGNx1nrQChg/wsHSqZiuT6hHvQOSm9GwCMR/mVUxCMyt9YeFnTeg2+fxRtG6bLwcMwZBXo+6E9FP7KO+TpiLsfFifxegtowbXB+3bIi0fkzIZVYF+/kwvV/lVD8cMrgk55/eoRuF2IJM22eFvOteLKuaZWZgXDrWEgY0Ag3KlZjz/HddRwGdhQ09u8TvgEi0UFPzN6FP6MQHLiX37gklI1R+PXo2JNt5g6svwgvwCXBcWPZOqv+fxaIthDEI1frqIMIMcttYRNJXRHIc0Xu4v1QNsNpZUgiysp/S8Q0CjNVt/VbONiCkFKji/yrR+aFu6q4c0XsDJGu+H9b7mzISq9MtopRxE1Hk9GW3M9Aa4SOqWa9nMwCk4vdf5J4IM39tlhlzplaQlIhqc+y8Ubp63zKrPqNwRQDGFrbdexj9vptbThy2yqd8fS0dTn3peZxmklo0GwQ372bagvlsHU08M1X7Ropdfl7LifyI7fZHLxckQete+pEpby6+md2riHp69YHPvUSS/BeShALT VEueWlkQ JfEqrcQaLjKvRLZHiPR00n0aUbKAPgbP+2PyNgoYlLvbCF4LJO/kLJO2KOqOQPE2esdIYKQNfaD6t/pMseCddMv9o4hKCYDfgm+Y194zeF8YCys6RvtXVN53AvyovZFknqac7aZN5lK0zxm/r57igjz0okhdtB6eqDfQrR2uq5/mGx8BRuxnh2jeRaU24+c2K9QSFPUSmb87ga3i+lvR3okVxgo0Fayo1JxGrS3axkB/E+VbqExaomyjB76uwFJbXSxbe9nNCTE9pG2bdT6kmJy0K67qREClaw+x0jfMbJWoOsxkNgESXq744Zgk7llpiekuT3hpcV8tp9Px9RiyO8GXsOTc7Mi2er0LW 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: At the moment memcg IDs are managed through IDR which requires external synchronization mechanisms and makes the allocation code a bit awkward. Let's switch to xarray and make the code simpler. Signed-off-by: Shakeel Butt Suggested-by: Matthew Wilcox Reviewed-by: Roman Gushchin Reviewed-by: Matthew Wilcox (Oracle) Acked-by: Johannes Weiner Reviewed-by: Muchun Song Acked-by: Michal Hocko Signed-off-by: Andrew Morton --- Changes since v1: - Fix error path in mem_cgroup_alloc (Dan Carpenter) mm/memcontrol.c | 39 ++++++++++----------------------------- 1 file changed, 10 insertions(+), 29 deletions(-) diff --git a/mm/memcontrol.c b/mm/memcontrol.c index df84683a0e1c..e8e03a5e1e5e 100644 --- a/mm/memcontrol.c +++ b/mm/memcontrol.c @@ -3408,29 +3408,12 @@ static void memcg_wb_domain_size_changed(struct mem_cgroup *memcg) */ #define MEM_CGROUP_ID_MAX ((1UL << MEM_CGROUP_ID_SHIFT) - 1) -static DEFINE_IDR(mem_cgroup_idr); -static DEFINE_SPINLOCK(memcg_idr_lock); - -static int mem_cgroup_alloc_id(void) -{ - int ret; - - idr_preload(GFP_KERNEL); - spin_lock(&memcg_idr_lock); - ret = idr_alloc(&mem_cgroup_idr, NULL, 1, MEM_CGROUP_ID_MAX + 1, - GFP_NOWAIT); - spin_unlock(&memcg_idr_lock); - idr_preload_end(); - return ret; -} +static DEFINE_XARRAY_ALLOC1(mem_cgroup_ids); static void mem_cgroup_id_remove(struct mem_cgroup *memcg) { if (memcg->id.id > 0) { - spin_lock(&memcg_idr_lock); - idr_remove(&mem_cgroup_idr, memcg->id.id); - spin_unlock(&memcg_idr_lock); - + xa_erase(&mem_cgroup_ids, memcg->id.id); memcg->id.id = 0; } } @@ -3465,7 +3448,7 @@ static inline void mem_cgroup_id_put(struct mem_cgroup *memcg) struct mem_cgroup *mem_cgroup_from_id(unsigned short id) { WARN_ON_ONCE(!rcu_read_lock_held()); - return idr_find(&mem_cgroup_idr, id); + return xa_load(&mem_cgroup_ids, id); } #ifdef CONFIG_SHRINKER_DEBUG @@ -3558,17 +3541,17 @@ static struct mem_cgroup *mem_cgroup_alloc(struct mem_cgroup *parent) struct mem_cgroup *memcg; int node, cpu; int __maybe_unused i; - long error = -ENOMEM; + long error; memcg = kzalloc(struct_size(memcg, nodeinfo, nr_node_ids), GFP_KERNEL); if (!memcg) - return ERR_PTR(error); + return ERR_PTR(-ENOMEM); - memcg->id.id = mem_cgroup_alloc_id(); - if (memcg->id.id < 0) { - error = memcg->id.id; + error = xa_alloc(&mem_cgroup_ids, &memcg->id.id, NULL, + XA_LIMIT(1, MEM_CGROUP_ID_MAX), GFP_KERNEL); + if (error) goto fail; - } + error = -ENOMEM; memcg->vmstats = kzalloc(sizeof(struct memcg_vmstats), GFP_KERNEL_ACCOUNT); @@ -3709,9 +3692,7 @@ static int mem_cgroup_css_online(struct cgroup_subsys_state *css) * publish it here at the end of onlining. This matches the * regular ID destruction during offlining. */ - spin_lock(&memcg_idr_lock); - idr_replace(&mem_cgroup_idr, memcg, memcg->id.id); - spin_unlock(&memcg_idr_lock); + xa_store(&mem_cgroup_ids, memcg->id.id, memcg, GFP_KERNEL); return 0; offline_kmem: