From patchwork Fri Mar 29 00:15:59 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: John Stultz X-Patchwork-Id: 161377 Delivered-To: patches@linaro.org Received: by 2002:a02:c6d8:0:0:0:0:0 with SMTP id r24csp1367892jan; Thu, 28 Mar 2019 17:16:14 -0700 (PDT) X-Received: by 2002:a17:902:1e6:: with SMTP id b93mr45628993plb.325.1553818574056; Thu, 28 Mar 2019 17:16:14 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1553818574; cv=none; d=google.com; s=arc-20160816; b=ZdpA95AJePp7d6hd7CPo3+Q2XdW0kOfEBNcirBeCk1/eeojdPZzIovjxfpkkt47Jx7 6HBawFikfeFjjSBNDN32bnjFi3f3Tst7d8P7tpiFmkAoAxwIufibaM/ykamqlk20CM/H u5Mx+MTNVOpgIBfbSWQeB5/YQAeYjr7YRkDDrng+hEJqOyJIFnM75y8MA0oqPqvhS4Wo EPIs6rTG7Y6J72ztuU9v4PH5s4nOE+mugJrw2ldGpTuinVoZzjodY8lB1d3NQNCL3i2K H4IGVHQMUgv9BJV5lZ7WcNExc8wbiNOS22/9qtX8Izi9BRsERIIw5BHdRZTvRzolOOBn 3pNQ== 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=5ErZHW0xXHQOtHF8GgpwR2Y0iAGiFqBZ9Qw3SJrGI8Y=; b=u4eseJOFTVtiF8y2hsvBOpSBnnxowXRIJLTdkSAJb/qZFWuyIkKN0wx8fgWYjA3WkU Vlk0+oad89DKhffTyEZYdkAi2HsWQJrSi1MMQRkN8/SvMbJuTOLpo1PP4wRxrXhF0DjT Z13sB0BiqO31eSJYWmAoIBXXHqK5ensEXwDEtnqNoAcCQqPLKhs+NoObSR2BvFG+Kejb 5lCUAJv9xpnLdvL1BjtF/sWEfJDs2zPx+6/Zg2l3c64lh8n15bSewVwGhQ8NuMcJkAkY izETllfFVeF6nf601WTAjbMN6GsGuZJMX/n1yhlppQfXuuazw9BHN45y3sT+sD0x8dPm Atdg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=BbA1bYUl; 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 y67sor469899pgy.46.2019.03.28.17.16.13 for (Google Transport Security); Thu, 28 Mar 2019 17:16:14 -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=BbA1bYUl; 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=5ErZHW0xXHQOtHF8GgpwR2Y0iAGiFqBZ9Qw3SJrGI8Y=; b=BbA1bYUleMH6srk+p8AhjUT3zZT5o5NCDatjMmdD99ai86fogk8ueAsFxTXT/C6VbU DPsHf7QyWdNPoo9uJbr8YWf4f8ejXnxtLOAicBLGEG2khUGAFBDfgmBrD2q3sYJfDTL7 KPT/eZhRlqFMSf9eG0/1w8OAFDur9LtQlCrN5lco5BXHPhXFGgQ479Veoif5gTLOOBBC 4k1ElYAtJRuHHwG/77+ASDrCxATOgRjl8sYJzANXVeAmYMvSmDEbl1KDiqFpx751nWTc EQz6525IOlGtSDGtvox54avpS7qMuoDv3/mc9EQKjIisBYa35fD1gnm7Sv/tNLRTkDNX 4Kxw== 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=5ErZHW0xXHQOtHF8GgpwR2Y0iAGiFqBZ9Qw3SJrGI8Y=; b=ZxtFJ6JsB5FHWjBJ88z5CAw9kzNudSkqiQuwYSTSjKBMOX/UGoo5GHSiIDur4DJ9II /8xGQFrUQNnOrTUyfzd0TxpqfFC7UzNyPY3XLgyRZXN5Dg4q4AxWTtsqjrj99Cvk0bK1 SJCmRK8nFjCiYGLJsM08hR5KcaS3shplHWOES2mU76j0bwAFmULqLzUOpUVWKbzpLi1G PWLvC1X9v9fWRFJna2UqJLtsJjHxAAZlsBty6OVFkiXsUKy6kDSZvpxjmLEUTbz0TJ9s eXJasMXDjD3TO205i441/jsm953D7c6Clc0w0845kFkn0+paJJmDUz81DdlWURFoLuxB GAAg== X-Gm-Message-State: APjAAAXpwHbJESdgXHBSNC97PTDKa5AEdw/vIl3GUCoUU+EgznyUitc2 BxlqHmTkYP5JdjkZ1+006qfdYH2H X-Google-Smtp-Source: APXvYqzpRv45mSiBRK4F8uIHbxMsgJ9NfclX6N6S5IIrui0p7t2w7YqterIiSae09yISN9fiZ3NLag== X-Received: by 2002:a65:4bcc:: with SMTP id p12mr43127793pgr.187.1553818573589; Thu, 28 Mar 2019 17:16:13 -0700 (PDT) Return-Path: Received: from localhost.localdomain ([2601:1c2:680:1319:4e72:b9ff:fe99:466a]) by smtp.gmail.com with ESMTPSA id g5sm430137pfo.53.2019.03.28.17.16.11 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Thu, 28 Mar 2019 17:16:12 -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" , Xu YiPing , "Chenfeng (puck)" , butao , "Xiaqing (A)" , Yudongbin , Christoph Hellwig , Chenbo Feng , Alistair Strachan , dri-devel@lists.freedesktop.org Subject: [RFC][PATCH 3/6 v3] dma-buf: heaps: Add system heap to dmabuf heaps Date: Thu, 28 Mar 2019 17:15:59 -0700 Message-Id: <1553818562-2516-4-git-send-email-john.stultz@linaro.org> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1553818562-2516-1-git-send-email-john.stultz@linaro.org> References: <1553818562-2516-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: Sumit Semwal Cc: Liam Mark Cc: Pratik Patel Cc: Brian Starkey Cc: Vincent Donnefort Cc: Sudipto Paul Cc: Andrew F. Davis Cc: Xu YiPing Cc: "Chenfeng (puck)" Cc: butao Cc: "Xiaqing (A)" Cc: Yudongbin Cc: Christoph Hellwig Cc: Chenbo Feng Cc: Alistair Strachan 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 --- drivers/dma-buf/Kconfig | 2 + drivers/dma-buf/heaps/Kconfig | 6 ++ drivers/dma-buf/heaps/Makefile | 1 + drivers/dma-buf/heaps/system_heap.c | 120 ++++++++++++++++++++++++++++++++++++ 4 files changed, 129 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..f58ee3f --- /dev/null +++ b/drivers/dma-buf/heaps/system_heap.c @@ -0,0 +1,120 @@ +// 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 flags) +{ + struct heap_helper_buffer *helper_buffer; + DEFINE_DMA_BUF_EXPORT_INFO(exp_info); + 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 = 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 = O_RDWR; + 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, O_CLOEXEC); + 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);