<div dir="ltr">Also note that it is not currently feasible for UDL code to access drm_global_mutex.<div><br></div><div>Please also refer to the similar comments ("FIXME: open_count is protected by drm_global_mutex but that would lead to locking inversion with the driver load path.") in nouveau, i915 and amdgpu.</div></div><div class="gmail_extra"><br><div class="gmail_quote">On Wed, Feb 10, 2016 at 12:51 PM, Haixia Shi <span dir="ltr"><<a href="mailto:hshi@chromium.org" target="_blank">hshi@chromium.org</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><span class=""><span style="font-size:12.8px">> This should rather be:</span><br style="font-size:12.8px"><span style="font-size:12.8px">> </span><br style="font-size:12.8px"><span style="font-size:12.8px">> </span><span style="font-size:12.8px"> drm_release(inode, filp);</span><br style="font-size:12.8px"><span style="font-size:12.8px">> </span><span style="font-size:12.8px"> mutex_lock(&drm_global_mutex);</span><br style="font-size:12.8px"><span style="font-size:12.8px"><span style="color:rgb(34,34,34);font-size:12.8px">> </span> if (!dev->open_count && udl_device_is_unplugged(dev))<br></span><span style="font-size:12.8px">> </span><span style="font-size:12.8px"> drm_put_dev(dev);</span><br style="font-size:12.8px"><span style="font-size:12.8px">> </span><span style="font-size:12.8px"> mutex_unlock(&drm_global_</span><span style="font-size:12.8px">mutex);</span><br style="font-size:12.8px"><span style="font-size:12.8px">> </span><br style="font-size:12.8px"><span style="font-size:12.8px">> </span><span style="font-size:12.8px"> return 0;</span><br style="font-size:12.8px"><span style="font-size:12.8px">> </span><br style="font-size:12.8px"><span style="font-size:12.8px">> </span><span style="font-size:12.8px">There is no reason to look at the return code of drm_release(), ever.</span><br><div><span style="font-size:12.8px"><br></span></div></span><div><span style="font-size:12.8px">But drm_release() does return a retcode. It would still make sense to return that as-is in case any existing code relies on it.</span></div></div><div class="gmail_extra"><br><div class="gmail_quote"><span class="">On Wed, Feb 10, 2016 at 4:24 AM, David Herrmann <span dir="ltr"><<a href="mailto:dh.herrmann@gmail.com" target="_blank">dh.herrmann@gmail.com</a>></span> wrote:<br></span><div><div class="h5"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Hi<br>
<div><div><br>
On Tue, Feb 9, 2016 at 10:05 PM, Haixia Shi <<a href="mailto:hshi@chromium.org" target="_blank">hshi@chromium.org</a>> wrote:<br>
> Remove the general drm_device_is_unplugged() checks, and move the unplugged<br>
> flag handling logic into drm/udl. In general we want to keep driver-specific<br>
> logic out of common drm code.<br>
><br>
> Signed-off-by: Haixia Shi <<a href="mailto:hshi@chromium.org" target="_blank">hshi@chromium.org</a>><br>
> Reviewed-by: Stéphane Marchesin <<a href="mailto:marcheu@chromium.org" target="_blank">marcheu@chromium.org</a>><br>
> ---<br>
> drivers/gpu/drm/drm_drv.c | 6 ----<br>
> drivers/gpu/drm/drm_fops.c | 2 --<br>
> drivers/gpu/drm/drm_gem.c | 3 --<br>
> drivers/gpu/drm/drm_ioctl.c | 3 --<br>
> drivers/gpu/drm/drm_vm.c | 3 --<br>
> drivers/gpu/drm/udl/udl_connector.c | 2 +-<br>
> drivers/gpu/drm/udl/udl_drv.c | 7 +++--<br>
> drivers/gpu/drm/udl/udl_drv.h | 6 ++++<br>
> drivers/gpu/drm/udl/udl_fb.c | 2 +-<br>
> drivers/gpu/drm/udl/udl_gem.c | 5 +++<br>
> drivers/gpu/drm/udl/udl_main.c | 61 +++++++++++++++++++++++++++++++++++++<br>
> include/drm/drmP.h | 14 ---------<br>
> 12 files changed, 78 insertions(+), 36 deletions(-)<br>
><br>
> diff --git a/drivers/gpu/drm/drm_drv.c b/drivers/gpu/drm/drm_drv.c<br>
> index 167c8d3..f93ee12 100644<br>
> --- a/drivers/gpu/drm/drm_drv.c<br>
> +++ b/drivers/gpu/drm/drm_drv.c<br>
> @@ -376,9 +376,6 @@ struct drm_minor *drm_minor_acquire(unsigned int minor_id)<br>
><br>
> if (!minor) {<br>
> return ERR_PTR(-ENODEV);<br>
> - } else if (drm_device_is_unplugged(minor->dev)) {<br>
> - drm_dev_unref(minor->dev);<br>
> - return ERR_PTR(-ENODEV);<br>
> }<br>
><br>
> return minor;<br>
> @@ -464,9 +461,6 @@ void drm_unplug_dev(struct drm_device *dev)<br>
> drm_minor_unregister(dev, DRM_MINOR_CONTROL);<br>
><br>
> mutex_lock(&drm_global_mutex);<br>
> -<br>
> - drm_device_set_unplugged(dev);<br>
> -<br>
> if (dev->open_count == 0) {<br>
> drm_put_dev(dev);<br>
> }<br>
> diff --git a/drivers/gpu/drm/drm_fops.c b/drivers/gpu/drm/drm_fops.c<br>
> index 1ea8790..b4332d4 100644<br>
> --- a/drivers/gpu/drm/drm_fops.c<br>
> +++ b/drivers/gpu/drm/drm_fops.c<br>
> @@ -497,8 +497,6 @@ int drm_release(struct inode *inode, struct file *filp)<br>
><br>
> if (!--dev->open_count) {<br>
> retcode = drm_lastclose(dev);<br>
> - if (drm_device_is_unplugged(dev))<br>
> - drm_put_dev(dev);<br>
> }<br>
> mutex_unlock(&drm_global_mutex);<br>
><br>
> diff --git a/drivers/gpu/drm/drm_gem.c b/drivers/gpu/drm/drm_gem.c<br>
> index 2e8c77e..c622e32 100644<br>
> --- a/drivers/gpu/drm/drm_gem.c<br>
> +++ b/drivers/gpu/drm/drm_gem.c<br>
> @@ -900,9 +900,6 @@ int drm_gem_mmap(struct file *filp, struct vm_area_struct *vma)<br>
> struct drm_vma_offset_node *node;<br>
> int ret;<br>
><br>
> - if (drm_device_is_unplugged(dev))<br>
> - return -ENODEV;<br>
> -<br>
> drm_vma_offset_lock_lookup(dev->vma_offset_manager);<br>
> node = drm_vma_offset_exact_lookup_locked(dev->vma_offset_manager,<br>
> vma->vm_pgoff,<br>
> diff --git a/drivers/gpu/drm/drm_ioctl.c b/drivers/gpu/drm/drm_ioctl.c<br>
> index 8ce2a0c..f959074 100644<br>
> --- a/drivers/gpu/drm/drm_ioctl.c<br>
> +++ b/drivers/gpu/drm/drm_ioctl.c<br>
> @@ -695,9 +695,6 @@ long drm_ioctl(struct file *filp,<br>
><br>
> dev = file_priv->minor->dev;<br>
><br>
> - if (drm_device_is_unplugged(dev))<br>
> - return -ENODEV;<br>
> -<br>
> is_driver_ioctl = nr >= DRM_COMMAND_BASE && nr < DRM_COMMAND_END;<br>
><br>
> if (is_driver_ioctl) {<br>
> diff --git a/drivers/gpu/drm/drm_vm.c b/drivers/gpu/drm/drm_vm.c<br>
> index f90bd5f..3a68be4 100644<br>
> --- a/drivers/gpu/drm/drm_vm.c<br>
> +++ b/drivers/gpu/drm/drm_vm.c<br>
> @@ -657,9 +657,6 @@ int drm_legacy_mmap(struct file *filp, struct vm_area_struct *vma)<br>
> struct drm_device *dev = priv->minor->dev;<br>
> int ret;<br>
><br>
> - if (drm_device_is_unplugged(dev))<br>
> - return -ENODEV;<br>
> -<br>
> mutex_lock(&dev->struct_mutex);<br>
> ret = drm_mmap_locked(filp, vma);<br>
> mutex_unlock(&dev->struct_mutex);<br>
> diff --git a/drivers/gpu/drm/udl/udl_connector.c b/drivers/gpu/drm/udl/udl_connector.c<br>
> index 4709b54..b168f2c 100644<br>
> --- a/drivers/gpu/drm/udl/udl_connector.c<br>
> +++ b/drivers/gpu/drm/udl/udl_connector.c<br>
> @@ -96,7 +96,7 @@ static int udl_mode_valid(struct drm_connector *connector,<br>
> static enum drm_connector_status<br>
> udl_detect(struct drm_connector *connector, bool force)<br>
> {<br>
> - if (drm_device_is_unplugged(connector->dev))<br>
> + if (udl_device_is_unplugged(connector->dev))<br>
> return connector_status_disconnected;<br>
> return connector_status_connected;<br>
> }<br>
> diff --git a/drivers/gpu/drm/udl/udl_drv.c b/drivers/gpu/drm/udl/udl_drv.c<br>
> index d5728ec..f5c2a97 100644<br>
> --- a/drivers/gpu/drm/udl/udl_drv.c<br>
> +++ b/drivers/gpu/drm/udl/udl_drv.c<br>
> @@ -24,12 +24,12 @@ static const struct vm_operations_struct udl_gem_vm_ops = {<br>
><br>
> static const struct file_operations udl_driver_fops = {<br>
> .owner = THIS_MODULE,<br>
> - .open = drm_open,<br>
> + .open = udl_drm_open,<br>
> .mmap = udl_drm_gem_mmap,<br>
> .poll = drm_poll,<br>
> .read = drm_read,<br>
> - .unlocked_ioctl = drm_ioctl,<br>
> - .release = drm_release,<br>
> + .unlocked_ioctl = udl_drm_ioctl,<br>
> + .release = udl_drm_release,<br>
> #ifdef CONFIG_COMPAT<br>
> .compat_ioctl = drm_compat_ioctl,<br>
> #endif<br>
> @@ -97,6 +97,7 @@ static void udl_usb_disconnect(struct usb_interface *interface)<br>
> drm_connector_unplug_all(dev);<br>
> udl_fbdev_unplug(dev);<br>
> udl_drop_usb(dev);<br>
> + udl_device_set_unplugged(dev);<br>
> drm_unplug_dev(dev);<br>
> }<br>
><br>
> diff --git a/drivers/gpu/drm/udl/udl_drv.h b/drivers/gpu/drm/udl/udl_drv.h<br>
> index 4a064ef..75aa91c 100644<br>
> --- a/drivers/gpu/drm/udl/udl_drv.h<br>
> +++ b/drivers/gpu/drm/udl/udl_drv.h<br>
> @@ -65,6 +65,7 @@ struct udl_device {<br>
> atomic_t bytes_identical; /* saved effort with backbuffer comparison */<br>
> atomic_t bytes_sent; /* to usb, after compression including overhead */<br>
> atomic_t cpu_kcycles_used; /* transpired during pixel processing */<br>
> + atomic_t unplugged; /* device has been unplugged or gone away */<br>
> };<br>
><br>
> struct udl_gem_object {<br>
> @@ -133,6 +134,9 @@ int udl_gem_get_pages(struct udl_gem_object *obj);<br>
> void udl_gem_put_pages(struct udl_gem_object *obj);<br>
> int udl_gem_vmap(struct udl_gem_object *obj);<br>
> void udl_gem_vunmap(struct udl_gem_object *obj);<br>
> +int udl_drm_open(struct inode *inode, struct file *filp);<br>
> +int udl_drm_release(struct inode *inode, struct file *filp);<br>
> +int udl_drm_ioctl(struct file *filp, unsigned int cmd, unsigned long arg);<br>
> int udl_drm_gem_mmap(struct file *filp, struct vm_area_struct *vma);<br>
> int udl_gem_fault(struct vm_area_struct *vma, struct vm_fault *vmf);<br>
><br>
> @@ -140,6 +144,8 @@ int udl_handle_damage(struct udl_framebuffer *fb, int x, int y,<br>
> int width, int height);<br>
><br>
> int udl_drop_usb(struct drm_device *dev);<br>
> +void udl_device_set_unplugged(struct drm_device *dev);<br>
> +int udl_device_is_unplugged(struct drm_device *dev);<br>
><br>
> #define CMD_WRITE_RAW8 "\xAF\x60" /**< 8 bit raw write command. */<br>
> #define CMD_WRITE_RL8 "\xAF\x61" /**< 8 bit run length command. */<br>
> diff --git a/drivers/gpu/drm/udl/udl_fb.c b/drivers/gpu/drm/udl/udl_fb.c<br>
> index 200419d..df1d53e 100644<br>
> --- a/drivers/gpu/drm/udl/udl_fb.c<br>
> +++ b/drivers/gpu/drm/udl/udl_fb.c<br>
> @@ -325,7 +325,7 @@ static int udl_fb_open(struct fb_info *info, int user)<br>
> struct udl_device *udl = dev->dev_private;<br>
><br>
> /* If the USB device is gone, we don't accept new opens */<br>
> - if (drm_device_is_unplugged(udl->ddev))<br>
> + if (udl_device_is_unplugged(udl->ddev))<br>
> return -ENODEV;<br>
><br>
> ufbdev->fb_count++;<br>
> diff --git a/drivers/gpu/drm/udl/udl_gem.c b/drivers/gpu/drm/udl/udl_gem.c<br>
> index 2a0a784..33c302f 100644<br>
> --- a/drivers/gpu/drm/udl/udl_gem.c<br>
> +++ b/drivers/gpu/drm/udl/udl_gem.c<br>
> @@ -86,8 +86,13 @@ int udl_dumb_create(struct drm_file *file,<br>
><br>
> int udl_drm_gem_mmap(struct file *filp, struct vm_area_struct *vma)<br>
> {<br>
> + struct drm_file *priv = filp->private_data;<br>
> + struct drm_device *dev = priv->minor->dev;<br>
> int ret;<br>
><br>
> + if (udl_device_is_unplugged(dev))<br>
> + return -ENODEV;<br>
> +<br>
> ret = drm_gem_mmap(filp, vma);<br>
> if (ret)<br>
> return ret;<br>
> diff --git a/drivers/gpu/drm/udl/udl_main.c b/drivers/gpu/drm/udl/udl_main.c<br>
> index 33dbfb2..2ba3ee3 100644<br>
> --- a/drivers/gpu/drm/udl/udl_main.c<br>
> +++ b/drivers/gpu/drm/udl/udl_main.c<br>
> @@ -350,3 +350,64 @@ int udl_driver_unload(struct drm_device *dev)<br>
> kfree(udl);<br>
> return 0;<br>
> }<br>
> +<br>
> +int udl_drm_open(struct inode *inode, struct file *filp)<br>
> +{<br>
> + struct drm_file *file_priv;<br>
> + struct drm_minor *minor;<br>
> +<br>
> + int ret = drm_open(inode, filp);<br>
> + if (ret)<br>
> + return ret;<br>
> +<br>
> + file_priv = filp->private_data;<br>
> + minor = file_priv->minor;<br>
> + if (udl_device_is_unplugged(minor->dev)) {<br>
> + drm_dev_unref(minor->dev);<br>
> + return -ENODEV;<br>
> + }<br>
<br>
</div></div>This doesn't make sense to me. You're called here with global-mutex<br>
held, so you can just check udl_device_is_unplugged() before calling<br>
into drm_open().<br>
<span><br>
> +<br>
> + return 0;<br>
> +}<br>
> +<br>
> +int udl_drm_release(struct inode *inode, struct file *filp)<br>
> +{<br>
> + struct drm_file *file_priv = filp->private_data;<br>
> + struct drm_device *dev = file_priv->minor->dev;<br>
> +<br>
> + int ret = drm_release(inode, filp);<br>
> + if (!dev->open_count && udl_device_is_unplugged(dev))<br>
> + drm_put_dev(dev);<br>
<br>
</span>This should rather be:<br>
<br>
drm_release(inode, filp);<br>
mutex_lock(&drm_global_mutex);<br>
<span> if (!dev->open_count && udl_device_is_unplugged(dev))<br>
</span> drm_put_dev(dev);<br>
mutex_unlock(&drm_global_mutex);<br>
<br>
return 0;<br>
<br>
There is no reason to look at the return code of drm_release(), ever.<br>
<span><br>
> +<br>
> + return ret;<br>
> +}<br>
> +<br>
> +int udl_drm_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)<br>
> +{<br>
> + struct drm_file *file_priv = filp->private_data;<br>
> + struct drm_device *dev = file_priv->minor->dev;<br>
> + unsigned int nr = DRM_IOCTL_NR(cmd);<br>
> +<br>
> + if (udl_device_is_unplugged(dev) &&<br>
> + nr != DRM_IOCTL_NR(DRM_IOCTL_MODE_SETCRTC) &&<br>
> + nr != DRM_IOCTL_NR(DRM_IOCTL_MODE_RMFB) &&<br>
> + nr != DRM_IOCTL_NR(DRM_IOCTL_MODE_DESTROY_DUMB))<br>
> + return -ENODEV;<br>
<br>
</span>Why?<br>
<br>
Just do:<br>
<br>
if (udl_device_is_unplugged(dev))<br>
return -ENODEV;<br>
<br>
Why this complex logic here?<br>
<br>
Thanks<br>
<span><font color="#888888">David<br>
</font></span><div><div><br>
> +<br>
> + return drm_ioctl(filp, cmd, arg);<br>
> +}<br>
> +<br>
> +void udl_device_set_unplugged(struct drm_device *dev)<br>
> +{<br>
> + struct udl_device *udl = dev->dev_private;<br>
> + smp_wmb();<br>
> + atomic_set(&udl->unplugged, 1);<br>
> +}<br>
> +<br>
> +int udl_device_is_unplugged(struct drm_device *dev)<br>
> +{<br>
> + struct udl_device *udl = dev->dev_private;<br>
> + int ret = atomic_read(&udl->unplugged);<br>
> + smp_rmb();<br>
> + return ret;<br>
> +}<br>
> diff --git a/include/drm/drmP.h b/include/drm/drmP.h<br>
> index d7162cf..40c6099 100644<br>
> --- a/include/drm/drmP.h<br>
> +++ b/include/drm/drmP.h<br>
> @@ -748,7 +748,6 @@ struct drm_device {<br>
> struct drm_minor *control; /**< Control node */<br>
> struct drm_minor *primary; /**< Primary node */<br>
> struct drm_minor *render; /**< Render node */<br>
> - atomic_t unplugged; /**< Flag whether dev is dead */<br>
> struct inode *anon_inode; /**< inode for private address-space */<br>
> char *unique; /**< unique name of the device */<br>
> /*@} */<br>
> @@ -879,19 +878,6 @@ static __inline__ int drm_core_check_feature(struct drm_device *dev,<br>
> return ((dev->driver->driver_features & feature) ? 1 : 0);<br>
> }<br>
><br>
> -static inline void drm_device_set_unplugged(struct drm_device *dev)<br>
> -{<br>
> - smp_wmb();<br>
> - atomic_set(&dev->unplugged, 1);<br>
> -}<br>
> -<br>
> -static inline int drm_device_is_unplugged(struct drm_device *dev)<br>
> -{<br>
> - int ret = atomic_read(&dev->unplugged);<br>
> - smp_rmb();<br>
> - return ret;<br>
> -}<br>
> -<br>
> static inline bool drm_is_render_client(const struct drm_file *file_priv)<br>
> {<br>
> return file_priv->minor->type == DRM_MINOR_RENDER;<br>
> --<br>
> 2.7.0.rc3.207.g0ac5344<br>
><br>
</div></div></blockquote></div></div></div><br></div>
</blockquote></div><br></div>