drivers: misc: goldfish_address_space: add a driver
PATCH 1/1 drivers: misc: goldfish_address_space: add a driver
From: Roman Kiryanov <[email protected]>
A driver for the Goldfish Android emulator that occupies address space to use it with the memory sharing device on the QEMU side. The memory sharding device allocates subranges and populate them with actual RAM. This allows sharing host's memory with the guest.
--- drivers/misc/Kconfig | 9 + drivers/misc/Makefile | 1 + drivers/misc/goldfish_address_space.c | 742 ++++++++++++++++++ .../linux/goldfish/goldfish_address_space.h | 27 + 4 files changed, 779 insertions(+) create mode 100644 drivers/misc/goldfish_address_space.c create mode 100644 include/uapi/linux/goldfish/goldfish_address_space.h
drivers/misc/Kconfig +9 -0
@@ -521,6 +521,15 @@ config PVPANIC a paravirtualized device provided by QEMU; it lets a virtual machine (guest) communicate panic events to the host. +config GOLDFISH_ADDRESS_SPACE + tristate "A Goldfish driver that talks to the memory sharing device in QEMU" + depends on PCI + depends on GOLDFISH + help + A Goldfish driver that allocates address space ranges in the guest to + populate them later in the host. This allows sharing host's memory + with the guest. + source "drivers/misc/c2port/Kconfig" source "drivers/misc/eeprom/Kconfig" source "drivers/misc/cb710/Kconfig"
drivers/misc/Makefile +1 -0
@@ -59,3 +59,4 @@ obj-$(CONFIG_PCI_ENDPOINT_TEST) += pci_endpoint_test.o obj-$(CONFIG_OCXL) += ocxl/ obj-y += cardreader/ obj-$(CONFIG_PVPANIC) += pvpanic.o +obj-$(CONFIG_GOLDFISH_ADDRESS_SPACE) += goldfish_address_space.o
drivers/misc/goldfish_address_space.c new file +742 -0
742 lines not on this page · raw message
include/uapi/linux/goldfish/goldfish_address_space.h new file +27 -0
@@ -0,0 +1,27 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#ifndef UAPI_GOLDFISH_ADDRESS_SPACE_H +#define UAPI_GOLDFISH_ADDRESS_SPACE_H + +#include <linux/types.h> + +#define GOLDFISH_ADDRESS_SPACE_DEVICE_NAME "goldfish_address_space" + +struct goldfish_address_space_allocate_block { +__u64 size; +__u64 offset; +__u64 phys_addr; +}; + +#define GOLDFISH_ADDRESS_SPACE_IOCTL_MAGIC 'G' + +#define GOLDFISH_ADDRESS_SPACE_IOCTL_OP (OP ,T ) \ + _IOWR(GOLDFISH_ADDRESS_SPACE_IOCTL_MAGIC, OP, T) + +#define GOLDFISH_ADDRESS_SPACE_IOCTL_ALLOCATE_BLOCK \ + GOLDFISH_ADDRESS_SPACE_IOCTL_OP(10, \ + struct goldfish_address_space_allocate_block) + +#define GOLDFISH_ADDRESS_SPACE_IOCTL_DEALLOCATE_BLOCK \ + GOLDFISH_ADDRESS_SPACE_IOCTL_OP(11, __u64) + +#endif /* UAPI_GOLDFISH_ADDRESS_SPACE_H */
Signature · 1 line
2.20.1.415.g653613c723-goog
Greg KH
maintainerQuoted 7 lines · On Thu, Jan 03, 2019 at 06:13:11PM -0800, [email protected] wrote:
From: Roman Kiryanov <[email protected]>
A driver for the Goldfish Android emulator that occupies address space to use it with the memory sharing device on the QEMU side. The memory sharding device allocates subranges and populate them with actual RAM. This allows sharing host's memory with the guest.
Why isn't this in drivers/platform/goldfish/ ?
Quoted 1 line
Why isn't this in
drivers/platform/goldfish/?
I was not sure where to put, but this driver is not a platform one, it is a pci one.
Greg KH
maintainerQuoted 4 lines · Roman Kiryanov
Quoted 1 line
Why isn't this in
drivers/platform/goldfish/?I was not sure where to put, but this driver is not a platform one, it is a pci one.
You are controlling a "platform" device, the goldfish platform. What makes this different from any of the other goldfish platform drivers in that directory other than you are triggering off of a virtual pci device?
thanks,
greg k-h
Greg KH
maintainerQuoted 6 lines · On Thu, Jan 03, 2019 at 06:13:11PM -0800, [email protected] wrote:
drivers/misc/goldfish_address_space.c new file +6
-0+static int as_release (struct inode *inode,struct file *filp) +{ +struct as_allocated_blocks *allocated_blocks = filp->private_data; +struct as_device_state *state; +int blocks_size; +int i;
drivers/misc/goldfish_address_space.c line 331
Quoted 10 lines
drivers/misc/goldfish_address_space.c new file +10
-0+ +WARN_ON (!allocated_blocks); +WARN_ON (!allocated_blocks->state); +WARN_ON (!allocated_blocks->blocks); +WARN_ON (allocated_blocks->blocks_size <0 ); + + state = allocated_blocks->state; + blocks_size = allocated_blocks->blocks_size; + +if (mutex_lock_interruptible (&state->registers_lock))
I just took this chunk of code as an example of what you do all over this file. Please do not use WARN_ON() as a lazy way of saying "I have no idea how to handle this random error that might happen, so I'm going to punt to the user and crash the machine."
If these things really can happen, then properly check for them and handle the error correctly. If they can not, then just remove the WARN_ON check as it is not needed at all.
As it is, this code is obviously broken because if allocated_blocks->state is NULL, you just crashed on the line after the check. So even if you did somehow want to "warn" for something like this happening, you did not handle it and killed the machine.
All of the WARN_ON can be removed here as I bet you are testing for things that can never happen. And if it could happen, then properyl test for it.
as-is, this code is not ok at all.
Also, along these lines, who else is reviewing this code before you send it out? Surely you are not reyling on just me to do that, you are taking advantage of the huge numbers of reviewers inside your company that could have told you this before posting it, right?
Please do so.
greg k-h
Hi. Thank you for looking into my patch.
Quoted 2 lines
If they can not, then just remove the
WARN_ONcheck as it is not needed at all.
No, these things don't happen. I will remove these lines.