diff mbox series

list_lru: expand list_lru_add() docs with info about sublists

Message ID 20241128-list_lru_memcg_docs-v1-1-7e4568978f4e@google.com (mailing list archive)
State New
Headers show
Series list_lru: expand list_lru_add() docs with info about sublists | expand

Commit Message

Alice Ryhl Nov. 28, 2024, 12:12 p.m. UTC
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].

Link: https://lore.kernel.org/all/Z0eXrllVhRI9Ag5b@dread.disaster.area/ [1]
Signed-off-by: Alice Ryhl <aliceryhl@google.com>
---
 include/linux/list_lru.h | 48 +++++++++++++++++++++++++++++++++++-------------
 1 file changed, 35 insertions(+), 13 deletions(-)


---
base-commit: b86545e02e8c22fb89218f29d381fa8e8b91d815
change-id: 20241128-list_lru_memcg_docs-d736e1d81a7f

Best regards,
diff mbox series

Patch

diff --git a/include/linux/list_lru.h b/include/linux/list_lru.h
index 05c166811f6b..d4fc967247ae 100644
--- a/include/linux/list_lru.h
+++ b/include/linux/list_lru.h
@@ -91,15 +91,26 @@  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.
  *
- * Return: true if the list was updated, false otherwise
+ * 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 value: true if the item was added, false otherwise
  */
 bool list_lru_add(struct list_lru *lru, struct list_head *item, int nid,
 		    struct mem_cgroup *memcg);
@@ -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 value: true if the item was added, false otherwise
  */
 bool list_lru_add_obj(struct list_lru *lru, struct list_head *item);
 
@@ -125,10 +136,21 @@  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
+ * Return value: true if the item was removed, false otherwise
  */
 bool list_lru_del(struct list_lru *lru, struct list_head *item, int nid,
 		    struct mem_cgroup *memcg);
@@ -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 value: true if the item was removed, false otherwise.
  */
 bool list_lru_del_obj(struct list_lru *lru, struct list_head *item);