The patch titled Subject: fs, mm: account buffer_head to kmemcg has been added to the -mm tree. Its filename is fs-mm-account-buffer_head-to-kmemcg.patch This patch should soon appear at http://ozlabs.org/~akpm/mmots/broken-out/fs-mm-account-buffer_head-to-kmemcg.patch and later at http://ozlabs.org/~akpm/mmotm/broken-out/fs-mm-account-buffer_head-to-kmemcg.patch Before you just go and hit "reply", please: a) Consider who else should be cc'ed b) Prefer to cc a suitable mailing list as well c) Ideally: find the original patch on the mailing list and do a reply-to-all to that, adding suitable additional cc's *** Remember to use Documentation/process/submit-checklist.rst when testing your code *** The -mm tree is included into linux-next and is updated there every 3-4 working days ------------------------------------------------------ From: Shakeel Butt <shakeelb@xxxxxxxxxx> Subject: fs, mm: account buffer_head to kmemcg The buffer_head can consume a significant amount of system memory and is directly related to the amount of page cache. In our production environment we have observed that a lot of machines are spending a significant amount of memory as buffer_head and can not be left as system memory overhead. Charging buffer_head is not as simple as adding __GFP_ACCOUNT to the allocation. The buffer_heads can be allocated in a memcg different from the memcg of the page for which buffer_heads are being allocated. One concrete example is memory reclaim. The reclaim can trigger I/O of pages of any memcg on the system. So, the right way to charge buffer_head is to extract the memcg from the page for which buffer_heads are being allocated and then use targeted memcg charging API. Link: http://lkml.kernel.org/r/20180627191250.209150-3-shakeelb@xxxxxxxxxx Signed-off-by: Shakeel Butt <shakeelb@xxxxxxxxxx> Cc: Michal Hocko <mhocko@xxxxxxxxxx> Cc: Jan Kara <jack@xxxxxxx> Cc: Amir Goldstein <amir73il@xxxxxxxxx> Cc: Greg Thelen <gthelen@xxxxxxxxxx> Cc: Johannes Weiner <hannes@xxxxxxxxxxx> Cc: Vladimir Davydov <vdavydov.dev@xxxxxxxxx> Cc: Roman Gushchin <guro@xxxxxx> Cc: Alexander Viro <viro@xxxxxxxxxxxxxxxxxx> Signed-off-by: Andrew Morton <akpm@xxxxxxxxxxxxxxxxxxxx> --- diff -puN fs/buffer.c~fs-mm-account-buffer_head-to-kmemcg fs/buffer.c --- a/fs/buffer.c~fs-mm-account-buffer_head-to-kmemcg +++ a/fs/buffer.c @@ -45,6 +45,7 @@ #include <linux/mpage.h> #include <linux/bit_spinlock.h> #include <linux/pagevec.h> +#include <linux/sched/mm.h> #include <trace/events/block.h> static int fsync_buffers_list(spinlock_t *lock, struct list_head *list); @@ -815,10 +816,14 @@ struct buffer_head *alloc_page_buffers(s struct buffer_head *bh, *head; gfp_t gfp = GFP_NOFS; long offset; + struct mem_cgroup *memcg; if (retry) gfp |= __GFP_NOFAIL; + memcg = get_mem_cgroup_from_page(page); + memalloc_use_memcg(memcg); + head = NULL; offset = PAGE_SIZE; while ((offset -= size) >= 0) { @@ -835,6 +840,9 @@ struct buffer_head *alloc_page_buffers(s /* Link the buffer to its page */ set_bh_page(bh, page, offset); } +out: + memalloc_unuse_memcg(); + mem_cgroup_put(memcg); return head; /* * In case anything failed, we just free everything we got. @@ -848,7 +856,7 @@ no_grow: } while (head); } - return NULL; + goto out; } EXPORT_SYMBOL_GPL(alloc_page_buffers); diff -puN include/linux/memcontrol.h~fs-mm-account-buffer_head-to-kmemcg include/linux/memcontrol.h --- a/include/linux/memcontrol.h~fs-mm-account-buffer_head-to-kmemcg +++ a/include/linux/memcontrol.h @@ -372,6 +372,8 @@ struct mem_cgroup *mem_cgroup_from_task( struct mem_cgroup *get_mem_cgroup_from_mm(struct mm_struct *mm); +struct mem_cgroup *get_mem_cgroup_from_page(struct page *page); + static inline struct mem_cgroup *mem_cgroup_from_css(struct cgroup_subsys_state *css){ return css ? container_of(css, struct mem_cgroup, css) : NULL; @@ -849,6 +851,11 @@ static inline struct mem_cgroup *get_mem { return NULL; } + +static inline struct mem_cgroup *get_mem_cgroup_from_page(struct page *page) +{ + return NULL; +} static inline void mem_cgroup_put(struct mem_cgroup *memcg) { diff -puN mm/memcontrol.c~fs-mm-account-buffer_head-to-kmemcg mm/memcontrol.c --- a/mm/memcontrol.c~fs-mm-account-buffer_head-to-kmemcg +++ a/mm/memcontrol.c @@ -714,6 +714,28 @@ struct mem_cgroup *get_mem_cgroup_from_m EXPORT_SYMBOL(get_mem_cgroup_from_mm); /** + * get_mem_cgroup_from_page: Obtain a reference on given page's memcg. + * @page: page from which memcg should be extracted. + * + * Obtain a reference on page->memcg and returns it if successful. Otherwise + * root_mem_cgroup is returned. + */ +struct mem_cgroup *get_mem_cgroup_from_page(struct page *page) +{ + struct mem_cgroup *memcg = page->mem_cgroup; + + if (mem_cgroup_disabled()) + return NULL; + + rcu_read_lock(); + if (!memcg || !css_tryget_online(&memcg->css)) + memcg = root_mem_cgroup; + rcu_read_unlock(); + return memcg; +} +EXPORT_SYMBOL(get_mem_cgroup_from_page); + +/** * If current->active_memcg is non-NULL, do not fallback to current->mm->memcg. */ static __always_inline struct mem_cgroup *get_mem_cgroup_from_current(void) _ Patches currently in -mm which might be from shakeelb@xxxxxxxxxx are slub-track-number-of-slabs-irrespective-of-config_slub_debug.patch fs-fsnotify-account-fsnotify-metadata-to-kmemcg.patch fs-mm-account-buffer_head-to-kmemcg.patch -- To unsubscribe from this list: send the line "unsubscribe mm-commits" in the body of a message to majordomo@xxxxxxxxxxxxxxx More majordomo info at http://vger.kernel.org/majordomo-info.html