* Re: [PATCH] virtio_pci_modern: Use GFP_ATOMIC with spin_lock_irqsave held in virtqueue_exec_admin_cmd()
[not found] ` <20260413100013.32399-1-guojinhui.liam@bytedance.com>
@ 2026-05-13 12:10 ` Michael S. Tsirkin
0 siblings, 0 replies; 2+ messages in thread
From: Michael S. Tsirkin @ 2026-05-13 12:10 UTC (permalink / raw)
To: Jinhui Guo
Cc: Eugenio Pérez, Jason Wang, Jiri Pirko, Xuan Zhuo,
linux-kernel, stable, virtualization
On Mon, Apr 13, 2026 at 06:00:13PM +0800, Jinhui Guo wrote:
> On Mon, Apr 13, 2026 at 03:45:20 -0400, "Michael S. Tsirkin" wrote:
> > GFP_ATOMIC allocations can and will fail. If using them, one must
> > retry, not just propagate failures.
> > Or just switch admin_vq->lock to a mutex?
>
> Hi Michael,
>
> Thank you for the review.
>
> Regarding the suggestion to switch admin_vq->lock to a mutex:
>
> The virtqueue callback vp_modern_avq_done() holds admin_vq->lock and
> runs in an interrupt handler context, making it impractical to replace
> the spinlock with a mutex directly.
>
> I considered deferring the completion to a workqueue so we could safely
> use a mutex, but since this is a bug fix destined for stable@vger.kernel.org,
> doing so would introduce significant code churn (e.g., handling INIT_WORK,
> cancel_work_sync during cleanup, etc.) and increase the risk for backports.
This is not how we do kernel development here. Please fix the bug
upstream first then we will consider backporting strategies.
> Therefore, using GFP_ATOMIC with the existing spinlock seems to be the most
> minimal and safest approach for a fix.
>
> However, just replacing GFP_KERNEL with GFP_ATOMIC isn't entirely safe
> because of how virtqueue_add_sgs() handles allocation failures. If kmalloc()
> fails under memory pressure with GFP_ATOMIC, the function falls back to using
> direct descriptors. If there are not enough free direct descriptors, it
> ultimately returns -ENOSPC.
>
> In the current code, -ENOSPC is handled with a busy loop:
>
> if (ret == -ENOSPC) {
> spin_unlock_irqrestore(&admin_vq->lock, flags);
> cpu_relax();
> goto again;
> }
>
> If the -ENOSPC is actually caused by a GFP_ATOMIC allocation failure under
> memory pressure, this cpu_relax() loop will never yield the CPU to memory
> reclaim mechanisms (like kswapd), potentially leading to a soft lockup.
>
> To properly handle both actual queue-full conditions and GFP_ATOMIC failures,
> I propose replacing cpu_relax() with a sleep (e.g., usleep_range(10, 100)).
> This allows memory reclaim to run while we wait.
>
> I plan to send out a v2 patch with this modification:
>
> --- a/drivers/virtio/virtio_pci_modern.c
> +++ b/drivers/virtio/virtio_pci_modern.c
> @@ -101,11 +101,11 @@ static int virtqueue_exec_admin_cmd(struct virtio_pci_admin_vq *admin_vq,
> return -EIO;
>
> spin_lock_irqsave(&admin_vq->lock, flags);
> - ret = virtqueue_add_sgs(vq, sgs, out_num, in_num, cmd, GFP_KERNEL);
> + ret = virtqueue_add_sgs(vq, sgs, out_num, in_num, cmd, GFP_ATOMIC);
> if (ret < 0) {
> if (ret == -ENOSPC) {
> spin_unlock_irqrestore(&admin_vq->lock, flags);
> - cpu_relax();
> + usleep_range(10, 100);
> goto again;
> }
> goto unlock_err;
>
> Does this approach align with your expectations for the fix?
>
> Thanks,
> Jinhui
Nope.
I think we need to get out of peephole mode where we are just looking
at the warnings and "fix" them by 1 line tweaks and actually analyze the
codepaths. GFP is just for indirect allocations and VQ already
falls back to using direct when that fails.
The question is:
- what is going on with VQ ring state, can we actually get into a situation
where indirect would succeed but direct fails?
- how can callers either prevent failures or get notified when buffers
have been used?
And it is quite possible that the fix in the end is exactly your v1 but
with the analysis in the commit log explaining why this fixes the
problem and does not paper over it.
--
MST
^ permalink raw reply [flat|nested] 2+ messages in thread
* Re: [PATCH] virtio_pci_modern: Use GFP_ATOMIC with spin_lock_irqsave held in virtqueue_exec_admin_cmd()
[not found] ` <20260413122244.534-1-guojinhui.liam@bytedance.com>
@ 2026-05-13 12:34 ` Michael S. Tsirkin
0 siblings, 0 replies; 2+ messages in thread
From: Michael S. Tsirkin @ 2026-05-13 12:34 UTC (permalink / raw)
To: Jinhui Guo
Cc: David Laight, Eugenio Pérez, Jason Wang, Jiri Pirko,
Xuan Zhuo, linux-kernel, stable, virtualization
On Mon, Apr 13, 2026 at 08:22:44PM +0800, Jinhui Guo wrote:
> On Mon, Apr 13, 2026 at 10:17:59 +0100, David Laight wrote:
> > Or do the allocate before acquiring the lock (and free it not used
> > in the error path).
>
> Hi David,
>
> Thanks for the suggestion.
>
> Pre-allocating the memory outside the lock is indeed a good practice,
> but unfortunately it doesn't work in this specific virtqueue context.
>
> The kmalloc() in question is not happening at the virtqueue_exec_admin_cmd()
> level. Instead, it is deeply embedded inside virtqueue_add_sgs()
> (specifically, in functions like alloc_indirect_split() or
> virtqueue_add_indirect_packed()) to allocate indirect descriptors when
> multiple SG elements are provided.
>
> As a caller, we have no mechanism to pre-allocate this indirect descriptor
> memory and pass it down to virtqueue_add_sgs(). Furthermore, virtqueue_add_sgs()
> needs to atomically check the queue's num_free status, allocate the indirect
> table if necessary, and update the queue pointers. All these operations
> must be protected by admin_vq->lock to prevent concurrent admin command
> submissions from corrupting the virtqueue state.
>
> Therefore, allocating before acquiring the lock isn't feasible here, and
> replacing GFP_KERNEL with GFP_ATOMIC (with a proper sleepable retry upon
> failure) seems to be the more viable fix.
>
> Does this make sense?
>
> Thanks,
> Jinhui
it might be quite ok. what is missing is the analysis of whether we
can actually get this error and what happens then.
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2026-05-13 12:34 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
[not found] <20260413101759.6323fb68@pumpkin>
[not found] ` <20260413122244.534-1-guojinhui.liam@bytedance.com>
2026-05-13 12:34 ` [PATCH] virtio_pci_modern: Use GFP_ATOMIC with spin_lock_irqsave held in virtqueue_exec_admin_cmd() Michael S. Tsirkin
[not found] <20260413034046-mutt-send-email-mst@kernel.org>
[not found] ` <20260413100013.32399-1-guojinhui.liam@bytedance.com>
2026-05-13 12:10 ` Michael S. Tsirkin
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox