Received: by 2002:a05:6358:45e:b0:b5:b6eb:e1f9 with SMTP id 30csp425368rwe; Wed, 24 Aug 2022 03:31:03 -0700 (PDT) X-Google-Smtp-Source: AA6agR7olNwKMTgZpHki//l9iVbk01osedMocOSeY9zQajIq5uumbaLOn49tiJt8bPX8PizveVsR X-Received: by 2002:a17:907:842:b0:731:3310:4187 with SMTP id ww2-20020a170907084200b0073133104187mr2353579ejb.578.1661337063600; Wed, 24 Aug 2022 03:31:03 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1661337063; cv=none; d=google.com; s=arc-20160816; b=fnaC1d6+/rgwNwHgAvHpT5yALkzxRg82ehft2ld9VJhNZa8Sd38MP4pWYurUFSC9e9 xh5AS2xT0V9bSewIk2O2cOwuRgwU4bIcnkEj2S++VrXXWwg3mtLQJPDMx4eHfA5dsSCt 6kkcIFFMt4V8CU3B+ZYRfq+PLYMUIX7mHZDRXuhozbIjxbJzJ4vOfsEkF4H8ZJRVM35a uz0iCF4HiWwe4n85T64WtT6/WPR/Urv4BF9ZOmPcauP/xdAbot9582mOxmJ/ndAqj5Tn bF1LA+ofB3K2MUE7KmMh6tSa0D7+IRxCKxmafwY5aYjiTGygA00BKJ+sQqicA7W4fpm3 uy/w== 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=I0SDgnTKMb27LBnBHtHLgd/k57byGdXGwdOkFT7Vb3I=; b=Ylj2SJ+wBTQhBdW75zrPL+oOaL/KsCVTqj+WU5nyEQShKJZ6AzLXs9F9gywJZsHQeg 5JDBMvkubdEHCcZjo1ke3wEBlHmXephZDPeyzBRS1EPlUG2BvWm43JBgHxeoXHbPzfx+ F+5mZrHkD2axZItbYgHPuBtI6p3C/2bdYxITkXuL1yqFIQANVemK234VkOU55tl7GnLJ CM1Qh42jSUiDPrMugHXXE3Z+POxnjcVzovtTnGIg3nfJJob4hPHjLR5mnXAzdkhnxMdr skkAmXRiR3XRG0i6Zgy73r4a/onxSoOBcQFQlaS8MQt/omEda2BNR94UqNHEbFA2I/Mp kWXA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@collabora.com header.s=mail header.b=YGplYiTH; 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 q16-20020a50c350000000b00445f3dce895si3848369edb.180.2022.08.24.03.30.36; Wed, 24 Aug 2022 03:31:03 -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=YGplYiTH; 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 S235909AbiHXKYl (ORCPT + 99 others); Wed, 24 Aug 2022 06:24:41 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38844 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236955AbiHXKYM (ORCPT ); Wed, 24 Aug 2022 06:24:12 -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 4513880B77; Wed, 24 Aug 2022 03:23:58 -0700 (PDT) Received: from dimapc.. (109-252-119-13.nat.spd-mgts.ru [109.252.119.13]) (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 02D2A6601E84; Wed, 24 Aug 2022 11:23:53 +0100 (BST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=collabora.com; s=mail; t=1661336636; bh=IF0WjJALlax5Sjcmk8CHr8CPUOmvo3HEAaymEv/yVHY=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=YGplYiTHLmmSLMfF20eO3q+05nXHZVdZDVBv77WBzMe0j6JOLi/+yjURFm5wIxfeR L2UQYrUHT+FkTSUz+q5bLy1eLS2OZg0IshXKGyAawacYjySjVw0b6BqpCBReZYSqTl qOmZJn6DXGOOoaOMjFFtWQ751z1+0TvrwmZwYjKyA1dQEFNSQCGe+GH19qnhAn6jEE K7T0VkAEZnAdHhfadMxupLSRCFOef0d9J+SuosVSOltE6sq6U+g+I5A66oUHkDBdUd gz79Q4ZQVWouf+FIj72SdWwjQ0Nqt2gzaxsQbdQe2ZzKFOTbn18PJSqniVuEGo8ckv ckAsVTWOek40A== 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 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, lima@lists.freedesktop.org Subject: [PATCH v3 7/9] dma-buf: Document dynamic locking convention Date: Wed, 24 Aug 2022 13:22:46 +0300 Message-Id: <20220824102248.91964-8-dmitry.osipenko@collabora.com> X-Mailer: git-send-email 2.37.2 In-Reply-To: <20220824102248.91964-1-dmitry.osipenko@collabora.com> References: <20220824102248.91964-1-dmitry.osipenko@collabora.com> MIME-Version: 1.0 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,URIBL_BLOCKED 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 Add documentation for the dynamic locking convention. The documentation tells dma-buf API users when they should take the reservation lock and when not. Signed-off-by: Dmitry Osipenko --- Documentation/driver-api/dma-buf.rst | 6 +++ drivers/dma-buf/dma-buf.c | 63 ++++++++++++++++++++++++++++ 2 files changed, 69 insertions(+) diff --git a/Documentation/driver-api/dma-buf.rst b/Documentation/driver-api/dma-buf.rst index 36a76cbe9095..622b8156d212 100644 --- a/Documentation/driver-api/dma-buf.rst +++ b/Documentation/driver-api/dma-buf.rst @@ -119,6 +119,12 @@ DMA Buffer ioctls .. kernel-doc:: include/uapi/linux/dma-buf.h +DMA-BUF locking convention +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +.. kernel-doc:: drivers/dma-buf/dma-buf.c + :doc: locking convention + Kernel Functions and Structures Reference ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ diff --git a/drivers/dma-buf/dma-buf.c b/drivers/dma-buf/dma-buf.c index f2a5a122da4a..696d132b02f4 100644 --- a/drivers/dma-buf/dma-buf.c +++ b/drivers/dma-buf/dma-buf.c @@ -794,6 +794,69 @@ static struct sg_table * __map_dma_buf(struct dma_buf_attachment *attach, return sg_table; } +/** + * DOC: locking convention + * + * In order to avoid deadlock situations between dma-buf exports and importers, + * all dma-buf API users must follow the common dma-buf locking convention. + * + * Convention for importers + * + * 1. Importers must hold the dma-buf reservation lock when calling these + * functions: + * + * - dma_buf_pin() + * - dma_buf_unpin() + * - dma_buf_map_attachment() + * - dma_buf_unmap_attachment() + * - dma_buf_vmap() + * - dma_buf_vunmap() + * + * 2. Importers must not hold the dma-buf reservation lock when calling these + * functions: + * + * - dma_buf_attach_unlocked() + * - dma_buf_dynamic_attach_unlocked() + * - dma_buf_detach_unlocked() + * - dma_buf_export( + * - dma_buf_fd() + * - dma_buf_get() + * - dma_buf_put() + * - dma_buf_begin_cpu_access() + * - dma_buf_end_cpu_access() + * - dma_buf_map_attachment_unlocked() + * - dma_buf_unmap_attachment_unlocked() + * - dma_buf_vmap_unlocked() + * - dma_buf_vunmap_unlocked() + * + * Convention for exporters + * + * 1. These &dma_buf_ops callbacks are invoked with unlocked dma-buf + * reservation and exporter can take the lock: + * + * - &dma_buf_ops.attach() + * - &dma_buf_ops.detach() + * - &dma_buf_ops.release() + * - &dma_buf_ops.begin_cpu_access() + * - &dma_buf_ops.end_cpu_access() + * + * 2. These &dma_buf_ops callbacks are invoked with locked dma-buf + * reservation and exporter can't take the lock: + * + * - &dma_buf_ops.pin() + * - &dma_buf_ops.unpin() + * - &dma_buf_ops.map_dma_buf() + * - &dma_buf_ops.unmap_dma_buf() + * - &dma_buf_ops.mmap() + * - &dma_buf_ops.vmap() + * - &dma_buf_ops.vunmap() + * + * 3. Exporters must hold the dma-buf reservation lock when calling these + * functions: + * + * - dma_buf_move_notify() + */ + /** * dma_buf_dynamic_attach_unlocked - Add the device to dma_buf's attachments list * @dmabuf: [in] buffer to attach device to. -- 2.37.2