From patchwork Fri Aug 25 17:52:20 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Jason Ekstrand X-Patchwork-Id: 9922589 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 92D7D600C5 for ; Fri, 25 Aug 2017 17:52:42 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 814CF28420 for ; Fri, 25 Aug 2017 17:52:42 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 75F0628429; Fri, 25 Aug 2017 17:52:42 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-4.1 required=2.0 tests=BAYES_00,DKIM_SIGNED, RCVD_IN_DNSWL_MED,T_DKIM_INVALID autolearn=ham version=3.3.1 Received: from gabe.freedesktop.org (gabe.freedesktop.org [131.252.210.177]) (using TLSv1.2 with cipher DHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id 2F50828420 for ; Fri, 25 Aug 2017 17:52:42 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id 3DCBE6E8DC; Fri, 25 Aug 2017 17:52:40 +0000 (UTC) X-Original-To: dri-devel@lists.freedesktop.org Delivered-To: dri-devel@lists.freedesktop.org Received: from mail-pg0-x244.google.com (mail-pg0-x244.google.com [IPv6:2607:f8b0:400e:c05::244]) by gabe.freedesktop.org (Postfix) with ESMTPS id E5FDA6E8DB for ; Fri, 25 Aug 2017 17:52:38 +0000 (UTC) Received: by mail-pg0-x244.google.com with SMTP id 63so675752pgc.1 for ; Fri, 25 Aug 2017 10:52:38 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=jlekstrand-net.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=Ejjwx/YiSr5WH+JtlZBckFcckJ3Kcr6GyxMAAIt4EjQ=; b=G0gj82GuPc5oXkmnu4xE1y/kDZyF1hQWn9SImChJbQsOzSqUt34P5UV4Z3vjE//kro nzz2MOQCSdz0RNq7WVk7EdrWe9ot1xBUciZeyU3qNA3kBBYrGQIuEpxv7u4az1VBesKC fCF0kCrco9tIbiXbtuviXkVr/rs6/SYzX97f7CxejARhBU4TxI9l45ooBjOZjeiA5FG1 zmJyWpl9c/zVXaihof54fk1T/AhctluTv9ik61ZRh5lvZdhNQSrW5fakGJbOypPaKX1G +IE5oexWENph1i3fDT9U8K+H2zXF6/ECWAJPx4aCRFEX/XFJptcaZekvgR4j8slXJhb+ tHXQ== 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=Ejjwx/YiSr5WH+JtlZBckFcckJ3Kcr6GyxMAAIt4EjQ=; b=o/8pxpuXkkorTitHOrUq5bw1Rg1auOaXotOWpkNevGfZ4P8JCc4RhEX5Mx7E3JpsEs yq+d21RODmX7QxoOpP7QL408pK0/TCiDohplJptjrQGSuu6zV8yjU5vE/W2qYzDSaZUO 2gLD4pvsVsv2q9OkFExDfuG3U0M0UIXev4JqBhi+0/+uHxcvETHlEitkdavaiTGejEez PblJe4e7zD3gEzYPnVgIaw7EaA64Lyf4Rw3pZn+szcozIu8McoJd4aHglz3IWt2GTmrJ sBBWPIE6Hc43qkkq6BRs5cnkOESCH2bNSdNkPgzWlUWW6Dm6MWDBuC9UVPGaNTvxXyH1 UuQQ== X-Gm-Message-State: AHYfb5hzaK48cwlVCBBqu8yt/EGnhVshOw7/gvrg6ZudS0MqUCJ8kdyn 8q/miYqve8uiEmeBcPfyCQ== X-Received: by 10.84.191.131 with SMTP id a3mr11663488pld.182.1503683558195; Fri, 25 Aug 2017 10:52:38 -0700 (PDT) Received: from omlet.jlekstrand.net (static-50-43-41-117.bvtn.or.frontiernet.net. [50.43.41.117]) by smtp.gmail.com with ESMTPSA id z8sm11024919pgs.25.2017.08.25.10.52.36 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Fri, 25 Aug 2017 10:52:37 -0700 (PDT) From: Jason Ekstrand X-Google-Original-From: Jason Ekstrand To: dri-devel@lists.freedesktop.org Subject: [PATCH 02/10] drm/syncobj: Add a race-free drm_syncobj_fence_get helper (v2) Date: Fri, 25 Aug 2017 10:52:20 -0700 Message-Id: <1503683548-23548-2-git-send-email-jason.ekstrand@intel.com> X-Mailer: git-send-email 2.5.0.400.gff86faf In-Reply-To: <1503683548-23548-1-git-send-email-jason.ekstrand@intel.com> References: <1503683548-23548-1-git-send-email-jason.ekstrand@intel.com> MIME-Version: 1.0 Cc: Jason Ekstrand , Jason Ekstrand X-BeenThere: dri-devel@lists.freedesktop.org X-Mailman-Version: 2.1.18 Precedence: list List-Id: Direct Rendering Infrastructure - Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: dri-devel-bounces@lists.freedesktop.org Sender: "dri-devel" X-Virus-Scanned: ClamAV using ClamSMTP The atomic exchange operation in drm_syncobj_replace_fence is sufficient for the case where it races with itself. However, if you have a race between a replace_fence and dma_fence_get(syncobj->fence), you may end up with the entire replace_fence happening between the point in time where the one thread gets the syncobj->fence pointer and when it calls dma_fence_get() on it. If this happens, then the reference may be dropped before we get a chance to get a new one. The new helper uses dma_fence_get_rcu_safe to get rid of the race. This is also needed because it allows us to do a bit more than just get a reference in drm_syncobj_fence_get should we wish to do so. v2: - RCU isn't that scary - Call rcu_read_lock/unlock - Don't rename fence to _fence - Make the helper static inline Signed-off-by: Jason Ekstrand Acked-by: Christian König (v1) --- drivers/gpu/drm/drm_syncobj.c | 2 +- include/drm/drm_syncobj.h | 12 ++++++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/drivers/gpu/drm/drm_syncobj.c b/drivers/gpu/drm/drm_syncobj.c index 0412b0b..eea38d8 100644 --- a/drivers/gpu/drm/drm_syncobj.c +++ b/drivers/gpu/drm/drm_syncobj.c @@ -105,7 +105,7 @@ int drm_syncobj_find_fence(struct drm_file *file_private, if (!syncobj) return -ENOENT; - *fence = dma_fence_get(syncobj->fence); + *fence = drm_syncobj_fence_get(syncobj); if (!*fence) { ret = -EINVAL; } diff --git a/include/drm/drm_syncobj.h b/include/drm/drm_syncobj.h index 7d4ad77..ce94d14 100644 --- a/include/drm/drm_syncobj.h +++ b/include/drm/drm_syncobj.h @@ -77,6 +77,18 @@ drm_syncobj_put(struct drm_syncobj *obj) kref_put(&obj->refcount, drm_syncobj_free); } +static inline struct dma_fence * +drm_syncobj_fence_get(struct drm_syncobj *syncobj) +{ + struct dma_fence *fence; + + rcu_read_lock(); + fence = dma_fence_get_rcu_safe(&syncobj->fence); + rcu_read_unlock(); + + return fence; +} + struct drm_syncobj *drm_syncobj_find(struct drm_file *file_private, u32 handle); void drm_syncobj_replace_fence(struct drm_syncobj *syncobj,