Skip to contentSkip to conversation

ocxl: Fix potential memory leak on context creation

PATCH Superseded Frederic Barrat 3 participants 3 messages May 20, 2019

Subsystems (2)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 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")

---

Changelog:
v2: reset context pointer in case of allocation failure (Andrew)

 drivers/misc/ocxl/context.c | 2 ++
 1 file changed, 2 insertions(+)
drivers/misc/ocxl/context.c +2 -0
index bab9c9364184..24e4fb010275 100644
@@ -22,6 +22,8 @@ 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);
+		*context = NULL;
 		return pasid;
 	}
 	afu->pasid_count++;
Signature · 1 line
2.21.0
Acked-by on v2 1/1
#1 lore raw
Quoted 26 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")

---

Changelog:
v2: reset context pointer in case of allocation failure (Andrew)

  drivers/misc/ocxl/context.c | 2 ++
  1 file changed, 2 insertions(+)
drivers/misc/ocxl/context.c +2 -0
index bab9c9364184..24e4fb010275 100644
@@ -22,6 +22,8 @@ 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);
+		*context = NULL;
  		return pasid;
  	}
  	afu->pasid_count++;
Signature · 2 lines
Andrew Donnellan              OzLabs, ADL Canberra
[email protected]             IBM Australia Limited
Reviewed-by on v2 1/1
#2 lore raw
In reply to Frederic Barrat
Quoted 3 lines · On Mon, 20 May 2019 09:16:18 +0200 Frederic Barrat <[email protected]>…

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

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

Oops... missed that during review :-\

Quoted 6 lines

---

Changelog:
v2: reset context pointer in case of allocation failure (Andrew)

Alternatively you could change the code to do:

	ctx = kzalloc(sizeof(struct ocxl_context), GFP_KERNEL);
	if (!ctx)
		return -ENOMEM;
	.
	.
	.
	if (pasid < 0) {
		mutex_unlock(&afu->contexts_lock);
		kfree(ctx);
		return pasid;
	}
	.
	.
	.
	*context = ctx;
	return 0;
}

This has the advantage of clearing any risk of side-effect with *context forever, which is a safer practice IMHO.

Patch is correct anyway, so:

Quoted 16 lines
 drivers/misc/ocxl/context.c | 2 ++
 1 file changed, 2 insertions(+)
drivers/misc/ocxl/context.c +2 -0
index bab9c9364184..24e4fb010275 100644
@@ -22,6 +22,8 @@ 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);
+		*context = NULL;
 		return pasid;
 	}
 	afu->pasid_count++;