From patchwork Sat Jul 29 15:22:16 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Gustavo Padovan X-Patchwork-Id: 9870189 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 A142A6038F for ; Sun, 30 Jul 2017 04:22:56 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 91CF1285E2 for ; Sun, 30 Jul 2017 04:22:56 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 806CA285E3; Sun, 30 Jul 2017 04:22:56 +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.2 required=2.0 tests=BAYES_00, RCVD_IN_DNSWL_MED 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 A59F2285E6 for ; Sun, 30 Jul 2017 04:22:55 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id 0DD866F656; Sun, 30 Jul 2017 04:21:36 +0000 (UTC) X-Original-To: dri-devel@lists.freedesktop.org Delivered-To: dri-devel@lists.freedesktop.org Received: from mail-qt0-x244.google.com (mail-qt0-x244.google.com [IPv6:2607:f8b0:400d:c0d::244]) by gabe.freedesktop.org (Postfix) with ESMTPS id 6FC8289145 for ; Sat, 29 Jul 2017 16:16:39 +0000 (UTC) Received: by mail-qt0-x244.google.com with SMTP id i19so15061429qte.1 for ; Sat, 29 Jul 2017 09:16:39 -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; bh=Y38HMkhP4n+oNEEh7QVuH5O78/81vk7Mr4c74kaRGbk=; b=ha0ciGpQ3YZWMoNPpFug1VhXxeCqHHTFuOELHD47J3MqZxRcjFjPw70uo+Xz6U95rA /rIckFbP8htSoZaso3mmjcVZKaJO++OtfW1dmojupeNm1I72ehUj2a0Q01IMnXSRjRkp xRYjaDNnZRd7lAidKjzSEJHBy78Whq9o5hIk9pbH6KK74qdK1JjUyKeGt6U9EROANlug I2q1y6YJM1yjNe3kKUypXaYOztAejXYPL9a3FXbioiRJKZgH+hPs9lula+KIvHbdsG7Q cAC8DsFKwdWzqgveABCH6Upw3OTBtr+jLlM6rou/aehNfTO2xGxmnfDIF5sMa4QX3WO2 yzwQ== X-Gm-Message-State: AIVw113JnqvHr5Ch/29ie8p8K0FiL4fXzyae8M0Tgir3rHxQX4Vuwn96 wagLh/ZvRO4w0al6YkQ= X-Received: by 10.200.50.53 with SMTP id x50mr15098255qta.135.1501341744250; Sat, 29 Jul 2017 08:22:24 -0700 (PDT) Received: from localhost.localdomain ([187.35.198.81]) by smtp.gmail.com with ESMTPSA id c204sm8737392qkb.32.2017.07.29.08.22.22 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sat, 29 Jul 2017 08:22:23 -0700 (PDT) From: Gustavo Padovan To: dri-devel@lists.freedesktop.org Subject: [PATCH 2/3] dma-buf/sw_sync: clean up list before signaling the fence Date: Sat, 29 Jul 2017 12:22:16 -0300 Message-Id: <20170729152217.8362-2-gustavo@padovan.org> X-Mailer: git-send-email 2.9.4 In-Reply-To: <20170729152217.8362-1-gustavo@padovan.org> References: <20170729152217.8362-1-gustavo@padovan.org> Cc: Gustavo Padovan 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: , MIME-Version: 1.0 Errors-To: dri-devel-bounces@lists.freedesktop.org Sender: "dri-devel" X-Virus-Scanned: ClamAV using ClamSMTP From: Gustavo Padovan If userspace already dropped its own reference by closing the sw_sync fence fd we might end up in a deadlock where dma_fence_is_signaled_locked() will trigger the release of the fence and thus try to hold the lock to remove the fence from the list. dma_fence_is_signaled_locked() tries to release/free the fence and hold the lock in the process. We fix that by changing the order operation and clean up the list and rb-tree first. v2: Drop the fence get/put dance and manipulate the list first (Chris Wilson) Cc: Chris Wilson Signed-off-by: Gustavo Padovan Reviewed-by: Chris Wilson --- drivers/dma-buf/sw_sync.c | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/drivers/dma-buf/sw_sync.c b/drivers/dma-buf/sw_sync.c index ef0cc08..38cc738 100644 --- a/drivers/dma-buf/sw_sync.c +++ b/drivers/dma-buf/sw_sync.c @@ -213,11 +213,21 @@ static void sync_timeline_signal(struct sync_timeline *obj, unsigned int inc) obj->value += inc; list_for_each_entry_safe(pt, next, &obj->pt_list, link) { - if (!dma_fence_is_signaled_locked(&pt->base)) + if (!timeline_fence_signaled(&pt->base)) break; list_del_init(&pt->link); rb_erase(&pt->node, &obj->pt_tree); + + /* + * A signal callback may release the last reference to this + * fence, causing it to be freed. That operation has to be + * last to avoid a use after free inside this loop, and must + * be after we remove the fence from the timeline in order to + * prevent deadlocking on timeline->lock inside + * timeline_fence_release(). + */ + dma_fence_signal_locked(&pt->base); } spin_unlock_irq(&obj->lock);