Received: by 2002:a25:4158:0:0:0:0:0 with SMTP id o85csp3456578yba; Tue, 16 Apr 2019 11:42:52 -0700 (PDT) X-Google-Smtp-Source: APXvYqxDoBUhrVfeJzZMziWjMVz5ypGUyNxlxBoviL59bFNqgJI15W+8zlNeJ0/URAuNj+QDDCki X-Received: by 2002:a17:902:184:: with SMTP id b4mr63550360plb.26.1555440172350; Tue, 16 Apr 2019 11:42:52 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1555440172; cv=none; d=google.com; s=arc-20160816; b=cx5l3oQ1JgXGWFBfenJk7sAeY8fp4n0qXguGNDZgykETCHQNtJMgEG7hDDavSHnTf4 HiDMR0JVSYgKVCq7BHKxu+zL6IB1M3DN9tOzPEmDM6Iew/88KGMCXAF3ZVflRvY//Knz 3mQqs3lcUVBFFm4ze/NypsjMUW+uHa2sCYPxa4Ef0Im5gYaPKutgd6gSQa69eXh1K3QQ PEfelP2flsbBbPTF8eooc5Lp73Nlp+xgF5WgcDrXP6ofz2i9FU8Rfas8Se1VKOPuoaEj rRk2FK2D3lLI7ihm1Uz63cgTUpoaXMUEXEtuEFj4/DzqS5pp5kRCWjpWjUo1w9//qWu7 oGtw== 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=FQjGrY5BOcl8yYSAGB0H12JRjT7ZcBW9E7af5GsHYPk=; b=J5NWyDcO6ZiD4/Ow8R2JcxdWh7GeTYFbVTrPAPyrdjaoHkwE+vt0UupDi/tk+rpObJ OON4PKm+L2OLE+2P0KRXhkFCiAca7jQnE0kJC2KsZzQBPoedOj2TuwDOPPe60okjkNcf HqRU2GXOj58agPNp1sGFFyWm8sCH8WOSv4OGLWy1RQauFzhqTTw+qxhZ1XKYYEbOUlxv yEkCC2Uvrm10qAz+MDclUbXKCDDQTod/2iiqJ2j5J9kXusnTvFFqXgtGMwb33Q363HGT LZXpFOs0jh1LmcOpewO8X+7qM9OJfq9Aj5I+96r0ginYtP4xjVUrgrN12weVLvk6EIHp JLzA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=GAsCHbJL; 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 14si47641485ple.218.2019.04.16.11.42.36; Tue, 16 Apr 2019 11:42:52 -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=GAsCHbJL; 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 S1730338AbfDPSiu (ORCPT + 99 others); Tue, 16 Apr 2019 14:38:50 -0400 Received: from mail-wr1-f67.google.com ([209.85.221.67]:43703 "EHLO mail-wr1-f67.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1730266AbfDPSit (ORCPT ); Tue, 16 Apr 2019 14:38:49 -0400 Received: by mail-wr1-f67.google.com with SMTP id k17so23794423wrx.10; Tue, 16 Apr 2019 11:38:48 -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=FQjGrY5BOcl8yYSAGB0H12JRjT7ZcBW9E7af5GsHYPk=; b=GAsCHbJLbkAY2QKeOH6xG7dbIdOQF8roD0mkA+N5KbhfZfOE7d13/I1tSthup4EHq+ uKGQs8YoK/PsJDmTmdhwG2uLXf9Hkrk2KB2Jc3cF9vfneK3FzsF1KPffq2TgtJb/V/AT e3fWZ1+ckhxtl/maSPZVK3CTZcwTv4wMB20zAuPLj393Z7v8nCdjOQL9a9UN4dL9M0sA fsbAyZnKD9yGZxMXXBxGGwOHTA+HzarYlbHHc5Cm8Buqfvkcf+uXVufUIZLLDvAOYTEM OFk+ZXMab6A8b7B7bnSpD6CAqh2OHklloPSif6HYgRjlGjN+b2BcUWjo67BGW8TNOBof m6MA== 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=FQjGrY5BOcl8yYSAGB0H12JRjT7ZcBW9E7af5GsHYPk=; b=Ddb05fRSTpPZlHlLA1mUPqiqLaVZ4XXClGRefukIPkpkBqdJuC7cUGbXaSIBmMyGyO qaRABxOG3Wq8hJzo8Y+VE7SRlXSsFjTjKD8rWorioos35pQ61loatTdJR7fEiQuA0by1 9lZ4TSvPMbquIlBBUp6htoYq1nEOxIrMm4tGpBlrDU6ibruW7hgqAtIkj/aaAQmYhaFu XbXQe4fbXrkkHV8lz7xplBTx6gy80Q6aWBUlMFW4SITxXLleBJI4mw5ktki+AMuVAoYt rE80GCoyXiAvLG28vm9XYKFZuVLlXvtApQXej9EnKFoGfZ8GjDJ5z6U5+D3Wq5PZZeDo 6LIw== X-Gm-Message-State: APjAAAXAYDW28+F8hbFz+/9RV9jrWkXeCVUxd/SumPN4esN2wnMxz2Rg cnG09x72nRS4VkT15objE3w= X-Received: by 2002:adf:e547:: with SMTP id z7mr56995337wrm.295.1555439927536; Tue, 16 Apr 2019 11:38:47 -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.46 (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 01/12] dma-buf: add dynamic caching of sg_table Date: Tue, 16 Apr 2019 20:38:30 +0200 Message-Id: <20190416183841.1577-2-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 To allow a smooth transition from pinning buffer objects to dynamic invalidation we first start to cache the sg_table for an attachment unless the driver explicitly says to not do so. Signed-off-by: Christian König --- drivers/dma-buf/dma-buf.c | 24 ++++++++++++++++++++++++ include/linux/dma-buf.h | 11 +++++++++++ 2 files changed, 35 insertions(+) diff --git a/drivers/dma-buf/dma-buf.c b/drivers/dma-buf/dma-buf.c index 7c858020d14b..65161a82d4d5 100644 --- a/drivers/dma-buf/dma-buf.c +++ b/drivers/dma-buf/dma-buf.c @@ -573,6 +573,20 @@ struct dma_buf_attachment *dma_buf_attach(struct dma_buf *dmabuf, list_add(&attach->node, &dmabuf->attachments); mutex_unlock(&dmabuf->lock); + + if (!dmabuf->ops->dynamic_sgt_mapping) { + struct sg_table *sgt; + + sgt = dmabuf->ops->map_dma_buf(attach, DMA_BIDIRECTIONAL); + if (!sgt) + sgt = ERR_PTR(-ENOMEM); + if (IS_ERR(sgt)) { + dma_buf_detach(dmabuf, attach); + return ERR_CAST(sgt); + } + attach->sgt = sgt; + } + return attach; err_attach: @@ -595,6 +609,10 @@ void dma_buf_detach(struct dma_buf *dmabuf, struct dma_buf_attachment *attach) if (WARN_ON(!dmabuf || !attach)) return; + if (attach->sgt) + dmabuf->ops->unmap_dma_buf(attach, attach->sgt, + DMA_BIDIRECTIONAL); + mutex_lock(&dmabuf->lock); list_del(&attach->node); if (dmabuf->ops->detach) @@ -630,6 +648,9 @@ struct sg_table *dma_buf_map_attachment(struct dma_buf_attachment *attach, if (WARN_ON(!attach || !attach->dmabuf)) return ERR_PTR(-EINVAL); + if (attach->sgt) + return attach->sgt; + sg_table = attach->dmabuf->ops->map_dma_buf(attach, direction); if (!sg_table) sg_table = ERR_PTR(-ENOMEM); @@ -657,6 +678,9 @@ void dma_buf_unmap_attachment(struct dma_buf_attachment *attach, if (WARN_ON(!attach || !attach->dmabuf || !sg_table)) return; + if (attach->sgt == sg_table) + return; + attach->dmabuf->ops->unmap_dma_buf(attach, sg_table, direction); } diff --git a/include/linux/dma-buf.h b/include/linux/dma-buf.h index 58725f890b5b..0d9c3c13c9fb 100644 --- a/include/linux/dma-buf.h +++ b/include/linux/dma-buf.h @@ -51,6 +51,16 @@ struct dma_buf_attachment; * @vunmap: [optional] unmaps a vmap from the buffer */ struct dma_buf_ops { + /** + * @dynamic_sgt_mapping: + * + * Flag controlling the caching of the sg_table in the DMA-buf helpers. + * If not set the sg_table is created during device attaching, if set + * the sg_table is created dynamically when dma_buf_map_attachment() is + * called. + */ + bool dynamic_sgt_mapping; + /** * @attach: * @@ -323,6 +333,7 @@ struct dma_buf_attachment { struct device *dev; struct list_head node; void *priv; + struct sg_table *sgt; }; /** -- 2.17.1