Received: by 2002:a05:6358:489b:b0:bb:da1:e618 with SMTP id x27csp7045637rwn; Tue, 13 Sep 2022 12:58:24 -0700 (PDT) X-Google-Smtp-Source: AA6agR42Dj243/PIfHWQopP6HXvATsmY3IZ4v/BpNpMFAzXxdzSnhzs9Zk51/gJs3qirGOHIZ/ff X-Received: by 2002:a17:907:6e0b:b0:73d:5850:ca15 with SMTP id sd11-20020a1709076e0b00b0073d5850ca15mr23545099ejc.344.1663099104270; Tue, 13 Sep 2022 12:58:24 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1663099104; cv=none; d=google.com; s=arc-20160816; b=Ca6JstPkmdFnGA4ARgF5d8WhXAnmpD6gNw6a9vGOtaCFShqW1i62NAU5hn2klZDavU hNlGtM4VHsP/mfuXSSsSsPL/KEp5utbpThiRVAwkCqHRrvVrz7f1g8nSbHM7tFPIEZSy 662tQUBn2S/BZRTn0ONKEwrsvsuRfnnmD9OKEpD+HRFkxsEy/cAR11wmqNx5P0eTheDA d5zrEvwdZ/HWdsf9pz47BOSbppBhyQhFqrdwBryugzwW9peqiMUpB6d0cEBWFubxscpf JWA5lgTOJTrgT9NR7vlBWL1g/EyysM6xRZqI8rGbWN7xhpwoazyWIsl7HI6MHpX0Nx/h fGkg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=zKUU/fhqF9g+mMzyB5cOuMgEu2S5NMiWicW7GZg2dTo=; b=ZSkExNsH7agb9vFzESLDrDJJa6QzrWjaAwk3dQKzCInhhINVluh2RRztVQZEIXxaZi EXfaT/69G0GooXE4mW/P6w5GeQQy74dSjVt52DGD3XxeXDVuCYirnab52G03l6VzJHpX ZfZU/VysocOMwCYPs/wcD+9+Tif9RPvnDvvX5KNmRZ9aVnshmFP80j02XylU9SrOJhP8 HTCX0UE88yrpHR9A7/u4fuXldkTd7w40L54VrO63yHTnCGQPIT06cNI2Uy1EvrYbwV7o nu9nIQcnrG3xpXo9ZvFq1HNhjqXsWYrk60D7sBea3GtN1R+tmcgZq9sAlDM7Y0X3oVqL se+Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@collabora.com header.s=mail header.b=BsvEpwxJ; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=collabora.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id cx1-20020a170906c80100b0073d84b10656si7832970ejb.322.2022.09.13.12.57.58; Tue, 13 Sep 2022 12:58:24 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@collabora.com header.s=mail header.b=BsvEpwxJ; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=collabora.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229908AbiIMTbP (ORCPT + 99 others); Tue, 13 Sep 2022 15:31:15 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55102 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229835AbiIMTa3 (ORCPT ); Tue, 13 Sep 2022 15:30:29 -0400 Received: from madras.collabora.co.uk (madras.collabora.co.uk [IPv6:2a00:1098:0:82:1000:25:2eeb:e5ab]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7CA8D1A04A; Tue, 13 Sep 2022 12:29:39 -0700 (PDT) Received: from dimapc.. (109-252-122-187.nat.spd-mgts.ru [109.252.122.187]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) (Authenticated sender: dmitry.osipenko) by madras.collabora.co.uk (Postfix) with ESMTPSA id 5CF496601FF7; Tue, 13 Sep 2022 20:29:33 +0100 (BST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=collabora.com; s=mail; t=1663097377; bh=PA8s1xebg4jnJB7FxTsQkH7ZwWxPD9Te59Bfb4p8xQA=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=BsvEpwxJgd2N75zCmwMOeeDg3YWzRkUHNfhs2et5tOTELNGPjBC7w43HGRT25+q+l yKL8nqE1GEvLQ4EGyh9kA19j7v+rtMW2VLx6c9TmV2XiioI6IgI++qG2+5dlR+ppYK JIXhx6p1HPQRCI9VGx18EScCC6vSaAZYFBLbnNGV/+nNDXf5Y6vNnImSIkEKQljMqa wCa0kkc6xvlVIw8HNirCOD+0n/PylnaE2E9lmfhjHHhWMbWCRu0wG+J8Ke4xYMP0TZ sFluo+c23ce59vhnWdFdXNl5GcPK9Uj6Lvb0LIOYnArXer7TmhnAol3GmmugzTzFfA kRSsduAILLw4A== From: Dmitry Osipenko To: David Airlie , Gerd Hoffmann , Gurchetan Singh , Chia-I Wu , Daniel Vetter , Daniel Almeida , Gert Wollny , Gustavo Padovan , Daniel Stone , Tomeu Vizoso , Maarten Lankhorst , Maxime Ripard , Thomas Zimmermann , Rob Clark , Sumit Semwal , =?UTF-8?q?Christian=20K=C3=B6nig?= , "Pan, Xinhui" , Thierry Reding , Tomasz Figa , Marek Szyprowski , Mauro Carvalho Chehab , Alex Deucher , Jani Nikula , Joonas Lahtinen , Rodrigo Vivi , Tvrtko Ursulin , =?UTF-8?q?Thomas=20Hellstr=C3=B6m?= , Qiang Yu , Srinivas Kandagatla , Amol Maheshwari , Jason Gunthorpe , Leon Romanovsky , Juergen Gross , Stefano Stabellini , Oleksandr Tyshchenko , Tomi Valkeinen , Russell King , Lucas Stach , Christian Gmeiner , Ruhl Michael J Cc: dri-devel@lists.freedesktop.org, linux-kernel@vger.kernel.org, Dmitry Osipenko , linux-media@vger.kernel.org, linaro-mm-sig@lists.linaro.org, amd-gfx@lists.freedesktop.org, intel-gfx@lists.freedesktop.org, kernel@collabora.com, virtualization@lists.linux-foundation.org, linux-rdma@vger.kernel.org, linux-arm-msm@vger.kernel.org Subject: [PATCH v5 16/21] dma-buf: Move dma_buf_attach() to dynamic locking specification Date: Tue, 13 Sep 2022 22:27:52 +0300 Message-Id: <20220913192757.37727-17-dmitry.osipenko@collabora.com> X-Mailer: git-send-email 2.37.3 In-Reply-To: <20220913192757.37727-1-dmitry.osipenko@collabora.com> References: <20220913192757.37727-1-dmitry.osipenko@collabora.com> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,SPF_HELO_NONE,SPF_PASS, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Move dma-buf attachment API functions to the dynamic locking specification by taking the reservation lock around the mapping operations. The strict locking convention prevents deadlock situations for dma-buf importers and exporters. Reviewed-by: Christian König Signed-off-by: Dmitry Osipenko --- drivers/dma-buf/dma-buf.c | 20 ++++++++------------ 1 file changed, 8 insertions(+), 12 deletions(-) diff --git a/drivers/dma-buf/dma-buf.c b/drivers/dma-buf/dma-buf.c index 80fd6ccc88c6..b406f42d1ebe 100644 --- a/drivers/dma-buf/dma-buf.c +++ b/drivers/dma-buf/dma-buf.c @@ -859,8 +859,8 @@ dma_buf_dynamic_attach(struct dma_buf *dmabuf, struct device *dev, dma_buf_is_dynamic(dmabuf)) { struct sg_table *sgt; + dma_resv_lock(attach->dmabuf->resv, NULL); if (dma_buf_is_dynamic(attach->dmabuf)) { - dma_resv_lock(attach->dmabuf->resv, NULL); ret = dmabuf->ops->pin(attach); if (ret) goto err_unlock; @@ -873,8 +873,7 @@ dma_buf_dynamic_attach(struct dma_buf *dmabuf, struct device *dev, ret = PTR_ERR(sgt); goto err_unpin; } - if (dma_buf_is_dynamic(attach->dmabuf)) - dma_resv_unlock(attach->dmabuf->resv); + dma_resv_unlock(attach->dmabuf->resv); attach->sgt = sgt; attach->dir = DMA_BIDIRECTIONAL; } @@ -890,8 +889,7 @@ dma_buf_dynamic_attach(struct dma_buf *dmabuf, struct device *dev, dmabuf->ops->unpin(attach); err_unlock: - if (dma_buf_is_dynamic(attach->dmabuf)) - dma_resv_unlock(attach->dmabuf->resv); + dma_resv_unlock(attach->dmabuf->resv); dma_buf_detach(dmabuf, attach); return ERR_PTR(ret); @@ -937,21 +935,19 @@ void dma_buf_detach(struct dma_buf *dmabuf, struct dma_buf_attachment *attach) if (WARN_ON(!dmabuf || !attach)) return; + dma_resv_lock(attach->dmabuf->resv, NULL); + if (attach->sgt) { - if (dma_buf_is_dynamic(attach->dmabuf)) - dma_resv_lock(attach->dmabuf->resv, NULL); __unmap_dma_buf(attach, attach->sgt, attach->dir); - if (dma_buf_is_dynamic(attach->dmabuf)) { + if (dma_buf_is_dynamic(attach->dmabuf)) dmabuf->ops->unpin(attach); - dma_resv_unlock(attach->dmabuf->resv); - } } - - dma_resv_lock(dmabuf->resv, NULL); list_del(&attach->node); + dma_resv_unlock(dmabuf->resv); + if (dmabuf->ops->detach) dmabuf->ops->detach(dmabuf, attach); -- 2.37.3