From patchwork Wed Jul 24 00:36:54 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: John Stultz X-Patchwork-Id: 169587 Delivered-To: patches@linaro.org Received: by 2002:a92:4782:0:0:0:0:0 with SMTP id e2csp9527497ilk; Tue, 23 Jul 2019 17:37:09 -0700 (PDT) X-Received: by 2002:a63:c342:: with SMTP id e2mr77892043pgd.79.1563928629174; Tue, 23 Jul 2019 17:37:09 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1563928629; cv=none; d=google.com; s=arc-20160816; b=0Z5VaWZ8tm4LIoIEVURr0peFUlbV+f5cg5Jer1U7APRVqIMA76y16IdOZy2S+wgj44 xmTkkNvm6GZ1f0q9EkeAEwrQApemfGhmeqmYu6JUgeM7wcBo4ifxWhj4BG8HtwMoiVbL NPK4lFpmraRruZeLBx/oLjIZy7pL9Fn4eY4RRlDkFVXvMOWVhNPBGsWXuUfLMdztsMTs Yk+WimPZrFA+iwyDBb4z7hMAtmEY2+Mo5NLgGWAPhCI2uTkjoMXVJZEB3apuHpoZY96S KavxbRTba4N0sy+1ScEcxmjMYuH7u2002Cu6GpOj0EPz6L/ZD27bZN6JGeUO3ZrVfNso X4ww== 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=cRgUrjkIfaz6iepdWbl+Z8FHJvikkt7XqivmX/WSvhU=; b=gF95skLDjg8rKlEZZhdnXgSDmheiBV5ZzoDTWB2OFdXV/74Z+0vTyJJD3AA1mLSzgm PE3zH+WjbpqOrSgjQM7VpJoiR8uPkEj8ig37hJRon5fDrQaBbIL5akW7CVXxK/kL0Ujq QRQAklAQ1YUYR/IULx2iqGaUwR46DRC9TpFy0fReEgOctJ2SLycKcdWkGEkr8WIywkCz owCtjT0lhUEjpgy/S4pHV1B9Qnl95QDOGQKeo2tTiK1dEfQS94HBJv6zwqsC/HdV5cwI baavKLwaE+gZtCRBZ3dfiWoPp88O5r92BFWGuyBwKNiroOn2dUz0DQgQWLIiFksPj/oQ wcew== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=GZag0zHA; 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 s7sor54083658plq.42.2019.07.23.17.37.09 for (Google Transport Security); Tue, 23 Jul 2019 17:37:09 -0700 (PDT) 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=GZag0zHA; 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=cRgUrjkIfaz6iepdWbl+Z8FHJvikkt7XqivmX/WSvhU=; b=GZag0zHAOWD5R8fN8DJoy8togA+QfOKEIUSdoU7SoTPZrLcT5QzSTZqxRx+R0F0me3 HODDMresVAjijh0GX3Xuh1Nv4Q4hgjhf/Ym9LonsUFlaqBGBTJ8NbzqI3FEMvaR0l+fA UiaM/Y2tgFB0RoNQvpiQ+wedvYbw2/esJbRAkRKws+AbGgF0jDszqBzvLoUPYx70cg0b qS0eBdOyW+wn2Kf1o7PXAWpCzfMlQ5JF60D+AIa0aRHK2/f4Is0UATfvFDI25cf9PlsL hNE39dbsDWfr/EiBEzd5IhuEkrIZTtuIFb0zX6ctuaYehU97HSkRNl4Kx2Hg5LXN91l6 7LEg== 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=cRgUrjkIfaz6iepdWbl+Z8FHJvikkt7XqivmX/WSvhU=; b=Dadmh99pu7V5qgXJKT3iG3+rvm+XZYBG15XuEA6fRaY2ZbbROE7Tm9kkuzglLkJwzR YAQcD3qlXetqEm6ilg7UWukqYwKVQRmdRMorjDv0xf9rFJdjHWvmYfMTy+17A00bikly C0J0ji0SzcJTZBFvrVocPz4wiDiG9zpkoGBx9UCDqcQpYCsoVM9ALvx55sVKEFgPAp3U 0u/BMahma8yH7ZYwXy/+c5XariQGlBLOZVPMeuhR45+hStyIaZhfgR0yL/+BamwWrW3i mRWqBYKhyOIO/F+VifdRyx/lwudCiRzLUqJOda/L+9qtW28/SKRUEx7KGGH3IaUhN37I smmw== X-Gm-Message-State: APjAAAUzDn0BnqE0xNwJDzzTNyR70n9w/f3wba3m3DRJHYTwrA4d3+T8 iFOvd8iXv6Dfajtp2O8RkNYvoX+G X-Google-Smtp-Source: APXvYqytR6TxYeoHUSQ5+pWgeQbgrWji25dscymmkUuFJLLAPXcnjyZrh3UypGJt021XJ1Klh5cBhA== X-Received: by 2002:a17:902:a60d:: with SMTP id u13mr83790488plq.144.1563928628769; Tue, 23 Jul 2019 17:37:08 -0700 (PDT) Return-Path: Received: from localhost.localdomain ([2601:1c2:680:1319:692:26ff:feda:3a81]) by smtp.gmail.com with ESMTPSA id x26sm58890022pfq.69.2019.07.23.17.37.07 (version=TLS1_3 cipher=AEAD-AES256-GCM-SHA384 bits=256/256); Tue, 23 Jul 2019 17:37:07 -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 , dri-devel@lists.freedesktop.org Subject: [PATCH v7 3/5] dma-buf: heaps: Add system heap to dmabuf heaps Date: Wed, 24 Jul 2019 00:36:54 +0000 Message-Id: <20190724003656.59780-4-john.stultz@linaro.org> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20190724003656.59780-1-john.stultz@linaro.org> References: <20190724003656.59780-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: dri-devel@lists.freedesktop.org Reviewed-by: Benjamin Gaignard 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 --- 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 162e24e1e429..657ce743abda 100644 --- a/drivers/dma-buf/Kconfig +++ b/drivers/dma-buf/Kconfig @@ -48,4 +48,6 @@ menuconfig DMABUF_HEAPS allows userspace to use 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..5e3eb1fb3bcd --- /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; + 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++) { + /* + * 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->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);