Skip to contentSkip to conversation

mm/util.c: Make kvfree() safe for calling while holding spinlocks

PATCH In review Manfred Spraul 7 participants 15 messages Dec 22, 2021 – Jan 27, 2022

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

PATCH 1/1 mm/util.c: Make kvfree() safe for calling while holding spinlocks

One codepath in find_alloc_undo() calls kvfree() while holding a spinlock. Since vfree() can sleep this is a bug.

Previously, the code path used kfree(), and kfree() is safe to be called while holding a spinlock.

Minghao proposed to fix this by updating find_alloc_undo().

Alternate proposal to fix this: Instead of changing find_alloc_undo(), change kvfree() so that the same rules as for kfree() apply:
Having different rules for kfree() and kvfree() just asks for bugs.

Disadvantage: Releasing vmalloc'ed memory will be delayed a bit.

Fixes fc37a3b8b438 ("[PATCH] ipc sem: use kvmalloc for sem_undo allocation")
---
 mm/util.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
mm/util.c +2 -2
index 741ba32a43ac..7f9181998835 100644
@@ -610,12 +610,12 @@ EXPORT_SYMBOL(kvmalloc_node);
  * It is slightly more efficient to use kfree() or vfree() if you are certain
  * that you know which one to use.
  *
- * Context: Either preemptible task context or not-NMI interrupt.
+ * Context: Any context except NMI interrupt.
  */
 void kvfree(const void *addr)
 {
 	if (is_vmalloc_addr(addr))
-		vfree(addr);
+		vfree_atomic(addr);
 	else
 		kfree(addr);
 }
Signature · 1 line
2.33.1
Acked-by on v1 1/1
#1 lore raw

Cc'ing more mm folks.

Quoted 2 lines · Manfred Spraul

One codepath in find_alloc_undo() calls kvfree() while holding a spinlock. Since vfree() can sleep this is a bug.

afaict the only other offender is devx_async_cmd_event_destroy_uobj(), in drivers/infiniband/hw/mlx5/devx.c. I was expecting to find more, actually.

Quoted 8 lines

Previously, the code path used kfree(), and kfree() is safe to be called while holding a spinlock.

Minghao proposed to fix this by updating find_alloc_undo().

Alternate proposal to fix this: Instead of changing find_alloc_undo(), change kvfree() so that the same rules as for kfree() apply:
Having different rules for kfree() and kvfree() just asks for bugs.

I agree that it is best to have the same atomic semantics across all family of calls.

Quoted 2 lines

Disadvantage: Releasing vmalloc'ed memory will be delayed a bit.

I would not expect the added latency to be a big deal unless under serious memory pressure, for which case things are already fragile to begin with. Furthermore users of kvfree() are already warned that this is the slower choice. Feel free to add my:

Quoted 32 lines

Link:
https://lore.kernel.org/all/[email protected]/

Fixes fc37a3b8b438 ("[PATCH] ipc sem: use kvmalloc for sem_undo allocation")
---
 mm/util.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
mm/util.c +0 -0
index 741ba32a43ac..7f9181998835 100644
@@ -610,12 +610,12 @@ EXPORT_SYMBOL(kvmalloc_node);
  * It is slightly more efficient to use kfree() or vfree() if you are 

certain
* that you know which one to use.
*
- * Context: Either preemptible task context or not-NMI interrupt.
+ * Context: Any context except NMI interrupt.
*/
void kvfree(const void *addr)
{

mm/util.c +1 -1
 	if (is_vmalloc_addr(addr))
-		vfree(addr);
+		vfree_atomic(addr);
 	else
 		kfree(addr);
 }
#2 lore raw
In reply to Manfred Spraul
Quoted 33 lines · Manfred Spraul

One codepath in find_alloc_undo() calls kvfree() while holding a spinlock. Since vfree() can sleep this is a bug.

Previously, the code path used kfree(), and kfree() is safe to be called while holding a spinlock.

Minghao proposed to fix this by updating find_alloc_undo().

Alternate proposal to fix this: Instead of changing find_alloc_undo(), change kvfree() so that the same rules as for kfree() apply:
Having different rules for kfree() and kvfree() just asks for bugs.

Disadvantage: Releasing vmalloc'ed memory will be delayed a bit.

Fixes fc37a3b8b438 ("[PATCH] ipc sem: use kvmalloc for sem_undo allocation")
---
 mm/util.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
mm/util.c +0 -1
index 741ba32a43ac..7f9181998835 100644
@@ -610,12 +610,12 @@ EXPORT_SYMBOL(kvmalloc_node);
  * It is slightly more efficient to use kfree() or vfree() if you are certain
  * that you know which one to use.
  *
- * Context: Either preemptible task context or not-NMI interrupt.

mm/util.c EXPORT_SYMBOL(kvmalloc_node); · line 621

Quoted 10 lines
mm/util.c +2 -1
+ * Context: Any context except NMI interrupt.
  */
 void kvfree(const void *addr)
 {
 	if (is_vmalloc_addr(addr))
-		vfree(addr);
+		vfree_atomic(addr);
 	else
 		kfree(addr);
 }

I would prefer to release memory ASAP if it's possible. What do you think about this change?

mm/util.c +6 -3
@@ -614,9 +614,12 @@ EXPORT_SYMBOL(kvmalloc_node);
  */
 void kvfree(const void *addr)
 {
-       if (is_vmalloc_addr(addr))
-               vfree(addr);
-       else
+       if (is_vmalloc_addr(addr)) {
+               if (in_atomic())
+                       vfree_atomic();
+               else
+                       vfree(addr);
+       } else
                kfree(addr);
 }
 EXPORT_SYMBOL(kvfree);
#3 lore raw

Hello Vasily,

Quoted 12 lines · Vasily Averin

I would prefer to release memory ASAP if it's possible. What do you think about this change?

mm/util.c +0 -2
@@ -614,9 +614,12 @@ EXPORT_SYMBOL(kvmalloc_node);
   */
  void kvfree(const void *addr)
  {
-       if (is_vmalloc_addr(addr))
-               vfree(addr);

mm/util.c EXPORT_SYMBOL(kvmalloc_node); · line 625

Quoted 10 lines
mm/util.c +6 -1
-       else
+       if (is_vmalloc_addr(addr)) {
+               if (in_atomic())
+                       vfree_atomic();
+               else
+                       vfree(addr);
+       } else
                 kfree(addr);
  }
  EXPORT_SYMBOL(kvfree);

Unfortunately this cannot work:

Quoted 9 lines

/*
* Are we running in atomic context?  WARNING: this macro cannot
* always detect atomic context; in particular, it cannot know about
* held spinlocks in non-preemptible kernels.  Thus it should not be
* used in the general case to determine whether sleeping is possible.
* Do not use in_atomic() in driver code.
*/
#define in_atomic()     (preempt_count() != 0)

--

    Manfred

#4 lore raw
Quoted 18 lines · Manfred Spraul

Hello Vasily,

Quoted 15 lines · Vasily Averin

I would prefer to release memory ASAP if it's possible. What do you think about this change?

mm/util.c +3 -3
@@ -614,9 +614,12 @@ EXPORT_SYMBOL(kvmalloc_node);
    */
   void kvfree(const void *addr)
   {
-       if (is_vmalloc_addr(addr))
-               vfree(addr);
-       else
+       if (is_vmalloc_addr(addr)) {
+               if (in_atomic())
+                       vfree_atomic();
Quoted 8 lines

mm/util.c EXPORT_SYMBOL(kvmalloc_node); · line 625

Quoted 7 lines
mm/util.c +3 -0
+               else
+                       vfree(addr);
+       } else
                  kfree(addr);
   }
   EXPORT_SYMBOL(kvfree);

Unfortunately this cannot work:

yes, you're right and I do not see any better solution yet.

maintainer
#5 lore raw
In reply to Manfred Spraul
Quoted 6 lines

One codepath in find_alloc_undo() calls kvfree() while holding a spinlock. Since vfree() can sleep this is a bug.

Previously, the code path used kfree(), and kfree() is safe to be called while holding a spinlock.

Quoted 8 lines

Minghao proposed to fix this by updating find_alloc_undo().

Alternate proposal to fix this: Instead of changing find_alloc_undo(), change kvfree() so that the same rules as for kfree() apply:
Having different rules for kfree() and kvfree() just asks for bugs.

Disadvantage: Releasing vmalloc'ed memory will be delayed a bit.

I guess the issues is with "vmap_purge_lock" mutex? I think it is better to make the vfree() call as non-blocking one, i.e. the current design is is suffering from one drawback. It is related to purging the outstanding lazy areas from caller context. The drain process can be time consuming and if it is done from high-prio or RT contexts it can hog a CPU. Another issue is what you have reported that is about calling the schedule() and holding spinlock. The proposal is to perform a drain in a separate work:

<snip>

mm/vmalloc.c +11 -13
index d2a00ad4e1dd..7c5d9b148fa4 100644
@@ -1717,18 +1717,6 @@ static bool __purge_vmap_area_lazy(unsigned long start, unsigned long end)
 	return true;
 }
 
-/*
- * Kick off a purge of the outstanding lazy areas. Don't bother if somebody
- * is already purging.
- */
-static void try_purge_vmap_area_lazy(void)
-{
-	if (mutex_trylock(&vmap_purge_lock)) {
-		__purge_vmap_area_lazy(ULONG_MAX, 0);
-		mutex_unlock(&vmap_purge_lock);
-	}
-}
-
 /*
  * Kick off a purge of the outstanding lazy areas.
  */
@@ -1740,6 +1728,16 @@ static void purge_vmap_area_lazy(void)
 	mutex_unlock(&vmap_purge_lock);
 }
 
+static void drain_vmap_area(struct work_struct *work)
+{
+	if (mutex_trylock(&vmap_purge_lock)) {
+		__purge_vmap_area_lazy(ULONG_MAX, 0);
+		mutex_unlock(&vmap_purge_lock);
+	}
+}
+
+static DECLARE_WORK(drain_vmap_area_work, drain_vmap_area);
+
 /*
  * Free a vmap area, caller ensuring that the area has been unmapped
  * and flush_cache_vunmap had been called for the correct range
@@ -1766,7 +1764,7 @@ static void free_vmap_area_noflush(struct vmap_area *va)
 
 	/* After this point, we may free va at any time */
 	if (unlikely(nr_lazy > lazy_max_pages()))
-		try_purge_vmap_area_lazy();
+		schedule_work(&drain_vmap_area_work);
 }
 
 /*

<snip>

--
Vlad Rezki

#6 lore raw

mm/vmalloc.c static void purge_vmap_area_lazy(void) · line 1739

Quoted 9 lines · Uladzislau Rezki
mm/vmalloc.c +9 -0
+static void drain_vmap_area(struct work_struct *work)
+{
+	if (mutex_trylock(&vmap_purge_lock)) {
+		__purge_vmap_area_lazy(ULONG_MAX, 0);
+		mutex_unlock(&vmap_purge_lock);
+	}
+}
+
+static DECLARE_WORK(drain_vmap_area_work, drain_vmap_area);

Presuambly if the worker fails to get the mutex, it should reschedule itself? And should it even trylock or just always lock?

This kind of ties into something I've been wondering about -- we have a number of places in the kernel which cache 'freed' vmalloc allocations in order to speed up future allocations of the same size. Kind of like slab. Would we be better off trying to cache frequent allocations inside vmalloc instead of always purging them?

maintainer
#7 lore raw
Quoted 14 lines · Matthew Wilcox

mm/vmalloc.c static void purge_vmap_area_lazy(void) · line 1739

Quoted 9 lines · Uladzislau Rezki
mm/vmalloc.c +9 -0
+static void drain_vmap_area(struct work_struct *work)
+{
+	if (mutex_trylock(&vmap_purge_lock)) {
+		__purge_vmap_area_lazy(ULONG_MAX, 0);
+		mutex_unlock(&vmap_purge_lock);
+	}
+}
+
+static DECLARE_WORK(drain_vmap_area_work, drain_vmap_area);

Presuambly if the worker fails to get the mutex, it should reschedule itself? And should it even trylock or just always lock?

mutex_trylock() has no sense here. It should just always get the lock. Otherwise we can miss the point to purge. Agree with your opinion.

Quoted 7 lines

This kind of ties into something I've been wondering about -- we have a number of places in the kernel which cache 'freed' vmalloc allocations in order to speed up future allocations of the same size. Kind of like slab. Would we be better off trying to cache frequent allocations inside vmalloc instead of always purging them?

Hm... Some sort of caching would be good. Though it will require some time to think over all details and design itself. We can cache VAs instead of purging them until some point or threshold. So basically we can keep it in our data structures, associate it with some cache, based on size and reuse it later in the alloc_vmap_area().

All that is related to "vmap_area" caching. Another option is to cache the "vm_struct". It includes "vmap_area" + pages to drive the mapping. It is a higher level of caching and i am not sure if an implementation would be so straightforward.

--
Vlad Rezki

maintainer
#8 lore raw
Quoted 15 lines · Uladzislau Rezki
Quoted 14 lines · Matthew Wilcox

Review

Quoted 9 lines · Uladzislau Rezki
(quoted diff) +9 -0
+static void drain_vmap_area(struct work_struct *work)
+{
+	if (mutex_trylock(&vmap_purge_lock)) {
+		__purge_vmap_area_lazy(ULONG_MAX, 0);
+		mutex_unlock(&vmap_purge_lock);
+	}
+}
+
+static DECLARE_WORK(drain_vmap_area_work, drain_vmap_area);

Presuambly if the worker fails to get the mutex, it should reschedule itself? And should it even trylock or just always lock?

Quoted 3 lines

mutex_trylock() has no sense here. It should just always get the lock. Otherwise we can miss the point to purge. Agree with your opinion.

Below the patch that address Matthew's points:

<snip>

mm/vmalloc.c +22 -12
index d2a00ad4e1dd..b82db44fea60 100644
@@ -1717,17 +1717,10 @@ static bool __purge_vmap_area_lazy(unsigned long start, unsigned long end)
 	return true;
 }
 
-/*
- * Kick off a purge of the outstanding lazy areas. Don't bother if somebody
- * is already purging.
- */
-static void try_purge_vmap_area_lazy(void)
-{
-	if (mutex_trylock(&vmap_purge_lock)) {
-		__purge_vmap_area_lazy(ULONG_MAX, 0);
-		mutex_unlock(&vmap_purge_lock);
-	}
-}
+static void purge_vmap_area_lazy(void);
+static void drain_vmap_area(struct work_struct *work);
+static DECLARE_WORK(drain_vmap_area_work, drain_vmap_area);
+static atomic_t drain_vmap_area_work_in_progress;
 
 /*
  * Kick off a purge of the outstanding lazy areas.
@@ -1740,6 +1733,22 @@ static void purge_vmap_area_lazy(void)
 	mutex_unlock(&vmap_purge_lock);
 }
 
+static void drain_vmap_area(struct work_struct *work)
+{
+	mutex_lock(&vmap_purge_lock);
+	__purge_vmap_area_lazy(ULONG_MAX, 0);
+	mutex_unlock(&vmap_purge_lock);
+
+	/*
+	 * Check if rearming is still required. If not, we are
+	 * done and can let a next caller to initiate a new drain.
+	 */
+	if (atomic_long_read(&vmap_lazy_nr) > lazy_max_pages())
+		schedule_work(&drain_vmap_area_work);
+	else
+		atomic_set(&drain_vmap_area_work_in_progress, 0);
+}
+
 /*
  * Free a vmap area, caller ensuring that the area has been unmapped
  * and flush_cache_vunmap had been called for the correct range
@@ -1766,7 +1775,8 @@ static void free_vmap_area_noflush(struct vmap_area *va)
 
 	/* After this point, we may free va at any time */
 	if (unlikely(nr_lazy > lazy_max_pages()))
-		try_purge_vmap_area_lazy();
+		if (!atomic_xchg(&drain_vmap_area_work_in_progress, 1))
+			schedule_work(&drain_vmap_area_work);
 }
 
 /*

<snip>

Manfred, could you please have a look and if you have a time test it? I mean if it solves your issue. You can take over this patch and resend it, otherwise i can send it myself later if we all agree with it.

--
Vlad Rezki

#9 lore raw

Hello Vlad,

Quoted 4 lines · Uladzislau Rezki

[...]
Manfred, could you please have a look and if you have a time test it? I mean if it solves your issue. You can take over this patch and resend it, otherwise i can send it myself later if we all agree with it.

I think we mix tasks: We have a bug in ipc/sem.c, thus we need a solution suitable for stable.

Fixes fc37a3b8b438 ("[PATCH] ipc sem: use kvmalloc for sem_undo allocation")

I think for stable, there are only two options:

- change ipc/sem.c, call kvfree() after dropping the spinlock

- change kvfree() to use vfree_atomic().

From my point of view, both approaches are fine.

I.e. I'm waiting for feedback from an mm maintainer.

As soon as it is agreed, I will retest the chosen solution.

Now you propose to redesign vfree(), so that vfree() is safe to be called while holding spinlocks:

Quoted 51 lines

<snip>

mm/vmalloc.c +20 -11
index d2a00ad4e1dd..b82db44fea60 100644
@@ -1717,17 +1717,10 @@ static bool __purge_vmap_area_lazy(unsigned long start, unsigned long end)
  	return true;
  }
 
-/*
- * Kick off a purge of the outstanding lazy areas. Don't bother if somebody
- * is already purging.
- */
-static void try_purge_vmap_area_lazy(void)
-{
-	if (mutex_trylock(&vmap_purge_lock)) {
-		__purge_vmap_area_lazy(ULONG_MAX, 0);
-		mutex_unlock(&vmap_purge_lock);
-	}
-}
+static void purge_vmap_area_lazy(void);
+static void drain_vmap_area(struct work_struct *work);
+static DECLARE_WORK(drain_vmap_area_work, drain_vmap_area);
+static atomic_t drain_vmap_area_work_in_progress;
 
  /*
   * Kick off a purge of the outstanding lazy areas.
@@ -1740,6 +1733,22 @@ static void purge_vmap_area_lazy(void)
  	mutex_unlock(&vmap_purge_lock);
  }
 
+static void drain_vmap_area(struct work_struct *work)
+{
+	mutex_lock(&vmap_purge_lock);
+	__purge_vmap_area_lazy(ULONG_MAX, 0);
+	mutex_unlock(&vmap_purge_lock);
+
+	/*
+	 * Check if rearming is still required. If not, we are
+	 * done and can let a next caller to initiate a new drain.
+	 */
+	if (atomic_long_read(&vmap_lazy_nr) > lazy_max_pages())
+		schedule_work(&drain_vmap_area_work);
+	else
+		atomic_set(&drain_vmap_area_work_in_progress, 0);
+}
+
  /*
   * Free a vmap area, caller ensuring that the area has been unmapped
   * and flush_cache_vunmap had been called for the correct range

mm/vmalloc.c static void free_vmap_area_noflush(struct vmap_area *va) · line 1782

Quoted 10 lines
mm/vmalloc.c +2 -1
@@ -1766,7 +1775,8 @@ static void free_vmap_area_noflush(struct vmap_area *va)
 
  	/* After this point, we may free va at any time */
  	if (unlikely(nr_lazy > lazy_max_pages()))
-		try_purge_vmap_area_lazy();
+		if (!atomic_xchg(&drain_vmap_area_work_in_progress, 1))
+			schedule_work(&drain_vmap_area_work);
  }
 
  /*
<snip>

I do now know the mm code well enough to understand the side effects of the change. And doubt that it is suitable for stable, i.e. we need the simple patch first.

--

    Manfred

maintainer
#10 lore raw
Quoted 19 lines

Hello Vlad,

Quoted 4 lines · Uladzislau Rezki

[...]
Manfred, could you please have a look and if you have a time test it? I mean if it solves your issue. You can take over this patch and resend it, otherwise i can send it myself later if we all agree with it.

I think we mix tasks: We have a bug in ipc/sem.c, thus we need a solution suitable for stable.

Fixes fc37a3b8b438 ("[PATCH] ipc sem: use kvmalloc for sem_undo

allocation")
Cc: [email protected]

I think for stable, there are only two options:

- change ipc/sem.c, call kvfree() after dropping the spinlock

Quoted 8 lines

- change kvfree() to use vfree_atomic().

From my point of view, both approaches are fine.

I.e. I'm waiting for feedback from an mm maintainer.

As soon as it is agreed, I will retest the chosen solution.

Here for me it anyway looks like a change and it is hard to judge if the second solution is stable or not, because it is a new change and the kvfree() interface is changed internally.

Quoted 55 lines

Now you propose to redesign vfree(), so that vfree() is safe to be called while holding spinlocks:

Quoted 51 lines

<snip>

mm/vmalloc.c +20 -12
index d2a00ad4e1dd..b82db44fea60 100644
@@ -1717,17 +1717,10 @@ static bool __purge_vmap_area_lazy(unsigned long start, unsigned long end)
  	return true;
  }
-/*
- * Kick off a purge of the outstanding lazy areas. Don't bother if somebody
- * is already purging.
- */
-static void try_purge_vmap_area_lazy(void)
-{
-	if (mutex_trylock(&vmap_purge_lock)) {
-		__purge_vmap_area_lazy(ULONG_MAX, 0);
-		mutex_unlock(&vmap_purge_lock);
-	}
-}
+static void purge_vmap_area_lazy(void);
+static void drain_vmap_area(struct work_struct *work);
+static DECLARE_WORK(drain_vmap_area_work, drain_vmap_area);
+static atomic_t drain_vmap_area_work_in_progress;
  /*
   * Kick off a purge of the outstanding lazy areas.
@@ -1740,6 +1733,22 @@ static void purge_vmap_area_lazy(void)
  	mutex_unlock(&vmap_purge_lock);
  }
+static void drain_vmap_area(struct work_struct *work)
+{
+	mutex_lock(&vmap_purge_lock);
+	__purge_vmap_area_lazy(ULONG_MAX, 0);
+	mutex_unlock(&vmap_purge_lock);
+
+	/*
+	 * Check if rearming is still required. If not, we are
+	 * done and can let a next caller to initiate a new drain.
+	 */
+	if (atomic_long_read(&vmap_lazy_nr) > lazy_max_pages())
+		schedule_work(&drain_vmap_area_work);
+	else
+		atomic_set(&drain_vmap_area_work_in_progress, 0);
+}
+
  /*
   * Free a vmap area, caller ensuring that the area has been unmapped
   * and flush_cache_vunmap had been called for the correct range
@@ -1766,7 +1775,8 @@ static void free_vmap_area_noflush(struct vmap_area *va)
  	/* After this point, we may free va at any time */
  	if (unlikely(nr_lazy > lazy_max_pages()))
-		try_purge_vmap_area_lazy();
Quoted 9 lines

mm/vmalloc.c static void free_vmap_area_noflush(struct vmap_area *va) · line 1782

Quoted 5 lines
mm/vmalloc.c +2 -0
+		if (!atomic_xchg(&drain_vmap_area_work_in_progress, 1))
+			schedule_work(&drain_vmap_area_work);
  }
  /*
<snip>

I do now know the mm code well enough to understand the side effects of the change. And doubt that it is suitable for stable, i.e. we need the simple patch first.

Well, it is as simple as it could be :)

--
Vlad Rezki

maintainer
#11 lore raw
In reply to Manfred Spraul
Quoted 6 lines · On Wed, 22 Dec 2021 20:48:28 +0100 Manfred Spraul <[email protected]>…

One codepath in find_alloc_undo() calls kvfree() while holding a spinlock. Since vfree() can sleep this is a bug.

Previously, the code path used kfree(), and kfree() is safe to be called while holding a spinlock.

Quoted 7 lines

Minghao proposed to fix this by updating find_alloc_undo().

Alternate proposal to fix this: Instead of changing find_alloc_undo(), change kvfree() so that the same rules as for kfree() apply:
Having different rules for kfree() and kvfree() just asks for bugs.

Disadvantage: Releasing vmalloc'ed memory will be delayed a bit.

I know we've been around this loop a bunch of times and deferring was
considered. But I forget the conclusion. IIRC, mhocko was involved?

Quoted 17 lines
mm/util.c +2 -2
@@ -610,12 +610,12 @@ EXPORT_SYMBOL(kvmalloc_node);
  * It is slightly more efficient to use kfree() or vfree() if you are certain
  * that you know which one to use.
  *
- * Context: Either preemptible task context or not-NMI interrupt.
+ * Context: Any context except NMI interrupt.
  */
 void kvfree(const void *addr)
 {
 	if (is_vmalloc_addr(addr))
-		vfree(addr);
+		vfree_atomic(addr);
 	else
 		kfree(addr);
 }
#12 lore raw

Hi Andrew,

Quoted 10 lines · Andrew Morton
Quoted 8 lines · On Wed, 22 Dec 2021 20:48:28 +0100 Manfred Spraul <[email protected]>…

One codepath in find_alloc_undo() calls kvfree() while holding a spinlock. Since vfree() can sleep this is a bug.

Previously, the code path used kfree(), and kfree() is safe to be called while holding a spinlock.

Minghao proposed to fix this by updating find_alloc_undo().

Quoted 7 lines
Quoted 5 lines

Alternate proposal to fix this: Instead of changing find_alloc_undo(), change kvfree() so that the same rules as for kfree() apply:
Having different rules for kfree() and kvfree() just asks for bugs.

Disadvantage: Releasing vmalloc'ed memory will be delayed a bit.

I know we've been around this loop a bunch of times and deferring was
considered. But I forget the conclusion. IIRC, mhocko was involved?

I do not remember a mail from mhocko.

Shakeel proposed to use the approach from Chi.

Decision: https://marc.info/?l=linux-kernel&m=164132032717757&w=2

With Reviewed-by:

https://marc.info/?l=linux-kernel&m=164132744522325&w=2

Quoted 17 lines
Quoted 17 lines
mm/util.c +2 -2
@@ -610,12 +610,12 @@ EXPORT_SYMBOL(kvmalloc_node);
   * It is slightly more efficient to use kfree() or vfree() if you are certain
   * that you know which one to use.
   *
- * Context: Either preemptible task context or not-NMI interrupt.
+ * Context: Any context except NMI interrupt.
   */
  void kvfree(const void *addr)
  {
  	if (is_vmalloc_addr(addr))
-		vfree(addr);
+		vfree_atomic(addr);
  	else
  		kfree(addr);
  }
reviewer
#13 lore raw
Quoted 17 lines · Manfred Spraul

Hi Andrew,

Quoted 14 lines · Andrew Morton
Quoted 12 lines · On Wed, 22 Dec 2021 20:48:28 +0100 Manfred Spraul <[email protected]>…

One codepath in find_alloc_undo() calls kvfree() while holding a spinlock. Since vfree() can sleep this is a bug.

Previously, the code path used kfree(), and kfree() is safe to be called while holding a spinlock.

Minghao proposed to fix this by updating find_alloc_undo().

Alternate proposal to fix this: Instead of changing find_alloc_undo(), change kvfree() so that the same rules as for kfree() apply:
Having different rules for kfree() and kvfree() just asks for bugs.

Quoted 5 lines
Quoted 3 lines
Quoted 1 line

Disadvantage: Releasing vmalloc'ed memory will be delayed a bit.

I know we've been around this loop a bunch of times and deferring was
considered. But I forget the conclusion. IIRC, mhocko was involved?

I do not remember a mail from mhocko.

I do not remember either.

Quoted 4 lines

Shakeel proposed to use the approach from Chi.

Decision: https://marc.info/?l=linux-kernel&m=164132032717757&w=2

And I would agree with Shakeel and go with the original change to the ipc code. That is trivial and without any other side effects like this one. I bet nobody has evaluated what the undconditional deferred freeing has. At least changelog doesn't really dive into that more than a very vague statement that this will happen.

Quoted 21 lines

With Reviewed-by:

https://marc.info/?l=linux-kernel&m=164132744522325&w=2

Quoted 17 lines
Quoted 17 lines
mm/util.c +2 -2
@@ -610,12 +610,12 @@ EXPORT_SYMBOL(kvmalloc_node);
   * It is slightly more efficient to use kfree() or vfree() if you are certain
   * that you know which one to use.
   *
- * Context: Either preemptible task context or not-NMI interrupt.
+ * Context: Any context except NMI interrupt.
   */
  void kvfree(const void *addr)
  {
  	if (is_vmalloc_addr(addr))
-		vfree(addr);
+		vfree_atomic(addr);
  	else
  		kfree(addr);
  }
Signature · 2 lines
Michal Hocko
SUSE Labs
maintainer
#14 lore raw
Quoted 29 lines · Michal Hocko
Quoted 17 lines · Manfred Spraul

Hi Andrew,

Quoted 14 lines · Andrew Morton
Quoted 12 lines · On Wed, 22 Dec 2021 20:48:28 +0100 Manfred Spraul <[email protected]>…

One codepath in find_alloc_undo() calls kvfree() while holding a spinlock. Since vfree() can sleep this is a bug.

Previously, the code path used kfree(), and kfree() is safe to be called while holding a spinlock.

Minghao proposed to fix this by updating find_alloc_undo().

Alternate proposal to fix this: Instead of changing find_alloc_undo(), change kvfree() so that the same rules as for kfree() apply:
Having different rules for kfree() and kvfree() just asks for bugs.

Quoted 5 lines
Quoted 3 lines
Quoted 1 line

Disadvantage: Releasing vmalloc'ed memory will be delayed a bit.

I know we've been around this loop a bunch of times and deferring was
considered. But I forget the conclusion. IIRC, mhocko was involved?

I do not remember a mail from mhocko.

I do not remember either.

Quoted 3 lines

Shakeel proposed to use the approach from Chi.

Quoted 8 lines
Quoted 1 line

Decision: https://marc.info/?l=linux-kernel&m=164132032717757&w=2

And I would agree with Shakeel and go with the original change to the ipc code. That is trivial and without any other side effects like this one. I bet nobody has evaluated what the undconditional deferred freeing has. At least changelog doesn't really dive into that more than a very vague statement that this will happen.

Absolutely agree here. Especially that changing the kvfree() will not look stable.

After applying the https://www.spinics.net/lists/linux-mm/msg282264.html
we will be able to use vfree() from atomic anyway.

--
Vlad Rezki