Received: by 2002:a05:6a10:1d13:0:0:0:0 with SMTP id pp19csp4227222pxb; Mon, 30 Aug 2021 23:16:33 -0700 (PDT) X-Google-Smtp-Source: ABdhPJx7Gc3DUM9VaqIlEOGE90p6pUNlCr3Einfhbert383WIbajAaX1oLe7awKzYDwd3kob9uvd X-Received: by 2002:a5d:9653:: with SMTP id d19mr21211605ios.74.1630390593648; Mon, 30 Aug 2021 23:16:33 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1630390593; cv=none; d=google.com; s=arc-20160816; b=d7WBTO9Oochle+JCaCpKjd9TAJ83A4GsMDixB66cyy3jU1LFyOxxF6VyBr3Fa/TCQF VzuVnNX/jfKMW7ZU5cO7Y+itqyOToS5ifZL0LYy354niH2+DtcByXH+No+ZxrPmEV0VS spUMCLL4NUnznnm7KvhH/lA4VcYt8GxrZuVgII2JsgzHnHtrArDMEbYPEeisJjRi7Nf0 2hpC5NQ3HYT60VjjJn6IdnCHVnOdduC/iQduxG/6GfXa09SmfUkA72zpF/tdUz854WCN ujBQL0zZnTyAcADnEyBjubTv6ZV8CTDWKxwBil/KXtOHUu4DYvzmkJKIswum0n5M7rIm JY/A== 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=8hkPxmWIahwpRPq8B7GtGqA3EkMAolIN63S2SawwkKE=; b=QxI2KxKhXAbaiQveYvaFPBVx1H/lauIe4+UGLgZarA7qE+1I1+M/qgL08OdPhjkXu4 AoGM99HBV/ssxcTncWxGsCRFGFB6CR6PvjJXy26unDiIyw/dlgvI+qjo+FbqHoNHYxPF vZemc7QZ0R7RwNrDuJLvvb+mC2NVjDkptW4gpUTaVHZkfpX6UdFn79bEVLe8LaAtv3sN j19r6q5kitWJidAsz+RiDB4nfFT/pPAQzc/b+HGwLgAknV3H560HHODSaLf5WWxvITJC rdxWJODhekU2RAoM/gvgemViOM18glug5+tg/o2debGWauFipTI4hVPQTxopzILQOb6+ /PfA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=u3DHwM2o; 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 o4si16166573ilf.130.2021.08.30.23.16.22; Mon, 30 Aug 2021 23:16:33 -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=u3DHwM2o; 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 S231882AbhHaGP4 (ORCPT + 99 others); Tue, 31 Aug 2021 02:15:56 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38136 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230519AbhHaGPz (ORCPT ); Tue, 31 Aug 2021 02:15:55 -0400 Received: from mail-pl1-x633.google.com (mail-pl1-x633.google.com [IPv6:2607:f8b0:4864:20::633]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id BB366C061575; Mon, 30 Aug 2021 23:15:00 -0700 (PDT) Received: by mail-pl1-x633.google.com with SMTP id e7so572005plh.8; Mon, 30 Aug 2021 23:15:00 -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=8hkPxmWIahwpRPq8B7GtGqA3EkMAolIN63S2SawwkKE=; b=u3DHwM2oboAS0ZS+3mC5Troi4b9j425hyQBxoON5rcNg7gbRyz6bFb/HNVYcYgRjXe QRCLG33ItY/zhpfIEpoNB9usWhaAB9rV9XlRBp4eJEaP7mJFFlhLdOXbBKpl9kJ0vnxO 9kpd9npyh9Qx6NhldeMLDO9kbZPERm5mel5aOlz46kOBFdM9PHtorvsCM+z9T+RF+mja zZd6FZTx2fo/nIzc4JovO0h9I4idoA45weme2RTDeEPL0Bcu1vXKWUhP1VZ84DJa77vu baujx/5CZlH+qX/QxlKFAyAOOgOhgXFy4BAcKCgFOtugIkWMKLGx6CgJ4VVwKwnI2OmV OJlA== 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=8hkPxmWIahwpRPq8B7GtGqA3EkMAolIN63S2SawwkKE=; b=RaxNmXHI1LKcMKr23KN6uEOWiLuKUTVLwduMii643zyz1gq/Pb5k52ipE9FLdOXYCR JHK5ZCNzCG0mgCLw8qxo12DrFzPzpnFG7WbEMyP4//zNoDN7l19BIlSNrAn6ZXZUrccT U+Vzb1qZmUKphozvj8trgGPIKWRjopKExbHgt43QHE3ZkYyo1u+kE9uiR/sMxejXM3du ivSMoQAY+xKQw9HIjQLg03FOfRj/z3omTssMaNb0X/rglECduyp6qf/yOYGa7WO5105Q nwnjRNBaRzfyOfzOa/amBK9Vkdb4K+eTVFRgywOhuKtNGQ7gocq9JryvG3IxHR6hUKbK RHWA== X-Gm-Message-State: AOAM531vDxosb0Qn5FHRpXg/WZSE8FpRA0HA4FihH9iN5V59MuMbMl6v ehQSYjkOxEr8xfrgwE+WsoM= X-Received: by 2002:a17:90a:4a8a:: with SMTP id f10mr3424851pjh.195.1630390500318; Mon, 30 Aug 2021 23:15:00 -0700 (PDT) Received: from sanitydock.wifi-cloud.jp ([210.160.217.69]) by smtp.gmail.com with ESMTPSA id z7sm1405724pjr.42.2021.08.30.23.14.55 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 30 Aug 2021 23:14:59 -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, skhan@linuxfoundation.org, gregkh@linuxfoundation.org, linux-kernel-mentees@lists.linuxfoundation.org, linux-media@vger.kernel.org, linaro-mm-sig@lists.linaro.org, Daniel Vetter Subject: [PATCH v9 3/4] drm: lock drm_global_mutex earlier in the ioctl handler Date: Tue, 31 Aug 2021 14:13:47 +0800 Message-Id: <20210831061348.97696-4-desmondcheongzx@gmail.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20210831061348.97696-1-desmondcheongzx@gmail.com> References: <20210831061348.97696-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 Reviewed-by: Daniel Vetter --- drivers/gpu/drm/drm_ioctl.c | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/drivers/gpu/drm/drm_ioctl.c b/drivers/gpu/drm/drm_ioctl.c index d25713b09b80..59c5aa850dd5 100644 --- a/drivers/gpu/drm/drm_ioctl.c +++ b/drivers/gpu/drm/drm_ioctl.c @@ -767,24 +767,27 @@ long drm_ioctl_kernel(struct file *file, drm_ioctl_t *func, void *kdata, { struct drm_file *file_priv = file->private_data; struct drm_device *dev = file_priv->minor->dev; + bool locked_ioctl; int retcode; if (drm_dev_is_unplugged(dev)) return -ENODEV; + /* Enforce sane locking for modern driver ioctls. */ + locked_ioctl = (unlikely(drm_core_check_feature(dev, DRIVER_LEGACY)) && + !(flags & DRM_UNLOCKED)); + if (locked_ioctl) + 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 (locked_ioctl) mutex_unlock(&drm_global_mutex); - } return retcode; } EXPORT_SYMBOL(drm_ioctl_kernel); -- 2.25.1