From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 41C2E606A322 for ; Thu, 3 Dec 2026 08:48:52 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=je/jQNb/ Received: by kanga.kvack.org (Postfix) id C66BD6B05BB; Thu, 3 Dec 2026 03:48:49 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id C3F2D6B05BD; Thu, 3 Dec 2026 03:48:49 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id B5CBD6B05BB; Thu, 3 Dec 2026 03:48:49 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id 8C21C6B05BB for ; Thu, 3 Dec 2026 03:48:49 -0500 (EST) Received: from smtpin20.hostedemail.com (lb01a-stub [10.200.18.249]) by unirelay05.hostedemail.com (Postfix) with ESMTP id 0CFCA405DA for ; Thu, 3 Dec 2026 08:48:49 +0000 (UTC) X-FDA: 85502046378.20.037C512 Received: from sea.source.kernel.org (sea.source.kernel.org [172.234.252.31]) by imf12.hostedemail.com (Postfix) with ESMTP id 564C64000A for ; Thu, 3 Dec 2026 08:48:47 +0000 (UTC) Authentication-Results: imf12.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20260515 header.b="je/jQNb/"; dmarc=pass (policy=quarantine) header.from=kernel.org; spf=pass (imf12.hostedemail.com: domain of sashiko-bot@kernel.org designates 172.234.252.31 as permitted sender) smtp.mailfrom=sashiko-bot@kernel.org ARC-Seal: i=1; a=rsa-sha256; d=hostedemail.com; s=arc-20220608; cv=none; t=1796287727; b=1phsmhTKd4fJdD+WCk8CMgsUFHdDsaIgEAdEVANJ2cxfZ54skagc1eyKRPLPUIgnmRCaFi adz+SMvVPCtYLiXJ+KCsFJyNVun0C7aYrjSVAXNddJcD/xu76BRut84MiDOT+0E5sF/YbA Ww3IuQt2bPUkuYtLyWwlfaUmOhdbTME= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1796287727; h=from:from:sender:reply-to:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=hXBOAw7GefsEpKBRSLh4yXp0sB5OEDUi75Jejz/ngtM=; b=Q5lTyNPTKa3Z+qEUKWTmvTyPn/YgZy1eHgUdieTIVnjThiefrxXbm74j+XVOfrlDl/NPtP YHEOurL9iI3LvQ7xZVqb/6O4TUqOV3iGhcozSL6CSu+/W/1vlqqFnsz/W3DKMZEGm/JTnj aBT6AvgGBdTxZV+VnabKqDCbxFaKLXc= ARC-Authentication-Results: i=1; imf12.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20260515 header.b="je/jQNb/"; dmarc=pass (policy=quarantine) header.from=kernel.org; spf=pass (imf12.hostedemail.com: domain of sashiko-bot@kernel.org designates 172.234.252.31 as permitted sender) smtp.mailfrom=sashiko-bot@kernel.org Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) by sea.source.kernel.org (Postfix) with ESMTP id 7D16440588; Thu, 3 Dec 2026 08:48:46 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id D28251F00893; Thu, 3 Dec 2026 08:48:45 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796287726; bh=hXBOAw7GefsEpKBRSLh4yXp0sB5OEDUi75Jejz/ngtM=; h=From:Subject:Reply-To:To:Cc:In-Reply-To:References:Date; b=je/jQNb/3ttj9/SWS0c9elx1x+OjQOIUpNpwoJcWJ9lLDpAtrHIcBdKrlRXpWR7SY ur+TajZMFP8UcV/yK61zdPTZJNj0oM/f00hSNz+XaR9cNhz0wbr7GMNJ7XE/90Mzpg t+0X07yO9xTvFIp4L8wbSo+076TE90s9YPnRzLJxmTPxtJ+xOe8jJnQg3F6Rs3RI9m yDmKVY/3GC5ZwqNAFAqsbIKAjG2mrFJlDN6xO0QQohh+HhfZTAbWLQGAhYxGgeo9nB CyoNAPFhR+YoblaDbMN1mHtKaVdgGxRdGPMcSmnGCa2UQHzHvuLTqJh5y3JGCgDHgR hYn+jc6KFEMzw== Message-ID: From: sashiko-bot@kernel.org Subject: Re: [PATCH v12 3/3] mm: swap: add debugfs to set memcg max swap tier Reply-To: sashiko-reviews@lists.linux.dev To: "Youngjun Park" , "Andrew Morton" , linux-mm@kvack.org Cc: "Johannes Weiner" , "Chris Li" , "Kairui Song" , "Kemeng Shi" , "Nhat Pham" , "Baoquan He" , "Barry Song" , "Shakeel Butt" , "Yosry Ahmed" , Lian In-Reply-To: <20261202084826.2131931-4-youngjun.park@lge.com> References: <20261202084826.2131931-1-youngjun.park@lge.com> <20261202084826.2131931-4-youngjun.park@lge.com> Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Date: Thu, 03 Dec 2026 08:48:45 +0000 X-Rspam-User: X-Rspamd-Queue-Id: 564C64000A X-Rspamd-Server: rspam12 X-Stat-Signature: cmok6seemrpwtnuwgzs1zhyxtg6banut X-Spam-Status: No, score=-10.8 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_PASS,SPF_PASS autolearn=ham autolearn_force=no version=4.0.1 X-HE-Tag: 1796287727-399498 X-HE-Meta: U2FsdGVkX1+txavQvkmXEXZ9aVr8M1ycrZRrOhPSqIZK4GonHebMOaYdkz1HtJBxLutkzUdgQebw32p+O4Zds2ikmYkfDm26vQbaNP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cRDQIy3BRZNoSE8vToe4/svoXaGJ+0fecOqy7FouazehlZN4GBRiua2Jdf/uPc5N05phkZjkbBn62GXbhx6vqUOx13+bAQU5GHWu/qDcH/6HBGoVxwR0jGfEKcNTH4NKti2H7KiB2oU1ga3NX8WM6telXczLvzoDPr8I5YZT5kjfB1w6rfkW1VYm2Tvw8BhspotyCEId3QyEqdybb8unDX+0zy+Lg9lu2aP3aj7TbRYtyh6iqvRMr3xtqcoeHNnaP5b0/h6kgYAoiMLvSw48Nh6NAakbySzJQn3IGdzT/DU/RiwY7xTzgnjFyoxgbeZ/IFQKsqbXvXVCLfEup/8PtOlki6M0HCR3yZRcLcHEIhQ6qCFxepXoW9KEHOEF1SykPDJ0MS875c03KQ1k2R2UCYdX0vNG65hkrFMS5bSfXQ2NM7+LDv9kUSUFBM16xmjddfKl7zeJCLgXnWA1lQj+sQX0X1TMy0ZYEJbJGGvYVfTD+RN/yAxoKeZZm4JOFebinXixNeg6S8aFlqYLV69JuwqhJV1PN3VYxj9UEcu1GD1HP77H1tN1eJ3inGiYZ+5WIY+iCIEIpE3Qzp3QHPYkFEaujPzFLwWe4QMb53VaC3V+c13jfkaTjn7OpTi/F8GJ3aPjn/MnDibgA28h1rSMOWVfYmgckhP9bFQsxanEr705qVUmd/U6SoAYPl4udGHqA= Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: X-Spam-Checker-Version: SpamAssassin 4.0.1 (2024-03-26) on aws-us-west-2-korg-listserv-alma10-1 X-Archived-By: Thank you for your contribution! Sashiko AI review found 4 potential issues to consider: - [High] mm: swap: tier limits break percpu_swap_cluster and exhaust free clusters - [Medium] mm: swap: cgroup_get_e_css() resolves disabled memcg to ancestor - [Medium] mm: swap: folio_alloc_swap() ignores memcg tier limit on fallback paths - [Low] mm: swap: SWAP_TIER_MAX sentinel collides with valid priority 32767 -- commit 2d5665a5aba7f63f5075e6bf592fe86a3b0bdc09 Author: Youngjun Park mm: swap: add debugfs to set memcg max swap tier This commit adds /sys/kernel/debug/swap/memcg_tiers to configure a per-memcg maximum swap tier (priority) and enforces the effective hierarchical limit = in swap_memcg_max_tier(). > diff --git a/mm/swapfile.c b/mm/swapfile.c > index d30ee41f82176..6acf177cb2dfc 100644 > --- a/mm/swapfile.c > +++ b/mm/swapfile.c [ ... ] > @@ -1439,14 +1441,219 @@ static inline short swap_tier(struct swap_info_s= truct *si) [ ... ] > +/* Highest swap tier the memcg of @folio may allocate from. */ > +static short swap_memcg_max_tier(struct folio *folio) > +{ > + struct mem_cgroup *memcg; > + struct memcg_swap_prio *r; > + short max =3D SWAP_TIER_MAX; > + > + if (mem_cgroup_disabled()) > + return max; > + > + rcu_read_lock(); > + if (list_empty(&memcg_prio_list)) > + goto out; > + > + for (memcg =3D folio_memcg(folio); memcg; > + memcg =3D parent_mem_cgroup(memcg)) { > + r =3D memcg_prio_find(mem_cgroup_id(memcg)); > + if (!r) > + continue; > + max =3D min_t(short, max, r->max); > + } > +out: > + rcu_read_unlock(); > + > + return max; > +} [Severity: High] When swap_memcg_max_tier() returns a limit below SWAP_TIER_MAX, how does swap allocation interact with percpu_swap_cluster on SWP_SOLIDSTATE devices? mm/swapfile.c:swap_alloc_fast() { ... /* The per-CPU cluster is shared by all memcgs, so skip it if limited. */ if (swap_memcg_max_tier(folio) !=3D SWAP_TIER_MAX) return false; ... } Every swap allocation from a limited memcg skips swap_alloc_fast() even if percpu_swap_cluster.si[order] already satisfies the tier limit, falling back to swap_alloc_slow() -> cluster_alloc_swap_entry(). On SWP_SOLIDSTATE devices, cluster_alloc_swap_entry() isolates a fresh free cluster from si->free_clusters, allocates one folio, moves the cluster (with 511 free slots remaining) to si->frag_clusters[ci->order] in relocate_cluster(), and stores the next offset in percpu_swap_cluster. Because the next allocation from the limited memcg skips swap_alloc_fast() again, it abandons that cluster and isolates another free cluster, draining all free clusters on the device at 1 folio per cluster (0.2% usage for order-0 pages) and causing large-folio swap allocations to fail with -E2BIG. In addition, alloc_swap_scan_cluster() overwrites percpu_swap_cluster.si[order] with the lower-tier device, so subsequent allocations on the same CPU from unlimited memcgs pass the check in swap_alloc_fast() and allocate from the lower-tier device instead of the highest-priority tier. > + > +static void memcg_prio_free(struct memcg_swap_prio *r) > +{ > + list_del_rcu(&r->node); > + kfree_rcu(r, rcu); > +} > + > +static int memcg_prio_parse(const char *tok, short *val) > +{ > + if (!strcmp(tok, "max")) { > + *val =3D SWAP_TIER_MAX; > + return 0; > + } > + > + return kstrtos16(tok, 0, val); > +} [ ... ] > +/* Set (or remove, for SWAP_TIER_MAX) the max tier of memcg @id. */ > +static int memcg_prio_set(u64 id, short max) > +{ > + struct memcg_swap_prio *r; > + int ret =3D 0; > + > + mutex_lock(&memcg_prio_lock); > + r =3D memcg_prio_find(id); > + if (max =3D=3D SWAP_TIER_MAX) { > + if (r) > + memcg_prio_free(r); > + else > + ret =3D -ENOENT; [Severity: Low] Does using SWAP_TIER_MAX (SHRT_MAX, 32767) as the removal sentinel in memcg_prio_set() collide with valid swap priority 32767? Because swapon(2) accepts priorities up to SWAP_FLAG_PRIO_MASK (32767) and memcg_prio_parse() accepts "32767" via kstrtos16(), writing " 32767" removes the limit (or fails with -ENOENT) instead of setting the maximum tier to 32767. [ ... ] > +/* > + * " " sets the limit, " max" remove= s it. > + * An entry of a removed cgroup is shown as [removed:] and can only = be > + * dropped by " max". > + */ > +static ssize_t memcg_prio_write(struct file *file, const char __user *ub= uf, > + size_t count, loff_t *ppos) > +{ [ ... ] > + if (*target =3D=3D '/') { > + cgrp =3D cgroup_get_from_path(target); > + if (IS_ERR(cgrp)) { > + ret =3D PTR_ERR(cgrp); > + goto out; > + } > + css =3D cgroup_get_e_css(cgrp, &memory_cgrp_subsys); > + cgroup_put(cgrp); > + if (!css) > + goto out; > + id =3D mem_cgroup_id(mem_cgroup_from_css(css)); > + css_put(css); [Severity: Medium] What happens here when the memory controller is not enabled on the target cgroup? cgroup_get_e_css() returns the effective CSS of the nearest ancestor with memory enabled (falling back to the root memcg), so id becomes the ancestor's cgroup ID and memcg_prio_set() silently modifies or removes the ancestor's tier limit. Similarly, memcg_prio_show() uses mem_cgroup_get_from_id(r->id), which also calls cgroup_get_e_css() and will print the ancestor's path if memory is disabled on that cgroup. Checking css->cgroup =3D=3D cgrp (or mem_cgroup_id(memcg) =3D=3D r->id) would avoid = operating on an ancestor. [ ... ] > @@ -1495,7 +1702,7 @@ static void swap_alloc_slow(struct folio *folio) > spin_lock(&swap_avail_lock); > start_over: > plist_for_each_entry_safe(si, next, &swap_avail_head, avail_list) { > - /* Skip tiers above the memcg limit. */ > + /* Skip tiers (priorities) above the memcg limit. */ > if (swap_tier(si) > max) > continue; [Severity: Medium] When all swap devices at or below a memcg's max tier are full (or none exis= t) while a higher-tier device still has free space, does folio_alloc_swap() handle the failure paths in a tier-aware way? mm/swapfile.c:folio_alloc_swap() { ... if (!order && unlikely(!folio_test_swapcache(folio))) { if (swap_sync_discard()) goto again; } ... failed: if (get_nr_swap_pages() <=3D 0) return -ENOSPC; if (mem_cgroup_get_folio_swap_margin(folio) <=3D 0) return -ENOMEM; return order ? -E2BIG : -ENOMEM; } For large folios (order > 0), get_nr_swap_pages() is still positive because of the higher-tier device, so folio_alloc_swap() returns -E2BIG and causes shrink_folio_list() to split every large folio in the limited memcg even though order-0 swap allocation cannot use the higher tier either. For order-0 folios, swap_sync_discard() discards pending clusters on any device in swap_active_head without checking the folio's tier limit, looping back to again until all pending discards on disallowed higher-tier devices are drained. --=20 Sashiko AI review =C2=B7 https://sashiko.dev/#/patchset/20261202084826.2131= 931-1-youngjun.park@lge.com?part=3D3