public inbox for cgroups@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH] cgroup/cpuset: Fix a memory leak in update_exclusive_cpumask()
@ 2024-02-28  0:58 Waiman Long
  2024-02-28 13:56 ` Mirsad Todorovac
  2024-02-28 18:04 ` Tejun Heo
  0 siblings, 2 replies; 4+ messages in thread
From: Waiman Long @ 2024-02-28  0:58 UTC (permalink / raw)
  To: Tejun Heo, Zefan Li, Johannes Weiner
  Cc: cgroups, linux-kernel, Mirsad Todorovac, Waiman Long

Fix a possible memory leak in update_exclusive_cpumask() by moving the
alloc_cpumasks() down after the validate_change() check which can fail
and still before the temporary cpumasks are needed.

Fixes: e2ffe502ba45 ("cgroup/cpuset: Add cpuset.cpus.exclusive for v2")
Reported-by: Mirsad Todorovac <mirsad.todorovac@alu.hr>
Closes: https://lore.kernel.org/lkml/14915689-27a3-4cd8-80d2-9c30d0c768b6@alu.unizg.hr
Signed-off-by: Waiman Long <longman@redhat.com>
---
 kernel/cgroup/cpuset.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/kernel/cgroup/cpuset.c b/kernel/cgroup/cpuset.c
index ba36c073304a..7260f095802a 100644
--- a/kernel/cgroup/cpuset.c
+++ b/kernel/cgroup/cpuset.c
@@ -2598,9 +2598,6 @@ static int update_exclusive_cpumask(struct cpuset *cs, struct cpuset *trialcs,
 	if (cpumask_equal(cs->exclusive_cpus, trialcs->exclusive_cpus))
 		return 0;
 
-	if (alloc_cpumasks(NULL, &tmp))
-		return -ENOMEM;
-
 	if (*buf)
 		compute_effective_exclusive_cpumask(trialcs, NULL);
 
@@ -2615,6 +2612,9 @@ static int update_exclusive_cpumask(struct cpuset *cs, struct cpuset *trialcs,
 	if (retval)
 		return retval;
 
+	if (alloc_cpumasks(NULL, &tmp))
+		return -ENOMEM;
+
 	if (old_prs) {
 		if (cpumask_empty(trialcs->effective_xcpus)) {
 			invalidate = true;
-- 
2.39.3


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH] cgroup/cpuset: Fix a memory leak in update_exclusive_cpumask()
  2024-02-28  0:58 [PATCH] cgroup/cpuset: Fix a memory leak in update_exclusive_cpumask() Waiman Long
@ 2024-02-28 13:56 ` Mirsad Todorovac
  2024-02-28 15:08   ` Waiman Long
  2024-02-28 18:04 ` Tejun Heo
  1 sibling, 1 reply; 4+ messages in thread
From: Mirsad Todorovac @ 2024-02-28 13:56 UTC (permalink / raw)
  To: Waiman Long, Tejun Heo, Zefan Li, Johannes Weiner
  Cc: cgroups, linux-kernel, Mirsad Todorovac

Hi,

On 2/28/2024 1:58 AM, Waiman Long wrote:
> Fix a possible memory leak in update_exclusive_cpumask() by moving the
> alloc_cpumasks() down after the validate_change() check which can fail
> and still before the temporary cpumasks are needed.
>
> Fixes: e2ffe502ba45 ("cgroup/cpuset: Add cpuset.cpus.exclusive for v2")
> Reported-by: Mirsad Todorovac <mirsad.todorovac@alu.hr>
> Closes: https://lore.kernel.org/lkml/14915689-27a3-4cd8-80d2-9c30d0c768b6@alu.unizg.hr
> Signed-off-by: Waiman Long <longman@redhat.com>
> ---
>   kernel/cgroup/cpuset.c | 6 +++---
>   1 file changed, 3 insertions(+), 3 deletions(-)
>
> diff --git a/kernel/cgroup/cpuset.c b/kernel/cgroup/cpuset.c
> index ba36c073304a..7260f095802a 100644
> --- a/kernel/cgroup/cpuset.c
> +++ b/kernel/cgroup/cpuset.c
> @@ -2598,9 +2598,6 @@ static int update_exclusive_cpumask(struct cpuset *cs, struct cpuset *trialcs,
>   	if (cpumask_equal(cs->exclusive_cpus, trialcs->exclusive_cpus))
>   		return 0;
>   
> -	if (alloc_cpumasks(NULL, &tmp))
> -		return -ENOMEM;
> -
>   	if (*buf)
>   		compute_effective_exclusive_cpumask(trialcs, NULL);
>   
> @@ -2615,6 +2612,9 @@ static int update_exclusive_cpumask(struct cpuset *cs, struct cpuset *trialcs,
>   	if (retval)
>   		return retval;
>   
> +	if (alloc_cpumasks(NULL, &tmp))
> +		return -ENOMEM;
> +
>   	if (old_prs) {
>   		if (cpumask_empty(trialcs->effective_xcpus)) {
>   			invalidate = true;Great work at such a short notice! (I failed to find the culprit myself.)

The leak is no longer present.

Please feel free to add:

Tested-by: Mirsad Todorovac <mtodorov@alu.unizg.hr>

Best regards, Mirsad Todorovac

P.S. APologies for the previous email accidentally sent as HTML.


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] cgroup/cpuset: Fix a memory leak in update_exclusive_cpumask()
  2024-02-28 13:56 ` Mirsad Todorovac
@ 2024-02-28 15:08   ` Waiman Long
  0 siblings, 0 replies; 4+ messages in thread
From: Waiman Long @ 2024-02-28 15:08 UTC (permalink / raw)
  To: Mirsad Todorovac, Tejun Heo, Zefan Li, Johannes Weiner
  Cc: cgroups, linux-kernel


On 2/28/24 08:56, Mirsad Todorovac wrote:
> Hi,
>
> On 2/28/2024 1:58 AM, Waiman Long wrote:
>> Fix a possible memory leak in update_exclusive_cpumask() by moving the
>> alloc_cpumasks() down after the validate_change() check which can fail
>> and still before the temporary cpumasks are needed.
>>
>> Fixes: e2ffe502ba45 ("cgroup/cpuset: Add cpuset.cpus.exclusive for v2")
>> Reported-by: Mirsad Todorovac <mirsad.todorovac@alu.hr>
>> Closes: 
>> https://lore.kernel.org/lkml/14915689-27a3-4cd8-80d2-9c30d0c768b6@alu.unizg.hr
>> Signed-off-by: Waiman Long <longman@redhat.com>
>> ---
>>   kernel/cgroup/cpuset.c | 6 +++---
>>   1 file changed, 3 insertions(+), 3 deletions(-)
>>
>> diff --git a/kernel/cgroup/cpuset.c b/kernel/cgroup/cpuset.c
>> index ba36c073304a..7260f095802a 100644
>> --- a/kernel/cgroup/cpuset.c
>> +++ b/kernel/cgroup/cpuset.c
>> @@ -2598,9 +2598,6 @@ static int update_exclusive_cpumask(struct 
>> cpuset *cs, struct cpuset *trialcs,
>>       if (cpumask_equal(cs->exclusive_cpus, trialcs->exclusive_cpus))
>>           return 0;
>>   -    if (alloc_cpumasks(NULL, &tmp))
>> -        return -ENOMEM;
>> -
>>       if (*buf)
>>           compute_effective_exclusive_cpumask(trialcs, NULL);
>>   @@ -2615,6 +2612,9 @@ static int update_exclusive_cpumask(struct 
>> cpuset *cs, struct cpuset *trialcs,
>>       if (retval)
>>           return retval;
>>   +    if (alloc_cpumasks(NULL, &tmp))
>> +        return -ENOMEM;
>> +
>>       if (old_prs) {
>>           if (cpumask_empty(trialcs->effective_xcpus)) {
>>               invalidate = true;Great work at such a short notice! (I 
>> failed to find the culprit myself.)
>
> The leak is no longer present.
>
> Please feel free to add:
>
> Tested-by: Mirsad Todorovac <mtodorov@alu.unizg.hr>
>
> Best regards, Mirsad Todorovac
>
> P.S. APologies for the previous email accidentally sent as HTML.

Thanks for verifying the fix.

Cheers,
Longman


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] cgroup/cpuset: Fix a memory leak in update_exclusive_cpumask()
  2024-02-28  0:58 [PATCH] cgroup/cpuset: Fix a memory leak in update_exclusive_cpumask() Waiman Long
  2024-02-28 13:56 ` Mirsad Todorovac
@ 2024-02-28 18:04 ` Tejun Heo
  1 sibling, 0 replies; 4+ messages in thread
From: Tejun Heo @ 2024-02-28 18:04 UTC (permalink / raw)
  To: Waiman Long
  Cc: Zefan Li, Johannes Weiner, cgroups, linux-kernel,
	Mirsad Todorovac

Hello,

On Tue, Feb 27, 2024 at 07:58:01PM -0500, Waiman Long wrote:
> Fix a possible memory leak in update_exclusive_cpumask() by moving the
> alloc_cpumasks() down after the validate_change() check which can fail
> and still before the temporary cpumasks are needed.
> 
> Fixes: e2ffe502ba45 ("cgroup/cpuset: Add cpuset.cpus.exclusive for v2")
> Reported-by: Mirsad Todorovac <mirsad.todorovac@alu.hr>
> Closes: https://lore.kernel.org/lkml/14915689-27a3-4cd8-80d2-9c30d0c768b6@alu.unizg.hr
> Signed-off-by: Waiman Long <longman@redhat.com>

Applied to cgroup/for-6.8-fixes with stable cc'd. Depending on when v6.9
gets cut, this might go in together with for-6.9 during rc1.

Thanks.

-- 
tejun

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2024-02-28 18:04 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-02-28  0:58 [PATCH] cgroup/cpuset: Fix a memory leak in update_exclusive_cpumask() Waiman Long
2024-02-28 13:56 ` Mirsad Todorovac
2024-02-28 15:08   ` Waiman Long
2024-02-28 18:04 ` Tejun Heo

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox