Skip to contentSkip to conversation

lib/btree: Fix possible NULL pointer dereference

PATCH In review Denis Kirjanov <[email protected] 3 participants 3 messages May 12, 2010 – May 14, 2010 Library code

lore
Share & tools
lore
#0 lore raw

PATCH 2/2 lib/btree: Fix possible NULL pointer dereference

mempool_alloc can return null in atomic case.

---

lib/btree.c +2 -1
index 41859a8..542c904 100644
@@ -95,7 +94,8 @@ static unsigned long *btree_node_alloc(struct btree_head *head, gfp_t gfp)
 	unsigned long *node;
 
 	node = mempool_alloc(head->mempool, gfp);
-	memset(node, 0, NODESIZE);
+	if (likely(node))
+		memset(node, 0, NODESIZE);
 	return node;
 }
 
maintainer
#1 lore raw
Quoted 9 lines · On Thu, 13 May 2010 01:20:27 +0400 "Denis Kirjanov <[email protected]"…

mempool_alloc can return null in atomic case.

---

lib/btree.c +0 -0
index 41859a8..542c904 100644

lib/btree.c static unsigned long *btree_node_alloc(struct btree_head *head, gfp_t gfp) · line 100

Quoted 8 lines
lib/btree.c +2 -1
@@ -95,7 +94,8 @@ static unsigned long *btree_node_alloc(struct btree_head *head, gfp_t gfp)
 	unsigned long *node;
 
 	node = mempool_alloc(head->mempool, gfp);
-	memset(node, 0, NODESIZE);
+	if (likely(node))
+		memset(node, 0, NODESIZE);
 	return node;
 }

hm, why is btree.c using mempools? mempools are only appropriate when it is known that objects will become available if the allocating task simply waits for a while. Typically, things like BIOs and request-structs. Simply waiting for the disk to complete some IO will cause some objects to be returned to the mempool.

If waiting-and-doing-nothing fails to cause objects to be returned to the pool then the mempool code can lock up.

#2 lore raw
Quoted 14 lines · Andrew Morton
Quoted 11 lines · On Thu, 13 May 2010 01:20:27 +0400 "Denis Kirjanov <[email protected]"…

mempool_alloc can return null in atomic case.

---

lib/btree.c +0 -0
index 41859a8..542c904 100644
@@ -95,7 +94,8 @@ static unsigned long *btree_node_alloc(struct btree_head *head, gfp_t gfp)
 	unsigned long *node;
 
Quoted 12 lines

lib/btree.c static unsigned long *btree_node_alloc(struct btree_head *head, gfp_t gfp) · line 100

Quoted 6 lines
lib/btree.c +2 -1
 	node = mempool_alloc(head->mempool, gfp);
-	memset(node, 0, NODESIZE);
+	if (likely(node))
+		memset(node, 0, NODESIZE);
 	return node;
 }

hm, why is btree.c using mempools? mempools are only appropriate when it is known that objects will become available if the allocating task simply waits for a while. Typically, things like BIOs and request-structs. Simply waiting for the disk to complete some IO will cause some objects to be returned to the mempool.

For the current caller (logfs), that is a fairly accurate description.

Quoted 2 lines

If waiting-and-doing-nothing fails to cause objects to be returned to the pool then the mempool code can lock up.

True. And I am not 100% sure logfs is bug-free in that respect. One item on my todo list is to add some sort of mempool_prefill() that either ensures pool->curr_nr == pool->min_nr or returns -ENOMEM. That would allow logfs start some writeback and wait for the flash, when necessary.

Jörn

Signature · 3 lines
When in doubt, punt.  When somebody actually complains, go back and fix it...
The 90% solution is a good thing.
-- Rob Landley