From patchwork Thu Aug 22 06:54:57 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Daniel Vetter X-Patchwork-Id: 11108515 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 D0790912 for ; Thu, 22 Aug 2019 06:55:09 +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 B929E233A1 for ; Thu, 22 Aug 2019 06:55:09 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org B929E233A1 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 E032E6E9D9; Thu, 22 Aug 2019 06:55:06 +0000 (UTC) X-Original-To: dri-devel@lists.freedesktop.org Delivered-To: dri-devel@lists.freedesktop.org Received: from mail-ed1-x542.google.com (mail-ed1-x542.google.com [IPv6:2a00:1450:4864:20::542]) by gabe.freedesktop.org (Postfix) with ESMTPS id 2F7C36E9D9 for ; Thu, 22 Aug 2019 06:55:05 +0000 (UTC) Received: by mail-ed1-x542.google.com with SMTP id h8so6362292edv.7 for ; Wed, 21 Aug 2019 23:55:05 -0700 (PDT) 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=N+Buk3ha0B02jHDXKZTLWVYSh1JZs7Ql7xpkMVKtYFk=; b=Y0CiSEm6n7leJu+b0ny65uNK5VfZJ7fPzxtRK302gjagS247XuoHIvW+eqp+6kCX/2 4SGYjDR5vlGJFhu1EQHz4J4+IbLD391JcO5y2WIsgf9P5mCVqpi1Nwcn58ZwoLHmANeW tlWi+ZI310pVUZXjzQG/QlLfomhlGStti8ZE36FMe9LixIFaQgrfKMEZDESK9Ry4Qfy/ Kh8t8/vj5P70OE2DTCoB9+nPgwv2t6mg0cPNCEzuNnY93BEUFfyg+dGyRD+bzmmxJdzS RS/wpFnopz0BkoUc7XYT8BvLnE/ghYk7RQqtYH/Dr64B06jc4kJcG2oQfnGnD4s0ksih Pk+A== X-Gm-Message-State: APjAAAXNuZfRRGqLCNi3RC2yS4SPw4cfU9uyYMUYtUC4TyuG1xhIXmj/ /mtmRPt5ZBSauI4vTUSURvIcHXS64dviJA== X-Google-Smtp-Source: APXvYqwN7Y41/okhwjrl4I1WprxzYPPW8IfcpdaTzNNPAj0BS+vSycFkdXqGxCvB4yL2mxRvW1fvSw== X-Received: by 2002:a50:86bb:: with SMTP id r56mr40019434eda.217.1566456903521; Wed, 21 Aug 2019 23:55:03 -0700 (PDT) Received: from phenom.ffwll.local (212-51-149-96.fiber7.init7.net. [212.51.149.96]) by smtp.gmail.com with ESMTPSA id y9sm4550763eds.49.2019.08.21.23.55.02 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 21 Aug 2019 23:55:02 -0700 (PDT) From: Daniel Vetter To: DRI Development Subject: [PATCH] dma_resv: prime lockdep annotations Date: Thu, 22 Aug 2019 08:54:57 +0200 Message-Id: <20190822065457.2830-1-daniel.vetter@ffwll.ch> X-Mailer: git-send-email 2.23.0.rc1 In-Reply-To: <20190821215030.31660-1-daniel.vetter@ffwll.ch> References: <20190821215030.31660-1-daniel.vetter@ffwll.ch> 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:in-reply-to:references :mime-version:content-transfer-encoding; bh=N+Buk3ha0B02jHDXKZTLWVYSh1JZs7Ql7xpkMVKtYFk=; b=O7k7QohBn/pC2KaX8PVbQDJqL0+5jNO54sbQ/zp6QPs8d2ktmf3Uf6IRA+Kly7NWAH 3f9hYfrElC4r4vDRB7UQ3IguqS5vJilfg12n7e33D1n+Qsf3xNqaBZzCry1nP9Nb+jyG WwzrB4Y83K8JotEq+eYOfm5XZwThSiduY5fyk= 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. 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 Signed-off-by: Daniel Vetter Reviewed-by: Chris Wilson Tested-by: Chris Wilson Reviewed-by: Christian König --- 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 42a8f3f11681..d233ef4cf0d7 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 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