Skip to content

Commit f745c6f

Browse files
shakeelbtorvalds
authored andcommitted
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. [shakeelb@google.com: use __GFP_ACCOUNT for directed memcg charging] Link: http://lkml.kernel.org/r/20180702220208.213380-1-shakeelb@google.com Link: http://lkml.kernel.org/r/20180627191250.209150-3-shakeelb@google.com Signed-off-by: Shakeel Butt <shakeelb@google.com> Acked-by: Johannes Weiner <hannes@cmpxchg.org> Cc: Michal Hocko <mhocko@kernel.org> Cc: Jan Kara <jack@suse.cz> Cc: Amir Goldstein <amir73il@gmail.com> Cc: Greg Thelen <gthelen@google.com> Cc: Vladimir Davydov <vdavydov.dev@gmail.com> Cc: Roman Gushchin <guro@fb.com> Cc: Alexander Viro <viro@zeniv.linux.org.uk> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent d46eb14 commit f745c6f

3 files changed

Lines changed: 39 additions & 2 deletions

File tree

‎fs/buffer.c‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
#include <linux/mpage.h>
4646
#include <linux/bit_spinlock.h>
4747
#include <linux/pagevec.h>
48+
#include <linux/sched/mm.h>
4849
#include <trace/events/block.h>
4950

5051
static int fsync_buffers_list(spinlock_t *lock, struct list_head *list);
@@ -813,12 +814,16 @@ struct buffer_head *alloc_page_buffers(struct page *page, unsigned long size,
813814
bool retry)
814815
{
815816
struct buffer_head *bh, *head;
816-
gfp_t gfp = GFP_NOFS;
817+
gfp_t gfp = GFP_NOFS | __GFP_ACCOUNT;
817818
long offset;
819+
struct mem_cgroup *memcg;
818820

819821
if (retry)
820822
gfp |= __GFP_NOFAIL;
821823

824+
memcg = get_mem_cgroup_from_page(page);
825+
memalloc_use_memcg(memcg);
826+
822827
head = NULL;
823828
offset = PAGE_SIZE;
824829
while ((offset -= size) >= 0) {
@@ -835,6 +840,9 @@ struct buffer_head *alloc_page_buffers(struct page *page, unsigned long size,
835840
/* Link the buffer to its page */
836841
set_bh_page(bh, page, offset);
837842
}
843+
out:
844+
memalloc_unuse_memcg();
845+
mem_cgroup_put(memcg);
838846
return head;
839847
/*
840848
* In case anything failed, we just free everything we got.
@@ -848,7 +856,7 @@ struct buffer_head *alloc_page_buffers(struct page *page, unsigned long size,
848856
} while (head);
849857
}
850858

851-
return NULL;
859+
goto out;
852860
}
853861
EXPORT_SYMBOL_GPL(alloc_page_buffers);
854862

‎include/linux/memcontrol.h‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -375,6 +375,8 @@ struct mem_cgroup *mem_cgroup_from_task(struct task_struct *p);
375375

376376
struct mem_cgroup *get_mem_cgroup_from_mm(struct mm_struct *mm);
377377

378+
struct mem_cgroup *get_mem_cgroup_from_page(struct page *page);
379+
378380
static inline
379381
struct mem_cgroup *mem_cgroup_from_css(struct cgroup_subsys_state *css){
380382
return css ? container_of(css, struct mem_cgroup, css) : NULL;
@@ -863,6 +865,11 @@ static inline struct mem_cgroup *get_mem_cgroup_from_mm(struct mm_struct *mm)
863865
return NULL;
864866
}
865867

868+
static inline struct mem_cgroup *get_mem_cgroup_from_page(struct page *page)
869+
{
870+
return NULL;
871+
}
872+
866873
static inline void mem_cgroup_put(struct mem_cgroup *memcg)
867874
{
868875
}

‎mm/memcontrol.c‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -713,6 +713,28 @@ struct mem_cgroup *get_mem_cgroup_from_mm(struct mm_struct *mm)
713713
}
714714
EXPORT_SYMBOL(get_mem_cgroup_from_mm);
715715

716+
/**
717+
* get_mem_cgroup_from_page: Obtain a reference on given page's memcg.
718+
* @page: page from which memcg should be extracted.
719+
*
720+
* Obtain a reference on page->memcg and returns it if successful. Otherwise
721+
* root_mem_cgroup is returned.
722+
*/
723+
struct mem_cgroup *get_mem_cgroup_from_page(struct page *page)
724+
{
725+
struct mem_cgroup *memcg = page->mem_cgroup;
726+
727+
if (mem_cgroup_disabled())
728+
return NULL;
729+
730+
rcu_read_lock();
731+
if (!memcg || !css_tryget_online(&memcg->css))
732+
memcg = root_mem_cgroup;
733+
rcu_read_unlock();
734+
return memcg;
735+
}
736+
EXPORT_SYMBOL(get_mem_cgroup_from_page);
737+
716738
/**
717739
* If current->active_memcg is non-NULL, do not fallback to current->mm->memcg.
718740
*/

0 commit comments

Comments
 (0)