Skip to content

Commit

Permalink
fix bede a little bit
Browse files Browse the repository at this point in the history
Signed-off-by: victoryang00 <[email protected]>
  • Loading branch information
victoryang00 committed Jul 25, 2023
1 parent 1791ee9 commit 02f4e9d
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 110 deletions.
102 changes: 28 additions & 74 deletions mm/bede.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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)
{
Expand Down Expand Up @@ -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++;
}
Expand Down
3 changes: 2 additions & 1 deletion mm/bede.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
struct bede_work_struct *bede_work_alloc(struct cgroup *cgrp);
bool bede_flush_node_rss(struct mem_cgroup *memcg);
11 changes: 6 additions & 5 deletions mm/memcontrol.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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 */
Expand Down Expand Up @@ -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');
}
Expand Down
52 changes: 22 additions & 30 deletions mm/mempolicy.c
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@
*/

#include "linux/memcontrol.h"
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt

#include <linux/mempolicy.h>
#include <linux/pagewalk.h>
Expand Down Expand Up @@ -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. */
Expand Down Expand Up @@ -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)) &&
Expand All @@ -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) {
Expand Down

0 comments on commit 02f4e9d

Please sign in to comment.