From patchwork Tue Mar 5 20:54:31 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: John Stultz X-Patchwork-Id: 159713 Delivered-To: patches@linaro.org Received: by 2002:a02:5cc1:0:0:0:0:0 with SMTP id w62csp5410385jad; Tue, 5 Mar 2019 12:54:46 -0800 (PST) X-Received: by 2002:a65:43cc:: with SMTP id n12mr3004344pgp.218.1551819286770; Tue, 05 Mar 2019 12:54:46 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1551819286; cv=none; d=google.com; s=arc-20160816; b=AJDvOcNZairK8akjzDecBV9BhqSkIjGNDTTWLxjtrEDzZsT9XkOCZt0bbT2v7/hMXZ 7MKw7eTBTQt7zxez4WboOmPXXYZ8vqflzuobnvBnFE1ymPlUdEwtbxwy9gFZvil81DOX kZS62uTjZEgjVTOjGq3zQ3oER4W+zm3VDXPgl3CNIYsrXXmPWMVO2qBlCJ+3e04sYZd1 9rODNCTFJ+lbHZdeA6Zs/+HpWR1YVlMC6MAmqn7yCpyCCe9UENr9763YSzj/CZT+mrbC FqrnDYWVV3wLx6A0boUFD9bKqC6qT1ZjsHg8gb2Tw2kKUGt4QSX/GEvdg39fkd+ejq69 W73Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=10sN7326k7j9LMSRAvavufAoxXSQ/sxj9rGttiEYBLI=; b=RD5D2OsgMXxSInZaM7D/5/gwA6If+yAi4XpGxuogutUKmYa8frh/uB1d9gJHpiyFHT 2JgAHMwwSZKu1nyMjGlDcQF4kgEcuUgyf/SWj1dowOwc1WYQKZzIfjAY069buUZe6UPf QBQyMzCFDAdrc4P3gQ2X8zaH118751fbIt+vtiyNOt07H6HpcVPfwYe9SpG42Wsaq5s0 8of6S0CvJL3UAOwjl0E1yyJtOWkIz0xOiSkHiTZymMJjAuTxhKzCJsx265WaGoqQwNYl btRziq5eMoS3ePq9hCDbo4b5p9y3TBAuyRwER7pcne1xbPu0W0hjAGy25U0Ex0zLKePo VqVw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=y9lnW8Km; spf=pass (google.com: domain of john.stultz@linaro.org designates 209.85.220.65 as permitted sender) smtp.mailfrom=john.stultz@linaro.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from mail-sor-f65.google.com (mail-sor-f65.google.com. [209.85.220.65]) by mx.google.com with SMTPS id h7sor14846601plb.46.2019.03.05.12.54.46 for (Google Transport Security); Tue, 05 Mar 2019 12:54:46 -0800 (PST) Received-SPF: pass (google.com: domain of john.stultz@linaro.org designates 209.85.220.65 as permitted sender) client-ip=209.85.220.65; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=y9lnW8Km; spf=pass (google.com: domain of john.stultz@linaro.org designates 209.85.220.65 as permitted sender) smtp.mailfrom=john.stultz@linaro.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org 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=10sN7326k7j9LMSRAvavufAoxXSQ/sxj9rGttiEYBLI=; b=y9lnW8Kmg4irPZPNx525O45cijd7sNdoQiLbkI6aBgRQH4pY3hwkFcZpSHZpvky9FZ wpSE5CC9PnR8pSMtn0ZsW1xOz92fTrNyLGW2r7ABwUQmx3L2VMncHcp5P3veEfzU6L4+ q2380Qw4gqYxrEPwi4arsJBcSu0eyBPNppR4/XvZxWsXchOWW4w8+QagHCsbsll+/3wx sp2F2H9XMwiCaWco6ECKJLGvu1nVENUrD52ScmkmD7PLxFiW4HRE8F73dtwP6om+pRd+ 0rtk3moDnsiXq1f9dQsdo7nXNnzESau052vS2EGHRWvdKP1Km7azf1V1S5TQulCcFwCs 6f6Q== 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=10sN7326k7j9LMSRAvavufAoxXSQ/sxj9rGttiEYBLI=; b=i9QH7oO5Rxb42XJfcfjVw0UJn3u/kzs1q85y8dCjYQYNFT1DCTst97tGG5qdBdnGNe 9PDe53hePP8K5fflnE2P4OhsYqLlwxpJrcUfjz+Qzaz7qIySONNI+CcqBu0q/J06q9Tf Fk5uE038Au9Xksmb30m9TsHOLHWgzkhRaBhYbeXpRUPM/bS/tYnnFqVibVHbPYXEalCp 3ct+CgtI5BTb/eK8RY/8exaLBdNs7fwjV/9+t+WRgM9/q/Yz0nFZ4UR/kGMBI8OvwyWX DuTOZNC9kAFi8AGP0/ES/1G1EppVrgpXLMOukbmgpI4nfa87NMJax9DMK4PpEEQjKoqV GqBQ== X-Gm-Message-State: APjAAAWzcLuCW0bUOhjY9+5DrnVv2XYHupukynDrqcDE2wYUwd/Ut3R1 yvDiInPgE5hONuVaNwFsK1SYuu3N X-Google-Smtp-Source: APXvYqyw6P1dcy/08qrowb3Lcm2ulZcEET3axbofUHPNEjyyJTCXZqG8t1VPyxs28WwSFfLYwYRtGw== X-Received: by 2002:a17:902:b093:: with SMTP id p19mr3194304plr.139.1551819286365; Tue, 05 Mar 2019 12:54:46 -0800 (PST) Return-Path: Received: from localhost.localdomain ([2601:1c2:680:1319:4e72:b9ff:fe99:466a]) by smtp.gmail.com with ESMTPSA id i4sm13411788pfo.158.2019.03.05.12.54.44 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Tue, 05 Mar 2019 12:54:45 -0800 (PST) From: John Stultz To: lkml Cc: John Stultz , Laura Abbott , Benjamin Gaignard , Greg KH , Sumit Semwal , Liam Mark , Brian Starkey , "Andrew F . Davis" , Chenbo Feng , Alistair Strachan , dri-devel@lists.freedesktop.org Subject: [RFC][PATCH 3/5 v2] dma-buf: heaps: Add system heap to dmabuf heaps Date: Tue, 5 Mar 2019 12:54:31 -0800 Message-Id: <1551819273-640-4-git-send-email-john.stultz@linaro.org> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1551819273-640-1-git-send-email-john.stultz@linaro.org> References: <1551819273-640-1-git-send-email-john.stultz@linaro.org> This patch adds system heap to the dma-buf heaps framework. This allows applications to get a page-allocator backed dma-buf for non-contiguous memory. 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: Greg KH Cc: Sumit Semwal Cc: Liam Mark Cc: Brian Starkey Cc: Andrew F. Davis Cc: Chenbo Feng Cc: Alistair Strachan Cc: dri-devel@lists.freedesktop.org Signed-off-by: John Stultz --- v2: * Switch allocate to return dmabuf fd * Simplify init code * Checkpatch fixups * Droped dead system-contig code --- drivers/dma-buf/Kconfig | 2 + drivers/dma-buf/heaps/Kconfig | 6 ++ drivers/dma-buf/heaps/Makefile | 1 + drivers/dma-buf/heaps/system_heap.c | 132 ++++++++++++++++++++++++++++++++++++ 4 files changed, 141 insertions(+) create mode 100644 drivers/dma-buf/heaps/Kconfig create mode 100644 drivers/dma-buf/heaps/system_heap.c -- 2.7.4 diff --git a/drivers/dma-buf/Kconfig b/drivers/dma-buf/Kconfig index 09c61db..63c139d 100644 --- a/drivers/dma-buf/Kconfig +++ b/drivers/dma-buf/Kconfig @@ -47,4 +47,6 @@ menuconfig DMABUF_HEAPS this allows userspace to allocate dma-bufs that can be shared between drivers. +source "drivers/dma-buf/heaps/Kconfig" + endmenu diff --git a/drivers/dma-buf/heaps/Kconfig b/drivers/dma-buf/heaps/Kconfig new file mode 100644 index 0000000..2050527 --- /dev/null +++ b/drivers/dma-buf/heaps/Kconfig @@ -0,0 +1,6 @@ +config DMABUF_HEAPS_SYSTEM + bool "DMA-BUF System Heap" + depends on DMABUF_HEAPS + help + Choose this option to enable the system dmabuf heap. The system heap + is backed by pages from the buddy allocator. If in doubt, say Y. diff --git a/drivers/dma-buf/heaps/Makefile b/drivers/dma-buf/heaps/Makefile index de49898..d1808ec 100644 --- a/drivers/dma-buf/heaps/Makefile +++ b/drivers/dma-buf/heaps/Makefile @@ -1,2 +1,3 @@ # SPDX-License-Identifier: GPL-2.0 obj-y += heap-helpers.o +obj-$(CONFIG_DMABUF_HEAPS_SYSTEM) += system_heap.o diff --git a/drivers/dma-buf/heaps/system_heap.c b/drivers/dma-buf/heaps/system_heap.c new file mode 100644 index 0000000..e001661 --- /dev/null +++ b/drivers/dma-buf/heaps/system_heap.c @@ -0,0 +1,132 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * DMABUF System heap exporter + * + * Copyright (C) 2011 Google, Inc. + * Copyright (C) 2019 Linaro Ltd. + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "heap-helpers.h" + + +struct system_heap { + struct dma_heap heap; +}; + + +static void system_heap_free(struct heap_helper_buffer *buffer) +{ + int i; + struct scatterlist *sg; + struct sg_table *table = buffer->sg_table; + + for_each_sg(table->sgl, sg, table->nents, i) + __free_page(sg_page(sg)); + + sg_free_table(table); + kfree(table); + kfree(buffer); +} + +static int system_heap_allocate(struct dma_heap *heap, + unsigned long len, + unsigned long flags) +{ + struct heap_helper_buffer *helper_buffer; + struct sg_table *table; + struct scatterlist *sg; + int i, j; + int npages = PAGE_ALIGN(len) / PAGE_SIZE; + DEFINE_DMA_BUF_EXPORT_INFO(exp_info); + struct dma_buf *dmabuf; + int ret = -ENOMEM; + + helper_buffer = kzalloc(sizeof(*helper_buffer), GFP_KERNEL); + if (!helper_buffer) + return -ENOMEM; + + INIT_HEAP_HELPER_BUFFER(helper_buffer, system_heap_free); + helper_buffer->heap_buffer.flags = flags; + helper_buffer->heap_buffer.heap = heap; + helper_buffer->heap_buffer.size = len; + + table = kmalloc(sizeof(struct sg_table), GFP_KERNEL); + if (!table) + goto err0; + + i = sg_alloc_table(table, npages, GFP_KERNEL); + if (i) + goto err1; + for_each_sg(table->sgl, sg, table->nents, i) { + struct page *page; + + page = alloc_page(GFP_KERNEL); + if (!page) + goto err2; + sg_set_page(sg, page, PAGE_SIZE, 0); + } + + /* create the dmabuf */ + exp_info.ops = &heap_helper_ops; + exp_info.size = len; + exp_info.flags = O_RDWR; + exp_info.priv = &helper_buffer->heap_buffer; + dmabuf = dma_buf_export(&exp_info); + if (IS_ERR(dmabuf)) { + ret = PTR_ERR(dmabuf); + goto err2; + } + + helper_buffer->heap_buffer.dmabuf = dmabuf; + helper_buffer->sg_table = table; + + ret = dma_buf_fd(dmabuf, O_CLOEXEC); + if (ret < 0) { + dma_buf_put(dmabuf); + /* just return, as put will call release and that will free */ + return ret; + } + + return ret; + +err2: + for_each_sg(table->sgl, sg, i, j) + __free_page(sg_page(sg)); + sg_free_table(table); +err1: + kfree(table); +err0: + kfree(helper_buffer); + return -ENOMEM; +} + + +static struct dma_heap_ops system_heap_ops = { + .allocate = system_heap_allocate, +}; + +static int system_heap_create(void) +{ + struct system_heap *sys_heap; + + sys_heap = kzalloc(sizeof(*sys_heap), GFP_KERNEL); + if (!sys_heap) + return -ENOMEM; + sys_heap->heap.name = "system_heap"; + sys_heap->heap.ops = &system_heap_ops; + + dma_heap_add(&sys_heap->heap); + + return 0; +} +device_initcall(system_heap_create);