Received: by 10.213.65.68 with SMTP id h4csp160755imn; Wed, 28 Mar 2018 00:40:38 -0700 (PDT) X-Google-Smtp-Source: AIpwx4/LqvxG+zK5lNRKkFE8L6bARBJY8IT2ZQshRvE2owTWdAv+PyZa76pGsOu9wsHEcaPfjgAy X-Received: by 10.99.0.200 with SMTP id 191mr1785150pga.33.1522222838282; Wed, 28 Mar 2018 00:40:38 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1522222838; cv=none; d=google.com; s=arc-20160816; b=arBO81RNuuVdM3ixmNvEKWeNddiW0heOyvyvYW3TN115hS9+s6ZoPNG9HRKzm63/sb 5OnxD2Pm3ahnaTX6RBTflnKc8GlepFqu/WShq7DDZRXiBc8dIQk74lC2J8dYhRwHZeAz eRNg0j4nvqWCIMYl1KI2TcENA28vM0wgx4Og0zyYgiacS2xUa3e7dVv2i5ZgE7poTv9s L5kXWhkHNC9Jq9eV8OnNOWKYiG7yjVfnG9zmkQAkzwaewvwO8pVADrh2gN/83LZ5FUbR 06VUMuCDOKokUtAcDRW8gpG9Lu3iWsH8EkZ4g/S4/PnGIaJmwCypCTRR6R/MYlGDuMCj wjTw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :message-id:date:subject:cc:to:from:dkim-signature :arc-authentication-results; bh=OG3Tm8S/hB2doIN9JivuXUglndSd9+dzkGe69RjBS88=; b=eZNI/kBwHSrucT96LqxzJuzebVww2+g9nmfY3je1uZ8C0W6VeSCOvGoP/bxErkzK0A 5uHWMrvwFEvSibrPgYs8bd+MUGv9HaCr9Y0a258/KODtYJR6hOiSJEcLTIg9xuxbE496 DfRaXWgsGa2KLrrpavMIs/ITC8KVzLfDyOBLE/azn1y4uRXHtiwB7BJs3G7OA1CiIro2 jGJM9tzwNIW5zYfuOo93Jw0A9IDHLXetyjOUmesiZDkBNjMigOy7eQ6mTl246KRAdwN4 tFRzCoHH37atRcBmWF4ObND0qfQsJUZtdiguXPaaOizoqRaRNmqctYWhOPUtN1hLR1mK nvnw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=KS1XqCTa; 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; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id 1-v6si3163159ple.629.2018.03.28.00.40.24; Wed, 28 Mar 2018 00:40:38 -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=pass header.i=@gmail.com header.s=20161025 header.b=KS1XqCTa; 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; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1753470AbeC1HjB (ORCPT + 99 others); Wed, 28 Mar 2018 03:39:01 -0400 Received: from mail-lf0-f65.google.com ([209.85.215.65]:43514 "EHLO mail-lf0-f65.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1753437AbeC1Hi6 (ORCPT ); Wed, 28 Mar 2018 03:38:58 -0400 Received: by mail-lf0-f65.google.com with SMTP id v207-v6so2039016lfa.10 for ; Wed, 28 Mar 2018 00:38:57 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=OG3Tm8S/hB2doIN9JivuXUglndSd9+dzkGe69RjBS88=; b=KS1XqCTac6YOo96tAqt76vYPQz31VTD7Omf8JSYrKHFFZ93p5ViiRdrUrO9FxdrcUj HVhR1rX2AqwcDxufvMnzgpJhEJJa3vgkt6/8fbuipzYh6fur93dShwhGW57Awkgo120T pSQbiWaotrJLHqoFzbriVNVFN7TOK3PWiUmFk2YiWAp3abpdw8LjtQWyk3c9gmpk/AMO VP+x6mFOB5HbJ7WjthhNyrpoGlnNDlwArU8egdROox14y2sjRXlW0qbmJUQ1GDZa56Nb evqASHfX8DNBma2U1LWktzPu/K3AbQZurECpnR6Jwn4EJqx/+8j7MfkMYPCQZooCmx3U ZlZQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=OG3Tm8S/hB2doIN9JivuXUglndSd9+dzkGe69RjBS88=; b=szpjwmUHVnHu/nDR2Vppk18PLJI+k3SRaSr7m4pjJrLueQSykyaVITbcGyiGnr/6NA X7vb1fRNmexbp4dH9iiRpa/gNqUupSmsjZM3Z/dMfgIwlOxmcn7Zs489EqYT66z2hPEn AUgyal4Ckml/CfMgJfYUUIBdkTVY7FiU9/pm/PFMXXMrW2qh2cTrI1iDrsLVKqdOuzO1 UfWtqq9b2srdq258an3fopDNwoQWoXPkVd0LCQBWmw7q9nWWcqlIovEo66bpTMvKiGwX e0gKW7jHLfJMpvAZGALfMrIslrCRFchB+/Gj9H6OeHQMXArpxxucGr74aqRF0qYgBQPD zJFQ== X-Gm-Message-State: AElRT7HQb1jETmmKhZb1zgB8v0JnC/rt1rRgPPersQxgujvw3gYN0aD0 0q+5zapr8+gdRxe3qOjkgul1pu+9 X-Received: by 2002:a19:dd4c:: with SMTP id u73-v6mr1643807lfg.145.1522222736312; Wed, 28 Mar 2018 00:38:56 -0700 (PDT) Received: from a2k-HP-ProDesk-600-G2-SFF.kyiv.epam.com (ll-55.209.223.85.sovam.net.ua. [85.223.209.55]) by smtp.gmail.com with ESMTPSA id a19sm510552lji.65.2018.03.28.00.38.54 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Wed, 28 Mar 2018 00:38:55 -0700 (PDT) From: Oleksandr Andrushchenko To: linux-kernel@vger.kernel.org, dri-devel@lists.freedesktop.org, airlied@linux.ie, daniel.vetter@intel.com, seanpaul@chromium.org, gustavo@padovan.org Cc: andr2000@gmail.com, =?UTF-8?q?Noralf=20Tr=C3=B8nnes?= , Oleksandr Andrushchenko , intel-gfx@lists.freedesktop.org Subject: [PATCH] drm: Use srcu to protect drm_device.unplugged Date: Wed, 28 Mar 2018 10:38:35 +0300 Message-Id: <1522222715-11814-1-git-send-email-andr2000@gmail.com> X-Mailer: git-send-email 2.7.4 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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 Signed-off-by: Oleksandr Andrushchenko Reviewed-by: Oleksandr Andrushchenko Tested-by: Oleksandr Andrushchenko Cc: intel-gfx@lists.freedesktop.org --- 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