Skip to contentSkip to conversation

mm/shmem: support deterministic charging of tmpfs

PATCH Superseded Mina Almasry 2 participants 7 messages Nov 10, 2021 – Nov 11, 2021 1 message not in the archive

Subsystems (8)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

A message that is not in the archive (sent off-list or not yet received): [email protected]

#1 lore raw

PATCH v2 1/4 mm/shmem: support deterministic charging of tmpfs

Add memcg= option to shmem mount.

Users can specify this option at mount time and all data page charges will be charged to the memcg supplied. Processes are only allowed to direct tmpfs changes to a cgroup that they themselves can enter and allocate memory in.

Cc Michal Hocko <mhocko-IBi9RG/[email protected]>
Cc Theodore Ts'o <[email protected]>
Cc Greg Thelen <[email protected]>
Cc Shakeel Butt <[email protected]>
Cc Andrew Morton <akpm-de/[email protected]>
Cc Hugh Dickins <[email protected]>
CC Roman Gushchin <[email protected]>
Cc Dave Chinner <[email protected]>
Cc Johannes Weiner <[email protected]>
Cc Hugh Dickins <[email protected]>
Cc Tejun Heo <[email protected]>
Cc Vladimir Davydov <[email protected]>
Cc Muchun Song <[email protected]>

---

Changes in v2:
- Fixed Roman's email.
- Added a new wrapper around charge_memcg() instead of __mem_cgroup_charge()
- Merged the permission check into this patch as Roman suggested.
- Instead of checking for a s_memcg_to_charge off the superblock in the filemap code, I set_active_memcg() before calling into the fs generic code as Dave suggests.
- I have kept the s_memcg_to_charge in the superblock to keep the struct address_space pointer small and preserve the remount use case..

---
 fs/super.c                 |   3 +
 include/linux/fs.h         |   5 ++
 include/linux/memcontrol.h |  47 ++++++++++++++
 mm/memcontrol.c            | 130 +++++++++++++++++++++++++++++++++++++
 mm/shmem.c                 |  71 +++++++++++++++++++-
 5 files changed, 254 insertions(+), 2 deletions(-)
fs/super.c +3 -0
index 3bfc0f8fbd5bc..8aafe5e4e6200 100644
@@ -24,6 +24,7 @@
 #include <linux/export.h>
 #include <linux/slab.h>
 #include <linux/blkdev.h>
+#include <linux/memcontrol.h>
 #include <linux/mount.h>
 #include <linux/security.h>
 #include <linux/writeback.h>		/* for the emergency remount stuff */
@@ -180,6 +181,7 @@ static void destroy_unused_super(struct super_block *s)
 	up_write(&s->s_umount);
 	list_lru_destroy(&s->s_dentry_lru);
 	list_lru_destroy(&s->s_inode_lru);
+	mem_cgroup_set_charge_target(&s->s_memcg_to_charge, NULL);
 	security_sb_free(s);
 	put_user_ns(s->s_user_ns);
 	kfree(s->s_subtype);
@@ -292,6 +294,7 @@ static void __put_super(struct super_block *s)
 		WARN_ON(s->s_dentry_lru.node);
 		WARN_ON(s->s_inode_lru.node);
 		WARN_ON(!list_empty(&s->s_mounts));
+		mem_cgroup_set_charge_target(&s->s_memcg_to_charge, NULL);
 		security_sb_free(s);
 		fscrypt_sb_free(s);
 		put_user_ns(s->s_user_ns);
include/linux/fs.h +5 -0
index 3afca821df32e..59407b3e7aee3 100644
@@ -1567,6 +1567,11 @@ struct super_block {
 	struct workqueue_struct *s_dio_done_wq;
 	struct hlist_head s_pins;
 
+#ifdef CONFIG_MEMCG
+	/* memcg to charge for pages allocated to this filesystem */
+	struct mem_cgroup *s_memcg_to_charge;
+#endif
+
 	/*
 	 * Owning user namespace and default context in which to
 	 * interpret filesystem uids, gids, quotas, device nodes,
include/linux/memcontrol.h +47 -0

71 lines not on this page · raw message

mm/memcontrol.c +130 -0

148 lines not on this page · raw message

mm/shmem.c +69 -2

155 lines not on this page · raw message

#2 lore raw
Thank you for the patch! Yet something to improve:

Hi Mina,

Thank you for the patch! Yet something to improve:

[auto build test ERROR on hnaz-mm/master]

url:    https://github.com/0day-ci/linux/commits/Mina-Almasry/mm-shmem-support-deterministic-charging-of-tmpfs/20211111-062122
base:   https://github.com/hnaz/linux-mm master
config: um-i386_defconfig (attached as .config)
compiler: gcc-9 (Debian 9.3.0-22) 9.3.0

reproduce (this is a W=1 build):
# https://github.com/0day-ci/linux/commit/aaf0e3e0832977b6eb257e1ed27747cf97c479d4
git remote add linux-review https://github.com/0day-ci/linux
git fetch --no-tags linux-review Mina-Almasry/mm-shmem-support-deterministic-charging-of-tmpfs/20211111-062122 git checkout aaf0e3e0832977b6eb257e1ed27747cf97c479d4
# save the attached .config to linux build tree
mkdir build_dir
make W=1 O=build_dir ARCH=um SUBARCH=i386 SHELL=/bin/bash

If you fix the issue, kindly add following tag as appropriate

All errors (new ones prefixed by >>):

mm/shmem.c:118:5: warning: "CONFIG_MEMCG" is not defined, evaluates to 0 [-Wundef]

     118 | #if CONFIG_MEMCG
         |     ^~~~~~~~~~~~
   mm/shmem.c: In function 'shmem_add_to_page_cache':
>> mm/shmem.c:721:25: error: dereferencing pointer to incomplete type 'struct mem_cgroup'
     721 |    css_put(&remote_memcg->css);
         |                         ^~
   mm/shmem.c: In function 'shmem_parse_one':
   mm/shmem.c:3410:21: warning: unused variable 'memcg' [-Wunused-variable]
    3410 |  struct mem_cgroup *memcg;
         |                     ^~~~~
   mm/shmem.c: In function 'shmem_reconfigure':
   mm/shmem.c:3616:5: warning: "CONFIG_MEMCG" is not defined, evaluates to 0 [-Wundef]
    3616 | #if CONFIG_MEMCG
         |     ^~~~~~~~~~~~
   mm/shmem.c: In function 'shmem_fill_super':
   mm/shmem.c:3772:5: warning: "CONFIG_MEMCG" is not defined, evaluates to 0 [-Wundef]
    3772 | #if CONFIG_MEMCG
         |     ^~~~~~~~~~~~
--
   fs/super.c: In function 'destroy_unused_super':
>> fs/super.c:184:33: error: 'struct super_block' has no member named 's_memcg_to_charge'
     184 |  mem_cgroup_set_charge_target(&s->s_memcg_to_charge, NULL);
         |                                 ^~
   fs/super.c: In function '__put_super':
   fs/super.c:297:34: error: 'struct super_block' has no member named 's_memcg_to_charge'
     297 |   mem_cgroup_set_charge_target(&s->s_memcg_to_charge, NULL);
         |                                  ^~

vim +721 mm/shmem.c

691
692 /*
693 * Like add_to_page_cache_locked, but error if expected item has gone.
694 */
695 static int shmem_add_to_page_cache(struct page *page,

   696					   struct address_space *mapping,
   697					   pgoff_t index, void *expected, gfp_t gfp,
   698					   struct mm_struct *charge_mm)
   699	{
   700		XA_STATE_ORDER(xas, &mapping->i_pages, index, compound_order(page));
   701		unsigned long i = 0;
   702		unsigned long nr = compound_nr(page);
   703		int error;
   704		struct mem_cgroup *remote_memcg;
   705	
   706		VM_BUG_ON_PAGE(PageTail(page), page);
   707		VM_BUG_ON_PAGE(index != round_down(index, nr), page);
   708		VM_BUG_ON_PAGE(!PageLocked(page), page);
   709		VM_BUG_ON_PAGE(!PageSwapBacked(page), page);
   710		VM_BUG_ON(expected && PageTransHuge(page));
   711	
   712		page_ref_add(page, nr);
   713		page->mapping = mapping;
   714		page->index = index;
   715	
   716		if (!PageSwapCache(page)) {
   717			remote_memcg = mem_cgroup_mapping_get_charge_target(mapping);
   718			if (remote_memcg) {
   719				error = mem_cgroup_charge_memcg(page_folio(page),
   720								remote_memcg, gfp);
 > 721				css_put(&remote_memcg->css);

722 } else
723 error = mem_cgroup_charge(page_folio(page), charge_mm,
724 gfp);
725 if (error) {
726 if (PageTransHuge(page)) {
727 count_vm_event(THP_FILE_FALLBACK);
728 count_vm_event(THP_FILE_FALLBACK_CHARGE);
729 }
730 goto error;
731 }
732 }
733 cgroup_throttle_swaprate(page, gfp);
734
735 do {
736 void *entry;
737 xas_lock_irq(&xas);
738 entry = xas_find_conflict(&xas);
739 if (entry != expected)
740 xas_set_err(&xas, -EEXIST);
741 xas_create_range(&xas);
742 if (xas_error(&xas))
743 goto unlock;
744 next:
745 xas_store(&xas, page);
746 if (++i < nr) {
747 xas_next(&xas);
748 goto next;
749 }
750 if (PageTransHuge(page)) {
751 count_vm_event(THP_FILE_ALLOC);
752 __mod_lruvec_page_state(page, NR_SHMEM_THPS, nr);
753 }
754 mapping->nrpages += nr;
755 __mod_lruvec_page_state(page, NR_FILE_PAGES, nr);
756 __mod_lruvec_page_state(page, NR_SHMEM, nr);
757 unlock:
758 xas_unlock_irq(&xas);
759 } while (xas_nomem(&xas, gfp));
760
761 if (xas_error(&xas)) {
762 error = xas_error(&xas);
763 goto error;
764 }
765
766 return 0;
767 error:
768 page->mapping = NULL;
769 page_ref_sub(page, nr);
770 return error;
771 }
772

---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/kbuild-all(a)lists.01.org

#3 lore raw
In reply to Mina Almasry
Thank you for the patch! Yet something to improve:

Hi Mina,

Thank you for the patch! Yet something to improve:

[auto build test ERROR on hnaz-mm/master]

url:    https://github.com/0day-ci/linux/commits/Mina-Almasry/mm-shmem-support-deterministic-charging-of-tmpfs/20211111-062122
base:   https://github.com/hnaz/linux-mm master
config: ia64-defconfig (attached as .config)
compiler: ia64-linux-gcc (GCC) 11.2.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/aaf0e3e0832977b6eb257e1ed27747cf97c479d4
git remote add linux-review https://github.com/0day-ci/linux
git fetch --no-tags linux-review Mina-Almasry/mm-shmem-support-deterministic-charging-of-tmpfs/20211111-062122 git checkout aaf0e3e0832977b6eb257e1ed27747cf97c479d4
# save the attached .config to linux build tree
mkdir build_dir
COMPILER_INSTALL_PATH=$HOME/0day COMPILER=gcc-11.2.0 make.cross O=build_dir ARCH=ia64 SHELL=/bin/bash

If you fix the issue, kindly add following tag as appropriate

All errors (new ones prefixed by >>):

mm/shmem.c:118:5: warning: "CONFIG_MEMCG" is not defined, evaluates to 0 [-Wundef]

     118 | #if CONFIG_MEMCG
         |     ^~~~~~~~~~~~
   mm/shmem.c: In function 'shmem_add_to_page_cache':
>> mm/shmem.c:721:46: error: invalid use of undefined type 'struct mem_cgroup'
     721 |                         css_put(&remote_memcg->css);
         |                                              ^~
   mm/shmem.c: In function 'shmem_getpage_gfp':
   mm/shmem.c:1863:38: error: invalid use of undefined type 'struct mem_cgroup'
    1863 |                 css_put(&remote_memcg->css);
         |                                      ^~
   mm/shmem.c: In function 'shmem_parse_one':
   mm/shmem.c:3410:28: warning: unused variable 'memcg' [-Wunused-variable]
    3410 |         struct mem_cgroup *memcg;
         |                            ^~~~~
   mm/shmem.c: In function 'shmem_reconfigure':
   mm/shmem.c:3616:5: warning: "CONFIG_MEMCG" is not defined, evaluates to 0 [-Wundef]
    3616 | #if CONFIG_MEMCG
         |     ^~~~~~~~~~~~
   mm/shmem.c: In function 'shmem_fill_super':
   mm/shmem.c:3772:5: warning: "CONFIG_MEMCG" is not defined, evaluates to 0 [-Wundef]
    3772 | #if CONFIG_MEMCG
         |     ^~~~~~~~~~~~

vim +721 mm/shmem.c

691
692 /*
693 * Like add_to_page_cache_locked, but error if expected item has gone.
694 */
695 static int shmem_add_to_page_cache(struct page *page,

   696					   struct address_space *mapping,
   697					   pgoff_t index, void *expected, gfp_t gfp,
   698					   struct mm_struct *charge_mm)
   699	{
   700		XA_STATE_ORDER(xas, &mapping->i_pages, index, compound_order(page));
   701		unsigned long i = 0;
   702		unsigned long nr = compound_nr(page);
   703		int error;
   704		struct mem_cgroup *remote_memcg;
   705	
   706		VM_BUG_ON_PAGE(PageTail(page), page);
   707		VM_BUG_ON_PAGE(index != round_down(index, nr), page);
   708		VM_BUG_ON_PAGE(!PageLocked(page), page);
   709		VM_BUG_ON_PAGE(!PageSwapBacked(page), page);
   710		VM_BUG_ON(expected && PageTransHuge(page));
   711	
   712		page_ref_add(page, nr);
   713		page->mapping = mapping;
   714		page->index = index;
   715	
   716		if (!PageSwapCache(page)) {
   717			remote_memcg = mem_cgroup_mapping_get_charge_target(mapping);
   718			if (remote_memcg) {
   719				error = mem_cgroup_charge_memcg(page_folio(page),
   720								remote_memcg, gfp);
 > 721				css_put(&remote_memcg->css);

722 } else
723 error = mem_cgroup_charge(page_folio(page), charge_mm,
724 gfp);
725 if (error) {
726 if (PageTransHuge(page)) {
727 count_vm_event(THP_FILE_FALLBACK);
728 count_vm_event(THP_FILE_FALLBACK_CHARGE);
729 }
730 goto error;
731 }
732 }
733 cgroup_throttle_swaprate(page, gfp);
734
735 do {
736 void *entry;
737 xas_lock_irq(&xas);
738 entry = xas_find_conflict(&xas);
739 if (entry != expected)
740 xas_set_err(&xas, -EEXIST);
741 xas_create_range(&xas);
742 if (xas_error(&xas))
743 goto unlock;
744 next:
745 xas_store(&xas, page);
746 if (++i < nr) {
747 xas_next(&xas);
748 goto next;
749 }
750 if (PageTransHuge(page)) {
751 count_vm_event(THP_FILE_ALLOC);
752 __mod_lruvec_page_state(page, NR_SHMEM_THPS, nr);
753 }
754 mapping->nrpages += nr;
755 __mod_lruvec_page_state(page, NR_FILE_PAGES, nr);
756 __mod_lruvec_page_state(page, NR_SHMEM, nr);
757 unlock:
758 xas_unlock_irq(&xas);
759 } while (xas_nomem(&xas, gfp));
760
761 if (xas_error(&xas)) {
762 error = xas_error(&xas);
763 goto error;
764 }
765
766 return 0;
767 error:
768 page->mapping = NULL;
769 page_ref_sub(page, nr);
770 return error;
771 }
772

---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/kbuild-all(a)lists.01.org

#4 lore raw

PATCH v2 2/4 mm/oom: handle remote ooms

In reply to a message not in the archive

On remote ooms (OOMs due to remote charging), the oom-killer will attempt to find a task to kill in the memcg under oom, if the oom-killer is unable to find one, the oom-killer should simply return ENOMEM to the allocating process.

If we're in pagefault path and we're unable to return ENOMEM to the allocating process, we instead kill the allocating process.

Cc Michal Hocko <mhocko-IBi9RG/[email protected]>
Cc Theodore Ts'o <[email protected]>
Cc Greg Thelen <[email protected]>
Cc Shakeel Butt <[email protected]>
Cc Andrew Morton <akpm-de/[email protected]>
Cc Hugh Dickins <[email protected]>
CC Roman Gushchin <[email protected]>
Cc Dave Chinner <[email protected]>
Cc Johannes Weiner <[email protected]>
Cc Hugh Dickins <[email protected]>
Cc Tejun Heo <[email protected]>
Cc Vladimir Davydov <[email protected]>
Cc Muchun Song <[email protected]>

---

Changes in v2:
- Moved the remote oom handling as Roman requested.
- Used mem_cgroup_from_task(current) instead of grabbing the memcg from current->mm

---
 include/linux/memcontrol.h |  6 ++++++
 mm/memcontrol.c            | 29 +++++++++++++++++++++++++++++
 mm/oom_kill.c              | 22 ++++++++++++++++++++++
 3 files changed, 57 insertions(+)
include/linux/memcontrol.h +6 -0
index 866904afd3563..ae4686abd4d32 100644
@@ -937,6 +937,7 @@ struct mem_cgroup *mem_cgroup_get_from_path(const char *path);
  * it.
  */
 int mem_cgroup_get_name_from_sb(struct super_block *sb, char *buf, size_t len);
+bool is_remote_oom(struct mem_cgroup *memcg_under_oom);
 
 void mem_cgroup_update_lru_size(struct lruvec *lruvec, enum lru_list lru,
 		int zid, int nr_pages);
@@ -1270,6 +1271,11 @@ static inline int mem_cgroup_get_name_from_sb(struct super_block *sb, char *buf,
 	return 0;
 }
 
+static inline bool is_remote_oom(struct mem_cgroup *memcg_under_oom)
+{
+	return false;
+}
+
 static inline int mem_cgroup_swapin_charge_page(struct page *page,
 			struct mm_struct *mm, gfp_t gfp, swp_entry_t entry)
 {
mm/memcontrol.c +29 -0
index b3d8f52a63d17..8019c396bfdd9 100644
@@ -2664,6 +2664,35 @@ int mem_cgroup_get_name_from_sb(struct super_block *sb, char *buf, size_t len)
 	return ret < 0 ? ret : 0;
 }
 
+/*
+ * Returns true if current's mm is a descendant of the memcg_under_oom (or
+ * equal to it). False otherwise. This is used by the oom-killer to detect
+ * ooms due to remote charging.
+ */
+bool is_remote_oom(struct mem_cgroup *memcg_under_oom)
+{
+	struct mem_cgroup *current_memcg;
+	bool is_remote_oom;
+
+	if (!memcg_under_oom)
+		return false;
+
+	rcu_read_lock();
+	current_memcg = mem_cgroup_from_task(current);
+	if (current_memcg && !css_tryget_online(&current_memcg->css))
+		current_memcg = NULL;
+	rcu_read_unlock();
+
+	if (!current_memcg)
+		return false;
+
+	is_remote_oom =
+		!mem_cgroup_is_descendant(current_memcg, memcg_under_oom);
+	css_put(&current_memcg->css);
+
+	return is_remote_oom;
+}
+
 /*
  * Set or clear (if @memcg is NULL) charge association from file system to
  * memcg.  If @memcg != NULL, then a css reference must be held by the caller to
mm/oom_kill.c +22 -0
index 0a7e16b16b8c3..0e0097a0aed45 100644
@@ -1108,6 +1108,28 @@ bool out_of_memory(struct oom_control *oc)
 	select_bad_process(oc);
 	/* Found nothing?!?! */
 	if (!oc->chosen) {
+		if (is_remote_oom(oc->memcg)) {
+			/*
+			 * For remote ooms in userfaults, we have no choice but
+			 * to kill the allocating process.
+			 */
+			if (current->in_user_fault &&
+			    !oom_unkillable_task(current)) {
+				get_task_struct(current);
+				oc->chosen = current;
+				oom_kill_process(
+					oc,
+					"Out of memory (Killing remote allocating task)");
+				return true;
+			}
+
+			/*
+			 * For remote ooms in non-userfaults, simply return
+			 * ENOMEM to the caller.
+			 */
+			return false;
+		}
+
 		dump_header(oc, NULL);
 		pr_warn("Out of memory and no killable processes...\n");
 		/*
#5 lore raw

Re: [PATCH v2 2/4] mm/oom: handle remote ooms

Thank you for the patch! Yet something to improve:

Hi Mina,

Thank you for the patch! Yet something to improve:

[auto build test ERROR on hnaz-mm/master]

url:    https://github.com/0day-ci/linux/commits/Mina-Almasry/mm-shmem-support-deterministic-charging-of-tmpfs/20211111-062122
base:   https://github.com/hnaz/linux-mm master
config: um-i386_defconfig (attached as .config)
compiler: gcc-9 (Debian 9.3.0-22) 9.3.0

reproduce (this is a W=1 build):
# https://github.com/0day-ci/linux/commit/452a4d110272eb39892e4be30526411c7a5cb2e3
git remote add linux-review https://github.com/0day-ci/linux
git fetch --no-tags linux-review Mina-Almasry/mm-shmem-support-deterministic-charging-of-tmpfs/20211111-062122 git checkout 452a4d110272eb39892e4be30526411c7a5cb2e3
# save the attached .config to linux build tree
mkdir build_dir
make W=1 O=build_dir ARCH=um SUBARCH=i386 SHELL=/bin/bash

If you fix the issue, kindly add following tag as appropriate

All errors (new ones prefixed by >>):

mm/oom_kill.c: In function 'out_of_memory':

>> mm/oom_kill.c:1116:15: error: 'struct task_struct' has no member named 'in_user_fault'
    1116 |    if (current->in_user_fault &&
         |               ^~

vim +1116 mm/oom_kill.c

1044
1045 /**
1046 * out_of_memory - kill the "best" process when we run out of memory
1047 * @oc: pointer to struct oom_control
1048 *
1049 * If we run out of memory, we have the choice between either
1050 * killing a random task (bad), letting the system crash (worse)
1051 * OR try to be smart about which process to kill. Note that we
1052 * don't have to be perfect here, we just have to be good.
1053 */
1054 bool out_of_memory(struct oom_control *oc)
1055 {
1056 unsigned long freed = 0;
1057
1058 if (oom_killer_disabled)
1059 return false;
1060
1061 if (!is_memcg_oom(oc)) {
1062 blocking_notifier_call_chain(&oom_notify_list, 0, &freed);
1063 if (freed > 0)
1064 /* Got some memory back in the last second. */
1065 return true;
1066 }
1067
1068 /*
1069 * If current has a pending SIGKILL or is exiting, then automatically
1070 * select it. The goal is to allow it to allocate so that it may
1071 * quickly exit and free its memory.
1072 */
1073 if (task_will_free_mem(current)) {
1074 mark_oom_victim(current);
1075 wake_oom_reaper(current);
1076 return true;
1077 }
1078
1079 /*
1080 * The OOM killer does not compensate for IO-less reclaim.
1081 * pagefault_out_of_memory lost its gfp context so we have to
1082 * make sure exclude 0 mask - all other users should have at least
1083 * ___GFP_DIRECT_RECLAIM to get here. But mem_cgroup_oom() has to
1084 * invoke the OOM killer even if it is a GFP_NOFS allocation.
1085 */
1086 if (oc->gfp_mask && !(oc->gfp_mask & __GFP_FS) && !is_memcg_oom(oc))
1087 return true;
1088
1089 /*
1090 * Check if there were limitations on the allocation (only relevant for
1091 * NUMA and memcg) that may require different handling.
1092 */
1093 oc->constraint = constrained_alloc(oc);
1094 if (oc->constraint != CONSTRAINT_MEMORY_POLICY)
1095 oc->nodemask = NULL;
1096 check_panic_on_oom(oc);
1097
1098 if (!is_memcg_oom(oc) && sysctl_oom_kill_allocating_task &&

  1099		    current->mm && !oom_unkillable_task(current) &&
  1100		    oom_cpuset_eligible(current, oc) &&
  1101		    current->signal->oom_score_adj != OOM_SCORE_ADJ_MIN) {
  1102			get_task_struct(current);
  1103			oc->chosen = current;
  1104			oom_kill_process(oc, "Out of memory (oom_kill_allocating_task)");
  1105			return true;
  1106		}
  1107	
  1108		select_bad_process(oc);
  1109		/* Found nothing?!?! */
  1110		if (!oc->chosen) {
  1111			if (is_remote_oom(oc->memcg)) {
  1112				/*
  1113				 * For remote ooms in userfaults, we have no choice but
  1114				 * to kill the allocating process.
  1115				 */
> 1116				if (current->in_user_fault &&

1117 !oom_unkillable_task(current)) {
1118 get_task_struct(current);
1119 oc->chosen = current;
1120 oom_kill_process(
1121 oc,
1122 "Out of memory (Killing remote allocating task)");
1123 return true;
1124 }
1125
1126 /*
1127 * For remote ooms in non-userfaults, simply return
1128 * ENOMEM to the caller.
1129 */
1130 return false;
1131 }
1132
1133 dump_header(oc, NULL);
1134 pr_warn("Out of memory and no killable processes...\n");
1135 /*
1136 * If we got here due to an actual allocation at the
1137 * system level, we cannot survive this and will enter
1138 * an endless loop in the allocator. Bail out now.
1139 */
1140 if (!is_sysrq_oom(oc) && !is_memcg_oom(oc))
1141 panic("System is deadlocked on memory\n");
1142 }
1143 if (oc->chosen && oc->chosen != (void *)-1UL)
1144 oom_kill_process(oc, !is_memcg_oom(oc) ? "Out of memory" :
1145 "Memory cgroup out of memory");
1146 return !!oc->chosen;
1147 }
1148

---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/kbuild-all(a)lists.01.org

#6 lore raw

PATCH v2 3/4 mm, shmem: add tmpfs memcg= option documentation

In reply to a message not in the archive
Cc Michal Hocko <mhocko-IBi9RG/[email protected]>
Cc Theodore Ts'o <[email protected]>
Cc Greg Thelen <[email protected]>
Cc Shakeel Butt <[email protected]>
Cc Andrew Morton <akpm-de/[email protected]>
Cc Hugh Dickins <[email protected]>
Cc Roman Gushchin <[email protected]>
Cc Dave Chinner <[email protected]>
Cc Johannes Weiner <[email protected]>
Cc Hugh Dickins <[email protected]>
Cc Tejun Heo <[email protected]>
Cc Vladimir Davydov <[email protected]>
Cc Muchun Song <[email protected]>
---
 Documentation/filesystems/tmpfs.rst | 17 +++++++++++++++++
 1 file changed, 17 insertions(+)
Documentation/filesystems/tmpfs.rst +17 -0
index 0408c245785e3..1ab04e8fa9222 100644
@@ -137,6 +137,23 @@ mount options.  It can be added later, when the tmpfs is already mounted
 on MountPoint, by 'mount -o remount,mpol=Policy:NodeList MountPoint'.
 
 
+If CONFIG_MEMCG is enabled, tmpfs has a mount option to specify the memory
+cgroup to be charged for page allocations.
+
+memcg=/sys/fs/cgroup/unified/test/: data page allocations are charged to
+cgroup /sys/fs/cgroup/unified/test/.
+
+When charging memory to the remote memcg (memcg specified with memcg=) and
+hitting the limit, the oom-killer will be invoked and will attempt to kill
+a process in the remote memcg. If no such processes are found, the remote
+charging process gets an ENOMEM. If the remote charging process is in the
+pagefault path, it gets killed.
+
+Only processes that have access to /sys/fs/cgroup/unified/test/cgroup.procs can
+mount a tmpfs with memcg=/sys/fs/cgroup/unified/test. Thus, a process is able
+to charge memory to a cgroup only if it itself is able to enter that cgroup.
+
+
 To specify the initial root directory you can use the following mount
 options:
#7 lore raw

PATCH v2 4/4 mm, shmem, selftests: add tmpfs memcg= mount option tests

In reply to a message not in the archive
Cc Michal Hocko <mhocko-IBi9RG/[email protected]>
Cc Theodore Ts'o <[email protected]>
Cc Greg Thelen <[email protected]>
Cc Shakeel Butt <[email protected]>
Cc Andrew Morton <akpm-de/[email protected]>
Cc Hugh Dickins <[email protected]>
Cc Roman Gushchin <[email protected]>
Cc Dave Chinner <[email protected]>
Cc Johannes Weiner <[email protected]>
Cc Hugh Dickins <[email protected]>
Cc Tejun Heo <[email protected]>
Cc Vladimir Davydov <[email protected]>
Cc Muchun Song <[email protected]>
---
 tools/testing/selftests/vm/.gitignore     |   1 +
 tools/testing/selftests/vm/mmap_write.c   | 103 ++++++++++++++++++++++
 tools/testing/selftests/vm/tmpfs-memcg.sh |  87 ++++++++++++++++++
 3 files changed, 191 insertions(+)
 create mode 100644 tools/testing/selftests/vm/mmap_write.c
 create mode 100755 tools/testing/selftests/vm/tmpfs-memcg.sh
tools/testing/selftests/vm/.gitignore +1 -0
index 2e7e86e852828..cb229974c5f15 100644
@@ -19,6 +19,7 @@ madv_populate
 userfaultfd
 mlock-intersect-test
 mlock-random-test
+mmap_write
 virtual_address_range
 gup_test
 va_128TBswitch
tools/testing/selftests/vm/mmap_write.c new file +103 -0

103 lines not on this page · raw message

tools/testing/selftests/vm/tmpfs-memcg.sh new file +87 -0

87 lines not on this page · raw message