linux-mm.kvack.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] mm: list_lru: Fix NULL pointer dereference in list_lru_add()
@ 2024-07-12  3:25 Youling Tang
  2024-07-12  4:07 ` Kent Overstreet
  0 siblings, 1 reply; 9+ messages in thread
From: Youling Tang @ 2024-07-12  3:25 UTC (permalink / raw)
  To: Andrew Morton; +Cc: Kent Overstreet, linux-mm, linux-kernel, Youling Tang

From: Youling Tang <tangyouling@kylinos.cn>

Note that list_lru_from_memcg_idx() may return NULL, so it is necessary
to error handle the return value to avoid triggering NULL pointer
dereference BUG.

The issue was triggered for discussion [1],
Link [1]: https://lore.kernel.org/linux-bcachefs/84de6cb1-57bd-42f7-8029-4203820ef0b4@linux.dev/T/#m901bb26cdb1d9d4bacebf0d034f0a5a712cc93a6

Signed-off-by: Youling Tang <tangyouling@kylinos.cn>
---
 mm/list_lru.c | 4 ++++
 1 file changed, 4 insertions(+)

diff --git a/mm/list_lru.c b/mm/list_lru.c
index 3fd64736bc45..ee7424c3879d 100644
--- a/mm/list_lru.c
+++ b/mm/list_lru.c
@@ -94,6 +94,9 @@ bool list_lru_add(struct list_lru *lru, struct list_head *item, int nid,
 	spin_lock(&nlru->lock);
 	if (list_empty(item)) {
 		l = list_lru_from_memcg_idx(lru, nid, memcg_kmem_id(memcg));
+		if (!l)
+			goto out;
+
 		list_add_tail(item, &l->list);
 		/* Set shrinker bit if the first element was added */
 		if (!l->nr_items++)
@@ -102,6 +105,7 @@ bool list_lru_add(struct list_lru *lru, struct list_head *item, int nid,
 		spin_unlock(&nlru->lock);
 		return true;
 	}
+out:
 	spin_unlock(&nlru->lock);
 	return false;
 }
-- 
2.34.1



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

end of thread, other threads:[~2024-07-17  2:37 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-07-12  3:25 [PATCH] mm: list_lru: Fix NULL pointer dereference in list_lru_add() Youling Tang
2024-07-12  4:07 ` Kent Overstreet
2024-07-12  4:28   ` Youling Tang
2024-07-12 15:49     ` Kent Overstreet
2024-07-16  2:28       ` Youling Tang
2024-07-16  2:30         ` Kent Overstreet
2024-07-15  3:27   ` Qi Zheng
2024-07-17  2:25     ` Youling Tang
2024-07-17  2:37       ` Qi Zheng

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).