Received: by 2002:a05:6a10:1d13:0:0:0:0 with SMTP id pp19csp4266585pxb; Tue, 31 Aug 2021 00:29:05 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxcb14SAHeXgkmcErXmklCn2iNN68WTHNHNNmUyGQidTXiAlGNfUZK90twepxmwwROkM4Xs X-Received: by 2002:aa7:ca14:: with SMTP id y20mr28493452eds.2.1630394945246; Tue, 31 Aug 2021 00:29:05 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1630394945; cv=none; d=google.com; s=arc-20160816; b=EZ0pQ0wRl3SJu30UdqrU0Fp+JDXqJXEcVXMWI3V9DfNdzl7h95SSl4Q85CWTo/n8Pw Clwo3+skI65IbiOvHxMeWMNCh3bjocWkWAUNN5c2Hv7qpFIKEYruee04hFdFGZL2Nph5 CN3Wmx4/qgBOryE4aouEUiTW1l38GYgnSr3Jrk8ZB5UOp4WRG51mEiuyuR0BMGupetex J29EvSYvE3NaJxb477zr9/B1iVM4+T0vJ0AyLuUqIenPZmiGYW/VygXdOTPs2BBmZEq2 OlPo3XZ2fmvVNgrUpNL37p/fh58xF8FvS8MncxhfIQ4iE/RWv8LKwtjIC+Z5RtQaxwt/ zcQQ== 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=FzExCBQE7rwMYVKetJ9aGi6+oVAsZBZjfgGFXxeHd5E=; b=yf54sMWVOAQ/Y4aTx/yzXBTwW7oVDArvItc2glnCu7524JD4fIbin21FcCi/UrJTEi 9AfLk2TNZ9vV9zCD/0s0G5qBvFaATbXRxKJPyTow5d74b9IDyMNPCLVMpy5xAqe1Ax41 BzxyN2rYTU+Oc/+D7O5SW+5sGSphGr7tzY4K0Vzci2+asDmRgeQAc53plT8fYWyHyIbJ VvZtXCd+bi4KWdcRpHb63C4xS4xSqWKq19U2TLkzNqMGBjo9GFuqy4OQW8MIfmsgx0iU LM+j6xrAoG+gHmWXHfdcTTjtvkCriYTcK6nA1Qycry8OvbLSircA/VH50d55GMHvi+Af Lbeg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=LXe0f4nP; 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 k23si18981558edj.270.2021.08.31.00.28.42; Tue, 31 Aug 2021 00:29:05 -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=LXe0f4nP; 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 S239969AbhHaH0o (ORCPT + 99 others); Tue, 31 Aug 2021 03:26:44 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53862 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S239965AbhHaH0n (ORCPT ); Tue, 31 Aug 2021 03:26:43 -0400 Received: from mail-pl1-x62e.google.com (mail-pl1-x62e.google.com [IPv6:2607:f8b0:4864:20::62e]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C84B6C061575; Tue, 31 Aug 2021 00:25:48 -0700 (PDT) Received: by mail-pl1-x62e.google.com with SMTP id x16so8423635pll.2; Tue, 31 Aug 2021 00:25:48 -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=FzExCBQE7rwMYVKetJ9aGi6+oVAsZBZjfgGFXxeHd5E=; b=LXe0f4nPVCe1oh3x5+e3cE7j/bEYgV/8zc80PbZ6LXQLG2n57Lx1fHt9akzFhqF06M vTvHuTvzUYHRxqmg69BlMsboB6VGAAGly8qOMokYCtmYecsc/TOUv+8JqRokhbx5GxOf 7AygM2bgBVxhQiCJ+s58MVLwLzRG/hbTFKHxlKIBKZrVwYd8An/rad3jdSNesX1cumJH kim90j9snnZU2/XgbD1BGQHgdYI+34sJTM6Na8n8uyhqGf+YOlaw6x6qj5shKYDbrYDE /gEJcfnkwnbfBUfokZXQMndj6Ux36lZJFc0ABmo8rh6Fp59spcjw6h7Gh/LyKq5bjNxX cypw== 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=FzExCBQE7rwMYVKetJ9aGi6+oVAsZBZjfgGFXxeHd5E=; b=NyPnr+2SfD+QFZsfmUvn87mrY2DFytXtfe9EUyH7ADZB61mK0XjJZSGACmLYXi6jY/ +e6cSAoBS/ibvBvR7XmlW1hulVVQPuFfmbKop0Maj/8L1TnvOYkDkB/qu9TDRoFq3mOi 3sIlWmh8zL6jBBuT1ESZjV99wgFdgJUJsmn+vzULixkhw9Zil583k4Bly2Dh5yccI+pE WM4r6cYimlJTb0BKQOEBDu2x4UuF5d06x/Edtrb5/p4SipcZXkOUi9csKShQlwvJNVtl S7JKxkIYfEBA8HgWlk2s4yYHQH7XwYFw95O76p1lLohZA7tTjCJSmGVGO5vNaaHzf94Y 2Nig== X-Gm-Message-State: AOAM533v9ii4MNG15EqGwDRl4kJy62JMnmqeFOXNl72DaEfqE2yCW8cQ O7odjffzU1TJBuIPsE8OfK8= X-Received: by 2002:a17:90a:e64a:: with SMTP id ep10mr3657825pjb.167.1630394748361; Tue, 31 Aug 2021 00:25:48 -0700 (PDT) Received: from sanitydock.wifi-cloud.jp ([210.160.217.69]) by smtp.gmail.com with ESMTPSA id m11sm1720724pjn.2.2021.08.31.00.25.43 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 31 Aug 2021 00:25:47 -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 v10 3/4] drm: lock drm_global_mutex earlier in the ioctl handler Date: Tue, 31 Aug 2021 15:25:00 +0800 Message-Id: <20210831072501.184211-4-desmondcheongzx@gmail.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20210831072501.184211-1-desmondcheongzx@gmail.com> References: <20210831072501.184211-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 9fc00e36c5d6..fe9c4c0264a9 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