Linux virtualization list
 help / color / mirror / Atom feed
* [PATCH] virtio_console: pass gfp flag through in alloc_buf()
@ 2026-08-29  1:20 Geliang Tang
  2026-09-05  1:14 ` Florian Westphal
  2026-09-05 17:32 ` Greg Kroah-Hartman
  0 siblings, 2 replies; 4+ messages in thread
From: Geliang Tang @ 2026-08-29  1:20 UTC (permalink / raw)
  To: Amit Shah, Arnd Bergmann, Greg Kroah-Hartman, Kees Cook
  Cc: Geliang Tang, virtualization

From: Geliang Tang <tanggeliang@kylinos.cn>

The `gfp` parameter of alloc_buf() has been dead code since the
function was introduced. The underlying allocation was always
hardcoded to GFP_KERNEL - first via kmalloc(..., GFP_KERNEL), then
via kmalloc_flex(*buf, sg, pages, GFP_KERNEL), and most recently
via kmalloc_flex(*buf, sg, pages) which defaults to GFP_KERNEL.

This causes a "sleeping function called from invalid context" BUG
when the hvc console flush path calls put_chars() with IRQs
disabled:

  BUG: sleeping function called from invalid context at \
       include/linux/sched/mm.h:322
  in_atomic(): 1, irqs_disabled(): 1, non_block: 0, pid: 416, \
	       name: test_progs
  ...
  Call Trace:
   __kmalloc_noprof+0x468/0x7c0
   alloc_buf.isra.0+0x42/0x260
   put_chars+0x1e5/0x2f0
   hvc_console_print+0x29d/0x6e0
   console_emit_next_record+0x21d/0x480
   console_flush_one_record+0x431/0x6a0
   console_unlock+0xda/0x1c0
   vprintk_emit+0x300/0x350

put_chars() correctly passes GFP_ATOMIC, but alloc_buf() silently
discards it and allocates with GFP_KERNEL, which may sleep.

Pass the caller-supplied gfp flag through to kmalloc_flex() so
that atomic callers get atomic allocations.

Fixes: 69050f8d6d07 ("treewide: Replace kmalloc with kmalloc_obj for non-scalar types")
Signed-off-by: Geliang Tang <tanggeliang@kylinos.cn>
---
 drivers/char/virtio_console.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/char/virtio_console.c b/drivers/char/virtio_console.c
index 62eecfa61646..7f6cbe851d1e 100644
--- a/drivers/char/virtio_console.c
+++ b/drivers/char/virtio_console.c
@@ -426,7 +426,7 @@ static struct port_buffer *alloc_buf(struct virtio_device *vdev, size_t buf_size
 	 * Allocate buffer and the sg list. The sg list array is allocated
 	 * directly after the port_buffer struct.
 	 */
-	buf = kmalloc_flex(*buf, sg, pages);
+	buf = kmalloc_flex(*buf, sg, pages, gfp);
 	if (!buf)
 		goto fail;
 
-- 
2.53.0


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

* Re: [PATCH] virtio_console: pass gfp flag through in alloc_buf()
  2026-08-29  1:20 [PATCH] virtio_console: pass gfp flag through in alloc_buf() Geliang Tang
@ 2026-09-05  1:14 ` Florian Westphal
  2026-09-05 11:37   ` Greg Kroah-Hartman
  2026-09-05 17:32 ` Greg Kroah-Hartman
  1 sibling, 1 reply; 4+ messages in thread
From: Florian Westphal @ 2026-09-05  1:14 UTC (permalink / raw)
  To: Geliang Tang
  Cc: Amit Shah, Arnd Bergmann, Greg Kroah-Hartman, Kees Cook,
	Geliang Tang, virtualization, Sungho Bae

Geliang Tang <geliang@kernel.org> wrote:

[ CC Sungho Bae ]

Ping, I still get this splat on mainline, this patch fixes things for
me, so

Tested-by: Florian Westphal <fw@strlen.de>

Could someone push this to Linus please (if not yet queued up)?

Thanks.

> put_chars() correctly passes GFP_ATOMIC, but alloc_buf() silently
> discards it and allocates with GFP_KERNEL, which may sleep.
> 
> Pass the caller-supplied gfp flag through to kmalloc_flex() so
> that atomic callers get atomic allocations.
> 
> Fixes: 69050f8d6d07 ("treewide: Replace kmalloc with kmalloc_obj for non-scalar types")


Hmm, probably:

Fixes: fc220d6be3c7 ("virtio_console: refactor __send_to_port() buffer ownership")

... as that adds "gfp_t gfp", but doesn't fix up kmalloc_flex() which
has GFP_KERNEL as a hidden argument via macro expansion.

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

* Re: [PATCH] virtio_console: pass gfp flag through in alloc_buf()
  2026-09-05  1:14 ` Florian Westphal
@ 2026-09-05 11:37   ` Greg Kroah-Hartman
  0 siblings, 0 replies; 4+ messages in thread
From: Greg Kroah-Hartman @ 2026-09-05 11:37 UTC (permalink / raw)
  To: Florian Westphal
  Cc: Geliang Tang, Amit Shah, Arnd Bergmann, Kees Cook, Geliang Tang,
	virtualization, Sungho Bae

On Sat, Sep 05, 2026 at 03:14:17AM +0200, Florian Westphal wrote:
> Geliang Tang <geliang@kernel.org> wrote:
> 
> [ CC Sungho Bae ]
> 
> Ping, I still get this splat on mainline, this patch fixes things for
> me, so
> 
> Tested-by: Florian Westphal <fw@strlen.de>
> 
> Could someone push this to Linus please (if not yet queued up)?
> 
> Thanks.
> 
> > put_chars() correctly passes GFP_ATOMIC, but alloc_buf() silently
> > discards it and allocates with GFP_KERNEL, which may sleep.
> > 
> > Pass the caller-supplied gfp flag through to kmalloc_flex() so
> > that atomic callers get atomic allocations.
> > 
> > Fixes: 69050f8d6d07 ("treewide: Replace kmalloc with kmalloc_obj for non-scalar types")
> 
> 
> Hmm, probably:
> 
> Fixes: fc220d6be3c7 ("virtio_console: refactor __send_to_port() buffer ownership")
> 
> ... as that adds "gfp_t gfp", but doesn't fix up kmalloc_flex() which
> has GFP_KERNEL as a hidden argument via macro expansion.

Sorry, this is on me, I've been swamped, will try to get this to him
"soon".

greg k-h

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

* Re: [PATCH] virtio_console: pass gfp flag through in alloc_buf()
  2026-08-29  1:20 [PATCH] virtio_console: pass gfp flag through in alloc_buf() Geliang Tang
  2026-09-05  1:14 ` Florian Westphal
@ 2026-09-05 17:32 ` Greg Kroah-Hartman
  1 sibling, 0 replies; 4+ messages in thread
From: Greg Kroah-Hartman @ 2026-09-05 17:32 UTC (permalink / raw)
  To: Geliang Tang
  Cc: Amit Shah, Arnd Bergmann, Kees Cook, Geliang Tang, virtualization

On Sat, Aug 29, 2026 at 09:20:39AM +0800, Geliang Tang wrote:
> From: Geliang Tang <tanggeliang@kylinos.cn>
> 
> The `gfp` parameter of alloc_buf() has been dead code since the
> function was introduced. The underlying allocation was always
> hardcoded to GFP_KERNEL - first via kmalloc(..., GFP_KERNEL), then
> via kmalloc_flex(*buf, sg, pages, GFP_KERNEL), and most recently
> via kmalloc_flex(*buf, sg, pages) which defaults to GFP_KERNEL.
> 
> This causes a "sleeping function called from invalid context" BUG
> when the hvc console flush path calls put_chars() with IRQs
> disabled:
> 
>   BUG: sleeping function called from invalid context at \
>        include/linux/sched/mm.h:322
>   in_atomic(): 1, irqs_disabled(): 1, non_block: 0, pid: 416, \
> 	       name: test_progs
>   ...
>   Call Trace:
>    __kmalloc_noprof+0x468/0x7c0
>    alloc_buf.isra.0+0x42/0x260
>    put_chars+0x1e5/0x2f0
>    hvc_console_print+0x29d/0x6e0
>    console_emit_next_record+0x21d/0x480
>    console_flush_one_record+0x431/0x6a0
>    console_unlock+0xda/0x1c0
>    vprintk_emit+0x300/0x350
> 
> put_chars() correctly passes GFP_ATOMIC, but alloc_buf() silently
> discards it and allocates with GFP_KERNEL, which may sleep.
> 
> Pass the caller-supplied gfp flag through to kmalloc_flex() so
> that atomic callers get atomic allocations.
> 
> Fixes: 69050f8d6d07 ("treewide: Replace kmalloc with kmalloc_obj for non-scalar types")
> Signed-off-by: Geliang Tang <tanggeliang@kylinos.cn>

Sorry for the delay, I took this earlier patch instead:
	https://lore.kernel.org/r/20260810-serial-v1-1-abbe51602c13@debian.org


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

end of thread, other threads:[~2026-09-05 17:33 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-08-29  1:20 [PATCH] virtio_console: pass gfp flag through in alloc_buf() Geliang Tang
2026-09-05  1:14 ` Florian Westphal
2026-09-05 11:37   ` Greg Kroah-Hartman
2026-09-05 17:32 ` Greg Kroah-Hartman

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