ipc/sem: do not sleep with a spin lock held
Subsystems (3)
PATCH 1/1 ipc/sem: do not sleep with a spin lock held
From: Minghao Chi <[email protected]>
We can't call kvfree() with a spin lock held, so defer it.
--- ipc/sem.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
ipc/sem.c +2 -2
@@ -1964,6 +1964,7 @@ static struct sem_undo *find_alloc_undo(struct ipc_namespace *ns, int semid)*/ un =lookup_undo (ulp, semid);if (un) { +spin_unlock (&ulp->lock);kvfree (new); goto success; }
@@ -1976,9 +1977,8 @@ static struct sem_undo *find_alloc_undo(struct ipc_namespace *ns, int semid)ipc_assert_locked_object (&sma->sem_perm);list_add (&new->list_id, &sma->list_id); un = new; - -success:spin_unlock (&ulp->lock); +success:sem_unlock (sma,-1 ); out:return un;
Signature · 1 line
2.25.1
Hi Minghao,
Quoted 6 lines · On 12/22/21 09:10, [email protected] wrote:
From: Minghao Chi <[email protected]>
We can't call
kvfree()with a spin lock held, so defer it.Reported-by Zeal Robot <[email protected]>Signed-off-by Minghao Chi <[email protected]>
Could you add
I will review/test the change in the next few days.
Especially, I would like to check if there are further instances where the same mistake was made.
Quoted 11 lines
/**
*kvfree()- Free memory.
* @addr: Pointer to allocated memory.
*
* kvfree frees memory allocated by any ofvmalloc(),kmalloc()orkvmalloc().
* It is slightly more efficient to usekfree()orvfree()if you are certain
* that you know which one to use.
*
* Context: Either preemptible task context or not-NMI interrupt. */
As an independent change: Should we add a
might_sleep_if(!in_interrupt());
into kvfree(), to trigger bugs more easily?
Quoted 27 lines
--- ipc/sem.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)ipc/sem.c +2
-2@@ -1964,6 +1964,7 @@ static struct sem_undo *find_alloc_undo(struct ipc_namespace *ns, int semid)*/ un =lookup_undo (ulp, semid);if (un) { +spin_unlock (&ulp->lock);kvfree (new); goto success; }@@ -1976,9 +1977,8 @@ static struct sem_undo *find_alloc_undo(struct ipc_namespace *ns, int semid)ipc_assert_locked_object (&sma->sem_perm);list_add (&new->list_id, &sma->list_id); un = new; - -success:spin_unlock (&ulp->lock); +success:sem_unlock (sma,-1 ); out:return un;
Quoted 6 lines · Manfred Spraul
Hi Minghao,
Quoted 3 lines · On 12/22/21 09:10, [email protected] wrote:
From: Minghao Chi <[email protected]>
We can't call
kvfree()with a spin lock held, so defer it.
I'm sorry, but I do not understand why exactly we cannot use kvfree? Could you explain it in more details?
Quoted 24 lines
Quoted 2 lines
Reported-by Zeal Robot <[email protected]>Signed-off-by Minghao Chi <[email protected]>Could you add
Fixes fc37a3b8b438 ("[PATCH] ipc sem: use kvmalloc for sem_undo allocation")I will review/test the change in the next few days.
Especially, I would like to check if there are further instances where the same mistake was made.
Quoted 11 lines
/**
*kvfree()- Free memory.
* @addr: Pointer to allocated memory.
*
* kvfree frees memory allocated by any ofvmalloc(),kmalloc()orkvmalloc().
* It is slightly more efficient to usekfree()orvfree()if you are certain
* that you know which one to use.
*
* Context: Either preemptible task context or not-NMI interrupt. */
Quoted 6 lines
As an independent change: Should we add a
might_sleep_if(!in_interrupt());
into
kvfree(), to trigger bugs more easily?
I think it is good idea in general,
however please do not use "in_interrupt()", it is obsoleted and in fact means "We're in NMI,IRQ,SoftIRQ context or have BH disabled"
Please use something like in_task()
Thank you, Vasily Averin
Quoted 10 lines · Vasily Averin
Quoted 6 lines · Manfred Spraul
Hi Minghao,
Quoted 3 lines · On 12/22/21 09:10, [email protected] wrote:
From: Minghao Chi <[email protected]>
We can't call
kvfree()with a spin lock held, so defer it.I'm sorry, but I do not understand why exactly we cannot use kvfree? Could you explain it in more details?
Got it,
there is cond_resched() called in __vfree() -> __vunmap()
However I'm still not sure that in_interrupt() is used correctly here.
Thank you,
Vasily Averin
Hi Vasily,
Quoted 7 lines · Vasily Averin
Quoted 6 lines · Vasily Averin
Quoted 5 lines · Manfred Spraul
Hi Minghao,
Quoted 2 lines · On 12/22/21 09:10, [email protected] wrote:
From: Minghao Chi <[email protected]>
Quoted 7 lines
Quoted 3 lines
Quoted 1 line
Quoted 1 line
We can't call
kvfree()with a spin lock held, so defer it.I'm sorry, but I do not understand why exactly we cannot use kvfree? Could you explain it in more details?
Got it,
there iscond_resched()called in__vfree()->__vunmap()However I'm still not sure that
in_interrupt()is used correctly here.
I see three different topics:
- is the current code violating the API? I think yes, thus there is a bug that needs to be fixed.
- Where is __vunmap() sleeping? Would it be possible to make __vunmap() safe to be called when owning a spinlock?
- should kvfree() use vfree() [i.e. unsafe when owning a spinlock] or vfree_atomic [i.e. a bit slower, but safe]
As we did quite many s/kfree/kvfree/ changes, perhaps just switching to vfree_atomic() is the best solution.
@Andrew: What would you prefer?
In addition, if we do not use vfree_atomic(): Then I would propose to copy the might_sleep_if() from vfree() into kvfree()
--
Manfred
Quoted 13 lines · Manfred Spraul
Hi Vasily,
Quoted 10 lines · Vasily Averin
Quoted 9 lines · Vasily Averin
Quoted 6 lines · Manfred Spraul
Hi Minghao,
Quoted 3 lines · On 12/22/21 09:10, [email protected] wrote:
From: Minghao Chi <[email protected]>
We can't call
kvfree()with a spin lock held, so defer it.I'm sorry, but I do not understand why exactly we cannot use kvfree? Could you explain it in more details?
Quoted 8 lines
Quoted 4 lines
Got it,
there iscond_resched()called in__vfree()->__vunmap()However I'm still not sure that
in_interrupt()is used correctly here.I see three different topics:
- is the current code violating the API? I think yes, thus there is a bug that needs to be fixed.
I'm agree. Found issue is a bug and it should be fixed ASAP, I'm sorry for a mistake in my patch.
Quoted 1 line
- Where is
__vunmap()sleeping? Would it be possible to make__vunmap()safe to be called when owning a spinlock?
I think it is possible, and we should do it to prevent similar incidents in future. vfree() should check preempt count to detect this situation (i.e. execution under taken spinlock) generate WARN_ON and then call __vfree_deferred() to avoid sleep.
Quoted 1 line
- should
kvfree()usevfree()[i.e. unsafe when owning a spinlock] orvfree_atomic[i.e. a bit slower, but safe]
I think it's better to change vfree.
Quoted 5 lines
As we did quite many s/kfree/kvfree/ changes, perhaps just switching to
vfree_atomic()is the best solution.@Andrew: What would you prefer?
In addition, if we do not use
vfree_atomic(): Then I would propose to copy themight_sleep_if()fromvfree()intokvfree()
I think it does not help, as far as I understand we are in task context, just under taken spinlock.
Thank you,
vasily Averin
Quoted 28 lines · Vasily Averin
Quoted 13 lines · Manfred Spraul
Hi Vasily,
Quoted 10 lines · Vasily Averin
Quoted 9 lines · Vasily Averin
Quoted 6 lines · Manfred Spraul
Hi Minghao,
Quoted 3 lines · On 12/22/21 09:10, [email protected] wrote:
From: Minghao Chi <[email protected]>
We can't call
kvfree()with a spin lock held, so defer it.I'm sorry, but I do not understand why exactly we cannot use kvfree? Could you explain it in more details?
Quoted 8 lines
Quoted 4 lines
Got it,
there iscond_resched()called in__vfree()->__vunmap()However I'm still not sure that
in_interrupt()is used correctly here.I see three different topics:
- is the current code violating the API? I think yes, thus there is a bug that needs to be fixed.
I'm agree. Found issue is a bug and it should be fixed ASAP, I'm sorry for a mistake in my patch.
Quoted 1 line
- Where is
__vunmap()sleeping? Would it be possible to make__vunmap()safe to be called when owning a spinlock?
Quoted 7 lines
I think it is possible, and we should do it to prevent similar incidents in future.
vfree()should check preempt count to detect this situation (i.e. execution under taken spinlock) generateWARN_ONand then call__vfree_deferred()to avoid sleep.Quoted 1 line
- should
kvfree()usevfree()[i.e. unsafe when owning a spinlock] orvfree_atomic[i.e. a bit slower, but safe]I think it's better to change vfree.
I mean something like this:
mm/vmalloc.c +2 -2
@@ -2674,7 +2674,7 @@ void vfree_atomic(const void *addr)static void __vfree (const void *addr) { -if (unlikely (in_interrupt ())) + if (unlikely(in_atomic())) <<<< VvS: do not sleep in atomic ... __vfree_deferred(addr); else __vunmap(addr, 1);
@@ -2703,7 +2703,7 @@ void vfree(const void *addr)kmemleak_free (addr); -might_sleep_if (!in_interrupt ()); + might_sleep_if(in_task()); <<<<< VvS: ... but generate warning if vfree was called <<<<< in task context with taken spin_lock or spin_lock_bhif (!addr)
return;
Quoted 10 lines
Quoted 5 lines
As we did quite many s/kfree/kvfree/ changes, perhaps just switching to
vfree_atomic()is the best solution.@Andrew: What would you prefer?
In addition, if we do not use
vfree_atomic(): Then I would propose to copy themight_sleep_if()fromvfree()intokvfree()I think it does not help, as far as I understand we are in task context, just under taken spinlock.
Thank you,
vasily Averin
PATCH v2 1/1 ipc/sem: do not sleep with a spin lock held
In reply to Manfred SpraulFrom: Minghao Chi <[email protected]>
We can't call kvfree() with a spin lock held, so defer it.
allocation")
---
changelog since v2:
+ Fixes: fc37a3b8b438 ("[PATCH] ipc sem: use kvmalloc for sem_undo
+ allocation")
ipc/sem.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
ipc/sem.c +2 -2
@@ -1964,6 +1964,7 @@ static struct sem_undo *find_alloc_undo(struct ipc_namespace *ns, int semid)*/ un =lookup_undo (ulp, semid);if (un) { +spin_unlock (&ulp->lock);kvfree (new); goto success; }
@@ -1976,9 +1977,8 @@ static struct sem_undo *find_alloc_undo(struct ipc_namespace *ns, int semid)ipc_assert_locked_object (&sma->sem_perm);list_add (&new->list_id, &sma->list_id); un = new; - -success:spin_unlock (&ulp->lock); +success:sem_unlock (sma,-1 ); out:return un;
Signature · 1 line
2.25.1
PATCH v2 1/1 ipc/sem: do not sleep with a spin lock held
In reply to Manfred SpraulFrom: Minghao Chi <[email protected]>
We can't call kvfree() with a spin lock held, so defer it.
allocation")
---
changelog since v2:
+ Fixes: fc37a3b8b438 ("[PATCH] ipc sem: use kvmalloc for sem_undo
+ allocation")
ipc/sem.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
ipc/sem.c +0 -0
@@ -1964,6 +1964,7 @@ static struct sem_undo *find_alloc_undo(struct
ipc_namespace *ns, int semid)
*/
un = lookup_undo(ulp, semid);
if (un) {
+ spin_unlock(&ulp->lock);
kvfree(new);
goto success;
}
@@ -1976,9 +1977,8 @@ static struct sem_undo *find_alloc_undo(struct ipc_namespace *ns, int semid)
ipc_assert_locked_object(&sma->sem_perm); list_add(&new->list_id, &sma->list_id);
un = new;
-
-success:
spin_unlock(&ulp->lock);
+success:
sem_unlock(sma, -1);
out:
return un;
Signature · 1 line
2.25.1
PATCH v2 1/1 ipc/sem: do not sleep with a spin lock held
In reply to Manfred SpraulFrom: Minghao Chi <[email protected]>
We can't call kvfree() with a spin lock held, so defer it.
allocation")
---
changelog since v2:
+ Fixes: fc37a3b8b438 ("[PATCH] ipc sem: use kvmalloc for sem_undo
+ allocation")
ipc/sem.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
ipc/sem.c +2 -2
@@ -1964,6 +1964,7 @@ static struct sem_undo *find_alloc_undo(struct ipc_namespace *ns, int semid)*/ un =lookup_undo (ulp, semid);if (un) { +spin_unlock (&ulp->lock);kvfree (new); goto success; }
@@ -1976,9 +1977,8 @@ static struct sem_undo *find_alloc_undo(struct ipc_namespace *ns, int semid)ipc_assert_locked_object (&sma->sem_perm);list_add (&new->list_id, &sma->list_id); un = new; - -success:spin_unlock (&ulp->lock); +success:sem_unlock (sma,-1 ); out:return un;
--
2.25.1
Quoted 3 lines · On 23. 12. 21, 4:12, [email protected] wrote:
From: Minghao Chi <[email protected]>
We can't call
kvfree()with a spin lock held, so defer it.
Sorry, defer what?
There are attempts to fix kvfree instead, not sure which of these approaches (fix kvfree or its callers) won in the end?
Quoted 24 lines
Fixes fc37a3b8b438 ("[PATCH] ipc sem: use kvmalloc for sem_undoallocation")
Reported-by Zeal Robot <[email protected]>Signed-off-by Minghao Chi <[email protected]>---
changelog since v2:
+ Fixes:fc37a3b8b438("[PATCH] ipc sem: use kvmalloc forsem_undo
+ allocation")ipc/sem.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)ipc/sem.c +1
-0@@ -1964,6 +1964,7 @@ static struct sem_undo *find_alloc_undo(struct ipc_namespace *ns, int semid)*/ un =lookup_undo (ulp, semid);if (un) { +spin_unlock (&ulp->lock);kvfree (new); goto success; }
Signature · 2 lines
js suse labs
Hi Jiri,
Quoted 7 lines · Jiri Slaby
Quoted 3 lines · On 23. 12. 21, 4:12, [email protected] wrote:
From: Minghao Chi <[email protected]>
We can't call
kvfree()with a spin lock held, so defer it.Sorry, defer what?
First drop the spinlock, then call kvfree().
Quoted 2 lines
There are attempts to fix kvfree instead, not sure which of these approaches (fix kvfree or its callers) won in the end?
Exactly. We have three options - but noone volunteered yet to decide:
- change ipc/sem.c [minimal change]
- change kvfree() to use vfree_atomic() [would also fix other changes that did s/kfree/kvfree/]
- Modify the vma handling so that it becomes safe to call vfree() while holding a spinlock. [perfect approach, but I'm concerned about side effects]
--
Manfred
Quoted 14 lines · Manfred Spraul
Hi Jiri,
Quoted 7 lines · Jiri Slaby
Quoted 3 lines · On 23. 12. 21, 4:12, [email protected] wrote:
From: Minghao Chi <[email protected]>
We can't call
kvfree()with a spin lock held, so defer it.Sorry, defer what?
First drop the spinlock, then call
kvfree().
Quoted 6 lines
Quoted 3 lines
There are attempts to fix kvfree instead, not sure which of these approaches (fix kvfree or its callers) won in the end?
Exactly. We have three options - but noone volunteered yet to decide:
- change
ipc/sem.c[minimal change]
Let's go with the minimal change for now which can easily be cherry-picked for the stable tree. It seems other approaches need more work/discussion.
Quoted 12 lines
- change
kvfree()to usevfree_atomic()[would also fix other changes that did s/kfree/kvfree/]- Modify the vma handling so that it becomes safe to call
vfree()while holding a spinlock. [perfect approach, but I'm concerned about side effects]--
Manfred
Quoted 9 lines · On Wed, Dec 22, 2021 at 7:12 PM <[email protected]> wrote:
From: Minghao Chi <[email protected]>
We can't call
kvfree()with a spin lock held, so defer it.Fixes fc37a3b8b438 ("[PATCH] ipc sem: use kvmalloc for sem_undoallocation")
Reported-by Zeal Robot <[email protected]>Signed-off-by Minghao Chi <[email protected]>
Manfred Spraul
Reviewed-byQuoted 10 lines · Shakeel Butt
Quoted 8 lines · On Wed, Dec 22, 2021 at 7:12 PM <[email protected]> wrote:
From: Minghao Chi <[email protected]>
We can't call
kvfree()with a spin lock held, so defer it.Fixes fc37a3b8b438 ("[PATCH] ipc sem: use kvmalloc for sem_undoallocation")
Reported-by Zeal Robot <[email protected]>Signed-off-by Minghao Chi <[email protected]>Reviewed-by Shakeel Butt <[email protected]>
--
Manfred