Received: by 10.213.65.68 with SMTP id h4csp148825imn; Wed, 28 Mar 2018 00:21:36 -0700 (PDT) X-Google-Smtp-Source: AIpwx4/7TTm7fc8Egh94O31eULENTKCGoFI+N9zDl028y61YY8MmtTtwa1FQx9y0OCgoTOxC4vpR X-Received: by 2002:a17:902:7e09:: with SMTP id b9-v6mr2589409plm.223.1522221696503; Wed, 28 Mar 2018 00:21:36 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1522221696; cv=none; d=google.com; s=arc-20160816; b=xgoBljWnmHAxC2INIUvvsFmCRVkfqMkkNLfaXl5TBu9Gf4rReOoVtw1RLntb1nLyT2 OwICY8+f+3hABROyzdA+R1KA9Y7KPgopmaj3DXFnGXi/KAXU39QS1wJDE3tlZIKJeQz9 xlISEES2OZ1BkWumJMJgBs5tLCG1aKyLZwsM8FzvNlkuc9g5lLHg88IXXjwkG8ArEDCT 4wNmqLWEbAkoppEG5qtwOzPoOw0gZ1RNMJaZ6yb5wyEc45tllEa0OkiaraeIN+pgABn8 2DmOGIJAy5iulBZpbJmcu50WE60NSwL3zjFQIC72GpMRpqb47SoZwb0Cp21Filg4908Z UukA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:user-agent:in-reply-to :content-transfer-encoding:content-disposition:mime-version :references:mail-followup-to:message-id:subject:cc:to:from:date :dkim-signature:arc-authentication-results; bh=FU92jrbSl4Wn1dwCIIoLQncglUMZtxaZDfQQwaVPQJU=; b=YIwQepfQ4uxkSOZ6hf6kaklZSDdA4Qwj+jUihXTVswhghDdXVIoCFe6JfEKQj/Ox0n D/BAeKvfSqlwivm5D7XNo1jBhSgO+U1q4jBD88fSY4vzInOIxbnFihcxDvaAZf+eh7kO +jcl5zUs2SsCo9O9oWtqmtYRznlem1Cfkc+5uqtB8Qfec8jCvB4w4TbIHn5ZKWxcynvK AZKT7k8OvDkiljA2l/1ImaZgpRQUlsBtyBa0p3yIZwmjtit/LEpOrEKKBQ/tKp0N46CT 5rkIGhBJ4/srVEh0JdpUXRh4CvqprntU0IOw5sKYBg052NvX/j/dWh9UH57uaZ5lgRYm S2Hw== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@ffwll.ch header.s=google header.b=YVsOTsIi; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id h14-v6si3179143plk.706.2018.03.28.00.21.21; Wed, 28 Mar 2018 00:21:36 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=fail header.i=@ffwll.ch header.s=google header.b=YVsOTsIi; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752427AbeC1HUR (ORCPT + 99 others); Wed, 28 Mar 2018 03:20:17 -0400 Received: from mail-wm0-f66.google.com ([74.125.82.66]:53134 "EHLO mail-wm0-f66.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751070AbeC1HUQ (ORCPT ); Wed, 28 Mar 2018 03:20:16 -0400 Received: by mail-wm0-f66.google.com with SMTP id l9so2918269wmh.2 for ; Wed, 28 Mar 2018 00:20:15 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ffwll.ch; s=google; h=sender:date:from:to:cc:subject:message-id:mail-followup-to :references:mime-version:content-disposition :content-transfer-encoding:in-reply-to:user-agent; bh=FU92jrbSl4Wn1dwCIIoLQncglUMZtxaZDfQQwaVPQJU=; b=YVsOTsIiz7pa+ZUkEwklS8ufM6D4nZm3pcQ1t1NShDbOBIKoBmlqz+Drl8LeUPYBHX cMFsvoZo4exl1w83uZt5sa50/L25/9zqseiWw8RjSFGjbkhysP9nCMZQS693A+Cpkxho TQ9NZu9kF1KI3KptN0CI46rjw84jz4M8MR0oE= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:sender:date:from:to:cc:subject:message-id :mail-followup-to:references:mime-version:content-disposition :content-transfer-encoding:in-reply-to:user-agent; bh=FU92jrbSl4Wn1dwCIIoLQncglUMZtxaZDfQQwaVPQJU=; b=VbePbIEqDQw5cBxXa6M2jifpzrM4txo8CpA4mFsb0MY2KDf5ul6NNGHzZBKn5Di/On Nxg8lUXT1bHCUGRJkOjUc/lOriUYkQRsjnb+564317GsiRlqbddxNVxSyHvivt7r0faS huvCqGjS/UUPDsoZdrmtAtQpj0I+Dc4F0tox02PhDmaBZsTFXtjNGQrmAGzPeJ6I1em8 /BXpV33DfSnoQlpyvtOklm23l13ALX+7G+vQy9a7UdxcNrTQXSF2IAOTAuhKXohqE+po A2zCqoA19pU3K6XQagS6FzDIQafR3y81k3n62oBjLoRRaK4EK1H2jzo1WmH0/thQJIMy HSAQ== X-Gm-Message-State: AElRT7GBWCQHvDnBXYKYjG7015C4SI0Cq9FM5YaKUL3mSIVKiY/XzeiF X9mj16k+m2iPoMYfYqcM8XfGsdgL X-Received: by 10.80.136.229 with SMTP id d92mr2188115edd.239.1522221614905; Wed, 28 Mar 2018 00:20:14 -0700 (PDT) Received: from phenom.ffwll.local (212-51-149-109.fiber7.init7.net. [212.51.149.109]) by smtp.gmail.com with ESMTPSA id x8sm1905574eda.58.2018.03.28.00.20.13 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Wed, 28 Mar 2018 00:20:14 -0700 (PDT) Date: Wed, 28 Mar 2018 09:20:12 +0200 From: Daniel Vetter To: Oleksandr Andrushchenko Cc: xen-devel@lists.xenproject.org, linux-kernel@vger.kernel.org, dri-devel@lists.freedesktop.org, airlied@linux.ie, daniel.vetter@intel.com, seanpaul@chromium.org, gustavo@padovan.org, jgross@suse.com, boris.ostrovsky@oracle.com, konrad.wilk@oracle.com, intel-gfx@lists.freedesktop.org, Noralf =?iso-8859-1?Q?Tr=F8nnes?= Subject: Re: [Intel-gfx] [PATCH v4 1/2] drm: Use srcu to protect drm_device.unplugged Message-ID: <20180328072012.GD14155@phenom.ffwll.local> Mail-Followup-To: Oleksandr Andrushchenko , xen-devel@lists.xenproject.org, linux-kernel@vger.kernel.org, dri-devel@lists.freedesktop.org, airlied@linux.ie, daniel.vetter@intel.com, seanpaul@chromium.org, gustavo@padovan.org, jgross@suse.com, boris.ostrovsky@oracle.com, konrad.wilk@oracle.com, intel-gfx@lists.freedesktop.org, Noralf =?iso-8859-1?Q?Tr=F8nnes?= References: <1522219661-26827-1-git-send-email-andr2000@gmail.com> <1522219661-26827-2-git-send-email-andr2000@gmail.com> MIME-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: <1522219661-26827-2-git-send-email-andr2000@gmail.com> X-Operating-System: Linux phenom 4.15.0-1-amd64 User-Agent: Mutt/1.9.4 (2018-02-28) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Wed, Mar 28, 2018 at 09:47:40AM +0300, Oleksandr Andrushchenko wrote: > From: Noralf Tr?nnes > > Use srcu to protect drm_device.unplugged in a race free manner. > Drivers can use drm_dev_enter()/drm_dev_exit() to protect and mark > sections preventing access to device resources that are not available > after the device is gone. > > Suggested-by: Daniel Vetter > Signed-off-by: Noralf Tr?nnes > Reviewed-by: Oleksandr Andrushchenko > Tested-by: Oleksandr Andrushchenko > Cc: intel-gfx@lists.freedesktop.org When you apply/forward a patch we also need your s-o-b line, even if you changed nothing. sob needs to reflect the full record of everyone who handled a patch from author to when it finally lands in git. -Daniel > --- > drivers/gpu/drm/drm_drv.c | 54 ++++++++++++++++++++++++++++++++++++++++++----- > include/drm/drm_device.h | 9 +++++++- > include/drm/drm_drv.h | 15 +++++++++---- > 3 files changed, 68 insertions(+), 10 deletions(-) > > diff --git a/drivers/gpu/drm/drm_drv.c b/drivers/gpu/drm/drm_drv.c > index a1b9338736e3..32a83b41ab61 100644 > --- a/drivers/gpu/drm/drm_drv.c > +++ b/drivers/gpu/drm/drm_drv.c > @@ -32,6 +32,7 @@ > #include > #include > #include > +#include > > #include > #include > @@ -75,6 +76,8 @@ static bool drm_core_init_complete = false; > > static struct dentry *drm_debugfs_root; > > +DEFINE_STATIC_SRCU(drm_unplug_srcu); > + > /* > * DRM Minors > * A DRM device can provide several char-dev interfaces on the DRM-Major. Each > @@ -318,18 +321,51 @@ void drm_put_dev(struct drm_device *dev) > } > EXPORT_SYMBOL(drm_put_dev); > > -static void drm_device_set_unplugged(struct drm_device *dev) > +/** > + * drm_dev_enter - Enter device critical section > + * @dev: DRM device > + * @idx: Pointer to index that will be passed to the matching drm_dev_exit() > + * > + * This function marks and protects the beginning of a section that should not > + * be entered after the device has been unplugged. The section end is marked > + * with drm_dev_exit(). Calls to this function can be nested. > + * > + * Returns: > + * True if it is OK to enter the section, false otherwise. > + */ > +bool drm_dev_enter(struct drm_device *dev, int *idx) > +{ > + *idx = srcu_read_lock(&drm_unplug_srcu); > + > + if (dev->unplugged) { > + srcu_read_unlock(&drm_unplug_srcu, *idx); > + return false; > + } > + > + return true; > +} > +EXPORT_SYMBOL(drm_dev_enter); > + > +/** > + * drm_dev_exit - Exit device critical section > + * @idx: index returned from drm_dev_enter() > + * > + * This function marks the end of a section that should not be entered after > + * the device has been unplugged. > + */ > +void drm_dev_exit(int idx) > { > - smp_wmb(); > - atomic_set(&dev->unplugged, 1); > + srcu_read_unlock(&drm_unplug_srcu, idx); > } > +EXPORT_SYMBOL(drm_dev_exit); > > /** > * drm_dev_unplug - unplug a DRM device > * @dev: DRM device > * > * This unplugs a hotpluggable DRM device, which makes it inaccessible to > - * userspace operations. Entry-points can use drm_dev_is_unplugged(). This > + * userspace operations. Entry-points can use drm_dev_enter() and > + * drm_dev_exit() to protect device resources in a race free manner. This > * essentially unregisters the device like drm_dev_unregister(), but can be > * called while there are still open users of @dev. > */ > @@ -338,10 +374,18 @@ void drm_dev_unplug(struct drm_device *dev) > drm_dev_unregister(dev); > > mutex_lock(&drm_global_mutex); > - drm_device_set_unplugged(dev); > if (dev->open_count == 0) > drm_dev_put(dev); > mutex_unlock(&drm_global_mutex); > + > + /* > + * After synchronizing any critical read section is guaranteed to see > + * the new value of ->unplugged, and any critical section which might > + * still have seen the old value of ->unplugged is guaranteed to have > + * finished. > + */ > + dev->unplugged = true; > + synchronize_srcu(&drm_unplug_srcu); > } > EXPORT_SYMBOL(drm_dev_unplug); > > diff --git a/include/drm/drm_device.h b/include/drm/drm_device.h > index 7c4fa32f3fc6..3a0eac2885b7 100644 > --- a/include/drm/drm_device.h > +++ b/include/drm/drm_device.h > @@ -46,7 +46,14 @@ struct drm_device { > /* currently active master for this device. Protected by master_mutex */ > struct drm_master *master; > > - atomic_t unplugged; /**< Flag whether dev is dead */ > + /** > + * @unplugged: > + * > + * Flag to tell if the device has been unplugged. > + * See drm_dev_enter() and drm_dev_is_unplugged(). > + */ > + bool unplugged; > + > struct inode *anon_inode; /**< inode for private address-space */ > char *unique; /**< unique name of the device */ > /*@} */ > diff --git a/include/drm/drm_drv.h b/include/drm/drm_drv.h > index d23dcdd1bd95..7e545f5f94d3 100644 > --- a/include/drm/drm_drv.h > +++ b/include/drm/drm_drv.h > @@ -624,6 +624,8 @@ void drm_dev_get(struct drm_device *dev); > void drm_dev_put(struct drm_device *dev); > void drm_dev_unref(struct drm_device *dev); > void drm_put_dev(struct drm_device *dev); > +bool drm_dev_enter(struct drm_device *dev, int *idx); > +void drm_dev_exit(int idx); > void drm_dev_unplug(struct drm_device *dev); > > /** > @@ -635,11 +637,16 @@ void drm_dev_unplug(struct drm_device *dev); > * unplugged, these two functions guarantee that any store before calling > * drm_dev_unplug() is visible to callers of this function after it completes > */ > -static inline int drm_dev_is_unplugged(struct drm_device *dev) > +static inline bool drm_dev_is_unplugged(struct drm_device *dev) > { > - int ret = atomic_read(&dev->unplugged); > - smp_rmb(); > - return ret; > + int idx; > + > + if (drm_dev_enter(dev, &idx)) { > + drm_dev_exit(idx); > + return false; > + } > + > + return true; > } > > > -- > 2.7.4 > > _______________________________________________ > Intel-gfx mailing list > Intel-gfx@lists.freedesktop.org > https://lists.freedesktop.org/mailman/listinfo/intel-gfx -- Daniel Vetter Software Engineer, Intel Corporation http://blog.ffwll.ch