linux-mm.kvack.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v2] list_lru: expand list_lru_add() docs with info about sublists
@ 2024-11-29 14:58 Alice Ryhl
  2024-12-02 19:29 ` Nhat Pham
  2024-12-03  3:40 ` Muchun Song
  0 siblings, 2 replies; 3+ messages in thread
From: Alice Ryhl @ 2024-11-29 14:58 UTC (permalink / raw)
  To: Dave Chinner, Andrew Morton, Matthew Wilcox
  Cc: Johannes Weiner, Nhat Pham, Qi Zheng, Roman Gushchin,
	Muchun Song, Michal Hocko, Shakeel Butt, linux-mm, cgroups,
	linux-kernel, Dave Chinner, Alice Ryhl

The documentation for list_lru_add() and list_lru_del() has not been
updated since lru lists were originally introduced by commit
a38e40824844 ("list: add a new LRU list type"). Back then, list_lru
stored all of the items in a single list, but the implementation has
since been expanded to use many sublists internally.

Thus, update the docs to mention that the requirements about not using
the item with several lists at the same time also applies not using
different sublists. Also mention that list_lru items are reparented when
the memcg is deleted as discussed on the LKML [1].

Also fix incorrect use of 'Return value:' which should be 'Return:'.

Link: https://lore.kernel.org/all/Z0eXrllVhRI9Ag5b@dread.disaster.area/ [1]
Reviewed-by: Dave Chinner <dchinner@redhat.com>
Signed-off-by: Alice Ryhl <aliceryhl@google.com>
---
Changes in v2:
- Don't reword "if the list was updated".
- Update 'Return value:' to 'Return:' on list_lru_*_obj() functions.
- Link to v1: https://lore.kernel.org/r/20241128-list_lru_memcg_docs-v1-1-7e4568978f4e@google.com
---
 include/linux/list_lru.h | 44 +++++++++++++++++++++++++++++++++-----------
 1 file changed, 33 insertions(+), 11 deletions(-)

diff --git a/include/linux/list_lru.h b/include/linux/list_lru.h
index 05c166811f6b..fe739d35a864 100644
--- a/include/linux/list_lru.h
+++ b/include/linux/list_lru.h
@@ -91,13 +91,24 @@ void memcg_reparent_list_lrus(struct mem_cgroup *memcg, struct mem_cgroup *paren
  * @memcg: the cgroup of the sublist to add the item to.
  *
  * If the element is already part of a list, this function returns doing
- * nothing. Therefore the caller does not need to keep state about whether or
- * not the element already belongs in the list and is allowed to lazy update
- * it. Note however that this is valid for *a* list, not *this* list. If
- * the caller organize itself in a way that elements can be in more than
- * one type of list, it is up to the caller to fully remove the item from
- * the previous list (with list_lru_del() for instance) before moving it
- * to @lru.
+ * nothing. This means that it is not necessary to keep state about whether or
+ * not the element already belongs in the list. That said, this logic only
+ * works if the item is in *this* list. If the item might be in some other
+ * list, then you cannot rely on this check and you must remove it from the
+ * other list before trying to insert it.
+ *
+ * The lru list consists of many sublists internally; the @nid and @memcg
+ * parameters are used to determine which sublist to insert the item into.
+ * It's important to use the right value of @nid and @memcg when deleting the
+ * item, since it might otherwise get deleted from the wrong sublist.
+ *
+ * This also applies when attempting to insert the item multiple times - if
+ * the item is currently in one sublist and you call list_lru_add() again, you
+ * must pass the right @nid and @memcg parameters so that the same sublist is
+ * used.
+ *
+ * You must ensure that the memcg is not freed during this call (e.g., with
+ * rcu or by taking a css refcnt).
  *
  * Return: true if the list was updated, false otherwise
  */
@@ -113,7 +124,7 @@ bool list_lru_add(struct list_lru *lru, struct list_head *item, int nid,
  * memcg of the sublist is determined by @item list_head. This assumption is
  * valid for slab objects LRU such as dentries, inodes, etc.
  *
- * Return value: true if the list was updated, false otherwise
+ * Return: true if the list was updated, false otherwise
  */
 bool list_lru_add_obj(struct list_lru *lru, struct list_head *item);
 
@@ -125,8 +136,19 @@ bool list_lru_add_obj(struct list_lru *lru, struct list_head *item);
  * @memcg: the cgroup of the sublist to delete the item from.
  *
  * This function works analogously as list_lru_add() in terms of list
- * manipulation. The comments about an element already pertaining to
- * a list are also valid for list_lru_del().
+ * manipulation.
+ *
+ * The comments in list_lru_add() about an element already being in a list are
+ * also valid for list_lru_del(), that is, you can delete an item that has
+ * already been removed or never been added. However, if the item is in a
+ * list, it must be in *this* list, and you must pass the right value of @nid
+ * and @memcg so that the right sublist is used.
+ *
+ * You must ensure that the memcg is not freed during this call (e.g., with
+ * rcu or by taking a css refcnt). When a memcg is deleted, list_lru entries
+ * are automatically moved to the parent memcg. This is done in a race-free
+ * way, so during deletion of an memcg both the old and new memcg will resolve
+ * to the same sublist internally.
  *
  * Return: true if the list was updated, false otherwise
  */
@@ -142,7 +164,7 @@ bool list_lru_del(struct list_lru *lru, struct list_head *item, int nid,
  * memcg of the sublist is determined by @item list_head. This assumption is
  * valid for slab objects LRU such as dentries, inodes, etc.
  *
- * Return value: true if the list was updated, false otherwise.
+ * Return: true if the list was updated, false otherwise.
  */
 bool list_lru_del_obj(struct list_lru *lru, struct list_head *item);
 

---
base-commit: 7af08b57bcb9ebf78675c50069c54125c0a8b795
change-id: 20241128-list_lru_memcg_docs-d736e1d81a7f

Best regards,
-- 
Alice Ryhl <aliceryhl@google.com>



^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH v2] list_lru: expand list_lru_add() docs with info about sublists
  2024-11-29 14:58 [PATCH v2] list_lru: expand list_lru_add() docs with info about sublists Alice Ryhl
@ 2024-12-02 19:29 ` Nhat Pham
  2024-12-03  3:40 ` Muchun Song
  1 sibling, 0 replies; 3+ messages in thread
From: Nhat Pham @ 2024-12-02 19:29 UTC (permalink / raw)
  To: Alice Ryhl
  Cc: Dave Chinner, Andrew Morton, Matthew Wilcox, Johannes Weiner,
	Qi Zheng, Roman Gushchin, Muchun Song, Michal Hocko,
	Shakeel Butt, linux-mm, cgroups, linux-kernel, Dave Chinner

On Fri, Nov 29, 2024 at 6:58 AM Alice Ryhl <aliceryhl@google.com> wrote:
>
> The documentation for list_lru_add() and list_lru_del() has not been
> updated since lru lists were originally introduced by commit
> a38e40824844 ("list: add a new LRU list type"). Back then, list_lru
> stored all of the items in a single list, but the implementation has
> since been expanded to use many sublists internally.
>
> Thus, update the docs to mention that the requirements about not using
> the item with several lists at the same time also applies not using
> different sublists. Also mention that list_lru items are reparented when
> the memcg is deleted as discussed on the LKML [1].
>
> Also fix incorrect use of 'Return value:' which should be 'Return:'.
>
> Link: https://lore.kernel.org/all/Z0eXrllVhRI9Ag5b@dread.disaster.area/ [1]
> Reviewed-by: Dave Chinner <dchinner@redhat.com>
> Signed-off-by: Alice Ryhl <aliceryhl@google.com>

LGTM.
Reviewed-by: Nhat Pham <nphamcs@gmail.com>


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH v2] list_lru: expand list_lru_add() docs with info about sublists
  2024-11-29 14:58 [PATCH v2] list_lru: expand list_lru_add() docs with info about sublists Alice Ryhl
  2024-12-02 19:29 ` Nhat Pham
@ 2024-12-03  3:40 ` Muchun Song
  1 sibling, 0 replies; 3+ messages in thread
From: Muchun Song @ 2024-12-03  3:40 UTC (permalink / raw)
  To: Alice Ryhl
  Cc: Dave Chinner, Andrew Morton, Matthew Wilcox, Johannes Weiner,
	Nhat Pham, Qi Zheng, Roman Gushchin, Michal Hocko, Shakeel Butt,
	linux-mm, cgroups, linux-kernel, Dave Chinner



> On Nov 29, 2024, at 22:58, Alice Ryhl <aliceryhl@google.com> wrote:
> 
> The documentation for list_lru_add() and list_lru_del() has not been
> updated since lru lists were originally introduced by commit
> a38e40824844 ("list: add a new LRU list type"). Back then, list_lru
> stored all of the items in a single list, but the implementation has
> since been expanded to use many sublists internally.
> 
> Thus, update the docs to mention that the requirements about not using
> the item with several lists at the same time also applies not using
> different sublists. Also mention that list_lru items are reparented when
> the memcg is deleted as discussed on the LKML [1].
> 
> Also fix incorrect use of 'Return value:' which should be 'Return:'.
> 
> Link: https://lore.kernel.org/all/Z0eXrllVhRI9Ag5b@dread.disaster.area/ [1]
> Reviewed-by: Dave Chinner <dchinner@redhat.com>
> Signed-off-by: Alice Ryhl <aliceryhl@google.com>

Acked-by: Muchun Song <muchun.song@linux.dev>

Thanks.



^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2024-12-03  3:40 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-11-29 14:58 [PATCH v2] list_lru: expand list_lru_add() docs with info about sublists Alice Ryhl
2024-12-02 19:29 ` Nhat Pham
2024-12-03  3:40 ` Muchun Song

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox