[PATCH] mm: list_lru: Fix NULL pointer dereference in list_lru_add()
From: Youling Tang
Date: Thu Jul 11 2024 - 23:26:48 EST
From: Youling Tang <tangyouling@xxxxxxxxxx>
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@xxxxxxxxx/T/#m901bb26cdb1d9d4bacebf0d034f0a5a712cc93a6
Signed-off-by: Youling Tang <tangyouling@xxxxxxxxxx>
---
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