From patchwork Thu Oct 10 18:44:56 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: John Stultz X-Patchwork-Id: 11184075 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 09B6176 for ; Thu, 10 Oct 2019 18:45:16 +0000 (UTC) Received: from gabe.freedesktop.org (gabe.freedesktop.org [131.252.210.177]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id E756B2053B for ; Thu, 10 Oct 2019 18:45:15 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org E756B2053B Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=linaro.org Authentication-Results: mail.kernel.org; spf=none smtp.mailfrom=dri-devel-bounces@lists.freedesktop.org Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id 941556E3B8; Thu, 10 Oct 2019 18:45:12 +0000 (UTC) X-Original-To: dri-devel@lists.freedesktop.org Delivered-To: dri-devel@lists.freedesktop.org Received: from mail-pg1-x543.google.com (mail-pg1-x543.google.com [IPv6:2607:f8b0:4864:20::543]) by gabe.freedesktop.org (Postfix) with ESMTPS id D2D6D6E3B2 for ; Thu, 10 Oct 2019 18:45:10 +0000 (UTC) Received: by mail-pg1-x543.google.com with SMTP id e1so4223993pgj.6 for ; Thu, 10 Oct 2019 11:45:10 -0700 (PDT) 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=SzDVV5Ho4P9GoLXF1MpBffaPgPj/HwAvXKYbZKvwIMc=; b=G2RnMzgpcG7DoL7CXbbmVqgs6zVUbXD7Fm5SMMU1ljyG8MJAYrY1HJfRWdnyh9YGqF EpI/+LmgGall7SHPxu5PKvZfMKSE0QQPHyUMIjpRW9asfHDEvDj6OoXc25i3U7bDzbOQ s5vdjABguKGtCLELXL3FWZGv1H5Z+qUsQ0Yq2Q4NDSA7yj+ICeM5HyviGjVSfqCPeaOq RiiDnqL6DrzAPS682K0xGEHrTHKS3BNID8C+0anvSBeczUnXp34gZ55L8qIt5PdgN+up GbOJz0uXxOwowjVHidf586SDHWhpp+e7qF7Y35i2TaJc8rF7S+N3DT4kHPQqqWg8Lbgz 4uMg== X-Gm-Message-State: APjAAAUhkz92tc95dXWJ6NBwvu3029khWwLH3a5sp5RW7edKn25yx0Z4 DXx8eq/sw1wXxaEfb25Wq564nA== X-Google-Smtp-Source: APXvYqwH27KzVffkAb7iG8Nta+phVvtJ38OPTkRTZZ7SMQMF34EJVDN1ur+b9tVQUO4i8HI0hoPvBw== X-Received: by 2002:a63:df42:: with SMTP id h2mr11866477pgj.405.1570733109991; Thu, 10 Oct 2019 11:45:09 -0700 (PDT) Received: from localhost.localdomain ([2601:1c2:680:1319:692:26ff:feda:3a81]) by smtp.gmail.com with ESMTPSA id w65sm7541732pfb.106.2019.10.10.11.45.08 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 10 Oct 2019 11:45:09 -0700 (PDT) From: John Stultz To: lkml Subject: [PATCH v11 3/5] dma-buf: heaps: Add system heap to dmabuf heaps Date: Thu, 10 Oct 2019 18:44:56 +0000 Message-Id: <20191010184458.51039-4-john.stultz@linaro.org> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20191010184458.51039-1-john.stultz@linaro.org> References: <20191010184458.51039-1-john.stultz@linaro.org> X-Mailman-Original-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=SzDVV5Ho4P9GoLXF1MpBffaPgPj/HwAvXKYbZKvwIMc=; b=QCG8LkyNGGyEuzGgEWgZbsHePY80nPOdjP2kiv3FrOaIVQjo2z3elrWm8eVbSehHqH CLHaEYLa6nTg19KPTqDnEb7mnAl9UR9mpUMQiyO7qMMN8PKfrMFjm9vbxK4yn3U3169y 9sQeKhDU5YKBSmCVdf3hWc+IUdD0S1/2TXT591z11U3FUutfCyLaGr/ubyCkYOvf8Q7S axl9hzPUserpOXk15RNYrW3iQzVw0//SY7olBdgFpVSk/W2LWpgtGRONCAA4s7uwX91r iblysZEBg8+5Iu/JdvwDMk5LWmz2wdCucelvlk46mF+cul1w/C/HJCyUcUyv9P+kWr3Z yVvg== X-BeenThere: dri-devel@lists.freedesktop.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: Direct Rendering Infrastructure - Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Hillf Danton , Sudipto Paul , Vincent Donnefort , Chenbo Feng , Alistair Strachan , Liam Mark , Christoph Hellwig , dri-devel@lists.freedesktop.org, "Andrew F . Davis" , Hridya Valsaraju , Pratik Patel MIME-Version: 1.0 Errors-To: dri-devel-bounces@lists.freedesktop.org Sender: "dri-devel" 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: 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: * 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) v6: * Minor whitespace change suggested by Brian * Remove unused variable v7: * Use newly lower-cased init_heap_helper_buffer helper * Add system heap DOS avoidance suggested by Laura from ION code * Use new dmabuf export helper v8: * Make struct dma_heap_ops consts (suggested by Christoph) * Get rid of needless struct system_heap (suggested by Christoph) * Condense dma_heap_buffer and heap_helper_buffer (suggested by Christoph) * Add forgotten include file to fix build issue on x86 --- drivers/dma-buf/Kconfig | 2 + drivers/dma-buf/heaps/Kconfig | 6 ++ drivers/dma-buf/heaps/Makefile | 1 + drivers/dma-buf/heaps/system_heap.c | 122 ++++++++++++++++++++++++++++ 4 files changed, 131 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 bffa58fc3e6e..0613bb7770f5 100644 --- a/drivers/dma-buf/Kconfig +++ b/drivers/dma-buf/Kconfig @@ -53,4 +53,6 @@ menuconfig DMABUF_HEAPS 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..5db4ef9b4afc --- /dev/null +++ b/drivers/dma-buf/heaps/system_heap.c @@ -0,0 +1,122 @@ +// 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 + +#include "heap-helpers.h" + +struct dma_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; + 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->flags = heap_flags; + helper_buffer->heap = heap; + helper_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++) { + /* + * Avoid trying to allocate memory if the process + * has been killed by by SIGKILL + */ + if (fatal_signal_pending(current)) + goto err1; + + helper_buffer->pages[pg] = alloc_page(GFP_KERNEL | __GFP_ZERO); + if (!helper_buffer->pages[pg]) + goto err1; + } + + /* create the dmabuf */ + dmabuf = heap_helper_export_dmabuf(helper_buffer, fd_flags); + if (IS_ERR(dmabuf)) { + ret = PTR_ERR(dmabuf); + goto err1; + } + + helper_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 const 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 = NULL; + + sys_heap = dma_heap_add(&exp_info); + if (IS_ERR(sys_heap)) + ret = PTR_ERR(sys_heap); + + return ret; +} +device_initcall(system_heap_create);