From nobody Sun Dec 14 13:45:40 2025 Received: from shelob.surriel.com (shelob.surriel.com [96.67.55.147]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 1C683213E6B; Thu, 12 Dec 2024 16:59:05 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=96.67.55.147 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1734022755; cv=none; b=P2mPSMRIq5zlnF1+yVDra5M8K1Re0CYDkS9/NuPKo9X3apPe2fqh8kCsF16571eIY7wpScjLla1/q97sh+q+tPpfXtU0e14I7yvjzdMEaAtXf5GHj+uoezvTv7t5hF1nUaCUZrmTe9bJ2SA6PQCWvd1jGi8cYHFuOdLACGqlgFQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1734022755; c=relaxed/simple; bh=UoVn7YgqG67MmpzHVdBzGefEpiXVqwUID793qHLxEb0=; h=Date:From:To:Cc:Subject:Message-ID:MIME-Version:Content-Type; b=GXAvtfckz5+aQ/hvNzkU6B3E4AsOjV7QwDqG32kjzS23qbIvfZHUrSLORuaDU7xtArmsCWOIPmWWeypNL/LuEly7nvodUyTflw0uSciKYRtA0NjUnXLq8hg8tyRZ/dw0tTz0a0f5rEayF5awY0oNEy7HUldQtJWYjq+LThsjoao= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=surriel.com; spf=pass smtp.mailfrom=shelob.surriel.com; arc=none smtp.client-ip=96.67.55.147 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=surriel.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=shelob.surriel.com Received: from [2601:18c:9101:a8b6:82e7:cf5d:dfd9:50ef] (helo=fangorn) by shelob.surriel.com with esmtpsa (TLS1.2) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.97.1) (envelope-from ) id 1tLmVe-0000000073q-41Zi; Thu, 12 Dec 2024 11:57:55 -0500 Date: Thu, 12 Dec 2024 11:57:54 -0500 From: Rik van Riel To: Yosry Ahmed Cc: Balbir Singh , Johannes Weiner , Michal Hocko , Roman Gushchin , hakeel Butt , Muchun Song , Andrew Morton , cgroups@vger.kernel.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org, kernel-team@meta.com, Nhat Pham Subject: [PATCH v2] memcg: allow exiting tasks to write back data to swap Message-ID: <20241212115754.38f798b3@fangorn> X-Mailer: Claws Mail 4.3.0 (GTK 3.24.43; x86_64-redhat-linux-gnu) 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 Sender: riel@surriel.com Content-Type: text/plain; charset="utf-8" A task already in exit can get stuck trying to allocate pages, if its cgroup is at the memory.max limit, the cgroup is using zswap, but zswap writeback is enabled, and the remaining memory in the cgroup is not compressible. This seems like an unlikely confluence of events, but it can happen quite easily if a cgroup is OOM killed due to exceeding its memory.max limit, and all the tasks in the cgroup are trying to exit simultaneously. When this happens, it can sometimes take hours for tasks to exit, as they are all trying to squeeze things into zswap to bring the group's memory consumption below memory.max. Allowing these exiting programs to push some memory from their own cgroup into swap allows them to quickly bring the cgroup's memory consumption below memory.max, and exit in seconds rather than hours. Signed-off-by: Rik van Riel --- v2: use mm_match_cgroup as suggested by Yosry Ahmed mm/memcontrol.c | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/mm/memcontrol.c b/mm/memcontrol.c index 7b3503d12aaf..ba1cd9c04a02 100644 --- a/mm/memcontrol.c +++ b/mm/memcontrol.c @@ -5371,6 +5371,18 @@ bool mem_cgroup_zswap_writeback_enabled(struct mem_c= group *memcg) if (!zswap_is_enabled()) return true; =20 + /* + * Always allow exiting tasks to push data to swap. A process in + * the middle of exit cannot get OOM killed, but may need to push + * uncompressible data to swap in order to get the cgroup memory + * use below the limit, and make progress with the exit. + */ + if (unlikely(current->flags & PF_EXITING)) { + struct mm_struct *mm =3D READ_ONCE(current->mm); + if (mm && mm_match_cgroup(mm, memcg)) + return true; + } + for (; memcg; memcg =3D parent_mem_cgroup(memcg)) if (!READ_ONCE(memcg->zswap_writeback)) return false; --=20 2.47.0