* [PATCHSET v2] mm, memcontrol: Implement memory.swap.events
@ 2018-04-16 23:09 Tejun Heo
2018-04-16 23:09 ` [PATCH 1/2] mm, memcontrol: Move swap charge handling into get_swap_page() Tejun Heo
2018-04-16 23:11 ` [PATCH 2/2] mm, memcontrol: Implement memory.swap.events Tejun Heo
0 siblings, 2 replies; 4+ messages in thread
From: Tejun Heo @ 2018-04-16 23:09 UTC (permalink / raw)
To: hannes, mhocko, vdavydov.dev
Cc: guro, riel, akpm, linux-kernel, kernel-team, cgroups, linux-mm
Hello,
Rebased on top of e27be240df53 ("mm: memcg: make sure memory.events is
uptodate when waking pollers").
This patchset implements memory.swap.events which contains max and
fail events so that userland can monitor and respond to swap running
out. It contains the following two patches.
0001-mm-memcontrol-Move-swap-charge-handling-into-get_swa.patch
0002-mm-memcontrol-Implement-memory.swap.events.patch
This patchset is on top of the current linus#master
(a27fc14219f2e3c4a46ba9177b04d9b52c875532).
Thanks.
--
tejun
^ permalink raw reply [flat|nested] 4+ messages in thread* [PATCH 1/2] mm, memcontrol: Move swap charge handling into get_swap_page() 2018-04-16 23:09 [PATCHSET v2] mm, memcontrol: Implement memory.swap.events Tejun Heo @ 2018-04-16 23:09 ` Tejun Heo 2018-04-16 23:11 ` [PATCH 2/2] mm, memcontrol: Implement memory.swap.events Tejun Heo 1 sibling, 0 replies; 4+ messages in thread From: Tejun Heo @ 2018-04-16 23:09 UTC (permalink / raw) To: hannes, mhocko, vdavydov.dev Cc: guro, riel, akpm, linux-kernel, kernel-team, cgroups, linux-mm get_swap_page() is always followed by mem_cgroup_try_charge_swap(). This patch moves mem_cgroup_try_charge_swap() into get_swap_page() and makes get_swap_page() call the function even after swap allocation failure. This simplifies the callers and consolidates memcg related logic and will ease adding swap related memcg events. Signed-off-by: Tejun Heo <tj@kernel.org> Cc: Johannes Weiner <hannes@cmpxchg.org> Cc: Michal Hocko <mhocko@kernel.org> Cc: Vladimir Davydov <vdavydov.dev@gmail.com> Cc: Roman Gushchin <guro@fb.com> Cc: Rik van Riel <riel@surriel.com> Cc: Andrew Morton <akpm@linux-foundation.org> --- mm/memcontrol.c | 3 +++ mm/shmem.c | 4 ---- mm/swap_slots.c | 10 +++++++--- mm/swap_state.c | 3 --- 4 files changed, 10 insertions(+), 10 deletions(-) --- a/mm/memcontrol.c +++ b/mm/memcontrol.c @@ -6012,6 +6012,9 @@ int mem_cgroup_try_charge_swap(struct pa if (!memcg) return 0; + if (!entry.val) + return 0; + memcg = mem_cgroup_id_get_online(memcg); if (!mem_cgroup_is_root(memcg) && --- a/mm/shmem.c +++ b/mm/shmem.c @@ -1322,9 +1322,6 @@ static int shmem_writepage(struct page * if (!swap.val) goto redirty; - if (mem_cgroup_try_charge_swap(page, swap)) - goto free_swap; - /* * Add inode to shmem_unuse()'s list of swapped-out inodes, * if it's not already there. Do it now before the page is @@ -1353,7 +1350,6 @@ static int shmem_writepage(struct page * } mutex_unlock(&shmem_swaplist_mutex); -free_swap: put_swap_page(page, swap); redirty: set_page_dirty(page); --- a/mm/swap_slots.c +++ b/mm/swap_slots.c @@ -317,7 +317,7 @@ swp_entry_t get_swap_page(struct page *p if (PageTransHuge(page)) { if (IS_ENABLED(CONFIG_THP_SWAP)) get_swap_pages(1, true, &entry); - return entry; + goto out; } /* @@ -347,10 +347,14 @@ repeat: } mutex_unlock(&cache->alloc_lock); if (entry.val) - return entry; + goto out; } get_swap_pages(1, false, &entry); - +out: + if (mem_cgroup_try_charge_swap(page, entry)) { + put_swap_page(page, entry); + entry.val = 0; + } return entry; } --- a/mm/swap_state.c +++ b/mm/swap_state.c @@ -216,9 +216,6 @@ int add_to_swap(struct page *page) if (!entry.val) return 0; - if (mem_cgroup_try_charge_swap(page, entry)) - goto fail; - /* * Radix-tree node allocations from PF_MEMALLOC contexts could * completely exhaust the page allocator. __GFP_NOMEMALLOC ^ permalink raw reply [flat|nested] 4+ messages in thread
* [PATCH 2/2] mm, memcontrol: Implement memory.swap.events 2018-04-16 23:09 [PATCHSET v2] mm, memcontrol: Implement memory.swap.events Tejun Heo 2018-04-16 23:09 ` [PATCH 1/2] mm, memcontrol: Move swap charge handling into get_swap_page() Tejun Heo @ 2018-04-16 23:11 ` Tejun Heo 1 sibling, 0 replies; 4+ messages in thread From: Tejun Heo @ 2018-04-16 23:11 UTC (permalink / raw) To: hannes, mhocko, vdavydov.dev Cc: guro, riel, akpm, linux-kernel, kernel-team, cgroups, linux-mm Add swap max and fail events so that userland can monitor and respond to running out of swap. v2: Rebased on top of e27be240df53 ("mm: memcg: make sure memory.events is uptodate when waking pollers") Signed-off-by: Tejun Heo <tj@kernel.org> Cc: Johannes Weiner <hannes@cmpxchg.org> Cc: Michal Hocko <mhocko@kernel.org> Cc: Vladimir Davydov <vdavydov.dev@gmail.com> Cc: Roman Gushchin <guro@fb.com> Cc: Rik van Riel <riel@surriel.com> Cc: Andrew Morton <akpm@linux-foundation.org> Cc: linux-api@vger.kernel.org --- Hello, I'm not too sure about the fail event. Right now, it's a bit confusing which stats / events are recursive and which aren't and also which ones reflect events which originate from a given cgroup and which targets the cgroup. No idea what the right long term solution is and it could just be that growing them organically is actually the only right thing to do. Thanks. Documentation/cgroup-v2.txt | 16 ++++++++++++++++ include/linux/memcontrol.h | 5 +++++ mm/memcontrol.c | 24 +++++++++++++++++++++++- 3 files changed, 44 insertions(+), 1 deletion(-) --- a/Documentation/cgroup-v2.txt +++ b/Documentation/cgroup-v2.txt @@ -1199,6 +1199,22 @@ PAGE_SIZE multiple when read back. Swap usage hard limit. If a cgroup's swap usage reaches this limit, anonymous memory of the cgroup will not be swapped out. + memory.swap.events + A read-only flat-keyed file which exists on non-root cgroups. + The following entries are defined. Unless specified + otherwise, a value change in this file generates a file + modified event. + + max + The number of times the cgroup's swap usage was about + to go over the max boundary and swap allocation + failed. + + fail + The number of times swap allocation failed either + because of running out of swap system-wide or max + limit. + Usage Guidelines ~~~~~~~~~~~~~~~~ --- a/include/linux/memcontrol.h +++ b/include/linux/memcontrol.h @@ -53,6 +53,8 @@ enum memcg_memory_event { MEMCG_HIGH, MEMCG_MAX, MEMCG_OOM, + MEMCG_SWAP_MAX, + MEMCG_SWAP_FAIL, MEMCG_NR_MEMORY_EVENTS, }; @@ -208,6 +210,9 @@ struct mem_cgroup { atomic_long_t memory_events[MEMCG_NR_MEMORY_EVENTS]; struct cgroup_file events_file; + /* handle for "memory.swap.events" */ + struct cgroup_file swap_events_file; + /* protect arrays of thresholds */ struct mutex thresholds_lock; --- a/mm/memcontrol.c +++ b/mm/memcontrol.c @@ -6012,13 +6012,17 @@ int mem_cgroup_try_charge_swap(struct pa if (!memcg) return 0; - if (!entry.val) + if (!entry.val) { + memcg_memory_event(memcg, MEMCG_SWAP_FAIL); return 0; + } memcg = mem_cgroup_id_get_online(memcg); if (!mem_cgroup_is_root(memcg) && !page_counter_try_charge(&memcg->swap, nr_pages, &counter)) { + memcg_memory_event(memcg, MEMCG_SWAP_MAX); + memcg_memory_event(memcg, MEMCG_SWAP_FAIL); mem_cgroup_id_put(memcg); return -ENOMEM; } @@ -6156,6 +6160,18 @@ static ssize_t swap_max_write(struct ker return nbytes; } +static int swap_events_show(struct seq_file *m, void *v) +{ + struct mem_cgroup *memcg = mem_cgroup_from_css(seq_css(m)); + + seq_printf(m, "max %lu\n", + atomic_long_read(&memcg->memory_events[MEMCG_SWAP_MAX])); + seq_printf(m, "fail %lu\n", + atomic_long_read(&memcg->memory_events[MEMCG_SWAP_FAIL])); + + return 0; +} + static struct cftype swap_files[] = { { .name = "swap.current", @@ -6168,6 +6184,12 @@ static struct cftype swap_files[] = { .seq_show = swap_max_show, .write = swap_max_write, }, + { + .name = "swap.events", + .flags = CFTYPE_NOT_ON_ROOT, + .file_offset = offsetof(struct mem_cgroup, swap_events_file), + .seq_show = swap_events_show, + }, { } /* terminate */ }; ^ permalink raw reply [flat|nested] 4+ messages in thread
* [PATCHSET] mm, memcontrol: Implement memory.swap.events @ 2018-03-24 16:51 Tejun Heo 2018-03-24 16:51 ` [PATCH 2/2] " Tejun Heo 0 siblings, 1 reply; 4+ messages in thread From: Tejun Heo @ 2018-03-24 16:51 UTC (permalink / raw) To: hannes, mhocko, vdavydov.dev Cc: guro, riel, akpm, linux-kernel, kernel-team, cgroups, linux-mm Hello, This patchset implements memory.swap.events which contains max and fail events so that userland can monitor and respond to swap running out. It contains the following two patches. 0001-mm-memcontrol-Move-swap-charge-handling-into-get_swa.patch 0002-mm-memcontrol-Implement-memory.swap.events.patch This patchset is on top of the "cgroup/for-4.17: Make cgroup_rstat available to controllers" patchset[1] and "mm, memcontrol: Make cgroup_rstat available to controllers" patchset[2] and also available in the following git branch. git://git.kernel.org/pub/scm/linux/kernel/git/tj/cgroup.git review-memcg-swap.events diffstat follows. Documentation/cgroup-v2.txt | 16 ++++++++++++++++ include/linux/memcontrol.h | 5 +++++ mm/memcontrol.c | 25 +++++++++++++++++++++++++ mm/shmem.c | 4 ---- mm/swap_slots.c | 10 +++++++--- mm/swap_state.c | 3 --- 6 files changed, 53 insertions(+), 10 deletions(-) Thanks. -- tejun [1] http://lkml.kernel.org/r/20180323231313.1254142-1-tj@kernel.org [2] http://lkml.kernel.org/r/20180324160901.512135-1-tj@kernel.org ^ permalink raw reply [flat|nested] 4+ messages in thread
* [PATCH 2/2] mm, memcontrol: Implement memory.swap.events 2018-03-24 16:51 [PATCHSET] " Tejun Heo @ 2018-03-24 16:51 ` Tejun Heo 0 siblings, 0 replies; 4+ messages in thread From: Tejun Heo @ 2018-03-24 16:51 UTC (permalink / raw) To: hannes, mhocko, vdavydov.dev Cc: guro, riel, akpm, linux-kernel, kernel-team, cgroups, linux-mm, Tejun Heo, linux-api Add swap max and fail events so that userland can monitor and respond to running out of swap. Signed-off-by: Tejun Heo <tj@kernel.org> Cc: Johannes Weiner <hannes@cmpxchg.org> Cc: Michal Hocko <mhocko@kernel.org> Cc: Vladimir Davydov <vdavydov.dev@gmail.com> Cc: Roman Gushchin <guro@fb.com> Cc: Rik van Riel <riel@surriel.com> Cc: Andrew Morton <akpm@linux-foundation.org> Cc: linux-api@vger.kernel.org --- Documentation/cgroup-v2.txt | 16 ++++++++++++++++ include/linux/memcontrol.h | 5 +++++ mm/memcontrol.c | 24 +++++++++++++++++++++++- 3 files changed, 44 insertions(+), 1 deletion(-) diff --git a/Documentation/cgroup-v2.txt b/Documentation/cgroup-v2.txt index 74cdeae..b0dda10 100644 --- a/Documentation/cgroup-v2.txt +++ b/Documentation/cgroup-v2.txt @@ -1199,6 +1199,22 @@ PAGE_SIZE multiple when read back. Swap usage hard limit. If a cgroup's swap usage reaches this limit, anonymous memory of the cgroup will not be swapped out. + memory.swap.events + A read-only flat-keyed file which exists on non-root cgroups. + The following entries are defined. Unless specified + otherwise, a value change in this file generates a file + modified event. + + max + The number of times the cgroup's swap usage was about + to go over the max boundary and swap allocation + failed. + + fail + The number of times swap allocation failed either + because of running out of swap system-wide or max + limit. + Usage Guidelines ~~~~~~~~~~~~~~~~ diff --git a/include/linux/memcontrol.h b/include/linux/memcontrol.h index 85a8f00..f198339 100644 --- a/include/linux/memcontrol.h +++ b/include/linux/memcontrol.h @@ -54,6 +54,8 @@ enum memcg_event_item { MEMCG_HIGH, MEMCG_MAX, MEMCG_OOM, + MEMCG_SWAP_MAX, + MEMCG_SWAP_FAIL, MEMCG_NR_EVENTS, }; @@ -202,6 +204,9 @@ struct mem_cgroup { /* handle for "memory.events" */ struct cgroup_file events_file; + /* handle for "memory.swap.events" */ + struct cgroup_file swap_events_file; + /* protect arrays of thresholds */ struct mutex thresholds_lock; diff --git a/mm/memcontrol.c b/mm/memcontrol.c index 9f9c8a7..1a14d4a4 100644 --- a/mm/memcontrol.c +++ b/mm/memcontrol.c @@ -5987,13 +5987,17 @@ int mem_cgroup_try_charge_swap(struct page *page, swp_entry_t entry) if (!memcg) return 0; - if (!entry.val) + if (!entry.val) { + mem_cgroup_event(memcg, MEMCG_SWAP_FAIL); return 0; + } memcg = mem_cgroup_id_get_online(memcg); if (!mem_cgroup_is_root(memcg) && !page_counter_try_charge(&memcg->swap, nr_pages, &counter)) { + mem_cgroup_event(memcg, MEMCG_SWAP_MAX); + mem_cgroup_event(memcg, MEMCG_SWAP_FAIL); mem_cgroup_id_put(memcg); return -ENOMEM; } @@ -6131,6 +6135,18 @@ static ssize_t swap_max_write(struct kernfs_open_file *of, return nbytes; } +static int swap_events_show(struct seq_file *m, void *v) +{ + struct mem_cgroup *memcg = mem_cgroup_from_css(seq_css(m)); + + memcg_stat_flush(memcg); + + seq_printf(m, "max %llu\n", memcg->events[MEMCG_SWAP_MAX]); + seq_printf(m, "fail %llu\n", memcg->events[MEMCG_SWAP_FAIL]); + + return 0; +} + static struct cftype swap_files[] = { { .name = "swap.current", @@ -6143,6 +6159,12 @@ static struct cftype swap_files[] = { .seq_show = swap_max_show, .write = swap_max_write, }, + { + .name = "swap.events", + .flags = CFTYPE_NOT_ON_ROOT, + .file_offset = offsetof(struct mem_cgroup, swap_events_file), + .seq_show = swap_events_show, + }, { } /* terminate */ }; -- 2.9.5 ^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2018-04-16 23:11 UTC | newest] Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed) -- links below jump to the message on this page -- 2018-04-16 23:09 [PATCHSET v2] mm, memcontrol: Implement memory.swap.events Tejun Heo 2018-04-16 23:09 ` [PATCH 1/2] mm, memcontrol: Move swap charge handling into get_swap_page() Tejun Heo 2018-04-16 23:11 ` [PATCH 2/2] mm, memcontrol: Implement memory.swap.events Tejun Heo -- strict thread matches above, loose matches on Subject: below -- 2018-03-24 16:51 [PATCHSET] " Tejun Heo 2018-03-24 16:51 ` [PATCH 2/2] " Tejun Heo
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox