Received: by 2002:a25:4158:0:0:0:0:0 with SMTP id o85csp3454561yba; Tue, 16 Apr 2019 11:39:58 -0700 (PDT) X-Google-Smtp-Source: APXvYqzPoOsUj3gwbESG8m8iWwHwse1u/dHoTFSktPHbRtlxbXS3XImHlNJK2iFSaTNFPjprRINk X-Received: by 2002:a63:b305:: with SMTP id i5mr74479849pgf.274.1555439998506; Tue, 16 Apr 2019 11:39:58 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1555439998; cv=none; d=google.com; s=arc-20160816; b=bvArx2qNqsDygiF2lqaYfsD4O778Ufht72MXd1tywOA5gq0QhvWIzhSCeFvILjEh5O UkyJadCDQFQGd75mDN2/jJ3GQqLDoAc57BUVPe3+xranpWfu/63o7d+j8hLNFSuTCl07 lbzPPgv8GZDV9NRrBUBWL7ur2Wo6/PkwNZXklB3gkBrr8bIsH/WI6air3dHwTYOnIkcQ fhbpnirKsXO8lek/fEAKMfLUdUNkCNyPjs9RxnXB63e0kbWRa0wWX4PjeV6dvbHBfLgd OlmADW5z4V7Ed+2jxcLlhIV4wdrt2qW/xwYyLu+uE3lT/Q52A8SvZh/z9V7hGl1MOvqE ZOWQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:to:from :dkim-signature; bh=BeyFL0YvTGgnodFDzdo62bEx81WVOxS6em1BI9hl5QI=; b=CZPlMm65540hXc/w5LtxBSw2ucF0TFFVh5jzxYLi66jMIQaJteY+zGRr+ACUyc851t q7AAvrbF0r79S5gE3ujSYFdFYDSfsYLfUBgc+OAW1QRPzyHKpZbeax/I3s88xqEO4+Oj ggcPzSfDgefAx/mtYEefHcpLEDuqDqDddxyjknMxP2sszKodVdSD3KSx5swffOC72OG6 kgQxU5YAvW7f7ng305GUTwESlsCYScVXBVXKmIiTTTl/BL9cJn76BTbAgJJpqgqAK1hI 1tN/9pC3x5iDcM0xlj7IYvVAFHuH99x47IqV5QGvCQI87jGw/igrKqKe9lRXhFhICC/9 xFgQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=WdNvQxPI; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id g123si48506639pfc.58.2019.04.16.11.39.42; Tue, 16 Apr 2019 11:39:58 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=WdNvQxPI; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1730373AbfDPSiy (ORCPT + 99 others); Tue, 16 Apr 2019 14:38:54 -0400 Received: from mail-wr1-f65.google.com ([209.85.221.65]:35358 "EHLO mail-wr1-f65.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1730275AbfDPSiv (ORCPT ); Tue, 16 Apr 2019 14:38:51 -0400 Received: by mail-wr1-f65.google.com with SMTP id w1so28502500wrp.2; Tue, 16 Apr 2019 11:38:49 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:subject:date:message-id:in-reply-to:references:mime-version :content-transfer-encoding; bh=BeyFL0YvTGgnodFDzdo62bEx81WVOxS6em1BI9hl5QI=; b=WdNvQxPIytMj1qcS3y/Pq9ARofZCeXMA6QPWfLgWwIfqJ3w1/cqln6BvzQ+CWWsjRR W00cz5kc3ayYiyAje0jTewl9MZA5haY2iF3gJ29ZvEU43GHSW15zCPQu6iNgRUXeWl+t A1vRu09nyipkP67ybvY82LjNK8IhxKz4Hg1qUFDMhnR3x15o/2YFcCvXWcN9FAmPD6+U uxvXdJDWMsreyovaNvwmkg4/bFUWxIFSC8T8+O92WlpbIxhNdUDc3CQktbsYM6Cynnn/ N9WaMqKKKpJlgcY7oIjGOop7ijew6jYL5g71vROQIq8/DEkR+ZUdVnvwqqVFKb4U3x1F CqbA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=BeyFL0YvTGgnodFDzdo62bEx81WVOxS6em1BI9hl5QI=; b=IYOaNHDbNHHdYewXJNpcYdPhWhndYdNRDyIlG2Z7SFfdXB4vG6YHrdks/YjCzyO42O DyAFaRTITB3oBUAtsfAZTlVx1guCyfqspSukPeMhX3jtvI1XF1XidUr859rUisgGFcND 4vj9tWc9uC2HOhh8/to8Ah9pjxhruuRbasBMt80caNecQNhJn36PURJuYmGvgUsX2ME4 G40YvSBxuczK3XmNRfkBAlfrrz8R2I0pOxF1dVgOk3FFXF7+kcK3Jp+lteFrMRUlB6G8 EfLEOuookdRb0GZqudPoYXma9XZj0rZwzefTsXvD1eCGsLiiFlaiO3oa+BV47jFDwwF5 Mznw== X-Gm-Message-State: APjAAAUbqQBtNyz+DiS6POdAR/nfHEdKMok24zcvKswGoWdOyOoyHL8U w00WYshRsp2NoBeWJRdPnLo= X-Received: by 2002:adf:ce8f:: with SMTP id r15mr51256764wrn.90.1555439928382; Tue, 16 Apr 2019 11:38:48 -0700 (PDT) Received: from abel.fritz.box ([2a02:908:1252:fb60:fcfa:e34f:82c0:3f66]) by smtp.gmail.com with ESMTPSA id m13sm128823wmg.42.2019.04.16.11.38.47 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 16 Apr 2019 11:38:47 -0700 (PDT) From: "=?UTF-8?q?Christian=20K=C3=B6nig?=" X-Google-Original-From: =?UTF-8?q?Christian=20K=C3=B6nig?= To: sumit.semwal@linaro.org, linux-media@vger.kernel.org, dri-devel@lists.freedesktop.org, linaro-mm-sig@lists.linaro.org, linux-kernel@vger.kernel.org, amd-gfx@lists.freedesktop.org Subject: [PATCH 02/12] dma-buf: add dma_buf_(un)map_attachment_locked variants v3 Date: Tue, 16 Apr 2019 20:38:31 +0200 Message-Id: <20190416183841.1577-3-christian.koenig@amd.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20190416183841.1577-1-christian.koenig@amd.com> References: <20190416183841.1577-1-christian.koenig@amd.com> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Add function variants which can be called with the reservation lock already held. v2: reordered, add lockdep asserts, fix kerneldoc v3: rebased on sgt caching Signed-off-by: Christian König --- drivers/dma-buf/dma-buf.c | 63 +++++++++++++++++++++++++++++++++++++++ include/linux/dma-buf.h | 5 ++++ 2 files changed, 68 insertions(+) diff --git a/drivers/dma-buf/dma-buf.c b/drivers/dma-buf/dma-buf.c index 65161a82d4d5..ef480e5fb239 100644 --- a/drivers/dma-buf/dma-buf.c +++ b/drivers/dma-buf/dma-buf.c @@ -623,6 +623,43 @@ void dma_buf_detach(struct dma_buf *dmabuf, struct dma_buf_attachment *attach) } EXPORT_SYMBOL_GPL(dma_buf_detach); +/** + * dma_buf_map_attachment_locked - Maps the buffer into _device_ address space + * with the reservation lock held. Is a wrapper for map_dma_buf() of the + * + * Returns the scatterlist table of the attachment; + * dma_buf_ops. + * @attach: [in] attachment whose scatterlist is to be returned + * @direction: [in] direction of DMA transfer + * + * Returns sg_table containing the scatterlist to be returned; returns ERR_PTR + * on error. May return -EINTR if it is interrupted by a signal. + * + * A mapping must be unmapped by using dma_buf_unmap_attachment_locked(). Note + * that the underlying backing storage is pinned for as long as a mapping + * exists, therefore users/importers should not hold onto a mapping for undue + * amounts of time. + */ +struct sg_table * +dma_buf_map_attachment_locked(struct dma_buf_attachment *attach, + enum dma_data_direction direction) +{ + struct sg_table *sg_table; + + might_sleep(); + reservation_object_assert_held(attach->dmabuf->resv); + + if (attach->sgt) + return attach->sgt; + + sg_table = attach->dmabuf->ops->map_dma_buf(attach, direction); + if (!sg_table) + sg_table = ERR_PTR(-ENOMEM); + + return sg_table; +} +EXPORT_SYMBOL_GPL(dma_buf_map_attachment_locked); + /** * dma_buf_map_attachment - Returns the scatterlist table of the attachment; * mapped into _device_ address space. Is a wrapper for map_dma_buf() of the @@ -659,6 +696,32 @@ struct sg_table *dma_buf_map_attachment(struct dma_buf_attachment *attach, } EXPORT_SYMBOL_GPL(dma_buf_map_attachment); +/** + * dma_buf_unmap_attachment_locked - unmaps the buffer with reservation lock + * held, should deallocate the associated scatterlist. Is a wrapper for + * unmap_dma_buf() of dma_buf_ops. + * @attach: [in] attachment to unmap buffer from + * @sg_table: [in] scatterlist info of the buffer to unmap + * @direction: [in] direction of DMA transfer + * + * This unmaps a DMA mapping for @attached obtained by + * dma_buf_map_attachment_locked(). + */ +void dma_buf_unmap_attachment_locked(struct dma_buf_attachment *attach, + struct sg_table *sg_table, + enum dma_data_direction direction) +{ + might_sleep(); + reservation_object_assert_held(attach->dmabuf->resv); + + if (attach->sgt == sg_table) + return; + + attach->dmabuf->ops->unmap_dma_buf(attach, sg_table, + direction); +} +EXPORT_SYMBOL_GPL(dma_buf_unmap_attachment_locked); + /** * dma_buf_unmap_attachment - unmaps and decreases usecount of the buffer;might * deallocate the scatterlist associated. Is a wrapper for unmap_dma_buf() of diff --git a/include/linux/dma-buf.h b/include/linux/dma-buf.h index 0d9c3c13c9fb..18a78be53541 100644 --- a/include/linux/dma-buf.h +++ b/include/linux/dma-buf.h @@ -395,8 +395,13 @@ int dma_buf_fd(struct dma_buf *dmabuf, int flags); struct dma_buf *dma_buf_get(int fd); void dma_buf_put(struct dma_buf *dmabuf); +struct sg_table *dma_buf_map_attachment_locked(struct dma_buf_attachment *, + enum dma_data_direction); struct sg_table *dma_buf_map_attachment(struct dma_buf_attachment *, enum dma_data_direction); +void dma_buf_unmap_attachment_locked(struct dma_buf_attachment *, + struct sg_table *, + enum dma_data_direction); void dma_buf_unmap_attachment(struct dma_buf_attachment *, struct sg_table *, enum dma_data_direction); int dma_buf_begin_cpu_access(struct dma_buf *dma_buf, -- 2.17.1