Received: by 2002:a25:d7c1:0:0:0:0:0 with SMTP id o184csp1307071ybg; Fri, 18 Oct 2019 15:36:08 -0700 (PDT) X-Google-Smtp-Source: APXvYqz16OKlHFRdQXVvWC91If3jCzGsUdW5mefeWm4M0GyTP9IUGt+mjadHlDMPmIg1SGx4HYZi X-Received: by 2002:aa7:c6d0:: with SMTP id b16mr12492070eds.108.1571438168517; Fri, 18 Oct 2019 15:36:08 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1571438168; cv=none; d=google.com; s=arc-20160816; b=FuH45Pf6fv1L8p8Dp9W2gm79qzva8/CG5H5FEgctJN0HnRNUjGywPAn4xLF6/9liAr AjIgMbKU8IRPQn4j8PYctnZSgXEmtCRg5hYwOTGPrzMP5oOBF6c/bgsHwxlV1DOg7Mi/ edr3wZ1drp+7fPx5MBuablF5PMV6IDI0iPx1zRjW64A0TKf2yRtanRNDvbacXrPiNrKR 3DvON6TYY1IV8b5rgLAjc3qeDMHllEfwLS73bY9FCdP6zDiEfalMc+67fkAr8AYpwJt1 mjlYSQJZn/hWlOii04xsMoGDun4tyVbJ7k7r18IZ4IH13JLRQDM1PiL6anMHpANPQqaC BvgA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:references:in-reply-to:message-id:date :subject:cc:to:from:dkim-signature; bh=QK2BBdmc3Msfibz5p+KhUzUiZCgSKEd6d3yu8VLaCNQ=; b=Af3BZCSLCuVNVCgADUZ0ZlJMMKmRb30nM6cCEd4NHDenE+aTJVsJY0ufSntR/lYkxE KpquDKK8slbSfgsAvASUiuVd4cDOOHEsKwjgcSi1dyTy2XtQBHurlCYzicOxlY6/IxYI qhNhV4wvy4Y7zVppNyi5idvijqvCNw+MeLLPXH0n1ifsHpEouu+3DDgE1uK2Y1RUeDfP dKc1Xy/9fLuVMSKep21cNkpWqIndhyh3u2XMqf7rbuzoE+GwxNOnZF0fww02U2/nl/1y /fRD3zAcmGfjeKKSZuQLFD2Yuyo9WH80e0U6TwXK6cZDno9XHU0P/wDvX6GG2KoDLjcf WzRg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=nYauYV6P; 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=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id j18si4272444ejv.201.2019.10.18.15.35.45; Fri, 18 Oct 2019 15:36:08 -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=@linaro.org header.s=google header.b=nYauYV6P; 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=NONE dis=NONE) header.from=linaro.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2438837AbfJRFXd (ORCPT + 99 others); Fri, 18 Oct 2019 01:23:33 -0400 Received: from mail-pl1-f193.google.com ([209.85.214.193]:35464 "EHLO mail-pl1-f193.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2438380AbfJRFXc (ORCPT ); Fri, 18 Oct 2019 01:23:32 -0400 Received: by mail-pl1-f193.google.com with SMTP id c3so2283461plo.2 for ; Thu, 17 Oct 2019 22:23:31 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=QK2BBdmc3Msfibz5p+KhUzUiZCgSKEd6d3yu8VLaCNQ=; b=nYauYV6PD3tEncPvIEJAd0pofaQah3e24VGy7r2LOiTjy9/lMKQF8D2aAOc7zUKXgD ofXwVif1a5r5TeAdcjNWhcXHqfiCn9cGaspjsC6VVKeoMswNj8+KKSlQjxntsyHAPKu7 28uHrHG2tSoObtddVuKNGVvr8YdV0QBVDXZUmtTMtCdLyUbTAcjiaXcDNEfYNnnMzvw6 ZVGiQl5YBXObtbEel7jyVkZGr0WyGXE6oI9JiZC+r0UXVVXbCP4KD1e5tNzh8ZMoqNK8 fpWRZM0ZMq+Ek41CsM1nlALMEhgyRK+PZiwN0lIXP4yV8FJq6w6uMP13zsSU0ytYdvDr IjoA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=QK2BBdmc3Msfibz5p+KhUzUiZCgSKEd6d3yu8VLaCNQ=; b=JG5MSsM/R5I3dKl7PlqOZvkeYeGf9Yf5yFT73ueSfy7sq4q6ghcInr+1lyi4Wn0CNa SEu1xBR4gnHbybF+xyoM6KldajZNqp3AFa2XkgczZnWcaMCnw6Ds1K66Vj+zAgIAIafZ czW1Yldu32ByJZPFqHJtiBlQgtw4r04WPWM+70V674OafM1cvyIXlU8NDEH97I+b13VJ HhjHrQGsjE9DRy3e4AuD2DiKqb1SiPk2A/zTL/ITH2Af6oW9mn2hFmWi3kcplbGd/hVN 7DF4D23WCrDPWdcGaMcHoEr8ZRyJkPirQj+UODBc+9AJkirB+19u9vc7qbP7oM/8HArF IqEw== X-Gm-Message-State: APjAAAWalT067XR/w8IbjRSsBQlhSWdEkmqBikpB1seTUXzFCSnyCB8s KATEwuEuBFrQtrueJLo7aaCdV7FRUrA= X-Received: by 2002:a17:902:bd06:: with SMTP id p6mr7366790pls.120.1571376210625; Thu, 17 Oct 2019 22:23:30 -0700 (PDT) Received: from localhost.localdomain ([2601:1c2:680:1319:692:26ff:feda:3a81]) by smtp.gmail.com with ESMTPSA id g202sm4729336pfb.155.2019.10.17.22.23.29 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 17 Oct 2019 22:23:29 -0700 (PDT) From: John Stultz To: lkml Cc: John Stultz , Laura Abbott , Benjamin Gaignard , Sumit Semwal , Liam Mark , Pratik Patel , Brian Starkey , Vincent Donnefort , Sudipto Paul , "Andrew F . Davis" , Christoph Hellwig , Chenbo Feng , Alistair Strachan , Hridya Valsaraju , Hillf Danton , dri-devel@lists.freedesktop.org Subject: [PATCH v12 2/5] dma-buf: heaps: Add heap helpers Date: Fri, 18 Oct 2019 05:23:20 +0000 Message-Id: <20191018052323.21659-3-john.stultz@linaro.org> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20191018052323.21659-1-john.stultz@linaro.org> References: <20191018052323.21659-1-john.stultz@linaro.org> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Add generic helper dmabuf ops for dma heaps, so we can reduce the amount of duplicative code for the exported dmabufs. This code is an evolution of the Android ION implementation, so thanks to its original authors and maintainters: Rebecca Schultz Zavin, Colin Cross, Laura Abbott, and others! Cc: Laura Abbott Cc: Benjamin Gaignard Cc: Sumit Semwal Cc: Liam Mark Cc: Pratik Patel Cc: Brian Starkey Cc: Vincent Donnefort Cc: Sudipto Paul Cc: Andrew F. Davis Cc: Christoph Hellwig Cc: Chenbo Feng Cc: Alistair Strachan Cc: Hridya Valsaraju Cc: Hillf Danton Cc: dri-devel@lists.freedesktop.org Reviewed-by: Benjamin Gaignard Reviewed-by: Brian Starkey Acked-by: Laura Abbott Tested-by: Ayan Kumar Halder Signed-off-by: John Stultz --- v2: * Removed cache management performance hack that I had accidentally folded in. * Removed stats code that was in helpers * Lots of checkpatch cleanups v3: * Uninline INIT_HEAP_HELPER_BUFFER (suggested by Christoph) * Switch to WARN on buffer destroy failure (suggested by Brian) * buffer->kmap_cnt decrementing cleanup (suggested by Christoph) * Extra buffer->vaddr checking in dma_heap_dma_buf_kmap (suggested by Brian) * Switch to_helper_buffer from macro to inline function (suggested by Benjamin) * Rename kmap->vmap (folded in from Andrew) * Use vmap for vmapping - not begin_cpu_access (folded in from Andrew) * Drop kmap for now, as its optional (folded in from Andrew) * Fold dma_heap_map_user into the single caller (foled in from Andrew) * Folded in patch from Andrew to track page list per heap not sglist, which simplifies the tracking logic v4: * Moved dma-heap.h change out to previous patch v6: * Minor cleanups and typo fixes suggested by Brian v7: * Removed stray ; * Make init_heap_helper_buffer lowercase, as suggested by Christoph * Add dmabuf export helper to reduce boilerplate code v8: * Remove unused private_flags value * Condense dma_heap_buffer and heap_helper_buffer (suggested by Christoph) * Fix indentation by using shorter argument names (suggested by Christoph) * Add flush_kernel_vmap_range/invalidate_kernel_vmap_range calls (suggested by Christoph) * Checkpatch whitespace fixups v9: * Minor cleanups suggested by Brian Starkey v10: * Fix missing vmalloc.h inclusion in heap helpers (found by kbuild test robot ) v12: * Add symbol exports for heaps as modules --- drivers/dma-buf/Makefile | 1 + drivers/dma-buf/heaps/Makefile | 2 + drivers/dma-buf/heaps/heap-helpers.c | 270 +++++++++++++++++++++++++++ drivers/dma-buf/heaps/heap-helpers.h | 55 ++++++ 4 files changed, 328 insertions(+) create mode 100644 drivers/dma-buf/heaps/Makefile create mode 100644 drivers/dma-buf/heaps/heap-helpers.c create mode 100644 drivers/dma-buf/heaps/heap-helpers.h diff --git a/drivers/dma-buf/Makefile b/drivers/dma-buf/Makefile index caee5eb3d351..9c190026bfab 100644 --- a/drivers/dma-buf/Makefile +++ b/drivers/dma-buf/Makefile @@ -2,6 +2,7 @@ obj-y := dma-buf.o dma-fence.o dma-fence-array.o dma-fence-chain.o \ dma-resv.o seqno-fence.o obj-$(CONFIG_DMABUF_HEAPS) += dma-heap.o +obj-$(CONFIG_DMABUF_HEAPS) += heaps/ obj-$(CONFIG_SYNC_FILE) += sync_file.o obj-$(CONFIG_SW_SYNC) += sw_sync.o sync_debug.o obj-$(CONFIG_UDMABUF) += udmabuf.o diff --git a/drivers/dma-buf/heaps/Makefile b/drivers/dma-buf/heaps/Makefile new file mode 100644 index 000000000000..de49898112db --- /dev/null +++ b/drivers/dma-buf/heaps/Makefile @@ -0,0 +1,2 @@ +# SPDX-License-Identifier: GPL-2.0 +obj-y += heap-helpers.o diff --git a/drivers/dma-buf/heaps/heap-helpers.c b/drivers/dma-buf/heaps/heap-helpers.c new file mode 100644 index 000000000000..fb9835126893 --- /dev/null +++ b/drivers/dma-buf/heaps/heap-helpers.c @@ -0,0 +1,270 @@ +// SPDX-License-Identifier: GPL-2.0 +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "heap-helpers.h" + +void init_heap_helper_buffer(struct heap_helper_buffer *buffer, + void (*free)(struct heap_helper_buffer *)) +{ + buffer->priv_virt = NULL; + mutex_init(&buffer->lock); + buffer->vmap_cnt = 0; + buffer->vaddr = NULL; + buffer->pagecount = 0; + buffer->pages = NULL; + INIT_LIST_HEAD(&buffer->attachments); + buffer->free = free; +} +EXPORT_SYMBOL_GPL(init_heap_helper_buffer); + +struct dma_buf *heap_helper_export_dmabuf(struct heap_helper_buffer *buffer, + int fd_flags) +{ + DEFINE_DMA_BUF_EXPORT_INFO(exp_info); + + exp_info.ops = &heap_helper_ops; + exp_info.size = buffer->size; + exp_info.flags = fd_flags; + exp_info.priv = buffer; + + return dma_buf_export(&exp_info); +} +EXPORT_SYMBOL_GPL(heap_helper_export_dmabuf); + +static void *dma_heap_map_kernel(struct heap_helper_buffer *buffer) +{ + void *vaddr; + + vaddr = vmap(buffer->pages, buffer->pagecount, VM_MAP, PAGE_KERNEL); + if (!vaddr) + return ERR_PTR(-ENOMEM); + + return vaddr; +} + +static void dma_heap_buffer_destroy(struct heap_helper_buffer *buffer) +{ + if (buffer->vmap_cnt > 0) { + WARN("%s: buffer still mapped in the kernel\n", __func__); + vunmap(buffer->vaddr); + } + + buffer->free(buffer); +} + +static void *dma_heap_buffer_vmap_get(struct heap_helper_buffer *buffer) +{ + void *vaddr; + + if (buffer->vmap_cnt) { + buffer->vmap_cnt++; + return buffer->vaddr; + } + vaddr = dma_heap_map_kernel(buffer); + if (IS_ERR(vaddr)) + return vaddr; + buffer->vaddr = vaddr; + buffer->vmap_cnt++; + return vaddr; +} + +static void dma_heap_buffer_vmap_put(struct heap_helper_buffer *buffer) +{ + if (!--buffer->vmap_cnt) { + vunmap(buffer->vaddr); + buffer->vaddr = NULL; + } +} + +struct dma_heaps_attachment { + struct device *dev; + struct sg_table table; + struct list_head list; +}; + +static int dma_heap_attach(struct dma_buf *dmabuf, + struct dma_buf_attachment *attachment) +{ + struct dma_heaps_attachment *a; + struct heap_helper_buffer *buffer = dmabuf->priv; + int ret; + + a = kzalloc(sizeof(*a), GFP_KERNEL); + if (!a) + return -ENOMEM; + + ret = sg_alloc_table_from_pages(&a->table, buffer->pages, + buffer->pagecount, 0, + buffer->pagecount << PAGE_SHIFT, + GFP_KERNEL); + if (ret) { + kfree(a); + return ret; + } + + a->dev = attachment->dev; + INIT_LIST_HEAD(&a->list); + + attachment->priv = a; + + mutex_lock(&buffer->lock); + list_add(&a->list, &buffer->attachments); + mutex_unlock(&buffer->lock); + + return 0; +} + +static void dma_heap_detach(struct dma_buf *dmabuf, + struct dma_buf_attachment *attachment) +{ + struct dma_heaps_attachment *a = attachment->priv; + struct heap_helper_buffer *buffer = dmabuf->priv; + + mutex_lock(&buffer->lock); + list_del(&a->list); + mutex_unlock(&buffer->lock); + + sg_free_table(&a->table); + kfree(a); +} + +static +struct sg_table *dma_heap_map_dma_buf(struct dma_buf_attachment *attachment, + enum dma_data_direction direction) +{ + struct dma_heaps_attachment *a = attachment->priv; + struct sg_table *table; + + table = &a->table; + + if (!dma_map_sg(attachment->dev, table->sgl, table->nents, + direction)) + table = ERR_PTR(-ENOMEM); + return table; +} + +static void dma_heap_unmap_dma_buf(struct dma_buf_attachment *attachment, + struct sg_table *table, + enum dma_data_direction direction) +{ + dma_unmap_sg(attachment->dev, table->sgl, table->nents, direction); +} + +static vm_fault_t dma_heap_vm_fault(struct vm_fault *vmf) +{ + struct vm_area_struct *vma = vmf->vma; + struct heap_helper_buffer *buffer = vma->vm_private_data; + + vmf->page = buffer->pages[vmf->pgoff]; + get_page(vmf->page); + + return 0; +} + +static const struct vm_operations_struct dma_heap_vm_ops = { + .fault = dma_heap_vm_fault, +}; + +static int dma_heap_mmap(struct dma_buf *dmabuf, struct vm_area_struct *vma) +{ + struct heap_helper_buffer *buffer = dmabuf->priv; + + if ((vma->vm_flags & (VM_SHARED | VM_MAYSHARE)) == 0) + return -EINVAL; + + vma->vm_ops = &dma_heap_vm_ops; + vma->vm_private_data = buffer; + + return 0; +} + +static void dma_heap_dma_buf_release(struct dma_buf *dmabuf) +{ + struct heap_helper_buffer *buffer = dmabuf->priv; + + dma_heap_buffer_destroy(buffer); +} + +static int dma_heap_dma_buf_begin_cpu_access(struct dma_buf *dmabuf, + enum dma_data_direction direction) +{ + struct heap_helper_buffer *buffer = dmabuf->priv; + struct dma_heaps_attachment *a; + int ret = 0; + + mutex_lock(&buffer->lock); + + if (buffer->vmap_cnt) + invalidate_kernel_vmap_range(buffer->vaddr, buffer->size); + + list_for_each_entry(a, &buffer->attachments, list) { + dma_sync_sg_for_cpu(a->dev, a->table.sgl, a->table.nents, + direction); + } + mutex_unlock(&buffer->lock); + + return ret; +} + +static int dma_heap_dma_buf_end_cpu_access(struct dma_buf *dmabuf, + enum dma_data_direction direction) +{ + struct heap_helper_buffer *buffer = dmabuf->priv; + struct dma_heaps_attachment *a; + + mutex_lock(&buffer->lock); + + if (buffer->vmap_cnt) + flush_kernel_vmap_range(buffer->vaddr, buffer->size); + + list_for_each_entry(a, &buffer->attachments, list) { + dma_sync_sg_for_device(a->dev, a->table.sgl, a->table.nents, + direction); + } + mutex_unlock(&buffer->lock); + + return 0; +} + +static void *dma_heap_dma_buf_vmap(struct dma_buf *dmabuf) +{ + struct heap_helper_buffer *buffer = dmabuf->priv; + void *vaddr; + + mutex_lock(&buffer->lock); + vaddr = dma_heap_buffer_vmap_get(buffer); + mutex_unlock(&buffer->lock); + + return vaddr; +} + +static void dma_heap_dma_buf_vunmap(struct dma_buf *dmabuf, void *vaddr) +{ + struct heap_helper_buffer *buffer = dmabuf->priv; + + mutex_lock(&buffer->lock); + dma_heap_buffer_vmap_put(buffer); + mutex_unlock(&buffer->lock); +} + +const struct dma_buf_ops heap_helper_ops = { + .map_dma_buf = dma_heap_map_dma_buf, + .unmap_dma_buf = dma_heap_unmap_dma_buf, + .mmap = dma_heap_mmap, + .release = dma_heap_dma_buf_release, + .attach = dma_heap_attach, + .detach = dma_heap_detach, + .begin_cpu_access = dma_heap_dma_buf_begin_cpu_access, + .end_cpu_access = dma_heap_dma_buf_end_cpu_access, + .vmap = dma_heap_dma_buf_vmap, + .vunmap = dma_heap_dma_buf_vunmap, +}; diff --git a/drivers/dma-buf/heaps/heap-helpers.h b/drivers/dma-buf/heaps/heap-helpers.h new file mode 100644 index 000000000000..911c931f7f06 --- /dev/null +++ b/drivers/dma-buf/heaps/heap-helpers.h @@ -0,0 +1,55 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* + * DMABUF Heaps helper code + * + * Copyright (C) 2011 Google, Inc. + * Copyright (C) 2019 Linaro Ltd. + */ + +#ifndef _HEAP_HELPERS_H +#define _HEAP_HELPERS_H + +#include +#include + +/** + * struct heap_helper_buffer - helper buffer metadata + * @heap: back pointer to the heap the buffer came from + * @dmabuf: backing dma-buf for this buffer + * @size: size of the buffer + * @flags: buffer specific flags + * @priv_virt pointer to heap specific private value + * @lock mutext to protect the data in this structure + * @vmap_cnt count of vmap references on the buffer + * @vaddr vmap'ed virtual address + * @pagecount number of pages in the buffer + * @pages list of page pointers + * @attachments list of device attachments + * + * @free heap callback to free the buffer + */ +struct heap_helper_buffer { + struct dma_heap *heap; + struct dma_buf *dmabuf; + size_t size; + unsigned long flags; + + void *priv_virt; + struct mutex lock; + int vmap_cnt; + void *vaddr; + pgoff_t pagecount; + struct page **pages; + struct list_head attachments; + + void (*free)(struct heap_helper_buffer *buffer); +}; + +void init_heap_helper_buffer(struct heap_helper_buffer *buffer, + void (*free)(struct heap_helper_buffer *)); + +struct dma_buf *heap_helper_export_dmabuf(struct heap_helper_buffer *buffer, + int fd_flags); + +extern const struct dma_buf_ops heap_helper_ops; +#endif /* _HEAP_HELPERS_H */ -- 2.17.1