]> git.ipfire.org Git - thirdparty/linux.git/commitdiff
vmscan,cgroup: apply mems_effective to reclaim
authorGregory Price <gourry@gourry.net>
Thu, 24 Apr 2025 20:28:06 +0000 (16:28 -0400)
committerAndrew Morton <akpm@linux-foundation.org>
Tue, 13 May 2025 06:50:33 +0000 (23:50 -0700)
It is possible for a reclaimer to cause demotions of an lruvec belonging
to a cgroup with cpuset.mems set to exclude some nodes.  Attempt to apply
this limitation based on the lruvec's memcg and prevent demotion.

Notably, this may still allow demotion of shared libraries or any memory
first instantiated in another cgroup.  This means cpusets still cannot
cannot guarantee complete isolation when demotion is enabled, and the docs
have been updated to reflect this.

This is useful for isolating workloads on a multi-tenant system from
certain classes of memory more consistently - with the noted exceptions.

Note on locking:

The cgroup_get_e_css reference protects the css->effective_mems, and calls
of this interface would be subject to the same race conditions associated
with a non-atomic access to cs->effective_mems.

So while this interface cannot make strong guarantees of correctness, it
can therefore avoid taking a global or rcu_read_lock for performance.

Link: https://lkml.kernel.org/r/20250424202806.52632-3-gourry@gourry.net
Signed-off-by: Gregory Price <gourry@gourry.net>
Suggested-by: Shakeel Butt <shakeel.butt@linux.dev>
Suggested-by: Waiman Long <longman@redhat.com>
Acked-by: Tejun Heo <tj@kernel.org>
Acked-by: Johannes Weiner <hannes@cmpxchg.org>
Reviewed-by: Shakeel Butt <shakeel.butt@linux.dev>
Reviewed-by: Waiman Long <longman@redhat.com>
Cc: Michal Hocko <mhocko@kernel.org>
Cc: Michal Koutný <mkoutny@suse.com>
Cc: Muchun Song <muchun.song@linux.dev>
Cc: Roman Gushchin <roman.gushchin@linux.dev>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Documentation/ABI/testing/sysfs-kernel-mm-numa
include/linux/cpuset.h
include/linux/memcontrol.h
kernel/cgroup/cpuset.c
mm/memcontrol.c
mm/vmscan.c

index 77e559d4ed800ff92275998a00f5b34ebecb171f..90e375ff54cbba4debd2096b7d7c936ddc8d6b0b 100644 (file)
@@ -16,9 +16,13 @@ Description: Enable/disable demoting pages during reclaim
                Allowing page migration during reclaim enables these
                systems to migrate pages from fast tiers to slow tiers
                when the fast tier is under pressure.  This migration
-               is performed before swap.  It may move data to a NUMA
-               node that does not fall into the cpuset of the
-               allocating process which might be construed to violate
-               the guarantees of cpusets.  This should not be enabled
-               on systems which need strict cpuset location
-               guarantees.
+               is performed before swap if an eligible numa node is
+               present in cpuset.mems for the cgroup (or if cpuset v1
+               is being used). If cpusets.mems changes at runtime, it
+               may move data to a NUMA node that does not fall into the
+               cpuset of the new cpusets.mems, which might be construed
+               to violate the guarantees of cpusets.  Shared memory,
+               such as libraries, owned by another cgroup may still be
+               demoted and result in memory use on a node not present
+               in cpusets.mem. This should not be enabled on systems
+               which need strict cpuset location guarantees.
index d6a4fe5c3b6e2a8b3dffe95fe9ffe8acc7a98380..2ddb256187b51b83415477b532738fa906a91111 100644 (file)
@@ -173,6 +173,7 @@ static inline void set_mems_allowed(nodemask_t nodemask)
        task_unlock(current);
 }
 
+extern bool cpuset_node_allowed(struct cgroup *cgroup, int nid);
 #else /* !CONFIG_CPUSETS */
 
 static inline bool cpusets_enabled(void) { return false; }
@@ -293,6 +294,10 @@ static inline bool read_mems_allowed_retry(unsigned int seq)
        return false;
 }
 
+static inline bool cpuset_node_allowed(struct cgroup *cgroup, int nid)
+{
+       return true;
+}
 #endif /* !CONFIG_CPUSETS */
 
 #endif /* _LINUX_CPUSET_H */
index 5264d148bdd9d463113c836f25a674a8488c5312..ac1b003ee5b81d6cfbc8f19e6409413a33d82185 100644 (file)
@@ -1736,6 +1736,8 @@ static inline void count_objcg_events(struct obj_cgroup *objcg,
        rcu_read_unlock();
 }
 
+bool mem_cgroup_node_allowed(struct mem_cgroup *memcg, int nid);
+
 #else
 static inline bool mem_cgroup_kmem_disabled(void)
 {
@@ -1797,6 +1799,11 @@ static inline ino_t page_cgroup_ino(struct page *page)
 {
        return 0;
 }
+
+static inline bool mem_cgroup_node_allowed(struct mem_cgroup *memcg, int nid)
+{
+       return true;
+}
 #endif /* CONFIG_MEMCG */
 
 #if defined(CONFIG_MEMCG) && defined(CONFIG_ZSWAP)
index 54f6af362191a1f4ed6f7f58dbada610e153969d..83639a12883d1825c2927bdac66a78926d88a5f8 100644 (file)
@@ -4237,6 +4237,42 @@ bool cpuset_current_node_allowed(int node, gfp_t gfp_mask)
        return allowed;
 }
 
+bool cpuset_node_allowed(struct cgroup *cgroup, int nid)
+{
+       struct cgroup_subsys_state *css;
+       struct cpuset *cs;
+       bool allowed;
+
+       /*
+        * In v1, mem_cgroup and cpuset are unlikely in the same hierarchy
+        * and mems_allowed is likely to be empty even if we could get to it,
+        * so return true to avoid taking a global lock on the empty check.
+        */
+       if (!cpuset_v2())
+               return true;
+
+       css = cgroup_get_e_css(cgroup, &cpuset_cgrp_subsys);
+       if (!css)
+               return true;
+
+       /*
+        * Normally, accessing effective_mems would require the cpuset_mutex
+        * or callback_lock - but node_isset is atomic and the reference
+        * taken via cgroup_get_e_css is sufficient to protect css.
+        *
+        * Since this interface is intended for use by migration paths, we
+        * relax locking here to avoid taking global locks - while accepting
+        * there may be rare scenarios where the result may be innaccurate.
+        *
+        * Reclaim and migration are subject to these same race conditions, and
+        * cannot make strong isolation guarantees, so this is acceptable.
+        */
+       cs = container_of(css, struct cpuset, css);
+       allowed = node_isset(nid, cs->effective_mems);
+       css_put(css);
+       return allowed;
+}
+
 /**
  * cpuset_spread_node() - On which node to begin search for a page
  * @rotor: round robin rotor
index 3020bb82c94cca0c87f24b9da965e9e8bdd5e122..e17e0a9ceee0c0830ef15e2634f294619a70f7b7 100644 (file)
@@ -29,6 +29,7 @@
 #include <linux/page_counter.h>
 #include <linux/memcontrol.h>
 #include <linux/cgroup.h>
+#include <linux/cpuset.h>
 #include <linux/sched/mm.h>
 #include <linux/shmem_fs.h>
 #include <linux/hugetlb.h>
@@ -5523,3 +5524,8 @@ static int __init mem_cgroup_swap_init(void)
 subsys_initcall(mem_cgroup_swap_init);
 
 #endif /* CONFIG_SWAP */
+
+bool mem_cgroup_node_allowed(struct mem_cgroup *memcg, int nid)
+{
+       return memcg ? cpuset_node_allowed(memcg->css.cgroup, nid) : true;
+}
index ceffb20bb8c94cb8eb5b8204ca2e99bef1dabb22..a4fbd52a82d45a939ccf87d36c2458f893da83a8 100644 (file)
@@ -342,16 +342,22 @@ static void flush_reclaim_state(struct scan_control *sc)
        }
 }
 
-static bool can_demote(int nid, struct scan_control *sc)
+static bool can_demote(int nid, struct scan_control *sc,
+                      struct mem_cgroup *memcg)
 {
+       int demotion_nid;
+
        if (!numa_demotion_enabled)
                return false;
        if (sc && sc->no_demotion)
                return false;
-       if (next_demotion_node(nid) == NUMA_NO_NODE)
+
+       demotion_nid = next_demotion_node(nid);
+       if (demotion_nid == NUMA_NO_NODE)
                return false;
 
-       return true;
+       /* If demotion node isn't in the cgroup's mems_allowed, fall back */
+       return mem_cgroup_node_allowed(memcg, demotion_nid);
 }
 
 static inline bool can_reclaim_anon_pages(struct mem_cgroup *memcg,
@@ -376,7 +382,7 @@ static inline bool can_reclaim_anon_pages(struct mem_cgroup *memcg,
         *
         * Can it be reclaimed from this node via demotion?
         */
-       return can_demote(nid, sc);
+       return can_demote(nid, sc, memcg);
 }
 
 /*
@@ -1096,7 +1102,8 @@ static bool may_enter_fs(struct folio *folio, gfp_t gfp_mask)
  */
 static unsigned int shrink_folio_list(struct list_head *folio_list,
                struct pglist_data *pgdat, struct scan_control *sc,
-               struct reclaim_stat *stat, bool ignore_references)
+               struct reclaim_stat *stat, bool ignore_references,
+               struct mem_cgroup *memcg)
 {
        struct folio_batch free_folios;
        LIST_HEAD(ret_folios);
@@ -1109,7 +1116,7 @@ static unsigned int shrink_folio_list(struct list_head *folio_list,
        folio_batch_init(&free_folios);
        memset(stat, 0, sizeof(*stat));
        cond_resched();
-       do_demote_pass = can_demote(pgdat->node_id, sc);
+       do_demote_pass = can_demote(pgdat->node_id, sc, memcg);
 
 retry:
        while (!list_empty(folio_list)) {
@@ -1658,7 +1665,7 @@ unsigned int reclaim_clean_pages_from_list(struct zone *zone,
         */
        noreclaim_flag = memalloc_noreclaim_save();
        nr_reclaimed = shrink_folio_list(&clean_folios, zone->zone_pgdat, &sc,
-                                       &stat, true);
+                                       &stat, true, NULL);
        memalloc_noreclaim_restore(noreclaim_flag);
 
        list_splice(&clean_folios, folio_list);
@@ -2029,7 +2036,8 @@ static unsigned long shrink_inactive_list(unsigned long nr_to_scan,
        if (nr_taken == 0)
                return 0;
 
-       nr_reclaimed = shrink_folio_list(&folio_list, pgdat, sc, &stat, false);
+       nr_reclaimed = shrink_folio_list(&folio_list, pgdat, sc, &stat, false,
+                                        lruvec_memcg(lruvec));
 
        spin_lock_irq(&lruvec->lru_lock);
        move_folios_to_lru(lruvec, &folio_list);
@@ -2212,7 +2220,7 @@ static unsigned int reclaim_folio_list(struct list_head *folio_list,
                .no_demotion = 1,
        };
 
-       nr_reclaimed = shrink_folio_list(folio_list, pgdat, &sc, &stat, true);
+       nr_reclaimed = shrink_folio_list(folio_list, pgdat, &sc, &stat, true, NULL);
        while (!list_empty(folio_list)) {
                folio = lru_to_folio(folio_list);
                list_del(&folio->lru);
@@ -2644,7 +2652,7 @@ out:
  * Anonymous LRU management is a waste if there is
  * ultimately no way to reclaim the memory.
  */
-static bool can_age_anon_pages(struct pglist_data *pgdat,
+static bool can_age_anon_pages(struct lruvec *lruvec,
                               struct scan_control *sc)
 {
        /* Aging the anon LRU is valuable if swap is present: */
@@ -2652,7 +2660,8 @@ static bool can_age_anon_pages(struct pglist_data *pgdat,
                return true;
 
        /* Also valuable if anon pages can be demoted: */
-       return can_demote(pgdat->node_id, sc);
+       return can_demote(lruvec_pgdat(lruvec)->node_id, sc,
+                         lruvec_memcg(lruvec));
 }
 
 #ifdef CONFIG_LRU_GEN
@@ -2730,7 +2739,7 @@ static int get_swappiness(struct lruvec *lruvec, struct scan_control *sc)
        if (!sc->may_swap)
                return 0;
 
-       if (!can_demote(pgdat->node_id, sc) &&
+       if (!can_demote(pgdat->node_id, sc, memcg) &&
            mem_cgroup_get_nr_swap_pages(memcg) < MIN_LRU_BATCH)
                return 0;
 
@@ -4693,7 +4702,7 @@ static int evict_folios(struct lruvec *lruvec, struct scan_control *sc, int swap
        if (list_empty(&list))
                return scanned;
 retry:
-       reclaimed = shrink_folio_list(&list, pgdat, sc, &stat, false);
+       reclaimed = shrink_folio_list(&list, pgdat, sc, &stat, false, memcg);
        sc->nr.unqueued_dirty += stat.nr_unqueued_dirty;
        sc->nr_reclaimed += reclaimed;
        trace_mm_vmscan_lru_shrink_inactive(pgdat->node_id,
@@ -5848,7 +5857,7 @@ static void shrink_lruvec(struct lruvec *lruvec, struct scan_control *sc)
         * Even if we did not try to evict anon pages at all, we want to
         * rebalance the anon lru active/inactive ratio.
         */
-       if (can_age_anon_pages(lruvec_pgdat(lruvec), sc) &&
+       if (can_age_anon_pages(lruvec, sc) &&
            inactive_is_low(lruvec, LRU_INACTIVE_ANON))
                shrink_active_list(SWAP_CLUSTER_MAX, lruvec,
                                   sc, LRU_ACTIVE_ANON);
@@ -6679,10 +6688,10 @@ static void kswapd_age_node(struct pglist_data *pgdat, struct scan_control *sc)
                return;
        }
 
-       if (!can_age_anon_pages(pgdat, sc))
+       lruvec = mem_cgroup_lruvec(NULL, pgdat);
+       if (!can_age_anon_pages(lruvec, sc))
                return;
 
-       lruvec = mem_cgroup_lruvec(NULL, pgdat);
        if (!inactive_is_low(lruvec, LRU_INACTIVE_ANON))
                return;