From 02f4e9d063558816c13ba9573584cd20d009ff8d Mon Sep 17 00:00:00 2001 From: victoryang00 Date: Mon, 24 Jul 2023 21:59:32 +0000 Subject: [PATCH] fix bede a little bit Signed-off-by: victoryang00 --- mm/bede.c | 102 +++++++++++++----------------------------------- mm/bede.h | 3 +- mm/memcontrol.c | 11 +++--- mm/mempolicy.c | 52 +++++++++++------------- 4 files changed, 58 insertions(+), 110 deletions(-) diff --git a/mm/bede.c b/mm/bede.c index 7abef3b6c..97101a230 100644 --- a/mm/bede.c +++ b/mm/bede.c @@ -5,54 +5,25 @@ #include "linux/mempolicy.h" #include "linux/workqueue.h" -struct memory_stat { - const char *name; - unsigned int idx; -}; - -static const struct memory_stat memory_stats[] = { - { "anon", NR_ANON_MAPPED }, - { "file", NR_FILE_PAGES }, - { "kernel", MEMCG_KMEM }, - { "kernel_stack", NR_KERNEL_STACK_KB }, - { "pagetables", NR_PAGETABLE }, - { "sec_pagetables", NR_SECONDARY_PAGETABLE }, - { "percpu", MEMCG_PERCPU_B }, - { "sock", MEMCG_SOCK }, - { "vmalloc", MEMCG_VMALLOC }, - { "shmem", NR_SHMEM }, -#if defined(CONFIG_MEMCG_KMEM) && defined(CONFIG_ZSWAP) - { "zswap", MEMCG_ZSWAP_B }, - { "zswapped", MEMCG_ZSWAPPED }, -#endif - { "file_mapped", NR_FILE_MAPPED }, - { "file_dirty", NR_FILE_DIRTY }, - { "file_writeback", NR_WRITEBACK }, -#ifdef CONFIG_SWAP - { "swapcached", NR_SWAPCACHE }, -#endif -#ifdef CONFIG_TRANSPARENT_HUGEPAGE - { "anon_thp", NR_ANON_THPS }, - { "file_thp", NR_FILE_THPS }, - { "shmem_thp", NR_SHMEM_THPS }, -#endif - { "inactive_anon", NR_INACTIVE_ANON }, - { "active_anon", NR_ACTIVE_ANON }, - { "inactive_file", NR_INACTIVE_FILE }, - { "active_file", NR_ACTIVE_FILE }, - { "unevictable", NR_UNEVICTABLE }, - { "slab_reclaimable", NR_SLAB_RECLAIMABLE_B }, - { "slab_unreclaimable", NR_SLAB_UNRECLAIMABLE_B }, +bool bede_flush_node_rss(struct mem_cgroup *memcg) { // work around for every time call policy_node for delayed + int nid; + // mem_cgroup_flush_stats(); + for_each_node_state(nid, N_MEMORY) { + u64 size; + struct lruvec *lruvec; + pg_data_t *pgdat = NODE_DATA(nid); + if (!pgdat) + return false; + lruvec = mem_cgroup_lruvec(memcg, pgdat); + if (!lruvec) + return false; + size = (lruvec_page_state(lruvec, NR_ANON_MAPPED)) * PAGE_SIZE; + memcg->node_rss[nid] = size >> 20; + } + return true; +} +EXPORT_SYMBOL_GPL(bede_flush_node_rss); - /* The memory events */ - { "workingset_refault_anon", WORKINGSET_REFAULT_ANON }, - { "workingset_refault_file", WORKINGSET_REFAULT_FILE }, - { "workingset_activate_anon", WORKINGSET_ACTIVATE_ANON }, - { "workingset_activate_file", WORKINGSET_ACTIVATE_FILE }, - { "workingset_restore_anon", WORKINGSET_RESTORE_ANON }, - { "workingset_restore_file", WORKINGSET_RESTORE_FILE }, - { "workingset_nodereclaim", WORKINGSET_NODERECLAIM }, -}; void bede_walk_page_table_and_migrate_to_node(struct task_struct *task, int from_node,int to_node, int count) { @@ -94,6 +65,7 @@ void bede_walk_page_table_and_migrate_to_node(struct task_struct *task, } EXPORT_SYMBOL_GPL(bede_walk_page_table_and_migrate_to_node); + int bede_get_node(struct mem_cgroup *memcg, int node) { if (memcg->node_limit[node] > memcg->node_rss[node]) { return node; @@ -130,24 +102,6 @@ struct bede_work_struct *bede_work_alloc(struct cgroup *cgrp){ return bede_work; } EXPORT_SYMBOL_GPL(bede_work_alloc); -void bede_flush_node_rss(struct mem_cgroup *memcg) { // work around for every time call policy_node for delayed - int i; - mem_cgroup_flush_stats(); - for(i = 0; i < 4; i++){ - memcg->node_rss[i] = 0; - } - int nid; - for_each_node_state(nid, N_MEMORY) { - u64 size; - struct lruvec *lruvec; - - lruvec = mem_cgroup_lruvec(memcg, NODE_DATA(nid)); - size = lruvec_page_state(lruvec, NR_PAGETABLE) * PAGE_SIZE; - memcg->node_rss[nid] += size >> 21; - - } -} -EXPORT_SYMBOL_GPL(bede_flush_node_rss); void bede_do_page_walk_and_migration(struct work_struct *work) { @@ -176,15 +130,15 @@ void bede_do_page_walk_and_migration(struct work_struct *work) } memcg = get_mem_cgroup_from_mm(task->mm); bede_flush_node_rss(memcg); - res = bede_get_node(memcg, 0); - // Here consider the control of node limit vs. node rss - if (bede_work->should_migrate){ // The same as before requires it's filled to full - if (res) { // denote - bede_walk_page_table_and_migrate_to_node(task, 0, res, memcg->node_rss[res]-memcg->node_limit[res]); - } else { // promote - bede_walk_page_table_and_migrate_to_node(task, res, 0, memcg->node_limit[res]-memcg->node_rss[res]); - } - } + // res = bede_get_node(memcg, 0); + // // Here consider the control of node limit vs. node rss + // if (bede_work->should_migrate){ // The same as before requires it's filled to full + // if (res) { // denote + // bede_walk_page_table_and_migrate_to_node(task, 0, res, memcg->node_rss[res]-memcg->node_limit[res]); + // } else { // promote + // bede_walk_page_table_and_migrate_to_node(task, res, 0, memcg->node_limit[res]-memcg->node_rss[res]); + // } + // } } index++; } diff --git a/mm/bede.h b/mm/bede.h index ada185ac8..5a5476913 100644 --- a/mm/bede.h +++ b/mm/bede.h @@ -33,4 +33,5 @@ void bede_do_page_walk_and_migration(struct work_struct *work); void bede_append_page_walk_and_migration(struct bede_work_struct *work); int bede_get_node(struct mem_cgroup *memcg, int node); bool bede_is_local_bind(struct mem_cgroup *memcg); -struct bede_work_struct *bede_work_alloc(struct cgroup *cgrp); \ No newline at end of file +struct bede_work_struct *bede_work_alloc(struct cgroup *cgrp); +bool bede_flush_node_rss(struct mem_cgroup *memcg); diff --git a/mm/memcontrol.c b/mm/memcontrol.c index 18884661f..0494670e1 100755 --- a/mm/memcontrol.c +++ b/mm/memcontrol.c @@ -3966,7 +3966,7 @@ static int mem_cgroup_move_charge_write(struct cgroup_subsys_state *css, #endif #ifdef CONFIG_NUMA - +extern bool bede_should_policy; #define LRU_ALL_FILE (BIT(LRU_INACTIVE_FILE) | BIT(LRU_ACTIVE_FILE)) #define LRU_ALL_ANON (BIT(LRU_INACTIVE_ANON) | BIT(LRU_ACTIVE_ANON)) #define LRU_ALL ((1 << NR_LRU_LISTS) - 1) @@ -4081,7 +4081,7 @@ static ssize_t mem_cgroup_node_limit1_write(struct kernfs_open_file *of, return -ENOMEM; WRITE_ONCE(memcg->node_limit[0], val); - + bede_should_policy = true; return nbytes; } @@ -4112,7 +4112,7 @@ static ssize_t mem_cgroup_node_limit2_write(struct kernfs_open_file *of, return -ENOMEM; WRITE_ONCE(memcg->node_limit[1], val); - + bede_should_policy = true; return nbytes; } @@ -4143,7 +4143,7 @@ static ssize_t mem_cgroup_node_limit3_write(struct kernfs_open_file *of, return -ENOMEM; WRITE_ONCE(memcg->node_limit[2], val); - + bede_should_policy = true; return nbytes; } @@ -4174,7 +4174,7 @@ static ssize_t mem_cgroup_node_limit4_write(struct kernfs_open_file *of, return -ENOMEM; WRITE_ONCE(memcg->node_limit[3], val); - + bede_should_policy = true; return nbytes; } #endif /* CONFIG_NUMA */ @@ -6749,6 +6749,7 @@ static int memory_numa_stat_show(struct seq_file *m, void *v) size = lruvec_page_state_output(lruvec, memory_stats[i].idx); seq_printf(m, " N%d=%llu", nid, size); + memcg->node_rss[nid] = ((lruvec_page_state(lruvec, NR_ANON_MAPPED)) * PAGE_SIZE) >> 20 ; } seq_putc(m, '\n'); } diff --git a/mm/mempolicy.c b/mm/mempolicy.c index 8d8bb92bb..eeb8f1d72 100755 --- a/mm/mempolicy.c +++ b/mm/mempolicy.c @@ -69,7 +69,6 @@ */ #include "linux/memcontrol.h" -#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt #include #include @@ -117,6 +116,7 @@ static struct kmem_cache *policy_cache; static struct kmem_cache *sn_cache; +bool bede_should_policy = false; /* Highest zone. An specific allocation for a zone below that is not policied. */ @@ -1841,24 +1841,16 @@ bool apply_policy_zone(struct mempolicy *policy, enum zone_type zone) nodemask_t *policy_nodemask(gfp_t gfp, struct mempolicy *policy) { int mode = policy->mode; - struct mem_cgroup *memcg = get_mem_cgroup_from_mm(current->mm); - if (mode != MPOL_BIND) - if (memcg) { - if (!bede_is_local_bind(memcg)) { - if (memcg != root_mem_cgroup) { - // bede_append_page_walk_and_migration(current->cgroups->dfl_cgrp->bede); - // int bede_get_node(memcg, 0); - nodes_clear(policy->nodes); - node_set(bede_get_node(memcg, 0),policy->nodes); - return &policy->nodes; - } - } else{ - nodes_clear(policy->nodes); - node_set(1,policy->nodes); - return &policy->nodes; - } + if (mode != MPOL_BIND && bede_should_policy) { + struct mem_cgroup *memcg = get_mem_cgroup_from_mm(current->mm); + if (memcg && root_mem_cgroup && memcg != root_mem_cgroup) { + // bede_append_page_walk_and_migration(current->cgroups->dfl_cgrp->bede); + // int bede_get_node(memcg, 0); + nodes_clear(policy->nodes); + node_set(bede_get_node(memcg, 0),policy->nodes); + return &policy->nodes; } - + } /* Lower zones don't get a nodemask applied for MPOL_BIND */ if (unlikely(mode == MPOL_BIND) && apply_policy_zone(policy, gfp_zone(gfp)) && @@ -1881,18 +1873,18 @@ ALLOW_ERROR_INJECTION(policy_nodemask, TRUE); * secures the nodemask limit for 'bind' and 'prefer-many' policy. */ int policy_node(gfp_t gfp, struct mempolicy *policy, int nd) -{// TODO: check if this is the root memcg and fast path - struct mem_cgroup *memcg = get_mem_cgroup_from_mm(current->mm); - if (policy->mode != MPOL_BIND) { - if (memcg) { - if (!bede_is_local_bind(memcg)) { - if (memcg != root_mem_cgroup) { - // bede_append_page_walk_and_migration(current->cgroups->dfl_cgrp->bede); - return bede_get_node(memcg, nd); - } - } else { - return 1; - } +{ + if (policy->mode != MPOL_BIND && bede_should_policy) { + rcu_read_lock(); + struct mem_cgroup *memcg = get_mem_cgroup_from_mm(current->mm); + if (memcg && root_mem_cgroup && memcg != root_mem_cgroup) { + if (bede_flush_node_rss(memcg)) { + // bede_append_page_walk_and_migration(current->cgroups->dfl_cgrp->bede); + mem_cgroup_flush_stats(); + nd = bede_get_node(memcg, nd); + rcu_read_unlock(); + return nd; + } } } if (policy->mode == MPOL_PREFERRED) {