| 0/0 |
2026/07/27 01:35 |
flow |
assessment-security |
34m
Results: map[DenialOfService:true Explanation:Based on the analysis of the bug report and the kernel source code, here is the
assessment of the vulnerability:
### Root Cause Analysis
The bug is a **Use-After-Free (UAF)** in the `virtio_balloon` driver,
specifically occurring in the device probe error path.
During the execution of `virtballoon_probe()`, the driver initializes the
device's virtqueues by calling `init_vqs()`. This step registers the interrupt
callbacks for the virtqueues, including `stats_request()` for the statistics
virtqueue. Once `init_vqs()` completes, the device is capable of generating
interrupts.
If the device generates an interrupt at this stage, the `stats_request()`
callback is executed, which queues the `vb->update_balloon_stats_work` work item
onto the `system_freezable_wq` workqueue.
Following the virtqueue initialization, `virtballoon_probe()` performs several
other checks. For instance, if the device advertises the
`VIRTIO_BALLOON_F_FREE_PAGE_HINT` feature, the driver checks if the vring size
is at least 2:
```c
if (virtqueue_get_vring_size(vb->free_page_vq) < 2) {
err = -ENOSPC;
goto out_del_vqs;
}
```
If this check (or any subsequent memory allocation like `alloc_workqueue`)
fails, the driver jumps to the error handling path (`out_del_vqs`). The error
path calls `vdev->config->del_vqs(vdev)` to free the virtqueues and then
`kfree(vb)` to free the `virtio_balloon` structure.
Crucially, the probe error path **fails to call `cancel_work_sync()`** to cancel
any pending work items that might have been queued by early interrupts.
Consequently, the `update_balloon_stats_work` remains queued and will execute
shortly after. When `update_balloon_stats_func()` runs, it calls
`stats_handle_request()`, which attempts to read from the already freed
`vb->stats_vq` and passes it to `virtqueue_get_buf()`, resulting in the observed
Use-After-Free crash.
### Security Impact
* **Exploitable:** **Yes.** The bug is a Use-After-Free involving the
`virtio_balloon` and `vring_virtqueue` structures. An attacker who can trigger
this can control the timing of the UAF and potentially reallocate the freed
memory with controlled data, leading to memory corruption, control-flow
hijacking, and privilege escalation.
* **Denial of Service:** **Yes.** Accessing the freed memory will typically
cause a kernel panic, leading to a complete system crash.
* **VM Guest Trigger:** **Yes.** A malicious or compromised hypervisor (e.g.,
QEMU/KVM) can easily trigger this bug in the guest kernel by presenting a
virtio-balloon device, sending an early interrupt, and intentionally providing a
vring size of 1 to force the probe to fail.
* **Peripheral Trigger:** **Yes.** A malicious physical PCIe device (e.g.,
plugged in via Thunderbolt/USB4) can present itself as a `virtio-pci` balloon
device. By sending an early MSI-X interrupt and advertising a malformed vring
size, the malicious peripheral can deterministically trigger this UAF on the
host machine.
* **Accessible From Unprivileged Processes / User Namespaces:** **No.**
Triggering this vulnerability requires the ability to plug in a physical device,
control the hypervisor, or inject faults (which Syzkaller does as root).
Unprivileged users cannot interact with the virtio probe sequence directly. Exploitable:true FilesystemTrigger:false NetworkTrigger:false PeripheralTrigger:true RemoteTrigger:false Unprivileged:false UserNamespace:false VMGuestTrigger:true VMHostTrigger:false]
|
| 1/1 |
2026/07/27 01:35 |
action |
syz-repro-to-c-repro |
0m
Results:
|
| 2/1 |
2026/07/27 01:35 |
action |
kernel-checkouter |
0m
Results: map[KernelSrc:/app/workdir/cache/src/ea1ec46165a34622b3d5769ce1a7084d96856f42]
|
| 3/1 |
2026/07/27 01:35 |
action |
kernel-builder |
13m
Results: map[KernelObj:/app/workdir/cache/build/ba1ffee9fc6f17d2848c1e2328c045af7273beb0]
|
| 4/1 |
2026/07/27 01:48 |
action |
codesearch-prepare |
14m
Results: map[Index:codesearch-index]
|
| 5/1 |
2026/07/27 02:02 |
agent |
expert |
7m
Model:
Results: map[DenialOfService:true Exploitable:true FilesystemTrigger:false NetworkTrigger:false PeripheralTrigger:true RemoteTrigger:false Unprivileged:false UserNamespace:false VMGuestTrigger:true VMHostTrigger:false]
Instruction: You are an experienced Linux kernel security engineer. Your task is to analyze given kernel bug report
and determine its security impact based on the following dimensions.
Use the provided tools to examine the source code, check for capability checks (e.g., capable(), ns_capable()),
and understand the nature of the bug. Analyze the given kernel build and configuration.
You can check the kernel config by grepping ".config" file; you can check kernel cmdline by grepping
".config" file for "CONFIG_CMDLINE=". Assume sysctl parameters have default values.
But analyze for the corresponding production build w/o debugging tools enabled (like KASAN, KMSAN, UBSAN).
Try different strategies when analyzing the bug:
- think of ways in which the vulnerable code is unreachable
- or the other way around: try to come up with different ideas of how an unprivileged user can reach the bug
If still unsure err on the side of the bug being non-exploitable/not-accessible.
In the final reply, provide a reasoning for your assessment.
Analysis dimensions:
* Exploitable:
Determine if the bug can result in memory corruption, elevated privileges, or an information leak.
Memory safety issues are almost always exploitable (KASAN or UBSAN reports for use-after-free, out-of-bounds;
refcounting issues, corrupted lists, etc). When kernel is crashing on a completely wild pointer access
(e.g. user-space address, or non-canonical address, but not on NULL or address corresponding to KASAN shadow
for NULL address), including both data accesses and control transfers, that also usually implies possibility
of exploitation. Such reports usually say "unable to handle kernel paging request".
Uses of uninitialized values detected by KMSAN may be exploitable b/c attacker frequently can affect uninit
values with spraying techniques. However, for these exploitability depends on how exactly the uninit value
is used in the code, and what it affects.
Information leaks are exploitable on their own and should be classified as such. A bug that copies kernel
memory contents to userspace (e.g. an out-of-bounds read whose result is returned to the caller, or
uninitialized stack/heap bytes written to a user buffer) is exploitable: it can reveal kernel pointer
values and defeat KASLR, expose sensitive data such as cryptographic keys or other processes' memory, and
serves as a necessary building block in most modern kernel privilege-escalation exploit chains. Do not classify
an information leak as non-exploitable solely because it does not directly cause a memory write or control-flow
hijack; the leak itself is the exploit primitive.
Think of what happens after the bug is triggered. Some bugs cause kernel panic and halt execution,
they are harder to exploit. For example, BUG reports halts the kernel. However, WARNING reports don't halt
execution in production builds. Debug bug detection tools (like KASAN, KMSAN, KCSAN, UBSAN) are also not enabled
in production builds, so attacker can freely exploit these bugs w/o being detected by these tools.
If you see an integer overflow, think how the overflowed value used later (if it's used as allocation size,
or an array index). If you see an out-of-bounds read, think if it's followed by an out-of-bounds write as well.
Some KCSAN data-races may be exploitable by skilled attackers as well. Think what data structures got corrupted
as the result of data races and how. However, note that kernel has lots of "benign" data races that don't lead
to any runtime misbehavior at all.
* Denial Of Service:
Determine if the bug can result in denial-of-service. Most bugs can, since they cause system crash,
hangs, deadlocks, or resource leaks. This is mostly applicable to WARNING bugs that won't cause system crash
in production. For these think what will be consequences of the violation of the kernel assumptions flagged
by the WARNING. In some cases the unexpected condition is also properly handled by the normal control flow
(e.g. with "if (WARN_ON(...))"), these won't cause denial-of-service. If the condition is not handled,
then it may or may not cause denial-of-service.
* Accessible From Unprivileged Processes:
Determine if the bug can be reached from a typical (non-root) user process that does NOT have any special capabilities
(like CAP_SYS_ADMIN, CAP_NET_ADMIN, CAP_NET_RAW, CAP_PERFMON) or access to device nodes restricted to root.
Assume that unprivileged_bpf_disabled=1, that is eBPF loading is not accessible. However, cBPF (classical BPF)
is still accessible to non-root processes.
Assume that user namespaces are not accessible, that is, the process cannot get the mentioned capabilities even
within a new user namespace (checked by ns_capable() function in the kernel sources).
* Accessible From User Namespaces:
Determine if the bug can be reached within a user-namespace where the process has all capabilities
(including CAP_SYS_ADMIN, CAP_NET_ADMIN, CAP_NET_RAW, CAP_PERFMON). Such capabilities are checked with ns_capable()
function in the kernel sources.
* VM Guest Trigger:
Determine if the bug can be triggered from the context of a typical KVM guest (e.g., set up by a QEMU VMM).
Consider accesses to standard Linux host paravirtualized features (virtio-blk, virtio-net, etc.),
and handling of VM exits in the KVM code.
* VM Host Trigger in The Confidential Computing Context:
Determine if the bug can be triggered in a confidential computing guest kernel from the context of a KVM host.
Consider access to standard Linux guest paravirtualized features (virtio-blk, virtio-net, etc.).
* Ethernet Network Trigger:
Determine if the bug can be triggered by processing ingress network Ethernet traffic, either directly (network stack)
or via drivers exposed to network data.
* Other Remote Trigger:
Determine if the bug can be triggered by processing remote traffic other than Ethernet (Wifi, Bluetooth, NFC, etc).
* Peripheral Trigger:
Determine if the bug can be triggered via an untrusted peripheral device that can be physically plugged
into a system, such as a USB device or a niche hardware driver handling external hardware inputs.
This is particularly important for mobile and desktop environments where users can plug in unknown devices.
* Malicious Filesystem Trigger:
Determine if the bug can be triggered by the kernel mounting and parsing a malicious filesystem image.
This is highly critical for Desktop and Mobile environments where external media or downloaded images
might be auto-mounted.
Don't make assumptions about the kernel source code (it may be different from what you assume it is).
Extensively use the provided code access tools (codesearch-*, git-*, grepper, etc)
to examine the actual source code, and confirm any assumptions.
Prefer calling several tools at the same time to save round-trips.
Use set-results tool to provide results of the analysis.
It must be called exactly once before the final reply.
Ignore results of this tool.
Prompt:
The kernel bug report is:
==================================================================
BUG: KFENCE: use-after-free read in virtqueue_get_buf_ctx+0x42/0x2150 drivers/virtio/virtio_ring.c:3129
Use-after-free read at 0xffff88823be22048 (in kfence-#16):
virtqueue_get_buf_ctx+0x42/0x2150 drivers/virtio/virtio_ring.c:3129
stats_handle_request drivers/virtio/virtio_balloon.c:450 [inline]
update_balloon_stats_func+0x121/0x300 drivers/virtio/virtio_balloon.c:547
process_one_work kernel/workqueue.c:3322 [inline]
process_scheduled_works+0xa8e/0x14e0 kernel/workqueue.c:3405
worker_thread+0xa47/0xfb0 kernel/workqueue.c:3486
kthread+0x388/0x470 kernel/kthread.c:436
ret_from_fork+0x514/0xb70 arch/x86/kernel/process.c:158
ret_from_fork_asm+0x1a/0x30 arch/x86/entry/entry_64.S:245
kfence-#16: 0xffff88823be22f00-0xffff88823be22fef, size=240, cache=skbuff_head_cache
allocated by task 13198 on cpu 0 at 1787.700554s (0.398917s ago):
__alloc_skb+0x1d7/0x7a0 net/core/skbuff.c:704
alloc_skb include/linux/skbuff.h:1384 [inline]
nsim_dev_trap_skb_build drivers/net/netdevsim/dev.c:819 [inline]
nsim_dev_trap_report drivers/net/netdevsim/dev.c:876 [inline]
nsim_dev_trap_report_work+0x2b1/0xcb0 drivers/net/netdevsim/dev.c:922
process_one_work kernel/workqueue.c:3322 [inline]
process_scheduled_works+0xa8e/0x14e0 kernel/workqueue.c:3405
worker_thread+0xa47/0xfb0 kernel/workqueue.c:3486
kthread+0x388/0x470 kernel/kthread.c:436
ret_from_fork+0x514/0xb70 arch/x86/kernel/process.c:158
ret_from_fork_asm+0x1a/0x30 arch/x86/entry/entry_64.S:245
freed by task 13198 on cpu 0 at 1787.704288s (0.395347s ago):
nsim_dev_trap_report drivers/net/netdevsim/dev.c:892 [inline]
nsim_dev_trap_report_work+0x879/0xcb0 drivers/net/netdevsim/dev.c:922
process_one_work kernel/workqueue.c:3322 [inline]
process_scheduled_works+0xa8e/0x14e0 kernel/workqueue.c:3405
worker_thread+0xa47/0xfb0 kernel/workqueue.c:3486
kthread+0x388/0x470 kernel/kthread.c:436
ret_from_fork+0x514/0xb70 arch/x86/kernel/process.c:158
ret_from_fork_asm+0x1a/0x30 arch/x86/entry/entry_64.S:245
CPU: 0 UID: 0 PID: 25534 Comm: kworker/0:1 Tainted: G L syzkaller #0 PREEMPT_{RT,(full)}
Tainted: [L]=SOFTLOCKUP
Hardware name: Google Google Compute Engine/Google Compute Engine, BIOS Google 07/16/2026
Workqueue: events_freezable update_balloon_stats_func
RIP: 0010:virtqueue_get_buf_ctx+0x42/0x2150 drivers/virtio/virtio_ring.c:3129
Code: 48 89 fb 49 bd 00 00 00 00 00 fc ff df e8 26 18 87 fc 4c 8d 73 48 4c 89 f0 48 c1 e8 03 42 0f b6 04 28 84 c0 0f 85 d5 15 00 00 <45> 8b 36 4c 89 f7 48 c7 c6 e0 de a8 8e e8 0c 1e 87 fc 41 83 fe 01
RSP: 0018:ffffc900110d7960 EFLAGS: 00010246
RAX: 0000000000000000 RBX: ffff88823be22000 RCX: ffff88803a8e8000
RDX: 0000000000000000 RSI: 0000000000000000 RDI: 0000000000000000
RBP: 0000000000000000 R08: ffffffff8de1c71f R09: 0000000000000000
R10: fffff5200221aeb8 R11: fffffbfff1bc38e4 R12: 1ffff11004063d32
R13: dffffc0000000000 R14: ffff88823be22048 R15: ffffc900110d7a80
FS: 0000000000000000(0000) GS:ffff888125c42000(0000) knlGS:0000000000000000
CS: 0010 DS: 0000 ES: 0000 CR0: 0000000080050033
CR2: ffff88823be22048 CR3: 0000000027530000 CR4: 00000000003526f0
Call Trace:
<TASK>
stats_handle_request drivers/virtio/virtio_balloon.c:450 [inline]
update_balloon_stats_func+0x121/0x300 drivers/virtio/virtio_balloon.c:547
process_one_work kernel/workqueue.c:3322 [inline]
process_scheduled_works+0xa8e/0x14e0 kernel/workqueue.c:3405
worker_thread+0xa47/0xfb0 kernel/workqueue.c:3486
kthread+0x388/0x470 kernel/kthread.c:436
ret_from_fork+0x514/0xb70 arch/x86/kernel/process.c:158
ret_from_fork_asm+0x1a/0x30 arch/x86/entry/entry_64.S:245
</TASK>
==================================================================
----------------
Code disassembly (best guess):
0: 48 89 fb mov %rdi,%rbx
3: 49 bd 00 00 00 00 00 movabs $0xdffffc0000000000,%r13
a: fc ff df
d: e8 26 18 87 fc call 0xfc871838
12: 4c 8d 73 48 lea 0x48(%rbx),%r14
16: 4c 89 f0 mov %r14,%rax
19: 48 c1 e8 03 shr $0x3,%rax
1d: 42 0f b6 04 28 movzbl (%rax,%r13,1),%eax
22: 84 c0 test %al,%al
24: 0f 85 d5 15 00 00 jne 0x15ff
* 2a: 45 8b 36 mov (%r14),%r14d <-- trapping instruction
2d: 4c 89 f7 mov %r14,%rdi
30: 48 c7 c6 e0 de a8 8e mov $0xffffffff8ea8dee0,%rsi
37: e8 0c 1e 87 fc call 0xfc871e48
3c: 41 83 fe 01 cmp $0x1,%r14d
Reply: Based on the analysis of the bug report and the kernel source code, here is the assessment of the vulnerability:
### Root Cause Analysis
The bug is a **Use-After-Free (UAF)** in the `virtio_balloon` driver, specifically occurring in the device probe error path.
During the execution of `virtballoon_probe()`, the driver initializes the device's virtqueues by calling `init_vqs()`. This step registers the interrupt callbacks for the virtqueues, including `stats_request()` for the statistics virtqueue. Once `init_vqs()` completes, the device is capable of generating interrupts.
If the device generates an interrupt at this stage, the `stats_request()` callback is executed, which queues the `vb->update_balloon_stats_work` work item onto the `system_freezable_wq` workqueue.
Following the virtqueue initialization, `virtballoon_probe()` performs several other checks. For instance, if the device advertises the `VIRTIO_BALLOON_F_FREE_PAGE_HINT` feature, the driver checks if the vring size is at least 2:
```c
if (virtqueue_get_vring_size(vb->free_page_vq) < 2) {
err = -ENOSPC;
goto out_del_vqs;
}
```
If this check (or any subsequent memory allocation like `alloc_workqueue`) fails, the driver jumps to the error handling path (`out_del_vqs`). The error path calls `vdev->config->del_vqs(vdev)` to free the virtqueues and then `kfree(vb)` to free the `virtio_balloon` structure.
Crucially, the probe error path **fails to call `cancel_work_sync()`** to cancel any pending work items that might have been queued by early interrupts. Consequently, the `update_balloon_stats_work` remains queued and will execute shortly after. When `update_balloon_stats_func()` runs, it calls `stats_handle_request()`, which attempts to read from the already freed `vb->stats_vq` and passes it to `virtqueue_get_buf()`, resulting in the observed Use-After-Free crash.
### Security Impact
* **Exploitable:** **Yes.** The bug is a Use-After-Free involving the `virtio_balloon` and `vring_virtqueue` structures. An attacker who can trigger this can control the timing of the UAF and potentially reallocate the freed memory with controlled data, leading to memory corruption, control-flow hijacking, and privilege escalation.
* **Denial of Service:** **Yes.** Accessing the freed memory will typically cause a kernel panic, leading to a complete system crash.
* **VM Guest Trigger:** **Yes.** A malicious or compromised hypervisor (e.g., QEMU/KVM) can easily trigger this bug in the guest kernel by presenting a virtio-balloon device, sending an early interrupt, and intentionally providing a vring size of 1 to force the probe to fail.
* **Peripheral Trigger:** **Yes.** A malicious physical PCIe device (e.g., plugged in via Thunderbolt/USB4) can present itself as a `virtio-pci` balloon device. By sending an early MSI-X interrupt and advertising a malformed vring size, the malicious peripheral can deterministically trigger this UAF on the host machine.
* **Accessible From Unprivileged Processes / User Namespaces:** **No.** Triggering this vulnerability requires the ability to plug in a physical device, control the hypervisor, or inject faults (which Syzkaller does as root). Unprivileged users cannot interact with the virtio probe sequence directly.
|
| 6/2 |
2026/07/27 02:02 |
llm |
expert |
0m
Model:
Tokens:
input: 8207
output: 39
thoughts: 235
Thoughts: **Debugging a Use-After-Free in the Virtio Balloon Driver**
Okay, so I've got a bug report that points to a use-after-free read within `virtqueue_get_buf_ctx`. The call stack goes through `stats_handle_request` in `drivers/virtio/virtio_balloon.c`. Immediately, my brain starts piecing together the likely culprits. The report mentions the freed object is a `skbuff_head_cache`, allocated and freed by `nsim_dev_trap_skb_build` / `nsim_dev_trap_report`.
Wait a minute, `netdevsim` is involved in the allocation/free lifecycle of the skb. It appears the root cause of the UAF is occurring in `virtqueue_get_buf_ctx`, which immediately suggests a wild pointer, or perhaps some form of memory corruption. Could the `virtqueue` structure itself be allocated from a slab cache that's being reused prematurely? That's a classic scenario for this kind of problem.
Crucially, though, I need to focus on `virtqueue` itself because the pointer is passed into `virtqueue_get_buf_ctx`. I should really check `stats_handle_request` in `drivers/virtio/virtio_balloon.c` more closely and ensure that the right `virtqueue` is being passed at every stage.
My next steps are clear: I'll need to dig into `drivers/virtio/virtio_balloon.c` and `drivers/virtio/virtio_ring.c` with tools. I'm going to need to trace and inspect the data to follow the allocation and free sequences to confirm the memory corruption root cause. Let's see what's really happening.
|
| 7/2 |
2026/07/27 02:02 |
tool |
read-file |
0m
Args: map[File:drivers/virtio/virtio_balloon.c FirstLine:440 LineCount:50]
Results: map[Contents: 440:
441: static void stats_handle_request(struct virtio_balloon *vb)
442: {
443: struct virtqueue *vq;
444: struct scatterlist sg;
445: unsigned int len, num_stats;
446:
447: num_stats = update_balloon_stats(vb);
448:
449: vq = vb->stats_vq;
450: if (!virtqueue_get_buf(vq, &len))
451: return;
452: sg_init_one(&sg, vb->stats, sizeof(vb->stats[0]) * num_stats);
453: virtqueue_add_outbuf(vq, &sg, 1, vb, GFP_KERNEL);
454: virtqueue_kick(vq);
455: }
456:
457: static inline s64 towards_target(struct virtio_balloon *vb)
458: {
459: s64 target;
460: u32 num_pages;
461:
462: /* Legacy balloon config space is LE, unlike all other devices. */
463: virtio_cread_le(vb->vdev, struct virtio_balloon_config, num_pages,
464: &num_pages);
465:
466: /*
467: * Aligned up to guest page size to avoid inflating and deflating
468: * balloon endlessly.
469: */
470: target = ALIGN(num_pages, VIRTIO_BALLOON_PAGES_PER_PAGE);
471: return target - vb->num_pages;
472: }
473:
474: /* Gives back @num_to_return blocks of free pages to mm. */
475: static unsigned long return_free_pages_to_mm(struct virtio_balloon *vb,
476: unsigned long num_to_return)
477: {
478: unsigned long num_returned = 0;
479: struct page *page, *next;
480:
481: if (unlikely(!num_to_return))
482: return 0;
483:
484: spin_lock_irq(&vb->free_page_list_lock);
485:
486: list_for_each_entry_safe(page, next, &vb->free_page_list, lru) {
487: list_del(&page->lru);
488: __free_pages(page, VIRTIO_BALLOON_HINT_BLOCK_ORDER);
489: if (++num_returned == num_to_return)
]
|
| 8/2 |
2026/07/27 02:02 |
llm |
expert |
0m
Model:
Tokens:
input: 9411
output: 40
thoughts: 76
Thoughts: Here's my analysis of the situation:
**Investigating a Use-After-Free in the Virtio Balloon Driver**
Okay, so we've got a use-after-free (UAF) bug. The stack trace points directly to `virtqueue_get_buf_ctx`, which is being called from `stats_handle_request` within the `virtio_balloon.c` driver. This means something's going wrong with the buffer context retrieval within the VirtIO queue handling, specifically when dealing with balloon statistics requests. The UAF implies that we're accessing a memory region after it's been freed, which is, of course, a nasty one. The culprit is most likely something related to a `virtqueue` object itself or some structure it manages. Let's dig into `virtqueue_get_buf_ctx` and see exactly what's going on within that function and its interactions. Time to trace the data flow and memory management surrounding that function call.
|
| 9/2 |
2026/07/27 02:02 |
tool |
read-file |
0m
Args: map[File:drivers/virtio/virtio_ring.c FirstLine:3119 LineCount:50]
Results: map[Contents:3119: * operations at the same time (except where noted).
3120: *
3121: * Returns NULL if there are no used buffers, or the "data" token
3122: * handed to virtqueue_add_*().
3123: */
3124: void *virtqueue_get_buf_ctx(struct virtqueue *_vq, unsigned int *len,
3125: void **ctx)
3126: {
3127: struct vring_virtqueue *vq = to_vvq(_vq);
3128:
3129: return VIRTQUEUE_CALL(vq, get, len, ctx);
3130: }
3131: EXPORT_SYMBOL_GPL(virtqueue_get_buf_ctx);
3132:
3133: void *virtqueue_get_buf(struct virtqueue *_vq, unsigned int *len)
3134: {
3135: return virtqueue_get_buf_ctx(_vq, len, NULL);
3136: }
3137: EXPORT_SYMBOL_GPL(virtqueue_get_buf);
3138: /**
3139: * virtqueue_disable_cb - disable callbacks
3140: * @_vq: the struct virtqueue we're talking about.
3141: *
3142: * Note that this is not necessarily synchronous, hence unreliable and only
3143: * useful as an optimization.
3144: *
3145: * Unlike other operations, this need not be serialized.
3146: */
3147: void virtqueue_disable_cb(struct virtqueue *_vq)
3148: {
3149: struct vring_virtqueue *vq = to_vvq(_vq);
3150:
3151: VOID_VIRTQUEUE_CALL(vq, disable_cb);
3152: }
3153: EXPORT_SYMBOL_GPL(virtqueue_disable_cb);
3154:
3155: /**
3156: * virtqueue_enable_cb_prepare - restart callbacks after disable_cb
3157: * @_vq: the struct virtqueue we're talking about.
3158: *
3159: * This re-enables callbacks; it returns current queue state
3160: * in an opaque unsigned value. This value should be later tested by
3161: * virtqueue_poll, to detect a possible race between the driver checking for
3162: * more work, and enabling callbacks.
3163: *
3164: * Caller must ensure we don't call this with other virtqueue
3165: * operations at the same time (except where noted).
3166: */
3167: unsigned int virtqueue_enable_cb_prepare(struct virtqueue *_vq)
3168: {
]
|
| 10/2 |
2026/07/27 02:02 |
llm |
expert |
0m
Model:
Tokens:
input: 10434
output: 20
thoughts: 597
Thoughts: **Debugging a Use-After-Free in virtio_balloon**
Okay, so I'm looking at a nasty bug: a use-after-free (UAF) read. The trace shows it happening within `virtqueue_get_buf_ctx`, which is called from `virtqueue_get_buf`, and that, in turn, is part of `stats_handle_request` in `virtio_balloon.c`. The core of the problem seems to be that an `skbuff_head_cache`, created by `nsim_dev_trap_skb_build` and later freed by `nsim_dev_trap_report`, is somehow being accessed after it's been released. That seems…unusual. Why is a virtqueue even touching an skb?
Let's drill down. The trace tells me that `virtqueue_get_buf_ctx` is accessing `vq`, and `vq` is actually `vb->stats_vq`. Aha, so the UAF is on the `vq` itself – interesting. I need to be sure; let's verify within `virtqueue_get_buf_ctx`:
`struct vring_virtqueue *vq = to_vvq(_vq);` and then `VIRTQUEUE_CALL(vq, get, len, ctx);`
Okay, so the issue is either on `vq` or `_vq`. This crash happens at `virtqueue_get_buf_ctx+0x42/0x2150`. Looking at the assembly:
```assembly
0: 48 89 fb mov %rdi,%rbx
3: 49 bd 00 00 00 00 00 movabs $0xdffffc0000000000,%r13
a: fc ff df
d: e8 26 18 87 fc call 0xfc871838
12: 4c 8d 73 48 lea 0x48(%rbx),%r14
16: 4c 89 f0 mov %r14,%rax
19: 48 c1 e8 03 shr $0x3,%rax
1d: 42 0f b6 04 28 movzbl (%rax,%r13,1),%eax
22: 84 c0 test %al,%al
24: 0f 85 d5 15 00 00 jne 0x15ff
* 2a: 45 8b 36 mov (%r14),%r14d <-- trapping instruction
```
`%rbx` is assigned `%rdi`, which, from the function signature, is `_vq`. The crashing instruction at `virtqueue_get_buf_ctx+0x2a` is accessing `0x48(%rbx)`, which is an offset from `_vq`. So the UAF read is occurring because the virtqueue data structure pointed to by `_vq` has been corrupted or freed. I need to understand what lives at offset `0x48` within `struct vring_virtqueue` or `struct virtqueue` to understand what's being read when this crash happens. Time to dive into the data structure definitions.
|
| 11/2 |
2026/07/27 02:02 |
tool |
codesearch-struct-layout |
0m
Args:
Results: map[Fields:[map[Name:list OffsetBits:0 SizeBits:128] map[Name:callback OffsetBits:128 SizeBits:64] map[Name:name OffsetBits:192 SizeBits:64] map[Name:vdev OffsetBits:256 SizeBits:64] map[Name:index OffsetBits:320 SizeBits:32] map[Name:num_free OffsetBits:352 SizeBits:32] map[Name:num_max OffsetBits:384 SizeBits:32] map[Name:reset OffsetBits:416 SizeBits:8] map[Name:priv OffsetBits:448 SizeBits:64]]]
|
| 12/2 |
2026/07/27 02:02 |
llm |
expert |
0m
Model:
Tokens:
input: 11419
output: 23
thoughts: 0
|
| 13/2 |
2026/07/27 02:02 |
tool |
codesearch-struct-layout |
0m
Args: map[Name:vring_virtqueue]
Results: map[Fields:[map[Name:vq OffsetBits:0 SizeBits:512] map[Name:use_map_api OffsetBits:512 SizeBits:8] map[Name:weak_barriers OffsetBits:520 SizeBits:8] map[Name:broken OffsetBits:528 SizeBits:8] map[Name:indirect OffsetBits:536 SizeBits:8] map[Name:event OffsetBits:544 SizeBits:8] map[Name:layout OffsetBits:576 SizeBits:32] map[Name:free_head OffsetBits:608 SizeBits:32] map[Name:batch_last OffsetBits:640 SizeBits:64] map[Name:num_added OffsetBits:704 SizeBits:32] map[Name:last_used_idx OffsetBits:736 SizeBits:16] map[Name:last_used OffsetBits:752 SizeBits:16] map[Name:event_triggered OffsetBits:768 SizeBits:8] map[Name: OffsetBits:832 SizeBits:768] map[Name:notify OffsetBits:1600 SizeBits:64] map[Name:we_own_ring OffsetBits:1664 SizeBits:8] map[Name:map OffsetBits:1728 SizeBits:64]]]
|
| 14/2 |
2026/07/27 02:02 |
llm |
expert |
0m
Model:
Tokens:
input: 11799
output: 39
thoughts: 0
|
| 15/2 |
2026/07/27 02:02 |
tool |
read-file |
0m
Args: map[File:drivers/virtio/virtio_balloon.c FirstLine:530 LineCount:50]
Results: map[Contents: 530: static void update_balloon_size(struct virtio_balloon *vb)
531: {
532: u32 actual = vb->num_pages;
533:
534: /* Legacy balloon config space is LE, unlike all other devices. */
535: virtio_cwrite_le(vb->vdev, struct virtio_balloon_config, actual,
536: &actual);
537: }
538:
539: static void update_balloon_stats_func(struct work_struct *work)
540: {
541: struct virtio_balloon *vb;
542:
543: vb = container_of(work, struct virtio_balloon,
544: update_balloon_stats_work);
545:
546: process_wakeup_event(vb, VIRTIO_BALLOON_WAKEUP_SIGNAL_STATS);
547: stats_handle_request(vb);
548: finish_wakeup_event(vb);
549: }
550:
551: static void update_balloon_size_func(struct work_struct *work)
552: {
553: struct virtio_balloon *vb;
554: s64 diff;
555:
556: vb = container_of(work, struct virtio_balloon,
557: update_balloon_size_work);
558:
559: process_wakeup_event(vb, VIRTIO_BALLOON_WAKEUP_SIGNAL_ADJUST);
560:
561: diff = towards_target(vb);
562:
563: if (diff) {
564: if (diff > 0)
565: diff -= fill_balloon(vb, diff);
566: else
567: diff += leak_balloon(vb, -diff);
568: update_balloon_size(vb);
569: }
570:
571: if (diff)
572: queue_work(system_freezable_wq, work);
573: else
574: finish_wakeup_event(vb);
575: }
576:
577: static int init_vqs(struct virtio_balloon *vb)
578: {
579: struct virtqueue_info vqs_info[VIRTIO_BALLOON_VQ_MAX] = {};
]
|
| 16/2 |
2026/07/27 02:02 |
llm |
expert |
0m
Model:
Tokens:
input: 12558
output: 25
thoughts: 0
|
| 17/2 |
2026/07/27 02:03 |
tool |
codesearch-find-references |
0m
Error:
Args: map[Name:update_balloon_stats_work]
Results: map[References:<nil> TruncatedOutput:false]
|
| 18/2 |
2026/07/27 02:03 |
llm |
expert |
0m
Model:
Tokens:
input: 12604
output: 22
thoughts: 0
|
| 19/2 |
2026/07/27 02:03 |
tool |
codesearch-struct-layout |
0m
Args:
Results: map[Fields:[map[Name:vdev OffsetBits:0 SizeBits:64] map[Name:inflate_vq OffsetBits:64 SizeBits:64] map[Name:deflate_vq OffsetBits:128 SizeBits:64] map[Name:stats_vq OffsetBits:192 SizeBits:64] map[Name:free_page_vq OffsetBits:256 SizeBits:64] map[Name:balloon_wq OffsetBits:320 SizeBits:64] map[Name:report_free_page_work OffsetBits:384 SizeBits:576] map[Name:update_balloon_stats_work OffsetBits:960 SizeBits:576] map[Name:update_balloon_size_work OffsetBits:1536 SizeBits:576] map[Name:stop_update_lock OffsetBits:2112 SizeBits:1024] map[Name:stop_update OffsetBits:3136 SizeBits:8] map[Name:config_read_bitmap OffsetBits:3200 SizeBits:64] map[Name:free_page_list OffsetBits:3264 SizeBits:128] map[Name:free_page_list_lock OffsetBits:3392 SizeBits:1024] map[Name:num_free_page_blocks OffsetBits:4416 SizeBits:64] map[Name:cmd_id_received_cache OffsetBits:4480 SizeBits:32] map[Name:cmd_id_active OffsetBits:4512 SizeBits:32] map[Name:cmd_id_stop OffsetBits:4544 SizeBits:32] map[Name:acked OffsetBits:4608 SizeBits:1152] map[Name:num_pages OffsetBits:5760 SizeBits:32] map[Name:vb_dev_info OffsetBits:5824 SizeBits:320] map[Name:balloon_lock OffsetBits:6144 SizeBits:1024] map[Name:num_pfns OffsetBits:7168 SizeBits:32] map[Name:pfns OffsetBits:7200 SizeBits:8192] map[Name:stats OffsetBits:15392 SizeBits:1280] map[Name:shrinker OffsetBits:16704 SizeBits:64] map[Name:oom_nb OffsetBits:16768 SizeBits:192] map[Name:reporting_vq OffsetBits:16960 SizeBits:64] map[Name:pr_dev_info OffsetBits:17024 SizeBits:1536] map[Name:wakeup_lock OffsetBits:18560 SizeBits:1024] map[Name:processing_wakeup_event OffsetBits:19584 SizeBits:8] map[Name:wakeup_signal_mask OffsetBits:19616 SizeBits:32]]]
|
| 20/2 |
2026/07/27 02:03 |
llm |
expert |
0m
Model:
Tokens:
input: 13400
output: 30
thoughts: 0
|
| 21/2 |
2026/07/27 02:03 |
tool |
codesearch-find-references |
0m
Args: map[Name:virtio_balloon::update_balloon_stats_work]
Results: map[References:[map[ReferenceKind:takes-address-of ReferencingEntityKind:function ReferencingEntityName:stats_request SourceFile:drivers/virtio/virtio_balloon.c SourceLine:436] map[ReferenceKind:reads ReferencingEntityKind:function ReferencingEntityName:update_balloon_stats_func SourceFile:drivers/virtio/virtio_balloon.c SourceLine:543] map[ReferenceKind:takes-address-of ReferencingEntityKind:function ReferencingEntityName:virtballoon_probe SourceFile:drivers/virtio/virtio_balloon.c SourceLine:936] map[ReferenceKind:takes-address-of ReferencingEntityKind:function ReferencingEntityName:virtballoon_probe SourceFile:drivers/virtio/virtio_balloon.c SourceLine:936] map[ReferenceKind:takes-address-of ReferencingEntityKind:function ReferencingEntityName:virtballoon_probe SourceFile:drivers/virtio/virtio_balloon.c SourceLine:936] map[ReferenceKind:takes-address-of ReferencingEntityKind:function ReferencingEntityName:virtballoon_probe SourceFile:drivers/virtio/virtio_balloon.c SourceLine:936] map[ReferenceKind:takes-address-of ReferencingEntityKind:function ReferencingEntityName:virtballoon_probe SourceFile:drivers/virtio/virtio_balloon.c SourceLine:936] map[ReferenceKind:takes-address-of ReferencingEntityKind:function ReferencingEntityName:virtballoon_remove SourceFile:drivers/virtio/virtio_balloon.c SourceLine:1112]] TruncatedOutput:false]
|
| 22/2 |
2026/07/27 02:03 |
llm |
expert |
0m
Model:
Tokens:
input: 13897
output: 39
thoughts: 0
|
| 23/2 |
2026/07/27 02:03 |
tool |
read-file |
0m
Args: map[File:drivers/virtio/virtio_balloon.c FirstLine:420 LineCount:50]
Results: map[Contents: 420: /*
421: * While most virtqueues communicate guest-initiated requests to the hypervisor,
422: * the stats queue operates in reverse. The driver initializes the virtqueue
423: * with a single buffer. From that point forward, all conversations consist of
424: * a hypervisor request (a call to this function) which directs us to refill
425: * the virtqueue with a fresh stats buffer. Since stats collection can sleep,
426: * we delegate the job to a freezable workqueue that will do the actual work via
427: * stats_handle_request().
428: */
429: static void stats_request(struct virtqueue *vq)
430: {
431: struct virtio_balloon *vb = vq->vdev->priv;
432:
433: spin_lock(&vb->stop_update_lock);
434: if (!vb->stop_update) {
435: start_wakeup_event(vb, VIRTIO_BALLOON_WAKEUP_SIGNAL_STATS);
436: queue_work(system_freezable_wq, &vb->update_balloon_stats_work);
437: }
438: spin_unlock(&vb->stop_update_lock);
439: }
440:
441: static void stats_handle_request(struct virtio_balloon *vb)
442: {
443: struct virtqueue *vq;
444: struct scatterlist sg;
445: unsigned int len, num_stats;
446:
447: num_stats = update_balloon_stats(vb);
448:
449: vq = vb->stats_vq;
450: if (!virtqueue_get_buf(vq, &len))
451: return;
452: sg_init_one(&sg, vb->stats, sizeof(vb->stats[0]) * num_stats);
453: virtqueue_add_outbuf(vq, &sg, 1, vb, GFP_KERNEL);
454: virtqueue_kick(vq);
455: }
456:
457: static inline s64 towards_target(struct virtio_balloon *vb)
458: {
459: s64 target;
460: u32 num_pages;
461:
462: /* Legacy balloon config space is LE, unlike all other devices. */
463: virtio_cread_le(vb->vdev, struct virtio_balloon_config, num_pages,
464: &num_pages);
465:
466: /*
467: * Aligned up to guest page size to avoid inflating and deflating
468: * balloon endlessly.
469: */
]
|
| 24/2 |
2026/07/27 02:03 |
llm |
expert |
0m
Model:
Tokens:
input: 14741
output: 22
thoughts: 0
|
| 25/2 |
2026/07/27 02:03 |
tool |
codesearch-find-references |
0m
Args: map[Name:virtballoon_remove]
Results: map[References:[map[ReferenceKind:takes-address-of ReferencingEntityKind:global_variable ReferencingEntityName:virtio_balloon_driver SourceFile:drivers/virtio/virtio_balloon.c SourceLine:1188]] TruncatedOutput:false]
|
| 26/2 |
2026/07/27 02:03 |
llm |
expert |
0m
Model:
Tokens:
input: 14844
output: 40
thoughts: 0
|
| 27/2 |
2026/07/27 02:03 |
tool |
read-file |
0m
Args: map[File:drivers/virtio/virtio_balloon.c FirstLine:1090 LineCount:50]
Results: map[Contents:1090: return_free_pages_to_mm(vb, ULONG_MAX);
1091:
1092: /* Now we reset the device so we can clean up the queues. */
1093: virtio_reset_device(vb->vdev);
1094:
1095: vb->vdev->config->del_vqs(vb->vdev);
1096: }
1097:
1098: static void virtballoon_remove(struct virtio_device *vdev)
1099: {
1100: struct virtio_balloon *vb = vdev->priv;
1101:
1102: if (virtio_has_feature(vb->vdev, VIRTIO_BALLOON_F_REPORTING))
1103: page_reporting_unregister(&vb->pr_dev_info);
1104: if (virtio_has_feature(vb->vdev, VIRTIO_BALLOON_F_DEFLATE_ON_OOM))
1105: unregister_oom_notifier(&vb->oom_nb);
1106: if (virtio_has_feature(vb->vdev, VIRTIO_BALLOON_F_FREE_PAGE_HINT))
1107: virtio_balloon_unregister_shrinker(vb);
1108: spin_lock_irq(&vb->stop_update_lock);
1109: vb->stop_update = true;
1110: spin_unlock_irq(&vb->stop_update_lock);
1111: cancel_work_sync(&vb->update_balloon_size_work);
1112: cancel_work_sync(&vb->update_balloon_stats_work);
1113:
1114: if (virtio_has_feature(vdev, VIRTIO_BALLOON_F_FREE_PAGE_HINT)) {
1115: cancel_work_sync(&vb->report_free_page_work);
1116: destroy_workqueue(vb->balloon_wq);
1117: }
1118:
1119: remove_common(vb);
1120: mutex_destroy(&vb->balloon_lock);
1121: kfree(vb);
1122: }
1123:
1124: #ifdef CONFIG_PM_SLEEP
1125: static int virtballoon_freeze(struct virtio_device *vdev)
1126: {
1127: struct virtio_balloon *vb = vdev->priv;
1128:
1129: /*
1130: * The workqueue is already frozen by the PM core before this
1131: * function is called.
1132: */
1133: remove_common(vb);
1134: return 0;
1135: }
1136:
1137: static int virtballoon_restore(struct virtio_device *vdev)
1138: {
1139: struct virtio_balloon *vb = vdev->priv;
]
|
| 28/2 |
2026/07/27 02:03 |
llm |
expert |
0m
Model:
Tokens:
input: 15717
output: 40
thoughts: 0
|
| 29/2 |
2026/07/27 02:03 |
tool |
read-file |
0m
Args: map[File:drivers/virtio/virtio_balloon.c FirstLine:1070 LineCount:50]
Results: map[Contents:1070: if (virtio_has_feature(vdev, VIRTIO_BALLOON_F_FREE_PAGE_HINT))
1071: destroy_workqueue(vb->balloon_wq);
1072: out_del_vqs:
1073: vdev->config->del_vqs(vdev);
1074: out_free_vb:
1075: mutex_destroy(&vb->balloon_lock);
1076: kfree(vb);
1077: out:
1078: return err;
1079: }
1080:
1081: static void remove_common(struct virtio_balloon *vb)
1082: {
1083: /* There might be pages left in the balloon: free them. */
1084: while (vb->num_pages)
1085: leak_balloon(vb, vb->num_pages);
1086: update_balloon_size(vb);
1087:
1088: /* There might be free pages that are being reported: release them. */
1089: if (virtio_has_feature(vb->vdev, VIRTIO_BALLOON_F_FREE_PAGE_HINT))
1090: return_free_pages_to_mm(vb, ULONG_MAX);
1091:
1092: /* Now we reset the device so we can clean up the queues. */
1093: virtio_reset_device(vb->vdev);
1094:
1095: vb->vdev->config->del_vqs(vb->vdev);
1096: }
1097:
1098: static void virtballoon_remove(struct virtio_device *vdev)
1099: {
1100: struct virtio_balloon *vb = vdev->priv;
1101:
1102: if (virtio_has_feature(vb->vdev, VIRTIO_BALLOON_F_REPORTING))
1103: page_reporting_unregister(&vb->pr_dev_info);
1104: if (virtio_has_feature(vb->vdev, VIRTIO_BALLOON_F_DEFLATE_ON_OOM))
1105: unregister_oom_notifier(&vb->oom_nb);
1106: if (virtio_has_feature(vb->vdev, VIRTIO_BALLOON_F_FREE_PAGE_HINT))
1107: virtio_balloon_unregister_shrinker(vb);
1108: spin_lock_irq(&vb->stop_update_lock);
1109: vb->stop_update = true;
1110: spin_unlock_irq(&vb->stop_update_lock);
1111: cancel_work_sync(&vb->update_balloon_size_work);
1112: cancel_work_sync(&vb->update_balloon_stats_work);
1113:
1114: if (virtio_has_feature(vdev, VIRTIO_BALLOON_F_FREE_PAGE_HINT)) {
1115: cancel_work_sync(&vb->report_free_page_work);
1116: destroy_workqueue(vb->balloon_wq);
1117: }
1118:
1119: remove_common(vb);
]
|
| 30/2 |
2026/07/27 02:03 |
llm |
expert |
0m
Model:
Tokens:
input: 16665
output: 62
thoughts: 0
|
| 31/2 |
2026/07/27 02:03 |
tool |
git-log |
0m
Args: map[CodeRegexp:cancel_work_sync.*update_balloon_stats_work Count:10 SourcePath:drivers/virtio/virtio_balloon.c SymbolName:virtballoon_remove]
Results: map[Output:7c59cc9cf729 virtio-balloon: Destroy mutex before freeing virtio_balloon
68f2736a8583 mm: Convert all PageMovable users to movable_operations
da10329cb057 virtio-balloon: switch back to OOM handler for VIRTIO_BALLOON_F_DEFLATE_ON_OOM
b0c504f15471 virtio-balloon: add support for providing free page reports to host
86a559787e6f virtio-balloon: VIRTIO_BALLOON_F_FREE_PAGE_HINT
71994620bb25 virtio_balloon: replace oom notifier with shrinker
9c57b5808c62 mm balloon: umount balloon_mnt when removing vb device
b1123ea6d3b3 mm: balloon: use general non-lru movable page feature
fd0e21c31e1e virtio_balloon: Allow to resize and update the balloon stats in parallel
fad7b7b27b6a virtio_balloon: Use a workqueue instead of "vballoon" kthread
]
|
| 32/2 |
2026/07/27 02:03 |
llm |
expert |
0m
Model:
Tokens:
input: 17020
output: 26
thoughts: 0
|
| 33/2 |
2026/07/27 02:03 |
tool |
git-show |
0m
Args:
Results: map[Output:commit fd0e21c31e1e6383af978239d07ced6e4e5d82c3
Author: Petr Mladek <pmladek@suse.com>
Date: Mon Jan 25 17:38:06 2016 +0100
virtio_balloon: Allow to resize and update the balloon stats in parallel
The virtio balloon statistics are not updated when the balloon
is being resized. But it seems that both tasks could be done
in parallel.
stats_handle_request() updates the statistics in the balloon
structure and then communicates with the host.
update_balloon_stats() calls all_vm_events() that just reads
some per-CPU variables. The values might change during and
after the call but it is expected and happens even without
this patch.
update_balloon_stats() also calls si_meminfo(). It is a bit
more complex function. It too just reads some variables and
looks lock-less safe. In each case, it seems to be called
lock-less on several similar locations, e.g. from post_status()
in dm_thread_func(), or from vmballoon_send_get_target().
The communication with the host is done via a separate virtqueue,
see vb->stats_vq vs. vb->inflate_vq and vb->deflate_vq. Therefore
it could be used in parallel with fill_balloon() and leak_balloon().
This patch splits the existing work into two pieces. One is for
updating the balloon stats. The other is for resizing of the balloon.
It seems that they can be proceed in parallel without any
extra locking.
Signed-off-by: Petr Mladek <pmladek@suse.com>
Signed-off-by: Michael S. Tsirkin <mst@redhat.com>
diff --git a/drivers/virtio/virtio_balloon.c b/drivers/virtio/virtio_balloon.c
index 2c9a92f1e525..9057cc768ca5 100644
--- a/drivers/virtio/virtio_balloon.c
+++ b/drivers/virtio/virtio_balloon.c
@@ -49,7 +49,8 @@ struct virtio_balloon {
struct virtqueue *inflate_vq, *deflate_vq, *stats_vq;
/* The balloon servicing is delegated to a freezable workqueue. */
- struct work_struct work;
+ struct work_struct update_balloon_stats_work;
+ struct work_struct update_balloon_size_work;
/* Prevent updating balloon when it is being canceled. */
spinlock_t stop_update_lock;
@@ -76,7 +77,6 @@ struct virtio_balloon {
u32 pfns[VIRTIO_BALLOON_ARRAY_PFNS_MAX];
/* Memory statistics */
- int need_stats_update;
struct virtio_balloon_stat stats[VIRTIO_BALLOON_S_NR];
/* To register callback in oom notifier call chain */
@@ -123,6 +123,7 @@ static void tell_host(struct virtio_balloon *vb, struct virtqueue *vq)
/* When host has read buffer, this completes via balloon_ack */
wait_event(vb->acked, virtqueue_get_buf(vq, &len));
+
}
static void set_page_pfns(u32 pfns[], struct page *page)
@@ -262,11 +263,9 @@ static void stats_request(struct virtqueue *vq)
{
struct virtio_balloon *vb = vq->vdev->priv;
- vb->need_stats_update = 1;
-
spin_lock(&vb->stop_update_lock);
if (!vb->stop_update)
- queue_work(system_freezable_wq, &vb->work);
+ queue_work(system_freezable_wq, &vb->update_balloon_stats_work);
spin_unlock(&vb->stop_update_lock);
}
@@ -276,7 +275,6 @@ static void stats_handle_request(struct virtio_balloon *vb)
struct scatterlist sg;
unsigned int len;
- vb->need_stats_update = 0;
update_balloon_stats(vb);
vq = vb->stats_vq;
@@ -294,7 +292,7 @@ static void virtballoon_changed(struct virtio_device *vdev)
spin_lock_irqsave(&vb->stop_update_lock, flags);
if (!vb->stop_update)
- queue_work(system_freezable_wq, &vb->work);
+ queue_work(system_freezable_wq, &vb->update_balloon_size_work);
spin_unlock_irqrestore(&vb->stop_update_lock, flags);
}
@@ -358,17 +356,24 @@ static int virtballoon_oom_notify(struct notifier_block *self,
return NOTIFY_OK;
}
-static void balloon(struct work_struct *work)
+static void update_balloon_stats_func(struct work_struct *work)
+{
+ struct virtio_balloon *vb;
+
+ vb = container_of(work, struct virtio_balloon,
+ update_balloon_stats_work);
+ stats_handle_request(vb);
+}
+
+static void update_balloon_size_func(struct work_struct *work)
{
struct virtio_balloon *vb;
s64 diff;
- vb = container_of(work, struct virtio_balloon, work);
+ vb = container_of(work, struct virtio_balloon,
+ update_balloon_size_work);
diff = towards_target(vb);
- if (vb->need_stats_update)
- stats_handle_request(vb);
-
if (diff > 0)
diff -= fill_balloon(vb, diff);
else if (diff < 0)
@@ -494,14 +499,14 @@ static int virtballoon_probe(struct virtio_device *vdev)
goto out;
}
- INIT_WORK(&vb->work, balloon);
+ INIT_WORK(&vb->update_balloon_stats_work, update_balloon_stats_func);
+ INIT_WORK(&vb->update_balloon_size_work, update_balloon_size_func);
spin_lock_init(&vb->stop_update_lock);
vb->stop_update = false;
vb->num_pages = 0;
mutex_init(&vb->balloon_lock);
init_waitqueue_head(&vb->acked);
vb->vdev = vdev;
- vb->need_stats_update = 0;
balloon_devinfo_init(&vb->vb_dev_info);
#ifdef CONFIG_BALLOON_COMPACTION
@@ -552,7 +557,8 @@ static void virtballoon_remove(struct virtio_device *vdev)
spin_lock_irq(&vb->stop_update_lock);
vb->stop_update = true;
spin_unlock_irq(&vb->stop_update_lock);
- cancel_work_sync(&vb->work);
+ cancel_work_sync(&vb->update_balloon_size_work);
+ cancel_work_sync(&vb->update_balloon_stats_work);
remove_common(vb);
kfree(vb);
]
|
| 34/2 |
2026/07/27 02:03 |
llm |
expert |
0m
Model:
Tokens:
input: 19004
output: 61
thoughts: 0
|
| 35/2 |
2026/07/27 02:03 |
tool |
git-log |
0m
Args: map[CodeRegexp:cancel_work_sync.*update_balloon_stats_work Count:10 SourcePath:drivers/virtio/virtio_balloon.c SymbolName:remove_common]
Results: map[Output:d9679d0013a6 virtio: wrap config->reset calls
6c22dc61c76b virtio-balloon: Fix memory leak when unloading while hinting is in progress
c877bab5072c virtio: balloon: separate out common code between remove and freeze functions
b8ae0eb320b3 virtio: balloon: let host know of updated balloon size before module removal
1e65175c2c73 move virtballoon_remove to .devexit.text
d2a7ddda9ffb virtio: find_vqs/del_vqs virtio operations
]
|
| 36/2 |
2026/07/27 02:03 |
llm |
expert |
0m
Model:
Tokens:
input: 19227
output: 25
thoughts: 0
|
| 37/2 |
2026/07/27 02:03 |
tool |
git-show |
0m
Args:
Results: map[Output:commit c877bab5072c8f461397949babbac10e348ae70d
Author: Amit Shah <amit.shah@redhat.com>
Date: Fri Apr 27 00:45:57 2012 +0530
virtio: balloon: separate out common code between remove and freeze functions
The remove and freeze functions have a lot of shared code; put it into a
common function that gets called by both.
Signed-off-by: Amit Shah <amit.shah@redhat.com>
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
diff --git a/drivers/virtio/virtio_balloon.c b/drivers/virtio/virtio_balloon.c
index 04baad647403..bfbc15ca38dd 100644
--- a/drivers/virtio/virtio_balloon.c
+++ b/drivers/virtio/virtio_balloon.c
@@ -381,21 +381,25 @@ static int virtballoon_probe(struct virtio_device *vdev)
return err;
}
-static void __devexit virtballoon_remove(struct virtio_device *vdev)
+static void remove_common(struct virtio_balloon *vb)
{
- struct virtio_balloon *vb = vdev->priv;
-
- kthread_stop(vb->thread);
-
/* There might be pages left in the balloon: free them. */
while (vb->num_pages)
leak_balloon(vb, vb->num_pages);
update_balloon_size(vb);
/* Now we reset the device so we can clean up the queues. */
- vdev->config->reset(vdev);
+ vb->vdev->config->reset(vb->vdev);
- vdev->config->del_vqs(vdev);
+ vb->vdev->config->del_vqs(vb->vdev);
+}
+
+static void __devexit virtballoon_remove(struct virtio_device *vdev)
+{
+ struct virtio_balloon *vb = vdev->priv;
+
+ kthread_stop(vb->thread);
+ remove_common(vb);
kfree(vb);
}
@@ -409,13 +413,7 @@ static int virtballoon_freeze(struct virtio_device *vdev)
* function is called.
*/
- while (vb->num_pages)
- leak_balloon(vb, vb->num_pages);
- update_balloon_size(vb);
-
- /* Ensure we don't get any more requests from the host */
- vdev->config->reset(vdev);
- vdev->config->del_vqs(vdev);
+ remove_common(vb);
return 0;
}
]
|
| 38/2 |
2026/07/27 02:03 |
llm |
expert |
0m
Model:
Tokens:
input: 20008
output: 62
thoughts: 0
|
| 39/2 |
2026/07/27 02:03 |
tool |
git-log |
0m
Args: map[CodeRegexp:cancel_work_sync.*update_balloon_stats_work Count:10 SourcePath:drivers/virtio/virtio_balloon.c SymbolName:virtballoon_remove]
Results: map[Output:7c59cc9cf729 virtio-balloon: Destroy mutex before freeing virtio_balloon
68f2736a8583 mm: Convert all PageMovable users to movable_operations
da10329cb057 virtio-balloon: switch back to OOM handler for VIRTIO_BALLOON_F_DEFLATE_ON_OOM
b0c504f15471 virtio-balloon: add support for providing free page reports to host
86a559787e6f virtio-balloon: VIRTIO_BALLOON_F_FREE_PAGE_HINT
71994620bb25 virtio_balloon: replace oom notifier with shrinker
9c57b5808c62 mm balloon: umount balloon_mnt when removing vb device
b1123ea6d3b3 mm: balloon: use general non-lru movable page feature
fd0e21c31e1e virtio_balloon: Allow to resize and update the balloon stats in parallel
fad7b7b27b6a virtio_balloon: Use a workqueue instead of "vballoon" kthread
]
|
| 40/2 |
2026/07/27 02:03 |
llm |
expert |
0m
Model:
Tokens:
input: 20363
output: 62
thoughts: 0
|
| 41/2 |
2026/07/27 02:04 |
tool |
git-log |
0m
Args: map[CodeRegexp:cancel_work_sync.*update_balloon_stats_work Count:10 SourcePath:drivers/virtio/virtio_balloon.c SymbolName:virtballoon_freeze]
Results: map[Output:fad7b7b27b6a virtio_balloon: Use a workqueue instead of "vballoon" kthread
c877bab5072c virtio: balloon: separate out common code between remove and freeze functions
4eb05d562ea1 virtio: balloon: leak / fill balloon across S4
e562966dbaf4 virtio: balloon: Add freeze, restore handlers to support S4
]
|
| 42/2 |
2026/07/27 02:04 |
llm |
expert |
0m
Model:
Tokens:
input: 20539
output: 25
thoughts: 0
|
| 43/2 |
2026/07/27 02:04 |
tool |
git-show |
0m
Args:
Results: map[Output:commit fad7b7b27b6a168ca8ebc84482043886f837b89d
Author: Petr Mladek <pmladek@suse.com>
Date: Mon Jan 25 17:38:05 2016 +0100
virtio_balloon: Use a workqueue instead of "vballoon" kthread
This patch moves the deferred work from the "vballoon" kthread into a
system freezable workqueue.
We do not need to maintain and run a dedicated kthread. Also the event
driven workqueues API makes the logic much easier. Especially, we do
not longer need an own wait queue, wait function, and freeze point.
The conversion is pretty straightforward. One cycle of the main loop
is put into a work. The work is queued instead of waking the kthread.
fill_balloon() and leak_balloon() have a limit for the amount of modified
pages. The work re-queues itself when necessary. For this, we make
fill_balloon() to return the number of really modified pages.
Note that leak_balloon() already did this.
virtballoon_restore() queues the work only when really needed.
The only complication is that we need to prevent queuing the work
when the balloon is being removed. It was easier before because the
kthread simply removed itself from the wait queue. We need an
extra boolean and spin lock now.
My initial idea was to use a dedicated workqueue. Michael S. Tsirkin
suggested using a system one. Tejun Heo confirmed that the system
workqueue has a pretty high concurrency level (256) by default.
Therefore we need not be afraid of too long blocking.
Signed-off-by: Petr Mladek <pmladek@suse.cz>
Signed-off-by: Michael S. Tsirkin <mst@redhat.com>
diff --git a/drivers/virtio/virtio_balloon.c b/drivers/virtio/virtio_balloon.c
index 0c3691f46575..2c9a92f1e525 100644
--- a/drivers/virtio/virtio_balloon.c
+++ b/drivers/virtio/virtio_balloon.c
@@ -22,8 +22,7 @@
#include <linux/virtio.h>
#include <linux/virtio_balloon.h>
#include <linux/swap.h>
-#include <linux/kthread.h>
-#include <linux/freezer.h>
+#include <linux/workqueue.h>
#include <linux/delay.h>
#include <linux/slab.h>
#include <linux/module.h>
@@ -49,11 +48,12 @@ struct virtio_balloon {
struct virtio_device *vdev;
struct virtqueue *inflate_vq, *deflate_vq, *stats_vq;
- /* Where the ballooning thread waits for config to change. */
- wait_queue_head_t config_change;
+ /* The balloon servicing is delegated to a freezable workqueue. */
+ struct work_struct work;
- /* The thread servicing the balloon. */
- struct task_struct *thread;
+ /* Prevent updating balloon when it is being canceled. */
+ spinlock_t stop_update_lock;
+ bool stop_update;
/* Waiting for host to ack the pages we released. */
wait_queue_head_t acked;
@@ -135,9 +135,10 @@ static void set_page_pfns(u32 pfns[], struct page *page)
pfns[i] = page_to_balloon_pfn(page) + i;
}
-static void fill_balloon(struct virtio_balloon *vb, size_t num)
+static unsigned fill_balloon(struct virtio_balloon *vb, size_t num)
{
struct balloon_dev_info *vb_dev_info = &vb->vb_dev_info;
+ unsigned num_allocated_pages;
/* We can only do one array worth at a time. */
num = min(num, ARRAY_SIZE(vb->pfns));
@@ -162,10 +163,13 @@ static void fill_balloon(struct virtio_balloon *vb, size_t num)
adjust_managed_page_count(page, -1);
}
+ num_allocated_pages = vb->num_pfns;
/* Did we get any? */
if (vb->num_pfns != 0)
tell_host(vb, vb->inflate_vq);
mutex_unlock(&vb->balloon_lock);
+
+ return num_allocated_pages;
}
static void release_pages_balloon(struct virtio_balloon *vb)
@@ -251,14 +255,19 @@ static void update_balloon_stats(struct virtio_balloon *vb)
* with a single buffer. From that point forward, all conversations consist of
* a hypervisor request (a call to this function) which directs us to refill
* the virtqueue with a fresh stats buffer. Since stats collection can sleep,
- * we notify our kthread which does the actual work via stats_handle_request().
+ * we delegate the job to a freezable workqueue that will do the actual work via
+ * stats_handle_request().
*/
static void stats_request(struct virtqueue *vq)
{
struct virtio_balloon *vb = vq->vdev->priv;
vb->need_stats_update = 1;
- wake_up(&vb->config_change);
+
+ spin_lock(&vb->stop_update_lock);
+ if (!vb->stop_update)
+ queue_work(system_freezable_wq, &vb->work);
+ spin_unlock(&vb->stop_update_lock);
}
static void stats_handle_request(struct virtio_balloon *vb)
@@ -281,8 +290,12 @@ static void stats_handle_request(struct virtio_balloon *vb)
static void virtballoon_changed(struct virtio_device *vdev)
{
struct virtio_balloon *vb = vdev->priv;
+ unsigned long flags;
- wake_up(&vb->config_change);
+ spin_lock_irqsave(&vb->stop_update_lock, flags);
+ if (!vb->stop_update)
+ queue_work(system_freezable_wq, &vb->work);
+ spin_unlock_irqrestore(&vb->stop_update_lock, flags);
}
static inline s64 towards_target(struct virtio_balloon *vb)
@@ -345,43 +358,25 @@ static int virtballoon_oom_notify(struct notifier_block *self,
return NOTIFY_OK;
}
-static int balloon(void *_vballoon)
+static void balloon(struct work_struct *work)
{
- struct virtio_balloon *vb = _vballoon;
- DEFINE_WAIT_FUNC(wait, woken_wake_function);
-
- set_freezable();
- while (!kthread_should_stop()) {
- s64 diff;
-
- try_to_freeze();
-
- add_wait_queue(&vb->config_change, &wait);
- for (;;) {
- if ((diff = towards_target(vb)) != 0 ||
- vb->need_stats_update ||
- kthread_should_stop() ||
- freezing(current))
- break;
- wait_woken(&wait, TASK_INTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT);
- }
- remove_wait_queue(&vb->config_change, &wait);
+ struct virtio_balloon *vb;
+ s64 diff;
- if (vb->need_stats_update)
- stats_handle_request(vb);
- if (diff > 0)
- fill_balloon(vb, diff);
- else if (diff < 0)
- leak_balloon(vb, -diff);
- update_balloon_size(vb);
+ vb = container_of(work, struct virtio_balloon, work);
+ diff = towards_target(vb);
- /*
- * For large balloon changes, we could spend a lot of time
- * and always have work to do. Be nice if preempt disabled.
- */
- cond_resched();
- }
- return 0;
+ if (vb->need_stats_update)
+ stats_handle_request(vb);
+
+ if (diff > 0)
+ diff -= fill_balloon(vb, diff);
+ else if (diff < 0)
+ diff += leak_balloon(vb, -diff);
+ update_balloon_size(vb);
+
+ if (diff)
+ queue_work(system_freezable_wq, work);
}
static int init_vqs(struct virtio_balloon *vb)
@@ -499,9 +494,11 @@ static int virtballoon_probe(struct virtio_device *vdev)
goto out;
}
+ INIT_WORK(&vb->work, balloon);
+ spin_lock_init(&vb->stop_update_lock);
+ vb->stop_update = false;
vb->num_pages = 0;
mutex_init(&vb->balloon_lock);
- init_waitqueue_head(&vb->config_change);
init_waitqueue_head(&vb->acked);
vb->vdev = vdev;
vb->need_stats_update = 0;
@@ -523,16 +520,8 @@ static int virtballoon_probe(struct virtio_device *vdev)
virtio_device_ready(vdev);
- vb->thread = kthread_run(balloon, vb, "vballoon");
- if (IS_ERR(vb->thread)) {
- err = PTR_ERR(vb->thread);
- goto out_del_vqs;
- }
-
return 0;
-out_del_vqs:
- unregister_oom_notifier(&vb->nb);
out_oom_notify:
vdev->config->del_vqs(vdev);
out_free_vb:
@@ -559,7 +548,12 @@ static void virtballoon_remove(struct virtio_device *vdev)
struct virtio_balloon *vb = vdev->priv;
unregister_oom_notifier(&vb->nb);
- kthread_stop(vb->thread);
+
+ spin_lock_irq(&vb->stop_update_lock);
+ vb->stop_update = true;
+ spin_unlock_irq(&vb->stop_update_lock);
+ cancel_work_sync(&vb->work);
+
remove_common(vb);
kfree(vb);
}
@@ -570,10 +564,9 @@ static int virtballoon_freeze(struct virtio_device *vdev)
struct virtio_balloon *vb = vdev->priv;
/*
- * The kthread is already frozen by the PM core before this
+ * The workqueue is already frozen by the PM core before this
* function is called.
*/
-
remove_common(vb);
return 0;
}
@@ -589,7 +582,8 @@ static int virtballoon_restore(struct virtio_device *vdev)
virtio_device_ready(vdev);
- fill_balloon(vb, towards_target(vb));
+ if (towards_target(vb))
+ virtballoon_changed(vdev);
update_balloon_size(vb);
return 0;
}
]
|
| 44/2 |
2026/07/27 02:04 |
llm |
expert |
0m
Model:
Tokens:
input: 23586
output: 62
thoughts: 0
|
| 45/2 |
2026/07/27 02:04 |
tool |
git-log |
0m
Args: map[CodeRegexp:cancel_work_sync.*update_balloon_stats_work Count:10 SourcePath:drivers/virtio/virtio_balloon.c SymbolName:virtballoon_remove]
Results: map[Output:7c59cc9cf729 virtio-balloon: Destroy mutex before freeing virtio_balloon
68f2736a8583 mm: Convert all PageMovable users to movable_operations
da10329cb057 virtio-balloon: switch back to OOM handler for VIRTIO_BALLOON_F_DEFLATE_ON_OOM
b0c504f15471 virtio-balloon: add support for providing free page reports to host
86a559787e6f virtio-balloon: VIRTIO_BALLOON_F_FREE_PAGE_HINT
71994620bb25 virtio_balloon: replace oom notifier with shrinker
9c57b5808c62 mm balloon: umount balloon_mnt when removing vb device
b1123ea6d3b3 mm: balloon: use general non-lru movable page feature
fd0e21c31e1e virtio_balloon: Allow to resize and update the balloon stats in parallel
fad7b7b27b6a virtio_balloon: Use a workqueue instead of "vballoon" kthread
]
|
| 46/2 |
2026/07/27 02:04 |
llm |
expert |
0m
Model:
Tokens:
input: 23941
output: 25
thoughts: 0
|
| 47/2 |
2026/07/27 02:04 |
tool |
git-show |
0m
Args:
Results: map[Output:commit 7c59cc9cf729092512b88ccd3290b2fd0c7e53c4
Author: Maurice Hieronymus <mhi@mailbox.org>
Date: Sun Nov 23 18:57:48 2025 +0100
virtio-balloon: Destroy mutex before freeing virtio_balloon
Add a call to mutex_destroy in the error code path as well as in the
virtballoon_remove code path.
Signed-off-by: Maurice Hieronymus <mhi@mailbox.org>
Acked-by: David Hildenbrand (Red Hat) <david@kernel.org>
Signed-off-by: Michael S. Tsirkin <mst@redhat.com>
Message-ID: <20251123175750.445461-2-mhi@mailbox.org>
diff --git a/drivers/virtio/virtio_balloon.c b/drivers/virtio/virtio_balloon.c
index f6c2dff33f8a..088b3a0e6ce6 100644
--- a/drivers/virtio/virtio_balloon.c
+++ b/drivers/virtio/virtio_balloon.c
@@ -1075,6 +1075,7 @@ static int virtballoon_probe(struct virtio_device *vdev)
out_del_vqs:
vdev->config->del_vqs(vdev);
out_free_vb:
+ mutex_destroy(&vb->balloon_lock);
kfree(vb);
out:
return err;
@@ -1119,6 +1120,7 @@ static void virtballoon_remove(struct virtio_device *vdev)
}
remove_common(vb);
+ mutex_destroy(&vb->balloon_lock);
kfree(vb);
}
]
|
| 48/2 |
2026/07/27 02:04 |
llm |
expert |
0m
Model:
Tokens:
input: 24468
output: 62
thoughts: 0
|
| 49/2 |
2026/07/27 02:04 |
tool |
git-log |
0m
Error:
Args: map[CodeRegexp:cancel_work_sync.*update_balloon_stats_work Count:10 SourcePath:drivers/virtio/virtio_balloon.c SymbolName:virtballoon_remove]
|
| 50/2 |
2026/07/27 02:04 |
llm |
expert |
0m
Model:
Tokens:
input: 24664
output: 61
thoughts: 0
|
| 51/2 |
2026/07/27 02:04 |
tool |
git-log |
0m
Args: map[CodeRegexp:cancel_work_sync.*update_balloon_stats_work Count:10 SourcePath:drivers/virtio/virtio_balloon.c SymbolName:remove_common]
Results: map[Output:d9679d0013a6 virtio: wrap config->reset calls
6c22dc61c76b virtio-balloon: Fix memory leak when unloading while hinting is in progress
c877bab5072c virtio: balloon: separate out common code between remove and freeze functions
b8ae0eb320b3 virtio: balloon: let host know of updated balloon size before module removal
1e65175c2c73 move virtballoon_remove to .devexit.text
d2a7ddda9ffb virtio: find_vqs/del_vqs virtio operations
]
|
| 52/2 |
2026/07/27 02:04 |
llm |
expert |
0m
Model:
Tokens:
input: 24887
output: 27
thoughts: 0
|
| 53/2 |
2026/07/27 02:04 |
tool |
git-show |
0m
Args:
Results: map[Output:commit d9679d0013a66849f23057978f92e76b255c50aa
Author: Michael S. Tsirkin <mst@redhat.com>
Date: Wed Oct 13 06:55:44 2021 -0400
virtio: wrap config->reset calls
This will enable cleanups down the road.
The idea is to disable cbs, then add "flush_queued_cbs" callback
as a parameter, this way drivers can flush any work
queued after callbacks have been disabled.
Signed-off-by: Michael S. Tsirkin <mst@redhat.com>
Link: https://lore.kernel.org/r/20211013105226.20225-1-mst@redhat.com
Signed-off-by: Michael S. Tsirkin <mst@redhat.com>
diff --git a/arch/um/drivers/virt-pci.c b/arch/um/drivers/virt-pci.c
index c08066633023..22c4d87c9c15 100644
--- a/arch/um/drivers/virt-pci.c
+++ b/arch/um/drivers/virt-pci.c
@@ -616,7 +616,7 @@ static void um_pci_virtio_remove(struct virtio_device *vdev)
int i;
/* Stop all virtqueues */
- vdev->config->reset(vdev);
+ virtio_reset_device(vdev);
vdev->config->del_vqs(vdev);
device_set_wakeup_enable(&vdev->dev, false);
diff --git a/drivers/block/virtio_blk.c b/drivers/block/virtio_blk.c
index 6ae38776e30e..644c6cddad6d 100644
--- a/drivers/block/virtio_blk.c
+++ b/drivers/block/virtio_blk.c
@@ -977,7 +977,7 @@ static void virtblk_remove(struct virtio_device *vdev)
mutex_lock(&vblk->vdev_mutex);
/* Stop all the virtqueues. */
- vdev->config->reset(vdev);
+ virtio_reset_device(vdev);
/* Virtqueues are stopped, nothing can use vblk->vdev anymore. */
vblk->vdev = NULL;
@@ -996,7 +996,7 @@ static int virtblk_freeze(struct virtio_device *vdev)
struct virtio_blk *vblk = vdev->priv;
/* Ensure we don't receive any more interrupts */
- vdev->config->reset(vdev);
+ virtio_reset_device(vdev);
/* Make sure no work handler is accessing the device. */
flush_work(&vblk->config_work);
diff --git a/drivers/bluetooth/virtio_bt.c b/drivers/bluetooth/virtio_bt.c
index 57908ce4fae8..24a9258962fa 100644
--- a/drivers/bluetooth/virtio_bt.c
+++ b/drivers/bluetooth/virtio_bt.c
@@ -364,7 +364,7 @@ static void virtbt_remove(struct virtio_device *vdev)
struct hci_dev *hdev = vbt->hdev;
hci_unregister_dev(hdev);
- vdev->config->reset(vdev);
+ virtio_reset_device(vdev);
hci_free_dev(hdev);
vbt->hdev = NULL;
diff --git a/drivers/char/hw_random/virtio-rng.c b/drivers/char/hw_random/virtio-rng.c
index 0a7dde135db1..b2bf78b25630 100644
--- a/drivers/char/hw_random/virtio-rng.c
+++ b/drivers/char/hw_random/virtio-rng.c
@@ -179,7 +179,7 @@ static void remove_common(struct virtio_device *vdev)
vi->data_avail = 0;
vi->data_idx = 0;
complete(&vi->have_data);
- vdev->config->reset(vdev);
+ virtio_reset_device(vdev);
if (vi->hwrng_register_done)
hwrng_unregister(&vi->hwrng);
vdev->config->del_vqs(vdev);
diff --git a/drivers/char/virtio_console.c b/drivers/char/virtio_console.c
index 660c5c388c29..2359889a35a0 100644
--- a/drivers/char/virtio_console.c
+++ b/drivers/char/virtio_console.c
@@ -1958,7 +1958,7 @@ static void virtcons_remove(struct virtio_device *vdev)
spin_unlock_irq(&pdrvdata_lock);
/* Disable interrupts for vqs */
- vdev->config->reset(vdev);
+ virtio_reset_device(vdev);
/* Finish up work that's lined up */
if (use_multiport(portdev))
cancel_work_sync(&portdev->control_work);
@@ -2148,7 +2148,7 @@ static int virtcons_freeze(struct virtio_device *vdev)
portdev = vdev->priv;
- vdev->config->reset(vdev);
+ virtio_reset_device(vdev);
if (use_multiport(portdev))
virtqueue_disable_cb(portdev->c_ivq);
diff --git a/drivers/crypto/virtio/virtio_crypto_core.c b/drivers/crypto/virtio/virtio_crypto_core.c
index e2375d992308..8e977b7627cb 100644
--- a/drivers/crypto/virtio/virtio_crypto_core.c
+++ b/drivers/crypto/virtio/virtio_crypto_core.c
@@ -404,7 +404,7 @@ static int virtcrypto_probe(struct virtio_device *vdev)
free_engines:
virtcrypto_clear_crypto_engines(vcrypto);
free_vqs:
- vcrypto->vdev->config->reset(vdev);
+ virtio_reset_device(vdev);
virtcrypto_del_vqs(vcrypto);
free_dev:
virtcrypto_devmgr_rm_dev(vcrypto);
@@ -436,7 +436,7 @@ static void virtcrypto_remove(struct virtio_device *vdev)
if (virtcrypto_dev_started(vcrypto))
virtcrypto_dev_stop(vcrypto);
- vdev->config->reset(vdev);
+ virtio_reset_device(vdev);
virtcrypto_free_unused_reqs(vcrypto);
virtcrypto_clear_crypto_engines(vcrypto);
virtcrypto_del_vqs(vcrypto);
@@ -456,7 +456,7 @@ static int virtcrypto_freeze(struct virtio_device *vdev)
{
struct virtio_crypto *vcrypto = vdev->priv;
- vdev->config->reset(vdev);
+ virtio_reset_device(vdev);
virtcrypto_free_unused_reqs(vcrypto);
if (virtcrypto_dev_started(vcrypto))
virtcrypto_dev_stop(vcrypto);
@@ -492,7 +492,7 @@ static int virtcrypto_restore(struct virtio_device *vdev)
free_engines:
virtcrypto_clear_crypto_engines(vcrypto);
free_vqs:
- vcrypto->vdev->config->reset(vdev);
+ virtio_reset_device(vdev);
virtcrypto_del_vqs(vcrypto);
return err;
}
diff --git a/drivers/firmware/arm_scmi/virtio.c b/drivers/firmware/arm_scmi/virtio.c
index 87039c5c03fd..eefcc4146749 100644
--- a/drivers/firmware/arm_scmi/virtio.c
+++ b/drivers/firmware/arm_scmi/virtio.c
@@ -452,7 +452,7 @@ static void scmi_vio_remove(struct virtio_device *vdev)
* outstanding message on any vqueue to be ignored by complete_cb: now
* we can just stop processing buffers and destroy the vqueues.
*/
- vdev->config->reset(vdev);
+ virtio_reset_device(vdev);
vdev->config->del_vqs(vdev);
/* Ensure scmi_vdev is visible as NULL */
smp_store_mb(scmi_vdev, NULL);
diff --git a/drivers/gpio/gpio-virtio.c b/drivers/gpio/gpio-virtio.c
index 9f4941bc5760..fcc5e8c08973 100644
--- a/drivers/gpio/gpio-virtio.c
+++ b/drivers/gpio/gpio-virtio.c
@@ -450,7 +450,7 @@ static void virtio_gpio_request_vq(struct virtqueue *vq)
static void virtio_gpio_free_vqs(struct virtio_device *vdev)
{
- vdev->config->reset(vdev);
+ virtio_reset_device(vdev);
vdev->config->del_vqs(vdev);
}
diff --git a/drivers/gpu/drm/virtio/virtgpu_kms.c b/drivers/gpu/drm/virtio/virtgpu_kms.c
index 21f410901694..3313b92db531 100644
--- a/drivers/gpu/drm/virtio/virtgpu_kms.c
+++ b/drivers/gpu/drm/virtio/virtgpu_kms.c
@@ -279,7 +279,7 @@ void virtio_gpu_deinit(struct drm_device *dev)
flush_work(&vgdev->ctrlq.dequeue_work);
flush_work(&vgdev->cursorq.dequeue_work);
flush_work(&vgdev->config_changed_work);
- vgdev->vdev->config->reset(vgdev->vdev);
+ virtio_reset_device(vgdev->vdev);
vgdev->vdev->config->del_vqs(vgdev->vdev);
}
diff --git a/drivers/i2c/busses/i2c-virtio.c b/drivers/i2c/busses/i2c-virtio.c
index 41eb0dcc3204..4b9536f50800 100644
--- a/drivers/i2c/busses/i2c-virtio.c
+++ b/drivers/i2c/busses/i2c-virtio.c
@@ -165,7 +165,7 @@ static int virtio_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg *msgs,
static void virtio_i2c_del_vqs(struct virtio_device *vdev)
{
- vdev->config->reset(vdev);
+ virtio_reset_device(vdev);
vdev->config->del_vqs(vdev);
}
diff --git a/drivers/iommu/virtio-iommu.c b/drivers/iommu/virtio-iommu.c
index 80930ce04a16..1d4e1e7cf175 100644
--- a/drivers/iommu/virtio-iommu.c
+++ b/drivers/iommu/virtio-iommu.c
@@ -1115,7 +1115,7 @@ static void viommu_remove(struct virtio_device *vdev)
iommu_device_unregister(&viommu->iommu);
/* Stop all virtqueues */
- vdev->config->reset(vdev);
+ virtio_reset_device(vdev);
vdev->config->del_vqs(vdev);
dev_info(&vdev->dev, "device removed\n");
diff --git a/drivers/net/caif/caif_virtio.c b/drivers/net/caif/caif_virtio.c
index 91230894692d..444ef6a342f6 100644
--- a/drivers/net/caif/caif_virtio.c
+++ b/drivers/net/caif/caif_virtio.c
@@ -754,7 +754,7 @@ static void cfv_remove(struct virtio_device *vdev)
debugfs_remove_recursive(cfv->debugfs);
vringh_kiov_cleanup(&cfv->ctx.riov);
- vdev->config->reset(vdev);
+ virtio_reset_device(vdev);
vdev->vringh_config->del_vrhs(cfv->vdev);
cfv->vr_rx = NULL;
vdev->config->del_vqs(cfv->vdev);
diff --git a/drivers/net/virtio_net.c b/drivers/net/virtio_net.c
index b107835242ad..eeed458c794e 100644
--- a/drivers/net/virtio_net.c
+++ b/drivers/net/virtio_net.c
@@ -3310,7 +3310,7 @@ static int virtnet_probe(struct virtio_device *vdev)
return 0;
free_unregister_netdev:
- vi->vdev->config->reset(vdev);
+ virtio_reset_device(vdev);
unregister_netdev(dev);
free_failover:
@@ -3326,7 +3326,7 @@ static int virtnet_probe(struct virtio_device *vdev)
static void remove_vq_common(struct virtnet_info *vi)
{
- vi->vdev->config->reset(vi->vdev);
+ virtio_reset_device(vi->vdev);
/* Free unused buffers in both send and recv, if any. */
free_unused_bufs(vi);
diff --git a/drivers/net/wireless/mac80211_hwsim.c b/drivers/net/wireless/mac80211_hwsim.c
index 23219f3747f8..02d374360bcc 100644
--- a/drivers/net/wireless/mac80211_hwsim.c
+++ b/drivers/net/wireless/mac80211_hwsim.c
@@ -4498,7 +4498,7 @@ static void remove_vqs(struct virtio_device *vdev)
{
int i;
- vdev->config->reset(vdev);
+ virtio_reset_device(vdev);
for (i = 0; i < ARRAY_SIZE(hwsim_vqs); i++) {
struct virtqueue *vq = hwsim_vqs[i];
diff --git a/drivers/nvdimm/virtio_pmem.c b/drivers/nvdimm/virtio_pmem.c
index 726c7354d465..995b6cdc67ed 100644
--- a/drivers/nvdimm/virtio_pmem.c
+++ b/drivers/nvdimm/virtio_pmem.c
@@ -105,7 +105,7 @@ static void virtio_pmem_remove(struct virtio_device *vdev)
nvdimm_bus_unregister(nvdimm_bus);
vdev->config->del_vqs(vdev);
- vdev->config->reset(vdev);
+ virtio_reset_device(vdev);
}
static struct virtio_driver virtio_pmem_driver = {
diff --git a/drivers/rpmsg/virtio_rpmsg_bus.c b/drivers/rpmsg/virtio_rpmsg_bus.c
index 9c112aa65040..4bda40568dc9 100644
--- a/drivers/rpmsg/virtio_rpmsg_bus.c
+++ b/drivers/rpmsg/virtio_rpmsg_bus.c
@@ -1024,7 +1024,7 @@ static void rpmsg_remove(struct virtio_device *vdev)
size_t total_buf_space = vrp->num_bufs * vrp->buf_size;
int ret;
- vdev->config->reset(vdev);
+ virtio_reset_device(vdev);
ret = device_for_each_child(&vdev->dev, NULL, rpmsg_remove_device);
if (ret)
diff --git a/drivers/scsi/virtio_scsi.c b/drivers/scsi/virtio_scsi.c
index 28e1d98ae102..c616c7171cc4 100644
--- a/drivers/scsi/virtio_scsi.c
+++ b/drivers/scsi/virtio_scsi.c
@@ -778,7 +778,7 @@ static void virtscsi_init_vq(struct virtio_scsi_vq *virtscsi_vq,
static void virtscsi_remove_vqs(struct virtio_device *vdev)
{
/* Stop all the virtqueues. */
- vdev->config->reset(vdev);
+ virtio_reset_device(vdev);
vdev->config->del_vqs(vdev);
}
diff --git a/drivers/virtio/virtio.c b/drivers/virtio/virtio.c
index 236081afe9a2..00ac9db792a4 100644
--- a/drivers/virtio/virtio.c
+++ b/drivers/virtio/virtio.c
@@ -204,6 +204,12 @@ int virtio_finalize_features(struct virtio_device *dev)
}
EXPORT_SYMBOL_GPL(virtio_finalize_features);
+void virtio_reset_device(struct virtio_device *dev)
+{
+ dev->config->reset(dev);
+}
+EXPORT_SYMBOL_GPL(virtio_reset_device);
+
static int virtio_dev_probe(struct device *_d)
{
int err, i;
diff --git a/drivers/virtio/virtio_balloon.c b/drivers/virtio/virtio_balloon.c
index c22ff0117b46..f4c34a2a6b8e 100644
--- a/drivers/virtio/virtio_balloon.c
+++ b/drivers/virtio/virtio_balloon.c
@@ -1056,7 +1056,7 @@ static void remove_common(struct virtio_balloon *vb)
return_free_pages_to_mm(vb, ULONG_MAX);
/* Now we reset the device so we can clean up the queues. */
- vb->vdev->config->reset(vb->vdev);
+ virtio_reset_device(vb->vdev);
vb->vdev->config->del_vqs(vb->vdev);
}
diff --git a/drivers/virtio/virtio_input.c b/drivers/virtio/virtio_input.c
index ce51ae165943..3aa46703872d 100644
--- a/drivers/virtio/virtio_input.c
+++ b/drivers/virtio/virtio_input.c
@@ -347,7 +347,7 @@ static void virtinput_remove(struct virtio_device *vdev)
spin_unlock_irqrestore(&vi->lock, flags);
input_unregister_device(vi->idev);
- vdev->config->reset(vdev);
+ virtio_reset_device(vdev);
while ((buf = virtqueue_detach_unused_buf(vi->sts)) != NULL)
kfree(buf);
vdev->config->del_vqs(vdev);
diff --git a/drivers/virtio/virtio_mem.c b/drivers/virtio/virtio_mem.c
index 96e5a8782769..033fb93ed528 100644
--- a/drivers/virtio/virtio_mem.c
+++ b/drivers/virtio/virtio_mem.c
@@ -2850,7 +2850,7 @@ static void virtio_mem_remove(struct virtio_device *vdev)
virtio_mem_deinit_hotplug(vm);
/* reset the device and cleanup the queues */
- vdev->config->reset(vdev);
+ virtio_reset_device(vdev);
vdev->config->del_vqs(vdev);
kfree(vm);
diff --git a/fs/fuse/virtio_fs.c b/fs/fuse/virtio_fs.c
index 4cfa4bc1f579..ca2cac196d73 100644
--- a/fs/fuse/virtio_fs.c
+++ b/fs/fuse/virtio_fs.c
@@ -895,7 +895,7 @@ static int virtio_fs_probe(struct virtio_device *vdev)
return 0;
out_vqs:
- vdev->config->reset(vdev);
+ virtio_reset_device(vdev);
virtio_fs_cleanup_vqs(vdev, fs);
kfree(fs->vqs);
@@ -927,7 +927,7 @@ static void virtio_fs_remove(struct virtio_device *vdev)
list_del_init(&fs->list);
virtio_fs_stop_all_queues(fs);
virtio_fs_drain_all_queues_locked(fs);
- vdev->config->reset(vdev);
+ virtio_reset_device(vdev);
virtio_fs_cleanup_vqs(vdev, fs);
vdev->priv = NULL;
diff --git a/include/linux/virtio.h b/include/linux/virtio.h
index 41edbc01ffa4..72292a62cd90 100644
--- a/include/linux/virtio.h
+++ b/include/linux/virtio.h
@@ -138,6 +138,7 @@ int virtio_finalize_features(struct virtio_device *dev);
int virtio_device_freeze(struct virtio_device *dev);
int virtio_device_restore(struct virtio_device *dev);
#endif
+void virtio_reset_device(struct virtio_device *dev);
size_t virtio_max_dma_size(struct virtio_device *vdev);
diff --git a/net/9p/trans_virtio.c b/net/9p/trans_virtio.c
index bd5a89c4960d..8ff6d7160677 100644
--- a/net/9p/trans_virtio.c
+++ b/net/9p/trans_virtio.c
@@ -721,7 +721,7 @@ static void p9_virtio_remove(struct virtio_device *vdev)
mutex_unlock(&virtio_9p_lock);
- vdev->config->reset(vdev);
+ virtio_reset_device(vdev);
vdev->config->del_vqs(vdev);
sysfs_remove_file(&(vdev->dev.kobj), &dev_attr_mount_tag.attr);
diff --git a/net/vmw_vsock/virtio_transport.c b/net/vmw_vsock/virtio_transport.c
index 4f7c99dfd16c..fb3302fff627 100644
--- a/net/vmw_vsock/virtio_transport.c
+++ b/net/vmw_vsock/virtio_transport.c
@@ -665,7 +665,7 @@ static void virtio_vsock_remove(struct virtio_device *vdev)
vsock_for_each_connected_socket(virtio_vsock_reset_sock);
/* Stop all work handlers to make sure no one is accessing the device,
- * so we can safely call vdev->config->reset().
+ * so we can safely call virtio_reset_device().
*/
mutex_lock(&vsock->rx_lock);
vsock->rx_run = false;
@@ -682,7 +682,7 @@ static void virtio_vsock_remove(struct virtio_device *vdev)
/* Flush all device writes and interrupts, device will not use any
* more buffers.
*/
- vdev->config->reset(vdev);
+ virtio_reset_device(vdev);
mutex_lock(&vsock->rx_lock);
while ((pkt = virtqueue_detach_unused_buf(vsock->vqs[VSOCK_VQ_RX])))
diff --git a/sound/virtio/virtio_card.c b/sound/virtio/virtio_card.c
index 150ab3e37013..e2847c040f75 100644
--- a/sound/virtio/virtio_card.c
+++ b/sound/virtio/virtio_card.c
@@ -350,7 +350,7 @@ static void virtsnd_remove(struct virtio_device *vdev)
snd_card_free(snd->card);
vdev->config->del_vqs(vdev);
- vdev->config->reset(vdev);
+ virtio_reset_device(vdev);
for (i = 0; snd->substreams && i < snd->nsubstreams; ++i) {
struct virtio_pcm_substream *vss = &snd->substreams[i];
@@ -379,7 +379,7 @@ static int virtsnd_freeze(struct virtio_device *vdev)
virtsnd_ctl_msg_cancel_all(snd);
vdev->config->del_vqs(vdev);
- vdev->config->reset(vdev);
+ virtio_reset_device(vdev);
for (i = 0; i < snd->nsubstreams; ++i)
cancel_work_sync(&snd->substreams[i].elapsed_period);
]
|
| 54/2 |
2026/07/27 02:04 |
llm |
expert |
0m
Model:
Tokens:
input: 32036
output: 39
thoughts: 4206
Thoughts: **Analyzing a Use-After-Free in virtio_balloon**
Okay, so I'm looking at a use-after-free (UAF) bug in the virtio balloon driver, specifically within the `virtqueue_get_buf_ctx` function, which is called from `stats_handle_request`. The kernel's KFENCE is flagging this, pointing to an access of freed memory at a specific address related to a `skbuff_head_cache` object, though the actual allocation/free seem to involve `netdevsim` and `nsim_dev_trap_skb_build` and `nsim_dev_trap_report`. This is odd. The report shows a memory access at an offset relative to a KFENCE-protected object.
The UAF instruction is within `virtqueue_get_buf_ctx`, which gets a buffer context from the virtqueue. I’m zeroing in on this part: `mov (%r14), %r14d`, trying to figure out which memory location is being read from a freed `virtqueue` structure. The disassembly is showing an issue around reading the `layout` element. So, the bug seems to be that `vq` is used after it's freed.
My initial thought is that the virtqueue (`vq`) might have been freed by `remove_common`, called by `virtballoon_remove`. This sequence involves canceling works, and I have to trace it: `cancel_work_sync` *should* prevent further execution of the `update_balloon_stats_work` function, which calls `stats_handle_request`. The code correctly sets `stop_update = true` before the work is canceled, preventing new work from being queued.
My focus has shifted to understanding how a stale work item could still execute after it should have been stopped. The code goes into `virtballoon_freeze`, but this path isn't the problem, because it *should* wait for work queues to drain before doing its removal. The system workqueue is already frozen. My mind has also circled to the `virtballoon_restore` process, when a previous virtqueue could be in an unfrozen workqueue. But even so, the virtqueue is re-initialized in this context.
Is there any race? It would have to involve something starting and *not* being stopped. The `stats_request` queues more work. *But*, it checks `vb->stop_update`. And that's locked. And it's set before the cancellation. So it shouldn't get re-queued after the stop.
The UAF is on the `vq` pointer itself, which is set to `vb->stats_vq`. Is it that the `update_balloon_stats_func` gets called even after `virtballoon_remove` cancels the work? That doesn't seem possible.
|
| 55/2 |
2026/07/27 02:05 |
tool |
read-file |
0m
Args: map[File:drivers/virtio/virtio_balloon.c FirstLine:920 LineCount:50]
Results: map[Contents: 920: {
921: struct virtio_balloon *vb;
922: int err;
923:
924: if (!vdev->config->get) {
925: dev_err(&vdev->dev, "%s failure: config access disabled\n",
926: __func__);
927: return -EINVAL;
928: }
929:
930: vdev->priv = vb = kzalloc_obj(*vb);
931: if (!vb) {
932: err = -ENOMEM;
933: goto out;
934: }
935:
936: INIT_WORK(&vb->update_balloon_stats_work, update_balloon_stats_func);
937: INIT_WORK(&vb->update_balloon_size_work, update_balloon_size_func);
938: spin_lock_init(&vb->stop_update_lock);
939: mutex_init(&vb->balloon_lock);
940: init_waitqueue_head(&vb->acked);
941: vb->vdev = vdev;
942:
943: balloon_devinfo_init(&vb->vb_dev_info);
944:
945: err = init_vqs(vb);
946: if (err)
947: goto out_free_vb;
948:
949: if (!virtio_has_feature(vb->vdev, VIRTIO_BALLOON_F_DEFLATE_ON_OOM))
950: vb->vb_dev_info.adjust_managed_page_count = true;
951: #ifdef CONFIG_BALLOON_MIGRATION
952: vb->vb_dev_info.migratepage = virtballoon_migratepage;
953: #endif
954: if (virtio_has_feature(vdev, VIRTIO_BALLOON_F_FREE_PAGE_HINT)) {
955: /*
956: * There is always one entry reserved for cmd id, so the ring
957: * size needs to be at least two to report free page hints.
958: */
959: if (virtqueue_get_vring_size(vb->free_page_vq) < 2) {
960: err = -ENOSPC;
961: goto out_del_vqs;
962: }
963: vb->balloon_wq = alloc_workqueue("balloon-wq",
964: WQ_FREEZABLE | WQ_CPU_INTENSIVE | WQ_PERCPU,
965: 0);
966: if (!vb->balloon_wq) {
967: err = -ENOMEM;
968: goto out_del_vqs;
969: }
]
|
| 56/2 |
2026/07/27 02:05 |
llm |
expert |
0m
Model:
Tokens:
input: 33490
output: 25
thoughts: 3234
Thoughts: **Analyzing a Use-After-Free in virtqueue_get_buf_ctx**
Okay, so I'm looking at a bug, a use-after-free read within `virtqueue_get_buf_ctx`. The trace tells me it's triggered when `stats_handle_request` is called from `update_balloon_stats_func`, which is a workqueue function linked to `vb->update_balloon_stats_work`. My initial thought was that the UAF might be on `skbuff_head_cache`, but the KFENCE report shows something different. The KFENCE output clearly identifies the memory location, size 240, and the cache as `skbuff_head_cache`, and the stack traces show allocation and freeing happen with `nsim_dev_trap_skb_build` and `nsim_dev_trap_report`. This points to the memory previously holding an `sk_buff` being accessed.
The disassembly of `virtqueue_get_buf_ctx` reveals a read at `&vq->broken`, and I'm reminded that the first argument to this function is a `struct virtqueue *` (pointed to by `%rdi` which is `%rbx`). However, the KFENCE report's size of 240 bytes and the instruction accessing offset 72 from the base address, implies we are not using the standard `virtqueue`. I recall that `vring_virtqueue` has the `vq` (virtqueue) as the first member, so I dive into the struct layout: `broken` is at offset 66 bytes. The total size of `virtqueue` is 64 bytes and the `vring_virtqueue` size is about 224 bytes, and so it seems that `vring_virtqueue` is likely allocated from `kmalloc-256`, however the cache reported is still `skbuff_head_cache`, which is not the same place.
The KFENCE report also shows that the memory was first allocated as an `sk_buff` and later freed. Now I need to understand how `virtqueue_get_buf_ctx` is being called with a pointer to memory that was most recently used as an `sk_buff`. Ah, this makes more sense, `vb->stats_vq` likely became a dangling pointer. This happens when the original `vring_virtqueue` was freed, the memory was reallocated as an `sk_buff`, and *then* the `stats_handle_request` attempted to use the dangling pointer. This means the root cause is the `vb->stats_vq` being used after it was freed.
When does this freeing happen? `virtballoon_remove` is where virtqueues are freed, and this function cancels work. So the work function can't be queued during the device removal, due to `vb->stop_update` flag being set. `stats_request` queues the work, but there is a check if we must stop, and that flag is set *before* `remove_common` calls which do the actual freeing. The crucial thing is that `cancel_work_sync` *should* prevent the workqueue function from running after the virtqueue has been freed. But that isn't true for the UAF. Therefore, the race must be such that the work is *still* running, or scheduled, at the moment of freeing. This likely happens if the work is already in progress, and thus, needs a lock or other mechanism to guard this use.
|
| 57/2 |
2026/07/27 02:05 |
tool |
codesearch-find-references |
0m
Error:
Args: map[Name:update_balloon_stats_work]
Results: map[References:<nil> TruncatedOutput:false]
|
| 58/2 |
2026/07/27 02:05 |
llm |
expert |
0m
Model:
Tokens:
input: 34287
output: 30
thoughts: 0
|
| 59/2 |
2026/07/27 02:05 |
tool |
codesearch-find-references |
0m
Args: map[Name:virtio_balloon::update_balloon_stats_work]
Results: map[References:[map[ReferenceKind:takes-address-of ReferencingEntityKind:function ReferencingEntityName:stats_request SourceFile:drivers/virtio/virtio_balloon.c SourceLine:436] map[ReferenceKind:reads ReferencingEntityKind:function ReferencingEntityName:update_balloon_stats_func SourceFile:drivers/virtio/virtio_balloon.c SourceLine:543] map[ReferenceKind:takes-address-of ReferencingEntityKind:function ReferencingEntityName:virtballoon_probe SourceFile:drivers/virtio/virtio_balloon.c SourceLine:936] map[ReferenceKind:takes-address-of ReferencingEntityKind:function ReferencingEntityName:virtballoon_probe SourceFile:drivers/virtio/virtio_balloon.c SourceLine:936] map[ReferenceKind:takes-address-of ReferencingEntityKind:function ReferencingEntityName:virtballoon_probe SourceFile:drivers/virtio/virtio_balloon.c SourceLine:936] map[ReferenceKind:takes-address-of ReferencingEntityKind:function ReferencingEntityName:virtballoon_probe SourceFile:drivers/virtio/virtio_balloon.c SourceLine:936] map[ReferenceKind:takes-address-of ReferencingEntityKind:function ReferencingEntityName:virtballoon_probe SourceFile:drivers/virtio/virtio_balloon.c SourceLine:936] map[ReferenceKind:takes-address-of ReferencingEntityKind:function ReferencingEntityName:virtballoon_remove SourceFile:drivers/virtio/virtio_balloon.c SourceLine:1112]] TruncatedOutput:false]
|
| 60/2 |
2026/07/27 02:05 |
llm |
expert |
0m
Model:
Tokens:
input: 34784
output: 40
thoughts: 0
|
| 61/2 |
2026/07/27 02:05 |
tool |
read-file |
0m
Args: map[File:drivers/virtio/virtio_balloon.c FirstLine:1100 LineCount:50]
Results: map[Contents:1100: struct virtio_balloon *vb = vdev->priv;
1101:
1102: if (virtio_has_feature(vb->vdev, VIRTIO_BALLOON_F_REPORTING))
1103: page_reporting_unregister(&vb->pr_dev_info);
1104: if (virtio_has_feature(vb->vdev, VIRTIO_BALLOON_F_DEFLATE_ON_OOM))
1105: unregister_oom_notifier(&vb->oom_nb);
1106: if (virtio_has_feature(vb->vdev, VIRTIO_BALLOON_F_FREE_PAGE_HINT))
1107: virtio_balloon_unregister_shrinker(vb);
1108: spin_lock_irq(&vb->stop_update_lock);
1109: vb->stop_update = true;
1110: spin_unlock_irq(&vb->stop_update_lock);
1111: cancel_work_sync(&vb->update_balloon_size_work);
1112: cancel_work_sync(&vb->update_balloon_stats_work);
1113:
1114: if (virtio_has_feature(vdev, VIRTIO_BALLOON_F_FREE_PAGE_HINT)) {
1115: cancel_work_sync(&vb->report_free_page_work);
1116: destroy_workqueue(vb->balloon_wq);
1117: }
1118:
1119: remove_common(vb);
1120: mutex_destroy(&vb->balloon_lock);
1121: kfree(vb);
1122: }
1123:
1124: #ifdef CONFIG_PM_SLEEP
1125: static int virtballoon_freeze(struct virtio_device *vdev)
1126: {
1127: struct virtio_balloon *vb = vdev->priv;
1128:
1129: /*
1130: * The workqueue is already frozen by the PM core before this
1131: * function is called.
1132: */
1133: remove_common(vb);
1134: return 0;
1135: }
1136:
1137: static int virtballoon_restore(struct virtio_device *vdev)
1138: {
1139: struct virtio_balloon *vb = vdev->priv;
1140: int ret;
1141:
1142: ret = init_vqs(vdev->priv);
1143: if (ret)
1144: return ret;
1145:
1146: virtio_device_ready(vdev);
1147:
1148: if (towards_target(vb))
1149: virtballoon_changed(vdev);
]
|
| 62/2 |
2026/07/27 02:05 |
llm |
expert |
0m
Model:
Tokens:
input: 35626
output: 40
thoughts: 0
|
| 63/2 |
2026/07/27 02:05 |
tool |
read-file |
0m
Args: map[File:drivers/virtio/virtio_balloon.c FirstLine:1080 LineCount:50]
Results: map[Contents:1080:
1081: static void remove_common(struct virtio_balloon *vb)
1082: {
1083: /* There might be pages left in the balloon: free them. */
1084: while (vb->num_pages)
1085: leak_balloon(vb, vb->num_pages);
1086: update_balloon_size(vb);
1087:
1088: /* There might be free pages that are being reported: release them. */
1089: if (virtio_has_feature(vb->vdev, VIRTIO_BALLOON_F_FREE_PAGE_HINT))
1090: return_free_pages_to_mm(vb, ULONG_MAX);
1091:
1092: /* Now we reset the device so we can clean up the queues. */
1093: virtio_reset_device(vb->vdev);
1094:
1095: vb->vdev->config->del_vqs(vb->vdev);
1096: }
1097:
1098: static void virtballoon_remove(struct virtio_device *vdev)
1099: {
1100: struct virtio_balloon *vb = vdev->priv;
1101:
1102: if (virtio_has_feature(vb->vdev, VIRTIO_BALLOON_F_REPORTING))
1103: page_reporting_unregister(&vb->pr_dev_info);
1104: if (virtio_has_feature(vb->vdev, VIRTIO_BALLOON_F_DEFLATE_ON_OOM))
1105: unregister_oom_notifier(&vb->oom_nb);
1106: if (virtio_has_feature(vb->vdev, VIRTIO_BALLOON_F_FREE_PAGE_HINT))
1107: virtio_balloon_unregister_shrinker(vb);
1108: spin_lock_irq(&vb->stop_update_lock);
1109: vb->stop_update = true;
1110: spin_unlock_irq(&vb->stop_update_lock);
1111: cancel_work_sync(&vb->update_balloon_size_work);
1112: cancel_work_sync(&vb->update_balloon_stats_work);
1113:
1114: if (virtio_has_feature(vdev, VIRTIO_BALLOON_F_FREE_PAGE_HINT)) {
1115: cancel_work_sync(&vb->report_free_page_work);
1116: destroy_workqueue(vb->balloon_wq);
1117: }
1118:
1119: remove_common(vb);
1120: mutex_destroy(&vb->balloon_lock);
1121: kfree(vb);
1122: }
1123:
1124: #ifdef CONFIG_PM_SLEEP
1125: static int virtballoon_freeze(struct virtio_device *vdev)
1126: {
1127: struct virtio_balloon *vb = vdev->priv;
1128:
1129: /*
]
|
| 64/2 |
2026/07/27 02:05 |
llm |
expert |
0m
Model:
Tokens:
input: 36542
output: 24
thoughts: 0
|
| 65/2 |
2026/07/27 02:05 |
tool |
codesearch-find-references |
0m
Args: map[Name:virtio_reset_device]
Results: map[References:[map[ReferenceKind:calls ReferencingEntityKind:function ReferencingEntityName:p9_virtio_remove SourceFile:net/9p/trans_virtio.c SourceLine:763] map[ReferenceKind:calls ReferencingEntityKind:function ReferencingEntityName:register_virtio_device SourceFile:drivers/virtio/virtio.c SourceLine:557] map[ReferenceKind:calls ReferencingEntityKind:function ReferencingEntityName:remove_common SourceFile:drivers/char/hw_random/virtio-rng.c SourceLine:207] map[ReferenceKind:calls ReferencingEntityKind:function ReferencingEntityName:remove_common SourceFile:drivers/virtio/virtio_balloon.c SourceLine:1093] map[ReferenceKind:calls ReferencingEntityKind:function ReferencingEntityName:remove_vq_common SourceFile:drivers/net/virtio_net.c SourceLine:7145] map[ReferenceKind:calls ReferencingEntityKind:function ReferencingEntityName:remove_vqs SourceFile:drivers/net/wireless/virtual/mac80211_hwsim_main.c SourceLine:7367] map[ReferenceKind:calls ReferencingEntityKind:function ReferencingEntityName:virtblk_freeze_priv SourceFile:drivers/block/virtio_blk.c SourceLine:1602] map[ReferenceKind:calls ReferencingEntityKind:function ReferencingEntityName:virtblk_remove SourceFile:drivers/block/virtio_blk.c SourceLine:1577] map[ReferenceKind:calls ReferencingEntityKind:function ReferencingEntityName:virtcons_freeze SourceFile:drivers/char/virtio_console.c SourceLine:2105] map[ReferenceKind:calls ReferencingEntityKind:function ReferencingEntityName:virtcons_remove SourceFile:drivers/char/virtio_console.c SourceLine:1927] map[ReferenceKind:calls ReferencingEntityKind:function ReferencingEntityName:virtcrypto_freeze SourceFile:drivers/crypto/virtio/virtio_crypto_core.c SourceLine:525] map[ReferenceKind:calls ReferencingEntityKind:function ReferencingEntityName:virtcrypto_probe SourceFile:drivers/crypto/virtio/virtio_crypto_core.c SourceLine:469] map[ReferenceKind:calls ReferencingEntityKind:function ReferencingEntityName:virtcrypto_remove SourceFile:drivers/crypto/virtio/virtio_crypto_core.c SourceLine:504] map[ReferenceKind:calls ReferencingEntityKind:function ReferencingEntityName:virtcrypto_restore SourceFile:drivers/crypto/virtio/virtio_crypto_core.c SourceLine:561] map[ReferenceKind:calls ReferencingEntityKind:function ReferencingEntityName:virtinput_freeze SourceFile:drivers/virtio/virtio_input.c SourceLine:372] map[ReferenceKind:calls ReferencingEntityKind:function ReferencingEntityName:virtinput_remove SourceFile:drivers/virtio/virtio_input.c SourceLine:354] map[ReferenceKind:calls ReferencingEntityKind:function ReferencingEntityName:virtio_device_restore_priv SourceFile:drivers/virtio/virtio.c SourceLine:607] map[ReferenceKind:calls ReferencingEntityKind:function ReferencingEntityName:virtio_fs_probe SourceFile:fs/fuse/virtio_fs.c SourceLine:1179] map[ReferenceKind:calls ReferencingEntityKind:function ReferencingEntityName:virtio_fs_remove SourceFile:fs/fuse/virtio_fs.c SourceLine:1214] map[ReferenceKind:calls ReferencingEntityKind:function ReferencingEntityName:virtio_gpu_deinit SourceFile:drivers/gpu/drm/virtio/virtgpu_kms.c SourceLine:300] map[ReferenceKind:calls ReferencingEntityKind:function ReferencingEntityName:virtio_mem_freeze SourceFile:drivers/virtio/virtio_mem.c SourceLine:3105] map[ReferenceKind:calls ReferencingEntityKind:function ReferencingEntityName:virtio_mem_remove SourceFile:drivers/virtio/virtio_mem.c SourceLine:3068] map[ReferenceKind:calls ReferencingEntityKind:function ReferencingEntityName:virtio_pmem_freeze SourceFile:drivers/nvdimm/virtio_pmem.c SourceLine:150] map[ReferenceKind:calls ReferencingEntityKind:function ReferencingEntityName:virtio_pmem_probe SourceFile:drivers/nvdimm/virtio_pmem.c SourceLine:130] map[ReferenceKind:calls ReferencingEntityKind:function ReferencingEntityName:virtio_pmem_remove SourceFile:drivers/nvdimm/virtio_pmem.c SourceLine:144] map[ReferenceKind:calls ReferencingEntityKind:function ReferencingEntityName:virtio_vsock_vqs_del SourceFile:net/vmw_vsock/virtio_transport.c SourceLine:780] map[ReferenceKind:calls ReferencingEntityKind:function ReferencingEntityName:virtnet_probe SourceFile:drivers/net/virtio_net.c SourceLine:7134] map[ReferenceKind:calls ReferencingEntityKind:function ReferencingEntityName:virtnet_restore_up SourceFile:drivers/net/virtio_net.c SourceLine:5766] map[ReferenceKind:calls ReferencingEntityKind:function ReferencingEntityName:virtnet_restore_up SourceFile:drivers/net/virtio_net.c SourceLine:5773] map[ReferenceKind:calls ReferencingEntityKind:function ReferencingEntityName:virtscsi_remove_vqs SourceFile:drivers/scsi/virtio_scsi.c SourceLine:838] map[ReferenceKind:calls ReferencingEntityKind:function ReferencingEntityName:virtsnd_freeze SourceFile:sound/virtio/virtio_card.c SourceLine:387] map[ReferenceKind:calls ReferencingEntityKind:function ReferencingEntityName:virtsnd_remove SourceFile:sound/virtio/virtio_card.c SourceLine:358] map[ReferenceKind:takes-address-of ReferencingEntityKind:global_variable ReferencingEntityName:__UNIQUE_ID_addressable_virtio_reset_device_786 SourceFile:drivers/virtio/virtio.c SourceLine:268] map[ReferenceKind:takes-address-of ReferencingEntityKind:global_variable ReferencingEntityName:__gendwarfksyms_ptr_virtio_reset_device SourceFile:drivers/virtio/virtio.c SourceLine:268] map[ReferenceKind:takes-address-of ReferencingEntityKind:global_variable ReferencingEntityName:__gendwarfksyms_ptr_virtio_reset_device SourceFile:drivers/virtio/virtio.c SourceLine:268]] TruncatedOutput:false]
|
| 66/2 |
2026/07/27 02:05 |
llm |
expert |
0m
Model:
Tokens:
input: 38543
output: 62
thoughts: 0
|
| 67/2 |
2026/07/27 02:05 |
tool |
git-log |
0m
Error:
Args: map[CodeRegexp:cancel_work_sync.*update_balloon_stats_work Count:10 SourcePath:drivers/virtio/virtio_balloon.c SymbolName:virtballoon_remove]
|
| 68/2 |
2026/07/27 02:05 |
llm |
expert |
0m
Model:
Tokens:
input: 47181
output: 62
thoughts: 0
|
| 69/2 |
2026/07/27 02:06 |
tool |
git-log |
0m
Args: map[CodeRegexp:cancel_work_sync.*update_balloon_stats_work Count:10 SourcePath:drivers/virtio/virtio_balloon.c SymbolName:virtballoon_restore]
Results: map[Output:fad7b7b27b6a virtio_balloon: Use a workqueue instead of "vballoon" kthread
486d2e632ca1 virtio_balloon: enable VQs early on restore
c45b4166d9ea virtio: balloon: drop restore_common()
f38f8387cbdc virtio: drop thaw PM operation
e47d854e5769 virtio: balloon: Allow stats update after restore from S4
4eb05d562ea1 virtio: balloon: leak / fill balloon across S4
e562966dbaf4 virtio: balloon: Add freeze, restore handlers to support S4
]
|
| 70/2 |
2026/07/27 02:06 |
llm |
expert |
0m
Model:
Tokens:
input: 47421
output: 39
thoughts: 1109
Thoughts: **The Bug Hunt is On!**
Okay, I think I've got it. The initial thought was about `virtballoon_remove` calling `cancel_work_sync` on `vb->update_balloon_stats_work`, but then I saw that `virtballoon_freeze` doesn't do the same. It just assumes the work won't run because the workqueue is frozen. But what if it's already queued and pending?
The system resumes, and `virtballoon_restore` is called. `init_vqs` is what allocates new virtqueues and assigns them to `vb->stats_vq`. My first thought was maybe it's a use-after-free situation, with the work trying to use the old, freed virtqueue. If it's using the *new* virtqueue, everything is fine.
The next thing I considered was what if the work was already *running* when the freeze happened? But the freeze should wait for running work to finish, so that can't be it. Then I started thinking about the case where the work is queued *before* the freeze, and then the freeze happens. `remove_common` deletes the old virtqueues, and then `init_vqs` creates the new ones.
The work then runs. It calls `stats_handle_request`, which uses `vb->stats_vq`. So the question becomes, is `vb->stats_vq` pointing to the old or the new virtqueue? If it's the new one, it's fine, no UAF. The core of the issue is what `stats_handle_request` does. If it's the *new* virtqueue, there shouldn't be a problem.
Ah, I see it! I was concerned about it using the local variable `vq`. But because `system_freezable_wq` is a freezable workqueue, the freezer is supposed to wait for running works on freezable workqueues to complete before it proceeds. That means it can't be preempted and suspended across a freeze.
Then I checked for other ways `update_balloon_stats_func` could run concurrently with `remove_common`. Could `virtballoon_remove` trigger something? No, because `cancel_work_sync` waits for the work to finish. What about `virtballoon_oom_notify`? Nope. `virtballoon_changed`? Nope.
Okay, let's look at the crash trace again... wait a minute... the crash is in `virtqueue_get_buf_ctx`. Is it possible that `stats_vq` isn't properly updated in `init_vqs`? I need to go back and check the code for `init_vqs`!
|
| 71/2 |
2026/07/27 02:06 |
tool |
read-file |
0m
Args: map[File:drivers/virtio/virtio_balloon.c FirstLine:570 LineCount:50]
Results: map[Contents: 570:
571: if (diff)
572: queue_work(system_freezable_wq, work);
573: else
574: finish_wakeup_event(vb);
575: }
576:
577: static int init_vqs(struct virtio_balloon *vb)
578: {
579: struct virtqueue_info vqs_info[VIRTIO_BALLOON_VQ_MAX] = {};
580: struct virtqueue *vqs[VIRTIO_BALLOON_VQ_MAX];
581: int err;
582:
583: /*
584: * Inflateq and deflateq are used unconditionally. The names[]
585: * will be NULL if the related feature is not enabled, which will
586: * cause no allocation for the corresponding virtqueue in find_vqs.
587: */
588: vqs_info[VIRTIO_BALLOON_VQ_INFLATE].callback = balloon_ack;
589: vqs_info[VIRTIO_BALLOON_VQ_INFLATE].name = "inflate";
590: vqs_info[VIRTIO_BALLOON_VQ_DEFLATE].callback = balloon_ack;
591: vqs_info[VIRTIO_BALLOON_VQ_DEFLATE].name = "deflate";
592:
593: if (virtio_has_feature(vb->vdev, VIRTIO_BALLOON_F_STATS_VQ)) {
594: vqs_info[VIRTIO_BALLOON_VQ_STATS].name = "stats";
595: vqs_info[VIRTIO_BALLOON_VQ_STATS].callback = stats_request;
596: }
597:
598: if (virtio_has_feature(vb->vdev, VIRTIO_BALLOON_F_FREE_PAGE_HINT))
599: vqs_info[VIRTIO_BALLOON_VQ_FREE_PAGE].name = "free_page_vq";
600:
601: if (virtio_has_feature(vb->vdev, VIRTIO_BALLOON_F_REPORTING)) {
602: vqs_info[VIRTIO_BALLOON_VQ_REPORTING].name = "reporting_vq";
603: vqs_info[VIRTIO_BALLOON_VQ_REPORTING].callback = balloon_ack;
604: }
605:
606: err = virtio_find_vqs(vb->vdev, VIRTIO_BALLOON_VQ_MAX, vqs,
607: vqs_info, NULL);
608: if (err)
609: return err;
610:
611: vb->inflate_vq = vqs[VIRTIO_BALLOON_VQ_INFLATE];
612: vb->deflate_vq = vqs[VIRTIO_BALLOON_VQ_DEFLATE];
613: if (virtio_has_feature(vb->vdev, VIRTIO_BALLOON_F_STATS_VQ)) {
614: struct scatterlist sg;
615: unsigned int num_stats;
616: vb->stats_vq = vqs[VIRTIO_BALLOON_VQ_STATS];
617:
618: /*
619: * Prime this virtqueue with one buffer so the hypervisor can
]
|
| 72/2 |
2026/07/27 02:06 |
llm |
expert |
0m
Model:
Tokens:
input: 50136
output: 21
thoughts: 3272
Thoughts: **Analyzing Potential Use-After-Free in Virtio Balloon Driver**
Okay, so I've been wrestling with a potential use-after-free (UAF) bug in the Virtio Balloon driver, and I think I'm starting to zero in on the root cause. My primary focus is on how work items, particularly `update_balloon_stats_work`, interact with device removal and restoration during power management.
Initially, I was worried about the standard race conditions between `stats_request` and `virtballoon_remove`, but after carefully examining the locking in `stats_request`, I've convinced myself that the `stop_update_lock` spinlock prevents any issues there. The lock ensures that work is queued atomically, and `virtballoon_remove` correctly blocks when it tries to shut down the device.
Then, I shifted my focus to the re-queuing behavior of `update_balloon_size_work`, but I'm not convinced it's related to my UAF. The re-queue-related hanging scenarios are not relevant to my crash in `update_balloon_stats_work`.
The key scenario appears to be around the workqueue and device suspend/resume. If `update_balloon_stats_work` is queued *before* the workqueue is frozen during suspend, that work item would remain pending, even after the device is removed via `remove_common`. When `virtballoon_restore` runs after a resume, it attempts to re-initialize the virtqueues using `init_vqs`. Now, if `init_vqs` *succeeds*, there's no problem. The restored virtqueue `vb->stats_vq` is correctly re-initialized. However, what if `init_vqs` *fails* during the restore? My assumption is that the device would be left in a broken state, but, more importantly, the workqueue is unfrozen! So the *pending* `update_balloon_stats_work` would be executed with the OLD (freed) `vb->stats_vq` leading to the UAF!
I've also confirmed that `cancel_work_sync` *does* work as expected in canceling the work.
My primary theory is: the failure of `init_vqs` during device restore, coupled with an unfrozen workqueue, is the most likely cause of the UAF. This explains the need to check all callers of `remove_common`. If the UAF happened elsewhere, then `virtqueue_get_buf` may have concurrent access to the `vq` pointer. However, `cancel_work_sync` is blocking, so I do not believe this is possible.
|
| 73/2 |
2026/07/27 02:06 |
tool |
codesearch-find-references |
0m
Args:
Results: map[References:[map[ReferenceKind:calls ReferencingEntityKind:function ReferencingEntityName:virtballoon_freeze SourceFile:drivers/virtio/virtio_balloon.c SourceLine:1133] map[ReferenceKind:calls ReferencingEntityKind:function ReferencingEntityName:virtballoon_remove SourceFile:drivers/virtio/virtio_balloon.c SourceLine:1119]] TruncatedOutput:false]
|
| 74/2 |
2026/07/27 02:06 |
llm |
expert |
0m
Model:
Tokens:
input: 41392
output: 62
thoughts: 0
|
| 75/2 |
2026/07/27 02:06 |
tool |
git-log |
0m
Args: map[CodeRegexp:cancel_work_sync.*update_balloon_stats_work Count:10 SourcePath:drivers/virtio/virtio_balloon.c SymbolName:virtballoon_changed]
Results: map[Output:b12fbc3f787e virtio_balloon: stay awake while adjusting balloon
bf4dc0b2beeb virtio-balloon: tweak config_changed implementation
86a559787e6f virtio-balloon: VIRTIO_BALLOON_F_FREE_PAGE_HINT
]
|
| 76/2 |
2026/07/27 02:06 |
llm |
expert |
0m
Model:
Tokens:
input: 54271
output: 23
thoughts: 886
Thoughts: **Deep Dive into a Potential UAF**
Okay, let's break this down. So, `remove_common` is only called from `virtballoon_freeze` and `virtballoon_remove`. We already poked around in `virtballoon_remove`, which seemed okay because of `cancel_work_sync`. I'm starting to think `virtballoon_freeze` is where the real nastiness lies, specifically if `init_vqs` fails during `virtballoon_restore`, which would lead to a UAF. But is it *impossible* to get a UAF without an `init_vqs` failure?
What if `virtballoon_freeze` is called, the workqueue gets frozen, and `remove_common` is called and deletes the virtqueues? Then the system resumes, but critically, `virtballoon_restore` isn't called? No, the PM core calls `virtballoon_restore`, so that's not the path. Wait a second... what if the device is REMOVED while it's suspended? In that case, `virtballoon_remove` would be called. Now I'm remembering that `virtballoon_remove` calls `cancel_work_sync`. BUT, if the workqueue is frozen, wouldn't `cancel_work_sync` HANG? No, `cancel_work_sync` handles that. It can cancel pending work even on a frozen workqueue. If it's pending, it just removes it and returns.
So, `virtballoon_remove` calls `remove_common` *again*? Hold on... `virtballoon_freeze` already called `remove_common`! This means `remove_common` would be called twice. Double calling `remove_common` means double calling `virtio_reset_device` and `del_vqs`. Is `del_vqs` safe to call twice? My gut says that normally `del_vqs` sets `vdev->vqs` to empty so it *might* be safe, but it's more likely to crash. But that's a different problem.
Let's go back to the bug report. Okay, the report says:
```
CPU: 0 UID: 0 PID: 25534 Comm: kworker/0:1 Tainted: G L syzkaller #0 PREEMPT_{RT,(full)}
Tainted: [L]=SOFTLOCKUP
Hardware name: Google Google Compute Engine/Google Compute Engine, BIOS Google 07/16/2026
Workqueue: events_freezable update_balloon_stats_func
```
*Tainted: [L]=SOFTLOCKUP* ... And it's `syzkaller` running. Now I'm paying attention. Syzkaller doesn't usually do suspend/resume unless it's specifically testing it. But it *can* trigger suspend/resume via `/sys/power/state`. Okay, so we're looking at a suspend/resume scenario.
Okay. Is there any other way `update_balloon_stats_func` can run while the virtqueue is freed? What if `stats_handle_request` is running? If it calls `virtqueue_get_buf` and that accesses the virtqueue. Could `vq` be freed by a concurrent `virtballoon_remove`? Hmm... I've established `virtballoon_remove` calls `cancel_work_sync`, which *waits* for `update_balloon_stats_func` to finish, so it *can't* run concurrently. Wait... is `cancel_work_sync` called on the RIGHT work item?
```c
cancel_work_sync(&vb->update_balloon_size_work);
cancel_work_sync(&vb->update_balloon_stats_work);
```
Yep, that's right. Wait... what if `vb` is freed BEFORE `update_balloon_stats_func` finishes? How? `cancel_work_sync` is supposed to wait for it. Okay, wait...is it possible that `update_balloon_stats_func` is NOT the one being canceled? No, it IS `&vb->update_balloon_stats_work`.
Wait... Could `stats_handle_request` be called from *somewhere ELSE*? Let's check callers of `stats_handle_request`....
|
| 77/2 |
2026/07/27 02:07 |
tool |
codesearch-find-references |
0m
Args: map[Name:stats_handle_request]
Results: map[References:[map[ReferenceKind:calls ReferencingEntityKind:function ReferencingEntityName:update_balloon_stats_func SourceFile:drivers/virtio/virtio_balloon.c SourceLine:547]] TruncatedOutput:false]
|
| 78/2 |
2026/07/27 02:07 |
llm |
expert |
0m
Model:
Tokens:
input: 42582
output: 26
thoughts: 0
|
| 79/2 |
2026/07/27 02:07 |
tool |
codesearch-find-references |
0m
Args: map[Name:virtqueue_get_buf_ctx]
Results: map[References:[map[ReferenceKind:calls ReferencingEntityKind:function ReferencingEntityName:virtnet_rq_get_buf SourceFile:drivers/net/virtio_net.c SourceLine:934] map[ReferenceKind:calls ReferencingEntityKind:function ReferencingEntityName:virtqueue_get_buf SourceFile:drivers/virtio/virtio_ring.c SourceLine:3135] map[ReferenceKind:takes-address-of ReferencingEntityKind:global_variable ReferencingEntityName:__UNIQUE_ID_addressable_virtqueue_get_buf_ctx_860 SourceFile:drivers/virtio/virtio_ring.c SourceLine:3131] map[ReferenceKind:takes-address-of ReferencingEntityKind:global_variable ReferencingEntityName:__gendwarfksyms_ptr_virtqueue_get_buf_ctx SourceFile:drivers/virtio/virtio_ring.c SourceLine:3131] map[ReferenceKind:takes-address-of ReferencingEntityKind:global_variable ReferencingEntityName:__gendwarfksyms_ptr_virtqueue_get_buf_ctx SourceFile:drivers/virtio/virtio_ring.c SourceLine:3131]] TruncatedOutput:false]
|
| 80/2 |
2026/07/27 02:07 |
llm |
expert |
0m
Model:
Tokens:
input: 56572
output: 35
thoughts: 168
Thoughts: **My Reasoning on Balloon Stats Update**
Okay, let's break this down. From what I'm seeing, `stats_handle_request` seems to be solely called from `update_balloon_stats_func`. And, that function, in turn, is *only* called by the workqueue associated with `vb->update_balloon_stats_work`. Now, `virtballoon_remove` explicitly calls `cancel_work_sync(&vb->update_balloon_stats_work)`. This strongly suggests that `virtballoon_remove` is intentionally waiting for the completion of `update_balloon_stats_func` before proceeding. In essence, it appears designed to ensure a clean shutdown, synchronizing the work queue.
But hold on a second... what if `virtballoon_remove` *isn't* always called? This is a crucial question. Is it possible for the virtual balloon's resources, particularly the virtqueue, to be deallocated via another path? Could a scenario exist where `del_vqs` is invoked without a preceding call to `virtballoon_remove`? To answer this, I need to investigate where `del_vqs` is actually used within `virtio_balloon.c`. Time to scrutinize the code for its call sites. Let's see...
|
| 81/2 |
2026/07/27 02:07 |
tool |
grepper |
0m
Args: map[Expression:del_vqs PathPrefix:drivers/virtio/virtio_balloon.c]
Results: map[Output:drivers/virtio/virtio_balloon.c=919=static int virtballoon_probe(struct virtio_device *vdev)
--
drivers/virtio/virtio_balloon.c-960- err = -ENOSPC;
drivers/virtio/virtio_balloon.c:961: goto out_del_vqs;
drivers/virtio/virtio_balloon.c-962- }
--
drivers/virtio/virtio_balloon.c-967- err = -ENOMEM;
drivers/virtio/virtio_balloon.c:968: goto out_del_vqs;
drivers/virtio/virtio_balloon.c-969- }
--
drivers/virtio/virtio_balloon.c-1071- destroy_workqueue(vb->balloon_wq);
drivers/virtio/virtio_balloon.c:1072:out_del_vqs:
drivers/virtio/virtio_balloon.c:1073: vdev->config->del_vqs(vdev);
drivers/virtio/virtio_balloon.c-1074-out_free_vb:
--
drivers/virtio/virtio_balloon.c=1081=static void remove_common(struct virtio_balloon *vb)
--
drivers/virtio/virtio_balloon.c-1094-
drivers/virtio/virtio_balloon.c:1095: vb->vdev->config->del_vqs(vb->vdev);
drivers/virtio/virtio_balloon.c-1096-}
]
|
| 82/2 |
2026/07/27 02:07 |
llm |
expert |
0m
Model:
Tokens:
input: 43676
output: 24
thoughts: 0
|
| 83/2 |
2026/07/27 02:07 |
tool |
codesearch-find-references |
0m
Args: map[Name:virtqueue_get_buf]
Results: map[References:[map[ReferenceKind:calls ReferencingEntityKind:function ReferencingEntityName:__free_old_xmit SourceFile:drivers/net/virtio_net.c SourceLine:588] map[ReferenceKind:calls ReferencingEntityKind:function ReferencingEntityName:__send_control_msg SourceFile:drivers/char/virtio_console.c SourceLine:561] map[ReferenceKind:calls ReferencingEntityKind:function ReferencingEntityName:__send_to_port SourceFile:drivers/char/virtio_console.c SourceLine:636] map[ReferenceKind:calls ReferencingEntityKind:function ReferencingEntityName:control_work_handler SourceFile:drivers/char/virtio_console.c SourceLine:1671] map[ReferenceKind:calls ReferencingEntityKind:function ReferencingEntityName:flush_bufs SourceFile:drivers/char/virtio_console.c SourceLine:1694] map[ReferenceKind:calls ReferencingEntityKind:function ReferencingEntityName:get_free_page_and_send SourceFile:drivers/virtio/virtio_balloon.c SourceLine:702] map[ReferenceKind:calls ReferencingEntityKind:function ReferencingEntityName:get_inbuf SourceFile:drivers/char/virtio_console.c SourceLine:468] map[ReferenceKind:calls ReferencingEntityKind:function ReferencingEntityName:hwsim_virtio_rx_work SourceFile:drivers/net/wireless/virtual/mac80211_hwsim_main.c SourceLine:7296] map[ReferenceKind:calls ReferencingEntityKind:function ReferencingEntityName:hwsim_virtio_tx_done SourceFile:drivers/net/wireless/virtual/mac80211_hwsim_main.c SourceLine:7238] map[ReferenceKind:calls ReferencingEntityKind:function ReferencingEntityName:random_recv_done SourceFile:drivers/char/hw_random/virtio-rng.c SourceLine:48] map[ReferenceKind:calls ReferencingEntityKind:function ReferencingEntityName:reclaim_consumed_buffers SourceFile:drivers/char/virtio_console.c SourceLine:590] map[ReferenceKind:calls ReferencingEntityKind:function ReferencingEntityName:reclaim_vbufs SourceFile:drivers/gpu/drm/virtio/virtgpu_vq.c SourceLine:217] map[ReferenceKind:calls ReferencingEntityKind:function ReferencingEntityName:req_done SourceFile:net/9p/trans_virtio.c SourceLine:139] map[ReferenceKind:calls ReferencingEntityKind:function ReferencingEntityName:send_cmd_id_start SourceFile:drivers/virtio/virtio_balloon.c SourceLine:664] map[ReferenceKind:calls ReferencingEntityKind:function ReferencingEntityName:send_cmd_id_stop SourceFile:drivers/virtio/virtio_balloon.c SourceLine:683] map[ReferenceKind:calls ReferencingEntityKind:function ReferencingEntityName:stats_handle_request SourceFile:drivers/virtio/virtio_balloon.c SourceLine:450] map[ReferenceKind:calls ReferencingEntityKind:function ReferencingEntityName:tell_host SourceFile:drivers/virtio/virtio_balloon.c SourceLine:195] map[ReferenceKind:calls ReferencingEntityKind:function ReferencingEntityName:tell_host SourceFile:drivers/virtio/virtio_balloon.c SourceLine:195] map[ReferenceKind:calls ReferencingEntityKind:function ReferencingEntityName:tell_host SourceFile:drivers/virtio/virtio_balloon.c SourceLine:195] map[ReferenceKind:calls ReferencingEntityKind:function ReferencingEntityName:virtballoon_free_page_report SourceFile:drivers/virtio/virtio_balloon.c SourceLine:221] map[ReferenceKind:calls ReferencingEntityKind:function ReferencingEntityName:virtballoon_free_page_report SourceFile:drivers/virtio/virtio_balloon.c SourceLine:221] map[ReferenceKind:calls ReferencingEntityKind:function ReferencingEntityName:virtballoon_free_page_report SourceFile:drivers/virtio/virtio_balloon.c SourceLine:221] map[ReferenceKind:calls ReferencingEntityKind:function ReferencingEntityName:virtblk_done SourceFile:drivers/block/virtio_blk.c SourceLine:362] map[ReferenceKind:calls ReferencingEntityKind:function ReferencingEntityName:virtblk_poll SourceFile:drivers/block/virtio_blk.c SourceLine:1215] map[ReferenceKind:calls ReferencingEntityKind:function ReferencingEntityName:virtcrypto_ctrlq_callback SourceFile:drivers/crypto/virtio/virtio_crypto_core.c SourceLine:40] map[ReferenceKind:calls ReferencingEntityKind:function ReferencingEntityName:virtcrypto_done_work SourceFile:drivers/crypto/virtio/virtio_crypto_core.c SourceLine:84] map[ReferenceKind:calls ReferencingEntityKind:function ReferencingEntityName:virtinput_recv_events SourceFile:drivers/virtio/virtio_input.c SourceLine:45] map[ReferenceKind:calls ReferencingEntityKind:function ReferencingEntityName:virtinput_recv_status SourceFile:drivers/virtio/virtio_input.c SourceLine:117] map[ReferenceKind:calls ReferencingEntityKind:function ReferencingEntityName:virtio_fs_hiprio_done_work SourceFile:fs/fuse/virtio_fs.c SourceLine:545] map[ReferenceKind:calls ReferencingEntityKind:function ReferencingEntityName:virtio_fs_requests_done_work SourceFile:fs/fuse/virtio_fs.c SourceLine:844] map[ReferenceKind:calls ReferencingEntityKind:function ReferencingEntityName:virtio_mem_send_request SourceFile:drivers/virtio/virtio_mem.c SourceLine:1414] map[ReferenceKind:calls ReferencingEntityKind:function ReferencingEntityName:virtio_mem_send_request SourceFile:drivers/virtio/virtio_mem.c SourceLine:1414] map[ReferenceKind:calls ReferencingEntityKind:function ReferencingEntityName:virtio_mem_send_request SourceFile:drivers/virtio/virtio_mem.c SourceLine:1414] map[ReferenceKind:calls ReferencingEntityKind:function ReferencingEntityName:virtio_pmem_host_ack SourceFile:drivers/nvdimm/nd_virtio.c SourceLine:21] map[ReferenceKind:calls ReferencingEntityKind:function ReferencingEntityName:virtio_transport_event_work SourceFile:net/vmw_vsock/virtio_transport.c SourceLine:466] map[ReferenceKind:calls ReferencingEntityKind:function ReferencingEntityName:virtio_transport_rx_work SourceFile:net/vmw_vsock/virtio_transport.c SourceLine:659] map[ReferenceKind:calls ReferencingEntityKind:function ReferencingEntityName:virtio_transport_tx_work SourceFile:net/vmw_vsock/virtio_transport.c SourceLine:360] map[ReferenceKind:calls ReferencingEntityKind:function ReferencingEntityName:virtnet_receive_packets SourceFile:drivers/net/virtio_net.c SourceLine:2908] map[ReferenceKind:calls ReferencingEntityKind:function ReferencingEntityName:virtnet_receive_xsk_bufs SourceFile:drivers/net/virtio_net.c SourceLine:2878] map[ReferenceKind:calls ReferencingEntityKind:function ReferencingEntityName:virtnet_send_command_reply SourceFile:drivers/net/virtio_net.c SourceLine:3585] map[ReferenceKind:calls ReferencingEntityKind:function ReferencingEntityName:virtscsi_mq_poll SourceFile:drivers/scsi/virtio_scsi.c SourceLine:768] map[ReferenceKind:calls ReferencingEntityKind:function ReferencingEntityName:virtscsi_vq_done SourceFile:drivers/scsi/virtio_scsi.c SourceLine:193] map[ReferenceKind:calls ReferencingEntityKind:function ReferencingEntityName:virtsnd_ctl_notify_cb SourceFile:sound/virtio/virtio_ctl_msg.c SourceLine:300] map[ReferenceKind:calls ReferencingEntityKind:function ReferencingEntityName:virtsnd_disable_event_vq SourceFile:sound/virtio/virtio_card.c SourceLine:180] map[ReferenceKind:calls ReferencingEntityKind:function ReferencingEntityName:virtsnd_event_notify_cb SourceFile:sound/virtio/virtio_card.c SourceLine:92] map[ReferenceKind:calls ReferencingEntityKind:function ReferencingEntityName:virtsnd_pcm_notify_cb SourceFile:sound/virtio/virtio_pcm_msg.c SourceLine:351] map[ReferenceKind:calls ReferencingEntityKind:function ReferencingEntityName:vp_modern_avq_done SourceFile:drivers/virtio/virtio_pci_modern.c SourceLine:59] map[ReferenceKind:calls ReferencingEntityKind:function ReferencingEntityName:xsk_append_merge_buffer SourceFile:drivers/net/virtio_net.c SourceLine:1185] map[ReferenceKind:calls ReferencingEntityKind:function ReferencingEntityName:xsk_drop_follow_bufs SourceFile:drivers/net/virtio_net.c SourceLine:1157] map[ReferenceKind:takes-address-of ReferencingEntityKind:global_variable ReferencingEntityName:__UNIQUE_ID_addressable_virtqueue_get_buf_861 SourceFile:drivers/virtio/virtio_ring.c SourceLine:3137] map[ReferenceKind:takes-address-of ReferencingEntityKind:global_variable ReferencingEntityName:__gendwarfksyms_ptr_virtqueue_get_buf SourceFile:drivers/virtio/virtio_ring.c SourceLine:3137] map[ReferenceKind:takes-address-of ReferencingEntityKind:global_variable ReferencingEntityName:__gendwarfksyms_ptr_virtqueue_get_buf SourceFile:drivers/virtio/virtio_ring.c SourceLine:3137]] TruncatedOutput:false]
|
| 84/2 |
2026/07/27 02:07 |
llm |
expert |
0m
Model:
Tokens:
input: 60449
output: 40
thoughts: 789
Thoughts: **UAF Vulnerability in virtio-balloon During Resume After Hibernation/Suspend**
Okay, let's break this down. I see that `stats_handle_request` eventually calls `virtqueue_get_buf`. I need to figure out how `stats_handle_request` can be called after something gets freed. A quick search confirms that `stats_handle_request` is only called from `update_balloon_stats_func`.
The core issue appears to be a use-after-free (UAF) read within `virtqueue_get_buf_ctx`. The freed memory is an `skbuff_head_cache`, which means a specific sequence happened: memory freed, reallocated as an `sk_buff`, and then freed *again*, before a later access to it. This suggests that `vq` in `virtqueue_get_buf_ctx` is pointing to a freed region. The vital point is how `vq` becomes stale.
`vq` is actually `vb->stats_vq`. If *that* points to freed memory, it means the underlying virtqueue was freed, but the pointer wasn't updated. `stats_handle_request` then ends up being called using this stale pointer. So the question is: When does the virtqueue get freed, and when is the `vb->stats_vq` not updated.
`del_vqs` is the key function that frees the virtqueue. `del_vqs` is called from `remove_common`, which is called from `virtballoon_remove` and `virtballoon_freeze`. If `virtballoon_remove` is called, it cancels the work. This won't trigger the UAF. But if `virtballoon_freeze` is called, it *doesn't* cancel the work. It relies on the workqueue being frozen.
Now, let's think about resume. After a `freeze`, `virtballoon_restore` is called on a system resume. `virtballoon_restore` then calls `init_vqs`, which allocates *new* virtqueues and updates `vb->stats_vq`. So, the work uses the *new* virtqueue after the system resumes. So, normally, no UAF. The key, then, is *failure* of `init_vqs`.
If `init_vqs` fails, `virtballoon_restore` returns an error, which the PM core will then have to deal with. The PM core could leave the device suspended, or it might remove the device. If it removes the device, `virtballoon_remove` gets called and `cancel_work_sync` will be performed. But here's the kicker: The workqueue could be unfrozen *before* `virtballoon_remove` is called!
When does the PM core unfreeze workqueues? It's done in `thaw_workqueues()`, which is called during the `resume` phase, *after* devices are resumed. This means that *even if* a device fails to resume, the PM core *still* unfreezes workqueues.
So, here's the scenario: `init_vqs` fails, `vb->stats_vq` *still* points to the old, freed virtqueue, and the workqueue *is* unfrozen. `stats_handle_request` runs, accesses that stale virtqueue, and the UAF occurs.
Is this the only way? What about hibernation, where `virtballoon_restore` might not even be called? During hibernation, `freeze` is called, followed by the creation of a snapshot, and then a `thaw` to resume the system (to write the snapshot). But, I don't see a `thaw` callback registered for `virtio_balloon`. I better double-check the PM callbacks.
|
| 85/2 |
2026/07/27 02:07 |
tool |
read-file |
0m
Args: map[File:drivers/virtio/virtio_balloon.c FirstLine:1150 LineCount:50]
Results: map[Contents:1150: update_balloon_size(vb);
1151: return 0;
1152: }
1153: #endif
1154:
1155: static int virtballoon_validate(struct virtio_device *vdev)
1156: {
1157: /*
1158: * Inform the hypervisor that our pages are poisoned or
1159: * initialized. If we cannot do that then we should disable
1160: * page reporting as it could potentially change the contents
1161: * of our free pages.
1162: */
1163: if (!want_init_on_free() && !page_poisoning_enabled_static())
1164: __virtio_clear_bit(vdev, VIRTIO_BALLOON_F_PAGE_POISON);
1165: else if (!virtio_has_feature(vdev, VIRTIO_BALLOON_F_PAGE_POISON))
1166: __virtio_clear_bit(vdev, VIRTIO_BALLOON_F_REPORTING);
1167:
1168: __virtio_clear_bit(vdev, VIRTIO_F_ACCESS_PLATFORM);
1169: return 0;
1170: }
1171:
1172: static unsigned int features[] = {
1173: VIRTIO_BALLOON_F_MUST_TELL_HOST,
1174: VIRTIO_BALLOON_F_STATS_VQ,
1175: VIRTIO_BALLOON_F_DEFLATE_ON_OOM,
1176: VIRTIO_BALLOON_F_FREE_PAGE_HINT,
1177: VIRTIO_BALLOON_F_PAGE_POISON,
1178: VIRTIO_BALLOON_F_REPORTING,
1179: };
1180:
1181: static struct virtio_driver virtio_balloon_driver = {
1182: .feature_table = features,
1183: .feature_table_size = ARRAY_SIZE(features),
1184: .driver.name = KBUILD_MODNAME,
1185: .id_table = id_table,
1186: .validate = virtballoon_validate,
1187: .probe = virtballoon_probe,
1188: .remove = virtballoon_remove,
1189: .config_changed = virtballoon_changed,
1190: #ifdef CONFIG_PM_SLEEP
1191: .freeze = virtballoon_freeze,
1192: .restore = virtballoon_restore,
1193: #endif
1194: };
1195:
1196: module_virtio_driver(virtio_balloon_driver);
1197: MODULE_DEVICE_TABLE(virtio, id_table);
1198: MODULE_DESCRIPTION("Virtio balloon driver");
1199: MODULE_LICENSE("GPL");
]
|
| 86/2 |
2026/07/27 02:07 |
llm |
expert |
0m
Model:
Tokens:
input: 62923
output: 37
thoughts: 63
Thoughts: Okay, let's break this down.
**Investigating the Virtio Balloon Driver's Power Management**
Right, so I'm digging into how power management interacts with the `virtio_balloon_driver`. I know that it has `.freeze` and `.restore` callbacks, which is good. But the nagging question is, does it have a `.thaw` callback? Because that would determine how this driver wakes up after a suspend. I don't see one at first glance, but I need to be sure. This is where things get interesting in how PM callbacks are handled.
I need to confirm if those functions exist and how they affect the system so I'm going to jump into `virtio.c`. It's time to check what `virtio_device_freeze` and `virtio_device_restore` are actually doing, how they're implemented, and what other factors are at play. I have to look into this at a lower level to understand the power management framework and what it may require to ensure data integrity during those calls. I want to see how these calls relate to the overall device-wide power management within the `virtio` subsystem. It is a critical check to make sure that balloon driver doesn't lose data on a system suspend/resume cycle.
|
| 87/2 |
2026/07/27 02:07 |
tool |
read-file |
0m
Args: map[File:drivers/virtio/virtio.c FirstLine:580 LineCount:50]
Results: map[Contents: 580: return err;
581: }
582: EXPORT_SYMBOL_GPL(register_virtio_device);
583:
584: bool is_virtio_device(struct device *dev)
585: {
586: return dev->bus == &virtio_bus;
587: }
588: EXPORT_SYMBOL_GPL(is_virtio_device);
589:
590: void unregister_virtio_device(struct virtio_device *dev)
591: {
592: int index = dev->index; /* save for after device release */
593:
594: device_unregister(&dev->dev);
595: virtio_debug_device_exit(dev);
596: ida_free(&virtio_index_ida, index);
597: }
598: EXPORT_SYMBOL_GPL(unregister_virtio_device);
599:
600: static int virtio_device_restore_priv(struct virtio_device *dev, bool restore)
601: {
602: struct virtio_driver *drv = drv_to_virtio(dev->dev.driver);
603: int ret;
604:
605: /* We always start by resetting the device, in case a previous
606: * driver messed it up. */
607: virtio_reset_device(dev);
608:
609: /* Acknowledge that we've seen the device. */
610: virtio_add_status(dev, VIRTIO_CONFIG_S_ACKNOWLEDGE);
611:
612: /* Maybe driver failed before freeze.
613: * Restore the failed status, for debugging. */
614: if (dev->failed)
615: virtio_add_status(dev, VIRTIO_CONFIG_S_FAILED);
616:
617: if (!drv)
618: return 0;
619:
620: /* We have a driver! */
621: virtio_add_status(dev, VIRTIO_CONFIG_S_DRIVER);
622:
623: ret = dev->config->finalize_features(dev);
624: if (ret)
625: goto err;
626:
627: ret = virtio_features_ok(dev);
628: if (ret)
629: goto err;
]
|
| 88/2 |
2026/07/27 02:07 |
llm |
expert |
0m
Model:
Tokens:
input: 64018
output: 37
thoughts: 0
|
| 89/2 |
2026/07/27 02:07 |
tool |
read-file |
0m
Args: map[File:drivers/virtio/virtio.c FirstLine:630 LineCount:50]
Results: map[Contents: 630:
631: if (restore) {
632: if (drv->restore) {
633: ret = drv->restore(dev);
634: if (ret)
635: goto err;
636: }
637: } else {
638: ret = drv->reset_done(dev);
639: if (ret)
640: goto err;
641: }
642:
643: /* If restore didn't do it, mark device DRIVER_OK ourselves. */
644: if (!(dev->config->get_status(dev) & VIRTIO_CONFIG_S_DRIVER_OK))
645: virtio_device_ready(dev);
646:
647: virtio_config_core_enable(dev);
648:
649: return 0;
650:
651: err:
652: virtio_add_status(dev, VIRTIO_CONFIG_S_FAILED);
653: return ret;
654: }
655:
656: #ifdef CONFIG_PM_SLEEP
657: int virtio_device_freeze(struct virtio_device *dev)
658: {
659: struct virtio_driver *drv = drv_to_virtio(dev->dev.driver);
660: int ret;
661:
662: virtio_config_core_disable(dev);
663:
664: dev->failed = dev->config->get_status(dev) & VIRTIO_CONFIG_S_FAILED;
665:
666: if (drv && drv->freeze) {
667: ret = drv->freeze(dev);
668: if (ret) {
669: virtio_config_core_enable(dev);
670: return ret;
671: }
672: }
673:
674: return 0;
675: }
676: EXPORT_SYMBOL_GPL(virtio_device_freeze);
677:
678: int virtio_device_restore(struct virtio_device *dev)
679: {
]
|
| 90/2 |
2026/07/27 02:07 |
llm |
expert |
1m
Model:
Tokens:
input: 64723
output: 54
thoughts: 10126
Thoughts: **Analysis of a Virtio Balloon Use-After-Free Vulnerability**
My thought process began with the observation that a potential use-after-free (UAF) condition exists within the `virtio_balloon` driver. Specifically, if `virtio_device_restore_priv` fails during a system resume, the device might be left in a `FAILED` state while pending workqueues are unfrozen. This could lead to a UAF, as the pending work could access freed memory (`vb->stats_vq`). My initial concern revolved around identifying all potential paths to trigger this vulnerability.
I considered various scenarios. What if `virtballoon_remove` was called, or what if a work wasn't queued, or what about a race condition. I explored scenarios where `stats_request` could execute after cancellation, or where `update_balloon_stats_work` was queued through alternative paths. I quickly ruled out multiple incorrect and non-existent code flows by stepping through all possible calling methods.
I then considered `virtballoon_remove` more carefully. It calls `cancel_work_sync`, and a race condition became clear. If `stats_request` and `virtballoon_remove` ran concurrently, the order of events with the lock could lead to a scenario where the work was queued, but then immediately cancelled. However, I found that there were no major concurrency issues that were not accounted for with the code itself.
The analysis shifted to investigate the impact of device reset, hibernation, and various other cases, all of which I was able to find were safe due to several safety constraints.
Then, the focus narrowed down to whether this UAF might lie in the virtqueue itself and not the buffer being used. The memory address from the crash pointed to the `vring_virtqueue` struct, not the buffer itself. This was the key.
I identified that `virtballoon_freeze` calls `del_vqs` without a `cancel_work_sync`. I realized if `virtballoon_restore` fails, the virtqueues would remain uninitialized, leading to a UAF. This happens during system resume.
The vulnerability stems from the potential for `virtballoon_restore` to fail after `virtballoon_freeze`. When this occurs, the workqueue is unfrozen, and any pending workqueues that reference the now-freed `vring_virtqueue` struct will lead to the UAF.
Then I considered ways a user would be able to trigger this, and the potential impact of such a failure. System calls, sysfs calls, and other user-space mechanisms were ruled out as attack vectors due to the lack of access an unprivileged user has to suspend and resume operations.
Finally, I concluded that the path is only accessible through the kernel. A fault must be injected by a process that is able to suspend and resume the system. It may be possible to inject a fault from an unprivileged process through an out-of-memory fault. This can be easily triggered by a fuzzing environment such as syzkaller, which can induce `init_vqs` to fail.
|
| 91/2 |
2026/07/27 02:09 |
tool |
git-log |
0m
Args: map[CodeRegexp:cancel_work_sync Count:10 SourcePath:drivers/virtio/virtio_balloon.c SymbolName:virtballoon_freeze]
Results: map[Output:fad7b7b27b6a virtio_balloon: Use a workqueue instead of "vballoon" kthread
c877bab5072c virtio: balloon: separate out common code between remove and freeze functions
4eb05d562ea1 virtio: balloon: leak / fill balloon across S4
e562966dbaf4 virtio: balloon: Add freeze, restore handlers to support S4
]
|
| 92/2 |
2026/07/27 02:09 |
llm |
expert |
0m
Model:
Tokens:
input: 50907
output: 54
thoughts: 0
|
| 93/2 |
2026/07/27 02:09 |
tool |
git-log |
0m
Args: map[CodeRegexp:cancel_work_sync Count:10 SourcePath:drivers/virtio/virtio_balloon.c SymbolName:virtballoon_remove]
Results: map[Output:7c59cc9cf729 virtio-balloon: Destroy mutex before freeing virtio_balloon
68f2736a8583 mm: Convert all PageMovable users to movable_operations
da10329cb057 virtio-balloon: switch back to OOM handler for VIRTIO_BALLOON_F_DEFLATE_ON_OOM
b0c504f15471 virtio-balloon: add support for providing free page reports to host
86a559787e6f virtio-balloon: VIRTIO_BALLOON_F_FREE_PAGE_HINT
71994620bb25 virtio_balloon: replace oom notifier with shrinker
9c57b5808c62 mm balloon: umount balloon_mnt when removing vb device
b1123ea6d3b3 mm: balloon: use general non-lru movable page feature
fd0e21c31e1e virtio_balloon: Allow to resize and update the balloon stats in parallel
fad7b7b27b6a virtio_balloon: Use a workqueue instead of "vballoon" kthread
]
|
| 94/2 |
2026/07/27 02:09 |
llm |
expert |
0m
Model:
Tokens:
input: 76015
output: 66
thoughts: 3799
Thoughts: **Analysis of a Potential Probe Error Path Use-After-Free in the virtio_balloon Driver**
Okay, here's my thought process on this. I'm looking at this syzkaller report, and the first thing I'm doing is trying to pinpoint where this crash is happening. I need to understand if the crash reported is related to a suspend/resume cycle. The fact that the allocation and free happened in such a short time, just 0.39 seconds, makes me think this isn't likely. If it's not a suspend cycle, then I am thinking, "Is there another way `del_vqs` gets called?"
My mind goes directly to failure conditions during the probe sequence. If the `virtballoon_probe` function has an error, what happens? Let's check the code:
If `init_vqs` fails early, it's pretty clean, the `vb` is freed immediately. But what if it succeeds but something *later* in the probe fails? Specifically, the code does:
```c
if (virtio_has_feature(vdev, VIRTIO_BALLOON_F_FREE_PAGE_HINT)) {
...
vb->balloon_wq = alloc_workqueue("balloon-wq", ...);
if (!vb->balloon_wq) {
err = -ENOMEM;
goto out_del_vqs;
}
...
}
virtio_device_ready(vdev);
if (towards_target(vb))
virtballoon_changed(vdev);
return 0;
out_del_vqs:
vdev->config->del_vqs(vdev);
out_free_vb:
mutex_destroy(&vb->balloon_lock);
kfree(vb);
out:
return err;
```
I'm thinking, "if `alloc_workqueue` fails, it calls `del_vqs`, but that function has `init_vqs` already run, right?" So, virtqueues are allocated, `vb->stats_vq` is set, and callbacks are registered. Then, *before* `del_vqs` is called, an interrupt could arrive! If an interrupt arrives early, `stats_request` could be called! Then, an interrupt happens BEFORE `virtio_device_ready` is called, so `stats_request` queues `update_balloon_stats_work`. Then, `virtballoon_probe` calls `del_vqs`, which frees the virtqueues, then `kfree(vb)`. Then the work runs and accesses `vb` and `vb->stats_vq`!
"Aha! This sounds like a classic probe failure UAF!"
Now I wonder: Can interrupts arrive *before* `virtio_device_ready`? Normally, not. However, a malicious or buggy host *could* send one! This will register the callbacks via the virtqueue. If the host is buggy, or perhaps in some nested virt scenarios, or even with a hardware virtio device, it could interrupt! The driver is set up to handle it by the time the interrupt handler is active. Syzkaller uses `vhci`, `vhost`, or `virtio-uml` and it *can* send interrupts immediately after `init_vqs`, then inject a fault to trigger `alloc_workqueue` failure. "This explains it perfectly!"
I need to confirm: does `virtballoon_probe` call `cancel_work_sync` in the error path?
I look again: `out_del_vqs`, `out_free_vb`, no `cancel_work_sync`. So, no. It *doesn't* call it! This means the queued work will run after `vb` is freed! It's a textbook case of a probe error path bug.
"Is this exploitable?" Yes, use-after-free!
"Can a user trigger it?" No. Probe requires root privileges or hypervisor access. But Syzkaller, which is running as root and can inject interrupts and faults, can.
However, what if it's a *hardware* device? A virtio-pci device? Could *that* trigger the bug? If someone plugs in a malicious virtio-pci device, could *that* send an early interrupt? Absolutely! Can a malicious device *cause* the probe to fail? Yep, if it advertises the `VIRTIO_BALLOON_F_FREE_PAGE_HINT` feature but sets the virtqueue size to 1. Check this:
```c
if (virtqueue_get_vring_size(vb->free_page
|
| 95/2 |
2026/07/27 02:09 |
tool |
set-results |
0m
Args: map[DenialOfService:true Exploitable:true FilesystemTrigger:false NetworkTrigger:false PeripheralTrigger:true RemoteTrigger:false Unprivileged:false UserNamespace:false VMGuestTrigger:true VMHostTrigger:false]
Results: map[DenialOfService:true Exploitable:true FilesystemTrigger:false NetworkTrigger:false PeripheralTrigger:true RemoteTrigger:false Unprivileged:false UserNamespace:false VMGuestTrigger:true VMHostTrigger:false]
|
| 96/2 |
2026/07/27 02:09 |
llm |
expert |
0m
Model:
Tokens:
input: 80969
output: 807
thoughts: 530
Thoughts: **My Analysis of the Virtio Balloon Driver UAF**
Okay, the results are in. Time to synthesize this into a clear, concise explanation. I need to nail down the precise sequence of events that leads to this bug, and then articulate it in a way that's easy to grasp.
Essentially, we're dealing with a nasty Use-After-Free (UAF) in the `virtio_balloon` driver. It's nestled in the error handling path of the probe function. The critical point is the timing, and a missing piece of cleanup.
Here's the breakdown as I see it: During the `virtballoon_probe`, the code initializes the virtqueues using `init_vqs()`. This function registers interrupt handlers. Of particular interest is the `stats_request` handler for the statistics virtqueue. Now, the problem rears its head when the device is a bit too eager with its interrupts. If the device sends an interrupt *immediately* after the virtqueues are initialized (even before the probe has really finished), the `stats_request` callback is executed. This triggers `vb->update_balloon_stats_work` to get queued on the `system_freezable_wq`.
Now, the vulnerability is exposed when `virtballoon_probe` *fails* after this interrupt. It might fail for several reasons. One example is if the device advertises a feature that needs more than a minimal vring size, and it subsequently provides a small vring size. This leads to an error return from the probe. The error handling pathway (`out_del_vqs`) kicks in. The virtqueues are deleted, and the `virtio_balloon` struct (`vb`) is freed. Here's where it goes south: the code *doesn't* call `cancel_work_sync()` to cancel any pending work items, such as the `update_balloon_stats_work`.
Consequently, the queued `update_balloon_stats_work` will execute even after `vb` and its associated virtqueues have been released. This is where the magic happens, or rather, where the memory corruption happens. The work function, `update_balloon_stats_func`, calls `stats_handle_request`, which accesses the now-freed `vb->stats_vq` and then passes it to `virtqueue_get_buf()`. Boom – UAF!
Exploitability-wise, it's a classic UAF on both the `vring_virtqueue` and the `virtio_balloon` structures. It's a prime target for exploitation. Memory corruption is on the table, which could lead to privilege escalation or, at the very least, a denial-of-service condition.
Triggering this is, unfortunately, quite straightforward for a malicious actor. A VM guest can easily trigger it by sending an early interrupt and providing a small vring size to force the probe to fail. Or, a malicious PCIe device plugged into the system (perhaps through Thunderbolt) could pose as a virtio-pci balloon device and do the same thing: send an early MSI-X interrupt and provide a small vring size to initiate the probe failure and trigger the UAF. Thankfully, this isn't something unprivileged users can trigger, as it requires either plugging a device or controlling the hypervisor.
I think I have a clear picture. Time to write this up formally.
|
| 97/1 |
2026/07/27 02:10 |
action |
format-explanation |
0m
Results: map[Explanation:Based on the analysis of the bug report and the kernel source code, here is the
assessment of the vulnerability:
### Root Cause Analysis
The bug is a **Use-After-Free (UAF)** in the `virtio_balloon` driver,
specifically occurring in the device probe error path.
During the execution of `virtballoon_probe()`, the driver initializes the
device's virtqueues by calling `init_vqs()`. This step registers the interrupt
callbacks for the virtqueues, including `stats_request()` for the statistics
virtqueue. Once `init_vqs()` completes, the device is capable of generating
interrupts.
If the device generates an interrupt at this stage, the `stats_request()`
callback is executed, which queues the `vb->update_balloon_stats_work` work item
onto the `system_freezable_wq` workqueue.
Following the virtqueue initialization, `virtballoon_probe()` performs several
other checks. For instance, if the device advertises the
`VIRTIO_BALLOON_F_FREE_PAGE_HINT` feature, the driver checks if the vring size
is at least 2:
```c
if (virtqueue_get_vring_size(vb->free_page_vq) < 2) {
err = -ENOSPC;
goto out_del_vqs;
}
```
If this check (or any subsequent memory allocation like `alloc_workqueue`)
fails, the driver jumps to the error handling path (`out_del_vqs`). The error
path calls `vdev->config->del_vqs(vdev)` to free the virtqueues and then
`kfree(vb)` to free the `virtio_balloon` structure.
Crucially, the probe error path **fails to call `cancel_work_sync()`** to cancel
any pending work items that might have been queued by early interrupts.
Consequently, the `update_balloon_stats_work` remains queued and will execute
shortly after. When `update_balloon_stats_func()` runs, it calls
`stats_handle_request()`, which attempts to read from the already freed
`vb->stats_vq` and passes it to `virtqueue_get_buf()`, resulting in the observed
Use-After-Free crash.
### Security Impact
* **Exploitable:** **Yes.** The bug is a Use-After-Free involving the
`virtio_balloon` and `vring_virtqueue` structures. An attacker who can trigger
this can control the timing of the UAF and potentially reallocate the freed
memory with controlled data, leading to memory corruption, control-flow
hijacking, and privilege escalation.
* **Denial of Service:** **Yes.** Accessing the freed memory will typically
cause a kernel panic, leading to a complete system crash.
* **VM Guest Trigger:** **Yes.** A malicious or compromised hypervisor (e.g.,
QEMU/KVM) can easily trigger this bug in the guest kernel by presenting a
virtio-balloon device, sending an early interrupt, and intentionally providing a
vring size of 1 to force the probe to fail.
* **Peripheral Trigger:** **Yes.** A malicious physical PCIe device (e.g.,
plugged in via Thunderbolt/USB4) can present itself as a `virtio-pci` balloon
device. By sending an early MSI-X interrupt and advertising a malformed vring
size, the malicious peripheral can deterministically trigger this UAF on the
host machine.
* **Accessible From Unprivileged Processes / User Namespaces:** **No.**
Triggering this vulnerability requires the ability to plug in a physical device,
control the hypervisor, or inject faults (which Syzkaller does as root).
Unprivileged users cannot interact with the virtio probe sequence directly.]
|