Received: by 2002:a05:6a10:1d13:0:0:0:0 with SMTP id pp19csp30832pxb; Wed, 25 Aug 2021 19:03:44 -0700 (PDT) X-Google-Smtp-Source: ABdhPJy4gq7ZfUMtafUzidOcYsDCWIpjvqG/JW6BHmpzpnqBBFlSF7YkCXx6ExSclUhb/FYvtonq X-Received: by 2002:a92:c04e:: with SMTP id o14mr878685ilf.289.1629943424398; Wed, 25 Aug 2021 19:03:44 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1629943424; cv=none; d=google.com; s=arc-20160816; b=t2VSYTNzpSXs4Utw1EmrRAeokaUzAMqwORA0rA8hoODq6Cqg/ILD+qTQCfR8TjU40d iHMFhL1LW7xD9ZF+SiOtuNSHgwx6/xsHNWr9hTDLQWtnIs0oWXyqaQVjHgSJYY4wcFWa ZQqZ1CXP624GjelX2zTY4GmwHaoIAXCBoivCID9hb26hCd5Aq2ASAX1mV34GsCOfRY4O A/g/SK5I6CxSRERVJqGR3Fu0ppPEeYBW03JQ/I3w5Jw96gzcQOq9mqqFYE9/0EDLVH+N VB/N99DWCYSdofCU4iHPCiTGZMRQEKflqTT2afecNBOtAa1l4KvC6tWzw4RuHP0/6qDT ZK7w== 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=P/HLeCdm1ln+WDWlHx3gbH2c2TGPxEmJ7R7sNcfLOXw=; b=EiOGCGSbku2MM1EvqWjPtL/1VP0V+/uHlMiCq7LfvGjhd/f8FBw7/jtW+UspY6NoZ+ QaDfYXNrlIGXeKMtW+Fsed9UH/5M+K+8C8cc7wvsOScGgFZc6S2aGW4Hb1fCTJeraG8t ZfgaKC/C2ZtwrW58RU2NNt9oXbnFAzBJF8EbmzsJd8mXODjY3dx/RrS0PMRxQMaWH7XV 4X+3yTuEMrKSbgsvk3DRpHijjqFEUWIAOXa2KAmpewYfTF3O6WiCbYbC3G7TCYNHAMZe VW4G12sarKp1S6ERAeiZtaiOdyzFYJgl8SyA1CyVOcNK9MIJ+P/aF026bc3MWDvTZgT/ ngYA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=CT7h6MBp; 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 r28si1288372jao.16.2021.08.25.19.03.33; Wed, 25 Aug 2021 19:03:44 -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=CT7h6MBp; 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 S237399AbhHZCDW (ORCPT + 99 others); Wed, 25 Aug 2021 22:03:22 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58728 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235823AbhHZCDV (ORCPT ); Wed, 25 Aug 2021 22:03:21 -0400 Received: from mail-pl1-x630.google.com (mail-pl1-x630.google.com [IPv6:2607:f8b0:4864:20::630]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id EFB3BC061757; Wed, 25 Aug 2021 19:02:34 -0700 (PDT) Received: by mail-pl1-x630.google.com with SMTP id q3so792658plx.4; Wed, 25 Aug 2021 19:02:34 -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=P/HLeCdm1ln+WDWlHx3gbH2c2TGPxEmJ7R7sNcfLOXw=; b=CT7h6MBpAMDIzIkV8lMUMCjYQ166pfsg2+w0kUZlShHVyArnL+WjVrSIfMoU4JnlZ6 6bPBRWqj0e3eOeUtjM3d24seYloYJPfjNpUKpWtnwUkUHD6iUXCPSX44W8kUrA0CGCgt LtQJZd4cGVCCUpyQZBLrpKgjNSmayak1iXZaJS2CMPIjouy2VJhpTuBbpdd/YHtD6beK dd04Kl0M4LQ4Yfs1VgdcmIMO8bNiWpLP54v0OHeYyVExW2djCkhTl9Psq4/em9ZKjlCK LzqXnyTFpjCOtvtKeHELyI3nKD2pD6pAtrDa+6RC9JtHGqG6hggk8IUfp0klqHWQrpPd sJlA== 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=P/HLeCdm1ln+WDWlHx3gbH2c2TGPxEmJ7R7sNcfLOXw=; b=WtfWjPWXsJ189PTUWzn1uKdBsQJ6IDUQIN842zK55GwMp39NBItkxouJSejzRataV0 FV6wJvV8GDdjj8Vf48GqN2yNLNDYs/tK0xoP0N9R+0ysCyKUlkKN8WCDB/KB/ViJP7gb vOtZw0Uj+xplj11H06A2x/yWiGsgmPsEmfRL5vz9Yp55xOh304ugDWIYRNei4XcPARtJ j05rTCZrEWumEzrt79qG4UVfx68b/aUdVydT+37pu/xKsgKdK5B0qtYC2wQARqsuUEEe wWOGHrXt4w0qfLaqqao/TAIhgYKxNtDQmduMty+SG5fdguGyw0oSntswi6y0PVvPavO1 lWsw== X-Gm-Message-State: AOAM531/CxEZiQmbJDWQGp+GJ07vS0zY0AKN4iKhtoyudHsiICRxvvr5 ZaV/vyooaOSiVwRR3d/Na/4= X-Received: by 2002:a17:90a:9411:: with SMTP id r17mr1401311pjo.49.1629943354485; Wed, 25 Aug 2021 19:02:34 -0700 (PDT) Received: from localhost.localdomain ([118.200.190.93]) by smtp.gmail.com with ESMTPSA id i11sm721973pjj.19.2021.08.25.19.02.26 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 25 Aug 2021 19:02:34 -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 v8 3/7] drm: lock drm_global_mutex earlier in the ioctl handler Date: Thu, 26 Aug 2021 10:01:18 +0800 Message-Id: <20210826020122.1488002-4-desmondcheongzx@gmail.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20210826020122.1488002-1-desmondcheongzx@gmail.com> References: <20210826020122.1488002-1-desmondcheongzx@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org In a future patch, a read lock on drm_device.master_rwsem is held in the ioctl handler before the check for ioctl permissions. However, this inverts the lock hierarchy of drm_global_mutex --> master_rwsem. To avoid this, we do some prep work to grab the drm_global_mutex before checking for ioctl permissions. Signed-off-by: Desmond Cheong Zhi Xi --- drivers/gpu/drm/drm_ioctl.c | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/drivers/gpu/drm/drm_ioctl.c b/drivers/gpu/drm/drm_ioctl.c index d25713b09b80..158629d88319 100644 --- a/drivers/gpu/drm/drm_ioctl.c +++ b/drivers/gpu/drm/drm_ioctl.c @@ -772,19 +772,19 @@ long drm_ioctl_kernel(struct file *file, drm_ioctl_t *func, void *kdata, if (drm_dev_is_unplugged(dev)) return -ENODEV; + /* Enforce sane locking for modern driver ioctls. */ + if (unlikely(drm_core_check_feature(dev, DRIVER_LEGACY)) && !(flags & DRM_UNLOCKED)) + mutex_lock(&drm_global_mutex); + retcode = drm_ioctl_permit(flags, file_priv); if (unlikely(retcode)) - return retcode; + goto out; - /* Enforce sane locking for modern driver ioctls. */ - if (likely(!drm_core_check_feature(dev, DRIVER_LEGACY)) || - (flags & DRM_UNLOCKED)) - retcode = func(dev, kdata, file_priv); - else { - mutex_lock(&drm_global_mutex); - retcode = func(dev, kdata, file_priv); + retcode = func(dev, kdata, file_priv); + +out: + if (unlikely(drm_core_check_feature(dev, DRIVER_LEGACY)) && !(flags & DRM_UNLOCKED)) mutex_unlock(&drm_global_mutex); - } return retcode; } EXPORT_SYMBOL(drm_ioctl_kernel); -- 2.25.1