diff mbox series

[v5,1/2] btrfs: always uses root memcgroup for filemap_add_folio()

Message ID d408a1b35307101e82a6845e26af4a122c8e5a25.1721363035.git.wqu@suse.com (mailing list archive)
State New, archived
Headers show
Series btrfs: try to allocate larger folios for metadata | expand

Commit Message

Qu Wenruo July 19, 2024, 4:49 a.m. UTC
The function filemap_add_folio() would also charge the memory cgroup,
as all filemap folios are controlled by memory cgroup.
This also means, if there is some memory cgroup set, we're only relying
on the __GFP_NOFAIL flag to prevent mem cgroup to return -ENOMEM.

However this is not really that suitable for btrfs btree inode, as the
btree inode is not accessible out of btrfs module, and we do not want
the memory limits on this critical part of btrfs metadata allocation.

So here we just manually set the active memory cgroup to the root one,
which has no memory limits or whatever, before calling
filemap_add_folio(), then restore to the old memcgroup.

By this we avoid the unnecessary memory limits and can later remove the
__GFP_NOFAIL usage.

Suggested-by: Michal Hocko <mhocko@suse.com>
Suggested-by: Vlastimil Babka (SUSE) <vbabka@kernel.org>
Signed-off-by: Qu Wenruo <wqu@suse.com>
---
 fs/btrfs/extent_io.c | 10 ++++++++++
 1 file changed, 10 insertions(+)

Comments

Michal Hocko July 19, 2024, 7:08 a.m. UTC | #1
On Fri 19-07-24 14:19:05, Qu Wenruo wrote:
[...]
> @@ -2981,8 +2982,17 @@ static int attach_eb_folio_to_filemap(struct extent_buffer *eb, int i,
>  	ASSERT(eb->folios[i]);
>  
>  retry:
> +	/*
> +	 * Btree inode is a btrfs internal inode, and not exposed to any
> +	 * user.
> +	 * Furthermore we do not want any cgroup limits on this inode.
> +	 * So we always use root_mem_cgroup as our active memcg when attaching
> +	 * the folios.
> +	 */
> +	old_memcg = set_active_memcg(root_mem_cgroup);
>  	ret = filemap_add_folio(mapping, eb->folios[i], index + i,
>  				GFP_NOFS | __GFP_NOFAIL);
> +	set_active_memcg(old_memcg);

I do not think this will compile with CONFIG_MEMCG=n

>  	if (!ret)
>  		goto finish;
>  
> -- 
> 2.45.2
Qu Wenruo July 19, 2024, 7:16 a.m. UTC | #2
在 2024/7/19 16:38, Michal Hocko 写道:
> On Fri 19-07-24 14:19:05, Qu Wenruo wrote:
> [...]
>> @@ -2981,8 +2982,17 @@ static int attach_eb_folio_to_filemap(struct extent_buffer *eb, int i,
>>   	ASSERT(eb->folios[i]);
>>
>>   retry:
>> +	/*
>> +	 * Btree inode is a btrfs internal inode, and not exposed to any
>> +	 * user.
>> +	 * Furthermore we do not want any cgroup limits on this inode.
>> +	 * So we always use root_mem_cgroup as our active memcg when attaching
>> +	 * the folios.
>> +	 */
>> +	old_memcg = set_active_memcg(root_mem_cgroup);
>>   	ret = filemap_add_folio(mapping, eb->folios[i], index + i,
>>   				GFP_NOFS | __GFP_NOFAIL);
>> +	set_active_memcg(old_memcg);
>
> I do not think this will compile with CONFIG_MEMCG=n

My bad.

I'll fix it from the btrfs part so that @root_mem_cgroup would be
changed to NULL for the CONFIG_MEMCG=n case.

Meanwhile, can we just define root_mem_cgroup as NULL globally?
That looks more reasonable to me, and if that's fine I can send out an
extra patch just for that.

Thanks,
Qu

>
>>   	if (!ret)
>>   		goto finish;
>>
>> --
>> 2.45.2
>
Qu Wenruo July 19, 2024, 7:20 a.m. UTC | #3
在 2024/7/19 16:46, Qu Wenruo 写道:
> 
> 
> 在 2024/7/19 16:38, Michal Hocko 写道:
>> On Fri 19-07-24 14:19:05, Qu Wenruo wrote:
>> [...]
>>> @@ -2981,8 +2982,17 @@ static int attach_eb_folio_to_filemap(struct 
>>> extent_buffer *eb, int i,
>>>       ASSERT(eb->folios[i]);
>>>
>>>   retry:
>>> +    /*
>>> +     * Btree inode is a btrfs internal inode, and not exposed to any
>>> +     * user.
>>> +     * Furthermore we do not want any cgroup limits on this inode.
>>> +     * So we always use root_mem_cgroup as our active memcg when 
>>> attaching
>>> +     * the folios.
>>> +     */
>>> +    old_memcg = set_active_memcg(root_mem_cgroup);
>>>       ret = filemap_add_folio(mapping, eb->folios[i], index + i,
>>>                   GFP_NOFS | __GFP_NOFAIL);
>>> +    set_active_memcg(old_memcg);
>>
>> I do not think this will compile with CONFIG_MEMCG=n
> 
> My bad.
> 
> I'll fix it from the btrfs part so that @root_mem_cgroup would be
> changed to NULL for the CONFIG_MEMCG=n case.
> 
> Meanwhile, can we just define root_mem_cgroup as NULL globally?

Of course, I mean for the CONFIG_MEMCG=n case.

Thanks,
Qu

> That looks more reasonable to me, and if that's fine I can send out an
> extra patch just for that.
> 
> Thanks,
> Qu
> 
>>
>>>       if (!ret)
>>>           goto finish;
>>>
>>> -- 
>>> 2.45.2
>>
Michal Hocko July 19, 2024, 7:26 a.m. UTC | #4
On Fri 19-07-24 16:46:50, Qu Wenruo wrote:
> Meanwhile, can we just define root_mem_cgroup as NULL globally?
> That looks more reasonable to me, and if that's fine I can send out an
> extra patch just for that.

I think it would be sufficient to pull root_mem_cgroup declaration out
of ifdef in memcontrol.h. We do not really have to define it. Please
send the patch and add all memcg maintainers. Ideally with this patch in
the series so that the intention is clear.
kernel test robot July 19, 2024, 3:42 p.m. UTC | #5
Hi Qu,

kernel test robot noticed the following build errors:

[auto build test ERROR on kdave/for-next]
[also build test ERROR on linus/master v6.10 next-20240719]
[If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]

url:    https://github.com/intel-lab-lkp/linux/commits/Qu-Wenruo/btrfs-always-uses-root-memcgroup-for-filemap_add_folio/20240719-125131
base:   https://git.kernel.org/pub/scm/linux/kernel/git/kdave/linux.git for-next
patch link:    https://lore.kernel.org/r/d408a1b35307101e82a6845e26af4a122c8e5a25.1721363035.git.wqu%40suse.com
patch subject: [PATCH v5 1/2] btrfs: always uses root memcgroup for filemap_add_folio()
config: sh-allmodconfig (https://download.01.org/0day-ci/archive/20240719/202407192308.yzjpnura-lkp@intel.com/config)
compiler: sh4-linux-gcc (GCC) 14.1.0
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20240719/202407192308.yzjpnura-lkp@intel.com/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of
the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <lkp@intel.com>
| Closes: https://lore.kernel.org/oe-kbuild-all/202407192308.yzjpnura-lkp@intel.com/

All errors (new ones prefixed by >>, old ones prefixed by <<):

WARNING: modpost: missing MODULE_DESCRIPTION() in drivers/hid/hid-samsung.o
WARNING: modpost: missing MODULE_DESCRIPTION() in drivers/hid/hid-semitek.o
WARNING: modpost: missing MODULE_DESCRIPTION() in drivers/hid/hid-sjoy.o
WARNING: modpost: missing MODULE_DESCRIPTION() in drivers/hid/hid-sony.o
WARNING: modpost: missing MODULE_DESCRIPTION() in drivers/hid/hid-speedlink.o
WARNING: modpost: missing MODULE_DESCRIPTION() in drivers/hid/hid-steam.o
WARNING: modpost: missing MODULE_DESCRIPTION() in drivers/hid/hid-steelseries.o
WARNING: modpost: missing MODULE_DESCRIPTION() in drivers/hid/hid-sunplus.o
WARNING: modpost: missing MODULE_DESCRIPTION() in drivers/hid/hid-gaff.o
WARNING: modpost: missing MODULE_DESCRIPTION() in drivers/hid/hid-tmff.o
WARNING: modpost: missing MODULE_DESCRIPTION() in drivers/hid/hid-tivo.o
WARNING: modpost: missing MODULE_DESCRIPTION() in drivers/hid/hid-topseed.o
WARNING: modpost: missing MODULE_DESCRIPTION() in drivers/hid/hid-twinhan.o
WARNING: modpost: missing MODULE_DESCRIPTION() in drivers/hid/hid-uclogic.o
WARNING: modpost: missing MODULE_DESCRIPTION() in drivers/hid/hid-xinmo.o
WARNING: modpost: missing MODULE_DESCRIPTION() in drivers/hid/hid-zpff.o
WARNING: modpost: missing MODULE_DESCRIPTION() in drivers/hid/hid-zydacron.o
WARNING: modpost: missing MODULE_DESCRIPTION() in drivers/hid/hid-viewsonic.o
WARNING: modpost: missing MODULE_DESCRIPTION() in drivers/hid/hid-waltop.o
WARNING: modpost: missing MODULE_DESCRIPTION() in drivers/hid/hid-winwing.o
WARNING: modpost: missing MODULE_DESCRIPTION() in drivers/staging/fbtft/fbtft.o
WARNING: modpost: missing MODULE_DESCRIPTION() in drivers/staging/greybus/gb-bootrom.o
WARNING: modpost: missing MODULE_DESCRIPTION() in drivers/staging/greybus/gb-spilib.o
WARNING: modpost: missing MODULE_DESCRIPTION() in drivers/staging/greybus/gb-hid.o
WARNING: modpost: missing MODULE_DESCRIPTION() in drivers/staging/greybus/gb-light.o
WARNING: modpost: missing MODULE_DESCRIPTION() in drivers/staging/greybus/gb-log.o
WARNING: modpost: missing MODULE_DESCRIPTION() in drivers/staging/greybus/gb-loopback.o
WARNING: modpost: missing MODULE_DESCRIPTION() in drivers/staging/greybus/gb-power-supply.o
WARNING: modpost: missing MODULE_DESCRIPTION() in drivers/staging/greybus/gb-raw.o
WARNING: modpost: missing MODULE_DESCRIPTION() in drivers/staging/greybus/gb-vibrator.o
WARNING: modpost: missing MODULE_DESCRIPTION() in drivers/staging/greybus/gb-audio-manager.o
WARNING: modpost: missing MODULE_DESCRIPTION() in drivers/staging/greybus/gb-gbphy.o
WARNING: modpost: missing MODULE_DESCRIPTION() in drivers/staging/greybus/gb-gpio.o
WARNING: modpost: missing MODULE_DESCRIPTION() in drivers/staging/greybus/gb-i2c.o
WARNING: modpost: missing MODULE_DESCRIPTION() in drivers/staging/greybus/gb-pwm.o
WARNING: modpost: missing MODULE_DESCRIPTION() in drivers/staging/greybus/gb-sdio.o
WARNING: modpost: missing MODULE_DESCRIPTION() in drivers/staging/greybus/gb-spi.o
WARNING: modpost: missing MODULE_DESCRIPTION() in drivers/staging/greybus/gb-uart.o
WARNING: modpost: missing MODULE_DESCRIPTION() in drivers/staging/greybus/gb-usb.o
WARNING: modpost: missing MODULE_DESCRIPTION() in drivers/platform/chrome/cros_kunit_proto_test.o
WARNING: modpost: missing MODULE_DESCRIPTION() in drivers/mailbox/mtk-cmdq-mailbox.o
WARNING: modpost: missing MODULE_DESCRIPTION() in drivers/devfreq/governor_simpleondemand.o
WARNING: modpost: missing MODULE_DESCRIPTION() in drivers/devfreq/governor_performance.o
WARNING: modpost: missing MODULE_DESCRIPTION() in drivers/devfreq/governor_powersave.o
WARNING: modpost: missing MODULE_DESCRIPTION() in drivers/devfreq/governor_userspace.o
WARNING: modpost: missing MODULE_DESCRIPTION() in drivers/perf/arm-ccn.o
WARNING: modpost: missing MODULE_DESCRIPTION() in drivers/perf/fsl_imx8_ddr_perf.o
WARNING: modpost: missing MODULE_DESCRIPTION() in drivers/perf/arm_cspmu/arm_cspmu_module.o
WARNING: modpost: missing MODULE_DESCRIPTION() in drivers/perf/arm_cspmu/nvidia_cspmu.o
WARNING: modpost: missing MODULE_DESCRIPTION() in drivers/perf/arm_cspmu/ampere_cspmu.o
WARNING: modpost: missing MODULE_DESCRIPTION() in drivers/nvmem/nvmem-apple-efuses.o
WARNING: modpost: missing MODULE_DESCRIPTION() in drivers/nvmem/nvmem_brcm_nvram.o
WARNING: modpost: missing MODULE_DESCRIPTION() in drivers/nvmem/nvmem_u-boot-env.o
WARNING: modpost: missing MODULE_DESCRIPTION() in drivers/interconnect/imx/imx-interconnect.o
WARNING: modpost: missing MODULE_DESCRIPTION() in drivers/interconnect/imx/imx8mm-interconnect.o
WARNING: modpost: missing MODULE_DESCRIPTION() in drivers/interconnect/imx/imx8mq-interconnect.o
WARNING: modpost: missing MODULE_DESCRIPTION() in drivers/interconnect/imx/imx8mn-interconnect.o
WARNING: modpost: missing MODULE_DESCRIPTION() in drivers/interconnect/imx/imx8mp-interconnect.o
WARNING: modpost: missing MODULE_DESCRIPTION() in drivers/hte/hte-tegra194-test.o
WARNING: modpost: missing MODULE_DESCRIPTION() in drivers/vdpa/vdpa.o
WARNING: modpost: missing MODULE_DESCRIPTION() in drivers/parport/parport.o
WARNING: modpost: missing MODULE_DESCRIPTION() in drivers/mtd/parsers/brcm_u-boot.o
WARNING: modpost: missing MODULE_DESCRIPTION() in drivers/mtd/parsers/tplink_safeloader.o
WARNING: modpost: missing MODULE_DESCRIPTION() in drivers/mtd/chips/cfi_util.o
WARNING: modpost: missing MODULE_DESCRIPTION() in drivers/mtd/chips/cfi_cmdset_0020.o
WARNING: modpost: missing MODULE_DESCRIPTION() in drivers/mtd/maps/map_funcs.o
WARNING: modpost: missing MODULE_DESCRIPTION() in drivers/spmi/hisi-spmi-controller.o
WARNING: modpost: missing MODULE_DESCRIPTION() in drivers/spmi/spmi-pmic-arb.o
WARNING: modpost: missing MODULE_DESCRIPTION() in drivers/pcmcia/pcmcia_rsrc.o
WARNING: modpost: missing MODULE_DESCRIPTION() in drivers/hwmon/corsair-cpro.o
WARNING: modpost: missing MODULE_DESCRIPTION() in drivers/hwmon/mr75203.o
WARNING: modpost: missing MODULE_DESCRIPTION() in drivers/greybus/greybus.o
WARNING: modpost: missing MODULE_DESCRIPTION() in drivers/greybus/gb-es2.o
WARNING: modpost: missing MODULE_DESCRIPTION() in drivers/rpmsg/rpmsg_char.o
WARNING: modpost: missing MODULE_DESCRIPTION() in drivers/iio/adc/ingenic-adc.o
WARNING: modpost: missing MODULE_DESCRIPTION() in drivers/iio/adc/xilinx-ams.o
WARNING: modpost: missing MODULE_DESCRIPTION() in drivers/iio/buffer/kfifo_buf.o
WARNING: modpost: missing MODULE_DESCRIPTION() in drivers/fsi/fsi-core.o
WARNING: modpost: missing MODULE_DESCRIPTION() in drivers/fsi/fsi-master-hub.o
WARNING: modpost: missing MODULE_DESCRIPTION() in drivers/fsi/fsi-master-aspeed.o
WARNING: modpost: missing MODULE_DESCRIPTION() in drivers/fsi/fsi-master-gpio.o
WARNING: modpost: missing MODULE_DESCRIPTION() in drivers/fsi/fsi-master-ast-cf.o
WARNING: modpost: missing MODULE_DESCRIPTION() in drivers/fsi/fsi-scom.o
WARNING: modpost: missing MODULE_DESCRIPTION() in drivers/siox/siox-bus-gpio.o
WARNING: modpost: missing MODULE_DESCRIPTION() in drivers/counter/ftm-quaddec.o
WARNING: modpost: missing MODULE_DESCRIPTION() in samples/vfio-mdev/mtty.o
WARNING: modpost: missing MODULE_DESCRIPTION() in samples/vfio-mdev/mdpy.o
WARNING: modpost: missing MODULE_DESCRIPTION() in samples/vfio-mdev/mdpy-fb.o
WARNING: modpost: missing MODULE_DESCRIPTION() in samples/vfio-mdev/mbochs.o
WARNING: modpost: missing MODULE_DESCRIPTION() in samples/configfs/configfs_sample.o
WARNING: modpost: missing MODULE_DESCRIPTION() in samples/kfifo/bytestream-example.o
WARNING: modpost: missing MODULE_DESCRIPTION() in samples/kfifo/dma-example.o
WARNING: modpost: missing MODULE_DESCRIPTION() in samples/kfifo/inttype-example.o
WARNING: modpost: missing MODULE_DESCRIPTION() in samples/kfifo/record-example.o
WARNING: modpost: missing MODULE_DESCRIPTION() in samples/kobject/kobject-example.o
WARNING: modpost: missing MODULE_DESCRIPTION() in samples/kobject/kset-example.o
WARNING: modpost: missing MODULE_DESCRIPTION() in samples/kprobes/kprobe_example.o
WARNING: modpost: missing MODULE_DESCRIPTION() in samples/kprobes/kretprobe_example.o
WARNING: modpost: missing MODULE_DESCRIPTION() in samples/kmemleak/kmemleak-test.o
WARNING: modpost: missing MODULE_DESCRIPTION() in arch/sh/drivers/push-switch.o
>> ERROR: modpost: "root_mem_cgroup" [fs/btrfs/btrfs.ko] undefined!
ERROR: modpost: "__delay" [drivers/net/mdio/mdio-cavium.ko] undefined!
ERROR: modpost: "devm_of_clk_add_hw_provider" [drivers/media/i2c/tc358746.ko] undefined!
ERROR: modpost: "devm_clk_hw_register" [drivers/media/i2c/tc358746.ko] undefined!
ERROR: modpost: "of_clk_hw_simple_get" [drivers/media/i2c/tc358746.ko] undefined!
kernel test robot July 19, 2024, 3:42 p.m. UTC | #6
Hi Qu,

kernel test robot noticed the following build errors:

[auto build test ERROR on kdave/for-next]
[also build test ERROR on linus/master v6.10 next-20240719]
[If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]

url:    https://github.com/intel-lab-lkp/linux/commits/Qu-Wenruo/btrfs-always-uses-root-memcgroup-for-filemap_add_folio/20240719-125131
base:   https://git.kernel.org/pub/scm/linux/kernel/git/kdave/linux.git for-next
patch link:    https://lore.kernel.org/r/d408a1b35307101e82a6845e26af4a122c8e5a25.1721363035.git.wqu%40suse.com
patch subject: [PATCH v5 1/2] btrfs: always uses root memcgroup for filemap_add_folio()
config: x86_64-buildonly-randconfig-002-20240719 (https://download.01.org/0day-ci/archive/20240719/202407192346.BjAEmrYl-lkp@intel.com/config)
compiler: clang version 18.1.5 (https://github.com/llvm/llvm-project 617a15a9eac96088ae5e9134248d8236e34b91b1)
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20240719/202407192346.BjAEmrYl-lkp@intel.com/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of
the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <lkp@intel.com>
| Closes: https://lore.kernel.org/oe-kbuild-all/202407192346.BjAEmrYl-lkp@intel.com/

All errors (new ones prefixed by >>):

>> fs/btrfs/extent_io.c:2992:31: error: use of undeclared identifier 'root_mem_cgroup'; did you mean 'parent_mem_cgroup'?
    2992 |         old_memcg = set_active_memcg(root_mem_cgroup);
         |                                      ^~~~~~~~~~~~~~~
         |                                      parent_mem_cgroup
   include/linux/memcontrol.h:1288:34: note: 'parent_mem_cgroup' declared here
    1288 | static inline struct mem_cgroup *parent_mem_cgroup(struct mem_cgroup *memcg)
         |                                  ^
   1 error generated.


vim +2992 fs/btrfs/extent_io.c

  2971	
  2972		struct btrfs_fs_info *fs_info = eb->fs_info;
  2973		struct address_space *mapping = fs_info->btree_inode->i_mapping;
  2974		struct mem_cgroup *old_memcg;
  2975		const unsigned long index = eb->start >> PAGE_SHIFT;
  2976		struct folio *existing_folio = NULL;
  2977		int ret;
  2978	
  2979		ASSERT(found_eb_ret);
  2980	
  2981		/* Caller should ensure the folio exists. */
  2982		ASSERT(eb->folios[i]);
  2983	
  2984	retry:
  2985		/*
  2986		 * Btree inode is a btrfs internal inode, and not exposed to any
  2987		 * user.
  2988		 * Furthermore we do not want any cgroup limits on this inode.
  2989		 * So we always use root_mem_cgroup as our active memcg when attaching
  2990		 * the folios.
  2991		 */
> 2992		old_memcg = set_active_memcg(root_mem_cgroup);
  2993		ret = filemap_add_folio(mapping, eb->folios[i], index + i,
  2994					GFP_NOFS | __GFP_NOFAIL);
  2995		set_active_memcg(old_memcg);
  2996		if (!ret)
  2997			goto finish;
  2998	
  2999		existing_folio = filemap_lock_folio(mapping, index + i);
  3000		/* The page cache only exists for a very short time, just retry. */
  3001		if (IS_ERR(existing_folio)) {
  3002			existing_folio = NULL;
  3003			goto retry;
  3004		}
  3005	
  3006		/* For now, we should only have single-page folios for btree inode. */
  3007		ASSERT(folio_nr_pages(existing_folio) == 1);
  3008	
  3009		if (folio_size(existing_folio) != eb->folio_size) {
  3010			folio_unlock(existing_folio);
  3011			folio_put(existing_folio);
  3012			return -EAGAIN;
  3013		}
  3014	
  3015	finish:
  3016		spin_lock(&mapping->i_private_lock);
  3017		if (existing_folio && fs_info->nodesize < PAGE_SIZE) {
  3018			/* We're going to reuse the existing page, can drop our folio now. */
  3019			__free_page(folio_page(eb->folios[i], 0));
  3020			eb->folios[i] = existing_folio;
  3021		} else if (existing_folio) {
  3022			struct extent_buffer *existing_eb;
  3023	
  3024			existing_eb = grab_extent_buffer(fs_info,
  3025							 folio_page(existing_folio, 0));
  3026			if (existing_eb) {
  3027				/* The extent buffer still exists, we can use it directly. */
  3028				*found_eb_ret = existing_eb;
  3029				spin_unlock(&mapping->i_private_lock);
  3030				folio_unlock(existing_folio);
  3031				folio_put(existing_folio);
  3032				return 1;
  3033			}
  3034			/* The extent buffer no longer exists, we can reuse the folio. */
  3035			__free_page(folio_page(eb->folios[i], 0));
  3036			eb->folios[i] = existing_folio;
  3037		}
  3038		eb->folio_size = folio_size(eb->folios[i]);
  3039		eb->folio_shift = folio_shift(eb->folios[i]);
  3040		/* Should not fail, as we have preallocated the memory. */
  3041		ret = attach_extent_buffer_folio(eb, eb->folios[i], prealloc);
  3042		ASSERT(!ret);
  3043		/*
  3044		 * To inform we have an extra eb under allocation, so that
  3045		 * detach_extent_buffer_page() won't release the folio private when the
  3046		 * eb hasn't been inserted into radix tree yet.
  3047		 *
  3048		 * The ref will be decreased when the eb releases the page, in
  3049		 * detach_extent_buffer_page().  Thus needs no special handling in the
  3050		 * error path.
  3051		 */
  3052		btrfs_folio_inc_eb_refs(fs_info, eb->folios[i]);
  3053		spin_unlock(&mapping->i_private_lock);
  3054		return 0;
  3055	}
  3056
diff mbox series

Patch

diff --git a/fs/btrfs/extent_io.c b/fs/btrfs/extent_io.c
index aa7f8148cd0d..cfeed7673009 100644
--- a/fs/btrfs/extent_io.c
+++ b/fs/btrfs/extent_io.c
@@ -2971,6 +2971,7 @@  static int attach_eb_folio_to_filemap(struct extent_buffer *eb, int i,
 
 	struct btrfs_fs_info *fs_info = eb->fs_info;
 	struct address_space *mapping = fs_info->btree_inode->i_mapping;
+	struct mem_cgroup *old_memcg;
 	const unsigned long index = eb->start >> PAGE_SHIFT;
 	struct folio *existing_folio = NULL;
 	int ret;
@@ -2981,8 +2982,17 @@  static int attach_eb_folio_to_filemap(struct extent_buffer *eb, int i,
 	ASSERT(eb->folios[i]);
 
 retry:
+	/*
+	 * Btree inode is a btrfs internal inode, and not exposed to any
+	 * user.
+	 * Furthermore we do not want any cgroup limits on this inode.
+	 * So we always use root_mem_cgroup as our active memcg when attaching
+	 * the folios.
+	 */
+	old_memcg = set_active_memcg(root_mem_cgroup);
 	ret = filemap_add_folio(mapping, eb->folios[i], index + i,
 				GFP_NOFS | __GFP_NOFAIL);
+	set_active_memcg(old_memcg);
 	if (!ret)
 		goto finish;