Skip to contentSkip to conversation

memcg-v1: fully deprecate charge moving

PATCH In review Shakeel Butt 7 participants 36 messages Oct 25, 2024 – Nov 4, 2024

Subsystems (9)
lore
Share & tools
lore

Shakeel Butt

author maintainer
#0 lore raw

cover memcg-v1: fully deprecate charge moving

The memcg v1's charge moving feature has been deprecated for almost 2 years and the kernel warns if someone try to use it. This warning has been backported to all stable kernel and there have not been any report of the warning or the request to support this feature anymore. Let's proceed to fully deprecate this feature.

Changes since RFC:
- Writing 0 to memory.move_charge_at_immigrate is allowed.
- Remove the memcg move locking in separate patches.

Shakeel Butt (6):
memcg-v1: fully deprecate move_charge_at_immigrate
memcg-v1: remove charge move code
memcg-v1: no need for memcg locking for dirty tracking
memcg-v1: no need for memcg locking for writeback tracking
memcg-v1: no need for memcg locking for MGLRU
memcg-v1: remove memcg move locking code

 .../admin-guide/cgroup-v1/memory.rst          |  82 +-
 fs/buffer.c                                   |   5 -
 include/linux/memcontrol.h                    |  59 --
 mm/filemap.c                                  |   1 -
 mm/memcontrol-v1.c                            | 958 +-----------------
 mm/memcontrol-v1.h                            |   6 -
 mm/memcontrol.c                               |  14 -
 mm/page-writeback.c                           |  20 +-
 mm/rmap.c                                     |   1 -
 mm/vmscan.c                                   |  11 -
 10 files changed, 8 insertions(+), 1149 deletions(-)
Signature · 1 line
2.43.5
author maintainer
#1 lore raw

PATCH 1/6 memcg-v1: fully deprecate move_charge_at_immigrate

Proceed with the complete deprecation of memcg v1's charge moving feature. The deprecation warning has been in the kernel for almost two years and has been ported to all stable kernel since. Now is the time to fully deprecate this feature.

---

Changes since RFC:
- Writing 0 to memory.move_charge_at_immigrate is allowed.

 .../admin-guide/cgroup-v1/memory.rst          | 82 +------------------
 mm/memcontrol-v1.c                            | 14 +---
 2 files changed, 5 insertions(+), 91 deletions(-)
Documentation/admin-guide/cgroup-v1/memory.rst +3 -79
index 270501db9f4e..286d16fc22eb 100644
@@ -90,9 +90,7 @@ Brief summary of control files.
                                      used.
  memory.swappiness		     set/show swappiness parameter of vmscan
 				     (See sysctl's vm.swappiness)
- memory.move_charge_at_immigrate     set/show controls of moving charges
-                                     This knob is deprecated and shouldn't be
-                                     used.
+ memory.move_charge_at_immigrate     This knob is deprecated.
  memory.oom_control		     set/show oom controls.
                                      This knob is deprecated and shouldn't be
                                      used.
@@ -243,10 +241,6 @@ behind this approach is that a cgroup that aggressively uses a shared
 page will eventually get charged for it (once it is uncharged from
 the cgroup that brought it in -- this will happen on memory pressure).
 
-But see :ref:`section 8.2 <cgroup-v1-memory-movable-charges>` when moving a
-task to another cgroup, its pages may be recharged to the new cgroup, if
-move_charge_at_immigrate has been chosen.
-
 2.4 Swap Extension
 --------------------------------------
 
@@ -756,78 +750,8 @@ If we want to change this to 1G, we can at any time use::
 
 THIS IS DEPRECATED!
 
-It's expensive and unreliable! It's better practice to launch workload
-tasks directly from inside their target cgroup. Use dedicated workload
-cgroups to allow fine-grained policy adjustments without having to
-move physical pages between control domains.
-
-Users can move charges associated with a task along with task migration, that
-is, uncharge task's pages from the old cgroup and charge them to the new cgroup.
-This feature is not supported in !CONFIG_MMU environments because of lack of
-page tables.
-
-8.1 Interface
--------------
-
-This feature is disabled by default. It can be enabled (and disabled again) by
-writing to memory.move_charge_at_immigrate of the destination cgroup.
-
-If you want to enable it::
-
-	# echo (some positive value) > memory.move_charge_at_immigrate
-
-.. note::
-      Each bits of move_charge_at_immigrate has its own meaning about what type
-      of charges should be moved. See :ref:`section 8.2
-      <cgroup-v1-memory-movable-charges>` for details.
-
-.. note::
-      Charges are moved only when you move mm->owner, in other words,
-      a leader of a thread group.
-
-.. note::
-      If we cannot find enough space for the task in the destination cgroup, we
-      try to make space by reclaiming memory. Task migration may fail if we
-      cannot make enough space.
-
-.. note::
-      It can take several seconds if you move charges much.
-
-And if you want disable it again::
-
-	# echo 0 > memory.move_charge_at_immigrate
-
-.. _cgroup-v1-memory-movable-charges:
-
-8.2 Type of charges which can be moved
---------------------------------------
-
-Each bit in move_charge_at_immigrate has its own meaning about what type of
-charges should be moved. But in any case, it must be noted that an account of
-a page or a swap can be moved only when it is charged to the task's current
-(old) memory cgroup.
-
-+---+--------------------------------------------------------------------------+
-|bit| what type of charges would be moved ?                                    |
-+===+==========================================================================+
-| 0 | A charge of an anonymous page (or swap of it) used by the target task.   |
-|   | You must enable Swap Extension (see 2.4) to enable move of swap charges. |
-+---+--------------------------------------------------------------------------+
-| 1 | A charge of file pages (normal file, tmpfs file (e.g. ipc shared memory) |
-|   | and swaps of tmpfs file) mmapped by the target task. Unlike the case of  |
-|   | anonymous pages, file pages (and swaps) in the range mmapped by the task |
-|   | will be moved even if the task hasn't done page fault, i.e. they might   |
-|   | not be the task's "RSS", but other task's "RSS" that maps the same file. |
-|   | The mapcount of the page is ignored (the page can be moved independent   |
-|   | of the mapcount). You must enable Swap Extension (see 2.4) to            |
-|   | enable move of swap charges.                                             |
-+---+--------------------------------------------------------------------------+
-
-8.3 TODO
---------
-
-- All of moving charge operations are done under cgroup_mutex. It's not good
-  behavior to hold the mutex too long, so we may need some trick.
+Reading memory.move_charge_at_immigrate will always return 0 and writing
+to it will always return -EINVAL.
 
 9. Memory thresholds
 ====================
mm/memcontrol-v1.c +2 -12
index 81d8819f13cd..9b3b1a446c65 100644
@@ -593,29 +593,19 @@ static inline int mem_cgroup_move_swap_account(swp_entry_t entry,
 static u64 mem_cgroup_move_charge_read(struct cgroup_subsys_state *css,
 				struct cftype *cft)
 {
-	return mem_cgroup_from_css(css)->move_charge_at_immigrate;
+	return 0;
 }
 
 #ifdef CONFIG_MMU
 static int mem_cgroup_move_charge_write(struct cgroup_subsys_state *css,
 				 struct cftype *cft, u64 val)
 {
-	struct mem_cgroup *memcg = mem_cgroup_from_css(css);
-
 	pr_warn_once("Cgroup memory moving (move_charge_at_immigrate) is deprecated. "
 		     "Please report your usecase to [email protected] if you "
 		     "depend on this functionality.\n");
 
-	if (val & ~MOVE_MASK)
+	if (val != 0)
 		return -EINVAL;
-
-	/*
-	 * No kind of locking is needed in here, because ->can_attach() will
-	 * check this value once in the beginning of the process, and then carry
-	 * on with stale data. This means that changes to this value will only
-	 * affect task migrations starting after the change.
-	 */
-	memcg->move_charge_at_immigrate = val;
 	return 0;
 }
 #else
Signature · 1 line
2.43.5
Acked-by on v1 1/6
#2 lore raw

Re: [PATCH v1 1/6] memcg-v1: fully deprecate move_charge_at_immigrate

Quoted 7 lines · Shakeel Butt

Proceed with the complete deprecation of memcg v1's charge moving feature. The deprecation warning has been in the kernel for almost two years and has been ported to all stable kernel since. Now is the time to fully deprecate this feature.

Thanks!

Quoted 154 lines

---

Changes since RFC:
- Writing 0 to memory.move_charge_at_immigrate is allowed.

 .../admin-guide/cgroup-v1/memory.rst          | 82 +------------------
 mm/memcontrol-v1.c                            | 14 +---
 2 files changed, 5 insertions(+), 91 deletions(-)
Documentation/admin-guide/cgroup-v1/memory.rst +3 -79
index 270501db9f4e..286d16fc22eb 100644
@@ -90,9 +90,7 @@ Brief summary of control files.
                                      used.
  memory.swappiness		     set/show swappiness parameter of vmscan
 				     (See sysctl's vm.swappiness)
- memory.move_charge_at_immigrate     set/show controls of moving charges
-                                     This knob is deprecated and shouldn't be
-                                     used.
+ memory.move_charge_at_immigrate     This knob is deprecated.
  memory.oom_control		     set/show oom controls.
                                      This knob is deprecated and shouldn't be
                                      used.
@@ -243,10 +241,6 @@ behind this approach is that a cgroup that aggressively uses a shared
 page will eventually get charged for it (once it is uncharged from
 the cgroup that brought it in -- this will happen on memory pressure).
 
-But see :ref:`section 8.2 <cgroup-v1-memory-movable-charges>` when moving a
-task to another cgroup, its pages may be recharged to the new cgroup, if
-move_charge_at_immigrate has been chosen.
-
 2.4 Swap Extension
 --------------------------------------
 
@@ -756,78 +750,8 @@ If we want to change this to 1G, we can at any time use::
 
 THIS IS DEPRECATED!
 
-It's expensive and unreliable! It's better practice to launch workload
-tasks directly from inside their target cgroup. Use dedicated workload
-cgroups to allow fine-grained policy adjustments without having to
-move physical pages between control domains.
-
-Users can move charges associated with a task along with task migration, that
-is, uncharge task's pages from the old cgroup and charge them to the new cgroup.
-This feature is not supported in !CONFIG_MMU environments because of lack of
-page tables.
-
-8.1 Interface
--------------
-
-This feature is disabled by default. It can be enabled (and disabled again) by
-writing to memory.move_charge_at_immigrate of the destination cgroup.
-
-If you want to enable it::
-
-	# echo (some positive value) > memory.move_charge_at_immigrate
-
-.. note::
-      Each bits of move_charge_at_immigrate has its own meaning about what type
-      of charges should be moved. See :ref:`section 8.2
-      <cgroup-v1-memory-movable-charges>` for details.
-
-.. note::
-      Charges are moved only when you move mm->owner, in other words,
-      a leader of a thread group.
-
-.. note::
-      If we cannot find enough space for the task in the destination cgroup, we
-      try to make space by reclaiming memory. Task migration may fail if we
-      cannot make enough space.
-
-.. note::
-      It can take several seconds if you move charges much.
-
-And if you want disable it again::
-
-	# echo 0 > memory.move_charge_at_immigrate
-
-.. _cgroup-v1-memory-movable-charges:
-
-8.2 Type of charges which can be moved
---------------------------------------
-
-Each bit in move_charge_at_immigrate has its own meaning about what type of
-charges should be moved. But in any case, it must be noted that an account of
-a page or a swap can be moved only when it is charged to the task's current
-(old) memory cgroup.
-
-+---+--------------------------------------------------------------------------+
-|bit| what type of charges would be moved ?                                    |
-+===+==========================================================================+
-| 0 | A charge of an anonymous page (or swap of it) used by the target task.   |
-|   | You must enable Swap Extension (see 2.4) to enable move of swap charges. |
-+---+--------------------------------------------------------------------------+
-| 1 | A charge of file pages (normal file, tmpfs file (e.g. ipc shared memory) |
-|   | and swaps of tmpfs file) mmapped by the target task. Unlike the case of  |
-|   | anonymous pages, file pages (and swaps) in the range mmapped by the task |
-|   | will be moved even if the task hasn't done page fault, i.e. they might   |
-|   | not be the task's "RSS", but other task's "RSS" that maps the same file. |
-|   | The mapcount of the page is ignored (the page can be moved independent   |
-|   | of the mapcount). You must enable Swap Extension (see 2.4) to            |
-|   | enable move of swap charges.                                             |
-+---+--------------------------------------------------------------------------+
-
-8.3 TODO
---------
-
-- All of moving charge operations are done under cgroup_mutex. It's not good
-  behavior to hold the mutex too long, so we may need some trick.
+Reading memory.move_charge_at_immigrate will always return 0 and writing
+to it will always return -EINVAL.
 
 9. Memory thresholds
 ====================
mm/memcontrol-v1.c +2 -12
index 81d8819f13cd..9b3b1a446c65 100644
@@ -593,29 +593,19 @@ static inline int mem_cgroup_move_swap_account(swp_entry_t entry,
 static u64 mem_cgroup_move_charge_read(struct cgroup_subsys_state *css,
 				struct cftype *cft)
 {
-	return mem_cgroup_from_css(css)->move_charge_at_immigrate;
+	return 0;
 }
 
 #ifdef CONFIG_MMU
 static int mem_cgroup_move_charge_write(struct cgroup_subsys_state *css,
 				 struct cftype *cft, u64 val)
 {
-	struct mem_cgroup *memcg = mem_cgroup_from_css(css);
-
 	pr_warn_once("Cgroup memory moving (move_charge_at_immigrate) is deprecated. "
 		     "Please report your usecase to [email protected] if you "
 		     "depend on this functionality.\n");
 
-	if (val & ~MOVE_MASK)
+	if (val != 0)
 		return -EINVAL;
-
-	/*
-	 * No kind of locking is needed in here, because ->can_attach() will
-	 * check this value once in the beginning of the process, and then carry
-	 * on with stale data. This means that changes to this value will only
-	 * affect task migrations starting after the change.
-	 */
-	memcg->move_charge_at_immigrate = val;
 	return 0;
 }
 #else
Signature · 1 line
2.43.5
Signature · 2 lines
Michal Hocko
SUSE Labs
maintainer Acked-by on v1 1/6
#3 lore raw

Re: [PATCH v1 1/6] memcg-v1: fully deprecate move_charge_at_immigrate

In reply to Shakeel Butt
Quoted 7 lines · Shakeel Butt

Proceed with the complete deprecation of memcg v1's charge moving feature. The deprecation warning has been in the kernel for almost two years and has been ported to all stable kernel since. Now is the time to fully deprecate this feature.

author maintainer
#4 lore raw

PATCH 2/6 memcg-v1: remove charge move code

In reply to Shakeel Butt

The memcg-v1 charge move feature has been deprecated completely and let's remove the relevant code as well.

---
 include/linux/memcontrol.h |   5 -
 mm/memcontrol-v1.c         | 862 -------------------------------------
 mm/memcontrol-v1.h         |   6 -
 mm/memcontrol.c            |   9 -
 4 files changed, 882 deletions(-)
include/linux/memcontrol.h +0 -5
index 524006313b0d..798db70b0a30 100644
@@ -299,11 +299,6 @@ struct mem_cgroup {
 	/* For oom notifier event fd */
 	struct list_head oom_notify;
 
-	/*
-	 * Should we move charges of a task when a task is moved into this
-	 * mem_cgroup ? And what type of charges should we move ?
-	 */
-	unsigned long move_charge_at_immigrate;
 	/* taken only while moving_account > 0 */
 	spinlock_t move_lock;
 	unsigned long move_lock_flags;
mm/memcontrol-v1.c +0 -862

886 lines not on this page · raw message

mm/memcontrol-v1.h +0 -6
index c0672e25bcdb..0e3b82951d91 100644
@@ -80,12 +80,7 @@ static inline void memcg1_soft_limit_reset(struct mem_cgroup *memcg)
 	WRITE_ONCE(memcg->soft_limit, PAGE_COUNTER_MAX);
 }
 
-bool memcg1_wait_acct_move(struct mem_cgroup *memcg);
-
 struct cgroup_taskset;
-int memcg1_can_attach(struct cgroup_taskset *tset);
-void memcg1_cancel_attach(struct cgroup_taskset *tset);
-void memcg1_move_task(void);
 void memcg1_css_offline(struct mem_cgroup *memcg);
 
 /* for encoding cft->private value on file */
@@ -130,7 +125,6 @@ static inline void memcg1_free_events(struct mem_cgroup *memcg) {}
 static inline void memcg1_memcg_init(struct mem_cgroup *memcg) {}
 static inline void memcg1_remove_from_trees(struct mem_cgroup *memcg) {}
 static inline void memcg1_soft_limit_reset(struct mem_cgroup *memcg) {}
-static inline bool memcg1_wait_acct_move(struct mem_cgroup *memcg) { return false; }
 static inline void memcg1_css_offline(struct mem_cgroup *memcg) {}
 
 static inline bool memcg1_oom_prepare(struct mem_cgroup *memcg, bool *locked) { return true; }
mm/memcontrol.c +0 -9
index 5c3a8629ef3e..94279b9c766a 100644
@@ -2242,12 +2242,6 @@ int try_charge_memcg(struct mem_cgroup *memcg, gfp_t gfp_mask,
 	 */
 	if (nr_reclaimed && nr_pages <= (1 << PAGE_ALLOC_COSTLY_ORDER))
 		goto retry;
-	/*
-	 * At task move, charge accounts can be doubly counted. So, it's
-	 * better to wait until the end of task_move if something is going on.
-	 */
-	if (memcg1_wait_acct_move(mem_over_limit))
-		goto retry;
 
 	if (nr_retries--)
 		goto retry;
@@ -4439,9 +4433,6 @@ struct cgroup_subsys memory_cgrp_subsys = {
 	.exit = mem_cgroup_exit,
 	.dfl_cftypes = memory_files,
 #ifdef CONFIG_MEMCG_V1
-	.can_attach = memcg1_can_attach,
-	.cancel_attach = memcg1_cancel_attach,
-	.post_attach = memcg1_move_task,
 	.legacy_cftypes = mem_cgroup_legacy_files,
 #endif
 	.early_init = 0,
Signature · 1 line
2.43.5
#5 lore raw

Re: [PATCH v1 2/6] memcg-v1: remove charge move code

mm/memcontrol-v1.c static int mem_cgroup_move_charge_write(struct cgroup_subsys_state *css, · line 778

Quoted 11 lines
mm/memcontrol-v1.c +0 -11
-
-	pgdat = folio_pgdat(folio);
-	from_vec = mem_cgroup_lruvec(from, pgdat);
-	to_vec = mem_cgroup_lruvec(to, pgdat);
-
-	folio_memcg_lock(folio);
-
-	if (folio_test_anon(folio)) {
-		if (folio_mapped(folio)) {
-			__mod_lruvec_state(from_vec, NR_ANON_MAPPED, -nr_pages);
-			__mod_lruvec_state(to_vec, NR_ANON_MAPPED, nr_pages);

Good, because this code was likely wrong :) (-> partially mapped anon folios)

Signature · 3 lines
Cheers,

David / dhildenb
#6 lore raw

Re: [PATCH v1 2/6] memcg-v1: remove charge move code

Quoted 6 lines · David Hildenbrand
Quoted 6 lines
mm/memcontrol-v1.c +0 -6
-
-	pgdat = folio_pgdat(folio);
-	from_vec = mem_cgroup_lruvec(from, pgdat);
-	to_vec = mem_cgroup_lruvec(to, pgdat);
-
-	folio_memcg_lock(folio);
Quoted 8 lines

mm/memcontrol-v1.c static int mem_cgroup_move_charge_write(struct cgroup_subsys_state *css, · line 778

Quoted 5 lines
mm/memcontrol-v1.c +0 -5
-
-	if (folio_test_anon(folio)) {
-		if (folio_mapped(folio)) {
-			__mod_lruvec_state(from_vec, NR_ANON_MAPPED, -nr_pages);
-			__mod_lruvec_state(to_vec, NR_ANON_MAPPED, nr_pages);

Good, because this code was likely wrong :) (-> partially mapped anon folios)

Staring at the code some more, mem_cgroup_move_charge_pte_range() refuses PTE-mapped large folios, so that might have done the right thing.

Signature · 3 lines
Cheers,

David / dhildenb
author maintainer
#8 lore raw

PATCH 3/6 memcg-v1: no need for memcg locking for dirty tracking

In reply to Shakeel Butt

During the era of memcg charge migration, the kernel has to be make sure that the dirty stat updates do not race with the charge migration. Otherwise it might update the dirty stats of the wrong memcg. Now with the memcg charge migration deprecated, there is no more race for dirty stat updates and the previous locking can be removed.

---
 fs/buffer.c         |  5 -----
 mm/page-writeback.c | 16 +++-------------
 2 files changed, 3 insertions(+), 18 deletions(-)
fs/buffer.c +0 -5
index 1fc9a50def0b..88e765b0699f 100644
@@ -736,15 +736,12 @@ bool block_dirty_folio(struct address_space *mapping, struct folio *folio)
 	 * Lock out page's memcg migration to keep PageDirty
 	 * synchronized with per-memcg dirty page counters.
 	 */
-	folio_memcg_lock(folio);
 	newly_dirty = !folio_test_set_dirty(folio);
 	spin_unlock(&mapping->i_private_lock);
 
 	if (newly_dirty)
 		__folio_mark_dirty(folio, mapping, 1);
 
-	folio_memcg_unlock(folio);
-
 	if (newly_dirty)
 		__mark_inode_dirty(mapping->host, I_DIRTY_PAGES);
 
@@ -1194,13 +1191,11 @@ void mark_buffer_dirty(struct buffer_head *bh)
 		struct folio *folio = bh->b_folio;
 		struct address_space *mapping = NULL;
 
-		folio_memcg_lock(folio);
 		if (!folio_test_set_dirty(folio)) {
 			mapping = folio->mapping;
 			if (mapping)
 				__folio_mark_dirty(folio, mapping, 0);
 		}
-		folio_memcg_unlock(folio);
 		if (mapping)
 			__mark_inode_dirty(mapping->host, I_DIRTY_PAGES);
 	}
mm/page-writeback.c +3 -13
index 1d7179aba8e3..a76a73529fd9 100644
@@ -2743,8 +2743,6 @@ EXPORT_SYMBOL(noop_dirty_folio);
 /*
  * Helper function for set_page_dirty family.
  *
- * Caller must hold folio_memcg_lock().
- *
  * NOTE: This relies on being atomic wrt interrupts.
  */
 static void folio_account_dirtied(struct folio *folio,
@@ -2777,7 +2775,6 @@ static void folio_account_dirtied(struct folio *folio,
 /*
  * Helper function for deaccounting dirty page without writeback.
  *
- * Caller must hold folio_memcg_lock().
  */
 void folio_account_cleaned(struct folio *folio, struct bdi_writeback *wb)
 {
@@ -2795,9 +2792,8 @@ void folio_account_cleaned(struct folio *folio, struct bdi_writeback *wb)
  * If warn is true, then emit a warning if the folio is not uptodate and has
  * not been truncated.
  *
- * The caller must hold folio_memcg_lock().  It is the caller's
- * responsibility to prevent the folio from being truncated while
- * this function is in progress, although it may have been truncated
+ * It is the caller's responsibility to prevent the folio from being truncated
+ * while this function is in progress, although it may have been truncated
  * before this function is called.  Most callers have the folio locked.
  * A few have the folio blocked from truncation through other means (e.g.
  * zap_vma_pages() has it mapped and is holding the page table lock).
@@ -2841,14 +2837,10 @@ void __folio_mark_dirty(struct folio *folio, struct address_space *mapping,
  */
 bool filemap_dirty_folio(struct address_space *mapping, struct folio *folio)
 {
-	folio_memcg_lock(folio);
-	if (folio_test_set_dirty(folio)) {
-		folio_memcg_unlock(folio);
+	if (folio_test_set_dirty(folio))
 		return false;
-	}
 
 	__folio_mark_dirty(folio, mapping, !folio_test_private(folio));
-	folio_memcg_unlock(folio);
 
 	if (mapping->host) {
 		/* !PageAnon && !swapper_space */
@@ -2975,14 +2967,12 @@ void __folio_cancel_dirty(struct folio *folio)
 		struct bdi_writeback *wb;
 		struct wb_lock_cookie cookie = {};
 
-		folio_memcg_lock(folio);
 		wb = unlocked_inode_to_wb_begin(inode, &cookie);
 
 		if (folio_test_clear_dirty(folio))
 			folio_account_cleaned(folio, wb);
 
 		unlocked_inode_to_wb_end(inode, &cookie);
-		folio_memcg_unlock(folio);
 	} else {
 		folio_clear_dirty(folio);
 	}
Signature · 1 line
2.43.5
Acked-by on v1 3/6
#9 lore raw

Re: [PATCH v1 3/6] memcg-v1: no need for memcg locking for dirty tracking

Quoted 4 lines · Shakeel Butt

During the era of memcg charge migration, the kernel has to be make sure that the dirty stat updates do not race with the charge migration. Otherwise it might update the dirty stats of the wrong memcg. Now with the memcg charge migration deprecated, there is no more race for dirty

s@deprecated@gone@

Quoted 3 lines

stat updates and the previous locking can be removed.

LGTM otherwise

Quoted 105 lines
---
 fs/buffer.c         |  5 -----
 mm/page-writeback.c | 16 +++-------------
 2 files changed, 3 insertions(+), 18 deletions(-)
fs/buffer.c +0 -5
index 1fc9a50def0b..88e765b0699f 100644
@@ -736,15 +736,12 @@ bool block_dirty_folio(struct address_space *mapping, struct folio *folio)
 	 * Lock out page's memcg migration to keep PageDirty
 	 * synchronized with per-memcg dirty page counters.
 	 */
-	folio_memcg_lock(folio);
 	newly_dirty = !folio_test_set_dirty(folio);
 	spin_unlock(&mapping->i_private_lock);
 
 	if (newly_dirty)
 		__folio_mark_dirty(folio, mapping, 1);
 
-	folio_memcg_unlock(folio);
-
 	if (newly_dirty)
 		__mark_inode_dirty(mapping->host, I_DIRTY_PAGES);
 
@@ -1194,13 +1191,11 @@ void mark_buffer_dirty(struct buffer_head *bh)
 		struct folio *folio = bh->b_folio;
 		struct address_space *mapping = NULL;
 
-		folio_memcg_lock(folio);
 		if (!folio_test_set_dirty(folio)) {
 			mapping = folio->mapping;
 			if (mapping)
 				__folio_mark_dirty(folio, mapping, 0);
 		}
-		folio_memcg_unlock(folio);
 		if (mapping)
 			__mark_inode_dirty(mapping->host, I_DIRTY_PAGES);
 	}
mm/page-writeback.c +3 -13
index 1d7179aba8e3..a76a73529fd9 100644
@@ -2743,8 +2743,6 @@ EXPORT_SYMBOL(noop_dirty_folio);
 /*
  * Helper function for set_page_dirty family.
  *
- * Caller must hold folio_memcg_lock().
- *
  * NOTE: This relies on being atomic wrt interrupts.
  */
 static void folio_account_dirtied(struct folio *folio,
@@ -2777,7 +2775,6 @@ static void folio_account_dirtied(struct folio *folio,
 /*
  * Helper function for deaccounting dirty page without writeback.
  *
- * Caller must hold folio_memcg_lock().
  */
 void folio_account_cleaned(struct folio *folio, struct bdi_writeback *wb)
 {
@@ -2795,9 +2792,8 @@ void folio_account_cleaned(struct folio *folio, struct bdi_writeback *wb)
  * If warn is true, then emit a warning if the folio is not uptodate and has
  * not been truncated.
  *
- * The caller must hold folio_memcg_lock().  It is the caller's
- * responsibility to prevent the folio from being truncated while
- * this function is in progress, although it may have been truncated
+ * It is the caller's responsibility to prevent the folio from being truncated
+ * while this function is in progress, although it may have been truncated
  * before this function is called.  Most callers have the folio locked.
  * A few have the folio blocked from truncation through other means (e.g.
  * zap_vma_pages() has it mapped and is holding the page table lock).
@@ -2841,14 +2837,10 @@ void __folio_mark_dirty(struct folio *folio, struct address_space *mapping,
  */
 bool filemap_dirty_folio(struct address_space *mapping, struct folio *folio)
 {
-	folio_memcg_lock(folio);
-	if (folio_test_set_dirty(folio)) {
-		folio_memcg_unlock(folio);
+	if (folio_test_set_dirty(folio))
 		return false;
-	}
 
 	__folio_mark_dirty(folio, mapping, !folio_test_private(folio));
-	folio_memcg_unlock(folio);
 
 	if (mapping->host) {
 		/* !PageAnon && !swapper_space */
@@ -2975,14 +2967,12 @@ void __folio_cancel_dirty(struct folio *folio)
 		struct bdi_writeback *wb;
 		struct wb_lock_cookie cookie = {};
 
-		folio_memcg_lock(folio);
 		wb = unlocked_inode_to_wb_begin(inode, &cookie);
 
 		if (folio_test_clear_dirty(folio))
 			folio_account_cleaned(folio, wb);
 
 		unlocked_inode_to_wb_end(inode, &cookie);
-		folio_memcg_unlock(folio);
 	} else {
 		folio_clear_dirty(folio);
 	}
Signature · 1 line
2.43.5
Signature · 2 lines
Michal Hocko
SUSE Labs
author maintainer
#10 lore raw

Re: [PATCH v1 3/6] memcg-v1: no need for memcg locking for dirty tracking

Quoted 6 lines · Michal Hocko
Quoted 4 lines · Shakeel Butt

During the era of memcg charge migration, the kernel has to be make sure that the dirty stat updates do not race with the charge migration. Otherwise it might update the dirty stats of the wrong memcg. Now with the memcg charge migration deprecated, there is no more race for dirty

Quoted 9 lines

s@deprecated@gone@

Quoted 3 lines

stat updates and the previous locking can be removed.

LGTM otherwise

Thanks Michal for the review.

Andrew, please fixup the commit message as suggested by Michal when you pick this series.

maintainer Reviewed-by on v1 3/6
#11 lore raw

Re: [PATCH v1 3/6] memcg-v1: no need for memcg locking for dirty tracking

In reply to Shakeel Butt
Quoted 7 lines · Shakeel Butt

During the era of memcg charge migration, the kernel has to be make sure that the dirty stat updates do not race with the charge migration. Otherwise it might update the dirty stats of the wrong memcg. Now with the memcg charge migration deprecated, there is no more race for dirty stat updates and the previous locking can be removed.

maintainer Acked-by on v1 3/6
#12 lore raw

Re: [PATCH v1 3/6] memcg-v1: no need for memcg locking for dirty tracking

In reply to Shakeel Butt
Quoted 7 lines · Shakeel Butt

During the era of memcg charge migration, the kernel has to be make sure that the dirty stat updates do not race with the charge migration. Otherwise it might update the dirty stats of the wrong memcg. Now with the memcg charge migration deprecated, there is no more race for dirty stat updates and the previous locking can be removed.

author maintainer
#13 lore raw

PATCH 4/6 memcg-v1: no need for memcg locking for writeback tracking

In reply to Shakeel Butt

During the era of memcg charge migration, the kernel has to be make sure that the writeback stat updates do not race with the charge migration. Otherwise it might update the writeback stats of the wrong memcg. Now with the memcg charge migration deprecated, there is no more race for writeback stat updates and the previous locking can be removed.

---
 mm/page-writeback.c | 4 ----
 1 file changed, 4 deletions(-)
mm/page-writeback.c +0 -4
index a76a73529fd9..9c3317c3a615 100644
@@ -3083,7 +3083,6 @@ bool __folio_end_writeback(struct folio *folio)
 	struct address_space *mapping = folio_mapping(folio);
 	bool ret;
 
-	folio_memcg_lock(folio);
 	if (mapping && mapping_use_writeback_tags(mapping)) {
 		struct inode *inode = mapping->host;
 		struct backing_dev_info *bdi = inode_to_bdi(inode);
@@ -3114,7 +3113,6 @@ bool __folio_end_writeback(struct folio *folio)
 	lruvec_stat_mod_folio(folio, NR_WRITEBACK, -nr);
 	zone_stat_mod_folio(folio, NR_ZONE_WRITE_PENDING, -nr);
 	node_stat_mod_folio(folio, NR_WRITTEN, nr);
-	folio_memcg_unlock(folio);
 
 	return ret;
 }
@@ -3127,7 +3125,6 @@ void __folio_start_writeback(struct folio *folio, bool keep_write)
 
 	VM_BUG_ON_FOLIO(folio_test_writeback(folio), folio);
 
-	folio_memcg_lock(folio);
 	if (mapping && mapping_use_writeback_tags(mapping)) {
 		XA_STATE(xas, &mapping->i_pages, folio_index(folio));
 		struct inode *inode = mapping->host;
@@ -3168,7 +3165,6 @@ void __folio_start_writeback(struct folio *folio, bool keep_write)
 
 	lruvec_stat_mod_folio(folio, NR_WRITEBACK, nr);
 	zone_stat_mod_folio(folio, NR_ZONE_WRITE_PENDING, nr);
-	folio_memcg_unlock(folio);
 
 	access_ret = arch_make_folio_accessible(folio);
 	/*
Signature · 1 line
2.43.5
Acked-by on v1 4/6
#14 lore raw

Re: [PATCH v1 4/6] memcg-v1: no need for memcg locking for writeback tracking

Quoted 4 lines · Shakeel Butt

During the era of memcg charge migration, the kernel has to be make sure that the writeback stat updates do not race with the charge migration. Otherwise it might update the writeback stats of the wrong memcg. Now with the memcg charge migration deprecated, there is no more race for

s@deprecated@gone

Quoted 3 lines

writeback stat updates and the previous locking can be removed.

Thanks!

Quoted 42 lines
---
 mm/page-writeback.c | 4 ----
 1 file changed, 4 deletions(-)
mm/page-writeback.c +0 -4
index a76a73529fd9..9c3317c3a615 100644
@@ -3083,7 +3083,6 @@ bool __folio_end_writeback(struct folio *folio)
 	struct address_space *mapping = folio_mapping(folio);
 	bool ret;
 
-	folio_memcg_lock(folio);
 	if (mapping && mapping_use_writeback_tags(mapping)) {
 		struct inode *inode = mapping->host;
 		struct backing_dev_info *bdi = inode_to_bdi(inode);
@@ -3114,7 +3113,6 @@ bool __folio_end_writeback(struct folio *folio)
 	lruvec_stat_mod_folio(folio, NR_WRITEBACK, -nr);
 	zone_stat_mod_folio(folio, NR_ZONE_WRITE_PENDING, -nr);
 	node_stat_mod_folio(folio, NR_WRITTEN, nr);
-	folio_memcg_unlock(folio);
 
 	return ret;
 }
@@ -3127,7 +3125,6 @@ void __folio_start_writeback(struct folio *folio, bool keep_write)
 
 	VM_BUG_ON_FOLIO(folio_test_writeback(folio), folio);
 
-	folio_memcg_lock(folio);
 	if (mapping && mapping_use_writeback_tags(mapping)) {
 		XA_STATE(xas, &mapping->i_pages, folio_index(folio));
 		struct inode *inode = mapping->host;
@@ -3168,7 +3165,6 @@ void __folio_start_writeback(struct folio *folio, bool keep_write)
 
 	lruvec_stat_mod_folio(folio, NR_WRITEBACK, nr);
 	zone_stat_mod_folio(folio, NR_ZONE_WRITE_PENDING, nr);
-	folio_memcg_unlock(folio);
 
 	access_ret = arch_make_folio_accessible(folio);
 	/*
Signature · 1 line
2.43.5
Signature · 2 lines
Michal Hocko
SUSE Labs
maintainer Reviewed-by on v1 4/6
#15 lore raw

Re: [PATCH v1 4/6] memcg-v1: no need for memcg locking for writeback tracking

In reply to Shakeel Butt
Quoted 7 lines · Shakeel Butt

During the era of memcg charge migration, the kernel has to be make sure that the writeback stat updates do not race with the charge migration. Otherwise it might update the writeback stats of the wrong memcg. Now with the memcg charge migration deprecated, there is no more race for writeback stat updates and the previous locking can be removed.

maintainer Acked-by on v1 4/6
#16 lore raw

Re: [PATCH v1 4/6] memcg-v1: no need for memcg locking for writeback tracking

In reply to Shakeel Butt
Quoted 7 lines · Shakeel Butt

During the era of memcg charge migration, the kernel has to be make sure that the writeback stat updates do not race with the charge migration. Otherwise it might update the writeback stats of the wrong memcg. Now with the memcg charge migration deprecated, there is no more race for writeback stat updates and the previous locking can be removed.

author maintainer
#17 lore raw

PATCH 5/6 memcg-v1: no need for memcg locking for MGLRU

In reply to Shakeel Butt

While updating the generation of the folios, MGLRU requires that the folio's memcg association remains stable. With the charge migration deprecated, there is no need for MGLRU to acquire locks to keep the folio and memcg association stable.

---
 mm/vmscan.c | 11 -----------
 1 file changed, 11 deletions(-)
mm/vmscan.c +0 -11
index 29c098790b01..fd7171658b63 100644
@@ -3662,10 +3662,6 @@ static void walk_mm(struct mm_struct *mm, struct lru_gen_mm_walk *walk)
 		if (walk->seq != max_seq)
 			break;
 
-		/* folio_update_gen() requires stable folio_memcg() */
-		if (!mem_cgroup_trylock_pages(memcg))
-			break;
-
 		/* the caller might be holding the lock for write */
 		if (mmap_read_trylock(mm)) {
 			err = walk_page_range(mm, walk->next_addr, ULONG_MAX, &mm_walk_ops, walk);
@@ -3673,8 +3669,6 @@ static void walk_mm(struct mm_struct *mm, struct lru_gen_mm_walk *walk)
 			mmap_read_unlock(mm);
 		}
 
-		mem_cgroup_unlock_pages();
-
 		if (walk->batched) {
 			spin_lock_irq(&lruvec->lru_lock);
 			reset_batch_size(walk);
@@ -4096,10 +4090,6 @@ bool lru_gen_look_around(struct page_vma_mapped_walk *pvmw)
 		}
 	}
 
-	/* folio_update_gen() requires stable folio_memcg() */
-	if (!mem_cgroup_trylock_pages(memcg))
-		return true;
-
 	arch_enter_lazy_mmu_mode();
 
 	pte -= (addr - start) / PAGE_SIZE;
@@ -4144,7 +4134,6 @@ bool lru_gen_look_around(struct page_vma_mapped_walk *pvmw)
 	}
 
 	arch_leave_lazy_mmu_mode();
-	mem_cgroup_unlock_pages();
 
 	/* feedback from rmap walkers to page table walkers */
 	if (mm_state && suitable_to_scan(i, young))
Signature · 1 line
2.43.5
maintainer Reviewed-by on v1 5/6
#18 lore raw

Re: [PATCH v1 5/6] memcg-v1: no need for memcg locking for MGLRU

Quoted 6 lines · Shakeel Butt

While updating the generation of the folios, MGLRU requires that the folio's memcg association remains stable. With the charge migration deprecated, there is no need for MGLRU to acquire locks to keep the folio and memcg association stable.

#19 lore raw

Re: [PATCH v1 5/6] memcg-v1: no need for memcg locking for MGLRU

In reply to Shakeel Butt
Quoted 16 lines · Shakeel Butt

While updating the generation of the folios, MGLRU requires that the folio's memcg association remains stable. With the charge migration deprecated, there is no need for MGLRU to acquire locks to keep the folio and memcg association stable.

---
 mm/vmscan.c | 11 -----------
 1 file changed, 11 deletions(-)
mm/vmscan.c +0 -0
index 29c098790b01..fd7171658b63 100644

mm/vmscan.c static void walk_mm(struct mm_struct *mm, struct lru_gen_mm_walk *walk) · line 3663

Quoted 2 lines
mm/vmscan.c +0 -0
@@ -3662,10 +3662,6 @@ static void walk_mm(struct mm_struct *mm, struct lru_gen_mm_walk *walk)
                if (walk->seq != max_seq)
                        break;

Please remove the lingering struct mem_cgroup *memcg as well as folio_memcg_rcu(). Otherwise it causes both build and lockdep warnings.

Quoted 39 lines
mm/vmscan.c +0 -12
-               /* folio_update_gen() requires stable folio_memcg() */
-               if (!mem_cgroup_trylock_pages(memcg))
-                       break;
-
                /* the caller might be holding the lock for write */
                if (mmap_read_trylock(mm)) {
                        err = walk_page_range(mm, walk->next_addr, ULONG_MAX, &mm_walk_ops, walk);
@@ -3673,8 +3669,6 @@ static void walk_mm(struct mm_struct *mm, struct lru_gen_mm_walk *walk)
                        mmap_read_unlock(mm);
                }
 
-               mem_cgroup_unlock_pages();
-
                if (walk->batched) {
                        spin_lock_irq(&lruvec->lru_lock);
                        reset_batch_size(walk);
@@ -4096,10 +4090,6 @@ bool lru_gen_look_around(struct page_vma_mapped_walk *pvmw)
                }
        }
 
-       /* folio_update_gen() requires stable folio_memcg() */
-       if (!mem_cgroup_trylock_pages(memcg))
-               return true;
-
        arch_enter_lazy_mmu_mode();
 
        pte -= (addr - start) / PAGE_SIZE;
@@ -4144,7 +4134,6 @@ bool lru_gen_look_around(struct page_vma_mapped_walk *pvmw)
        }
 
        arch_leave_lazy_mmu_mode();
-       mem_cgroup_unlock_pages();
 
        /* feedback from rmap walkers to page table walkers */
        if (mm_state && suitable_to_scan(i, young))
--

2.43.5

author maintainer
#20 lore raw

Re: [PATCH v1 5/6] memcg-v1: no need for memcg locking for MGLRU

Quoted 12 lines · Yu Zhao
Quoted 11 lines · Shakeel Butt

While updating the generation of the folios, MGLRU requires that the folio's memcg association remains stable. With the charge migration deprecated, there is no need for MGLRU to acquire locks to keep the folio and memcg association stable.

---
 mm/vmscan.c | 11 -----------
 1 file changed, 11 deletions(-)
Quoted 12 lines

mm/vmscan.c static void walk_mm(struct mm_struct *mm, struct lru_gen_mm_walk *walk) · line 3663

Quoted 7 lines
mm/vmscan.c +0 -0
index 29c098790b01..fd7171658b63 100644
@@ -3662,10 +3662,6 @@ static void walk_mm(struct mm_struct *mm, struct lru_gen_mm_walk *walk)
                if (walk->seq != max_seq)
                        break;

Please remove the lingering struct mem_cgroup *memcg as well as folio_memcg_rcu(). Otherwise it causes both build and lockdep warnings.

Thanks for catching this. The unused warning is already fixed by Andrew, I will fix the folio_memcg_rcu() usage.

author maintainer
#21 lore raw

Re: [PATCH v1 5/6] memcg-v1: no need for memcg locking for MGLRU

In reply to Shakeel Butt
Quoted 6 lines · Shakeel Butt

While updating the generation of the folios, MGLRU requires that the folio's memcg association remains stable. With the charge migration deprecated, there is no need for MGLRU to acquire locks to keep the folio and memcg association stable.

Andrew, can you please apply the following fix to this patch after your unused fixup?

index fd7171658b63..b8b0e8fa1332 100644

mm/vmscan.c +1 -1
@@ -3353,7 +3353,7 @@ static struct folio *get_pfn_folio(unsigned long pfn, struct mem_cgroup *memcg,
        if (folio_nid(folio) != pgdat->node_id)
                return NULL;
 
-       if (folio_memcg_rcu(folio) != memcg)
+       if (folio_memcg(folio) != memcg)
                return NULL;
 
        /* file VMAs can contain anon pages from COW */
#22 lore raw

Re: [PATCH v1 5/6] memcg-v1: no need for memcg locking for MGLRU

Quoted 11 lines · Shakeel Butt
Quoted 6 lines · Shakeel Butt

While updating the generation of the folios, MGLRU requires that the folio's memcg association remains stable. With the charge migration deprecated, there is no need for MGLRU to acquire locks to keep the folio and memcg association stable.

Andrew, can you please apply the following fix to this patch after your unused fixup?

Thanks!

Quoted 14 lines

index fd7171658b63..b8b0e8fa1332 100644

mm/vmscan.c +1 -1
@@ -3353,7 +3353,7 @@ static struct folio *get_pfn_folio(unsigned long pfn, struct mem_cgroup *memcg,
        if (folio_nid(folio) != pgdat->node_id)
                return NULL;
 
-       if (folio_memcg_rcu(folio) != memcg)
+       if (folio_memcg(folio) != memcg)
                return NULL;
 
        /* file VMAs can contain anon pages from COW */
#23 lore raw

Re: [PATCH v1 5/6] memcg-v1: no need for memcg locking for MGLRU

Quoted 8 lines · Yu Zhao
Quoted 7 lines · Shakeel Butt
Quoted 5 lines · Shakeel Butt

While updating the generation of the folios, MGLRU requires that the folio's memcg association remains stable. With the charge migration deprecated, there is no need for MGLRU to acquire locks to keep the folio and memcg association stable.

Quoted 6 lines
Quoted 4 lines
Quoted 1 line

Andrew, can you please apply the following fix to this patch after your unused fixup?

Thanks!

syzbot caught the following:

  WARNING: CPU: 0 PID: 85 at mm/vmscan.c:3140 folio_update_gen+0x23d/0x250 mm/vmscan.c:3140
  ...

Andrew, can you please fix this in place? Thank you.

mm/vmscan.c +0 -1
index ddaaff67642e..9a610dbff384 100644
@@ -3138,7 +3138,6 @@ static int folio_update_gen(struct folio *folio, int gen)
 	unsigned long new_flags, old_flags = READ_ONCE(folio->flags);
 
 	VM_WARN_ON_ONCE(gen >= MAX_NR_GENS);
-	VM_WARN_ON_ONCE(!rcu_read_lock_held());
 
 	do {
 		/* lru_gen_del_folio() has isolated this page? */
maintainer
#24 lore raw

Re: [PATCH v1 5/6] memcg-v1: no need for memcg locking for MGLRU

Quoted 14 lines · On Mon, 4 Nov 2024 10:30:29 -0700 Yu Zhao <[email protected]> wrote:
Quoted 13 lines · Yu Zhao
Quoted 11 lines · Shakeel Butt
Quoted 6 lines · Shakeel Butt

While updating the generation of the folios, MGLRU requires that the folio's memcg association remains stable. With the charge migration deprecated, there is no need for MGLRU to acquire locks to keep the folio and memcg association stable.

Andrew, can you please apply the following fix to this patch after your unused fixup?

Quoted 8 lines
Quoted 1 line

Thanks!

syzbot caught the following:

  WARNING: CPU: 0 PID: 85 at mm/vmscan.c:3140 folio_update_gen+0x23d/0x250 mm/vmscan.c:3140
  ...

Andrew, can you please fix this in place?

OK, but...

mm/vmscan.c static int folio_update_gen(struct folio *folio, int gen) · line 3143

Quoted 10 lines
mm/vmscan.c +0 -1
@@ -3138,7 +3138,6 @@ static int folio_update_gen(struct folio *folio, int gen)
 	unsigned long new_flags, old_flags = READ_ONCE(folio->flags);
 
 	VM_WARN_ON_ONCE(gen >= MAX_NR_GENS);
-	VM_WARN_ON_ONCE(!rcu_read_lock_held());
 
 	do {
 		/* lru_gen_del_folio() has isolated this page? */

it would be good to know why this assertion is considered incorrect? And a link to the sysbot report?

Thanks.

author maintainer
#25 lore raw

Re: [PATCH v1 5/6] memcg-v1: no need for memcg locking for MGLRU

Quoted 32 lines · Andrew Morton
Quoted 14 lines · On Mon, 4 Nov 2024 10:30:29 -0700 Yu Zhao <[email protected]> wrote:
Quoted 13 lines · Yu Zhao
Quoted 11 lines · Shakeel Butt
Quoted 6 lines · Shakeel Butt

While updating the generation of the folios, MGLRU requires that the folio's memcg association remains stable. With the charge migration deprecated, there is no need for MGLRU to acquire locks to keep the folio and memcg association stable.

Andrew, can you please apply the following fix to this patch after your unused fixup?

Quoted 8 lines
Quoted 1 line

Thanks!

syzbot caught the following:

  WARNING: CPU: 0 PID: 85 at mm/vmscan.c:3140 folio_update_gen+0x23d/0x250 mm/vmscan.c:3140
  ...

Andrew, can you please fix this in place?

OK, but...

Quoted 5 lines
mm/vmscan.c +0 -0
@@ -3138,7 +3138,6 @@ static int folio_update_gen(struct folio *folio, int gen)
 	unsigned long new_flags, old_flags = READ_ONCE(folio->flags);
 
Quoted 8 lines

mm/vmscan.c static int folio_update_gen(struct folio *folio, int gen) · line 3143

Quoted 5 lines
mm/vmscan.c +0 -1
 	VM_WARN_ON_ONCE(gen >= MAX_NR_GENS);
-	VM_WARN_ON_ONCE(!rcu_read_lock_held());
 
 	do {
 		/* lru_gen_del_folio() has isolated this page? */

it would be good to know why this assertion is considered incorrect? And a link to the sysbot report?

So, this assertion is incorrect after this patch series that has removed the charge migration and has removed mem_cgroup_trylock_pages() / mem_cgroup_unlock_pages() from the caller of this function (folio_update_gen()).

#26 lore raw

Re: [PATCH v1 5/6] memcg-v1: no need for memcg locking for MGLRU

In reply to Andrew Morton
Quoted 33 lines · Andrew Morton
Quoted 14 lines · On Mon, 4 Nov 2024 10:30:29 -0700 Yu Zhao <[email protected]> wrote:
Quoted 13 lines · Yu Zhao
Quoted 11 lines · Shakeel Butt
Quoted 6 lines · Shakeel Butt

While updating the generation of the folios, MGLRU requires that the folio's memcg association remains stable. With the charge migration deprecated, there is no need for MGLRU to acquire locks to keep the folio and memcg association stable.

Andrew, can you please apply the following fix to this patch after your unused fixup?

Quoted 8 lines
Quoted 1 line

Thanks!

syzbot caught the following:

  WARNING: CPU: 0 PID: 85 at mm/vmscan.c:3140 folio_update_gen+0x23d/0x250 mm/vmscan.c:3140
  ...

Andrew, can you please fix this in place?

OK, but...

Quoted 5 lines
mm/vmscan.c +0 -0
@@ -3138,7 +3138,6 @@ static int folio_update_gen(struct folio *folio, int gen)
      unsigned long new_flags, old_flags = READ_ONCE(folio->flags);
 
Quoted 7 lines

mm/vmscan.c static int folio_update_gen(struct folio *folio, int gen) · line 3143

Quoted 5 lines
mm/vmscan.c +0 -1
      VM_WARN_ON_ONCE(gen >= MAX_NR_GENS);
-     VM_WARN_ON_ONCE(!rcu_read_lock_held());
 
      do {
              /* lru_gen_del_folio() has isolated this page? */

it would be good to know why this assertion is considered incorrect?

The assertion was caused by the patch in this thread. It used to assert that a folio must be protected from charge migration. Charge migration is removed by this series, and as part of the effort, this patch removes the RCU lock.

Quoted 1 line

And a link to the sysbot report?

https://syzkaller.appspot.com/bug?extid=24f45b8beab9788e467e

#27 lore raw

Re: [PATCH v1 5/6] memcg-v1: no need for memcg locking for MGLRU

Quoted 43 lines · Yu Zhao
Quoted 33 lines · Andrew Morton
Quoted 14 lines · On Mon, 4 Nov 2024 10:30:29 -0700 Yu Zhao <[email protected]> wrote:
Quoted 13 lines · Yu Zhao
Quoted 11 lines · Shakeel Butt
Quoted 6 lines · Shakeel Butt

While updating the generation of the folios, MGLRU requires that the folio's memcg association remains stable. With the charge migration deprecated, there is no need for MGLRU to acquire locks to keep the folio and memcg association stable.

Andrew, can you please apply the following fix to this patch after your unused fixup?

Quoted 8 lines
Quoted 1 line

Thanks!

syzbot caught the following:

  WARNING: CPU: 0 PID: 85 at mm/vmscan.c:3140 folio_update_gen+0x23d/0x250 mm/vmscan.c:3140
  ...

Andrew, can you please fix this in place?

OK, but...

Quoted 5 lines
mm/vmscan.c +0 -0
@@ -3138,7 +3138,6 @@ static int folio_update_gen(struct folio *folio, int gen)
      unsigned long new_flags, old_flags = READ_ONCE(folio->flags);
 
Quoted 7 lines

mm/vmscan.c static int folio_update_gen(struct folio *folio, int gen) · line 3143

Quoted 5 lines
mm/vmscan.c +0 -1
      VM_WARN_ON_ONCE(gen >= MAX_NR_GENS);
-     VM_WARN_ON_ONCE(!rcu_read_lock_held());
 
      do {
              /* lru_gen_del_folio() has isolated this page? */

it would be good to know why this assertion is considered incorrect?

Quoted 8 lines

The assertion was caused by the patch in this thread. It used to assert that a folio must be protected from charge migration. Charge migration is removed by this series, and as part of the effort, this patch removes the RCU lock.

Quoted 1 line

And a link to the sysbot report?

https://syzkaller.appspot.com/bug?extid=24f45b8beab9788e467e

Or this link would work better:

https://lore.kernel.org/lkml/[email protected]/

maintainer
#28 lore raw

Re: [PATCH v1 5/6] memcg-v1: no need for memcg locking for MGLRU

Quoted 5 lines · On Mon, 4 Nov 2024 15:08:09 -0700 Yu Zhao <[email protected]> wrote:
Quoted 5 lines

The assertion was caused by the patch in this thread. It used to assert that a folio must be protected from charge migration. Charge migration is removed by this series, and as part of the effort, this patch removes the RCU lock.

Quoted 7 lines
Quoted 3 lines
Quoted 1 line

And a link to the sysbot report?

https://syzkaller.appspot.com/bug?extid=24f45b8beab9788e467e

Or this link would work better:

https://lore.kernel.org/lkml/[email protected]/

Thanks, I pasted everyone's everything in there, so it will all be accessible by the sufficiently patient.

author maintainer
#29 lore raw

PATCH 6/6 memcg-v1: remove memcg move locking code

In reply to Shakeel Butt

The memcg v1's charge move feature has been deprecated. All the places using the memcg move lock, have stopped using it as they don't need the protection any more. Let's proceed to remove all the locking code related to charge moving.

---

Changes since RFC:
- Remove the memcg move locking in separate patches.

 include/linux/memcontrol.h | 54 -------------------------
 mm/filemap.c               |  1 -
 mm/memcontrol-v1.c         | 82 --------------------------------------
 mm/memcontrol.c            |  5 ---
 mm/rmap.c                  |  1 -
 5 files changed, 143 deletions(-)
include/linux/memcontrol.h +0 -54
index 798db70b0a30..932534291ca2 100644
@@ -299,20 +299,10 @@ struct mem_cgroup {
 	/* For oom notifier event fd */
 	struct list_head oom_notify;
 
-	/* taken only while moving_account > 0 */
-	spinlock_t move_lock;
-	unsigned long move_lock_flags;
-
 	/* Legacy tcp memory accounting */
 	bool tcpmem_active;
 	int tcpmem_pressure;
 
-	/*
-	 * set > 0 if pages under this cgroup are moving to other cgroup.
-	 */
-	atomic_t moving_account;
-	struct task_struct *move_lock_task;
-
 	/* List of events which userspace want to receive */
 	struct list_head event_list;
 	spinlock_t event_list_lock;
@@ -428,9 +418,7 @@ static inline struct obj_cgroup *__folio_objcg(struct folio *folio)
  *
  * - the folio lock
  * - LRU isolation
- * - folio_memcg_lock()
  * - exclusive reference
- * - mem_cgroup_trylock_pages()
  *
  * For a kmem folio a caller should hold an rcu read lock to protect memcg
  * associated with a kmem folio from being released.
@@ -499,9 +487,7 @@ static inline struct mem_cgroup *folio_memcg_rcu(struct folio *folio)
  *
  * - the folio lock
  * - LRU isolation
- * - lock_folio_memcg()
  * - exclusive reference
- * - mem_cgroup_trylock_pages()
  *
  * For a kmem folio a caller should hold an rcu read lock to protect memcg
  * associated with a kmem folio from being released.
@@ -1873,26 +1859,6 @@ static inline bool task_in_memcg_oom(struct task_struct *p)
 	return p->memcg_in_oom;
 }
 
-void folio_memcg_lock(struct folio *folio);
-void folio_memcg_unlock(struct folio *folio);
-
-/* try to stablize folio_memcg() for all the pages in a memcg */
-static inline bool mem_cgroup_trylock_pages(struct mem_cgroup *memcg)
-{
-	rcu_read_lock();
-
-	if (mem_cgroup_disabled() || !atomic_read(&memcg->moving_account))
-		return true;
-
-	rcu_read_unlock();
-	return false;
-}
-
-static inline void mem_cgroup_unlock_pages(void)
-{
-	rcu_read_unlock();
-}
-
 static inline void mem_cgroup_enter_user_fault(void)
 {
 	WARN_ON(current->in_user_fault);
@@ -1914,26 +1880,6 @@ unsigned long memcg1_soft_limit_reclaim(pg_data_t *pgdat, int order,
 	return 0;
 }
 
-static inline void folio_memcg_lock(struct folio *folio)
-{
-}
-
-static inline void folio_memcg_unlock(struct folio *folio)
-{
-}
-
-static inline bool mem_cgroup_trylock_pages(struct mem_cgroup *memcg)
-{
-	/* to match folio_memcg_rcu() */
-	rcu_read_lock();
-	return true;
-}
-
-static inline void mem_cgroup_unlock_pages(void)
-{
-	rcu_read_unlock();
-}
-
 static inline bool task_in_memcg_oom(struct task_struct *p)
 {
 	return false;
mm/filemap.c +0 -1
index 630a1c431ea1..e582a1545d2a 100644
@@ -119,7 +119,6 @@
  *    ->i_pages lock		(folio_remove_rmap_pte->set_page_dirty)
  *    bdi.wb->list_lock		(folio_remove_rmap_pte->set_page_dirty)
  *    ->inode->i_lock		(folio_remove_rmap_pte->set_page_dirty)
- *    ->memcg->move_lock	(folio_remove_rmap_pte->folio_memcg_lock)
  *    bdi.wb->list_lock		(zap_pte_range->set_page_dirty)
  *    ->inode->i_lock		(zap_pte_range->set_page_dirty)
  *    ->private_lock		(zap_pte_range->block_dirty_folio)
mm/memcontrol-v1.c +0 -82

94 lines not on this page · raw message

mm/memcontrol.c +0 -5
index 94279b9c766a..3c223aaeb6af 100644
@@ -1189,7 +1189,6 @@ void lruvec_memcg_debug(struct lruvec *lruvec, struct folio *folio)
  * These functions are safe to use under any of the following conditions:
  * - folio locked
  * - folio_test_lru false
- * - folio_memcg_lock()
  * - folio frozen (refcount of 0)
  *
  * Return: The lruvec this folio is on with its lock held.
@@ -1211,7 +1210,6 @@ struct lruvec *folio_lruvec_lock(struct folio *folio)
  * These functions are safe to use under any of the following conditions:
  * - folio locked
  * - folio_test_lru false
- * - folio_memcg_lock()
  * - folio frozen (refcount of 0)
  *
  * Return: The lruvec this folio is on with its lock held and interrupts
@@ -1235,7 +1233,6 @@ struct lruvec *folio_lruvec_lock_irq(struct folio *folio)
  * These functions are safe to use under any of the following conditions:
  * - folio locked
  * - folio_test_lru false
- * - folio_memcg_lock()
  * - folio frozen (refcount of 0)
  *
  * Return: The lruvec this folio is on with its lock held and interrupts
@@ -2375,9 +2372,7 @@ static void commit_charge(struct folio *folio, struct mem_cgroup *memcg)
 	 *
 	 * - the page lock
 	 * - LRU isolation
-	 * - folio_memcg_lock()
 	 * - exclusive reference
-	 * - mem_cgroup_trylock_pages()
 	 */
 	folio->memcg_data = (unsigned long)memcg;
 }
mm/rmap.c +0 -1
index 4785a693857a..c6c4d4ea29a7 100644
@@ -32,7 +32,6 @@
  *                   swap_lock (in swap_duplicate, swap_info_get)
  *                     mmlist_lock (in mmput, drain_mmlist and others)
  *                     mapping->private_lock (in block_dirty_folio)
- *                       folio_lock_memcg move_lock (in block_dirty_folio)
  *                         i_pages lock (widely used)
  *                           lruvec->lru_lock (in folio_lruvec_lock_irq)
  *                     inode->i_lock (in set_page_dirty's __mark_inode_dirty)
Signature · 1 line
2.43.5
Acked-by on v1 6/6
#30 lore raw

Re: [PATCH v1 6/6] memcg-v1: remove memcg move locking code

Quoted 6 lines · Shakeel Butt

The memcg v1's charge move feature has been deprecated. All the places using the memcg move lock, have stopped using it as they don't need the protection any more. Let's proceed to remove all the locking code related to charge moving.

Thank you for restructuring this. Having all callers gone by now certainly makes this much safer and easier to review.

Quoted 275 lines

---

Changes since RFC:
- Remove the memcg move locking in separate patches.

 include/linux/memcontrol.h | 54 -------------------------
 mm/filemap.c               |  1 -
 mm/memcontrol-v1.c         | 82 --------------------------------------
 mm/memcontrol.c            |  5 ---
 mm/rmap.c                  |  1 -
 5 files changed, 143 deletions(-)
include/linux/memcontrol.h +0 -54
index 798db70b0a30..932534291ca2 100644
@@ -299,20 +299,10 @@ struct mem_cgroup {
 	/* For oom notifier event fd */
 	struct list_head oom_notify;
 
-	/* taken only while moving_account > 0 */
-	spinlock_t move_lock;
-	unsigned long move_lock_flags;
-
 	/* Legacy tcp memory accounting */
 	bool tcpmem_active;
 	int tcpmem_pressure;
 
-	/*
-	 * set > 0 if pages under this cgroup are moving to other cgroup.
-	 */
-	atomic_t moving_account;
-	struct task_struct *move_lock_task;
-
 	/* List of events which userspace want to receive */
 	struct list_head event_list;
 	spinlock_t event_list_lock;
@@ -428,9 +418,7 @@ static inline struct obj_cgroup *__folio_objcg(struct folio *folio)
  *
  * - the folio lock
  * - LRU isolation
- * - folio_memcg_lock()
  * - exclusive reference
- * - mem_cgroup_trylock_pages()
  *
  * For a kmem folio a caller should hold an rcu read lock to protect memcg
  * associated with a kmem folio from being released.
@@ -499,9 +487,7 @@ static inline struct mem_cgroup *folio_memcg_rcu(struct folio *folio)
  *
  * - the folio lock
  * - LRU isolation
- * - lock_folio_memcg()
  * - exclusive reference
- * - mem_cgroup_trylock_pages()
  *
  * For a kmem folio a caller should hold an rcu read lock to protect memcg
  * associated with a kmem folio from being released.
@@ -1873,26 +1859,6 @@ static inline bool task_in_memcg_oom(struct task_struct *p)
 	return p->memcg_in_oom;
 }
 
-void folio_memcg_lock(struct folio *folio);
-void folio_memcg_unlock(struct folio *folio);
-
-/* try to stablize folio_memcg() for all the pages in a memcg */
-static inline bool mem_cgroup_trylock_pages(struct mem_cgroup *memcg)
-{
-	rcu_read_lock();
-
-	if (mem_cgroup_disabled() || !atomic_read(&memcg->moving_account))
-		return true;
-
-	rcu_read_unlock();
-	return false;
-}
-
-static inline void mem_cgroup_unlock_pages(void)
-{
-	rcu_read_unlock();
-}
-
 static inline void mem_cgroup_enter_user_fault(void)
 {
 	WARN_ON(current->in_user_fault);
@@ -1914,26 +1880,6 @@ unsigned long memcg1_soft_limit_reclaim(pg_data_t *pgdat, int order,
 	return 0;
 }
 
-static inline void folio_memcg_lock(struct folio *folio)
-{
-}
-
-static inline void folio_memcg_unlock(struct folio *folio)
-{
-}
-
-static inline bool mem_cgroup_trylock_pages(struct mem_cgroup *memcg)
-{
-	/* to match folio_memcg_rcu() */
-	rcu_read_lock();
-	return true;
-}
-
-static inline void mem_cgroup_unlock_pages(void)
-{
-	rcu_read_unlock();
-}
-
 static inline bool task_in_memcg_oom(struct task_struct *p)
 {
 	return false;
mm/filemap.c +0 -1
index 630a1c431ea1..e582a1545d2a 100644
@@ -119,7 +119,6 @@
  *    ->i_pages lock		(folio_remove_rmap_pte->set_page_dirty)
  *    bdi.wb->list_lock		(folio_remove_rmap_pte->set_page_dirty)
  *    ->inode->i_lock		(folio_remove_rmap_pte->set_page_dirty)
- *    ->memcg->move_lock	(folio_remove_rmap_pte->folio_memcg_lock)
  *    bdi.wb->list_lock		(zap_pte_range->set_page_dirty)
  *    ->inode->i_lock		(zap_pte_range->set_page_dirty)
  *    ->private_lock		(zap_pte_range->block_dirty_folio)
mm/memcontrol-v1.c +0 -82
index 9c0fba8c8a83..539ceefa9d2d 100644
@@ -401,87 +401,6 @@ unsigned long memcg1_soft_limit_reclaim(pg_data_t *pgdat, int order,
 	return nr_reclaimed;
 }
 
-/**
- * folio_memcg_lock - Bind a folio to its memcg.
- * @folio: The folio.
- *
- * This function prevents unlocked LRU folios from being moved to
- * another cgroup.
- *
- * It ensures lifetime of the bound memcg.  The caller is responsible
- * for the lifetime of the folio.
- */
-void folio_memcg_lock(struct folio *folio)
-{
-	struct mem_cgroup *memcg;
-	unsigned long flags;
-
-	/*
-	 * The RCU lock is held throughout the transaction.  The fast
-	 * path can get away without acquiring the memcg->move_lock
-	 * because page moving starts with an RCU grace period.
-         */
-	rcu_read_lock();
-
-	if (mem_cgroup_disabled())
-		return;
-again:
-	memcg = folio_memcg(folio);
-	if (unlikely(!memcg))
-		return;
-
-#ifdef CONFIG_PROVE_LOCKING
-	local_irq_save(flags);
-	might_lock(&memcg->move_lock);
-	local_irq_restore(flags);
-#endif
-
-	if (atomic_read(&memcg->moving_account) <= 0)
-		return;
-
-	spin_lock_irqsave(&memcg->move_lock, flags);
-	if (memcg != folio_memcg(folio)) {
-		spin_unlock_irqrestore(&memcg->move_lock, flags);
-		goto again;
-	}
-
-	/*
-	 * When charge migration first begins, we can have multiple
-	 * critical sections holding the fast-path RCU lock and one
-	 * holding the slowpath move_lock. Track the task who has the
-	 * move_lock for folio_memcg_unlock().
-	 */
-	memcg->move_lock_task = current;
-	memcg->move_lock_flags = flags;
-}
-
-static void __folio_memcg_unlock(struct mem_cgroup *memcg)
-{
-	if (memcg && memcg->move_lock_task == current) {
-		unsigned long flags = memcg->move_lock_flags;
-
-		memcg->move_lock_task = NULL;
-		memcg->move_lock_flags = 0;
-
-		spin_unlock_irqrestore(&memcg->move_lock, flags);
-	}
-
-	rcu_read_unlock();
-}
-
-/**
- * folio_memcg_unlock - Release the binding between a folio and its memcg.
- * @folio: The folio.
- *
- * This releases the binding created by folio_memcg_lock().  This does
- * not change the accounting of this folio to its memcg, but it does
- * permit others to change it.
- */
-void folio_memcg_unlock(struct folio *folio)
-{
-	__folio_memcg_unlock(folio_memcg(folio));
-}
-
 static u64 mem_cgroup_move_charge_read(struct cgroup_subsys_state *css,
 				struct cftype *cft)
 {
@@ -1189,7 +1108,6 @@ void memcg1_memcg_init(struct mem_cgroup *memcg)
 {
 	INIT_LIST_HEAD(&memcg->oom_notify);
 	mutex_init(&memcg->thresholds_lock);
-	spin_lock_init(&memcg->move_lock);
 	INIT_LIST_HEAD(&memcg->event_list);
 	spin_lock_init(&memcg->event_list_lock);
 }
mm/memcontrol.c +0 -5
index 94279b9c766a..3c223aaeb6af 100644
@@ -1189,7 +1189,6 @@ void lruvec_memcg_debug(struct lruvec *lruvec, struct folio *folio)
  * These functions are safe to use under any of the following conditions:
  * - folio locked
  * - folio_test_lru false
- * - folio_memcg_lock()
  * - folio frozen (refcount of 0)
  *
  * Return: The lruvec this folio is on with its lock held.
@@ -1211,7 +1210,6 @@ struct lruvec *folio_lruvec_lock(struct folio *folio)
  * These functions are safe to use under any of the following conditions:
  * - folio locked
  * - folio_test_lru false
- * - folio_memcg_lock()
  * - folio frozen (refcount of 0)
  *
  * Return: The lruvec this folio is on with its lock held and interrupts
@@ -1235,7 +1233,6 @@ struct lruvec *folio_lruvec_lock_irq(struct folio *folio)
  * These functions are safe to use under any of the following conditions:
  * - folio locked
  * - folio_test_lru false
- * - folio_memcg_lock()
  * - folio frozen (refcount of 0)
  *
  * Return: The lruvec this folio is on with its lock held and interrupts
@@ -2375,9 +2372,7 @@ static void commit_charge(struct folio *folio, struct mem_cgroup *memcg)
 	 *
 	 * - the page lock
 	 * - LRU isolation
-	 * - folio_memcg_lock()
 	 * - exclusive reference
-	 * - mem_cgroup_trylock_pages()
 	 */
 	folio->memcg_data = (unsigned long)memcg;
 }
mm/rmap.c +0 -1
index 4785a693857a..c6c4d4ea29a7 100644
@@ -32,7 +32,6 @@
  *                   swap_lock (in swap_duplicate, swap_info_get)
  *                     mmlist_lock (in mmput, drain_mmlist and others)
  *                     mapping->private_lock (in block_dirty_folio)
- *                       folio_lock_memcg move_lock (in block_dirty_folio)
  *                         i_pages lock (widely used)
  *                           lruvec->lru_lock (in folio_lruvec_lock_irq)
  *                     inode->i_lock (in set_page_dirty's __mark_inode_dirty)
Signature · 1 line
2.43.5
Signature · 2 lines
Michal Hocko
SUSE Labs
maintainer Reviewed-by on v1 6/6
#31 lore raw

Re: [PATCH v1 6/6] memcg-v1: remove memcg move locking code

In reply to Shakeel Butt
Quoted 6 lines · Shakeel Butt

The memcg v1's charge move feature has been deprecated. All the places using the memcg move lock, have stopped using it as they don't need the protection any more. Let's proceed to remove all the locking code related to charge moving.

#32 lore raw

Re: [PATCH v1 6/6] memcg-v1: remove memcg move locking code

In reply to Shakeel Butt

include/linux/memcontrol.h static inline struct mem_cgroup *folio_memcg_rcu(struct folio *folio)

Quoted 55 lines · Shakeel Butt

The memcg v1's charge move feature has been deprecated. All the places using the memcg move lock, have stopped using it as they don't need the protection any more. Let's proceed to remove all the locking code related to charge moving.

---

Changes since RFC:
- Remove the memcg move locking in separate patches.

 include/linux/memcontrol.h | 54 -------------------------
 mm/filemap.c               |  1 -
 mm/memcontrol-v1.c         | 82 --------------------------------------
 mm/memcontrol.c            |  5 ---
 mm/rmap.c                  |  1 -
 5 files changed, 143 deletions(-)
include/linux/memcontrol.h +0 -12
index 798db70b0a30..932534291ca2 100644
@@ -299,20 +299,10 @@ struct mem_cgroup {
        /* For oom notifier event fd */
        struct list_head oom_notify;
 
-       /* taken only while moving_account > 0 */
-       spinlock_t move_lock;
-       unsigned long move_lock_flags;
-
        /* Legacy tcp memory accounting */
        bool tcpmem_active;
        int tcpmem_pressure;
 
-       /*
-        * set > 0 if pages under this cgroup are moving to other cgroup.
-        */
-       atomic_t moving_account;
-       struct task_struct *move_lock_task;
-
        /* List of events which userspace want to receive */
        struct list_head event_list;
        spinlock_t event_list_lock;
@@ -428,9 +418,7 @@ static inline struct obj_cgroup *__folio_objcg(struct folio *folio)
  *
  * - the folio lock
  * - LRU isolation
- * - folio_memcg_lock()
  * - exclusive reference
- * - mem_cgroup_trylock_pages()
  *
  * For a kmem folio a caller should hold an rcu read lock to protect memcg
  * associated with a kmem folio from being released.
@@ -499,9 +487,7 @@ static inline struct mem_cgroup *folio_memcg_rcu(struct folio *folio)

I think you missed folio_memcg_rcu().

(I don't think workingset_activation() needs it, since its only caller must hold a refcnt on the folio.)

Quoted 229 lines
include/linux/memcontrol.h +0 -42
  *
  * - the folio lock
  * - LRU isolation
- * - lock_folio_memcg()
  * - exclusive reference
- * - mem_cgroup_trylock_pages()
  *
  * For a kmem folio a caller should hold an rcu read lock to protect memcg
  * associated with a kmem folio from being released.
@@ -1873,26 +1859,6 @@ static inline bool task_in_memcg_oom(struct task_struct *p)
        return p->memcg_in_oom;
 }
 
-void folio_memcg_lock(struct folio *folio);
-void folio_memcg_unlock(struct folio *folio);
-
-/* try to stablize folio_memcg() for all the pages in a memcg */
-static inline bool mem_cgroup_trylock_pages(struct mem_cgroup *memcg)
-{
-       rcu_read_lock();
-
-       if (mem_cgroup_disabled() || !atomic_read(&memcg->moving_account))
-               return true;
-
-       rcu_read_unlock();
-       return false;
-}
-
-static inline void mem_cgroup_unlock_pages(void)
-{
-       rcu_read_unlock();
-}
-
 static inline void mem_cgroup_enter_user_fault(void)
 {
        WARN_ON(current->in_user_fault);
@@ -1914,26 +1880,6 @@ unsigned long memcg1_soft_limit_reclaim(pg_data_t *pgdat, int order,
        return 0;
 }
 
-static inline void folio_memcg_lock(struct folio *folio)
-{
-}
-
-static inline void folio_memcg_unlock(struct folio *folio)
-{
-}
-
-static inline bool mem_cgroup_trylock_pages(struct mem_cgroup *memcg)
-{
-       /* to match folio_memcg_rcu() */
-       rcu_read_lock();
-       return true;
-}
-
-static inline void mem_cgroup_unlock_pages(void)
-{
-       rcu_read_unlock();
-}
-
 static inline bool task_in_memcg_oom(struct task_struct *p)
 {
        return false;
mm/filemap.c +0 -1
index 630a1c431ea1..e582a1545d2a 100644
@@ -119,7 +119,6 @@
  *    ->i_pages lock           (folio_remove_rmap_pte->set_page_dirty)
  *    bdi.wb->list_lock                (folio_remove_rmap_pte->set_page_dirty)
  *    ->inode->i_lock          (folio_remove_rmap_pte->set_page_dirty)
- *    ->memcg->move_lock       (folio_remove_rmap_pte->folio_memcg_lock)
  *    bdi.wb->list_lock                (zap_pte_range->set_page_dirty)
  *    ->inode->i_lock          (zap_pte_range->set_page_dirty)
  *    ->private_lock           (zap_pte_range->block_dirty_folio)
mm/memcontrol-v1.c +0 -82
index 9c0fba8c8a83..539ceefa9d2d 100644
@@ -401,87 +401,6 @@ unsigned long memcg1_soft_limit_reclaim(pg_data_t *pgdat, int order,
        return nr_reclaimed;
 }
 
-/**
- * folio_memcg_lock - Bind a folio to its memcg.
- * @folio: The folio.
- *
- * This function prevents unlocked LRU folios from being moved to
- * another cgroup.
- *
- * It ensures lifetime of the bound memcg.  The caller is responsible
- * for the lifetime of the folio.
- */
-void folio_memcg_lock(struct folio *folio)
-{
-       struct mem_cgroup *memcg;
-       unsigned long flags;
-
-       /*
-        * The RCU lock is held throughout the transaction.  The fast
-        * path can get away without acquiring the memcg->move_lock
-        * because page moving starts with an RCU grace period.
-         */
-       rcu_read_lock();
-
-       if (mem_cgroup_disabled())
-               return;
-again:
-       memcg = folio_memcg(folio);
-       if (unlikely(!memcg))
-               return;
-
-#ifdef CONFIG_PROVE_LOCKING
-       local_irq_save(flags);
-       might_lock(&memcg->move_lock);
-       local_irq_restore(flags);
-#endif
-
-       if (atomic_read(&memcg->moving_account) <= 0)
-               return;
-
-       spin_lock_irqsave(&memcg->move_lock, flags);
-       if (memcg != folio_memcg(folio)) {
-               spin_unlock_irqrestore(&memcg->move_lock, flags);
-               goto again;
-       }
-
-       /*
-        * When charge migration first begins, we can have multiple
-        * critical sections holding the fast-path RCU lock and one
-        * holding the slowpath move_lock. Track the task who has the
-        * move_lock for folio_memcg_unlock().
-        */
-       memcg->move_lock_task = current;
-       memcg->move_lock_flags = flags;
-}
-
-static void __folio_memcg_unlock(struct mem_cgroup *memcg)
-{
-       if (memcg && memcg->move_lock_task == current) {
-               unsigned long flags = memcg->move_lock_flags;
-
-               memcg->move_lock_task = NULL;
-               memcg->move_lock_flags = 0;
-
-               spin_unlock_irqrestore(&memcg->move_lock, flags);
-       }
-
-       rcu_read_unlock();
-}
-
-/**
- * folio_memcg_unlock - Release the binding between a folio and its memcg.
- * @folio: The folio.
- *
- * This releases the binding created by folio_memcg_lock().  This does
- * not change the accounting of this folio to its memcg, but it does
- * permit others to change it.
- */
-void folio_memcg_unlock(struct folio *folio)
-{
-       __folio_memcg_unlock(folio_memcg(folio));
-}
-
 static u64 mem_cgroup_move_charge_read(struct cgroup_subsys_state *css,
                                struct cftype *cft)
 {
@@ -1189,7 +1108,6 @@ void memcg1_memcg_init(struct mem_cgroup *memcg)
 {
        INIT_LIST_HEAD(&memcg->oom_notify);
        mutex_init(&memcg->thresholds_lock);
-       spin_lock_init(&memcg->move_lock);
        INIT_LIST_HEAD(&memcg->event_list);
        spin_lock_init(&memcg->event_list_lock);
 }
mm/memcontrol.c +0 -5
index 94279b9c766a..3c223aaeb6af 100644
@@ -1189,7 +1189,6 @@ void lruvec_memcg_debug(struct lruvec *lruvec, struct folio *folio)
  * These functions are safe to use under any of the following conditions:
  * - folio locked
  * - folio_test_lru false
- * - folio_memcg_lock()
  * - folio frozen (refcount of 0)
  *
  * Return: The lruvec this folio is on with its lock held.
@@ -1211,7 +1210,6 @@ struct lruvec *folio_lruvec_lock(struct folio *folio)
  * These functions are safe to use under any of the following conditions:
  * - folio locked
  * - folio_test_lru false
- * - folio_memcg_lock()
  * - folio frozen (refcount of 0)
  *
  * Return: The lruvec this folio is on with its lock held and interrupts
@@ -1235,7 +1233,6 @@ struct lruvec *folio_lruvec_lock_irq(struct folio *folio)
  * These functions are safe to use under any of the following conditions:
  * - folio locked
  * - folio_test_lru false
- * - folio_memcg_lock()
  * - folio frozen (refcount of 0)
  *
  * Return: The lruvec this folio is on with its lock held and interrupts
@@ -2375,9 +2372,7 @@ static void commit_charge(struct folio *folio, struct mem_cgroup *memcg)
         *
         * - the page lock
         * - LRU isolation
-        * - folio_memcg_lock()
         * - exclusive reference
-        * - mem_cgroup_trylock_pages()
         */
        folio->memcg_data = (unsigned long)memcg;
 }
mm/rmap.c +0 -2
index 4785a693857a..c6c4d4ea29a7 100644
@@ -32,7 +32,6 @@
  *                   swap_lock (in swap_duplicate, swap_info_get)
  *                     mmlist_lock (in mmput, drain_mmlist and others)
  *                     mapping->private_lock (in block_dirty_folio)
- *                       folio_lock_memcg move_lock (in block_dirty_folio)
  *                         i_pages lock (widely used)
  *                           lruvec->lru_lock (in folio_lruvec_lock_irq)
  *                     inode->i_lock (in set_page_dirty's __mark_inode_dirty)
--

2.43.5

author maintainer
#33 lore raw

Re: [PATCH v1 6/6] memcg-v1: remove memcg move locking code

Quoted 59 lines · Yu Zhao

include/linux/memcontrol.h static inline struct mem_cgroup *folio_memcg_rcu(struct folio *folio)

Quoted 55 lines · Shakeel Butt

The memcg v1's charge move feature has been deprecated. All the places using the memcg move lock, have stopped using it as they don't need the protection any more. Let's proceed to remove all the locking code related to charge moving.

---

Changes since RFC:
- Remove the memcg move locking in separate patches.

 include/linux/memcontrol.h | 54 -------------------------
 mm/filemap.c               |  1 -
 mm/memcontrol-v1.c         | 82 --------------------------------------
 mm/memcontrol.c            |  5 ---
 mm/rmap.c                  |  1 -
 5 files changed, 143 deletions(-)
include/linux/memcontrol.h +0 -12
index 798db70b0a30..932534291ca2 100644
@@ -299,20 +299,10 @@ struct mem_cgroup {
        /* For oom notifier event fd */
        struct list_head oom_notify;
 
-       /* taken only while moving_account > 0 */
-       spinlock_t move_lock;
-       unsigned long move_lock_flags;
-
        /* Legacy tcp memory accounting */
        bool tcpmem_active;
        int tcpmem_pressure;
 
-       /*
-        * set > 0 if pages under this cgroup are moving to other cgroup.
-        */
-       atomic_t moving_account;
-       struct task_struct *move_lock_task;
-
        /* List of events which userspace want to receive */
        struct list_head event_list;
        spinlock_t event_list_lock;
@@ -428,9 +418,7 @@ static inline struct obj_cgroup *__folio_objcg(struct folio *folio)
  *
  * - the folio lock
  * - LRU isolation
- * - folio_memcg_lock()
  * - exclusive reference
- * - mem_cgroup_trylock_pages()
  *
  * For a kmem folio a caller should hold an rcu read lock to protect memcg
  * associated with a kmem folio from being released.
@@ -499,9 +487,7 @@ static inline struct mem_cgroup *folio_memcg_rcu(struct folio *folio)

I think you missed folio_memcg_rcu().

Quoted 3 lines

(I don't think workingset_activation() needs it, since its only caller must hold a refcnt on the folio.)

Yes I think so too but I will send a separate followup patch for that.

maintainer Acked-by on v1 6/6
#34 lore raw

Re: [PATCH v1 6/6] memcg-v1: remove memcg move locking code

In reply to Shakeel Butt
Quoted 6 lines · Shakeel Butt

The memcg v1's charge move feature has been deprecated. All the places using the memcg move lock, have stopped using it as they don't need the protection any more. Let's proceed to remove all the locking code related to charge moving.

author maintainer
#35 lore raw
In reply to Shakeel Butt
Quoted 10 lines · Shakeel Butt

The memcg v1's charge moving feature has been deprecated for almost 2 years and the kernel warns if someone try to use it. This warning has been backported to all stable kernel and there have not been any report of the warning or the request to support this feature anymore. Let's proceed to fully deprecate this feature.

Changes since RFC:
- Writing 0 to memory.move_charge_at_immigrate is allowed.
- Remove the memcg move locking in separate patches.

Quoted 8 lines

Shakeel Butt (6):
memcg-v1: fully deprecate move_charge_at_immigrate
memcg-v1: remove charge move code
memcg-v1: no need for memcg locking for dirty tracking
memcg-v1: no need for memcg locking for writeback tracking
memcg-v1: no need for memcg locking for MGLRU
memcg-v1: remove memcg move locking code

Please ignore the patch at [1] as it was resent by mistake.

[1] https://lore.kernel.org/linux-mm/[email protected]