Received: by 2002:a05:6a10:1d13:0:0:0:0 with SMTP id pp19csp539720pxb; Wed, 25 Aug 2021 09:01:37 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxC3JRGURxdUvqNCFMlRQCHbH5AoON+wCjhN4/65GWfpnLLbZmc8PLq0IP4xaWccp2iW+Oh X-Received: by 2002:a17:906:c0cd:: with SMTP id bn13mr47429036ejb.251.1629907296866; Wed, 25 Aug 2021 09:01:36 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1629907296; cv=none; d=google.com; s=arc-20160816; b=jdt6PizUKvoIKqOmiGo1Aq+xHeA7FmlysOjAVFT3siCcADg9VygQnw9zq3oF2worZ2 vkIZWULot1/CufzABisjdeC1pDhM7BJQfjbwIaK1iC7iNq8mRzJ/2+h0hw1/TE8FFnen 4RBkQNL41AjetMNtO8E+S3ib0RNJ4co0m56pw1Z6Yol0znO32Qd9+5nCRIJA2TarqNmI BU4iMylvQLGpZmHsQObKXNx0dsO+zUXICx27dCXddBV0r6vqqk5bI097H4F79IKtjnPz ixVA4T9jlzSiom7u9U88gbfe9RfHO0astk7KTBGuDpoz1Yg2Gbotjw1OFpI8uV5a03oN UE1A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=uk23yrQj2bYGx5iUQvrvtVaf8WaEKXc6e3h1Qpr0Wxc=; b=r/pfyTAl/EJ1AWzvTnT+m7NWq+FcBk+xvc1IGxDnrw8FprjfOWmmZVkOmanSAVGEdA V6yZz/oS+H0l0H1K5k6R3+4Lkykxlkc+hWktQLe/HOXvua8jZw+TFnNzSQiTVCTrp46Y fkKlK+57/eL7khZdxI/0MzqJrVrf37RROsrXqKr+WM1rMqc4WDcWZjjAc+PaEURO7OTU GnQXmhC4j6DDL+p6A1hBbt4oiCOqrylEFAnTjZR9iUmTvbcfMVzepEYROB4dKyCbm0g3 KyQWVHXhn07SnX1+BKBIEeyRuS2cQ9odzUS9pJulqUGJtoxCxrLARcgaXgyVs4y1aOi3 Xrqw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=TVtTp34a; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 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. [23.128.96.18]) by mx.google.com with ESMTP id j18si356406edw.597.2021.08.25.09.01.10; Wed, 25 Aug 2021 09:01:36 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=TVtTp34a; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 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 S241531AbhHYOYo (ORCPT + 99 others); Wed, 25 Aug 2021 10:24:44 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39798 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S241438AbhHYOYb (ORCPT ); Wed, 25 Aug 2021 10:24:31 -0400 Received: from mail-pj1-x102d.google.com (mail-pj1-x102d.google.com [IPv6:2607:f8b0:4864:20::102d]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6F4F8C06179A; Wed, 25 Aug 2021 07:23:38 -0700 (PDT) Received: by mail-pj1-x102d.google.com with SMTP id u11-20020a17090adb4b00b00181668a56d6so4809236pjx.5; Wed, 25 Aug 2021 07:23:38 -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:in-reply-to:references :mime-version:content-transfer-encoding; bh=uk23yrQj2bYGx5iUQvrvtVaf8WaEKXc6e3h1Qpr0Wxc=; b=TVtTp34aCJBzvJ6goeHuIpGYFT2j35xmSVarMK07LV6ssbq6N3Xr3jKAtfwrGbNUJ8 8KaG/5k0Qk42WlpPCz1Kj51f/hGs4iFfkI7x3d7A0jYCEWm76X1B519CZqXInYNjbYat ab+RXSwXTlaQgaOuo21amm9/um2xZwXHg21kqAL7rcVv+J5ez020+0WMZGH7j2GQzGaN uibeXX/a1bPEFkgni9Fs2+und/mvkX/EBrti6qDlmoSWEx1zprLl5ANdl4l1w/8d37Qk RKVyYSHgQTw0cZ1B+cXkZC9tb71uNiT4fqQas8LIc7cikgXcJAHIJbSLJGgoZiVWz6Uz X++g== 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:in-reply-to :references:mime-version:content-transfer-encoding; bh=uk23yrQj2bYGx5iUQvrvtVaf8WaEKXc6e3h1Qpr0Wxc=; b=W6oaQxml8S5afUEWAJpxhOu2Rr2HT66CyodmkJUgAWQNDwZVC/qoHRtbTom08uTD0Z jsDgf0+NxMFcVoyMD21H4b9kCTvUz6SaGYF1KDqqXk3qREt4F7tuHkhhxgo47muzA/Nw J0wAgbysY1vFNIJcpH7+y35DWtVuRRb63E4/jGIxXr4Ts4CFztb5Ae/AdIxlkMvt76IK eZyWbtzNvxf2hBocOmiXg9VhCrBBGSr9QRt9sVRHxh7psdYFKJgDDY8veOV30fqS/9lB T7MortbrrRIsatL01gjuAFf0Hi2U1pwkC5DDzIHi8aViB3LMHHToxtfsoCwSrfzjxaOk +dPQ== X-Gm-Message-State: AOAM532hMpt2YMN9SzLLjBsmRoRi4+rqTkSqRhPh57QXL7s1d8a0PYmi +Z07dshhSRhQLo6DwiDPXcU= X-Received: by 2002:a17:90a:c481:: with SMTP id j1mr10895311pjt.164.1629901418017; Wed, 25 Aug 2021 07:23:38 -0700 (PDT) Received: from localhost.localdomain ([118.200.190.93]) by smtp.gmail.com with ESMTPSA id n3sm54431pfo.101.2021.08.25.07.23.31 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 25 Aug 2021 07:23:37 -0700 (PDT) From: Desmond Cheong Zhi Xi To: maarten.lankhorst@linux.intel.com, mripard@kernel.org, tzimmermann@suse.de, airlied@linux.ie, daniel@ffwll.ch, sumit.semwal@linaro.org, christian.koenig@amd.com, jani.nikula@linux.intel.com, joonas.lahtinen@linux.intel.com, rodrigo.vivi@intel.com, chris@chris-wilson.co.uk, ville.syrjala@linux.intel.com, matthew.auld@intel.com, dan.carpenter@oracle.com, tvrtko.ursulin@intel.com, matthew.d.roper@intel.com, lucas.demarchi@intel.com, karthik.b.s@intel.com, jose.souza@intel.com, manasi.d.navare@intel.com, airlied@redhat.com, aditya.swarup@intel.com, andrescj@chromium.org, linux-graphics-maintainer@vmware.com, zackr@vmware.com Cc: Desmond Cheong Zhi Xi , dri-devel@lists.freedesktop.org, linux-kernel@vger.kernel.org, intel-gfx@lists.freedesktop.org, linux-media@vger.kernel.org, linaro-mm-sig@lists.linaro.org, skhan@linuxfoundation.org, gregkh@linuxfoundation.org, linux-kernel-mentees@lists.linuxfoundation.org Subject: [PATCH v7 6/7] drm: avoid circular locks with modeset_mutex and master_rwsem Date: Wed, 25 Aug 2021 22:22:04 +0800 Message-Id: <20210825142205.1376789-7-desmondcheongzx@gmail.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20210825142205.1376789-1-desmondcheongzx@gmail.com> References: <20210825142205.1376789-1-desmondcheongzx@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org drm_lease_held calls drm_file_get_master. However, this function is sometimes called while holding on to modeset_mutex. Since drm_device.master_rwsem will replace drm_file.master_lookup_lock in drm_file_get_master in a future patch, this inverts the master_rwsem --> modeset_mutex lock hierarchy. To fix this, we create a new drm_lease_held_master helper function that enables us to avoid calling drm_file_get_master after locking modeset_mutex. Signed-off-by: Desmond Cheong Zhi Xi --- drivers/gpu/drm/drm_auth.c | 3 +++ drivers/gpu/drm/drm_encoder.c | 7 ++++++- drivers/gpu/drm/drm_lease.c | 30 +++++++++++++++--------------- drivers/gpu/drm/drm_plane.c | 14 +++++++++++--- include/drm/drm_lease.h | 2 ++ 5 files changed, 37 insertions(+), 19 deletions(-) diff --git a/drivers/gpu/drm/drm_auth.c b/drivers/gpu/drm/drm_auth.c index 65065f7e1499..f2b2f197052a 100644 --- a/drivers/gpu/drm/drm_auth.c +++ b/drivers/gpu/drm/drm_auth.c @@ -410,6 +410,9 @@ struct drm_master *drm_file_get_master(struct drm_file *file_priv) { struct drm_master *master = NULL; + if (!file_priv) + return NULL; + spin_lock(&file_priv->master_lookup_lock); if (!file_priv->master) goto unlock; diff --git a/drivers/gpu/drm/drm_encoder.c b/drivers/gpu/drm/drm_encoder.c index 72e982323a5e..bacb2f6a325c 100644 --- a/drivers/gpu/drm/drm_encoder.c +++ b/drivers/gpu/drm/drm_encoder.c @@ -22,6 +22,7 @@ #include +#include #include #include #include @@ -281,6 +282,7 @@ int drm_mode_getencoder(struct drm_device *dev, void *data, struct drm_mode_get_encoder *enc_resp = data; struct drm_encoder *encoder; struct drm_crtc *crtc; + struct drm_master *master; if (!drm_core_check_feature(dev, DRIVER_MODESET)) return -EOPNOTSUPP; @@ -289,13 +291,16 @@ int drm_mode_getencoder(struct drm_device *dev, void *data, if (!encoder) return -ENOENT; + master = drm_file_get_master(file_priv); drm_modeset_lock(&dev->mode_config.connection_mutex, NULL); crtc = drm_encoder_get_crtc(encoder); - if (crtc && drm_lease_held(file_priv, crtc->base.id)) + if (crtc && drm_lease_held_master(master, crtc->base.id)) enc_resp->crtc_id = crtc->base.id; else enc_resp->crtc_id = 0; drm_modeset_unlock(&dev->mode_config.connection_mutex); + if (master) + drm_master_put(&master); enc_resp->encoder_type = encoder->encoder_type; enc_resp->encoder_id = encoder->base.id; diff --git a/drivers/gpu/drm/drm_lease.c b/drivers/gpu/drm/drm_lease.c index 1b156c85d1c8..15bf3a3c76d1 100644 --- a/drivers/gpu/drm/drm_lease.c +++ b/drivers/gpu/drm/drm_lease.c @@ -114,27 +114,30 @@ bool _drm_lease_held(struct drm_file *file_priv, int id) return _drm_lease_held_master(file_priv->master, id); } -bool drm_lease_held(struct drm_file *file_priv, int id) +bool drm_lease_held_master(struct drm_master *master, int id) { - struct drm_master *master; bool ret; - if (!file_priv) + if (!master || !master->lessor) return true; - master = drm_file_get_master(file_priv); - if (!master) - return true; - if (!master->lessor) { - ret = true; - goto out; - } mutex_lock(&master->dev->mode_config.idr_mutex); ret = _drm_lease_held_master(master, id); mutex_unlock(&master->dev->mode_config.idr_mutex); -out: - drm_master_put(&master); + return ret; +} + +bool drm_lease_held(struct drm_file *file_priv, int id) +{ + struct drm_master *master; + bool ret; + + master = drm_file_get_master(file_priv); + ret = drm_lease_held_master(master, id); + if (master) + drm_master_put(&master); + return ret; } @@ -150,9 +153,6 @@ uint32_t drm_lease_filter_crtcs(struct drm_file *file_priv, uint32_t crtcs_in) int count_in, count_out; uint32_t crtcs_out = 0; - if (!file_priv) - return crtcs_in; - master = drm_file_get_master(file_priv); if (!master) return crtcs_in; diff --git a/drivers/gpu/drm/drm_plane.c b/drivers/gpu/drm/drm_plane.c index b5566167a798..af9f65bf74d4 100644 --- a/drivers/gpu/drm/drm_plane.c +++ b/drivers/gpu/drm/drm_plane.c @@ -23,6 +23,7 @@ #include #include +#include #include #include #include @@ -687,6 +688,7 @@ int drm_mode_getplane(struct drm_device *dev, void *data, struct drm_mode_get_plane *plane_resp = data; struct drm_plane *plane; uint32_t __user *format_ptr; + struct drm_master *master; if (!drm_core_check_feature(dev, DRIVER_MODESET)) return -EOPNOTSUPP; @@ -695,10 +697,13 @@ int drm_mode_getplane(struct drm_device *dev, void *data, if (!plane) return -ENOENT; + master = drm_file_get_master(file_priv); drm_modeset_lock(&plane->mutex, NULL); - if (plane->state && plane->state->crtc && drm_lease_held(file_priv, plane->state->crtc->base.id)) + if (plane->state && plane->state->crtc && + drm_lease_held_master(master, plane->state->crtc->base.id)) plane_resp->crtc_id = plane->state->crtc->base.id; - else if (!plane->state && plane->crtc && drm_lease_held(file_priv, plane->crtc->base.id)) + else if (!plane->state && plane->crtc && + drm_lease_held_master(master, plane->crtc->base.id)) plane_resp->crtc_id = plane->crtc->base.id; else plane_resp->crtc_id = 0; @@ -710,6 +715,8 @@ int drm_mode_getplane(struct drm_device *dev, void *data, else plane_resp->fb_id = 0; drm_modeset_unlock(&plane->mutex); + if (master) + drm_master_put(&master); plane_resp->plane_id = plane->base.id; plane_resp->possible_crtcs = drm_lease_filter_crtcs(file_priv, @@ -1114,6 +1121,7 @@ static int drm_mode_cursor_common(struct drm_device *dev, return -ENOENT; } + lockdep_assert_held_once(&dev->master_rwsem); drm_modeset_acquire_init(&ctx, DRM_MODESET_ACQUIRE_INTERRUPTIBLE); retry: ret = drm_modeset_lock(&crtc->mutex, &ctx); @@ -1128,7 +1136,7 @@ static int drm_mode_cursor_common(struct drm_device *dev, if (ret) goto out; - if (!drm_lease_held(file_priv, crtc->cursor->base.id)) { + if (file_priv && !drm_lease_held_master(file_priv->master, crtc->cursor->base.id)) { ret = -EACCES; goto out; } diff --git a/include/drm/drm_lease.h b/include/drm/drm_lease.h index 5c9ef6a2aeae..426ea86d3c6a 100644 --- a/include/drm/drm_lease.h +++ b/include/drm/drm_lease.h @@ -18,6 +18,8 @@ bool drm_lease_held(struct drm_file *file_priv, int id); bool _drm_lease_held(struct drm_file *file_priv, int id); +bool drm_lease_held_master(struct drm_master *master, int id); + void drm_lease_revoke(struct drm_master *master); uint32_t drm_lease_filter_crtcs(struct drm_file *file_priv, uint32_t crtcs); -- 2.25.1