From nobody Sat Apr 4 01:48:49 2026 Received: from mail-dl1-f74.google.com (mail-dl1-f74.google.com [74.125.82.74]) (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 611DB23507B for ; Sat, 21 Mar 2026 03:35:19 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=74.125.82.74 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1774064120; cv=none; b=A3PkyzcPPKC8OydO9etijEk0Ve+9X21gAyYFVP/mEwSYdM1pw0DVnUJVNDas08QaqksjbKOVx6ticjMKz2oPZ2KomIV1+y8QMgWJI1bLj2VU9go5UEPAzm1fmSYGk38zXVKRRapPm/GGitcdjO6LPNfYkAVHOY+S2MJrddtQsV0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1774064120; c=relaxed/simple; bh=96B7pb5M0XiruGqfKkKfkAko2qpTlFcP4SKbRs28Wgo=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=jdkZy4Iv7yHaLLMDI+p7PdVlWNAtPECCMvX0E0xiyAs4ZtWLwOnP9MeVl68lPvPVR6f0h26mMlvkHykanFuN/AmCfVOdcXbcILqs8iSQWUN+i5bfNKuJUmHMakjoabVL5MZs+PfwdN+prNYTIlUYoXqVnPqFT1FO2bmw1sYoI88= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--bingjiao.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=TiiIokb7; arc=none smtp.client-ip=74.125.82.74 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--bingjiao.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="TiiIokb7" Received: by mail-dl1-f74.google.com with SMTP id a92af1059eb24-127337c8e52so72928015c88.1 for ; Fri, 20 Mar 2026 20:35:19 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1774064118; x=1774668918; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=AoMBtlhfnp9XRcs7RdoT2ynBQbMVoKkeC7D17jrPW5U=; b=TiiIokb7r2JG6VUOYJwsD5NumP7Y2EUqF67ZeT17OYe7mbqCUGt68knko571W5Nvya enP0ZCOSWJpD6zFgnSgjidc8Jac35BySQ7atl1LDNNVokOqwUCyDTnZBPTSYdIlZnJoR JQvVPJECEanRbvhKXTNQAAPxDzHKsH7iHob49eJcUz2ABr8d15Z3AUIY/z6HR6kO55Ov tOuj5r7TrIVx0YPG9zxpVT11P0dQWiUllqLBYr79eZ6VeWXZ4hleppUKJ7mG/TqW3vub BxwIGT8o9r1xW1e1iE/WLjK5pTq6MXp1vo2uoFjVBEeTgSxMqAC2WPEOJ/YOa3YH5Vis axlQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1774064118; x=1774668918; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=AoMBtlhfnp9XRcs7RdoT2ynBQbMVoKkeC7D17jrPW5U=; b=qRZ6ClY4l3vGCaDXdnA4vf2K2gw9VS4D/QvqIa+Zy22LmbGOUbvobZ+ufbc+ByPjBw 6nBg5YFfSJi35OqBEWmqukzqpZvgZQkTGvu0S0y71ZpKl0WpOmLkM+wYA6X9sO+63JyT r18W4b82Yfuy+r4iTZaqlJr3cPg99Ymf7N+zOLj4uyvFtuU49lS3mCCzfk2UEFsr0n4g XzJtctjngNgsjGEfYiIa541k33EkVykLSLjm7xcetgQDD8kSisarH6tadP/CCnwnqoC4 yy2osalFJo9QYPXXb7gqX3E6h1yuw+5s25S4+6brs/ZtJd0ZKigRkIBoLgsz2jEpYSU2 t09Q== X-Forwarded-Encrypted: i=1; AJvYcCVvZ5K1oK+v5lZS/+WjUQ5oMaDiALKu14+l7A+l8qMeTpnMy9Sn94noE6ws5/cFEG28N3Gp+asJtCMaAZI=@vger.kernel.org X-Gm-Message-State: AOJu0YywCt1bRyxBJLeTiBE8xV2iA6JKm6lvvZuPAQaV96f5a37YYvvi 3rmzk2Fpx5Y/Ao+RGHErBjoDJaev4Hs9Z2YIfq23fiH1ZY9afTPTWgPWHBHJHDVPOxpZeaT02Lx aciojWOMXfZxpuw== X-Received: from dlak23.prod.google.com ([2002:a05:701b:2917:b0:128:d29c:ddaa]) (user=bingjiao job=prod-delivery.src-stubby-dispatcher) by 2002:a05:7022:2525:b0:128:cedb:33c6 with SMTP id a92af1059eb24-12a7266088amr3054155c88.16.1774064118178; Fri, 20 Mar 2026 20:35:18 -0700 (PDT) Date: Sat, 21 Mar 2026 03:34:13 +0000 In-Reply-To: <20260318221957.2979346-1-bingjiao@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20260318221957.2979346-1-bingjiao@google.com> X-Mailer: git-send-email 2.53.0.959.g497ff81fa9-goog Message-ID: <20260321033500.2558070-1-bingjiao@google.com> Subject: [PATCH v4] mm/memcontrol: fix reclaim_options leak in try_charge_memcg() From: Bing Jiao To: bingjiao@google.com Cc: akpm@linux-foundation.org, axelrasmussen@google.com, baohua@kernel.org, bhe@redhat.com, cgroups@vger.kernel.org, chrisl@kernel.org, david@kernel.org, hannes@cmpxchg.org, joshua.hahnjy@gmail.com, kasong@tencent.com, linux-kernel@vger.kernel.org, linux-mm@kvack.org, ljs@kernel.org, mhocko@kernel.org, muchun.song@linux.dev, nphamcs@gmail.com, rientjes@google.com, roman.gushchin@linux.dev, shakeel.butt@linux.dev, shikemeng@huaweicloud.com, weixugc@google.com, yosry@kernel.org, youngjun.park@lge.com, yuanchu@google.com, zhengqi.arch@bytedance.com, Michal Hocko Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" In try_charge_memcg(), the 'reclaim_options' variable is initialized once at the start of the function. However, the function contains a retry loop. If reclaim_options were modified during an iteration (e.g., by encountering a memsw limit), the modified state would persist into subsequent retries. This leads to incorrect reclaim behavior. Specifically, MEMCG_RECLAIM_MAY_SWAP is cleared when the combined memcg->memsw limit is reached. After reclaimation attemps, a subsequent retry may successfully charge memcg->memsw but fail on the memcg->memory charge. In this case, swapping should be permitted, but the carried-over state prevents it. This issue was identified during code reading of try_charge_memcg() while analyzing memsw limit behavior in tiered-memory systems; no production failures have been reported yet. Fix by moving the initialization of 'reclaim_options' inside the retry loop, ensuring a clean state for every reclaim attempt. Fixes: 6539cc053869 ("mm: memcontrol: fold mem_cgroup_do_charge()") Signed-off-by: Bing Jiao Reviewed-by: Yosry Ahmed Acked-by: Michal Hocko Acked-by: Johannes Weiner --- v4: - Clarify in the commit message that the issue was found via code reading (Michal). - Add ACKs (Michal and Johannes). mm/memcontrol.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/mm/memcontrol.c b/mm/memcontrol.c index a47fb68dd65f..303ac622d22d 100644 --- a/mm/memcontrol.c +++ b/mm/memcontrol.c @@ -2558,7 +2558,7 @@ static int try_charge_memcg(struct mem_cgroup *memcg,= gfp_t gfp_mask, struct page_counter *counter; unsigned long nr_reclaimed; bool passed_oom =3D false; - unsigned int reclaim_options =3D MEMCG_RECLAIM_MAY_SWAP; + unsigned int reclaim_options; bool drained =3D false; bool raised_max_event =3D false; unsigned long pflags; @@ -2572,6 +2572,7 @@ static int try_charge_memcg(struct mem_cgroup *memcg,= gfp_t gfp_mask, /* Avoid the refill and flush of the older stock */ batch =3D nr_pages; + reclaim_options =3D MEMCG_RECLAIM_MAY_SWAP; if (!do_memsw_account() || page_counter_try_charge(&memcg->memsw, batch, &counter)) { if (page_counter_try_charge(&memcg->memory, batch, &counter)) -- 2.53.0.959.g497ff81fa9-goog