From patchwork Fri Aug 9 17:26:18 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Shakeel Butt X-Patchwork-Id: 13759032 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 917B9C3DA4A for ; Fri, 9 Aug 2024 17:26:41 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 26EFA6B009A; Fri, 9 Aug 2024 13:26:41 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 21FD86B009E; Fri, 9 Aug 2024 13:26:41 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 0E7906B009F; Fri, 9 Aug 2024 13:26:41 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id E4E946B009A for ; Fri, 9 Aug 2024 13:26:40 -0400 (EDT) Received: from smtpin10.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id 9BA45C132E for ; Fri, 9 Aug 2024 17:26:40 +0000 (UTC) X-FDA: 82433386560.10.3B9E0E4 Received: from out-182.mta0.migadu.com (out-182.mta0.migadu.com [91.218.175.182]) by imf15.hostedemail.com (Postfix) with ESMTP id B885BA0020 for ; Fri, 9 Aug 2024 17:26:38 +0000 (UTC) Authentication-Results: imf15.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=G20TQ2+p; spf=pass (imf15.hostedemail.com: domain of shakeel.butt@linux.dev designates 91.218.175.182 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=1723224311; 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=4iM0sefc3D7BN1ZfyJuESRluKUUBy69Kzbm9ZY4Fy3M=; b=vmZKD+jhnVLz4sOJKB9jKROE7UGX74mLGL/bopTDpCHIkp4Au/OLZHd5ea1HsjY9Qlo8he 2VFoNj2SoOIOoy1VNIvmTc/BJYuPRBdoVq1oedzy+xkJna0Iz0rcAItGgL9frRS+CkQ/m+ UQs+2E/1odhiwiUYUpAxzqCaj1tIx4A= ARC-Authentication-Results: i=1; imf15.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=G20TQ2+p; spf=pass (imf15.hostedemail.com: domain of shakeel.butt@linux.dev designates 91.218.175.182 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=1723224311; a=rsa-sha256; cv=none; b=bBRRtKQ73LWlYt0DTKMAGZKn4ROWFE/rxbBDHLHN4bJxkgFEvJoppOsib/8eTdC+yQ1UEH VT7nStbE8HTTLrNLcIjB9BKSvsgWS2TEzxQIPsr7oQ0x3tLegHBEciNGRUiQtC7QRKRjgR tSiIUvP5war5LjPorT+r7gTYy3lq0H8= 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=1723224396; 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=4iM0sefc3D7BN1ZfyJuESRluKUUBy69Kzbm9ZY4Fy3M=; b=G20TQ2+pPsPGtYkq8R7r38M9mMpyGGn6n276jn3JI1ZBVxvkOU5QGK9WvKp0ioHSZ+boXo Sv7Gu5thZjvb35Jh4i2klPZasx1mouiXlypVYRheOY9kUwUC/vGw7qs1xTDlKYswOxyHLT zTFBdkng29rTwZaGOhIMbkqBm5ZR5UE= From: Shakeel Butt To: Andrew Morton Cc: Johannes Weiner , Michal Hocko , Roman Gushchin , Muchun Song , linux-mm@kvack.org, linux-kernel@vger.kernel.org, Meta kernel team , cgroups@vger.kernel.org, Matthew Wilcox Subject: [PATCH] memcg: replace memcg ID idr with xarray Date: Fri, 9 Aug 2024 10:26:18 -0700 Message-ID: <20240809172618.2946790-1-shakeel.butt@linux.dev> MIME-Version: 1.0 X-Migadu-Flow: FLOW_OUT X-Rspamd-Server: rspam06 X-Rspamd-Queue-Id: B885BA0020 X-Stat-Signature: kek1nieo9f9851hmzirdtx31ms8nq847 X-Rspam-User: X-HE-Tag: 1723224398-510003 X-HE-Meta: U2FsdGVkX1/bIJHnOLFSCF+e7wI11LOnjgiIiBROU3ZaYDYmHqVzPwRgrD8jmTGK3j7VPsSPWRefMLFXM7xR5iN4RPNqsNUWv3GQBsOpsXrpSZJmq5DXxHaYcZHvk1X5O4F2sp1IEyz64Hij0i0TrGEn2f5COGtJw2ZxVkECVoYZmPdoc+S28GTQkbRWHIAMRiXlC3ZQI7sPuByEdJqiKRFZPlH1NzPrehKnnBhIXRRxCMmcEd91x9dXqLDTH1eV7iofNqULegV0+60qjP/W7HBgrF7CWMu9F8x5MCkcNse1IFl7OJUPIOTliZQCzA4f+4IEILMfUqNHJLRMdseStPoJ0RuoonG9Zm80dTziCfIvuj+1W6pIivP68KbErXppl+OevOWuC8Ocb+cHhzTz4qf6va5ZAWrkkVll/OBbNu1ScxV6n8hnuVLkMgZCBQQ7vmAyP/ffmHmHCOg2c2N1WjSo1vvVERLwvGIo5bFevF4mpwGBkv90FCpbh5Gpef3yME0nVzvZiaHjx36VHxDY066PCNCvVgxEIq+kL15wEbtMtFrYXHvYcb4NUIS0j5S0m8B0DzUmydtSzCoy9qoGmaO67RXUchHCm1x3rBFfP40y55iY5lke6UQCZUKxSP9tPW5qjrOFZxV9Iuo92S50Xr/jHJ9orjOjBMy7Nagol+tDMcI/ce+eXuS/KauZQ1jF7fLD9npadoCpdm48L1TqBP8rfg7ryuqzMA++t5koQzBZ8+zqT41f4O6C5gCwaXNZjP45RyS0p/bLpW3c/HKg4B8q6lJwr1agIAhDci/9K7cA5SNuu3sYoieA5LbYZIqAZH9ol6f6jaXJEUycOwRG1OR+bFfF5uZS+U5DGkARxAYg0Am0ARWfXIZzmy01uILCF3Zib5U+jOo59B7XVtWwD9AmqS0nAoMM3xXhpYWypfCZCF3QsMWoNaoYnLfcbBEaI4r94LRVyesBMWU+/DB 3bb5xD+b 3wsm7UdoL5QqsjVxe8T1bBzUPDkzvCdpSqgyiKmrfj4qGnKyh0M/8lTN6Pbhm0i8IGN0PWEIFaoCjkaKqmllWPAokiABUFHPQuZD9TLuKMSTV08aoJyXNhBceT2rbWkQQPeQJhAydrnQOhQ/nFmUqsyDhVAevAjB5X8in 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. Suggested-by: Matthew Wilcox Signed-off-by: Shakeel Butt Reviewed-by: Roman Gushchin Reviewed-by: Matthew Wilcox (Oracle) Acked-by: Johannes Weiner Reviewed-by: Muchun Song Acked-by: Michal Hocko --- mm/memcontrol.c | 34 +++++++--------------------------- 1 file changed, 7 insertions(+), 27 deletions(-) diff --git a/mm/memcontrol.c b/mm/memcontrol.c index e1ffd2950393..b8e6b98485c6 100644 --- a/mm/memcontrol.c +++ b/mm/memcontrol.c @@ -3363,29 +3363,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; } } @@ -3420,7 +3403,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 @@ -3519,11 +3502,10 @@ static struct mem_cgroup *mem_cgroup_alloc(struct mem_cgroup *parent) if (!memcg) return ERR_PTR(error); - 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; - } memcg->vmstats = kzalloc(sizeof(struct memcg_vmstats), GFP_KERNEL_ACCOUNT); @@ -3664,9 +3646,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: