From nobody Tue Mar 3 05:23:43 2026 Received: from mail-qk1-f180.google.com (mail-qk1-f180.google.com [209.85.222.180]) (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 A0001314B8C for ; Mon, 2 Mar 2026 19:53:13 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.222.180 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1772481195; cv=none; b=jpPntzOp9sY+Q6JUIrYenRwR9rDRbi87yl4HJrdL0e/h406Q4y5x+ysLeBJUMqcbabNPtoncHmJb8opByrDhLEAqHnEJz6Clh1HlOoOJfaHtnWAd+IWFC6CvjVJLmTYO9TPxvCxcPSkE+sIRcYK9DCgmnFKy5q9ICRoSySWz06E= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1772481195; c=relaxed/simple; bh=I+F9Yqfxg7lyOH6Y6lDKdBT3X45kJ6gN2c857wyMTEg=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=SDnj3hevqapLKI9Cd8uxfck+sv7voQssVXOrfFLR++sHSxgoeArs5Kcn0+jEUOIsAMezqSRD+S4rW10jCcDJheMgxopfO3/xWfTLcM+dRAip6CuuVQAWKUNwok8rZav1jCxX80nUto0LozgXECxvgeqHvut56ITHE/vPjiM0+as= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=cmpxchg.org; spf=pass smtp.mailfrom=cmpxchg.org; dkim=pass (2048-bit key) header.d=cmpxchg.org header.i=@cmpxchg.org header.b=d/S0SdA2; arc=none smtp.client-ip=209.85.222.180 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=cmpxchg.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=cmpxchg.org Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=cmpxchg.org header.i=@cmpxchg.org header.b="d/S0SdA2" Received: by mail-qk1-f180.google.com with SMTP id af79cd13be357-8cb40149037so503527485a.2 for ; Mon, 02 Mar 2026 11:53:13 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cmpxchg.org; s=google; t=1772481192; x=1773085992; 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=5m4nFf6dF08qw2hzXIDF9i+2wZFYfD7fQfBYIdzHPvQ=; b=d/S0SdA218kpr3WEz6Jfp4A08F9qCQyZo6qWjiKGVb2TInbyyuzb3p7GjmGO/+ZzlA kdTQLFmDSA+z5xkDmOmcwSKXW/6ELLYBkOuwQCaT/kuTndGs/q/oRwPXbSrU6oLO0OUC OYS8S9CJc8s9Jq0ajufCdcuj6iR1H4vTvOZoQIrRh3cu+/vmwPzDff6AlulOnxv1ytQm VFVT70CH/ANernkwQMoUnmqGLqQaemujiMJXnO68s+KMCn9Sm/lD2wmcA/8hkPiYf4az /bfRf0gMK4hQJFkd0GqLul6A58TnQ7EY34xX4lzf+AHu69dG4fIYx/EyP8statVf+FsG F3/g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1772481192; x=1773085992; 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=5m4nFf6dF08qw2hzXIDF9i+2wZFYfD7fQfBYIdzHPvQ=; b=ny9fCJA2HLydrl+YfBjKUER/5ozVH8ONvXQCxmdTZM7yaAEFtYSd35GUoHrwAmGhdm xU8BPYUQH5gIolacS1u2rJcfIPEd1RQnGE7sr5E3wbhKMsnQB6kdcfrDwEAHD+lgS9Wk iEjaIL6DcaYmh4YITib7w2PYKBJt+T/DqsGKmR8Tm0o+eWNn6Bj/+U5k4NKL1EAaYWR6 +x1gG3W3ffxzfJCe05L+glftNeu2LGeLrFYqUHcIlgeEOBo2nY17/+EeHv5DDXaoI6bh 37iTICMrheZsfwPogvJK49vWAimEb7CI4YQRS/5XWM4ZCuvLAuUOyIpNwg9ogT1WaOtp arDA== X-Forwarded-Encrypted: i=1; AJvYcCVPKImiRhHdaoEbJNSWm1ikmjVqDJHkamF14QseEkNXa80AVRTPT22ifQ0K2cLyiAzltjDDoMrI+94yH7U=@vger.kernel.org X-Gm-Message-State: AOJu0YwmZY5TqELMZCLpaujOf5VC3ouQBn+4Co4G52qVR7YU5k8v9536 mQLxoYiRVLCSLayEA/o82tN5+7+tqnrRjNMCoxaRVS3DfhwTth6rNrRTSCKX+X1vYbo= X-Gm-Gg: ATEYQzzlURkke/ngWldCcGRgaIAy+9O7VOavrBzX+elTwaq1OHiFE52WtdgN/DjMZ1p xEqvVD9I1CBFfOQDG3Qr+ylaxgUyfmCbw7bA1/hVMZGfeb/Vo94eTB3V7be8ln2mx2TKf5gDZl3 o4Jqspk4yuYX+rKhl7/oJO270561x/AhZkZrHDn1HZH11s/mOdbCg90j8f5tMXxOfR5E5gKSLQ/ I/werUjfNsasJ0udzJA7xAMN0x0keItumV49WEWtwtbtKhWTYQrYqUNSBTq13NAevyGzdO5YwcJ o7TB8B3G3o8cduUzLcjybrlcCIYw4G1HurbXManQhuQP93UTbHk9cVm880rw+/LxCzNDLIP22yL AOEjKz4XDU+PBenBra8sfkw2PYf2BTjxUcTxMQvmEnsqqTX8UklQGI1OynWPcTsDGjKkaU3NT4b Nnc1nQU9s3/kHwG6neBgVLOg== X-Received: by 2002:a05:620a:254b:b0:8a2:3be9:1d79 with SMTP id af79cd13be357-8cbc8d7a9camr1778134585a.18.1772481192477; Mon, 02 Mar 2026 11:53:12 -0800 (PST) Received: from localhost ([2603:7000:c00:3a00:365a:60ff:fe62:ff29]) by smtp.gmail.com with ESMTPSA id af79cd13be357-8cbbf652bb6sm1307822585a.4.2026.03.02.11.53.11 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 02 Mar 2026 11:53:11 -0800 (PST) From: Johannes Weiner To: Andrew Morton Cc: Hao Li , Michal Hocko , Roman Gushchin , Shakeel Butt , Vlastimil Babka , Harry Yoo , linux-mm@kvack.org, cgroups@vger.kernel.org, linux-kernel@vger.kernel.org, Johannes Weiner Subject: [PATCH 1/5] mm: memcg: factor out trylock_stock() and unlock_stock() Date: Mon, 2 Mar 2026 14:50:14 -0500 Message-ID: <20260302195305.620713-2-hannes@cmpxchg.org> X-Mailer: git-send-email 2.53.0 In-Reply-To: <20260302195305.620713-1-hannes@cmpxchg.org> References: <20260302195305.620713-1-hannes@cmpxchg.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Johannes Weiner Consolidate the local lock acquisition and the local stock lookup. This allows subsequent patches to use !!stock as an easy way to disambiguate the locked vs. contended cases through the callstack. Signed-off-by: Johannes Weiner Acked-by: Shakeel Butt --- mm/memcontrol.c | 25 +++++++++++++++++++------ 1 file changed, 19 insertions(+), 6 deletions(-) diff --git a/mm/memcontrol.c b/mm/memcontrol.c index 753d76e96cc6..a975ab3aee10 100644 --- a/mm/memcontrol.c +++ b/mm/memcontrol.c @@ -3208,6 +3208,19 @@ void __memcg_kmem_uncharge_page(struct page *page, i= nt order) obj_cgroup_put(objcg); } =20 +static struct obj_stock_pcp *trylock_stock(void) +{ + if (local_trylock(&obj_stock.lock)) + return this_cpu_ptr(&obj_stock); + + return NULL; +} + +static void unlock_stock(struct obj_stock_pcp *stock) +{ + local_unlock(&obj_stock.lock); +} + static void __account_obj_stock(struct obj_cgroup *objcg, struct obj_stock_pcp *stock, int nr, struct pglist_data *pgdat, enum node_stat_item idx) @@ -3263,10 +3276,10 @@ static bool consume_obj_stock(struct obj_cgroup *ob= jcg, unsigned int nr_bytes, struct obj_stock_pcp *stock; bool ret =3D false; =20 - if (!local_trylock(&obj_stock.lock)) + stock =3D trylock_stock(); + if (!stock) return ret; =20 - stock =3D this_cpu_ptr(&obj_stock); if (objcg =3D=3D READ_ONCE(stock->cached_objcg) && stock->nr_bytes >=3D n= r_bytes) { stock->nr_bytes -=3D nr_bytes; ret =3D true; @@ -3275,7 +3288,7 @@ static bool consume_obj_stock(struct obj_cgroup *objc= g, unsigned int nr_bytes, __account_obj_stock(objcg, stock, nr_bytes, pgdat, idx); } =20 - local_unlock(&obj_stock.lock); + unlock_stock(stock); =20 return ret; } @@ -3366,7 +3379,8 @@ static void refill_obj_stock(struct obj_cgroup *objcg= , unsigned int nr_bytes, struct obj_stock_pcp *stock; unsigned int nr_pages =3D 0; =20 - if (!local_trylock(&obj_stock.lock)) { + stock =3D trylock_stock(); + if (!stock) { if (pgdat) mod_objcg_mlstate(objcg, pgdat, idx, nr_acct); nr_pages =3D nr_bytes >> PAGE_SHIFT; @@ -3375,7 +3389,6 @@ static void refill_obj_stock(struct obj_cgroup *objcg= , unsigned int nr_bytes, goto out; } =20 - stock =3D this_cpu_ptr(&obj_stock); if (READ_ONCE(stock->cached_objcg) !=3D objcg) { /* reset if necessary */ drain_obj_stock(stock); obj_cgroup_get(objcg); @@ -3395,7 +3408,7 @@ static void refill_obj_stock(struct obj_cgroup *objcg= , unsigned int nr_bytes, stock->nr_bytes &=3D (PAGE_SIZE - 1); } =20 - local_unlock(&obj_stock.lock); + unlock_stock(stock); out: if (nr_pages) obj_cgroup_uncharge_pages(objcg, nr_pages); --=20 2.53.0