Re: [PATCHv2] memcg: reclaim memory from node in round-robin

From: KAMEZAWA Hiroyuki
Date: Wed Apr 27 2011 - 20:05:19 EST


On Wed, 27 Apr 2011 10:33:43 -0700
Ying Han <yinghan@xxxxxxxxxx> wrote:

> On Wed, Apr 27, 2011 at 12:51 AM, KAMEZAWA Hiroyuki
> <kamezawa.hiroyu@xxxxxxxxxxxxxx> wrote:
> > I changed the logic a little and add a filter for skipping nodes.
> > With large NUMA, tasks may under cpuset or mempolicy and the usage of memory
> > can be unbalanced. So, I think a filter is required.
>
> Thank you.
>
> >
> > ==
> > Now, memory cgroup's direct reclaim frees memory from the current node.
> > But this has some troubles. In usual, when a set of threads works in
> > cooperative way, they are tend to on the same node. So, if they hit
> > limits under memcg, it will reclaim memory from themselves, it may be
> > active working set.
> >
> > For example, assume 2 node system which has Node 0 and Node 1
> > and a memcg which has 1G limit. After some work, file cacne remains and
> > and usages are
> > Â Node 0: Â1M
> > Â Node 1: Â998M.
> >
> > and run an application on Node 0, it will eats its foot before freeing
> > unnecessary file caches.
> >
> > This patch adds round-robin for NUMA and adds equal pressure to each
> > node. When using cpuset's spread memory feature, this will work very well.
> >
> >
> > From: Ying Han <yinghan@xxxxxxxxxx>
> > Signed-off-by: Ying Han <yinghan@xxxxxxxxxx>
> > Signed-off-by: KAMEZAWA Hiroyuki <kamezawa.hiroyu@xxxxxxxxxxxxxx>
> >
> > Changelog v1->v2:
> > Â- fixed comments.
> > Â- added a logic to avoid scanning unused node.
> >
> > ---
> > Âinclude/linux/memcontrol.h | Â Â1
> > Âmm/memcontrol.c      Â|  98 ++++++++++++++++++++++++++++++++++++++++++---
> > Âmm/vmscan.c        Â|  Â9 +++-
> > Â3 files changed, 101 insertions(+), 7 deletions(-)
> >
> > Index: memcg/include/linux/memcontrol.h
> > ===================================================================
> > --- memcg.orig/include/linux/memcontrol.h
> > +++ memcg/include/linux/memcontrol.h
> > @@ -108,6 +108,7 @@ extern void mem_cgroup_end_migration(str
> > Â*/
> > Âint mem_cgroup_inactive_anon_is_low(struct mem_cgroup *memcg);
> > Âint mem_cgroup_inactive_file_is_low(struct mem_cgroup *memcg);
> > +int mem_cgroup_select_victim_node(struct mem_cgroup *memcg);
> > Âunsigned long mem_cgroup_zone_nr_pages(struct mem_cgroup *memcg,
> > Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â struct zone *zone,
> > Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â enum lru_list lru);
> > Index: memcg/mm/memcontrol.c
> > ===================================================================
> > --- memcg.orig/mm/memcontrol.c
> > +++ memcg/mm/memcontrol.c
> > @@ -237,6 +237,11 @@ struct mem_cgroup {
> > Â Â Â Â * reclaimed from.
> > Â Â Â Â */
> > Â Â Â Âint last_scanned_child;
> > + Â Â Â int last_scanned_node;
> > +#if MAX_NUMNODES > 1
> > +    nodemask_t   Âscan_nodes;
> > +    unsigned long  next_scan_node_update;
> > +#endif
> > Â Â Â Â/*
> > Â Â Â Â * Should the accounting and control be hierarchical, per subtree?
> > Â Â Â Â */
> > @@ -650,18 +655,27 @@ static void mem_cgroup_soft_scan(struct
> > Â Â Â Âthis_cpu_add(mem->stat->events[MEM_CGROUP_EVENTS_SOFT_SCAN], val);
> > Â}
> >
> > +static unsigned long
> > +mem_cgroup_get_zonestat_node(struct mem_cgroup *mem, int nid, enum lru_list idx)
> > +{
> > + Â Â Â struct mem_cgroup_per_zone *mz;
> > + Â Â Â u64 total;
> > + Â Â Â int zid;
> > +
> > + Â Â Â for (zid = 0; zid < MAX_NR_ZONES; zid++) {
> > + Â Â Â Â Â Â Â mz = mem_cgroup_zoneinfo(mem, nid, zid);
> > + Â Â Â Â Â Â Â total += MEM_CGROUP_ZSTAT(mz, idx);
> > + Â Â Â }
> > + Â Â Â return total;
> > +}
> > Âstatic unsigned long mem_cgroup_get_local_zonestat(struct mem_cgroup *mem,
> > Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Âenum lru_list idx)
> > Â{
> > - Â Â Â int nid, zid;
> > - Â Â Â struct mem_cgroup_per_zone *mz;
> > + Â Â Â int nid;
> > Â Â Â Âu64 total = 0;
> >
> > Â Â Â Âfor_each_online_node(nid)
> > - Â Â Â Â Â Â Â for (zid = 0; zid < MAX_NR_ZONES; zid++) {
> > - Â Â Â Â Â Â Â Â Â Â Â mz = mem_cgroup_zoneinfo(mem, nid, zid);
> > - Â Â Â Â Â Â Â Â Â Â Â total += MEM_CGROUP_ZSTAT(mz, idx);
> > - Â Â Â Â Â Â Â }
> > + Â Â Â Â Â Â Â total += mem_cgroup_get_zonestat_node(mem, nid, idx);
> > Â Â Â Âreturn total;
> > Â}
> >
> > @@ -1471,6 +1485,77 @@ mem_cgroup_select_victim(struct mem_cgro
> > Â Â Â Âreturn ret;
> > Â}
> >
> > +#if MAX_NUMNODES > 1
> > +
> > +/*
> > + * Update nodemask always is not very good. Even if we have empty
> > + * list, or wrong list here, we can start from some node and traverse all nodes
> > + * based on zonelist. So, update the list loosely once in 10 secs.
> > + *
> > + */
> > +static void mem_cgroup_may_update_nodemask(struct mem_cgroup *mem)
> > +{
> > + Â Â Â int nid;
> > +
> > + Â Â Â if (time_after(mem->next_scan_node_update, jiffies))
> > + Â Â Â Â Â Â Â return;
> > +
> > + Â Â Â mem->next_scan_node_update = jiffies + 10*HZ;
> > + Â Â Â /* make a nodemask where this memcg uses memory from */
> > + Â Â Â mem->scan_nodes = node_states[N_HIGH_MEMORY];
> > +
> > + Â Â Â for_each_node_mask(nid, node_states[N_HIGH_MEMORY]) {
> > +
> > + Â Â Â Â Â Â Â if (mem_cgroup_get_zonestat_node(mem, nid, LRU_INACTIVE_FILE) ||
> > + Â Â Â Â Â Â Â Â Â mem_cgroup_get_zonestat_node(mem, nid, LRU_ACTIVE_FILE))
> > + Â Â Â Â Â Â Â Â Â Â Â continue;
> > +
> > + Â Â Â Â Â Â Â if (total_swap_pages &&
> > + Â Â Â Â Â Â Â Â Â (mem_cgroup_get_zonestat_node(mem, nid, LRU_INACTIVE_ANON) ||
> > + Â Â Â Â Â Â Â Â Â Âmem_cgroup_get_zonestat_node(mem, nid, LRU_ACTIVE_ANON)))
> > + Â Â Â Â Â Â Â Â Â Â Â continue;
> > + Â Â Â Â Â Â Â node_clear(nid, mem->scan_nodes);
> > + Â Â Â }
> > +
> > +}
> > +
> > +/*
> > + * Selecting a node where we start reclaim from. Because what we need is just
> > + * reducing usage counter, start from anywhere is O,K. Considering
> > + * memory reclaim from current node, there are pros. and cons.
> > + *
> > + * Freeing memory from current node means freeing memory from a node which
> > + * we'll use or we've used. So, it may make LRU bad. And if several threads
> > + * hit limits, it will see a contention on a node. But freeing from remote
> > + * node means more costs for memory reclaim because of memory latency.
> > + *
> > + * Now, we use round-robin. Better algorithm is welcomed.
> > + */
> > +int mem_cgroup_select_victim_node(struct mem_cgroup *mem)
> > +{
> > + Â Â Â int node;
> > +
> > + Â Â Â mem_cgroup_may_update_nodemask(mem);
> > + Â Â Â node = mem->last_scanned_node;
> > +
> > + Â Â Â node = next_node(node, mem->scan_nodes);
> > + Â Â Â if (node == MAX_NUMNODES) {
> > + Â Â Â Â Â Â Â node = first_node(mem->scan_nodes);
> > + Â Â Â Â Â Â Â if (unlikely(node == MAX_NUMNODES))
> > + Â Â Â Â Â Â Â Â Â Â Â node = numa_node_id();
> not sure about this logic, is that possible we reclaim from a node
> with all "unreclaimable" pages (based on the
> mem_cgroup_may_update_nodemask check).
> If i missed anything here, it would be helpful to add comment.
>

What I'm afraid here is when a user uses very small memcg,
all pages on the LRU may be isolated or all usages are in per-cpu cache
of memcg or because of task-migration between memcg, it hits limit before
having any pages on LRU.....I think there is possible corner cases which
can cause hang.

ok, will add comment.

Thanks,
-Kame






--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/