Received: by 2002:a25:ab43:0:0:0:0:0 with SMTP id u61csp8779791ybi; Thu, 6 Jun 2019 20:10:10 -0700 (PDT) X-Google-Smtp-Source: APXvYqwa6ubVTFVbutU1tu6NNLolh4I31K178+ypGhVqZbLQIzm2zhTKDjtdxGvIOitVy8XOitX6 X-Received: by 2002:a62:6241:: with SMTP id w62mr57351191pfb.226.1559877010542; Thu, 06 Jun 2019 20:10:10 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1559877010; cv=none; d=google.com; s=arc-20160816; b=jvwbLfaEsQTij2wHZ3Jj3FT2J1ZZu4yY/JPBffUt1zYaifbmPO8G0z1WRHfx04ukuZ 8WUCny6BiMKJsNIc5TpucD49dzj0vMnWDOiZhUkBe/9KkAkiMaHJGIY7MGZtZ9ljGY3V 71kAJ0oxZdzWbCLud47qrPQjqi4Ua6iAbmsu1SHUBB3/1oRbagLXL8KN2+yoKWWPeuGP wrEV6Yjg1k9w2kCjU2m4+jLI/l02DrSGohTYbqpIsJRP+iPPvjlYfST2FuxqMGb9OVpu 7jkI0M+l0adXptn2D2oRILA//nZDIvnulDjYMrp+ZympGYsBjOWTDjk6jkn3/0ZgP8Iq e9Lg== 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=/8wm7Os9CdHGCNMP7yPxmNwyEoZH+5kBwJGylr4mIo8=; b=AyhCqhC6yQvj0AlBFcIxkkZM4M24l1/e1mMksHpKe6jhWwGuWVnpwIJ5gm7HlouLFy oAeFmSvaZuxen/wWM0Zsq6A+ExpzP/MYfsA3TQHcJacE4Zezkl5qxEy8eGAeJM+R2lBu UDOMfMaUcRUYcriyUqEVDzLXEkBoGzxlMsjNp7hFOX/LsZaoUA3tjlXMCyu7/3dNtB5d b6iuePKl87z9o3Y7XkLVIyC+1RbsKTtdjD2XolJOyg2p/15tZydpkcClOSBI+r9PHj8X 2E++ESaSQlYPzeMQx3G3FwRtVvCJnYP0HDJMlLfWk9DUTuZvCjuWMmtSTy7esaf5Lz0z DqAQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=tuWgrDsc; 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 f63si776370plf.224.2019.06.06.20.09.54; Thu, 06 Jun 2019 20:10:10 -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=tuWgrDsc; 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 S1727081AbfFGDHn (ORCPT + 99 others); Thu, 6 Jun 2019 23:07:43 -0400 Received: from mail-pf1-f195.google.com ([209.85.210.195]:34103 "EHLO mail-pf1-f195.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726519AbfFGDHa (ORCPT ); Thu, 6 Jun 2019 23:07:30 -0400 Received: by mail-pf1-f195.google.com with SMTP id c85so349335pfc.1 for ; Thu, 06 Jun 2019 20:07:29 -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=/8wm7Os9CdHGCNMP7yPxmNwyEoZH+5kBwJGylr4mIo8=; b=tuWgrDscGO252aS52SS7dNvF94DS/hG3WfNmkJrq5q4p7EEoYDIMjLFo+79UiOgWz2 Hk3wd3Ob8CJQsEiJKbHNopmAZQXsdpTF5jNZBp0KBu0hF/2cHo634tI6LmH1n+zvMh0h 4AJY/1+tedbWMNPyyJ5o8TkG/82wvZ4y1qEGWamAWwssIlETCG0WPHI+UvK+loGSpFxu 7M1GLlnK4ICL/JyuHbcAayGZ8HKjjxnoN8kDjXCnucnsPJjUgtb8M2WdZSXMBmrhlK29 hUjhBiftfDkau0UK1/MtCcV9BTzma5xGFALVF1kjMBqUdREGkF3kwMgD7k9hNXI8/vHQ bqhw== 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=/8wm7Os9CdHGCNMP7yPxmNwyEoZH+5kBwJGylr4mIo8=; b=AGwlPi5OcLTUj8KU3kchPCX5wPm8G/trrFrrfoZxOy5RZnVx0Z/PO8ozMKv3lnBaNW uFMaCBBh6hnNqmz/3H0MTc4cRwpWFQVkcLit5rwVemiH3MOk7Rug3tTPp/A2N3etlgp4 5sBmHqpEc2DHpokBMXfQPjOtU7+DbTAEVLKqZhWnWdt362kv2tnDsmKvL8+EVoBArj0k +uL5aDjFr+scWVfUb+bd0kP9aVX1gHVJapbdhkj68HX4xJTpg4Ru5hQIqxbJc4JaZgiS tE4LsqDkiUhTKQNf/JblNxXARxEs6n5l0JM+JWsE0cJuuddw3qx4kDvuVhSMZxNFwYJg n3/g== X-Gm-Message-State: APjAAAW1/J9fX0swmoNMUEsltAHZdnnEbs2nYzxUiTT37hxl3wfT/DV5 laY22MJpmxZdXRgrx5Y2cyzmZgOqNPA= X-Received: by 2002:a17:90a:b30a:: with SMTP id d10mr3213703pjr.8.1559876848934; Thu, 06 Jun 2019 20:07:28 -0700 (PDT) Received: from localhost.localdomain ([2601:1c2:680:1319:692:26ff:feda:3a81]) by smtp.gmail.com with ESMTPSA id f4sm506575pfn.118.2019.06.06.20.07.27 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 06 Jun 2019 20:07:28 -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 , dri-devel@lists.freedesktop.org Subject: [PATCH v5 3/5] dma-buf: heaps: Add system heap to dmabuf heaps Date: Fri, 7 Jun 2019 03:07:17 +0000 Message-Id: <20190607030719.77286-4-john.stultz@linaro.org> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20190607030719.77286-1-john.stultz@linaro.org> References: <20190607030719.77286-1-john.stultz@linaro.org> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.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: 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: dri-devel@lists.freedesktop.org Reviewed-by: Benjamin Gaignard Signed-off-by: John Stultz Change-Id: I4dc5ff54ccb1f7ca3ac8675661114ca33813654b --- v2: * Switch allocate to return dmabuf fd * Simplify init code * Checkpatch fixups * Droped dead system-contig code v3: * Whitespace fixups from Benjamin * Make sure we're zeroing the allocated pages (from Liam) * Use PAGE_ALIGN() consistently (suggested by Brian) * Fold in new registration style from Andrew * Avoid needless dynamic allocation of sys_heap (suggested by Christoph) * Minor cleanups * Folded in changes from Andrew to use simplified page list from the heap helpers v4: * Optimization to allocate pages in chunks, similar to old pagepool code * Use fd_flags when creating dmabuf fd (Suggested by Benjamin) v5: * Back out large order page allocations (was leaking memory, as the page array didn't properly track order size) --- drivers/dma-buf/Kconfig | 2 + drivers/dma-buf/heaps/Kconfig | 6 ++ drivers/dma-buf/heaps/Makefile | 1 + drivers/dma-buf/heaps/system_heap.c | 123 ++++++++++++++++++++++++++++ 4 files changed, 132 insertions(+) create mode 100644 drivers/dma-buf/heaps/Kconfig create mode 100644 drivers/dma-buf/heaps/system_heap.c diff --git a/drivers/dma-buf/Kconfig b/drivers/dma-buf/Kconfig index 9b93f86f597c..434cfe646dad 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 000000000000..205052744169 --- /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 de49898112db..d1808eca2581 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 000000000000..863834499ce1 --- /dev/null +++ b/drivers/dma-buf/heaps/system_heap.c @@ -0,0 +1,123 @@ +// 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; +} sys_heap; + + +static void system_heap_free(struct heap_helper_buffer *buffer) +{ + pgoff_t pg; + + for (pg = 0; pg < buffer->pagecount; pg++) + __free_page(buffer->pages[pg]); + kfree(buffer->pages); + kfree(buffer); +} + +static int system_heap_allocate(struct dma_heap *heap, + unsigned long len, + unsigned long fd_flags, + unsigned long heap_flags) +{ + struct heap_helper_buffer *helper_buffer; + DEFINE_DMA_BUF_EXPORT_INFO(exp_info); + unsigned long size_remaining = len; + struct dma_buf *dmabuf; + int ret = -ENOMEM; + pgoff_t pg; + + 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 = heap_flags; + helper_buffer->heap_buffer.heap = heap; + helper_buffer->heap_buffer.size = len; + + helper_buffer->pagecount = len / PAGE_SIZE; + helper_buffer->pages = kmalloc_array(helper_buffer->pagecount, + sizeof(*helper_buffer->pages), + GFP_KERNEL); + if (!helper_buffer->pages) { + ret = -ENOMEM; + goto err0; + } + + for (pg = 0; pg < helper_buffer->pagecount; pg++) { + helper_buffer->pages[pg] = alloc_page(GFP_KERNEL | __GFP_ZERO); + if (!helper_buffer->pages[pg]) + goto err1; + } + + /* create the dmabuf */ + exp_info.ops = &heap_helper_ops; + exp_info.size = len; + exp_info.flags = fd_flags; + exp_info.priv = &helper_buffer->heap_buffer; + dmabuf = dma_buf_export(&exp_info); + if (IS_ERR(dmabuf)) { + ret = PTR_ERR(dmabuf); + goto err1; + } + + helper_buffer->heap_buffer.dmabuf = dmabuf; + + ret = dma_buf_fd(dmabuf, fd_flags); + if (ret < 0) { + dma_buf_put(dmabuf); + /* just return, as put will call release and that will free */ + return ret; + } + + return ret; + +err1: + while (pg > 0) + __free_page(helper_buffer->pages[--pg]); + kfree(helper_buffer->pages); +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 dma_heap_export_info exp_info; + int ret = 0; + + exp_info.name = "system_heap"; + exp_info.ops = &system_heap_ops; + exp_info.priv = &sys_heap; + + sys_heap.heap = dma_heap_add(&exp_info); + if (IS_ERR(sys_heap.heap)) + ret = PTR_ERR(sys_heap.heap); + + return ret; +} +device_initcall(system_heap_create); -- 2.17.1