Skip to contentSkip to conversation

virt: bao: Add Bao hypervisor IPC and I/O dispatcher drivers

RFC PATCH Superseded João Peixoto 3 participants 12 messages Aug 7 – Aug 11

Subsystems (5)

v3 of 7 Superseded Latest is v7 →

lore
Share & tools
lore
#0 lore raw

v3 cover virt: bao: Add Bao hypervisor IPC and I/O dispatcher drivers

This series adds guest-side drivers for the Bao static-partitioning
hypervisor: an IPC shared-memory driver and an I/O dispatcher, plus their device-tree bindings, UAPI and MAINTAINERS entry.

Bao is a lightweight static-partitioning hypervisor for embedded and safety-critical systems.
- The IPC shared-memory driver lets Bao guests exchange data through a shared-memory region split into a read and a write channel.
- The I/O dispatcher lets a backend guest service paravirtualised (VirtIO) I/O on behalf of frontend guests.

Sent as RFC: the RISC-V backend uses the SBI experimental extension space (see "Open items" below), and the device-tree bindings were reworked in this version and would benefit from another look.

## Changes since v2
- dt-bindings (ipcshmem): describe the two channels through reg/reg-names, drop read-channel/write-channel and the "id" property, use a vendor "bao,id", and use a generic node name (Krzysztof Kozlowski).
- dt-bindings (io-dispatcher): one node per backend device (single reg + interrupt) instead of one node describing many devices, modelled on the gunyah/Mediatek Genio bindings (Krzysztof Kozlowski).
- io dispatcher driver: one platform device per device model (/dev/bao-dmX); removed the global DM list, the anonymous-inode fd and the dispatcher indirection ioctl; fixed a leak and probe error-path bugs; initialise virtio_requests_lock and the hypercall context fields.
- ipcshmem driver: derive the two regions from reg; drop the success print; do the range arithmetic in u64.
- Kconfig: fix the help-text indentation (Randy Dunlap).
- style: unwrap the few-char line wraps; the IPC hypercall helper now takes a single argument and fits on one line (Andrew Jones).
- riscv: use BAO_SBI_EXT_ID consistently, document that it is in the SBI experimental extension space, and drop the redundant ecall input constraints (Andrew Jones).
- commit messages: rewritten to kernel style; the "consolidate the IPC hypercall ID" patch now explains why and covers the signature change (Greg KH).
- process: sent as a fresh thread with this changelog, and the mail setup that bounced v1/v2 is fixed (Krzysztof Kozlowski, Greg KH).

## Open items / still under discussion
- RISC-V uses the experimental SBI extension ID (0x08000ba0). A permanent implementation ID must be registered in the RISC-V SBI spec before RISC-V can be non-experimental; hence RFC.
- Whether the Remote I/O hypercall must follow SBI chapter-3 register rules (Andrew Jones) — see the reply on patch 4.
- The "bao,id" property and the "bao" vendor prefix (Krzysztof Kozlowski) — rationale is in the bindings; alternatives welcome.

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

João Peixoto (6):
dt-bindings: bao: add IPC shared-memory device
virt: bao: add IPC shared-memory driver
dt-bindings: bao: add I/O dispatcher device
virt: bao: add I/O dispatcher driver
virt: bao: consolidate the IPC hypercall ID in include/linux/bao.h
MAINTAINERS: add Bao hypervisor entry

 .../bindings/bao/bao,io-dispatcher.yaml       |  58 +++
 .../devicetree/bindings/bao/bao,ipcshmem.yaml |  59 +++
 .../devicetree/bindings/vendor-prefixes.yaml  |   2 +
 .../userspace-api/ioctl/ioctl-number.rst      |   2 +
 MAINTAINERS                                   |  13 +
 arch/arm/include/asm/bao.h                    |  60 +++
 arch/arm64/include/asm/bao.h                  |  60 +++
 arch/riscv/include/asm/bao.h                  |  66 +++
 drivers/virt/Kconfig                          |   2 +
 drivers/virt/Makefile                         |   2 +
 drivers/virt/bao/Kconfig                      |   5 +
 drivers/virt/bao/Makefile                     |   4 +
 drivers/virt/bao/io-dispatcher/Kconfig        |  16 +
 drivers/virt/bao/io-dispatcher/Makefile       |   4 +
 drivers/virt/bao/io-dispatcher/bao_drv.h      | 349 +++++++++++++++
 drivers/virt/bao/io-dispatcher/dm.c           | 316 ++++++++++++++
 drivers/virt/bao/io-dispatcher/driver.c       |  95 +++++
 drivers/virt/bao/io-dispatcher/intc.c         |  64 +++
 drivers/virt/bao/io-dispatcher/io_client.c    | 401 ++++++++++++++++++
 .../virt/bao/io-dispatcher/io_dispatcher.c    | 181 ++++++++
 drivers/virt/bao/io-dispatcher/ioeventfd.c    | 323 ++++++++++++++
 drivers/virt/bao/io-dispatcher/irqfd.c        | 314 ++++++++++++++
 drivers/virt/bao/ipcshmem/Kconfig             |  10 +
 drivers/virt/bao/ipcshmem/Makefile            |   3 +
 drivers/virt/bao/ipcshmem/ipcshmem.c          | 228 ++++++++++
 include/linux/bao.h                           |  44 ++
 include/uapi/linux/bao.h                      |  96 +++++
 27 files changed, 2777 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/bao/bao,io-dispatcher.yaml
 create mode 100644 Documentation/devicetree/bindings/bao/bao,ipcshmem.yaml
 create mode 100644 arch/arm/include/asm/bao.h
 create mode 100644 arch/arm64/include/asm/bao.h
 create mode 100644 arch/riscv/include/asm/bao.h
 create mode 100644 drivers/virt/bao/Kconfig
 create mode 100644 drivers/virt/bao/Makefile
 create mode 100644 drivers/virt/bao/io-dispatcher/Kconfig
 create mode 100644 drivers/virt/bao/io-dispatcher/Makefile
 create mode 100644 drivers/virt/bao/io-dispatcher/bao_drv.h
 create mode 100644 drivers/virt/bao/io-dispatcher/dm.c
 create mode 100644 drivers/virt/bao/io-dispatcher/driver.c
 create mode 100644 drivers/virt/bao/io-dispatcher/intc.c
 create mode 100644 drivers/virt/bao/io-dispatcher/io_client.c
 create mode 100644 drivers/virt/bao/io-dispatcher/io_dispatcher.c
 create mode 100644 drivers/virt/bao/io-dispatcher/ioeventfd.c
 create mode 100644 drivers/virt/bao/io-dispatcher/irqfd.c
 create mode 100644 drivers/virt/bao/ipcshmem/Kconfig
 create mode 100644 drivers/virt/bao/ipcshmem/Makefile
 create mode 100644 drivers/virt/bao/ipcshmem/ipcshmem.c
 create mode 100644 include/linux/bao.h
 create mode 100644 include/uapi/linux/bao.h

base-commit: dc59e4fea9d83f03bad6bddf3fa2e52491777482

Signature · 1 line
2.43.0
#1 lore raw

RFC PATCH v3 1/6 dt-bindings: bao: add IPC shared-memory device

Add the device-tree binding for the Bao hypervisor IPC shared-memory device, used by a Bao guest to exchange data with a peer guest over a shared-memory region.

---
v3:
- Describe the read and write channels through reg/reg-names instead of the custom read-channel/write-channel properties; drop those properties and the "id" property; add a vendor "bao,id" for the hypercall channel id; use a generic node name (shmem@...) (Krzysztof Kozlowski).
- Clarify the "bao" vendor-prefix description.
- Subject: drop the redundant trailing "binding" (Krzysztof Kozlowski).
- Add José's and David's Co-developed-by/Signed-off-by.

 .../devicetree/bindings/bao/bao,ipcshmem.yaml | 59 +++++++++++++++++++
 .../devicetree/bindings/vendor-prefixes.yaml  |  2 +
 2 files changed, 61 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/bao/bao,ipcshmem.yaml
Documentation/devicetree/bindings/bao/bao,ipcshmem.yaml new file +59 -0
new file mode 100644
index 000000000000..60947150d576
@@ -0,0 +1,59 @@
+# SPDX-License-Identifier: GPL-2.0-only OR BSD-2-Clause
+%YAML 1.2
+---
+$id: http://devicetree.org/schemas/bao/bao,ipcshmem.yaml#
+$schema: http://devicetree.org/meta-schemas/core.yaml#
+
+title: Bao Hypervisor IPC Shared Memory Device
+
+maintainers:
+  - João Peixoto <[email protected]>
+  - José Martins <[email protected]>
+  - David Cerdeira <[email protected]>
+
+description: |
+  Shared-memory device exposed by the Bao hypervisor to a guest so that it
+  can exchange data with a peer guest. The hypervisor provides two
+  contiguous memory regions, one the guest reads from (written by the peer)
+  and one the guest writes to (read by the peer). A write is signalled to
+  the peer through a hypercall identified by bao,id.
+
+properties:
+  compatible:
+    const: bao,ipcshmem
+
+  reg:
+    items:
+      - description: region read by this guest and written by the peer
+      - description: region written by this guest and read by the peer
+
+  reg-names:
+    items:
+      - const: read
+      - const: write
+
+  bao,id:
+    $ref: /schemas/types.yaml#/definitions/uint32
+    description:
+      Hypervisor-assigned identifier of this shared-memory channel. The
+      guest passes it to the Bao hypercall that notifies the peer of a
+      write, so it must match the identifier configured for the channel in
+      the hypervisor.
+
+required:
+  - compatible
+  - reg
+  - reg-names
+  - bao,id
+
+additionalProperties: false
+
+examples:
+  - |
+    shmem@f0000000 {
+        compatible = "bao,ipcshmem";
+        reg = <0xf0000000 0x2000>,
+              <0xf0002000 0x2000>;
+        reg-names = "read", "write";
+        bao,id = <0>;
+    };
Documentation/devicetree/bindings/vendor-prefixes.yaml +2 -0
index 396044f368e7..b11b3dd995a5 100644
@@ -231,6 +231,8 @@ patternProperties:
     description: Shenzhen AZW Technology Co., Ltd.
   "^baikal,.*":
     description: BAIKAL ELECTRONICS, JSC
+  "^bao,.*":
+    description: Bao Project (https://github.com/bao-project)
   "^bananapi,.*":
     description: BIPAI KEJI LIMITED
   "^beacon,.*":
Signature · 1 line
2.43.0
#3 lore raw

RFC PATCH v3 2/6 virt: bao: add IPC shared-memory driver

In reply to João Peixoto

Add a driver that lets guests running on the Bao static-partitioning hypervisor communicate through shared memory. Each guest is assigned a read and a write region within a shared-memory area described in the device tree.

Userspace accesses the regions through a misc character device using read(), write() and mmap(). A write() notifies the peer guest through an architecture-specific hypercall (HVC on arm/arm64, SBI ecall on RISC-V).

---
v3:
- Map the read and write regions from the two reg entries (by reg-name) instead of parsing read-channel/write-channel offsets; read the channel id from "bao,id"; mmap() now maps per region.
- Drop the noisy dev_info() on successful probe.
- Do the shared-memory range arithmetic in u64 to avoid a u32 overflow.
- Fix the Kconfig help-text indentation (Randy Dunlap).
- Add José's and David's Co-developed-by/Signed-off-by.

 arch/arm/include/asm/bao.h           |  31 ++++
 arch/arm64/include/asm/bao.h         |  31 ++++
 arch/riscv/include/asm/bao.h         |  31 ++++
 drivers/virt/Kconfig                 |   2 +
 drivers/virt/Makefile                |   1 +
 drivers/virt/bao/Kconfig             |   3 +
 drivers/virt/bao/Makefile            |   3 +
 drivers/virt/bao/ipcshmem/Kconfig    |  10 ++
 drivers/virt/bao/ipcshmem/Makefile   |   3 +
 drivers/virt/bao/ipcshmem/ipcshmem.c | 231 +++++++++++++++++++++++++++
 10 files changed, 346 insertions(+)
 create mode 100644 arch/arm/include/asm/bao.h
 create mode 100644 arch/arm64/include/asm/bao.h
 create mode 100644 arch/riscv/include/asm/bao.h
 create mode 100644 drivers/virt/bao/Kconfig
 create mode 100644 drivers/virt/bao/Makefile
 create mode 100644 drivers/virt/bao/ipcshmem/Kconfig
 create mode 100644 drivers/virt/bao/ipcshmem/Makefile
 create mode 100644 drivers/virt/bao/ipcshmem/ipcshmem.c
arch/arm/include/asm/bao.h new file +31 -0
new file mode 100644
index 000000000000..ba64d1a18f91
@@ -0,0 +1,31 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/*
+ * Bao Hypervisor Hypercall Interface
+ *
+ * Copyright (c) Bao Project and Contributors. All rights reserved.
+ *
+ * Authors:
+ *	João Peixoto <[email protected]>
+ *	José Martins <[email protected]>
+ *	David Cerdeira <[email protected]>
+ */
+
+#ifndef __ASM_ARM_BAO_H
+#define __ASM_ARM_BAO_H
+
+#include <linux/arm-smccc.h>
+
+static inline unsigned long bao_ipcshmem_hypercall(unsigned long hypercall_id,
+						   unsigned long ipcshmem_id)
+{
+	struct arm_smccc_res res;
+
+	arm_smccc_hvc(ARM_SMCCC_CALL_VAL(ARM_SMCCC_FAST_CALL, ARM_SMCCC_SMC_32,
+					 ARM_SMCCC_OWNER_VENDOR_HYP,
+					 hypercall_id),
+		      ipcshmem_id, 0, 0, 0, 0, 0, 0, &res);
+
+	return res.a0;
+}
+
+#endif /* __ASM_ARM_BAO_H */
arch/arm64/include/asm/bao.h new file +31 -0
new file mode 100644
index 000000000000..ab9b283168e3
@@ -0,0 +1,31 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/*
+ * Bao Hypervisor Hypercall Interface
+ *
+ * Copyright (c) Bao Project and Contributors. All rights reserved.
+ *
+ * Authors:
+ *	João Peixoto <[email protected]>
+ *	José Martins <[email protected]>
+ *	David Cerdeira <[email protected]>
+ */
+
+#ifndef __ASM_ARM64_BAO_H
+#define __ASM_ARM64_BAO_H
+
+#include <linux/arm-smccc.h>
+
+static inline unsigned long bao_ipcshmem_hypercall(unsigned long hypercall_id,
+						   unsigned long ipcshmem_id)
+{
+	struct arm_smccc_res res;
+
+	arm_smccc_hvc(ARM_SMCCC_CALL_VAL(ARM_SMCCC_FAST_CALL, ARM_SMCCC_SMC_64,
+					 ARM_SMCCC_OWNER_VENDOR_HYP,
+					 hypercall_id),
+		      ipcshmem_id, 0, 0, 0, 0, 0, 0, &res);
+
+	return res.a0;
+}
+
+#endif /* __ASM_ARM64_BAO_H */
arch/riscv/include/asm/bao.h new file +31 -0
new file mode 100644
index 000000000000..d2c79a6a4ade
@@ -0,0 +1,31 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/*
+ * Bao Hypervisor Hypercall Interface
+ *
+ * Copyright (c) Bao Project and Contributors. All rights reserved.
+ *
+ * Authors:
+ *	João Peixoto <[email protected]>
+ *	José Martins <[email protected]>
+ *	David Cerdeira <[email protected]>
+ */
+
+#ifndef __ASM_RISCV_BAO_H
+#define __ASM_RISCV_BAO_H
+
+#include <asm/sbi.h>
+
+#define BAO_SBI_EXT_ID 0x08000ba0
+
+static inline unsigned long bao_ipcshmem_hypercall(unsigned long hypercall_id,
+						   unsigned long ipcshmem_id)
+{
+	struct sbiret ret;
+
+	ret = sbi_ecall(BAO_SBI_EXT_ID, hypercall_id, ipcshmem_id, 0, 0, 0, 0,
+			0);
+
+	return ret.error;
+}
+
+#endif /* __ASM_RISCV_BAO_H */
drivers/virt/Kconfig +2 -0
index 52eb7e4ba71f..cb98c4c52fd1 100644
@@ -47,6 +47,8 @@ source "drivers/virt/nitro_enclaves/Kconfig"
 
 source "drivers/virt/acrn/Kconfig"
 
+source "drivers/virt/bao/Kconfig"
+
 endif
 
 source "drivers/virt/coco/Kconfig"
drivers/virt/Makefile +1 -0
index f29901bd7820..623a671f8711 100644
@@ -10,3 +10,4 @@ obj-y				+= vboxguest/
 obj-$(CONFIG_NITRO_ENCLAVES)	+= nitro_enclaves/
 obj-$(CONFIG_ACRN_HSM)		+= acrn/
 obj-y				+= coco/
+obj-$(CONFIG_BAO_SHMEM)		+= bao/
drivers/virt/bao/Kconfig new file +3 -0
new file mode 100644
index 000000000000..4f7929d57475
@@ -0,0 +1,3 @@
+# SPDX-License-Identifier: GPL-2.0
+
+source "drivers/virt/bao/ipcshmem/Kconfig"
drivers/virt/bao/Makefile new file +3 -0
new file mode 100644
index 000000000000..68f5d3f282c4
@@ -0,0 +1,3 @@
+# SPDX-License-Identifier: GPL-2.0
+
+obj-$(CONFIG_BAO_SHMEM) += ipcshmem/
drivers/virt/bao/ipcshmem/Kconfig new file +10 -0
new file mode 100644
index 000000000000..b789e5ea1264
@@ -0,0 +1,10 @@
+# SPDX-License-Identifier: GPL-2.0
+config BAO_SHMEM
+	tristate "Bao hypervisor shared memory support"
+	help
+	  This enables support for Bao shared memory communication.
+	  It allows the kernel to interface with guests running under
+	  the Bao hypervisor, providing a character device interface
+	  for exchanging data through dedicated shared-memory regions.
+
+	  If unsure, say N.
drivers/virt/bao/ipcshmem/Makefile new file +3 -0
new file mode 100644
index 000000000000..e027dcdb06aa
@@ -0,0 +1,3 @@
+# SPDX-License-Identifier: GPL-2.0
+obj-$(CONFIG_BAO_SHMEM) += bao.o
+bao-objs += ipcshmem.o
drivers/virt/bao/ipcshmem/ipcshmem.c new file +231 -0

231 lines not on this page · raw message

Signature · 1 line
2.43.0
#5 lore raw

RFC PATCH v3 3/6 dt-bindings: bao: add I/O dispatcher device

In reply to João Peixoto

Add the device-tree binding for the Bao I/O dispatcher, present in backend VMs that service paravirtualised (VirtIO) I/O on behalf of frontend guests.

---
v3:
- Model one node per backend device (single reg + single interrupt + "bao,id") instead of a single node describing many devices, following the gunyah and Mediatek Genio bindings (Krzysztof Kozlowski).
- Add José's and David's Co-developed-by/Signed-off-by.

 .../bindings/bao/bao,io-dispatcher.yaml       | 58 +++++++++++++++++++
 1 file changed, 58 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/bao/bao,io-dispatcher.yaml
Documentation/devicetree/bindings/bao/bao,io-dispatcher.yaml new file +58 -0
new file mode 100644
index 000000000000..f118c6d486a6
@@ -0,0 +1,58 @@
+# SPDX-License-Identifier: GPL-2.0-only OR BSD-2-Clause
+%YAML 1.2
+---
+$id: http://devicetree.org/schemas/bao/bao,io-dispatcher.yaml#
+$schema: http://devicetree.org/meta-schemas/core.yaml#
+
+title: Bao Hypervisor I/O Dispatcher Device
+
+maintainers:
+  - João Peixoto <[email protected]>
+  - José Martins <[email protected]>
+  - David Cerdeira <[email protected]>
+
+description:
+  Backend device exposed by the Bao hypervisor to a guest that services
+  paravirtualised (VirtIO) I/O on behalf of a frontend guest. Each device
+  owns one contiguous shared-memory region used to exchange I/O buffers
+  with its frontend, and one interrupt the hypervisor raises to signal a
+  pending I/O request. A backend guest that services several frontends
+  instantiates one node per backend device.
+
+properties:
+  compatible:
+    const: bao,io-dispatcher
+
+  reg:
+    maxItems: 1
+    description:
+      Shared-memory region used to exchange I/O buffers with the frontend.
+
+  interrupts:
+    maxItems: 1
+    description:
+      Interrupt raised by the hypervisor to signal a pending I/O request.
+
+  bao,id:
+    $ref: /schemas/types.yaml#/definitions/uint32
+    description:
+      Hypervisor-assigned identifier of the backend device model. It must
+      match the identifier configured for the device model in the
+      hypervisor.
+
+required:
+  - compatible
+  - reg
+  - interrupts
+  - bao,id
+
+additionalProperties: false
+
+examples:
+  - |
+    shmem@50000000 {
+        compatible = "bao,io-dispatcher";
+        reg = <0x50000000 0x01000000>;
+        interrupts = <0x0 0x08 0x1>;
+        bao,id = <0>;
+    };
Signature · 1 line
2.43.0
#6 lore raw

RFC PATCH v3 4/6 virt: bao: add I/O dispatcher driver

In reply to João Peixoto

Add the Bao I/O dispatcher, used by backend VMs to service I/O on behalf of frontend guests. It bridges Bao's Remote I/O mechanism to userspace VirtIO backend device models.

Each backend device has a contiguous shared-memory region for exchanging I/O buffers with its frontend and an interrupt the hypervisor uses to signal pending requests. Userspace drives the dispatcher through a set of ioctls on a misc character device.

---
v3:
- Bind one platform device per device model and expose one /dev/bao-dmX each; drop the global DM list and its locking, the anonymous-inode fd, and the dispatcher indirection ioctl (BAO_IOCTL_DM_GET_INFO now lives in the DM fops).
- Fix the DM leak on remove, free the misc-device name, and unwind the probe error path fully; validate "bao,id" against BAO_IO_MAX_DMS.
- Initialise virtio_requests_lock and the hypercall context fields; drop the redundant control-client dispatch loop.
- riscv: reference BAO_SBI_EXT_ID consistently, document that it uses the SBI experimental extension space (RISC-V stays experimental until an SBI implementation ID is registered), and drop the redundant ecall input constraints (Andrew Jones).
- Use kzalloc_obj() instead of kzalloc(sizeof(*x)).
- Add José's and David's Co-developed-by/Signed-off-by.

 .../userspace-api/ioctl/ioctl-number.rst      |   2 +
 arch/arm/include/asm/bao.h                    |  30 ++
 arch/arm64/include/asm/bao.h                  |  30 ++
 arch/riscv/include/asm/bao.h                  |  37 ++
 drivers/virt/Makefile                         |   1 +
 drivers/virt/bao/Kconfig                      |   2 +
 drivers/virt/bao/Makefile                     |   1 +
 drivers/virt/bao/io-dispatcher/Kconfig        |  16 +
 drivers/virt/bao/io-dispatcher/Makefile       |   4 +
 drivers/virt/bao/io-dispatcher/bao_drv.h      | 349 +++++++++++++++
 drivers/virt/bao/io-dispatcher/dm.c           | 316 ++++++++++++++
 drivers/virt/bao/io-dispatcher/driver.c       |  95 +++++
 drivers/virt/bao/io-dispatcher/intc.c         |  64 +++
 drivers/virt/bao/io-dispatcher/io_client.c    | 401 ++++++++++++++++++
 .../virt/bao/io-dispatcher/io_dispatcher.c    | 181 ++++++++
 drivers/virt/bao/io-dispatcher/ioeventfd.c    | 323 ++++++++++++++
 drivers/virt/bao/io-dispatcher/irqfd.c        | 314 ++++++++++++++
 include/linux/bao.h                           |  41 ++
 include/uapi/linux/bao.h                      |  96 +++++
 19 files changed, 2303 insertions(+)
 create mode 100644 drivers/virt/bao/io-dispatcher/Kconfig
 create mode 100644 drivers/virt/bao/io-dispatcher/Makefile
 create mode 100644 drivers/virt/bao/io-dispatcher/bao_drv.h
 create mode 100644 drivers/virt/bao/io-dispatcher/dm.c
 create mode 100644 drivers/virt/bao/io-dispatcher/driver.c
 create mode 100644 drivers/virt/bao/io-dispatcher/intc.c
 create mode 100644 drivers/virt/bao/io-dispatcher/io_client.c
 create mode 100644 drivers/virt/bao/io-dispatcher/io_dispatcher.c
 create mode 100644 drivers/virt/bao/io-dispatcher/ioeventfd.c
 create mode 100644 drivers/virt/bao/io-dispatcher/irqfd.c
 create mode 100644 include/linux/bao.h
 create mode 100644 include/uapi/linux/bao.h
Documentation/userspace-api/ioctl/ioctl-number.rst +2 -0
index 3f0ef1e27eb0..3c6b0c060bab 100644
@@ -346,6 +346,8 @@ Code  Seq#    Include File                                             Comments
                                                                        <mailto:[email protected]>
 0xA5  20-2F  linux/surface_aggregator/dtx.h                            Microsoft Surface DTX driver
                                                                        <mailto:[email protected]>
+0xA6  all    uapi/linux/bao.h                                          Bao hypervisor
+                                                                       <mailto:[email protected]>
 0xAA  00-3F  linux/uapi/linux/userfaultfd.h
 0xAB  00-1F  linux/nbd.h
 0xAC  00-1F  linux/raw.h
arch/arm/include/asm/bao.h +30 -0
index ba64d1a18f91..eca258cc94e3 100644
@@ -14,6 +14,7 @@
 #define __ASM_ARM_BAO_H
 
 #include <linux/arm-smccc.h>
+#include <linux/bao.h>
 
 static inline unsigned long bao_ipcshmem_hypercall(unsigned long hypercall_id,
 						   unsigned long ipcshmem_id)
@@ -28,4 +29,33 @@ static inline unsigned long bao_ipcshmem_hypercall(unsigned long hypercall_id,
 	return res.a0;
 }
 
+static inline unsigned long
+bao_remio_hypercall(struct bao_remio_hypercall_ctx *ctx)
+{
+	register int r0 asm("r0") =
+		ARM_SMCCC_CALL_VAL(ARM_SMCCC_FAST_CALL, ARM_SMCCC_SMC_32,
+				   ARM_SMCCC_OWNER_VENDOR_HYP, BAO_REMIO_HYPERCALL_ID);
+	register u32 r1 asm("r1") = ctx->dm_id;
+	register u32 r2 asm("r2") = ctx->addr;
+	register u32 r3 asm("r3") = ctx->op;
+	register u32 r4 asm("r4") = ctx->value;
+	register u32 r5 asm("r5") = ctx->request_id;
+	register u32 r6 asm("r6") = 0;
+
+	asm volatile("hvc 0\n\t"
+		     : "=r"(r0), "=r"(r1), "=r"(r2), "=r"(r3), "=r"(r4),
+		       "=r"(r5), "=r"(r6)
+		     : "r"(r0), "r"(r1), "r"(r2), "r"(r3), "r"(r4), "r"(r5)
+		     : "memory");
+
+	ctx->addr = r1;
+	ctx->op = r2;
+	ctx->value = r3;
+	ctx->access_width = r4;
+	ctx->request_id = r5;
+	ctx->npend_req = r6;
+
+	return r0;
+}
+
 #endif /* __ASM_ARM_BAO_H */
arch/arm64/include/asm/bao.h +30 -0
index ab9b283168e3..1dc09a2c261b 100644
@@ -14,6 +14,7 @@
 #define __ASM_ARM64_BAO_H
 
 #include <linux/arm-smccc.h>
+#include <linux/bao.h>
 
 static inline unsigned long bao_ipcshmem_hypercall(unsigned long hypercall_id,
 						   unsigned long ipcshmem_id)
@@ -28,4 +29,33 @@ static inline unsigned long bao_ipcshmem_hypercall(unsigned long hypercall_id,
 	return res.a0;
 }
 
+static inline unsigned long
+bao_remio_hypercall(struct bao_remio_hypercall_ctx *ctx)
+{
+	register int x0 asm("x0") =
+		ARM_SMCCC_CALL_VAL(ARM_SMCCC_FAST_CALL, ARM_SMCCC_SMC_64,
+				   ARM_SMCCC_OWNER_VENDOR_HYP, BAO_REMIO_HYPERCALL_ID);
+	register u64 x1 asm("x1") = ctx->dm_id;
+	register u64 x2 asm("x2") = ctx->addr;
+	register u64 x3 asm("x3") = ctx->op;
+	register u64 x4 asm("x4") = ctx->value;
+	register u64 x5 asm("x5") = ctx->request_id;
+	register u64 x6 asm("x6") = 0;
+
+	asm volatile("hvc 0\n\t"
+		     : "=r"(x0), "=r"(x1), "=r"(x2), "=r"(x3), "=r"(x4),
+		       "=r"(x5), "=r"(x6)
+		     : "r"(x0), "r"(x1), "r"(x2), "r"(x3), "r"(x4), "r"(x5)
+		     : "memory");
+
+	ctx->addr = x1;
+	ctx->op = x2;
+	ctx->value = x3;
+	ctx->access_width = x4;
+	ctx->request_id = x5;
+	ctx->npend_req = x6;
+
+	return x0;
+}
+
 #endif /* __ASM_ARM64_BAO_H */
arch/riscv/include/asm/bao.h +37 -0
index d2c79a6a4ade..6dc0cb13c94c 100644
@@ -14,7 +14,16 @@
 #define __ASM_RISCV_BAO_H
 
 #include <asm/sbi.h>
+#include <linux/bao.h>
 
+/*
+ * Bao SBI extension ID.
+ *
+ * This currently lives in the SBI experimental extension space
+ * (0x08000000-0x08FFFFFF). A permanent ID has to be assigned through the
+ * RISC-V SBI specification before the RISC-V support can be considered
+ * stable; until then the RISC-V backend is experimental.
+ */
 #define BAO_SBI_EXT_ID 0x08000ba0
 
 static inline unsigned long bao_ipcshmem_hypercall(unsigned long hypercall_id,
@@ -28,4 +37,32 @@ static inline unsigned long bao_ipcshmem_hypercall(unsigned long hypercall_id,
 	return ret.error;
 }
 
+static inline unsigned long
+bao_remio_hypercall(struct bao_remio_hypercall_ctx *ctx)
+{
+	register uintptr_t a0 asm("a0") = (uintptr_t)(ctx->dm_id);
+	register uintptr_t a1 asm("a1") = (uintptr_t)(ctx->addr);
+	register uintptr_t a2 asm("a2") = (uintptr_t)(ctx->op);
+	register uintptr_t a3 asm("a3") = (uintptr_t)(ctx->value);
+	register uintptr_t a4 asm("a4") = (uintptr_t)(ctx->request_id);
+	register uintptr_t a5 asm("a5") = (uintptr_t)(0);
+	register uintptr_t a6 asm("a6") = (uintptr_t)(BAO_REMIO_HYPERCALL_ID);
+	register uintptr_t a7 asm("a7") = (uintptr_t)(BAO_SBI_EXT_ID);
+
+	asm volatile("ecall"
+		     : "+r"(a0), "+r"(a1), "+r"(a2), "+r"(a3), "+r"(a4),
+		       "+r"(a5), "+r"(a6), "+r"(a7)
+		     :
+		     : "memory");
+
+	ctx->addr = a2;
+	ctx->op = a3;
+	ctx->value = a4;
+	ctx->access_width = a5;
+	ctx->request_id = a6;
+	ctx->npend_req = a7;
+
+	return a0;
+}
+
 #endif /* __ASM_RISCV_BAO_H */
drivers/virt/Makefile +1 -0
index 623a671f8711..8bffc7ccd29e 100644
@@ -11,3 +11,4 @@ obj-$(CONFIG_NITRO_ENCLAVES)	+= nitro_enclaves/
 obj-$(CONFIG_ACRN_HSM)		+= acrn/
 obj-y				+= coco/
 obj-$(CONFIG_BAO_SHMEM)		+= bao/
+obj-$(CONFIG_BAO_IO_DISPATCHER)	+= bao/
drivers/virt/bao/Kconfig +2 -0
index 4f7929d57475..ab08a20db8c4 100644
@@ -1,3 +1,5 @@
 # SPDX-License-Identifier: GPL-2.0
 
 source "drivers/virt/bao/ipcshmem/Kconfig"
+
+source "drivers/virt/bao/io-dispatcher/Kconfig"
drivers/virt/bao/Makefile +1 -0
index 68f5d3f282c4..c463f04cf206 100644
@@ -1,3 +1,4 @@
 # SPDX-License-Identifier: GPL-2.0
 
 obj-$(CONFIG_BAO_SHMEM) += ipcshmem/
+obj-$(CONFIG_BAO_IO_DISPATCHER) += io-dispatcher/
drivers/virt/bao/io-dispatcher/Kconfig new file +16 -0
new file mode 100644
index 000000000000..cf3e8df0f2f4
@@ -0,0 +1,16 @@
+# SPDX-License-Identifier: GPL-2.0
+config BAO_IO_DISPATCHER
+	tristate "Bao Hypervisor I/O Dispatcher"
+	select EVENTFD
+	help
+	  The Bao I/O Dispatcher is a kernel module for backend Linux VMs
+	  running under the Bao hypervisor. It establishes the connection
+	  between the Remote I/O system (Bao's mechanism for forwarding
+	  I/O requests from frontend VMs to the backend VMs) and the
+	  VirtIO backend device.
+
+	  This provides a unified API to support various VirtIO backends,
+	  allowing Bao guests to perform I/O through the hypervisor
+	  transparently.
+
+	  If unsure, say N.
drivers/virt/bao/io-dispatcher/Makefile new file +4 -0
new file mode 100644
index 000000000000..e18de1d1a026
@@ -0,0 +1,4 @@
+# SPDX-License-Identifier: GPL-2.0
+obj-$(CONFIG_BAO_IO_DISPATCHER) += bao.o
+bao-objs += ioeventfd.o io_client.o io_dispatcher.o irqfd.o dm.o intc.o driver.o
+
drivers/virt/bao/io-dispatcher/bao_drv.h new file +349 -0

349 lines not on this page · raw message

drivers/virt/bao/io-dispatcher/dm.c new file +316 -0

316 lines not on this page · raw message

drivers/virt/bao/io-dispatcher/driver.c new file +95 -0

95 lines not on this page · raw message

drivers/virt/bao/io-dispatcher/intc.c new file +64 -0

64 lines not on this page · raw message

drivers/virt/bao/io-dispatcher/io_client.c new file +401 -0

401 lines not on this page · raw message

drivers/virt/bao/io-dispatcher/io_dispatcher.c new file +181 -0

181 lines not on this page · raw message

drivers/virt/bao/io-dispatcher/ioeventfd.c new file +323 -0

323 lines not on this page · raw message

drivers/virt/bao/io-dispatcher/irqfd.c new file +314 -0

314 lines not on this page · raw message

include/linux/bao.h new file +41 -0

41 lines not on this page · raw message

include/uapi/linux/bao.h new file +96 -0

96 lines not on this page · raw message

Signature · 1 line
2.43.0
#9 lore raw

RFC PATCH v3 5/6 virt: bao: consolidate the IPC hypercall ID in include/linux/bao.h

In reply to João Peixoto

The IPC shared-memory hypercall ID was passed as a parameter through each architecture's bao_ipcshmem_hypercall() and defined locally in ipcshmem.c. Define it once in include/linux/bao.h, next to the Remote I/O hypercall ID, and reference it directly from the arch helpers. As there is only one IPC hypercall, this also lets bao_ipcshmem_hypercall() drop its hypercall_id parameter.

---
v3:
- Rewrite the commit message to explain why the ID is consolidated and to describe the bao_ipcshmem_hypercall() parameter removal it performs (Greg KH).
- The helper now takes a single argument and fits on one line; unwrap the remaining few-char line wraps (Andrew Jones).

 arch/arm/include/asm/bao.h           | 5 ++---
 arch/arm64/include/asm/bao.h         | 5 ++---
 arch/riscv/include/asm/bao.h         | 6 ++----
 drivers/virt/bao/ipcshmem/ipcshmem.c | 5 +----
 include/linux/bao.h                  | 3 +++
 5 files changed, 10 insertions(+), 14 deletions(-)
arch/arm/include/asm/bao.h +2 -3
index eca258cc94e3..a7d608493723 100644
@@ -16,14 +16,13 @@
 #include <linux/arm-smccc.h>
 #include <linux/bao.h>
 
-static inline unsigned long bao_ipcshmem_hypercall(unsigned long hypercall_id,
-						   unsigned long ipcshmem_id)
+static inline unsigned long bao_ipcshmem_hypercall(unsigned long ipcshmem_id)
 {
 	struct arm_smccc_res res;
 
 	arm_smccc_hvc(ARM_SMCCC_CALL_VAL(ARM_SMCCC_FAST_CALL, ARM_SMCCC_SMC_32,
 					 ARM_SMCCC_OWNER_VENDOR_HYP,
-					 hypercall_id),
+					 BAO_IPCSHMEM_HYPERCALL_ID),
 		      ipcshmem_id, 0, 0, 0, 0, 0, 0, &res);
 
 	return res.a0;
arch/arm64/include/asm/bao.h +2 -3
index 1dc09a2c261b..5bd2a8c9905d 100644
@@ -16,14 +16,13 @@
 #include <linux/arm-smccc.h>
 #include <linux/bao.h>
 
-static inline unsigned long bao_ipcshmem_hypercall(unsigned long hypercall_id,
-						   unsigned long ipcshmem_id)
+static inline unsigned long bao_ipcshmem_hypercall(unsigned long ipcshmem_id)
 {
 	struct arm_smccc_res res;
 
 	arm_smccc_hvc(ARM_SMCCC_CALL_VAL(ARM_SMCCC_FAST_CALL, ARM_SMCCC_SMC_64,
 					 ARM_SMCCC_OWNER_VENDOR_HYP,
-					 hypercall_id),
+					 BAO_IPCSHMEM_HYPERCALL_ID),
 		      ipcshmem_id, 0, 0, 0, 0, 0, 0, &res);
 
 	return res.a0;
arch/riscv/include/asm/bao.h +2 -4
index 6dc0cb13c94c..a587a6abd54e 100644
@@ -26,13 +26,11 @@
  */
 #define BAO_SBI_EXT_ID 0x08000ba0
 
-static inline unsigned long bao_ipcshmem_hypercall(unsigned long hypercall_id,
-						   unsigned long ipcshmem_id)
+static inline unsigned long bao_ipcshmem_hypercall(unsigned long ipcshmem_id)
 {
 	struct sbiret ret;
 
-	ret = sbi_ecall(BAO_SBI_EXT_ID, hypercall_id, ipcshmem_id, 0, 0, 0, 0,
-			0);
+	ret = sbi_ecall(BAO_SBI_EXT_ID, BAO_IPCSHMEM_HYPERCALL_ID, ipcshmem_id, 0, 0, 0, 0, 0);
 
 	return ret.error;
 }
drivers/virt/bao/ipcshmem/ipcshmem.c +1 -4
index 0d46d89ee788..5bf37c1c7933 100644
@@ -14,9 +14,6 @@
 
 #define BAO_IPCSHMEM_NAME_LEN 16
 
-/* IPC through shared-memory hypercall ID */
-#define BAO_IPCSHMEM_HYPERCALL_ID 0x1
-
 struct bao_ipcshmem {
 	struct miscdevice miscdev;
 	u32 id;
@@ -104,7 +101,7 @@ static ssize_t bao_ipcshmem_write(struct file *filp, const char __user *buf,
 	*ppos += count;
 
 	/* Notify Bao hypervisor */
-	bao_ipcshmem_hypercall(BAO_IPCSHMEM_HYPERCALL_ID, bao->id);
+	bao_ipcshmem_hypercall(bao->id);
 
 	return count;
 }
include/linux/bao.h +3 -0
index 076f071086b6..17d6ea472521 100644
@@ -15,6 +15,9 @@
 
 #include <linux/types.h>
 
+/* IPC through shared-memory hypercall ID */
+#define BAO_IPCSHMEM_HYPERCALL_ID 0x1
+
 /* Remote I/O Hypercall ID */
 #define BAO_REMIO_HYPERCALL_ID 0x2
 
Signature · 1 line
2.43.0
#11 lore raw

RFC PATCH v3 6/6 MAINTAINERS: add Bao hypervisor entry

In reply to João Peixoto

Add a MAINTAINERS entry covering the Bao hypervisor drivers, the architecture headers, the in-kernel and UAPI headers, and the device-tree bindings.

---
v3:
- Concise commit message (Krzysztof Kozlowski).

 MAINTAINERS | 13 +++++++++++++
 1 file changed, 13 insertions(+)
MAINTAINERS +13 -0
index 15011f5752a9..6a81a8da58f1 100644
@@ -4519,6 +4519,19 @@ F:	drivers/video/backlight/
 F:	include/linux/backlight.h
 F:	include/linux/pwm_backlight.h
 
+BAO HYPERVISOR
+M:	José Martins <[email protected]>
+M:	David Cerdeira <[email protected]>
+M:	João Peixoto <[email protected]>
+S:	Maintained
+F:	Documentation/devicetree/bindings/bao/
+F:	arch/arm/include/asm/bao.h
+F:	arch/arm64/include/asm/bao.h
+F:	arch/riscv/include/asm/bao.h
+F:	drivers/virt/bao
+F:	include/linux/bao.h
+F:	include/uapi/linux/bao.h
+
 BARCO P50 GPIO DRIVER
 M:	Santosh Kumar Yadav <[email protected]>
 M:	Peter Korsgaard <[email protected]>
Signature · 1 line
2.43.0
#2 lore raw

Re: [RFC PATCH v3 1/6] dt-bindings: bao: add IPC shared-memory device

In reply to João Peixoto
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Low] The new vendor prefix '^bao,.*' is inserted out of alphabetical order in vendor-prefixes.yaml. --

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Low] The new vendor prefix '^bao,.*' is inserted out of alphabetical order in vendor-prefixes.yaml. --

--- Patch [1]: [RFC PATCH v3 1/6] dt-bindings: bao: add IPC shared-memory device --- commit 41886074b8a6e75abf127210a8dc6414ebf6935a
Author: João Peixoto <[email protected]>

dt-bindings: bao: add IPC shared-memory device

Add the device-tree binding for the Bao hypervisor IPC shared-memory device, used by a Bao guest to exchange data with a peer guest over a shared-memory region.

[ ... ]

Documentation/devicetree/bindings/vendor-prefixes.yaml patternProperties: · line 236

Quoted 11 lines
Documentation/devicetree/bindings/vendor-prefixes.yaml +2 -0
index 396044f368e7c..b11b3dd995a53 100644
@@ -231,6 +231,8 @@ patternProperties:
     description: Shenzhen AZW Technology Co., Ltd.
   "^baikal,.*":
     description: BAIKAL ELECTRONICS, JSC
+  "^bao,.*":
+    description: Bao Project (https://github.com/bao-project)
   "^bananapi,.*":

[Severity: Low]
This isn't a bug, but should the bao prefix be placed after bananapi to maintain alphabetical sorting?

Quoted 2 lines
Documentation/devicetree/bindings/vendor-prefixes.yaml +0 -0
     description: BIPAI KEJI LIMITED
   "^beacon,.*":
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
#10 lore raw

Re: [RFC PATCH v3 5/6] virt: bao: consolidate the IPC hypercall ID in include/linux/bao.h

In reply to João Peixoto
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:

Pre-existing issues:
- [High] Missing memory barrier (e.g., wmb()) before notifying the hypervisor of newly written data, potentially causing the hypervisor or the receiving VM to read stale memory. --

--- Patch [5]: [RFC PATCH v3 5/6] virt: bao: consolidate the IPC hypercall ID in include/linux/bao.h --- commit 392291f754c5a86346cd59911eed878c78654f9d
Author: João Peixoto <[email protected]>

virt: bao: consolidate the IPC hypercall ID in include/linux/bao.h

This commit consolidates the IPC shared-memory hypercall ID into a common header. It also simplifies the architecture-specific bao_ipcshmem_hypercall() helpers by dropping the hypercall_id parameter.

Quoted 4 lines
drivers/virt/bao/ipcshmem/ipcshmem.c +0 -0
index 0d46d89ee788b..5bf37c1c7933d 100644

[ ... ]

drivers/virt/bao/ipcshmem/ipcshmem.c static ssize_t bao_ipcshmem_write(struct file *filp, const char __user *buf, · line 104

Quoted 6 lines
drivers/virt/bao/ipcshmem/ipcshmem.c +1 -1
@@ -104,7 +101,7 @@ static ssize_t bao_ipcshmem_write(struct file *filp, const char __user *buf,
 	*ppos += count;
 
 	/* Notify Bao hypervisor */
-	bao_ipcshmem_hypercall(BAO_IPCSHMEM_HYPERCALL_ID, bao->id);
+	bao_ipcshmem_hypercall(bao->id);

[Severity: High]
This isn't a bug introduced by this patch, but is there a missing memory barrier before notifying the hypervisor here in bao_ipcshmem_write()?

Data is written to the shared memory via copy_from_user() just before this, but no memory barrier ensures global visibility of the write prior to the notification.

The ARM SMCCC explicitly states that callers must ensure memory updates are visible before invoking SMC/HVC, meaning an explicit memory barrier (like wmb()) is architecturally required to prevent stale reads.

Without it, could the hypervisor or the receiving VM read stale memory before the stores from copy_from_user() become globally visible across PEs?

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=5
#7 lore raw

Re: [RFC PATCH v3 4/6] virt: bao: add I/O dispatcher driver

In reply to João Peixoto
Thank you for your contribution! Sashiko AI review found 16 potential issue(s) to consider: - [High] Userspace can spoof the `dm_id` in `BAO_IOCTL_IO_REQUEST_COMPLETE`, allowing one device model…

Thank you for your contribution! Sashiko AI review found 16 potential issue(s) to consider:
- [High] Userspace can spoof the dm_id in BAO_IOCTL_IO_REQUEST_COMPLETE, allowing one device model instance to interfere with others.
- [High] Unrecoverable self-deadlock in the I/O client kernel thread during teardown.
- [High] Global interrupt handler pointer breaks multi-DM support by starving active DMs of interrupts.
- [High] TOCTOU Use-After-Free race in bao_irqfd_assign() when polling the eventfd.
- [High] Missing workqueue cancellation during irqfd shutdown causes Use-After-Free on asynchronous POLLHUP.
- [High] Device unbind frees struct bao_dm while userspace holds open file descriptors.
- [High] Calling destroy_workqueue before removing eventfd wait queues creates a Use-After-Free on the irqfd workqueue.
- [High] Passing a stack-allocated string as the 'name' argument to request_irq() leads to a Use-After-Free.
- [High] A missing return statement in bao_ioeventfd_client_config silently breaks ioeventfd deassignment.
- [High] The I/O dispatcher workqueue queues bao_io_request structures unboundedly, allowing a malicious frontend VM to cause a kernel OOM.
- [Medium] The struct bao_dm_info UAPI structure contains uninitialized implicit padding holes, leaking kernel stack memory to userspace.
- [Medium] Ioeventfd deassignment matches only by eventfd, breaking targeted range deregistration.
- [Medium] Signals are incorrectly handled in bao_io_client_attach, translating normal signal interruptions into spurious -EFAULT errors in userspace.
- [Medium] The bao_io_client_destroy function fails to free pending I/O requests.
- [Medium] Unused kernel memory mapping of backend shared memory wastes vmalloc space.
- [Medium] Silent discard of I/O requests when copy_to_user fails or memory allocation fails.
--

--- Patch [4]: [RFC PATCH v3 4/6] virt: bao: add I/O dispatcher driver --- commit fc6ea3f8ec16168d50252a311b2bf53f0409c312
Author: João Peixoto <[email protected]>
Subject: virt: bao: add I/O dispatcher driver

Add the Bao I/O dispatcher, used by backend VMs to service I/O on behalf of frontend guests. It bridges Bao's Remote I/O mechanism to userspace VirtIO backend device models.

include/uapi/linux/bao.h struct bao_irqfd { · line 75

Quoted 11 lines
include/uapi/linux/bao.h +6 -0
@@ -68,6 +68,13 @@ struct bao_irqfd {
 };
 
+struct bao_dm_info {
+	__u32 id;
+	__u64 shmem_addr;
+	__u64 shmem_size;
+	__u32 irq;
+};

[Severity: Medium]
Does this UAPI structure leak kernel stack memory to userspace? The structure contains implicit padding holes after the 32-bit fields.

When it is allocated on the stack in bao_io_dispatcher_driver_probe() and copied to userspace via bao_dm_ioctl() using copy_to_user(), the uninitialized 4-byte holes are leaked.

Quoted 6 lines
drivers/virt/bao/io-dispatcher/dm.c +3 -0
@@ -34,6 +34,107 @@ static int bao_dm_release(struct inode *inode, struct file *filp)
+static long bao_dm_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
+{
+	struct bao_dm *dm = filp->private_data;

[ ... ]

Quoted 16 lines
drivers/virt/bao/io-dispatcher/dm.c +16 -0
+	case BAO_IOCTL_IO_CLIENT_ATTACH: {
+		struct bao_virtio_request *req;
+
+		req = memdup_user((void __user *)arg, sizeof(*req));
+		if (IS_ERR(req)) {
+			rc = PTR_ERR(req);
+			break;
+		}
+
+		if (!dm->control_client) {
+			rc = -ENOENT;
+			goto out_free;
+		}
+
+		rc = bao_io_client_attach(dm->control_client);
+		if (rc)

drivers/virt/bao/io-dispatcher/dm.c line 78

Quoted 10 lines
drivers/virt/bao/io-dispatcher/dm.c +10 -0
+			goto out_free;
+
+		rc = bao_io_client_request(dm->control_client, req);
+		if (rc)
+			goto out_free;
+
+		if (copy_to_user((void __user *)arg, req, sizeof(*req))) {
+			rc = -EFAULT;
+			goto out_free;
+		}

[Severity: Medium]
What happens to the I/O request if copy_to_user() fails here? The request is permanently popped from the queue in bao_io_client_request().

If copying it to userspace fails, the request appears to be silently discarded, which would leave the frontend guest VM hanging indefinitely.

Quoted 8 lines
drivers/virt/bao/io-dispatcher/dm.c +8 -0
+	case BAO_IOCTL_IO_REQUEST_COMPLETE: {
+		struct bao_virtio_request *req;
+		struct bao_remio_hypercall_ctx ctx;
+
+		req = memdup_user((void __user *)arg, sizeof(*req));
+		if (IS_ERR(req)) {
+			rc = PTR_ERR(req);
+			break;

drivers/virt/bao/io-dispatcher/dm.c line 103

Quoted 10 lines
drivers/virt/bao/io-dispatcher/dm.c +10 -0
+		}
+
+		ctx.dm_id = req->dm_id;
+		ctx.addr = req->addr;
+		ctx.op = req->op;
+		ctx.value = req->value;
+		ctx.access_width = req->access_width;
+		ctx.request_id = req->request_id;
+
+		rc = bao_remio_hypercall(&ctx);

[Severity: High]
Can userspace spoof the dm_id in this hypercall context? The request's dm_id is blindly copied from userspace instead of being validated against the dm associated with the file descriptor (dm->info.id).

Could this allow an unprivileged frontend process to complete or corrupt requests belonging to other device models?

[ ... ]

Quoted 4 lines
drivers/virt/bao/io-dispatcher/dm.c +4 -0
+struct bao_dm *bao_dm_create(struct bao_dm_info *info, struct device *dev)
+{
+	struct bao_dm *dm;
+	int ret;

[ ... ]

drivers/virt/bao/io-dispatcher/dm.c line 265

Quoted 2 lines
drivers/virt/bao/io-dispatcher/dm.c +2 -0
+	dm->shmem_base_addr = memremap(dm->info.shmem_addr, dm->info.shmem_size,
+				       MEMREMAP_WB);

[Severity: Medium]
Is this kernel memory mapping necessary? The mapped physical memory pointer dm->shmem_base_addr is never dereferenced by the kernel for reading or writing.

Since userspace maps the memory directly via remap_pfn_range() using the physical address, does this just waste kernel virtual address space?

[ ... ]

Quoted 6 lines
drivers/virt/bao/io-dispatcher/dm.c +6 -0
+void bao_dm_destroy(struct bao_dm *dm)
+{
+	if (WARN_ON_ONCE(!dm))
+		return;
+
+	misc_deregister(&dm->miscdev);

drivers/virt/bao/io-dispatcher/dm.c line 316

Quoted 10 lines
drivers/virt/bao/io-dispatcher/dm.c +10 -0
+
+	if (dm->shmem_base_addr)
+		memunmap(dm->shmem_base_addr);
+
+	bao_irqfd_server_destroy(dm);
+	bao_io_clients_destroy(dm);
+	bao_io_dispatcher_destroy(dm);
+
+	kfree(dm);
+}

[Severity: High]
Is it safe to unconditionally free the dm structure here? This runs during the driver's remove callback, but misc_deregister() does not revoke existing open file descriptors.

If userspace holds the device open and issues an ioctl() or mmap() after the driver unbinds, the VFS callbacks will dereference the freed dm pointer, leading to a use-after-free.

drivers/virt/bao/io-dispatcher/intc.c line 14

Quoted 5 lines
drivers/virt/bao/io-dispatcher/intc.c +2 -0
@@ -13,6 +13,52 @@
+/* Top-level handler registered by the Bao interrupt controller */
+static void (*bao_intc_handler)(struct bao_dm *dm);

[Severity: High]
Does this global function pointer break multi-DM support? When any single DM is paused or destroyed, bao_intc_remove_handler() sets this global pointer to NULL.

This would silently disable I/O interrupt dispatching for all other active DMs on the system.

[ ... ]

drivers/virt/bao/io-dispatcher/intc.c line 56

Quoted 11 lines
drivers/virt/bao/io-dispatcher/intc.c +11 -0
+int bao_intc_init(struct bao_dm *dm)
+{
+	char name[BAO_NAME_MAX_LEN];
+
+	if (WARN_ON_ONCE(!dm))
+		return -EINVAL;
+
+	scnprintf(name, sizeof(name), "bao-iodintc%d", dm->info.id);
+
+	return request_irq(dm->info.irq, bao_interrupt_handler, 0, name, dm);
+}

[Severity: High]
Can this lead to a use-after-free when reading /proc/interrupts? The character array name is allocated on the stack and passed to request_irq().

The IRQ subsystem stores this pointer directly without copying the string. When this function returns and the stack frame is destroyed, any subsequent access to the interrupt name will dereference a stale stack pointer.

drivers/virt/bao/io-dispatcher/io_client.c static inline bool bao_io_client_is_destroying(struct bao_io_client *client) · line 53

Quoted 12 lines
drivers/virt/bao/io-dispatcher/io_client.c +9 -0
@@ -45,6 +45,356 @@ static inline bool bao_io_client_is_destroying(struct bao_io_client *client)
+bool bao_io_client_push_request(struct bao_io_client *client,
+				struct bao_virtio_request *req)
+{
+	struct bao_io_request *io_req;
+
+	if (WARN_ON_ONCE(!client || !req))
+		return false;
+
+	io_req = kzalloc_obj(*io_req, GFP_KERNEL);

[Severity: High]
Could this allow a malicious frontend VM to cause a host kernel OOM? The io_dispatcher workqueue continually pulls pending I/O requests from the hypervisor and pushes them here, which allocates memory with GFP_KERNEL.

There doesn't appear to be any limit or backpressure mechanism if userspace processes requests slower than they are produced.

[ ... ]

Quoted 12 lines
drivers/virt/bao/io-dispatcher/io_client.c +12 -0
+static void bao_io_client_destroy(struct bao_io_client *client)
+{
+	struct bao_io_client *range;
+	struct bao_io_client *next;
+	struct bao_dm *dm;
+
+	if (WARN_ON_ONCE(!client))
+		return;
+
+	dm = client->dm;
+
+	bao_io_dispatcher_pause(dm);

drivers/virt/bao/io-dispatcher/io_client.c line 131

Quoted 10 lines
drivers/virt/bao/io-dispatcher/io_client.c +10 -0
+
+	set_bit(BAO_IO_CLIENT_DESTROYING, &client->flags);
+
+	if (client->is_control) {
+		wake_up_interruptible(&client->wq);
+	} else {
+		bao_ioeventfd_client_destroy(dm);
+		if (client->thread)
+			kthread_stop(client->thread);
+	}

[Severity: High]
Could this cause an unrecoverable self-deadlock? In bao_io_client_attach(), when kthread_should_stop() is true, the kthread recursively calls bao_io_client_destroy() on itself.

Inside this call, it invokes kthread_stop() on its own thread context, which blocks waiting for the thread to exit.

[ ... ]

drivers/virt/bao/io-dispatcher/io_client.c line 152

Quoted 13 lines
drivers/virt/bao/io-dispatcher/io_client.c +13 -0
+	down_write(&dm->io_clients_lock);
+	if (client->is_control)
+		dm->control_client = NULL;
+	else
+		dm->ioeventfd_client = NULL;
+
+	list_del(&client->list);
+	up_write(&dm->io_clients_lock);
+
+	bao_io_dispatcher_resume(dm);
+
+	kfree(client);
+}

[Severity: Medium]
Is there a memory leak here? When the client is destroyed, its range_list is cleaned up, but it does not iterate over the virtio_requests list to free pending struct bao_io_request items before calling kfree(client).

drivers/virt/bao/io-dispatcher/io_client.c line 176

Quoted 11 lines
drivers/virt/bao/io-dispatcher/io_client.c +11 -0
+int bao_io_client_attach(struct bao_io_client *client)
+{
+	if (WARN_ON_ONCE(!client))
+		return -EINVAL;
+
+	if (client->is_control) {
+		wait_event_interruptible(client->wq,
+					 bao_io_client_has_pending_requests(client) ||
+					 bao_io_client_is_destroying(client));
+		if (bao_io_client_is_destroying(client))
+			return -EPERM;

[Severity: Medium]
Does this mishandle signals? The wait_event_interruptible() macro returns -ERESTARTSYS if interrupted by a signal, but this return value is ignored.

As a result, normal signal interruptions cause bao_dm_ioctl() to attempt to pop an empty queue, failing and returning a spurious -EFAULT to userspace.

Quoted 8 lines
drivers/virt/bao/io-dispatcher/io_dispatcher.c +5 -0
@@ -25,6 +25,156 @@ struct bao_io_dispatcher_work {
+int bao_dispatch_io(struct bao_dm *dm)
+{
+	struct bao_io_client *client;
+	struct bao_remio_hypercall_ctx ctx;
+	struct bao_virtio_request req;

[ ... ]

drivers/virt/bao/io-dispatcher/io_dispatcher.c line 87

Quoted 4 lines
drivers/virt/bao/io-dispatcher/io_dispatcher.c +4 -0
+	if (!bao_io_client_push_request(client, &req)) {
+		up_read(&dm->io_clients_lock);
+		return -EINVAL;
+	}

[Severity: Medium]
Are requests silently discarded here if bao_io_client_push_request() fails? If memory exhaustion prevents the allocation, the request fetched from the hypervisor is dropped, permanently hanging the frontend guest waiting for I/O completion.

drivers/virt/bao/io-dispatcher/ioeventfd.c static int bao_ioeventfd_deassign(struct bao_dm *dm, · line 210

Quoted 13 lines
drivers/virt/bao/io-dispatcher/ioeventfd.c +10 -0
@@ -201,6 +201,122 @@ static int bao_ioeventfd_deassign(struct bao_dm *dm,
+	list_for_each_entry(p, &dm->ioeventfds, list) {
+		if (p->eventfd != eventfd)
+			continue;
+
+		bao_io_client_range_del(dm->ioeventfd_client, p->addr,
+					p->addr + p->length - 1);
+
+		bao_ioeventfd_shutdown(dm, p);
+		break;
+	}

[Severity: Medium]
Does this deassignment loop match solely on the eventfd context and ignore the requested memory address range? If userspace registers multiple memory ranges to trigger the same eventfd, this will arbitrarily delete the first matching entry rather than the specific range requested by userspace.

[ ... ]

drivers/virt/bao/io-dispatcher/ioeventfd.c line 289

Quoted 10 lines
drivers/virt/bao/io-dispatcher/ioeventfd.c +10 -0
+int bao_ioeventfd_client_config(struct bao_dm *dm, struct bao_ioeventfd *config)
+{
+	if (WARN_ON_ONCE(!dm || !config))
+		return -EINVAL;
+
+	if (config->flags & BAO_IOEVENTFD_FLAG_DEASSIGN)
+		bao_ioeventfd_deassign(dm, config);
+
+	return bao_ioeventfd_assign(dm, config);
+}

[Severity: High]
Is a return statement missing here? Because there is no return after calling bao_ioeventfd_deassign(), execution falls through to bao_ioeventfd_assign(), immediately recreating the ioeventfd that was just destroyed.

Quoted 10 lines
drivers/virt/bao/io-dispatcher/irqfd.c +7 -0
@@ -34,6 +34,280 @@ struct irqfd {
+static void bao_irqfd_shutdown(struct irqfd *irqfd)
+{
+	u64 cnt;
+
+	if (WARN_ON_ONCE(!irqfd || !irqfd->dm))
+		return;
+

drivers/virt/bao/io-dispatcher/irqfd.c struct irqfd { · line 50

Quoted 10 lines
drivers/virt/bao/io-dispatcher/irqfd.c +10 -0
+	lockdep_assert_held(&irqfd->dm->irqfds_lock);
+
+	list_del_init(&irqfd->list);
+
+	eventfd_ctx_remove_wait_queue(irqfd->eventfd, &irqfd->wait, &cnt);
+
+	eventfd_ctx_put(irqfd->eventfd);
+
+	kfree(irqfd);
+}

[Severity: High]
Is it safe to free the irqfd here without cancelling the asynchronous shutdown work? If userspace closes the eventfd concurrently, a POLLHUP event queues the shutdown work.

The irqfd is then freed, and when the workqueue executes irqfd_shutdown_work(), it dereferences the freed memory to access irqfd->dm.

[ ... ]

Quoted 4 lines
drivers/virt/bao/io-dispatcher/irqfd.c +4 -0
+static int bao_irqfd_assign(struct bao_dm *dm, struct bao_irqfd *args)
+{
+	struct eventfd_ctx *eventfd = NULL;
+	struct irqfd *irqfd;

[ ... ]

drivers/virt/bao/io-dispatcher/irqfd.c line 219

Quoted 4 lines
drivers/virt/bao/io-dispatcher/irqfd.c +4 -0
+	list_add_tail(&irqfd->list, &dm->irqfds);
+	mutex_unlock(&dm->irqfds_lock);
+
+	events = vfs_poll(fd_file(f), &irqfd->pt);

[Severity: High]
Could this cause a use-after-free race? The newly created irqfd is published to the globally visible dm->irqfds list, and the lock is dropped before calling vfs_poll().

A concurrent thread calling deassign can remove and free this irqfd while vfs_poll() proceeds to access the freed &irqfd->pt.

[ ... ]

Quoted 6 lines
drivers/virt/bao/io-dispatcher/irqfd.c +6 -0
+void bao_irqfd_server_destroy(struct bao_dm *dm)
+{
+	struct irqfd *irqfd;
+	struct irqfd *next;
+
+	if (WARN_ON_ONCE(!dm))

drivers/virt/bao/io-dispatcher/irqfd.c line 314

Quoted 10 lines
drivers/virt/bao/io-dispatcher/irqfd.c +10 -0
+		return;
+
+	if (dm->irqfd_server)
+		destroy_workqueue(dm->irqfd_server);
+
+	mutex_lock(&dm->irqfds_lock);
+	list_for_each_entry_safe(irqfd, next, &dm->irqfds, list)
+		bao_irqfd_shutdown(irqfd);
+	mutex_unlock(&dm->irqfds_lock);
+}

[Severity: High]
Can this lead to a use-after-free on the irqfd workqueue? Calling destroy_workqueue() before shutting down the active eventfds leaves their wait queues active.

If an eventfd receives an event in this window, bao_irqfd_wakeup() will execute queue_work() on the destroyed and freed workqueue pointer.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=4
#4 lore raw

Re: [RFC PATCH v3 2/6] virt: bao: add IPC shared-memory driver

In reply to João Peixoto
Thank you for your contribution! Sashiko AI review found 8 potential issue(s) to consider: - [High] Unbinding the device while the misc character device is open causes a Use-After-Free of the driver…

Thank you for your contribution! Sashiko AI review found 8 potential issue(s) to consider:
- [High] Unbinding the device while the misc character device is open causes a Use-After-Free of the driver context and unmapped memory access.
- [High] Missing memory barrier before notifying the peer guest via hypercall, causing potential data corruption due to stale memory observation.
- [High] Missing write-protection enforcement allows userspace to mmap the read region with write permissions, triggering a hypervisor stage-2 fault.
- [Medium] The mmap offset calculation uses unsigned long, which overflows on 32-bit architectures, contradicting the author's intent to fix u32 overflows.
- [Medium] The mmap logic fails entirely for the write region if the read region's size is not page-aligned.
- [Medium] Missing .llseek operation prevents userspace from resetting the file position, allowing the device to be read or written only once per open.
- [Low] The device label string buffer is too small to hold the maximum device ID, leading to truncation and potential naming collisions.
- [Low] Missing architecture dependencies in Kconfig will cause build failures on unsupported architectures.
--

--- Patch [2]: [RFC PATCH v3 2/6] virt: bao: add IPC shared-memory driver --- commit 79e4d2a70a45b49aa525420ee0e59987417ec11f
Author: João Peixoto <[email protected]>
Subject: virt: bao: add IPC shared-memory driver

Adds a driver for guests running on the Bao static-partitioning hypervisor to communicate through shared memory. Userspace accesses the regions through a misc character device using read(), write(), and mmap(), where a write() notifies the peer guest through an architecture-specific hypercall.

drivers/virt/bao/ipcshmem/Kconfig line 3

Quoted 7 lines
drivers/virt/bao/ipcshmem/Kconfig new file +3 -0
@@ -0,0 +1,10 @@
+# SPDX-License-Identifier: GPL-2.0
+config BAO_SHMEM
+	tristate "Bao hypervisor shared memory support"

[Severity: Low]
Does this config need an architecture dependency?

Building this on an unsupported architecture like x86 (e.g., via allmodconfig) might fail because it unconditionally includes <asm/bao.h> which only exists for ARM, ARM64, and RISC-V.

[ ... ]

Quoted 3 lines
drivers/virt/bao/ipcshmem/ipcshmem.c new file +0 -0

[ ... ]

drivers/virt/bao/ipcshmem/ipcshmem.c line 36

Quoted 5 lines
drivers/virt/bao/ipcshmem/ipcshmem.c new file +5 -0
+static int bao_ipcshmem_mmap(struct file *filp, struct vm_area_struct *vma)
+{
+	struct bao_ipcshmem *bao = filp->private_data;
+	unsigned long vsize = vma->vm_end - vma->vm_start;
+	unsigned long offset = vma->vm_pgoff << PAGE_SHIFT;

[Severity: Medium]
Will this offset calculation overflow on 32-bit architectures?

Since unsigned long is 32 bits there, an offset larger than 4GB might cause an overflow. This seems to contradict the commit message's goal to avoid u32 overflows.

Quoted 4 lines
drivers/virt/bao/ipcshmem/ipcshmem.c new file +4 -0
+	phys_addr_t region_phys;
+	size_t region_size;
+
+	if (!vsize)

drivers/virt/bao/ipcshmem/ipcshmem.c line 50

Quoted 10 lines
drivers/virt/bao/ipcshmem/ipcshmem.c new file +10 -0
+		return -EINVAL;
+
+	/*
+	 * The read region is exposed at offset 0 and the write region right
+	 * after it. A single mapping cannot span both regions, since they are
+	 * not guaranteed to be physically contiguous.
+	 */
+	if (offset < bao->read_size) {
+		region_phys = bao->read_phys;
+		region_size = bao->read_size;

[Severity: High]
Can a user map the read region with write permissions here?

If userspace opens the device O_RDWR and maps this region with PROT_WRITE, the kernel will create writable PTEs because vma->vm_page_prot is used directly during remap_pfn_range().

If the hypervisor maps the read region as read-only at Stage-2, a subsequent write from userspace could trigger a Stage-2 permission fault and crash the guest VM. Does this need a check to enforce !(vma->vm_flags & VM_WRITE)?

drivers/virt/bao/ipcshmem/ipcshmem.c line 52

Quoted 2 lines
drivers/virt/bao/ipcshmem/ipcshmem.c new file +2 -0
+	} else if (offset < bao->read_size + bao->write_size) {
+		offset -= bao->read_size;

[Severity: Medium]
What happens if the device tree specifies a read_size that is not page-aligned?

Subtracting an unaligned read_size from a page-aligned offset would result in an unaligned offset. This would then cause the PAGE_ALIGNED(region_phys) check below to fail unconditionally, preventing userspace from mapping the write region.

Quoted 5 lines
drivers/virt/bao/ipcshmem/ipcshmem.c new file +5 -0
+		region_phys = bao->write_phys;
+		region_size = bao->write_size;
+	} else {
+		return -EINVAL;
+	}

[ ... ]

Quoted 3 lines
drivers/virt/bao/ipcshmem/ipcshmem.c new file +3 -0
+static ssize_t bao_ipcshmem_write(struct file *filp, const char __user *buf,
+				  size_t count, loff_t *ppos)
+{

[ ... ]

drivers/virt/bao/ipcshmem/ipcshmem.c line 107

Quoted 7 lines
drivers/virt/bao/ipcshmem/ipcshmem.c new file +7 -0
+	if (copy_from_user(bao->write_base + *ppos, buf, count))
+		return -EFAULT;
+
+	*ppos += count;
+
+	/* Notify Bao hypervisor */
+	bao_ipcshmem_hypercall(BAO_IPCSHMEM_HYPERCALL_ID, bao->id);

[Severity: High]
Is a memory barrier needed here before notifying the peer guest?

The hypercall provides a Context Synchronization Event, but it does not guarantee that prior memory stores to the MEMREMAP_WB (cacheable) memory are globally visible.

Could the peer guest observe stale data if the local CPU's store buffer hasn't drained before the hypervisor wakes it up?

Quoted 2 lines
drivers/virt/bao/ipcshmem/ipcshmem.c new file +2 -0
+	return count;
+}

[ ... ]

drivers/virt/bao/ipcshmem/ipcshmem.c line 135

Quoted 8 lines
drivers/virt/bao/ipcshmem/ipcshmem.c new file +8 -0
+static const struct file_operations bao_ipcshmem_fops = {
+	.owner = THIS_MODULE,
+	.read = bao_ipcshmem_read,
+	.write = bao_ipcshmem_write,
+	.mmap = bao_ipcshmem_mmap,
+	.open = bao_ipcshmem_open,
+	.release = bao_ipcshmem_release,
+};

[Severity: Medium]
Does this file operations structure need an .llseek callback (such as default_llseek)?

Without it, the VFS will clear the FMODE_LSEEK flag during open(). Any subsequent lseek() calls will return -ESPIPE. Once a read() or write() hits the end of the shared memory region, userspace might be completely unable to rewind the offset without closing and reopening the character device.

Quoted 2 lines
drivers/virt/bao/ipcshmem/ipcshmem.c new file +2 -0
+static int bao_ipcshmem_probe(struct platform_device *pdev)
+{

[ ... ]

Quoted 8 lines
drivers/virt/bao/ipcshmem/ipcshmem.c new file +8 -0
+	bao = devm_kzalloc(dev, sizeof(*bao), GFP_KERNEL);
+	if (!bao)
+		return -ENOMEM;
+
+	bao->read_base = devm_memremap(dev, read_res->start,
+				       resource_size(read_res), MEMREMAP_WB);
+	if (IS_ERR(bao->read_base))
+		return PTR_ERR(bao->read_base);

[ ... ]

drivers/virt/bao/ipcshmem/ipcshmem.c line 181

Quoted 3 lines
drivers/virt/bao/ipcshmem/ipcshmem.c new file +3 -0
+	bao->id = id;
+	bao->read_phys = read_res->start;
+	bao->read_size = resource_size(read_res);

[Severity: Medium]
Will resource_size() be truncated here on 32-bit systems with LPAE?

The resource_size() helper returns a 64-bit resource_size_t, but read_size (and write_size) are defined as size_t in the bao_ipcshmem struct, which will be 32 bits on those platforms.

drivers/virt/bao/ipcshmem/ipcshmem.c line 185

Quoted 4 lines
drivers/virt/bao/ipcshmem/ipcshmem.c new file +4 -0
+	bao->write_phys = write_res->start;
+	bao->write_size = resource_size(write_res);
+
+	scnprintf(bao->label, BAO_IPCSHMEM_NAME_LEN, "baoipc%u", id);

[Severity: Low]
Can this label string be truncated?

If the bao,id from the device tree is a 10-digit number (e.g., 4294967295), the formatted string requires 17 bytes including the null terminator. Since BAO_IPCSHMEM_NAME_LEN is 16, the last digit could be truncated, potentially leading to naming collisions.

[ ... ]

drivers/virt/bao/ipcshmem/ipcshmem.c line 208

Quoted 6 lines
drivers/virt/bao/ipcshmem/ipcshmem.c new file +6 -0
+static void bao_ipcshmem_remove(struct platform_device *pdev)
+{
+	struct bao_ipcshmem *bao = platform_get_drvdata(pdev);
+
+	misc_deregister(&bao->miscdev);
+}

[Severity: High]
Can this result in a use-after-free if the driver is unbound while the character device is still open?

While misc_deregister() removes the device, it does not revoke existing open file descriptors. When bao_ipcshmem_remove() returns, devres will automatically free the bao structure and unmap the MEMREMAP_WB regions.

If a user process subsequently calls read, write, or mmap on the open file descriptor, could it dereference the freed bao structure and access unmapped memory?

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
maintainer
#8 lore raw

Re: [RFC PATCH v3 4/6] virt: bao: add I/O dispatcher driver

In reply to João Peixoto
Quoted 49 lines · João Peixoto

Add the Bao I/O dispatcher, used by backend VMs to service I/O on behalf of frontend guests. It bridges Bao's Remote I/O mechanism to userspace VirtIO backend device models.

Each backend device has a contiguous shared-memory region for exchanging I/O buffers with its frontend and an interrupt the hypervisor uses to signal pending requests. Userspace drives the dispatcher through a set of ioctls on a misc character device.

---

arch/arm64/include/asm/bao.h +19 -0
index ab9b283168e3..1dc09a2c261b 100644
@@ -14,6 +14,7 @@
 #define __ASM_ARM64_BAO_H
 
 #include <linux/arm-smccc.h>
+#include <linux/bao.h>
 
 static inline unsigned long bao_ipcshmem_hypercall(unsigned long hypercall_id,
 						   unsigned long ipcshmem_id)
@@ -28,4 +29,33 @@ static inline unsigned long bao_ipcshmem_hypercall(unsigned long hypercall_id,
 	return res.a0;
 }
 
+static inline unsigned long
+bao_remio_hypercall(struct bao_remio_hypercall_ctx *ctx)
+{
+	register int x0 asm("x0") =
+		ARM_SMCCC_CALL_VAL(ARM_SMCCC_FAST_CALL, ARM_SMCCC_SMC_64,
+				   ARM_SMCCC_OWNER_VENDOR_HYP, BAO_REMIO_HYPERCALL_ID);
+	register u64 x1 asm("x1") = ctx->dm_id;
+	register u64 x2 asm("x2") = ctx->addr;
+	register u64 x3 asm("x3") = ctx->op;
+	register u64 x4 asm("x4") = ctx->value;
+	register u64 x5 asm("x5") = ctx->request_id;
+	register u64 x6 asm("x6") = 0;
+
+	asm volatile("hvc 0\n\t"
+		     : "=r"(x0), "=r"(x1), "=r"(x2), "=r"(x3), "=r"(x4),
+		       "=r"(x5), "=r"(x6)
+		     : "r"(x0), "r"(x1), "r"(x2), "r"(x3), "r"(x4), "r"(x5)
+		     : "memory");

arch/arm64/include/asm/bao.h static inline unsigned long bao_ipcshmem_hypercall(unsigned long hypercall_id, · line 59

Quoted 10 lines
arch/arm64/include/asm/bao.h +10 -0
+
+	ctx->addr = x1;
+	ctx->op = x2;
+	ctx->value = x3;
+	ctx->access_width = x4;
+	ctx->request_id = x5;
+	ctx->npend_req = x6;
+
+	return x0;
+}

Why aren't you using SMCCC for this? You should be able to use that (like you did for bao_ipcshmem_hypercall()) and then there would be no need to add any code to arch/arm64/.

Will