Skip to contentSkip to conversation

workqueue: Fix double kfree for rescuer

PATCH In review [email protected] 3 participants 7 messages May 25, 2020 – May 26, 2020

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH v3 1/1 workqueue: Fix double kfree for rescuer

From: Zhang Qiang <[email protected]>

The callback function "rcu_free_wq" could be called after memory was released for "rescuer" already, Thus delete a misplaced call of the function "kfree".

Fixes 6ba94429c8e7 ("workqueue: Reorder sysfs code")

---
v1->v2->v3:
Only commit information modification.

 kernel/workqueue.c | 1 -
 1 file changed, 1 deletion(-)
kernel/workqueue.c +0 -1
index 891ccad5f271..a2451cdcd503 100644
@@ -3491,7 +3491,6 @@ static void rcu_free_wq(struct rcu_head *rcu)
 	else
 		free_workqueue_attrs(wq->unbound_attrs);
 
-	kfree(wq->rescuer);
 	kfree(wq);
 }
 
Signature · 1 line
2.24.1
#1 lore raw
Quoted 3 lines

The callback function "rcu_free_wq" could be called after memory was released for "rescuer" already, Thus delete a misplaced call of the function "kfree".

I got into the mood to follow your interpretation of the software situation for a moment.

I have taken another look also at the implementation of the function “destroy_workqueue”.

* The function call “kfree(rescuer)” can be performed there in an if branch after the statement “wq->rescuer = NULL” was executed.

* This data processing is independent from a possible call of the function “call_rcu(&wq->rcu, rcu_free_wq)” in another if branch. Thus it seems that a null pointer is intentionally passed by a data structure member to this callback function on demand.
The corresponding call of the function “kfree” can tolerate this special case.

Now I find that the proposed change can be inappropriate. I'm sorry for undesirable confusion because of this patch review.

Regards,
Markus

#2 lore raw
In reply to [email protected]
Quoted 3 lines

The callback function "rcu_free_wq" could be called after memory was released for "rescuer" already, Thus delete a misplaced call of the function "kfree".

I got into the mood to follow your interpretation of the software situation for a moment.

I have taken another look also at the implementation of the function “destroy_workqueue”.

* The function call “kfree(rescuer)” can be performed there in an if branch after the statement “wq->rescuer = NULL” was executed.

* This data processing is independent from a possible call of the function “call_rcu(&wq->rcu, rcu_free_wq)” in another if branch. Thus it seems that a null pointer is intentionally passed by a data structure member to this callback function on demand.
The corresponding call of the function “kfree” can tolerate this special case.

Now I find that the proposed change can be inappropriate. I'm sorry for undesirable confusion because of this patch review.

Regards,
Markus

reviewer
#3 lore raw
In reply to [email protected]
Quoted 6 lines · On Mon, May 25, 2020 at 5:22 PM <[email protected]> wrote:

From: Zhang Qiang <[email protected]>

The callback function "rcu_free_wq" could be called after memory was released for "rescuer" already, Thus delete a misplaced call of the function "kfree".

Hello

wq->rescuer is guaranteed to be NULL in rcu_free_wq()
since def98c84b6cd
("workqueue: Fix spurious sanity check failures in destroy_workqueue()")

And the resucer is already free in destroy_workqueue() since 8efe1223d73c
("workqueue: Fix missing kfree(rescuer) in destroy_workqueue()")

The patch is a cleanup to remove a "kfree(NULL);". But the changelog is misleading.

Quoted 2 lines
Fixes 6ba94429c8e7 ("workqueue: Reorder sysfs code")

It is totally unrelated.

Quoted 22 lines

---
v1->v2->v3:
Only commit information modification.

 kernel/workqueue.c | 1 -
 1 file changed, 1 deletion(-)
kernel/workqueue.c +0 -2
index 891ccad5f271..a2451cdcd503 100644
@@ -3491,7 +3491,6 @@ static void rcu_free_wq(struct rcu_head *rcu)
        else
                free_workqueue_attrs(wq->unbound_attrs);
 
-       kfree(wq->rescuer);
        kfree(wq);
 }
 
--

2.24.1

#4 lore raw
Quoted 1 line

wq->rescuer is guaranteed to be NULL in rcu_free_wq()

I was unsure about this data processing detail.

Quoted 1 line

The patch is a cleanup to remove a "kfree(NULL);".

I would prefer also an improved commit message according to the understanding of the software situation in this direction.

Regards,
Markus

#5 lore raw
In reply to Lai Jiangshan

Thank you reply
There is something wrong with my description. is it feasible to describe as follows:

The resucer is already free in "destroy_workqueue" and
"wq->rescuer = NULL" was executed, but in "rcu_free_wq" it's release again (equivalent to kfree(NULL)), this is unnecessary, so should remove.

Quoted 48 lines · Lai Jiangshan
Quoted 6 lines · On Mon, May 25, 2020 at 5:22 PM <[email protected]> wrote:

From: Zhang Qiang <[email protected]>

The callback function "rcu_free_wq" could be called after memory was released for "rescuer" already, Thus delete a misplaced call of the function "kfree".

Hello

wq->rescuer is guaranteed to be NULL in rcu_free_wq()
since def98c84b6cd
("workqueue: Fix spurious sanity check failures in destroy_workqueue()")

And the resucer is already free in destroy_workqueue() since 8efe1223d73c
("workqueue: Fix missing kfree(rescuer) in destroy_workqueue()")

The patch is a cleanup to remove a "kfree(NULL);". But the changelog is misleading.

Quoted 2 lines
Fixes 6ba94429c8e7 ("workqueue: Reorder sysfs code")

It is totally unrelated.

Quoted 22 lines

---
v1->v2->v3:
Only commit information modification.

  kernel/workqueue.c | 1 -
  1 file changed, 1 deletion(-)
kernel/workqueue.c +0 -2
index 891ccad5f271..a2451cdcd503 100644
@@ -3491,7 +3491,6 @@ static void rcu_free_wq(struct rcu_head *rcu)
         else
                 free_workqueue_attrs(wq->unbound_attrs);
 
-       kfree(wq->rescuer);
         kfree(wq);
  }
 
--

2.24.1

#6 lore raw
Quoted 6 lines

There is something wrong with my description. is it feasible to describe as follows:

The resucer is already free in "destroy_workqueue" and
"wq->rescuer = NULL" was executed, but in "rcu_free_wq" it's release again (equivalent to kfree(NULL)), this is unnecessary, so should remove.

I find that this suggestion indicates further wording challenges. Please try another variant.

Regards,
Markus