Skip to contentSkip to conversation

ocxl: Fix potential memory leak on context creation

PATCH Superseded Frederic Barrat 2 participants 3 messages May 17, 2019 – May 20, 2019

Subsystems (2)

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 ocxl: Fix potential memory leak on context creation

If we couldn't fully init a context, we were leaking memory.

Fixes b9721d275cc2 ("ocxl: Allow external drivers to use OpenCAPI contexts")
---
 drivers/misc/ocxl/context.c | 1 +
 1 file changed, 1 insertion(+)
drivers/misc/ocxl/context.c +1 -0
index bab9c9364184..ab93156aa83e 100644
@@ -22,6 +22,7 @@ int ocxl_context_alloc(struct ocxl_context **context, struct ocxl_afu *afu,
 			afu->pasid_base + afu->pasid_max, GFP_KERNEL);
 	if (pasid < 0) {
 		mutex_unlock(&afu->contexts_lock);
+		kfree(*context);
 		return pasid;
 	}
 	afu->pasid_count++;
Signature · 1 line
2.21.0
Acked-by on v1 1/1
#1 lore raw
Quoted 4 lines · Frederic Barrat

If we couldn't fully init a context, we were leaking memory.

Fixes b9721d275cc2 ("ocxl: Allow external drivers to use OpenCAPI contexts")

drivers/misc/ocxl/context.c int ocxl_context_alloc(struct ocxl_context **context, struct ocxl_afu *afu, · line 25

Quoted 13 lines
---
  drivers/misc/ocxl/context.c | 1 +
  1 file changed, 1 insertion(+)
drivers/misc/ocxl/context.c +1 -0
index bab9c9364184..ab93156aa83e 100644
@@ -22,6 +22,7 @@ int ocxl_context_alloc(struct ocxl_context **context, struct ocxl_afu *afu,
  			afu->pasid_base + afu->pasid_max, GFP_KERNEL);
  	if (pasid < 0) {
  		mutex_unlock(&afu->contexts_lock);
+		kfree(*context);

(defensive programming: set *context = NULL so that if the caller ignores the return code we get an obvious crash)

Quoted 4 lines
drivers/misc/ocxl/context.c +0 -0
  		return pasid;
  	}
  	afu->pasid_count++;
Signature · 2 lines
Andrew Donnellan              OzLabs, ADL Canberra
[email protected]             IBM Australia Limited
#2 lore raw

Le 20/05/2019 à 03:45, Andrew Donnellan a écrit :

Quoted 12 lines · Frederic Barrat
Quoted 4 lines

If we couldn't fully init a context, we were leaking memory.

Fixes b9721d275cc2 ("ocxl: Allow external drivers to use OpenCAPI contexts")
Quoted 4 lines

---
  drivers/misc/ocxl/context.c | 1 +

  1 file changed, 1 insertion(+)
Quoted 12 lines

drivers/misc/ocxl/context.c int ocxl_context_alloc(struct ocxl_context · line 26

Quoted 10 lines
drivers/misc/ocxl/context.c +1 -0
index bab9c9364184..ab93156aa83e 100644
@@ -22,6 +22,7 @@ int ocxl_context_alloc(struct ocxl_context 
 **context, struct ocxl_afu *afu,
               afu->pasid_base + afu->pasid_max, GFP_KERNEL);
       if (pasid < 0) {
           mutex_unlock(&afu->contexts_lock);
+        kfree(*context);

(defensive programming: set *context = NULL so that if the caller ignores the return code we get an obvious crash)

Good point. v2 on its way

Quoted 5 lines
Quoted 4 lines
drivers/misc/ocxl/context.c +0 -0
           return pasid;
       }

      afu->pasid_count++;