From patchwork Mon Nov 4 17:37:59 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Daniel Vetter X-Patchwork-Id: 11226155 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id A9EFC16B1 for ; Mon, 4 Nov 2019 17:38:15 +0000 (UTC) Received: from gabe.freedesktop.org (gabe.freedesktop.org [131.252.210.177]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 91C1620869 for ; Mon, 4 Nov 2019 17:38:15 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 91C1620869 Authentication-Results: mail.kernel.org; dmarc=none (p=none dis=none) header.from=ffwll.ch Authentication-Results: mail.kernel.org; spf=none smtp.mailfrom=dri-devel-bounces@lists.freedesktop.org Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id AE03F6E794; Mon, 4 Nov 2019 17:38:10 +0000 (UTC) X-Original-To: dri-devel@lists.freedesktop.org Delivered-To: dri-devel@lists.freedesktop.org Received: from mail-wm1-x343.google.com (mail-wm1-x343.google.com [IPv6:2a00:1450:4864:20::343]) by gabe.freedesktop.org (Postfix) with ESMTPS id F08D26E794 for ; Mon, 4 Nov 2019 17:38:08 +0000 (UTC) Received: by mail-wm1-x343.google.com with SMTP id 11so17676655wmk.0 for ; Mon, 04 Nov 2019 09:38:08 -0800 (PST) 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:mime-version :content-transfer-encoding; bh=/uGn0Km0FI86h50wVhYKQwM6gSDKOGpzem2gbRa6Lao=; b=O18zx6HhXSZ0zMgNh11EF4arGVFnHgckSnWnVwv+R0rJKSzjOtq3M2oIlA+XB8I/7B FjVACfZkhcBSxMSi7YUwn5rb44KIegrjs8tM06oa2dGKGLBKMqpLsWmQ4XxF7Qs8FI09 wrduul+uG5OvBMDIYGNuaF12RAcJY77DC2dEx6pWMHvwHBRBxBaL9KJS9aZKH5Vi0Ez9 yXlyGUpBGt73+2z8mReehgmFM2HJudCed0bt/3CMVRnDRj4EncSF9h4UsetCYYBgVV63 63VQMEyCpKU4FpCuHPFZAwz/ae2UOkrFP574H21uosR0Htu2my7zhBqyhLJ/y5jnVrCC 1Rnw== X-Gm-Message-State: APjAAAUtAyln6vhwIvx8Q9132O/ubOyLidWQxTzR4Y84mWL7zkMTJPdW eu/i46b4n9/Ue4eETrA7uSyHe3f04Ms= X-Google-Smtp-Source: APXvYqzbBV4olnOJk+ZGQRiFY+QOFgkktd8pK7ZQ7U+kESTvrZi73lTcfeG5dplu0Ia+9nsLJ3xi6Q== X-Received: by 2002:a7b:c0da:: with SMTP id s26mr261805wmh.6.1572889087288; Mon, 04 Nov 2019 09:38:07 -0800 (PST) Received: from phenom.ffwll.local (212-51-149-96.fiber7.init7.net. [212.51.149.96]) by smtp.gmail.com with ESMTPSA id h140sm17991468wme.22.2019.11.04.09.38.06 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 04 Nov 2019 09:38:06 -0800 (PST) From: Daniel Vetter To: DRI Development Subject: [PATCH 1/3] dma_resv: prime lockdep annotations Date: Mon, 4 Nov 2019 18:37:59 +0100 Message-Id: <20191104173801.2972-1-daniel.vetter@ffwll.ch> X-Mailer: git-send-email 2.24.0.rc2 MIME-Version: 1.0 X-Mailman-Original-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ffwll.ch; s=google; h=from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=/uGn0Km0FI86h50wVhYKQwM6gSDKOGpzem2gbRa6Lao=; b=eISenJRhmEKiiDXafrTX4L4vUNfrQ/M6yO3IN1b871Oqc2CmLKF3DAo3FuDl41ttyb rwLMxPXuTLmWtp/ehYMbuHQ+IYQFBFnnYznC4vEcpOL/MdAOUo4pY3rMtvl7pcdz4bcQ Penk3psoy2yrfNpNjyD6z0twPfizixU5tHzPQ= X-BeenThere: dri-devel@lists.freedesktop.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: Direct Rendering Infrastructure - Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Thomas Hellstrom , Tomeu Vizoso , Daniel Vetter , Intel Graphics Development , VMware Graphics , Gerd Hoffmann , Thomas Zimmermann , Daniel Vetter , Alex Deucher , Dave Airlie , =?utf-8?q?Christian_K=C3=B6nig?= , Ben Skeggs Errors-To: dri-devel-bounces@lists.freedesktop.org Sender: "dri-devel" Full audit of everyone: - i915, radeon, amdgpu should be clean per their maintainers. - vram helpers should be fine, they don't do command submission, so really no business holding struct_mutex while doing copy_*_user. But I haven't checked them all. - panfrost seems to dma_resv_lock only in panfrost_job_push, which looks clean. - v3d holds dma_resv locks in the tail of its v3d_submit_cl_ioctl(), copying from/to userspace happens all in v3d_lookup_bos which is outside of the critical section. - vmwgfx has a bunch of ioctls that do their own copy_*_user: - vmw_execbuf_process: First this does some copies in vmw_execbuf_cmdbuf() and also in the vmw_execbuf_process() itself. Then comes the usual ttm reserve/validate sequence, then actual submission/fencing, then unreserving, and finally some more copy_to_user in vmw_execbuf_copy_fence_user. Glossing over tons of details, but looks all safe. - vmw_fence_event_ioctl: No ttm_reserve/dma_resv_lock anywhere to be seen, seems to only create a fence and copy it out. - a pile of smaller ioctl in vmwgfx_ioctl.c, no reservations to be found there. Summary: vmwgfx seems to be fine too. - virtio: There's virtio_gpu_execbuffer_ioctl, which does all the copying from userspace before even looking up objects through their handles, so safe. Plus the getparam/getcaps ioctl, also both safe. - qxl only has qxl_execbuffer_ioctl, which calls into qxl_process_single_command. There's a lovely comment before the __copy_from_user_inatomic that the slowpath should be copied from i915, but I guess that never happened. Try not to be unlucky and get your CS data evicted between when it's written and the kernel tries to read it. The only other copy_from_user is for relocs, but those are done before qxl_release_reserve_list(), which seems to be the only thing reserving buffers (in the ttm/dma_resv sense) in that code. So looks safe. - A debugfs file in nouveau_debugfs_pstate_set() and the usif ioctl in usif_ioctl() look safe. nouveau_gem_ioctl_pushbuf() otoh breaks this everywhere and needs to be fixed up. v2: Thomas pointed at that vmwgfx calls dma_resv_init while it holds a dma_resv lock of a different object already. Christian mentioned that ttm core does this too for ghost objects. intel-gfx-ci highlighted that i915 has similar issues. Unfortunately we can't do this in the usual module init functions, because kernel threads don't have an ->mm - we have to wait around for some user thread to do this. Solution is to spawn a worker (but only once). It's horrible, but it works. v3: We can allocate mm! (Chris). Horrible worker hack out, clean initcall solution in. v4: Annotate with __init (Rob Herring) Cc: Rob Herring Cc: Alex Deucher Cc: Christian König Cc: Chris Wilson Cc: Thomas Zimmermann Cc: Rob Herring Cc: Tomeu Vizoso Cc: Eric Anholt Cc: Dave Airlie Cc: Gerd Hoffmann Cc: Ben Skeggs Cc: "VMware Graphics" Cc: Thomas Hellstrom Reviewed-by: Christian König Reviewed-by: Chris Wilson Tested-by: Chris Wilson Signed-off-by: Daniel Vetter Reviewed-by: Thomas Hellstrom Signed-off-by: Steven Price Reviewed-by: Daniel Vetter --- drivers/dma-buf/dma-resv.c | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/drivers/dma-buf/dma-resv.c b/drivers/dma-buf/dma-resv.c index 709002515550..a05ff542be22 100644 --- a/drivers/dma-buf/dma-resv.c +++ b/drivers/dma-buf/dma-resv.c @@ -34,6 +34,7 @@ #include #include +#include /** * DOC: Reservation Object Overview @@ -95,6 +96,29 @@ static void dma_resv_list_free(struct dma_resv_list *list) kfree_rcu(list, rcu); } +#if IS_ENABLED(CONFIG_LOCKDEP) +static void __init dma_resv_lockdep(void) +{ + struct mm_struct *mm = mm_alloc(); + struct dma_resv obj; + + if (!mm) + return; + + dma_resv_init(&obj); + + down_read(&mm->mmap_sem); + ww_mutex_lock(&obj.lock, NULL); + fs_reclaim_acquire(GFP_KERNEL); + fs_reclaim_release(GFP_KERNEL); + ww_mutex_unlock(&obj.lock); + up_read(&mm->mmap_sem); + + mmput(mm); +} +subsys_initcall(dma_resv_lockdep); +#endif + /** * dma_resv_init - initialize a reservation object * @obj: the reservation object