staging: Android: Add 'vsoc' driver for cuttlefish.
PATCH 1/1 staging: Android: Add 'vsoc' driver for cuttlefish.
From: Greg Hartman <[email protected]>
The cuttlefish system is a virtual SoC architecture based on QEMU. It uses the QEMU ivshmem feature to share memory regions between guest and host with a custom protocol.
--- drivers/staging/android/Kconfig | 9 + drivers/staging/android/Makefile | 1 + drivers/staging/android/TODO | 10 + drivers/staging/android/uapi/vsoc_shm.h | 303 ++++++ drivers/staging/android/vsoc.c | 1167 +++++++++++++++++++++++ 5 files changed, 1490 insertions(+) create mode 100644 drivers/staging/android/uapi/vsoc_shm.h create mode 100644 drivers/staging/android/vsoc.c
drivers/staging/android/Kconfig +9 -0
@@ -14,6 +14,15 @@ config ASHMEM It is, in theory, a good memory allocator for low-memory devices, because it can discard shared memory units when under memory pressure. +config ANDROID_VSOC + tristate "Android Virtual SoC support" + default n + depends on PCI_MSI + ---help--- + This option adds support for the Virtual SoC driver needed to boot + a 'cuttlefish' Android image inside QEmu. The driver interacts with + a QEmu ivshmem device. If built as a module, it will be called vsoc. + source "drivers/staging/android/ion/Kconfig" endif # if ANDROID
drivers/staging/android/Makefile +1 -0
@@ -3,3 +3,4 @@ ccflags-y += -I$(src) # needed for trace events obj-y += ion/ obj-$(CONFIG_ASHMEM) += ashmem.o +obj-$(CONFIG_ANDROID_VSOC) += vsoc.o
drivers/staging/android/TODO +10 -0
@@ -11,5 +11,15 @@ ion/ - Split /dev/ion up into multiple nodes (e.g. /dev/ion/heap0) - Better test framework (integration with VGEM was suggested) +vsoc.c, uapi/vsoc_shm.h + - The current driver uses the same wait queue for all of the futexes in a + region. This will cause false wakeups in regions with a large number of + waiting threads. We should eventually use multiple queues and select the + queue based on the region. + - Add debugfs support for examining the permissions of regions. + - Use ioremap_wc instead of ioremap_nocache. + - Remove VSOC_WAIT_FOR_INCOMING_INTERRUPT ioctl. This functionality has been + superseded by the futex and is there for legacy reasons. + Please send patches to Greg Kroah-Hartman <[email protected]> and Cc: Arve Hjønnevåg <[email protected]> and Riley Andrews <[email protected]>
drivers/staging/android/uapi/vsoc_shm.h new file +303 -0
303 lines not on this page · raw message
drivers/staging/android/vsoc.c new file +1167 -0
1,167 lines not on this page · raw message
Signature · 1 line
2.17.0.484.g0c8726318c-goog
Quoted 34 lines · Alistair Strachan
drivers/staging/android/vsoc.c new file +34
-0+static int do_create_fd_scoped_permission ( +struct vsoc_device_region *region_p, +struct fd_scoped_permission_node *np, +struct fd_scoped_permission_arg *__user arg) +{ +struct file *managed_filp; +s32 managed_fd; +atomic_t *owner_ptr =NULL ; +struct vsoc_device_region *managed_region_p; + +if (copy_from_user (&np->permission, &arg->perm,sizeof (*np)) || +copy_from_user (&managed_fd, + &arg->managed_region_fd,sizeof (managed_fd))) { +return -EFAULT ; + } + managed_filp =fdget (managed_fd).file; +/* Check that it's a valid fd, */ +if (!managed_filp ||vsoc_validate_filep (managed_filp)) +return -EPERM ; +/* EEXIST if the given fd already has a permission. */ +if (((struct vsoc_private_data *)managed_filp->private_data)-> + fd_scoped_permission_node) +return -EEXIST ; + managed_region_p =vsoc_region_from_filep (managed_filp); +/* Check that the provided region is managed by this one */ +if (&vsoc_dev.regions[managed_region_p->managed_by] != region_p) +return -EPERM ; +/* The area must be well formed and have non-zero size */ +if (np->permission.begin_offset >= np->permission.end_offset) +return -EINVAL ; +/* The area must fit in the memory window */ +if (np->permission.end_offset > +vsoc_device_region_size (managed_region_p)) +return -ERANGE ;
drivers/staging/android/vsoc.c line 303
Quoted 10 lines
drivers/staging/android/vsoc.c new file +10
-0+/* The area must be in the region data section */ +if (np->permission.begin_offset < + managed_region_p->offset_of_region_data) +return -ERANGE ; +/* The area must be page aligned */ +if (!PAGE_ALIGNED (np->permission.begin_offset) || + !PAGE_ALIGNED (np->permission.end_offset)) +return -EINVAL ; +/* The owner flag must reside in the owner memory */ + if (np->permission.owner_offset + sizeof(np->permission.owner_offset) >
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
There is a potential integer overflow here
drivers/staging/android/vsoc.c line 311
Quoted 8 lines
drivers/staging/android/vsoc.c new file +8
-0+ vsoc_device_region_size(region_p)) +return -ERANGE ; +/* The owner flag must reside in the data section */ +if (np->permission.owner_offset < region_p->offset_of_region_data) +return -EINVAL ; +/* Owner offset must be naturally aligned in the window */ +if (np->permission.owner_offset & + (sizeof (np->permission.owner_offset) -1 ))
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
but then we prevent it here so that's fine. But it would be better to reverse these checks so that it's easier to review.
Quoted 42 lines
drivers/staging/android/vsoc.c new file +42
-0+return -EINVAL ; +/* The owner value must change to claim the memory */ +if (np->permission.owned_value ==VSOC_REGION_FREE ) +return -EINVAL ; + owner_ptr = + (atomic_t *)shm_off_to_virtual_addr (region_p->region_begin_offset + + np->permission.owner_offset); +/* We've already verified that this is in the shared memory window, so +* it should be safe to write to this address. +*/ +if (atomic_cmpxchg (owner_ptr, +VSOC_REGION_FREE , + np->permission.owned_value) !=VSOC_REGION_FREE ) { +return -EBUSY ; + } + ((struct vsoc_private_data *)managed_filp->private_data)-> + fd_scoped_permission_node = np; +/* The file offset needs to be adjusted if the calling +* process did any read/write operations on the fd +* before creating the permission. +*/ +if (managed_filp->f_pos) { +if (managed_filp->f_pos > np->permission.end_offset) { +/* If the offset is beyond the permission end, set it +* to the end. +*/ + managed_filp->f_pos = np->permission.end_offset; + }else { +/* If the offset is within the permission interval +* keep it there otherwise reset it to zero. +*/ +if (managed_filp->f_pos < np->permission.begin_offset) { + managed_filp->f_pos =0 ; + }else { + managed_filp->f_pos -= + np->permission.begin_offset; + } + } + } +return 0 ; +} +
[ snip ]
Quoted 12 lines
drivers/staging/android/vsoc.c new file +12
-0+ +/** +* Implements the inner logic of cond_wait. Copies to and from userspace are +* done in the helper function below. +*/ +static int handle_vsoc_cond_wait (struct file *filp,struct vsoc_cond_wait *arg) +{ +DEFINE_WAIT (wait); +u32 region_number =iminor (file_inode (filp)); +struct vsoc_region_data *data = vsoc_dev.regions_data + region_number; +struct hrtimer_sleeper timeout, *to =NULL ; +int ret =0 ;
drivers/staging/android/vsoc.c line 422
Quoted 10 lines
drivers/staging/android/vsoc.c new file +10
-0+struct vsoc_device_region *region_p =vsoc_region_from_filep (filp); +atomic_t *address =NULL ; +struct timespec ts; + +/* Ensure that the offset is aligned */ +if (arg->offset & (sizeof (uint32_t ) -1 )) +return -EADDRNOTAVAIL ; +/* Ensure that the offset is within shared memory */ +if (((uint64_t )arg->offset) + region_p->region_begin_offset + +sizeof (uint32_t ) > region_p->region_end_offset)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
This could overflow.
Quoted 74 lines
drivers/staging/android/vsoc.c new file +74
-0+return -E2BIG ; + address =shm_off_to_virtual_addr (region_p->region_begin_offset + + arg->offset); + +/* Ensure that the type of wait is valid */ +switch (arg->wait_type) { +case VSOC_WAIT_IF_EQUAL : +break ; +case VSOC_WAIT_IF_EQUAL_TIMEOUT : + to = &timeout; +break ; +default : +return -EINVAL ; + } + +if (to) { +/* Copy the user-supplied timesec into the kernel structure. +* We do things this way to flatten differences between 32 bit +* and 64 bit timespecs. +*/ + ts.tv_sec = arg->wake_time_sec; + ts.tv_nsec = arg->wake_time_nsec; + +if (!timespec_valid (&ts)) +return -EINVAL ; +hrtimer_init_on_stack (&to->timer,CLOCK_MONOTONIC , +HRTIMER_MODE_ABS ); +hrtimer_set_expires_range_ns (&to->timer,timespec_to_ktime (ts), + current->timer_slack_ns); + +hrtimer_init_sleeper (to, current); + } + +while (1 ) { +prepare_to_wait (&data->futex_wait_queue, &wait, +TASK_INTERRUPTIBLE ); +/* +* Check the sentinel value after prepare_to_wait. If the value +* changes after this check the writer will call signal, +* changing the task state from INTERRUPTIBLE to RUNNING. That +* will ensure that schedule() will eventually schedule this +* task. +*/ +if (atomic_read (address) != arg->value) { + ret =0 ; +break ; + } +if (to) { +hrtimer_start_expires (&to->timer,HRTIMER_MODE_ABS ); +if (likely (to->task)) +freezable_schedule (); +hrtimer_cancel (&to->timer); +if (!to->task) { + ret = -ETIMEDOUT ; +break ; + } + }else { +freezable_schedule (); + } +/* Count the number of times that we woke up. This is useful +* for unit testing. +*/ + ++arg->wakes; +if (signal_pending (current)) { + ret = -EINTR ; +break ; + } + } +finish_wait (&data->futex_wait_queue, &wait); +if (to) +destroy_hrtimer_on_stack (&to->timer); +return ret; +} +
[ snip ]
Quoted 81 lines
drivers/staging/android/vsoc.c new file +81
-0+ +static int vsoc_probe_device (struct pci_dev *pdev, +const struct pci_device_id *ent) +{ +int result; +int i; +resource_size_t reg_size; +dev_t devt; + + vsoc_dev.dev = pdev; + result =pci_enable_device (pdev); +if (result) { +dev_err (&pdev->dev, +"pci_enable_device failed %s: error %d\n" , +pci_name (pdev), result); +return result; + } + vsoc_dev.enabled_device =1 ; + result =pci_request_regions (pdev,"vsoc" ); +if (result <0 ) { +dev_err (&pdev->dev,"pci_request_regions failed\n" ); +vsoc_remove_device (pdev); +return -EBUSY ; + } + vsoc_dev.requested_regions =1 ; +/* Set up the control registers in BAR 0 */ + reg_size =pci_resource_len (pdev,REGISTER_BAR ); +if (reg_size >MAX_REGISTER_BAR_LEN ) + vsoc_dev.regs = +pci_iomap (pdev,REGISTER_BAR ,MAX_REGISTER_BAR_LEN ); +else + vsoc_dev.regs =pci_iomap (pdev,REGISTER_BAR , reg_size); + +if (!vsoc_dev.regs) { +dev_err (&pdev->dev, +"cannot ioremap registers of size %zu\n" , + (size_t )reg_size); +vsoc_remove_device (pdev); +return -EBUSY ; + } + +/* Map the shared memory in BAR 2 */ + vsoc_dev.shm_phys_start =pci_resource_start (pdev,SHARED_MEMORY_BAR ); + vsoc_dev.shm_size =pci_resource_len (pdev,SHARED_MEMORY_BAR ); + +dev_info (&pdev->dev,"shared memory @ DMA %p size=0x%zx\n" , + (void *)vsoc_dev.shm_phys_start, vsoc_dev.shm_size); +/* TODO(ghartman): ioremap_wc should work here */ + vsoc_dev.kernel_mapped_shm =ioremap_nocache ( + vsoc_dev.shm_phys_start, vsoc_dev.shm_size); +if (!vsoc_dev.kernel_mapped_shm) { +dev_err (&vsoc_dev.dev->dev,"cannot iomap region\n" ); +vsoc_remove_device (pdev); +return -EBUSY ; + } + + vsoc_dev.layout = + (struct vsoc_shm_layout_descriptor *)vsoc_dev.kernel_mapped_shm; +dev_info (&pdev->dev,"major_version: %d\n" , + vsoc_dev.layout->major_version); +dev_info (&pdev->dev,"minor_version: %d\n" , + vsoc_dev.layout->minor_version); +dev_info (&pdev->dev,"size: 0x%x\n" , vsoc_dev.layout->size); +dev_info (&pdev->dev,"regions: %d\n" , vsoc_dev.layout->region_count); +if (vsoc_dev.layout->major_version != +CURRENT_VSOC_LAYOUT_MAJOR_VERSION ) { +dev_err (&vsoc_dev.dev->dev, +"driver supports only major_version %d\n" , +CURRENT_VSOC_LAYOUT_MAJOR_VERSION ); +vsoc_remove_device (pdev); +return -EBUSY ; + } + result =alloc_chrdev_region (&devt,0 , vsoc_dev.layout->region_count, +VSOC_DEV_NAME ); +if (result) { +dev_err (&vsoc_dev.dev->dev,"alloc_chrdev_region failed\n" ); +vsoc_remove_device (pdev); +return -EBUSY ; + } + vsoc_dev.major =MAJOR (devt); +cdev_init (&vsoc_dev.cdev, &vsoc_ops);
drivers/staging/android/vsoc.c line 847
Quoted 10 lines
drivers/staging/android/vsoc.c new file +10
-0+ vsoc_dev.cdev.owner =THIS_MODULE ; + result =cdev_add (&vsoc_dev.cdev, devt, vsoc_dev.layout->region_count); +if (result) { +dev_err (&vsoc_dev.dev->dev,"cdev_add error\n" ); +vsoc_remove_device (pdev); +return -EBUSY ; + } + vsoc_dev.cdev_added =1 ; + vsoc_dev.class =class_create (THIS_MODULE ,VSOC_DEV_NAME ); +if (!vsoc_dev.class) {
^^^^^^^^^^^^^^^
This should be if (IS_ERR(vsoc_dev.class)) {. The test in vsoc_remove_device() needs to be updated as well.
Quoted 164 lines
drivers/staging/android/vsoc.c new file +164
-0+dev_err (&vsoc_dev.dev->dev,"class_create failed\n" ); +vsoc_remove_device (pdev); +return -EBUSY ; + } + vsoc_dev.regions = (struct vsoc_device_region *) + (vsoc_dev.kernel_mapped_shm + + vsoc_dev.layout->vsoc_region_desc_offset); + vsoc_dev.msix_entries =kcalloc ( + vsoc_dev.layout->region_count, +sizeof (vsoc_dev.msix_entries[0 ]),GFP_KERNEL ); +if (!vsoc_dev.msix_entries) { +dev_err (&vsoc_dev.dev->dev, +"unable to allocate msix_entries\n" ); +vsoc_remove_device (pdev); +return -ENOSPC ; + } + vsoc_dev.regions_data =kcalloc ( + vsoc_dev.layout->region_count, +sizeof (vsoc_dev.regions_data[0 ]),GFP_KERNEL ); +if (!vsoc_dev.regions_data) { +dev_err (&vsoc_dev.dev->dev, +"unable to allocate regions' data\n" ); +vsoc_remove_device (pdev); +return -ENOSPC ; + } +for (i =0 ; i < vsoc_dev.layout->region_count; ++i) + vsoc_dev.msix_entries[i].entry = i; + + result =pci_enable_msix_exact (vsoc_dev.dev, vsoc_dev.msix_entries, + vsoc_dev.layout->region_count); +if (result) { +dev_info (&pdev->dev,"pci_enable_msix failed: %d\n" , result); +vsoc_remove_device (pdev); +return -ENOSPC ; + } +/* Check that all regions are well formed */ +for (i =0 ; i < vsoc_dev.layout->region_count; ++i) { +const struct vsoc_device_region *region = vsoc_dev.regions + i; + +if (!PAGE_ALIGNED (region->region_begin_offset) || + !PAGE_ALIGNED (region->region_end_offset)) { +dev_err (&vsoc_dev.dev->dev, +"region %d not aligned (%x:%x)" , i, + region->region_begin_offset, + region->region_end_offset); +vsoc_remove_device (pdev); +return -EFAULT ; + } +if (region->region_begin_offset >= region->region_end_offset || + region->region_end_offset > vsoc_dev.shm_size) { +dev_err (&vsoc_dev.dev->dev, +"region %d offsets are wrong: %x %x %zx" , + i, region->region_begin_offset, + region->region_end_offset, vsoc_dev.shm_size); +vsoc_remove_device (pdev); +return -EFAULT ; + } +if (region->managed_by >= vsoc_dev.layout->region_count) { +dev_err (&vsoc_dev.dev->dev, +"region %d has invalid owner: %u" , + i, region->managed_by); +vsoc_remove_device (pdev); +return -EFAULT ; + } + } + vsoc_dev.msix_enabled =1 ; +for (i =0 ; i < vsoc_dev.layout->region_count; ++i) { +const struct vsoc_device_region *region = vsoc_dev.regions + i; +size_t name_sz =sizeof (vsoc_dev.regions_data[i].name) -1 ; +const struct vsoc_signal_table_layout *h_to_g_signal_table = + ®ion->host_to_guest_signal_table; +const struct vsoc_signal_table_layout *g_to_h_signal_table = + ®ion->guest_to_host_signal_table; + + vsoc_dev.regions_data[i].name[name_sz] ='\0' ; +memcpy (vsoc_dev.regions_data[i].name, region->device_name, + name_sz); +dev_info (&pdev->dev,"region %d name=%s\n" , + i, vsoc_dev.regions_data[i].name); +init_waitqueue_head ( + &vsoc_dev.regions_data[i].interrupt_wait_queue); +init_waitqueue_head (&vsoc_dev.regions_data[i].futex_wait_queue); + vsoc_dev.regions_data[i].incoming_signalled = + vsoc_dev.kernel_mapped_shm + + region->region_begin_offset + + h_to_g_signal_table->interrupt_signalled_offset; + vsoc_dev.regions_data[i].outgoing_signalled = + vsoc_dev.kernel_mapped_shm + + region->region_begin_offset + + g_to_h_signal_table->interrupt_signalled_offset; + + result =request_irq ( + vsoc_dev.msix_entries[i].vector, + vsoc_interrupt,0 , + vsoc_dev.regions_data[i].name, + vsoc_dev.regions_data + i); +if (result) { +dev_info (&pdev->dev, +"request_irq failed irq=%d vector=%d\n" , + i, vsoc_dev.msix_entries[i].vector); +vsoc_remove_device (pdev); +return -ENOSPC ; + } + vsoc_dev.regions_data[i].irq_requested =1 ; +if (!device_create (vsoc_dev.class,NULL , +MKDEV (vsoc_dev.major, i), +NULL , vsoc_dev.regions_data[i].name)) { +dev_err (&vsoc_dev.dev->dev,"device_create failed\n" ); +vsoc_remove_device (pdev); +return -EBUSY ; + } + vsoc_dev.regions_data[i].device_created =1 ; + } +return 0 ; +} + +/* +* This should undo all of the allocations in the probe function in reverse +* order. +* +* Notes: +* +* The device may have been partially initialized, so double check +* that the allocations happened. +* +* This function may be called multiple times, so mark resources as freed +* as they are deallocated. +*/ +static void vsoc_remove_device (struct pci_dev *pdev) +{ +int i; +/* +* pdev is the first thing to be set on probe and the last thing +* to be cleared here. If it's NULL then there is no cleanup. +*/ +if (!pdev || !vsoc_dev.dev) +return ; +dev_info (&pdev->dev,"remove_device\n" ); +if (vsoc_dev.regions_data) { +for (i =0 ; i < vsoc_dev.layout->region_count; ++i) { +if (vsoc_dev.regions_data[i].device_created) { +device_destroy (vsoc_dev.class, +MKDEV (vsoc_dev.major, i)); + vsoc_dev.regions_data[i].device_created =0 ; + } +if (vsoc_dev.regions_data[i].irq_requested) +free_irq (vsoc_dev.msix_entries[i].vector,NULL ); + vsoc_dev.regions_data[i].irq_requested =0 ; + } +kfree (vsoc_dev.regions_data); + vsoc_dev.regions_data =0 ; + } +if (vsoc_dev.msix_enabled) { +pci_disable_msix (pdev); + vsoc_dev.msix_enabled =0 ; + } +kfree (vsoc_dev.msix_entries); + vsoc_dev.msix_entries =0 ; + vsoc_dev.regions =0 ; +if (vsoc_dev.class) { +class_destroy (vsoc_dev.class); + vsoc_dev.class =0 ; + } +if (vsoc_dev.cdev_added) {
drivers/staging/android/vsoc.c line 1021
Quoted 10 lines
drivers/staging/android/vsoc.c new file +10
-0+cdev_del (&vsoc_dev.cdev); + vsoc_dev.cdev_added =0 ; + } +if (vsoc_dev.major && vsoc_dev.layout) { +unregister_chrdev_region (MKDEV (vsoc_dev.major,0 ), + vsoc_dev.layout->region_count); + vsoc_dev.major =0 ; + } + vsoc_dev.layout =0 ; +if (vsoc_dev.kernel_mapped_shm && pdev) {
^^^^
These tests can be removed since we checked at the start of the function.
drivers/staging/android/vsoc.c line 1025
Quoted 4 lines
drivers/staging/android/vsoc.c new file +4
-0+pci_iounmap (pdev, vsoc_dev.kernel_mapped_shm); + vsoc_dev.kernel_mapped_shm =0 ; + } +if (vsoc_dev.regs && pdev) {
^^^^
drivers/staging/android/vsoc.c line 1029
Quoted 4 lines
drivers/staging/android/vsoc.c new file +4
-0+pci_iounmap (pdev, vsoc_dev.regs); + vsoc_dev.regs =0 ; + } +if (vsoc_dev.requested_regions && pdev) {
^^^^
drivers/staging/android/vsoc.c line 1033
Quoted 4 lines
drivers/staging/android/vsoc.c new file +4
-0+pci_release_regions (pdev); + vsoc_dev.requested_regions =0 ; + } +if (vsoc_dev.enabled_device && pdev) {
^^^^
drivers/staging/android/vsoc.c line 1040
Quoted 7 lines
drivers/staging/android/vsoc.c new file +7
-0+pci_disable_device (pdev); + vsoc_dev.enabled_device =0 ; + } +/* Do this last: it indicates that the device is not initialized. */ + vsoc_dev.dev =NULL ; +} +
regards,
dan carpenter
_______________________________________________
devel mailing list
[email protected]
http://driverdev.linuxdriverproject.org/mailman/listinfo/driverdev-devel
drivers/staging/android/vsoc.c line 422
Quoted 3 lines
drivers/staging/android/vsoc.c new file +3
-0+/* Ensure that the offset is within shared memory */ +if (((uint64_t )arg->offset) + region_p->region_begin_offset + +sizeof (uint32_t ) > region_p->region_end_offset)
I'm not certain if I understand the overflow.
arg->offset, region_begin_offset, and region_end_offset are all uint32.
arg->offset is uint32, and is cast to uint_64_t. There is no sign extension, so we known that the result is [0, 2^32)
adding region_begin_offset (also uint32) makes the result [0, 2^33) adding sizeof(uint32_t) is unlikely to push the result above [0, 2^34)
region_end_offset is also uint32, but I'm fairly confident that the comparison is well-formed.
Let me know if my reasoning is wrong on any of the above issues.
Either way, do you have suggestions for how to make this more idiomatic?
Quoted 548 lines · Dan Carpenter
Quoted 35 lines · Alistair Strachan
drivers/staging/android/vsoc.c new file +34
-0+static int do_create_fd_scoped_permission ( +struct vsoc_device_region *region_p, +struct fd_scoped_permission_node *np, +struct fd_scoped_permission_arg *__user arg) +{ +struct file *managed_filp; +s32 managed_fd; +atomic_t *owner_ptr =NULL ; +struct vsoc_device_region *managed_region_p; + +if (copy_from_user (&np->permission, &arg->perm,sizeof (*np)) || +copy_from_user (&managed_fd, + &arg->managed_region_fd,sizeof (managed_fd))) { +return -EFAULT ; + } + managed_filp =fdget (managed_fd).file; +/* Check that it's a valid fd, */ +if (!managed_filp ||vsoc_validate_filep (managed_filp)) +return -EPERM ; +/* EEXIST if the given fd already has a permission. */ +if (((struct vsoc_private_data *)managed_filp->private_data)-> + fd_scoped_permission_node) +return -EEXIST ; + managed_region_p =vsoc_region_from_filep (managed_filp); +/* Check that the provided region is managed by this one */ +if (&vsoc_dev.regions[managed_region_p->managed_by] != region_p) +return -EPERM ; +/* The area must be well formed and have non-zero size */ +if (np->permission.begin_offset >= np->permission.end_offset) +return -EINVAL ; +/* The area must fit in the memory window */ +if (np->permission.end_offset > +vsoc_device_region_size (managed_region_p)) +return -ERANGE ;drivers/staging/android/vsoc.c
Quoted 10 lines
drivers/staging/android/vsoc.c new file +10
-0+/* The area must be in the region data section */ +if (np->permission.begin_offset < + managed_region_p->offset_of_region_data) +return -ERANGE ; +/* The area must be page aligned */ +if (!PAGE_ALIGNED (np->permission.begin_offset) || + !PAGE_ALIGNED (np->permission.end_offset)) +return -EINVAL ; +/* The owner flag must reside in the owner memory */ + if (np->permission.owner_offset + sizeof(np->permission.owner_offset)^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
There is a potential integer overflow heredrivers/staging/android/vsoc.c line 311
Quoted 8 lines
drivers/staging/android/vsoc.c new file +8
-0+ vsoc_device_region_size(region_p)) +return -ERANGE ; +/* The owner flag must reside in the data section */ +if (np->permission.owner_offset < region_p->offset_of_region_data) +return -EINVAL ; +/* Owner offset must be naturally aligned in the window */ +if (np->permission.owner_offset & + (sizeof (np->permission.owner_offset) -1 ))^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^but then we prevent it here so that's fine. But it would be better to reverse these checks so that it's easier to review.
drivers/staging/android/vsoc.c
Quoted 6 lines
drivers/staging/android/vsoc.c new file +6
-0+return -EINVAL ; +/* The owner value must change to claim the memory */ +if (np->permission.owned_value ==VSOC_REGION_FREE ) +return -EINVAL ; + owner_ptr = + (atomic_t *)shm_off_to_virtual_addr(region_p->region_begin_offset+
Quoted 1 line
+
np->permission.owner_offset);Quoted 1 line
+ /* We've already verified that this is in the shared memory
window, so
drivers/staging/android/vsoc.c
Quoted 5 lines
drivers/staging/android/vsoc.c new file +5
-0+* it should be safe to write to this address. +*/ + if (atomic_cmpxchg(owner_ptr, + VSOC_REGION_FREE, + np->permission.owned_value) !=
VSOC_REGION_FREE) {drivers/staging/android/vsoc.c
Quoted 11 lines
drivers/staging/android/vsoc.c new file +11
-0+return -EBUSY ; + } + ((struct vsoc_private_data *)managed_filp->private_data)-> + fd_scoped_permission_node = np; +/* The file offset needs to be adjusted if the calling +* process did any read/write operations on the fd +* before creating the permission. +*/ +if (managed_filp->f_pos) { +if (managed_filp->f_pos > np->permission.end_offset) { +/* If the offset is beyond the permission end, set it
drivers/staging/android/vsoc.c
Quoted 8 lines
drivers/staging/android/vsoc.c new file +8
-0+* to the end. +*/ + managed_filp->f_pos = np->permission.end_offset; + }else { +/* If the offset is within the permission interval +* keep it there otherwise reset it to zero. +*/ + if (managed_filp->f_pos <
np->permission.begin_offset) {Quoted 10 lines
drivers/staging/android/vsoc.c new file +10
-0+ managed_filp->f_pos =0 ; + }else { + managed_filp->f_pos -= + np->permission.begin_offset; + } + } + } +return 0 ; +} +[ snip ]
drivers/staging/android/uapi/vsoc_shm.h
Quoted 3 lines
drivers/staging/android/uapi/vsoc_shm.h new file +3
-0+ +/** +* Implements the inner logic of cond_wait. Copies to and from userspace are
Quoted 3 lines
+ * done in the helper function below.
+ */
+static int handle_vsoc_cond_wait(struct file *filp, struct
vsoc_cond_wait*arg)drivers/staging/android/vsoc.c
Quoted 4 lines
drivers/staging/android/vsoc.c new file +4
-0+{ +DEFINE_WAIT (wait); +u32 region_number =iminor (file_inode (filp)); + struct vsoc_region_data *data = vsoc_dev.regions_data +
region_number;drivers/staging/android/vsoc.c line 422
Quoted 12 lines
drivers/staging/android/vsoc.c new file +12
-0+struct hrtimer_sleeper timeout, *to =NULL ; +int ret =0 ; +struct vsoc_device_region *region_p =vsoc_region_from_filep (filp); +atomic_t *address =NULL ; +struct timespec ts; + +/* Ensure that the offset is aligned */ +if (arg->offset & (sizeof (uint32_t ) -1 )) +return -EADDRNOTAVAIL ; +/* Ensure that the offset is within shared memory */ +if (((uint64_t )arg->offset) + region_p->region_begin_offset + +sizeof (uint32_t ) > region_p->region_end_offset)^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^This could overflow.
Quoted 7 lines
drivers/staging/android/vsoc.c new file +7
-0+return -E2BIG ; + address =shm_off_to_virtual_addr (region_p->region_begin_offset + + arg->offset); + +/* Ensure that the type of wait is valid */ +switch (arg->wait_type) { +case VSOC_WAIT_IF_EQUAL :drivers/staging/android/vsoc.c
Quoted 10 lines
drivers/staging/android/vsoc.c new file +10
-0+break ; +case VSOC_WAIT_IF_EQUAL_TIMEOUT : + to = &timeout; +break ; +default : +return -EINVAL ; + } + +if (to) { +/* Copy the user-supplied timesec into the kernel structure.
Quoted 1 line
+ * We do things this way to flatten differences between 32
bit
drivers/staging/android/vsoc.c
Quoted 10 lines
drivers/staging/android/vsoc.c new file +10
-0+* and 64 bit timespecs. +*/ + ts.tv_sec = arg->wake_time_sec; + ts.tv_nsec = arg->wake_time_nsec; + +if (!timespec_valid (&ts)) +return -EINVAL ; +hrtimer_init_on_stack (&to->timer,CLOCK_MONOTONIC , +HRTIMER_MODE_ABS ); + hrtimer_set_expires_range_ns(&to->timer,
timespec_to_ktime(ts),drivers/staging/android/vsoc.c
Quoted 10 lines
drivers/staging/android/vsoc.c new file +10
-0+ current->timer_slack_ns); + +hrtimer_init_sleeper (to, current); + } + +while (1 ) { +prepare_to_wait (&data->futex_wait_queue, &wait, +TASK_INTERRUPTIBLE ); +/* +* Check the sentinel value after prepare_to_wait. If the value
Review
Quoted 2 lines
(quoted diff) +2
-0+ * changes after this check the writer will call signal, + * changing the task state from INTERRUPTIBLE to RUNNING.That
Quoted 1 line
+ * will ensure that
schedule()will eventually schedulethis
drivers/staging/android/vsoc.c
Quoted 8 lines
drivers/staging/android/vsoc.c new file +8
-0+* task. +*/ +if (atomic_read (address) != arg->value) { + ret =0 ; +break ; + } +if (to) { + hrtimer_start_expires(&to->timer,
HRTIMER_MODE_ABS);drivers/staging/android/vsoc.c
Quoted 11 lines
drivers/staging/android/vsoc.c new file +11
-0+if (likely (to->task)) +freezable_schedule (); +hrtimer_cancel (&to->timer); +if (!to->task) { + ret = -ETIMEDOUT ; +break ; + } + }else { +freezable_schedule (); + } +/* Count the number of times that we woke up. This is useful
Quoted 14 lines
drivers/staging/android/vsoc.c new file +14
-0+* for unit testing. +*/ + ++arg->wakes; +if (signal_pending (current)) { + ret = -EINTR ; +break ; + } + } +finish_wait (&data->futex_wait_queue, &wait); +if (to) +destroy_hrtimer_on_stack (&to->timer); +return ret; +} +[ snip ]
Quoted 33 lines
drivers/staging/android/vsoc.c new file +33
-0+ +static int vsoc_probe_device (struct pci_dev *pdev, +const struct pci_device_id *ent) +{ +int result; +int i; +resource_size_t reg_size; +dev_t devt; + + vsoc_dev.dev = pdev; + result =pci_enable_device (pdev); +if (result) { +dev_err (&pdev->dev, +"pci_enable_device failed %s: error %d\n" , +pci_name (pdev), result); +return result; + } + vsoc_dev.enabled_device =1 ; + result =pci_request_regions (pdev,"vsoc" ); +if (result <0 ) { +dev_err (&pdev->dev,"pci_request_regions failed\n" ); +vsoc_remove_device (pdev); +return -EBUSY ; + } + vsoc_dev.requested_regions =1 ; +/* Set up the control registers in BAR 0 */ + reg_size =pci_resource_len (pdev,REGISTER_BAR ); +if (reg_size >MAX_REGISTER_BAR_LEN ) + vsoc_dev.regs = +pci_iomap (pdev,REGISTER_BAR ,MAX_REGISTER_BAR_LEN ); +else + vsoc_dev.regs =pci_iomap (pdev,REGISTER_BAR , reg_size); +drivers/staging/android/vsoc.c
Quoted 10 lines
drivers/staging/android/vsoc.c new file +10
-0+if (!vsoc_dev.regs) { +dev_err (&pdev->dev, +"cannot ioremap registers of size %zu\n" , + (size_t )reg_size); +vsoc_remove_device (pdev); +return -EBUSY ; + } + +/* Map the shared memory in BAR 2 */ + vsoc_dev.shm_phys_start = pci_resource_start(pdev,
SHARED_MEMORY_BAR);Quoted 5 lines
drivers/staging/android/vsoc.c new file +5
-0+ vsoc_dev.shm_size =pci_resource_len (pdev,SHARED_MEMORY_BAR ); + +dev_info (&pdev->dev,"shared memory @ DMA %p size=0x%zx\n" , + (void *)vsoc_dev.shm_phys_start, vsoc_dev.shm_size); +/* TODO(ghartman): ioremap_wc should work here */ drivers/staging/android/vsoc.c
Quoted 10 lines
drivers/staging/android/vsoc.c new file +10
-0+ vsoc_dev.kernel_mapped_shm =ioremap_nocache ( + vsoc_dev.shm_phys_start, vsoc_dev.shm_size); +if (!vsoc_dev.kernel_mapped_shm) { +dev_err (&vsoc_dev.dev->dev,"cannot iomap region\n" ); +vsoc_remove_device (pdev); +return -EBUSY ; + } + + vsoc_dev.layout = + (struct vsoc_shm_layout_descriptor*)vsoc_dev.kernel_mapped_shm;
drivers/staging/android/vsoc.c
Quoted 6 lines
drivers/staging/android/vsoc.c new file +6
-0+dev_info (&pdev->dev,"major_version: %d\n" , + vsoc_dev.layout->major_version); +dev_info (&pdev->dev,"minor_version: %d\n" , + vsoc_dev.layout->minor_version); +dev_info (&pdev->dev,"size: 0x%x\n" , vsoc_dev.layout->size); + dev_info(&pdev->dev, "regions: %d\n",
vsoc_dev.layout->region_count);drivers/staging/android/vsoc.c
Quoted 9 lines
drivers/staging/android/vsoc.c new file +9
-0+if (vsoc_dev.layout->major_version != +CURRENT_VSOC_LAYOUT_MAJOR_VERSION ) { +dev_err (&vsoc_dev.dev->dev, +"driver supports only major_version %d\n" , +CURRENT_VSOC_LAYOUT_MAJOR_VERSION ); +vsoc_remove_device (pdev); +return -EBUSY ; + } + result = alloc_chrdev_region(&devt, 0,
vsoc_dev.layout->region_count,drivers/staging/android/vsoc.c
Quoted 3 lines
drivers/staging/android/vsoc.c new file +3
-0+ VSOC_DEV_NAME); +if (result) { + dev_err(&vsoc_dev.dev->dev, "alloc_chrdev_regionfailed\n");
drivers/staging/android/vsoc.c
Quoted 7 lines
drivers/staging/android/vsoc.c new file +7
-0+vsoc_remove_device (pdev); +return -EBUSY ; + } + vsoc_dev.major =MAJOR (devt); +cdev_init (&vsoc_dev.cdev, &vsoc_ops); + vsoc_dev.cdev.owner =THIS_MODULE ; + result = cdev_add(&vsoc_dev.cdev, devt,
vsoc_dev.layout->region_count);drivers/staging/android/vsoc.c line 847
Quoted 8 lines
drivers/staging/android/vsoc.c new file +8
-0+if (result) { +dev_err (&vsoc_dev.dev->dev,"cdev_add error\n" ); +vsoc_remove_device (pdev); +return -EBUSY ; + } + vsoc_dev.cdev_added =1 ; + vsoc_dev.class =class_create (THIS_MODULE ,VSOC_DEV_NAME ); +if (!vsoc_dev.class) {^^^^^^^^^^^^^^^This should be if (
IS_ERR(vsoc_dev.class)) {. The test invsoc_remove_device()needs to be updated as well.Quoted 19 lines
drivers/staging/android/vsoc.c new file +19
-0+dev_err (&vsoc_dev.dev->dev,"class_create failed\n" ); +vsoc_remove_device (pdev); +return -EBUSY ; + } + vsoc_dev.regions = (struct vsoc_device_region *) + (vsoc_dev.kernel_mapped_shm + + vsoc_dev.layout->vsoc_region_desc_offset); + vsoc_dev.msix_entries =kcalloc ( + vsoc_dev.layout->region_count, +sizeof (vsoc_dev.msix_entries[0 ]),GFP_KERNEL ); +if (!vsoc_dev.msix_entries) { +dev_err (&vsoc_dev.dev->dev, +"unable to allocate msix_entries\n" ); +vsoc_remove_device (pdev); +return -ENOSPC ; + } + vsoc_dev.regions_data =kcalloc ( + vsoc_dev.layout->region_count, +sizeof (vsoc_dev.regions_data[0 ]),GFP_KERNEL );drivers/staging/android/vsoc.c
Quoted 10 lines
drivers/staging/android/vsoc.c new file +10
-0+if (!vsoc_dev.regions_data) { +dev_err (&vsoc_dev.dev->dev, +"unable to allocate regions' data\n" ); +vsoc_remove_device (pdev); +return -ENOSPC ; + } +for (i =0 ; i < vsoc_dev.layout->region_count; ++i) + vsoc_dev.msix_entries[i].entry = i; + + result = pci_enable_msix_exact(vsoc_dev.dev,vsoc_dev.msix_entries,
drivers/staging/android/vsoc.c
Quoted 3 lines
drivers/staging/android/vsoc.c new file +3
-0+ vsoc_dev.layout->region_count); +if (result) { + dev_info(&pdev->dev, "pci_enable_msix failed: %d\n",result);
drivers/staging/android/vsoc.c
Quoted 6 lines
drivers/staging/android/vsoc.c new file +6
-0+vsoc_remove_device (pdev); +return -ENOSPC ; + } +/* Check that all regions are well formed */ +for (i =0 ; i < vsoc_dev.layout->region_count; ++i) { +const struct vsoc_device_region *region = vsoc_dev.regions+ i;
drivers/staging/android/vsoc.c
Quoted 11 lines
drivers/staging/android/vsoc.c new file +11
-0+ +if (!PAGE_ALIGNED (region->region_begin_offset) || + !PAGE_ALIGNED (region->region_end_offset)) { +dev_err (&vsoc_dev.dev->dev, +"region %d not aligned (%x:%x)" , i, + region->region_begin_offset, + region->region_end_offset); +vsoc_remove_device (pdev); +return -EFAULT ; + } + if (region->region_begin_offset >=
region->region_end_offset||drivers/staging/android/vsoc.c
Quoted 5 lines
drivers/staging/android/vsoc.c new file +5
-0+ region->region_end_offset > vsoc_dev.shm_size) { + dev_err(&vsoc_dev.dev->dev, + "region %d offsets are wrong: %x %x %zx", + i, region->region_begin_offset, + region->region_end_offset,vsoc_dev.shm_size);
Quoted 4 lines
drivers/staging/android/vsoc.c new file +4
-0+vsoc_remove_device (pdev); +return -EFAULT ; + } +if (region->managed_by >= vsoc_dev.layout->region_count) {drivers/staging/android/vsoc.c
Quoted 10 lines
drivers/staging/android/vsoc.c new file +10
-0+dev_err (&vsoc_dev.dev->dev, +"region %d has invalid owner: %u" , + i, region->managed_by); +vsoc_remove_device (pdev); +return -EFAULT ; + } + } + vsoc_dev.msix_enabled =1 ; +for (i =0 ; i < vsoc_dev.layout->region_count; ++i) { +const struct vsoc_device_region *region = vsoc_dev.regions+ i;
Quoted 1 line
+
size_tname_sz=sizeof(vsoc_dev.regions_data[i].name)-1;
Quoted 1 line
+ const struct
vsoc_signal_table_layout*h_to_g_signal_table=
Review
Quoted 2 lines
(quoted diff) +2
-0+ ®ion->host_to_guest_signal_table; + const struct vsoc_signal_table_layout *g_to_h_signal_table=
drivers/staging/android/vsoc.c
Quoted 9 lines
drivers/staging/android/vsoc.c new file +9
-0+ ®ion->guest_to_host_signal_table; + + vsoc_dev.regions_data[i].name[name_sz] ='\0' ; +memcpy (vsoc_dev.regions_data[i].name, region->device_name, + name_sz); +dev_info (&pdev->dev,"region %d name=%s\n" , + i, vsoc_dev.regions_data[i].name); +init_waitqueue_head ( + &vsoc_dev.regions_data[i].
interrupt_wait_queue);Quoted 1 line
+ init_waitqueue_head(&vsoc_dev.regions_data[i].futex_wait_
queue);
Quoted 16 lines
drivers/staging/android/vsoc.c new file +16
-0+ vsoc_dev.regions_data[i].incoming_signalled = + vsoc_dev.kernel_mapped_shm + + region->region_begin_offset + + h_to_g_signal_table->interrupt_signalled_offset; + vsoc_dev.regions_data[i].outgoing_signalled = + vsoc_dev.kernel_mapped_shm + + region->region_begin_offset + + g_to_h_signal_table->interrupt_signalled_offset; + + result =request_irq ( + vsoc_dev.msix_entries[i].vector, + vsoc_interrupt,0 , + vsoc_dev.regions_data[i].name, + vsoc_dev.regions_data + i); +if (result) { + dev_info(&pdev->dev,drivers/staging/android/vsoc.c
Quoted 10 lines
drivers/staging/android/vsoc.c new file +10
-0+"request_irq failed irq=%d vector=%d\n" , + i, vsoc_dev.msix_entries[i].vector); +vsoc_remove_device (pdev); +return -ENOSPC ; + } + vsoc_dev.regions_data[i].irq_requested =1 ; +if (!device_create (vsoc_dev.class,NULL , +MKDEV (vsoc_dev.major, i), +NULL , vsoc_dev.regions_data[i].name)) { + dev_err(&vsoc_dev.dev->dev, "device_createfailed\n");
drivers/staging/android/vsoc.c
Quoted 10 lines
drivers/staging/android/vsoc.c new file +10
-0+vsoc_remove_device (pdev); +return -EBUSY ; + } + vsoc_dev.regions_data[i].device_created =1 ; + } +return 0 ; +} + +/* +* This should undo all of the allocations in the probe function in reverse
Quoted 8 lines
+ * order.
+ *
+ * Notes:
+ *
+ * The device may have been partially initialized, so double check
+ * that the allocations happened.
+ *
+ * This function may be called multiple times, so mark resources asfreed
Quoted 8 lines
+ * as they are deallocated.
+ */drivers/staging/android/vsoc.c new file +6
-0+static void vsoc_remove_device (struct pci_dev *pdev) +{ +int i; +/* +* pdev is the first thing to be set on probe and the last thing +* to be cleared here. If it's NULL then there is no cleanup. drivers/staging/android/vsoc.c
Quoted 10 lines
drivers/staging/android/vsoc.c new file +10
-0+*/ +if (!pdev || !vsoc_dev.dev) +return ; +dev_info (&pdev->dev,"remove_device\n" ); +if (vsoc_dev.regions_data) { +for (i =0 ; i < vsoc_dev.layout->region_count; ++i) { +if (vsoc_dev.regions_data[i].device_created) { +device_destroy (vsoc_dev.class, +MKDEV (vsoc_dev.major, i)); + vsoc_dev.regions_data[i].device_created =0;
drivers/staging/android/vsoc.c
Quoted 3 lines
drivers/staging/android/vsoc.c new file +3
-0+ } +if (vsoc_dev.regions_data[i].irq_requested) + free_irq(vsoc_dev.msix_entries[i].vector,NULL);
Quoted 17 lines
drivers/staging/android/vsoc.c new file +17
-0+ vsoc_dev.regions_data[i].irq_requested =0 ; + } +kfree (vsoc_dev.regions_data); + vsoc_dev.regions_data =0 ; + } +if (vsoc_dev.msix_enabled) { +pci_disable_msix (pdev); + vsoc_dev.msix_enabled =0 ; + } +kfree (vsoc_dev.msix_entries); + vsoc_dev.msix_entries =0 ; + vsoc_dev.regions =0 ; +if (vsoc_dev.class) { +class_destroy (vsoc_dev.class); + vsoc_dev.class =0 ; + } +if (vsoc_dev.cdev_added) {drivers/staging/android/vsoc.c line 1021
Quoted 10 lines
drivers/staging/android/vsoc.c new file +10
-0+cdev_del (&vsoc_dev.cdev); + vsoc_dev.cdev_added =0 ; + } +if (vsoc_dev.major && vsoc_dev.layout) { +unregister_chrdev_region (MKDEV (vsoc_dev.major,0 ), + vsoc_dev.layout->region_count); + vsoc_dev.major =0 ; + } + vsoc_dev.layout =0 ; +if (vsoc_dev.kernel_mapped_shm && pdev) {^^^^These tests can be removed since we checked at the start of the function.
drivers/staging/android/vsoc.c line 1025
Quoted 4 lines
drivers/staging/android/vsoc.c new file +4
-0+pci_iounmap (pdev, vsoc_dev.kernel_mapped_shm); + vsoc_dev.kernel_mapped_shm =0 ; + } +if (vsoc_dev.regs && pdev) {^^^^drivers/staging/android/vsoc.c line 1029
Quoted 4 lines
drivers/staging/android/vsoc.c new file +4
-0+pci_iounmap (pdev, vsoc_dev.regs); + vsoc_dev.regs =0 ; + } +if (vsoc_dev.requested_regions && pdev) {^^^^drivers/staging/android/vsoc.c line 1033
Quoted 4 lines
drivers/staging/android/vsoc.c new file +4
-0+pci_release_regions (pdev); + vsoc_dev.requested_regions =0 ; + } +if (vsoc_dev.enabled_device && pdev) {^^^^drivers/staging/android/vsoc.c
Quoted 4 lines
drivers/staging/android/vsoc.c new file +4
-0+pci_disable_device (pdev); + vsoc_dev.enabled_device =0 ; + } +/* Do this last: it indicates that the device is not initialized. */
drivers/staging/android/vsoc.c line 1040
Quoted 3 lines
drivers/staging/android/vsoc.c new file +3
-0+ vsoc_dev.dev =NULL ; +} +regards,
dan carpenter
Quoted 8 lines · Greg Hartman
drivers/staging/android/vsoc.c line 422
Quoted 3 lines
drivers/staging/android/vsoc.c new file +3
-0+/* Ensure that the offset is within shared memory */ +if (((uint64_t )arg->offset) + region_p->region_begin_offset + +sizeof (uint32_t ) > region_p->region_end_offset)I'm not certain if I understand the overflow.
arg->offset,region_begin_offset, andregion_end_offsetare all uint32.
Ah... You're right. I didn't read carefully. Sorry for the noise.
regards,
dan carpenter