From patchwork Mon Nov 18 20:23:30 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: John Stultz X-Patchwork-Id: 179605 Delivered-To: patches@linaro.org Received: by 2002:a92:38d5:0:0:0:0:0 with SMTP id g82csp3371610ilf; Mon, 18 Nov 2019 12:23:40 -0800 (PST) X-Received: by 2002:a17:902:6506:: with SMTP id b6mr24458672plk.133.1574108619923; Mon, 18 Nov 2019 12:23:39 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1574108619; cv=none; d=google.com; s=arc-20160816; b=FKu6FPzYuksXAz3sBZ/Vcuus6IrCB5j33879NpRn3X69k9xxblFEg8W2mSihufG/uI waA9sz17w8lBVoN0UwRmdTQCNzJbTh6bEWWJ+SuM0bAZXUfg61tQPDhOhzSs0VPaDyp7 lLwimXXlxQGY6yqV7sM43iqLqK+Koa61I+YtXVAVSPabPT/0O3rlhD9FyoujkCdGyQxj 47E+hmtmfWv/SOKhG2bKx5jaL+Ab8qvbUO11dowIFVAt4ufbKa0k9CiAduc8xuSn35dn DIpji6TXYx5UKkBUHT4rYUvF1fX8vx2LMU+Eevr4DNTzKJ4fSTKQiBrR3cqMJc6B46iZ FuJA== 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=62c/6FPwz+q6rvXrG8i3YVfn6MxAXoMreZTxPxOlj5A=; b=PUZ2K4nA6557kJuSHbrvoFJzwjVqQWDSAERe3pDyUpUjLAUnL8rJ9NgOwuUDdu/FGq 6VnZVYn5T9wZpbFcKoH7vu1qlIGEf9v1Pc6m9sGWIC8QBrO/4zkom7goz3Yqu9JF6bka d4WBGQ0VwnfRuzWSxG+Veu/GuKVvy05wCYD5mWcF6axV8u1irL9jRhwy1ITjif0ra2nc dWPr5kqZVUbDsx74CLA2iUnvpnKtW3/Yx1LH70AnWlKMWRdwurW0JdzCoLHtcBmFyOud sFM3e1U+rdTM29joj6od/2hslbz4QpkqtjTwRnVY8+5P4c9/wyaV3+8+/1ipbr/J2FmX zoSA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=ch5NaihK; 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 w2sor22921126plk.41.2019.11.18.12.23.39 for (Google Transport Security); Mon, 18 Nov 2019 12:23:39 -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=ch5NaihK; 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=62c/6FPwz+q6rvXrG8i3YVfn6MxAXoMreZTxPxOlj5A=; b=ch5NaihKV/QyVydlbPH7HihH2fQ5HQ3c59g1Hep+tufaTPejbI8jrd7lb5L8P2Evxw pgh3an1c4f+0Tw0N4DhrtXJtQz6oJ8MoMtEKSTeEwYgMazMoiPpQO5IfAOgYR4fyI9ky f+72CIIs9Y+bOF80GWTaNlUzuKAI29ccFTf1M/2L9aB4arYsy+tH23llZSheGJtExQw/ 1xlL8zIYhqmCIGTCY8MzoukyLr4gGm9GVZNZTSRpLdA//Jjoln/LFlgKy7DSGwFvlnTX x1NnUQ7PPjdnMayhB4N1Hmlo9YEWL0Zp5QlkVA86dHMngz/UuCP/NXIDowJNRU/O9Uxf H31w== 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=62c/6FPwz+q6rvXrG8i3YVfn6MxAXoMreZTxPxOlj5A=; b=NSO/PY+q26Ewxorf1EBrLITND2r+7ikhxPHAYZhuu5DlAdmgguS8lHpk4bmrTdk3IG Jp4ltS+NCubAgZ53aIPG3hUovmF/m/BwXAMwqTuNfD8adKVAF7VOH44l1LW8kkKcAXF1 fTuoEkM9LGUd76uspLSMVTcbnvh/QAgupmLFGKpvBn5lML5Xg4ScP1mQwg5gtgY7doFD BBmzz2lF0tUCgWoilUqd1HHLd5RrdhUFsDDvCXOXItZRHvZ2uBiAeq6XehCIVuLB+cri 2zlAKJVrdk46lc0N/TWe4sf61ku7kh/WkiT8PL7rGSxqzjazvMY443wzVyo2k2jJjv+k 5SQA== X-Gm-Message-State: APjAAAXEZJnHM+GKi2yqytkU2A6kOWpjTClPBKA0xl+BBritP7w0Uu3b wWkmR/2mbL8YHxUcsru9194FM1gj X-Google-Smtp-Source: APXvYqyJhcXgx0ajlguJa2TigxhWo7esltd1yW4mf6KPoDh9FGobxzdvDxd9NiNVFa2cCxtYQed64Q== X-Received: by 2002:a17:902:4a:: with SMTP id 68mr2183630pla.8.1574108619396; Mon, 18 Nov 2019 12:23:39 -0800 (PST) Return-Path: Received: from localhost.localdomain (c-67-170-172-113.hsd1.or.comcast.net. [67.170.172.113]) by smtp.gmail.com with ESMTPSA id 7sm11021871pgk.25.2019.11.18.12.23.38 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 18 Nov 2019 12:23:38 -0800 (PST) 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 , Sandeep Patil , Hillf Danton , Dave Airlie , dri-devel@lists.freedesktop.org Subject: [PATCH v16 3/5] dma-buf: heaps: Add system heap to dmabuf heaps Date: Mon, 18 Nov 2019 20:23:30 +0000 Message-Id: <20191118202332.109172-4-john.stultz@linaro.org> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20191118202332.109172-1-john.stultz@linaro.org> References: <20191118202332.109172-1-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: 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: Sandeep Patil Cc: Hillf Danton Cc: Dave Airlie Cc: dri-devel@lists.freedesktop.org Reviewed-by: Benjamin Gaignard Reviewed-by: Brian Starkey Acked-by: Sandeep Patil 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 v12: * Minor tweaks to prep loading heap from module v14: * Fix "redundant assignment to variable ret" issue reported by Colin King and fixed by Andrew Davis v15: * Drop unused heap flag from heap_helper_buffer as suggested by Sandeep Patil --- 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 -- 2.17.1 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..1aa01e98c595 --- /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 +#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->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 ret; +} + +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; +} +module_init(system_heap_create); +MODULE_LICENSE("GPL v2");