memcg: charge before adding to swapcache on swapin
Subsystems (5)
PATCH v2 1/1 memcg: charge before adding to swapcache on swapin
Currently the kernel adds the page, allocated for swapin, to the swapcache before charging the page. This is fine but now we want a per-memcg swapcache stat which is essential for folks who wants to transparently migrate from cgroup v1's memsw to cgroup v2's memory and swap counters. In addition charging a page before exposing it to other parts of the kernel is a step in the right direction.
To correctly maintain the per-memcg swapcache stat, this patch has adopted to charge the page before adding it to swapcache. One challenge in this option is the failure case of add_to_swap_cache() on which we need to undo the mem_cgroup_charge(). Specifically undoing mem_cgroup_uncharge_swap() is not simple.
To resolve the issue, this patch introduces transaction like interface to charge a page for swapin. The function mem_cgroup_charge_swapin_page() initiates the charging of the page and mem_cgroup_finish_swapin_page() completes the charging process. So, the kernel starts the charging process of the page for swapin with mem_cgroup_charge_swapin_page(), adds the page to the swapcache and on success completes the charging process with mem_cgroup_finish_swapin_page().
---
Changes since v1:
- Removes __GFP_NOFAIL and introduced transaction interface for charging (suggested by Johannes)
- Updated the commit message
include/linux/memcontrol.h | 14 +++++ mm/memcontrol.c | 116 +++++++++++++++++++++++-------------- mm/memory.c | 14 ++--- mm/swap_state.c | 11 ++-- 4 files changed, 97 insertions(+), 58 deletions(-)
include/linux/memcontrol.h +14 -0
@@ -596,6 +596,9 @@ static inline bool mem_cgroup_below_min(struct mem_cgroup *memcg) }int mem_cgroup_charge (struct page *page,struct mm_struct *mm,gfp_t gfp_mask); +int mem_cgroup_charge_swapin_page (struct page *page,struct mm_struct *mm, +gfp_t gfp,swp_entry_t entry); +void mem_cgroup_finish_swapin_page (struct page *page,swp_entry_t entry);void mem_cgroup_uncharge (struct page *page);void mem_cgroup_uncharge_list (struct list_head *page_list);
@@ -1141,6 +1144,17 @@ static inline int mem_cgroup_charge(struct page *page, struct mm_struct *mm,return 0 ; } +static inline int mem_cgroup_charge_swapin_page (struct page *page, +struct mm_struct *mm,gfp_t gfp,swp_entry_t entry); +{ +return 0 ; +} + +static inline void mem_cgroup_finish_swapin_page (struct page *page, +swp_entry_t entry) +{ +} +static inline void mem_cgroup_uncharge (struct page *page) { }
mm/memcontrol.c +73 -43
154 lines not on this page · raw message
mm/memory.c +4 -10
@@ -3307,21 +3307,15 @@ vm_fault_t do_swap_page(struct vm_fault *vmf) page =alloc_page_vma (GFP_HIGHUSER_MOVABLE , vma, vmf->address);if (page) { -int err; -__SetPageLocked (page);__SetPageSwapBacked (page); -set_page_private (page, entry.val); - -/* Tell memcg to use swap ownership records */ -SetPageSwapCache (page); - err =mem_cgroup_charge (page, vma->vm_mm, -GFP_KERNEL ); -ClearPageSwapCache (page); -if (err) { + +if (mem_cgroup_charge_swapin_page (page, + vma->vm_mm,GFP_KERNEL , entry)) { ret =VM_FAULT_OOM ; goto out_page; } +mem_cgroup_finish_swapin_page (page, entry); shadow =get_shadow_from_swap_cache (entry);if (shadow)
mm/swap_state.c +6 -5
@@ -497,16 +497,16 @@ struct page *__read_swap_cache_async(swp_entry_t entry, gfp_t gfp_mask,__SetPageLocked (page);__SetPageSwapBacked (page); +if (mem_cgroup_charge_swapin_page (page,NULL , gfp_mask, entry)) + goto fail_unlock; +/* May fail (-ENOMEM) if XArray node allocation failed. */ if (add_to_swap_cache (page, entry, gfp_mask &GFP_RECLAIM_MASK , &shadow)) { -put_swap_page (page, entry); +mem_cgroup_uncharge (page); goto fail_unlock; } -if (mem_cgroup_charge (page,NULL , gfp_mask)) { -delete_from_swap_cache (page); - goto fail_unlock; - } +mem_cgroup_finish_swapin_page (page, entry);if (shadow)workingset_refault (page, shadow);
@@ -517,6 +517,7 @@ struct page *__read_swap_cache_async(swp_entry_t entry, gfp_t gfp_mask,return page; fail_unlock: +put_swap_page (page, entry);unlock_page (page);put_page (page);return NULL ;
Signature · 1 line
2.30.0.617.g56c4b15f3c-goog
kernel test robot
build botThank you for the patch! Yet something to improve:
Hi Shakeel,
Thank you for the patch! Yet something to improve:
[auto build test ERROR on next-20210222]
[cannot apply to linus/master hnaz-linux-mm/master v5.11 v5.11-rc7 v5.11-rc6 v5.11] [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]
url: https://github.com/0day-ci/linux/commits/Shakeel-Butt/memcg-charge-before-adding-to-swapcache-on-swapin/20210223-135711 base: 37dfbfbdca66834bc0f64ec9b35e09ac6c8898da config: x86_64-randconfig-m031-20210223 (attached as .config) compiler: gcc-9 (Debian 9.3.0-15) 9.3.0 reproduce (this is a W=1 build): # https://github.com/0day-ci/linux/commit/7ad6fb47580886394809b563b7476954a35f3054 git remote add linux-review https://github.com/0day-ci/linux git fetch --no-tags linux-review Shakeel-Butt/memcg-charge-before-adding-to-swapcache-on-swapin/20210223-135711 git checkout 7ad6fb47580886394809b563b7476954a35f3054 # save the attached .config to linux build tree make W=1 ARCH=x86_64
If you fix the issue, kindly add following tag as appropriate
All errors (new ones prefixed by >>):
In file included from include/linux/swap.h:9,
from include/linux/suspend.h:5,
from arch/x86/kernel/asm-offsets.c:13:
>> include/linux/memcontrol.h:1149:1: error: expected identifier or '(' before '{' token
1149 | {
| ^ include/linux/memcontrol.h:1147:19: warning: 'mem_cgroup_charge_swapin_page' declared 'static' but never defined [-Wunused-function]
1147 | static inline int mem_cgroup_charge_swapin_page(struct page *page,
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
--
In file included from include/linux/swap.h:9,
from include/linux/suspend.h:5,
from arch/x86/kernel/asm-offsets.c:13:
>> include/linux/memcontrol.h:1149:1: error: expected identifier or '(' before '{' token
1149 | {
| ^ include/linux/memcontrol.h:1147:19: warning: 'mem_cgroup_charge_swapin_page' declared 'static' but never defined [-Wunused-function]
1147 | static inline int mem_cgroup_charge_swapin_page(struct page *page,
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
make[2]: *** [scripts/Makefile.build:117: arch/x86/kernel/asm-offsets.s] Error 1
make[2]: Target '__build' not remade because of errors.
make[1]: *** [Makefile:1228: prepare0] Error 2
make[1]: Target 'modules_prepare' not remade because of errors.
make: *** [Makefile:185: __sub-make] Error 2
make: Target 'modules_prepare' not remade because of errors.
--
In file included from include/linux/swap.h:9,
from include/linux/suspend.h:5,
from arch/x86/kernel/asm-offsets.c:13:
>> include/linux/memcontrol.h:1149:1: error: expected identifier or '(' before '{' token
1149 | {
| ^ include/linux/memcontrol.h:1147:19: warning: 'mem_cgroup_charge_swapin_page' declared 'static' but never defined [-Wunused-function]
1147 | static inline int mem_cgroup_charge_swapin_page(struct page *page,
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
make[2]: *** [scripts/Makefile.build:117: arch/x86/kernel/asm-offsets.s] Error 1
make[2]: Target '__build' not remade because of errors.
make[1]: *** [Makefile:1228: prepare0] Error 2
make[1]: Target 'prepare' not remade because of errors.
make: *** [Makefile:185: __sub-make] Error 2
make: Target 'prepare' not remade because of errors.
vim +1149 include/linux/memcontrol.h
1146
1147 static inline int mem_cgroup_charge_swapin_page(struct page *page,
1148 struct mm_struct *mm, gfp_t gfp, swp_entry_t entry);
> 1149 { 1150 return 0;
1151 }
1152
---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/[email protected]
kernel test robot
build botThank you for the patch! Perhaps something to improve:
Hi Shakeel,
Thank you for the patch! Perhaps something to improve:
[auto build test WARNING on next-20210222]
[cannot apply to linus/master hnaz-linux-mm/master v5.11 v5.11-rc7 v5.11-rc6 v5.11] [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]
url: https://github.com/0day-ci/linux/commits/Shakeel-Butt/memcg-charge-before-adding-to-swapcache-on-swapin/20210223-135711 base: 37dfbfbdca66834bc0f64ec9b35e09ac6c8898da config: arm-colibri_pxa300_defconfig (attached as .config) compiler: arm-linux-gnueabi-gcc (GCC) 9.3.0
reproduce (this is a W=1 build):
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross chmod +x ~/bin/make.cross
# https://github.com/0day-ci/linux/commit/7ad6fb47580886394809b563b7476954a35f3054
git remote add linux-review https://github.com/0day-ci/linux
git fetch --no-tags linux-review Shakeel-Butt/memcg-charge-before-adding-to-swapcache-on-swapin/20210223-135711 git checkout 7ad6fb47580886394809b563b7476954a35f3054
# save the attached .config to linux build tree
COMPILER_INSTALL_PATH=$HOME/0day COMPILER=gcc-9.3.0 make.cross ARCH=arm
If you fix the issue, kindly add following tag as appropriate
All warnings (new ones prefixed by >>):
In file included from include/linux/swap.h:9,
from mm/swap_state.c:13:
include/linux/memcontrol.h:1149:1: error: expected identifier or '(' before '{' token
1149 | {
| ^
>> include/linux/memcontrol.h:1147:19: warning: 'mem_cgroup_charge_swapin_page' used but never defined 1147 | static inline int mem_cgroup_charge_swapin_page(struct page *page,
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
vim +/mem_cgroup_charge_swapin_page +1147 include/linux/memcontrol.h
1146
> 1147 static inline int mem_cgroup_charge_swapin_page(struct page *page,
1148 struct mm_struct *mm, gfp_t gfp, swp_entry_t entry);
> 1149 { 1150 return 0;
1151 }
1152
---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/[email protected]
On Mon, Feb 22, 2021 at 9:55 PM Shakeel Butt <[email protected]> wrote:
[snip]
Quoted 13 lines
include/linux/memcontrol.h +3
-0@@ -596,6 +596,9 @@ static inline bool mem_cgroup_below_min(struct mem_cgroup *memcg) }int mem_cgroup_charge (struct page *page,struct mm_struct *mm,gfp_t gfp_mask); +int mem_cgroup_charge_swapin_page (struct page *page,struct mm_struct *mm, +gfp_t gfp,swp_entry_t entry); +void mem_cgroup_finish_swapin_page (struct page *page,swp_entry_t entry);void mem_cgroup_uncharge (struct page *page);void mem_cgroup_uncharge_list (struct list_head *page_list);
include/linux/memcontrol.h static inline int mem_cgroup_charge(struct page *page, struct mm_struct *mm, · line 1148
Quoted 5 lines
include/linux/memcontrol.h +2
-0@@ -1141,6 +1144,17 @@ static inline int mem_cgroup_charge(struct page *page, struct mm_struct *mm,return 0 ; } +static inline int mem_cgroup_charge_swapin_page (struct page *page, +struct mm_struct *mm,gfp_t gfp,swp_entry_t entry);
I didn't build-test the !CONFIG_MEMCG config and missed this semicolon. Andrew, let me know if you want me to send a new version.
Quoted 4 lines
include/linux/memcontrol.h +4
-0+{ +return 0 ; +} +