From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-ot1-f41.google.com (mail-ot1-f41.google.com [209.85.210.41]) (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 4E861371D0A for ; Thu, 21 May 2026 03:22:54 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.41 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1779333777; cv=none; b=npcC7jaDd6AAKR327kw01zExQ2goZAnJeXEtXYgXu9AaZnpvWNKc66jLPcIpBZLNf2Q9cv93SrNVRuevAd/22qG2N2cx5KkUgCjyPfvIOypaMZRBi8iLQ3jh3HeYIVN94b6hQi3vtgcruL2mq437QaNzvblCtPs8/bMIIpAQ8ps= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1779333777; c=relaxed/simple; bh=2KM+WJfxfvhK3onCCgBv0kY15e0rpvyDbEWfA+CdVTQ=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=L1q1WLYyX2Yla5J2FkaSxnxrhqeIpjHmC1/l1Q633w9SpZYc7YX1BtqBPGw6bhXC2CMsXuIUpm+3ssHeobK3w0Mqn+4R/fJR3hgCiBWteJhKyF5LvQkTNRc9APfoIlapLq4uFEllaxVICj48G33SA0PVjhe9EBxDEO4+Iwh/PME= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=THT5j+78; arc=none smtp.client-ip=209.85.210.41 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="THT5j+78" Received: by mail-ot1-f41.google.com with SMTP id 46e09a7af769-7de46b8e432so5629969a34.1 for ; Wed, 20 May 2026 20:22:54 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1779333773; x=1779938573; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=RCEFUIphaYwH63nwZhxQG+TN7ecroRFNX1EgOEudxtY=; b=THT5j+780wOJG/bYq0x3r8ibCbf+Y7laAEsM+VlQotkoyNE1gAY+uefVMG4aiNqj9g 6Bdi9FZmM91p/nJMJPL0yPylGMiWFG6HLcuS0HNA5/X+RmGsK0c1KLmxydzkhyO2LdnI 7XLOexrvZ8s42Xt1Hp/VmqUzrmWaxtozzzHwgWVQNeUi4Uq64F9P0JPP+44UnWFLEOia r+eM76mjTtYG61C3EZ7ObVP0ovacJrhTffl0UcKz/DGr5L1vDlijMbgscJ+r5iC4/b/4 6DddXipQVM79LZu56Q1dJWEUc9A9RfZD9Ya8Awef/L6/FSS/oNzV6vVcr+ZywX87NDkw 6Z5Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1779333773; x=1779938573; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=RCEFUIphaYwH63nwZhxQG+TN7ecroRFNX1EgOEudxtY=; b=GPjmbK2sM4/x+h8arIbRW4BMbk1ydrjBuQf2yvUHOdlNwHsr/k3BpkqzpkxVG2xwyv UN9UACjD4KQbdDJA93DfwZNxQqinQayoTJiiTpWbKSEKn5CboSRfEGbhZqTC/tFUQJNu M/UGHH7GY61ERclZjv6O8NVc/VOBxbN0QzKiaFZaUF0jc2fGYc6SMp48Zwbw9H4gpf2Y oDQX9XFYsTJo29Uzk5Ja8bpY94ziBjiFAPltcu41Zm9EcH38aoUEKg4xMN9E2OoUSOna GeMAtojfTkL6dYz9FKXa1teIHREf78nN3kKc8Q2AYY8+yO0iBXWw1tUJuoQLn9yDFpqR nPYg== X-Forwarded-Encrypted: i=1; AFNElJ89xYQ1sl9+NWbC4g3bdgt2ANBG2Yt3OYZGvIoKoKBvbdQqj4hjcyVhJEBCf64tPNoL7c4OCnK4C2VVaa0=@vger.kernel.org X-Gm-Message-State: AOJu0YzSPzo/myDFVqiCsdZDVtkkzX/DMQ2AavXiYFQSunYMuy8MBn+5 IHH6N4F54IytszzPPGZliLQsB8X/EVvZ64KL/bKMZ2CHIQEvpwbuan+j X-Gm-Gg: Acq92OF/9j9a3Hd9sFyeV3db9Dn3SNGW+jfM4SjAITSOD4uEC8i7eIhR/ULqTfFSKr3 tP45aRYS+HiCp5F2nu/LGp3fPeMVtCLDzKOYZ+7etjtIEl5Kw0p+WSDjbxd5eHFkiT3lEiPHH9/ gb+ClnkaxfNn2brhNPzZ+6M0qt75oEEe0XmgL8aG2ECm6mcG2XcLZ0FPZebhnP+/NKxxFbqJy1J FWrhSH+h10FC5mumdMVu8y0E551ZmKmKrfS5rpmMsgT1vY6iDYM15G9gcedBBHkLby5MtZxMeX3 x4eJMLXulgj69t3gO2ZzjjwKxnSB0RMAOvcBQJBJkK3AS4uSuflRv/f7YbngH3ADCfRjRaeSVAu hVo/l2oFrMKEcQ43WB6hc6iMPFwT/S6a71G2NPZZrineQ0g4FODZHlnf0LaVwCbLrePrIoaNQ6x 1DeU4Ss3lvv6bKP+vbScRkflNNYxSc8CyEQ2PxORuZwEPTn+k1Auy9 X-Received: by 2002:a05:6830:6881:b0:7dc:3db6:f02 with SMTP id 46e09a7af769-7e5ec271d04mr523258a34.9.1779333773170; Wed, 20 May 2026 20:22:53 -0700 (PDT) Received: from localhost ([2a03:2880:10ff:6::]) by smtp.gmail.com with ESMTPSA id 46e09a7af769-7e55b7c68d6sm13479796a34.3.2026.05.20.20.22.51 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 20 May 2026 20:22:52 -0700 (PDT) From: Joshua Hahn To: Shakeel Butt Cc: Harry Yoo , Andrew Morton , Johannes Weiner , Michal Hocko , Roman Gushchin , Muchun Song , Qi Zheng , Alexandre Ghiti , Meta kernel team , linux-mm@kvack.org, cgroups@vger.kernel.org, linux-kernel@vger.kernel.org, kernel test robot Subject: Re: [PATCH 4/4] memcg: multi objcg charge support Date: Wed, 20 May 2026 20:22:49 -0700 Message-ID: <20260521032250.3853564-1-joshua.hahnjy@gmail.com> X-Mailer: git-send-email 2.52.0 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit On Wed, 20 May 2026 18:05:23 -0700 Shakeel Butt wrote: > On Wed, May 20, 2026 at 06:35:30PM +0900, Harry Yoo wrote: > > > > > > On 5/20/26 2:31 PM, Shakeel Butt wrote: > > > Commit 01b9da291c49 ("mm: memcontrol: convert objcg to be per-memcg > > > per-node type") split a memcg's single obj_cgroup into one per NUMA > > > node so that reparenting LRU folios can take per-node lru locks. As a > > > side effect, the per-CPU obj_stock_pcp -- which caches exactly one > > > cached_objcg -- thrashes on workloads where threads of the same memcg > > > run on different NUMA nodes. The kernel test robot reported a 67.7% > > > regression on stress-ng.switch.ops_per_sec from this pattern. > > > > > > Mirror the multi-slot pattern already used by memcg_stock_pcp: turn > > > nr_bytes and cached_objcg into NR_OBJ_STOCK-element arrays, scan all > > > slots on consume/refill/account, prefer empty slots when inserting, > > > and evict a random slot only when full. With multiple slots a CPU can > > > hold the per-node objcg variants of one memcg plus a few siblings > > > without ever forcing a drain. > > > > > > A single int8_t index records which slot the cached slab stats belong > > > to; the stats are flushed on slot or pgdat change. With NR_OBJ_STOCK > > > = 5 the layout (verified with pahole) is: > > > > > > offset 0 : lock(1) + index(1) + node_id(2) + slab stats(4) = 8B > > > offset 8 : nr_bytes[5] = 10B > > > offset 18 : padding = 6B > > > offset 24 : cached[5] = 40B > > > offset 64 : (line 2) work_struct + flags (cold) > > > > > > so consume_obj_stock, refill_obj_stock and the slab account path each > > > touch exactly one 64-byte cache line on non-debug 64-bit builds. > > > > > > Reported-by: kernel test robot > > > Closes: https://lore.kernel.org/oe-lkp/202605121641.b6a60cb0-lkp@intel.com > > > Fixes: 01b9da291c49 ("mm: memcontrol: convert objcg to be per-memcg per-node type") > > > Signed-off-by: Shakeel Butt > > > Tested-by: kernel test robot > > > --- > > > @@ -3350,19 +3405,45 @@ static void __refill_obj_stock(struct obj_cgroup *objcg, > > > goto out; > > > } > > > - stock_nr_bytes = stock->nr_bytes; > > > - if (READ_ONCE(stock->cached_objcg) != objcg) { /* reset if necessary */ > > > - drain_obj_stock(stock); > > > + for (i = 0; i < NR_OBJ_STOCK; ++i) { > > > + struct obj_cgroup *cached = READ_ONCE(stock->cached[i]); > > > + > > > + if (!cached) { > > > + if (empty_slot == -1) > > > + empty_slot = i; > > > + continue; > > > + } > > > + if (cached == objcg) { > > > + slot = i; > > > + break; > > > + } > > > + } > > > + > > > + if (slot == -1) { > > > + slot = empty_slot; > > > + if (slot == -1) { > > > + slot = get_random_u32_below(NR_OBJ_STOCK); > > > > It would break kmalloc_nolock() because _get_random_bytes() uses a spinlock. > > perhaps prandom_u32_state() should be sufficient in this case. > > > Is there a reason why it uses random eviction, unlike multi-memcg percpu > > charge cache? > > Oh I didn't know and actually we are already using get_random_u32_below() in > refill_stock(). So, it need fixing as well. That would be a separate patch. Hello Harry and Shakeel! I just wanted to note that my work to push the memcg stock to page_counter [1] actually gets rid of the random eviction and also the get_random_u32_below. So the problem will no longer exist, at least for memcg/page_counter stock. I was also thinking whether there was any way we can abstract or push down the objcg stock to a different per_X slot (with finer granularity than per-node) so we don't do random evictions, but it doesn't seem like there's a natural point to do this from a quick glance. The fix for get_random_32_below to use prandom_u32_state seems quite simple though, so if either of you would like to send that in as a small hotfix it shouldn't be a big deal to rebase on top of that instead too. (Sorry for joining the conversation late too. I'll take a closer look at this entire series tomorrow as well). Thank you both. Have a great day! Joshua [1] https://lore.kernel.org/all/20260410210742.550489-1-joshua.hahnjy@gmail.com/