Skip to contentSkip to conversation

xdp: avoid calling kfree twice

PATCH Superseded Zhu Yanjun 2 participants 2 messages Dec 7, 2020

Subsystems (3)

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Zhu Yanjun

author
#0 lore raw

PATCH 1/1 xdp: avoid calling kfree twice

From: Zhu Yanjun <[email protected]>

In the function xdp_umem_pin_pages, if npgs != umem->npgs and npgs >= 0, the function xdp_umem_unpin_pages is called. In this function, kfree is called to handle umem->pgs, and then in the function xdp_umem_pin_pages, kfree is called again to handle umem->pgs. Eventually, umem->pgs is freed twice.

---
 net/xdp/xdp_umem.c | 17 +++++------------
 1 file changed, 5 insertions(+), 12 deletions(-)
net/xdp/xdp_umem.c +5 -12
index 56a28a686988..ff5173f72920 100644
@@ -97,7 +97,6 @@ static int xdp_umem_pin_pages(struct xdp_umem *umem, unsigned long address)
 {
 	unsigned int gup_flags = FOLL_WRITE;
 	long npgs;
-	int err;
 
 	umem->pgs = kcalloc(umem->npgs, sizeof(*umem->pgs),
 			    GFP_KERNEL | __GFP_NOWARN);
@@ -112,20 +111,14 @@ static int xdp_umem_pin_pages(struct xdp_umem *umem, unsigned long address)
 	if (npgs != umem->npgs) {
 		if (npgs >= 0) {
 			umem->npgs = npgs;
-			err = -ENOMEM;
-			goto out_pin;
+			xdp_umem_unpin_pages(umem);
+			return -ENOMEM;
 		}
-		err = npgs;
-		goto out_pgs;
+		kfree(umem->pgs);
+		umem->pgs = NULL;
+		return npgs;
 	}
 	return 0;
-
-out_pin:
-	xdp_umem_unpin_pages(umem);
-out_pgs:
-	kfree(umem->pgs);
-	umem->pgs = NULL;
-	return err;
 }
 
 static int xdp_umem_account_pages(struct xdp_umem *umem)
Signature · 1 line
2.18.4
Acked-by on v1 1/1
#1 lore raw
Quoted 8 lines · Zhu Yanjun

From: Zhu Yanjun <[email protected]>

In the function xdp_umem_pin_pages, if npgs != umem->npgs and npgs >= 0, the function xdp_umem_unpin_pages is called. In this function, kfree is called to handle umem->pgs, and then in the function xdp_umem_pin_pages, kfree is called again to handle umem->pgs. Eventually, umem->pgs is freed twice.

Hi Zhu,

Thanks for the cleanup! kfree(NULL) is valid, so this is not a double-free, but still a nice cleanup!

Quoted 21 lines
---
  net/xdp/xdp_umem.c | 17 +++++------------
  1 file changed, 5 insertions(+), 12 deletions(-)
net/xdp/xdp_umem.c +0 -1
index 56a28a686988..ff5173f72920 100644
@@ -97,7 +97,6 @@ static int xdp_umem_pin_pages(struct xdp_umem *umem, unsigned long address)
  {
  	unsigned int gup_flags = FOLL_WRITE;
  	long npgs;
-	int err;
 
  	umem->pgs = kcalloc(umem->npgs, sizeof(*umem->pgs),
  			    GFP_KERNEL | __GFP_NOWARN);
@@ -112,20 +111,14 @@ static int xdp_umem_pin_pages(struct xdp_umem *umem, unsigned long address)
  	if (npgs != umem->npgs) {
  		if (npgs >= 0) {
  			umem->npgs = npgs;

net/xdp/xdp_umem.c static int xdp_umem_pin_pages(struct xdp_umem *umem, unsigned long address) · line 119

Quoted 10 lines
net/xdp/xdp_umem.c +5 -4
-			err = -ENOMEM;
-			goto out_pin;
+			xdp_umem_unpin_pages(umem);
+			return -ENOMEM;
  		}
-		err = npgs;
-		goto out_pgs;
+		kfree(umem->pgs);
+		umem->pgs = NULL;
+		return npgs;

I'd like an explicit cast "(int)" here (-Wconversion). Please spin a v2 with the cast, with my:

added.

Cheers!
Björn

Quoted 13 lines
net/xdp/xdp_umem.c +0 -7
  	}
  	return 0;
-
-out_pin:
-	xdp_umem_unpin_pages(umem);
-out_pgs:
-	kfree(umem->pgs);
-	umem->pgs = NULL;
-	return err;
  }
 
  static int xdp_umem_account_pages(struct xdp_umem *umem)