From nobody Mon Apr 6 15:50:54 2026 Received: from mail-dy1-f202.google.com (mail-dy1-f202.google.com [74.125.82.202]) (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 406BE31A7F6 for ; Wed, 18 Mar 2026 21:56:32 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=74.125.82.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1773870994; cv=none; b=FJXqhhwOHEjiRorQ8qA7dOx9zWKdw57wCSJWtyy40S+U7DXNVbFVmiLWutfxO8nc5p9wQZ0aPkJlYSQ06b5ECx8/AhUOmSEFyBJ5RTn6FnpiQNVvoU2KA0oNUaBmYdW6divM1ic6cQwid3wCUX3WtMKm/wtFvRfyt8bXNgIPocM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1773870994; c=relaxed/simple; bh=e0x8hojThMgnc4nAAWULGmSUzZ71a6Z6ijKXYo1/91E=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=oRgpFUv6qqoGZJu8E7VUE6MrRbO72sCikG9LBvVzk9Gp7AiTvekM+8ybn9v/p95bsOoLRb+sJZfqn5coojV81twUi0rmKNFYoHKMeYLnrh/zJdQFmj3h1zI7th3/brV1mO+BuPkHswmpu5MLyYfRjeUc2zWMeS3wPShByBb596A= 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=UdpMEsSa; arc=none smtp.client-ip=74.125.82.202 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="UdpMEsSa" Received: by mail-dy1-f202.google.com with SMTP id 5a478bee46e88-2c0ba59a830so380244eec.0 for ; Wed, 18 Mar 2026 14:56:32 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1773870991; x=1774475791; 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=ctmE5aFMWKkncy3sWCLL07LF9OqWa0rsovqCMz1tZkA=; b=UdpMEsSac49oOW8a+OrtTBdnrSuLTRFbnSHvzIoFUO3B42wSXmQAeQjm5SBt3hKhTV tLvoCcA5YmLnKY9vN9xx2n9rGfe6/wXnsDb5RiTbZ3PzRipcDrEon4fRK4mYIQov6nk8 PDlujiuyGRjBIY5YbWoKDht7tQ61xve1bBKDFbXC8gNi7LoiYjUHQu0jwYPnI7N7SIis lU7uMZxCu+tXCAlx0YDG1Da9UIOaktQmC6v3tD7tSmgQMIQEg1ujhOr/sVGGsOzAhq0o erQSzKOma/wDhf2YPyjQHg/LgG/661O662upsXmWbXVu7tS4Y2iUUOli35ETI8BWKMos E+pw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1773870991; x=1774475791; 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=ctmE5aFMWKkncy3sWCLL07LF9OqWa0rsovqCMz1tZkA=; b=Pswavy6aXXqrBBFItbacf51nZRu/2mrZ4jEhIV2Z0Za6sbeGWoUy6Gs3dZW/o8z6bf ITVcGzEhVHLhoDQ6Ex1tVNxkdELKc89w8VUVA/AIbqH66kQvZ8NS2Cs8p2GWht/IU6Li pu9W7hqXYQKAw4mW6SPAy0iZx3R5Z/VudrncLRstgmwP/BDw10RNl3gGx5A7e2zOKNNZ TpoL0rOdURVcWoQ6Alel79nN68B3PZAakpRUj5J7HwNlceU3jzQr6seUdHx2YBt9f2/y /7DiEnaMSwRb8AtXbRFnJBanhxw6sxFRbxtLAkwxWkXGykUoT7RRyFkWFd03uRRi/rSH 3VBg== X-Forwarded-Encrypted: i=1; AJvYcCVaE8jqwRpa9LG1W+3CdvPOKAfWOgGWuRGuLHSAcnaVagTHX4xyyvVuLCbtPALyYdzv4of233O2YxisXXk=@vger.kernel.org X-Gm-Message-State: AOJu0Yz6t6owbnb4AevveMUSyrXcfcfBTQ6pdG6NHt431fIEhNb95oid JJBs+MvlSN5LL5TAk51n/BfhEEWKIad0jKhQDkz215VXkp2FsQ5sO1SMJSOmH7rf7trSxeRyey8 SY4dSquSnCHQ3yA== X-Received: from dybuh11.prod.google.com ([2002:a05:7301:750b:b0:2be:82ee:95dc]) (user=bingjiao job=prod-delivery.src-stubby-dispatcher) by 2002:a05:7301:1285:b0:2c0:dd99:f0b with SMTP id 5a478bee46e88-2c0e5089246mr2321633eec.25.1773870990982; Wed, 18 Mar 2026 14:56:30 -0700 (PDT) Date: Wed, 18 Mar 2026 21:56:05 +0000 In-Reply-To: Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: X-Mailer: git-send-email 2.53.0.851.ga537e3e6e9-goog Message-ID: <20260318215629.2849052-1-bingjiao@google.com> Subject: [PATCH v2] 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 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. Fix by moving the initialization of 'reclaim_options' inside the retry loop, ensuring a clean state for every reclaim attempt. Fixes: 73b73bac90d9 ("mm: vmpressure: don't count proactive reclaim in vmpr= essure") Signed-off-by: Bing Jiao Reviewed-by: Yosry Ahmed --- v2: - Dropped other patches. - Refined commit message to clarify the impact of the leak (Yosry). - Added Reviewed-by tag. 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.851.ga537e3e6e9-goog