Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.221.44 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796222113; b=U3jwFz+kj7d/AhREk1BhQvBm2/mBUHyEHhu/xQ39sgjROM6RLmZbcI7r96/Ulp51Li6h 1OVcyAx5ePbzxfhwOTo6G591WYheiSJyQUBBHYIVfObxsNLMrACWuZ/NOcONdASnq16oC Vvsu8Nk9jaclTxW6P7xnG2ZnXOQfJpQDFGwAgNg3XqvPdZW9WLjtK3jBOaTXgPA6RiN/o JtjCo2rwTVxeNkdrYWEgnO8ou5AarvQ9BMBUIpDW9KXFt57RmPoO2SYxjlcl7SMnpOeog bBoCCe4QQL0k+aby9NgbvJ4qUF76FQf/HpshB+AbmWcxbQxjI4H+pYpBxUovKPLkqxQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796222113; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Forwarded-Encrypted:X-Gm-Message-State:X-Gm-Gg:X-Received:Received: From:X-Google-Original-From:To:Cc:Subject:Date:Message-ID:X-Mailer: MIME-Version:Content-Type:Reply-To:Content-Transfer-Encoding; bh=/w58xBKbPkh/hlxwk76ySjsT2BVAV8yDzLN/hCDSliM=; b=vcA9j4eP/ZndKQNCOAZxksRMoph4g7xEPSMa4YIgNt7QRuZIV+cF9QGO4q9oEPowMTB9 KQvt9UEZy9nxQQshTtoArnKpQjmES9m8tXhHfaHR1a7qMwSfPoIEcJ/bieEq86h0TPj/H 1HZu8vB6WaQzjDUcLBo8bhpAWau4Y/HTJNHOZnbH23S9yWsh4TXRgXZAYb4l6yHFqonrG OzTiTL2jXdAaeEzTkxJoiQ6/YUjc9tBMczh90FlNXVpRH203LPXlPOqQj1jdj6LtM7Cxh SsOq+mxeucOQ3mEnPP6QiwKJd29Fsjxj/XTB9IafW5cWmDl0Qb59h4NCmJw9Eq4FrHQ== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=G5s7Mfbg; arc=none smtp.remote-ip=209.85.221.44 Received: from mail-wr1-f44.google.com (mail-wr1-f44.google.com [209.85.221.44]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id CD2E2606A323 for ; Wed, 2 Dec 2026 14:35:12 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org CD2E2606A323 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=G5s7Mfbg Received: by mail-wr1-f44.google.com with SMTP id ffacd0b85a97d-4a358881793so7028846f8f.0 for ; Wed, 02 Dec 2026 06:35:12 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1796222111; x=1796826911; darn=vger.kernel.org; h=content-transfer-encoding:reply-to:content-type:mime-version :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to:content-type; bh=/w58xBKbPkh/hlxwk76ySjsT2BVAV8yDzLN/hCDSliM=; b=G5s7Mfbg72hhk4pEJkTA4gvfzA/MHaLWn4tJd+t7xfu2darDyus4cY7T38VZeOtqf1 n17aZn1yKxnm/Y+C0axFPOeuyjqPwWK9yAKUaG9domJx80MXL1j+RObWjobuyYF8DTVU RBXJyCCPkknDGA3aJSrGvuORJmkJIX3/2ln+mVofDVTIm3MW353qSKupQeq9k9JJdKl8 tWgpuBQRTKLKOKoydO6DGpcuiR6lH4Z1KTFpkmYKagKgiz/guyMOGNL0ba9bHqpGUNF5 vMfTo6inmnanZCYouWDaTkAnrgiPDWAgdEFv/JQQffdyv6ifNKxuEpNnnvTz3GL3VftX AskA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796222111; x=1796826911; h=content-transfer-encoding:reply-to:content-type:mime-version :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to:content-type; bh=/w58xBKbPkh/hlxwk76ySjsT2BVAV8yDzLN/hCDSliM=; b=PGJOFWRplXf+n/l6mT8DJvR0ujBzBp/3Bd5vB3LDAFLiayzfytLp7aX0WctNfJYNZ1 /k+nPpTuS/IDL1zhsmygVVcVywJHPZLGLuC6wnyIHFxR13T7Q6E85dHbe7OXGn8y5xZK iNqiSZBhyVcz8DWj6Yr8BPebiSVkyN1YrIve1sw1dQVjwElvWDnOiHhRFA2eP2x1cuQB XNBcoTAogCnH6+PqEOSqsekOKRj02ih9RUltI09OA1Y4+U5V6sgcrC83GR1p6/MkSVxC 8YCCdzPhNGCmjwUlnTwk+KgNzAmFSFtT/lCtHMzP1XUPW174ONRF1z0Y80Cemxbphxur IU0Q== X-Forwarded-Encrypted: i=1; AL+hErEk+ZDEm6ZWFkbha9hY+TNIqCyJu+dAucGAKhHdcYKedpL4s5FsoxcZEU4uGgepsnd6nkOI3hMXILwd4Q==@vger.kernel.org X-Gm-Message-State: APuU4Z/rBSHdE1BZy8McV9JNtwdmnUbLc8atCzXBiRNrKO/Ti5AsloXF ZJmMTMVy04s9eEq9mfttLE7fe2H1IwB850Guzfecn2IuRaxhi9wV5dUNRSZawnFL X-Gm-Gg: AZFdTZe+FX2JwqqRLV2Zg04PB/iGCseuN3yee009SgmE5Bsvg5fSBCSuUAp8SjmgvBW FkaZnNXmuVL1VGDtY9Ta4BJX2287IsNDWrK4L3KftA2tLvXSjGI/m1rs5bTjKt/WVscq4KkCBYe he9zajAU2yzIztwr/8Qqt4TKEgFboUsSD8f+GiTpkaeEDd/FHMAEXT+3W+SGRn+HAaEqQ/9bdg5 U5Er/RD5RN3b0VS9hvIri3dbLWCLsqmlItfKsbdIFyaehW3Tezk3Bj4EbZvz83VuGR3vNQSFxlp D7BOXc9VVsmepISwSYq3rel8+Lr2Xump2ap6vznyIr7827z2GdNs5WPZpJ0TagdFwJrOYLFjTRe ZjRxp9T4MF1w2eNCBGbo9T62Q0SwWoYftSaojWuHyqOrFpPFtkqjnmUQvRHyPLiRcvJe4ijvRQc TLjA4Vdd5p/8UxfUWK87Bv5tGaaX4tHi8N+JBMKjPNrX4U1b87goHQfxxq9qyDHt1PItutGP4mE 76Q8EhbsryaElJENTKaIN6Zg92DeRy5viKqQ4LehxPT+wvWXji8IHlySPcJom0D+wI8NA9QbXtC 88fk0RvGGgLDsGv8jEn/soZgZPp4AuCO5mj0B0A5jw== X-Received: by 2002:a05:6000:4812:b0:4a7:b8ab:df86 with SMTP id ffacd0b85a97d-4abb5db73bcmr73214482f8f.38.1796222110862; Wed, 02 Dec 2026 06:35:10 -0800 (PST) Received: from able.fritz.box ([2a00:e180:1580:7700:c63f:50ec:a04c:ce3e]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-4ab6a67f204sm97464273f8f.21.2026.12.02.06.35.09 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 02 Dec 2026 06:35:10 -0800 (PST) From: "=?UTF-8?q?Christian=20K=C3=B6nig?=" X-Google-Original-From: =?UTF-8?q?Christian=20K=C3=B6nig?= To: boris.brezillon@collabora.com, tursulin@ursulin.net, phasta@mailbox.org, dakr@kernel.org, erhan.bilgili@gmail.com, sumit.semwal@linaro.org, alessio.belle@imgtec.com, luigi.santivetti@imgtec.com, thierry.reding@kernel.org, mperttunen@nvidia.com Cc: dri-devel@lists.freedesktop.org, imagination@lists.freedesktop.org, linux-tegra@vger.kernel.org, virtualization@lists.linux.dev Subject: [PATCH] dma-buf: improve robustness of dma_fence_unwrap_for_each v2 Date: Wed, 2 Dec 2026 15:35:08 +0100 Message-ID: <20261202143509.6903-1-christian.koenig@amd.com> X-Mailer: git-send-email 2.43.0 X-Mailing-List: linux-tegra@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Reply-To: christian.koenig@amd.com Content-Transfer-Encoding: 8bit The macro dma_fence_unwrap_for_each() is used to deep dive into fences in chain and array containers. To guarantee correct functionality it does this by keeping a reference to chain elements in the iterator structure. But so far there was no well defined cleanup procedure for the iterator when breaking out of the loop early. Some code implemented dropping the reference manually, but Erhan now found another issue in an error path in amdgpu. Fix this for good, provide a proper cleanup function which is automatically used with a guard and remove the workarounds spread around in the calling code. v2: make the cleanup function inline (Boris), cleanup Tegra code and add another kerneldoc sentence about reference lifetime (Tvrtko) There is a conflict with drm-xe-next Sashiko pointed out but that can only be solved after pushing to drm-misc-next. Signed-off-by: Christian König Reported-by: Erhan Bilgili Reviewed-by: Boris Brezillon --- drivers/dma-buf/dma-buf.c | 5 +- drivers/dma-buf/dma-fence-unwrap.c | 47 ++++++++-------- drivers/dma-buf/st-dma-fence-unwrap.c | 27 ++++------ drivers/dma-buf/sync_file.c | 5 +- .../gpu/drm/amd/amdgpu/amdgpu_userq_fence.c | 6 +-- drivers/gpu/drm/imagination/pvr_sync.c | 14 ++--- drivers/gpu/drm/tegra/submit.c | 21 +++----- drivers/gpu/drm/virtio/virtgpu_submit.c | 7 +-- include/linux/dma-fence-unwrap.h | 54 +++++++++++++------ 9 files changed, 88 insertions(+), 98 deletions(-) diff --git a/drivers/dma-buf/dma-buf.c b/drivers/dma-buf/dma-buf.c index 5c6957f8839a..098d22d1a628 100644 --- a/drivers/dma-buf/dma-buf.c +++ b/drivers/dma-buf/dma-buf.c @@ -494,7 +494,6 @@ static long dma_buf_import_sync_file(struct dma_buf *dmabuf, struct dma_buf_import_sync_file arg; struct dma_fence *fence, *f; enum dma_resv_usage usage; - struct dma_fence_unwrap iter; unsigned int num_fences; int ret = 0; @@ -515,7 +514,7 @@ static long dma_buf_import_sync_file(struct dma_buf *dmabuf, DMA_RESV_USAGE_READ; num_fences = 0; - dma_fence_unwrap_for_each(f, &iter, fence) + dma_fence_unwrap_for_each(f, fence) ++num_fences; if (num_fences > 0) { @@ -523,7 +522,7 @@ static long dma_buf_import_sync_file(struct dma_buf *dmabuf, ret = dma_resv_reserve_fences(dmabuf->resv, num_fences); if (!ret) { - dma_fence_unwrap_for_each(f, &iter, fence) + dma_fence_unwrap_for_each(f, fence) dma_resv_add_fence(dmabuf->resv, f, usage); } diff --git a/drivers/dma-buf/dma-fence-unwrap.c b/drivers/dma-buf/dma-fence-unwrap.c index 120fe0c2a75a..845a6fcaadcc 100644 --- a/drivers/dma-buf/dma-fence-unwrap.c +++ b/drivers/dma-buf/dma-fence-unwrap.c @@ -15,52 +15,54 @@ #include /* Internal helper to start new array iteration, don't use directly */ -static struct dma_fence * -__dma_fence_unwrap_array(struct dma_fence_unwrap *cursor) +static void __dma_fence_unwrap_array(struct dma_fence_unwrap *cursor, + struct dma_fence **fence) { cursor->array = dma_fence_chain_contained(cursor->chain); cursor->index = 0; - return dma_fence_array_first(cursor->array); + *fence = dma_fence_array_first(cursor->array); } /** * dma_fence_unwrap_first - return the first fence from fence containers * @head: the entrypoint into the containers - * @cursor: current position inside the containers + * @fence: pointer to the resulting fence * * Unwraps potential dma_fence_chain/dma_fence_array containers and return the - * first fence. + * initialized cursor structure. */ -struct dma_fence *dma_fence_unwrap_first(struct dma_fence *head, - struct dma_fence_unwrap *cursor) +struct dma_fence_unwrap dma_fence_unwrap_first(struct dma_fence *head, + struct dma_fence **fence) { - cursor->chain = dma_fence_get(head); - return __dma_fence_unwrap_array(cursor); + struct dma_fence_unwrap tmp; + + tmp.chain = dma_fence_get(head); + __dma_fence_unwrap_array(&tmp, fence); + return tmp; } EXPORT_SYMBOL_GPL(dma_fence_unwrap_first); /** * dma_fence_unwrap_next - return the next fence from a fence containers * @cursor: current position inside the containers + * @fence: pointer to the resulting fence * - * Continue unwrapping the dma_fence_chain/dma_fence_array containers and return - * the next fence from them. + * Continue unwrapping the dma_fence_chain/dma_fence_array containers and update + * the pointer to the resulting fence. */ -struct dma_fence *dma_fence_unwrap_next(struct dma_fence_unwrap *cursor) +void dma_fence_unwrap_next(struct dma_fence_unwrap *cursor, + struct dma_fence **fence) { - struct dma_fence *tmp; - ++cursor->index; - tmp = dma_fence_array_next(cursor->array, cursor->index); - if (tmp) - return tmp; + *fence = dma_fence_array_next(cursor->array, cursor->index); + if (*fence) + return; cursor->chain = dma_fence_chain_walk(cursor->chain); - return __dma_fence_unwrap_array(cursor); + __dma_fence_unwrap_array(cursor, fence); } EXPORT_SYMBOL_GPL(dma_fence_unwrap_next); - static int fence_cmp(const void *_a, const void *_b) { struct dma_fence *a = *(struct dma_fence **)_a; @@ -119,8 +121,7 @@ EXPORT_SYMBOL_GPL(dma_fence_dedup_array); /* Implementation for the dma_fence_merge() marco, don't use directly */ struct dma_fence *__dma_fence_unwrap_merge(size_t num_fences, - struct dma_fence **fences, - struct dma_fence_unwrap *iter) + struct dma_fence **fences) { struct dma_fence *tmp, *unsignaled = NULL, **array; struct dma_fence_array *result; @@ -130,7 +131,7 @@ struct dma_fence *__dma_fence_unwrap_merge(size_t num_fences, count = 0; timestamp = ns_to_ktime(0); for (i = 0; i < num_fences; ++i) { - dma_fence_unwrap_for_each(tmp, &iter[i], fences[i]) { + dma_fence_unwrap_for_each(tmp, fences[i]) { if (!dma_fence_is_signaled(tmp)) { dma_fence_put(unsignaled); unsignaled = dma_fence_get(tmp); @@ -164,7 +165,7 @@ struct dma_fence *__dma_fence_unwrap_merge(size_t num_fences, count = 0; for (i = 0; i < num_fences; ++i) { - dma_fence_unwrap_for_each(tmp, &iter[i], fences[i]) { + dma_fence_unwrap_for_each(tmp, fences[i]) { if (!dma_fence_is_signaled(tmp)) { array[count++] = dma_fence_get(tmp); } else { diff --git a/drivers/dma-buf/st-dma-fence-unwrap.c b/drivers/dma-buf/st-dma-fence-unwrap.c index c5353b36f0e4..e754133afbdf 100644 --- a/drivers/dma-buf/st-dma-fence-unwrap.c +++ b/drivers/dma-buf/st-dma-fence-unwrap.c @@ -117,7 +117,6 @@ static void test_sanitycheck(struct kunit *test) static void test_unwrap_array(struct kunit *test) { struct dma_fence *fence, *f1, *f2, *array; - struct dma_fence_unwrap iter; f1 = mock_fence(); KUNIT_ASSERT_NOT_NULL(test, f1); @@ -136,7 +135,7 @@ static void test_unwrap_array(struct kunit *test) array = mock_array(2, f1, f2); KUNIT_ASSERT_NOT_NULL(test, array); - dma_fence_unwrap_for_each(fence, &iter, array) { + dma_fence_unwrap_for_each(fence, array) { if (fence == f1) { f1 = NULL; } else if (fence == f2) { @@ -155,7 +154,6 @@ static void test_unwrap_array(struct kunit *test) static void test_unwrap_chain(struct kunit *test) { struct dma_fence *fence, *f1, *f2, *chain; - struct dma_fence_unwrap iter; f1 = mock_fence(); KUNIT_ASSERT_NOT_NULL(test, f1); @@ -174,7 +172,7 @@ static void test_unwrap_chain(struct kunit *test) chain = mock_chain(f1, f2); KUNIT_ASSERT_NOT_NULL(test, chain); - dma_fence_unwrap_for_each(fence, &iter, chain) { + dma_fence_unwrap_for_each(fence, chain) { if (fence == f1) { f1 = NULL; } else if (fence == f2) { @@ -193,7 +191,6 @@ static void test_unwrap_chain(struct kunit *test) static void test_unwrap_chain_array(struct kunit *test) { struct dma_fence *fence, *f1, *f2, *array, *chain; - struct dma_fence_unwrap iter; f1 = mock_fence(); KUNIT_ASSERT_NOT_NULL(test, f1); @@ -215,7 +212,7 @@ static void test_unwrap_chain_array(struct kunit *test) chain = mock_chain(NULL, array); KUNIT_ASSERT_NOT_NULL(test, chain); - dma_fence_unwrap_for_each(fence, &iter, chain) { + dma_fence_unwrap_for_each(fence, chain) { if (fence == f1) { f1 = NULL; } else if (fence == f2) { @@ -234,7 +231,6 @@ static void test_unwrap_chain_array(struct kunit *test) static void test_unwrap_merge(struct kunit *test) { struct dma_fence *fence, *f1, *f2, *f3; - struct dma_fence_unwrap iter; f1 = mock_fence(); KUNIT_ASSERT_NOT_NULL(test, f1); @@ -255,7 +251,7 @@ static void test_unwrap_merge(struct kunit *test) goto error_put_f2; } - dma_fence_unwrap_for_each(fence, &iter, f3) { + dma_fence_unwrap_for_each(fence, f3) { if (fence == f1) { dma_fence_put(f1); f1 = NULL; @@ -280,7 +276,6 @@ static void test_unwrap_merge(struct kunit *test) static void test_unwrap_merge_duplicate(struct kunit *test) { struct dma_fence *fence, *f1, *f2; - struct dma_fence_unwrap iter; f1 = mock_fence(); KUNIT_ASSERT_NOT_NULL(test, f1); @@ -293,7 +288,7 @@ static void test_unwrap_merge_duplicate(struct kunit *test) goto error_put_f1; } - dma_fence_unwrap_for_each(fence, &iter, f2) { + dma_fence_unwrap_for_each(fence, f2) { if (fence == f1) { dma_fence_put(f1); f1 = NULL; @@ -313,7 +308,6 @@ static void test_unwrap_merge_duplicate(struct kunit *test) static void test_unwrap_merge_seqno(struct kunit *test) { struct dma_fence *fence, *f1, *f2, *f3, *f4; - struct dma_fence_unwrap iter; u64 ctx[2]; ctx[0] = dma_fence_context_alloc(1); @@ -346,7 +340,7 @@ static void test_unwrap_merge_seqno(struct kunit *test) goto error_put_f3; } - dma_fence_unwrap_for_each(fence, &iter, f4) { + dma_fence_unwrap_for_each(fence, f4) { if (fence == f3 && f2) { dma_fence_put(f3); f3 = NULL; @@ -382,7 +376,6 @@ static void test_dedup_empty(struct kunit *test) static void test_unwrap_merge_order(struct kunit *test) { struct dma_fence *fence, *f1, *f2, *a1, *a2, *c1, *c2; - struct dma_fence_unwrap iter; f1 = mock_fence(); KUNIT_ASSERT_NOT_NULL(test, f1); @@ -419,7 +412,7 @@ static void test_unwrap_merge_order(struct kunit *test) */ a2 = dma_fence_unwrap_merge(a1, c2); - dma_fence_unwrap_for_each(fence, &iter, a2) { + dma_fence_unwrap_for_each(fence, a2) { if (fence == f1) { f1 = NULL; if (!f2) @@ -446,7 +439,6 @@ static void test_unwrap_merge_order(struct kunit *test) static void test_unwrap_merge_complex(struct kunit *test) { struct dma_fence *fence, *f1, *f2, *f3, *f4, *f5; - struct dma_fence_unwrap iter; f1 = mock_fence(); KUNIT_ASSERT_NOT_NULL(test, f1); @@ -481,7 +473,7 @@ static void test_unwrap_merge_complex(struct kunit *test) goto error_put_f4; } - dma_fence_unwrap_for_each(fence, &iter, f5) { + dma_fence_unwrap_for_each(fence, f5) { if (fence == f1) { dma_fence_put(f1); f1 = NULL; @@ -510,7 +502,6 @@ static void test_unwrap_merge_complex(struct kunit *test) static void test_unwrap_merge_complex_seqno(struct kunit *test) { struct dma_fence *fence, *f1, *f2, *f3, *f4, *f5, *f6, *f7; - struct dma_fence_unwrap iter; u64 ctx[2]; ctx[0] = dma_fence_context_alloc(1); @@ -563,7 +554,7 @@ static void test_unwrap_merge_complex_seqno(struct kunit *test) goto error_put_f6; } - dma_fence_unwrap_for_each(fence, &iter, f7) { + dma_fence_unwrap_for_each(fence, f7) { if (fence == f1 && f4) { dma_fence_put(f1); f1 = NULL; diff --git a/drivers/dma-buf/sync_file.c b/drivers/dma-buf/sync_file.c index 2166bbdf7e4a..51e4edb1f44e 100644 --- a/drivers/dma-buf/sync_file.c +++ b/drivers/dma-buf/sync_file.c @@ -296,7 +296,6 @@ static long sync_file_ioctl_fence_info(struct sync_file *sync_file, unsigned long arg) { struct sync_fence_info *fence_info = NULL; - struct dma_fence_unwrap iter; struct sync_file_info info; unsigned int num_fences; struct dma_fence *fence; @@ -310,7 +309,7 @@ static long sync_file_ioctl_fence_info(struct sync_file *sync_file, return -EINVAL; num_fences = 0; - dma_fence_unwrap_for_each(fence, &iter, sync_file->fence) + dma_fence_unwrap_for_each(fence, sync_file->fence) ++num_fences; /* @@ -335,7 +334,7 @@ static long sync_file_ioctl_fence_info(struct sync_file *sync_file, return -ENOMEM; num_fences = 0; - dma_fence_unwrap_for_each(fence, &iter, sync_file->fence) { + dma_fence_unwrap_for_each(fence, sync_file->fence) { int status; status = sync_fill_fence_info(fence, &fence_info[num_fences++]); diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_userq_fence.c b/drivers/gpu/drm/amd/amdgpu/amdgpu_userq_fence.c index b7cebe392108..e125d0580dae 100644 --- a/drivers/gpu/drm/amd/amdgpu/amdgpu_userq_fence.c +++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_userq_fence.c @@ -643,7 +643,6 @@ amdgpu_userq_wait_count_fences(struct drm_file *filp, struct drm_gem_object **gobj_read) { int num_read_bo_handles, num_write_bo_handles; - struct dma_fence_unwrap iter; struct dma_fence *fence, *f; unsigned int num_fences = 0; struct drm_exec exec; @@ -663,7 +662,7 @@ amdgpu_userq_wait_count_fences(struct drm_file *filp, if (r) return r; - dma_fence_unwrap_for_each(f, &iter, fence) + dma_fence_unwrap_for_each(f, fence) num_fences++; dma_fence_put(fence); @@ -758,7 +757,6 @@ amdgpu_userq_wait_return_fence_info(struct drm_device *dev, struct drm_file *fil int num_read_bo_handles, num_write_bo_handles; struct amdgpu_usermode_queue *waitq; struct dma_fence **fences, *fence, *f; - struct dma_fence_unwrap iter; int num_points, num_syncobj; unsigned int num_fences = 0; struct drm_exec exec; @@ -784,7 +782,7 @@ amdgpu_userq_wait_return_fence_info(struct drm_device *dev, struct drm_file *fil if (r) goto free_fences; - dma_fence_unwrap_for_each(f, &iter, fence) { + dma_fence_unwrap_for_each(f, fence) { r = amdgpu_userq_wait_add_fence(wait_info, fences, &num_fences, f); if (r) { diff --git a/drivers/gpu/drm/imagination/pvr_sync.c b/drivers/gpu/drm/imagination/pvr_sync.c index 936f840a5221..ef631008c809 100644 --- a/drivers/gpu/drm/imagination/pvr_sync.c +++ b/drivers/gpu/drm/imagination/pvr_sync.c @@ -205,12 +205,11 @@ pvr_sync_signal_array_push_fences(struct xarray *array) static int pvr_sync_add_dep_to_job(struct drm_sched_job *job, struct dma_fence *f) { - struct dma_fence_unwrap iter; u32 native_fence_count = 0; struct dma_fence *uf; int err = 0; - dma_fence_unwrap_for_each(uf, &iter, f) { + dma_fence_unwrap_for_each(uf, f) { if (pvr_queue_fence_is_native(uf)) native_fence_count++; } @@ -219,14 +218,7 @@ pvr_sync_add_dep_to_job(struct drm_sched_job *job, struct dma_fence *f) if (!native_fence_count) return drm_sched_job_add_dependency(job, f); - dma_fence_unwrap_for_each(uf, &iter, f) { - /* There's no dma_fence_unwrap_stop() helper cleaning up the refs - * owned by dma_fence_unwrap(), so let's just iterate over all - * entries without doing anything when something failed. - */ - if (err) - continue; - + dma_fence_unwrap_for_each(uf, f) { if (pvr_queue_fence_is_native(uf)) { struct drm_sched_fence *s_fence = to_drm_sched_fence(uf); @@ -238,6 +230,8 @@ pvr_sync_add_dep_to_job(struct drm_sched_job *job, struct dma_fence *f) } else { err = drm_sched_job_add_dependency(job, dma_fence_get(uf)); } + if (err) + break; } dma_fence_put(f); diff --git a/drivers/gpu/drm/tegra/submit.c b/drivers/gpu/drm/tegra/submit.c index 5db37856db12..7c8365fb9f1d 100644 --- a/drivers/gpu/drm/tegra/submit.c +++ b/drivers/gpu/drm/tegra/submit.c @@ -353,19 +353,15 @@ static int submit_get_syncpt(struct tegra_drm_context *context, struct host1x_jo static int submit_handle_in_fence(struct tegra_drm_context *context, struct host1x *host1x, struct dma_fence *fence, u32 *num_waits) { - struct dma_fence_unwrap iter; struct host1x *fence_host1x; struct dma_fence *f; - int err = 0; + int err; *num_waits = 0; - dma_fence_unwrap_for_each(f, &iter, fence) { + dma_fence_unwrap_for_each(f, fence) { long wait_err; - if (err) - continue; - if (dma_fence_is_signaled(f)) continue; @@ -378,26 +374,21 @@ static int submit_handle_in_fence(struct tegra_drm_context *context, struct host wait_err = dma_fence_wait_timeout(f, true, msecs_to_jiffies(10000)); if (wait_err == 0) { SUBMIT_ERR(context, "wait for syncobj_in timed out"); - err = -ETIMEDOUT; - continue; + return -ETIMEDOUT; } else if (wait_err < 0) { /* In practice, -ERESTARTSYS */ - err = wait_err; - continue; - } else { - err = 0; + return wait_err; } } - return err; + return 0; } static void submit_job_add_prefence(struct host1x_job *job, struct dma_fence *fence, u32 class) { - struct dma_fence_unwrap iter; struct dma_fence *f; - dma_fence_unwrap_for_each(f, &iter, fence) { + dma_fence_unwrap_for_each(f, fence) { u32 id, threshold; if (dma_fence_is_signaled(f)) diff --git a/drivers/gpu/drm/virtio/virtgpu_submit.c b/drivers/gpu/drm/virtio/virtgpu_submit.c index 6580ece65870..9771eff9b08e 100644 --- a/drivers/gpu/drm/virtio/virtgpu_submit.c +++ b/drivers/gpu/drm/virtio/virtgpu_submit.c @@ -59,16 +59,13 @@ static int virtio_gpu_do_fence_wait(struct virtio_gpu_submit *submit, static int virtio_gpu_dma_fence_wait(struct virtio_gpu_submit *submit, struct dma_fence *fence) { - struct dma_fence_unwrap itr; struct dma_fence *f; int err; - dma_fence_unwrap_for_each(f, &itr, fence) { + dma_fence_unwrap_for_each(f, fence) { err = virtio_gpu_do_fence_wait(submit, f); - if (err) { - dma_fence_put(itr.chain); + if (err) return err; - } } return 0; diff --git a/include/linux/dma-fence-unwrap.h b/include/linux/dma-fence-unwrap.h index 7bfacdf79de2..31f9fdeddf28 100644 --- a/include/linux/dma-fence-unwrap.h +++ b/include/linux/dma-fence-unwrap.h @@ -8,10 +8,9 @@ #ifndef __LINUX_DMA_FENCE_UNWRAP_H #define __LINUX_DMA_FENCE_UNWRAP_H +#include #include -struct dma_fence; - /** * struct dma_fence_unwrap - cursor into the container structure * @@ -32,27 +31,49 @@ struct dma_fence_unwrap { unsigned int index; }; -struct dma_fence *dma_fence_unwrap_first(struct dma_fence *head, - struct dma_fence_unwrap *cursor); -struct dma_fence *dma_fence_unwrap_next(struct dma_fence_unwrap *cursor); +/** + * dma_fence_unwrap_cleanup - cleanup the cursor should we exit early + * @cursor: current position inside the containers + * + * Drop the fence reference inside the cursor should we exit the unwrap loop + * early. + */ +static inline void dma_fence_unwrap_cleanup(struct dma_fence_unwrap *cursor) +{ + dma_fence_put(cursor->chain); +} + +struct dma_fence_unwrap dma_fence_unwrap_first(struct dma_fence *head, + struct dma_fence **fence); +void dma_fence_unwrap_next(struct dma_fence_unwrap *cursor, + struct dma_fence **fence); + +/* Helper for dma_fence_unwrap_for_each(). Internal use only. */ +#define __dma_fence_unwrap_for_each(fence, cursor, head) \ + for (struct dma_fence_unwrap cursor \ + __cleanup(dma_fence_unwrap_cleanup) \ + = dma_fence_unwrap_first(head, &(fence)); fence; \ + dma_fence_unwrap_next(&cursor, &(fence))) /** * dma_fence_unwrap_for_each - iterate over all fences in containers * @fence: current fence - * @cursor: current position inside the containers * @head: starting point for the iterator * * Unwrap dma_fence_chain and dma_fence_array containers and deep dive into all * potential fences in them. If @head is just a normal fence only that one is * returned. + * + * Keeps a reference to the current chain node to ensure that the data structure + * can't be freed while iterating over the fences in it. This reference is + * automatically dropped when breaking out of the loop which will eventually + * free up the current fence element. */ -#define dma_fence_unwrap_for_each(fence, cursor, head) \ - for (fence = dma_fence_unwrap_first(head, cursor); fence; \ - fence = dma_fence_unwrap_next(cursor)) +#define dma_fence_unwrap_for_each(fence, head) \ + __dma_fence_unwrap_for_each(fence, __UNIQUE_ID(cursor), head) struct dma_fence *__dma_fence_unwrap_merge(size_t num_fences, - struct dma_fence **fences, - struct dma_fence_unwrap *cursors); + struct dma_fence **fences); size_t dma_fence_dedup_array(struct dma_fence **array, size_t num_fences); @@ -68,12 +89,11 @@ size_t dma_fence_dedup_array(struct dma_fence **array, size_t num_fences); * Returns NULL on memory allocation failure, a dma_fence object representing * all the given fences otherwise. */ -#define dma_fence_unwrap_merge(...) \ - ({ \ - struct dma_fence *__f[] = { __VA_ARGS__ }; \ - struct dma_fence_unwrap __c[ARRAY_SIZE(__f)]; \ - \ - __dma_fence_unwrap_merge(ARRAY_SIZE(__f), __f, __c); \ +#define dma_fence_unwrap_merge(...) \ + ({ \ + struct dma_fence *__f[] = { __VA_ARGS__ }; \ + \ + __dma_fence_unwrap_merge(ARRAY_SIZE(__f), __f); \ }) #endif -- 2.43.0