Received: by 2002:a05:6a10:1d13:0:0:0:0 with SMTP id pp19csp1321152pxb; Fri, 20 Aug 2021 03:05:08 -0700 (PDT) X-Google-Smtp-Source: ABdhPJypa5b0+TsxdjEmSZLsFezWhl/NZjmEPVTzBtzkxRTkpTjihC0L9+Av8ZFVmp3hlhTibJC8 X-Received: by 2002:a05:6602:2ac7:: with SMTP id m7mr15550876iov.66.1629453908539; Fri, 20 Aug 2021 03:05:08 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1629453908; cv=none; d=google.com; s=arc-20160816; b=d4vyHWZ9OtTpShFOeJbyXg/sIWCr5qlkaLigPQoalBayV1ZC/v+xOV/CkZ/CbDJ+Z7 aEerDvR9tg4lw9pTOFwyFn+QJtnndVRH3qhkE9cHQb12bhIWzwQ0gmJI3uw8wLBg9IhX 3y/JmVSDztM7FIUSiqGJqyTHiRAcXp0qggjpMIQchHgUy+ga7Hai6uHXdFfUzaIj8lRp sUthhBwmH4fy50XuO5Yp6IlU9j4xE0y63AYH+P364+ErclabQSWd9YK794ttd2SAUdV4 EW7dja+feaDWRZ+9Vj/gOeRjr2KNhE+UbanUjcvg3ughV+cCIsZDQTaSjvLMSSi2vVVs 2r2Q== 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=n1sb8nvJtdy47yyt3BKo3nW1Uqy7ctVSpZPJh5P4TDAVFKSyKJ14U4Umg/mVc5YXU8 cSMLY3+a8O9e9oAbTcvcd2VlP+MzJzQGeYn61L8PZ+UmfWtlviXduV+S2xHqpwlg1lTw FOfvS59mSUXQkknNkwtNg14ZFlLG6fhdGTWUmozYEu6S1L2t2P+wluCpUhZWw0dkUR43 YB/0zRkoAQNIxLoJSgKclKlh5UumpeGv8PzZbIQ4fwuxVq1QPwgi2hPOFkTDwpcn1ypW qGyZ8lVB3kk2AultsQ36h/G1nuFK6QCfwjELYkw+Q34Oz9uKkEYXfYc1Szrrm48weKd5 E0zQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=DTQmjYbe; 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 p19si7533548iov.90.2021.08.20.03.04.57; Fri, 20 Aug 2021 03:05:08 -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=DTQmjYbe; 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 S239035AbhHTKEb (ORCPT + 99 others); Fri, 20 Aug 2021 06:04:31 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58194 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238975AbhHTKE1 (ORCPT ); Fri, 20 Aug 2021 06:04:27 -0400 Received: from mail-pl1-x62d.google.com (mail-pl1-x62d.google.com [IPv6:2607:f8b0:4864:20::62d]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E3FB2C061757; Fri, 20 Aug 2021 03:03:49 -0700 (PDT) Received: by mail-pl1-x62d.google.com with SMTP id x1so3114488plg.10; Fri, 20 Aug 2021 03:03:49 -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=DTQmjYbeclJ/d85lmygNs7AQOxoVIEgJifRv2qQSOWvqZ2ZZ2rWY/looltY8Elkp94 tBSeEkCIew/UhUnyrbqdFtHVhXWK9S/xgAOoa0cxD4dA26Cau+TwjwQ2MiKvSUp3UBOJ 3ALHjolNoFTSFhdHZzTUXlBa4vlPqtchzwsSLBYQB9C/ZwNy7iDCtU2d3Pu8bWjY/F5f vDs6F8kL/cWPMz9qfbCTWsNRyab9mhiuGdsF6QrPf3hAScC1rHNgHcndN/OfZu8xIAE/ FmAMJVxwiE4yvYhiC9HTr73nQS6OvqGHo5zvHigLOMsBzFIvEahGL09MXfi7a3fCyOAj GP2A== 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=CKfa0LiSNk8IO1gev1n6SDPz2G9zgK2dV7/+2VrJdM69qE70WoT0E+km0vTZiOOhLC ElmYEmS+oQCtxvYdLMz24UEw/YS/KxvPp6yXSJCTKkSowV9YUGc6yfiTxD6RP6wHJ+Ox jDOMXPsfm0tR+xRSR9hpqJIaNeVpq+MLap66v1I38OL+hyESCjvOi+YpWgLAWonxeSez O5hT7g6zIO7Q5v6z7AxNu0dG3t/R1pLFuPiLuuGEwy1qIUD1k0rcB1C7kI65HVEIuXE+ 6qVT1aETVTB4tCN+vQm9eBzMukzsL9YWJW7gQOgeOrq+3FQHdBMEwSTU028qz/5FBn9v tq3A== X-Gm-Message-State: AOAM531ydGkPirlPhZ4BkjFvxI+EyE91FpE9PIMeeLPJ+/+V4fDwHZcB P3DdB0GpesXWyscUg9Z87eE= X-Received: by 2002:a17:902:a604:b029:12c:dda2:30c4 with SMTP id u4-20020a170902a604b029012cdda230c4mr15447497plq.73.1629453829526; Fri, 20 Aug 2021 03:03:49 -0700 (PDT) Received: from localhost.localdomain ([118.200.190.93]) by smtp.gmail.com with ESMTPSA id i8sm6503876pfo.117.2021.08.20.03.03.45 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 20 Aug 2021 03:03:48 -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 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 v4 3/5] drm: lock drm_global_mutex earlier in the ioctl handler Date: Fri, 20 Aug 2021 18:02:49 +0800 Message-Id: <20210820100251.448346-4-desmondcheongzx@gmail.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20210820100251.448346-1-desmondcheongzx@gmail.com> References: <20210820100251.448346-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