[PATCH 2/2] mm: allow certain address space to be not accounted by memcg

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Currently any page/folio added to an address space will be accounted by
memcg.

However this is not always true for certain address spaces.
One example is the address space of btrfs btree inode, which stores the
metadata of one btrfs.

Currently btrfs is using __GFP_NOFAIL for all operations touching that
address space, but this can has other problems as __GFP_NOFAIL can wait
infinitely until the request is met.

To avoid unnecessary memcg charge for the btree inode address space,
introduce a new flag, AS_NO_MEMCG, to inform that folios added to that
address space should not trigger memcg charge/uncharge, and add btrfs
btree inode to utilize that new flag.

Signed-off-by: Qu Wenruo <wqu@xxxxxxxx>
---
 fs/btrfs/disk-io.c      |  1 +
 include/linux/pagemap.h |  1 +
 mm/filemap.c            | 12 +++++++++---
 3 files changed, 11 insertions(+), 3 deletions(-)

diff --git a/fs/btrfs/disk-io.c b/fs/btrfs/disk-io.c
index 9e64e9fde832..2de9db95fbb9 100644
--- a/fs/btrfs/disk-io.c
+++ b/fs/btrfs/disk-io.c
@@ -1931,6 +1931,7 @@ static int btrfs_init_btree_inode(struct super_block *sb)
 	inode->i_size = OFFSET_MAX;
 	inode->i_mapping->a_ops = &btree_aops;
 	mapping_set_gfp_mask(inode->i_mapping, GFP_NOFS);
+	set_bit(AS_NO_MEMCG, &inode->i_mapping->flags);
 
 	extent_io_tree_init(fs_info, &BTRFS_I(inode)->io_tree,
 			    IO_TREE_BTREE_INODE_IO);
diff --git a/include/linux/pagemap.h b/include/linux/pagemap.h
index 59f1df0cde5a..d4634d5d5dab 100644
--- a/include/linux/pagemap.h
+++ b/include/linux/pagemap.h
@@ -209,6 +209,7 @@ enum mapping_flags {
 	AS_STABLE_WRITES,	/* must wait for writeback before modifying
 				   folio contents */
 	AS_UNMOVABLE,		/* The mapping cannot be moved, ever */
+	AS_NO_MEMCG,		/* No memcg should be applied to this address space .*/
 };
 
 /**
diff --git a/mm/filemap.c b/mm/filemap.c
index 876cc64aadd7..a2282aa4f2a6 100644
--- a/mm/filemap.c
+++ b/mm/filemap.c
@@ -960,11 +960,17 @@ int filemap_add_folio(struct address_space *mapping, struct folio *folio,
 				pgoff_t index, gfp_t gfp)
 {
 	void *shadow = NULL;
+	const bool no_memcg = test_bit(AS_NO_MEMCG, &mapping->flags);
 	int ret;
 
-	ret = mem_cgroup_charge(folio, NULL, gfp);
-	if (ret)
-		return ret;
+	if (!no_memcg) {
+		ret = mem_cgroup_charge(folio, NULL, gfp);
+		if (ret)
+			return ret;
+	} else {
+		/* The page should not has any memcg set for it. */
+		VM_BUG_ON_FOLIO(folio_memcg(folio), folio);
+	}
 
 	__folio_set_locked(folio);
 	ret = __filemap_add_folio(mapping, folio, index, gfp, &shadow);
-- 
2.45.2





[Index of Archives]     [Linux ARM Kernel]     [Linux ARM]     [Linux Omap]     [Fedora ARM]     [IETF Annouce]     [Bugtraq]     [Linux OMAP]     [Linux MIPS]     [eCos]     [Asterisk Internet PBX]     [Linux API]

  Powered by Linux