Received: by 2002:ac0:aed5:0:0:0:0:0 with SMTP id t21csp3570062imb; Tue, 5 Mar 2019 12:56:42 -0800 (PST) X-Google-Smtp-Source: APXvYqxR/7kn0F8oWwCOua3dpZk4EgHx5LQpKCeqH7UPBPmScqO+ZuQRTJNlhTzDLlcOU68MqaXH X-Received: by 2002:aa7:8849:: with SMTP id k9mr3714366pfo.149.1551819402196; Tue, 05 Mar 2019 12:56:42 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1551819402; cv=none; d=google.com; s=arc-20160816; b=Ms11VDWOxlnk23ABmPHYRjQUYtgLJqTfyis+2yRKUT602NavcU5D0//3x8oSBHIA1v JgI+5hx0V3nnb+dOS10JuozoaKkjVKTF18VmaBkLVNInHiv0VFcb1l/hK+o0Jb63RkCM w7S1L3QAdD1v+TL5y0I43SM75ZJOq4HbdkhesCtgMO3Fl5f/hmKfZKATWq8RakdYgte1 S7nvmf7iK8JjatsXI8sPFgxRuVb0GBFRkX4Y6/lCIf22CmrLhETdIYVqKN+GcnwlZ8vX BBfHJETj5B+LMbU2aZ/xqPa7U/Eh2hLI0erwQJ4K6ZwOYatehJ0mUrwOnsqPTl/gvYN7 u6hw== 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=10sN7326k7j9LMSRAvavufAoxXSQ/sxj9rGttiEYBLI=; b=MiZonN9ZKgZhtu5x6HXItUcOHRQaf+E3LSdUgMdqJxYt1gS+Q8sojew7O+pqbeDZmq w8FzSlyFBj3rD7UDORH1FKM1xAaxIkcsmhUtXFOUR7TiopCEv2XVbdsPQzdhAPkl+4tl Fo8EHHBuX5e0QHJXJj7bHQwZADFx8q1h0fS3NfpH7Pw+BCXtNLtjx0nfrzwaoEXCH7aD 0zv1gUFM4yk7XhysstlsbWzEiEYrd76j6u5hLaDwR0bRwANLN4IfxTE4sfRSy0FuAYv8 ehge7TaHdB6ravPRLYtz2S4a4/UswM9rj56ZVX2jemtZzyVsFc7uoOnyqjtqkWQ6+AL0 wZBw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=y9lnW8Km; 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 r1si8536394pgo.227.2019.03.05.12.56.26; Tue, 05 Mar 2019 12:56:42 -0800 (PST) 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=y9lnW8Km; 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 S1728545AbfCEUyt (ORCPT + 99 others); Tue, 5 Mar 2019 15:54:49 -0500 Received: from mail-pf1-f195.google.com ([209.85.210.195]:33591 "EHLO mail-pf1-f195.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728468AbfCEUyr (ORCPT ); Tue, 5 Mar 2019 15:54:47 -0500 Received: by mail-pf1-f195.google.com with SMTP id i19so6623693pfd.0 for ; Tue, 05 Mar 2019 12:54:47 -0800 (PST) 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=10sN7326k7j9LMSRAvavufAoxXSQ/sxj9rGttiEYBLI=; b=y9lnW8Kmg4irPZPNx525O45cijd7sNdoQiLbkI6aBgRQH4pY3hwkFcZpSHZpvky9FZ wpSE5CC9PnR8pSMtn0ZsW1xOz92fTrNyLGW2r7ABwUQmx3L2VMncHcp5P3veEfzU6L4+ q2380Qw4gqYxrEPwi4arsJBcSu0eyBPNppR4/XvZxWsXchOWW4w8+QagHCsbsll+/3wx sp2F2H9XMwiCaWco6ECKJLGvu1nVENUrD52ScmkmD7PLxFiW4HRE8F73dtwP6om+pRd+ 0rtk3moDnsiXq1f9dQsdo7nXNnzESau052vS2EGHRWvdKP1Km7azf1V1S5TQulCcFwCs 6f6Q== 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=10sN7326k7j9LMSRAvavufAoxXSQ/sxj9rGttiEYBLI=; b=rRo4X13obMccDsscSyYTeUSDXgkv/TvtRrpORxDNTzJFnqqbO4iIONC3VLHuUf5X1l 74ucVX7s1+af0YfrarPF4xbtxKThp6UF9sQUvf5ueX33K8R1fBjkCa9XdfOA4qPmDtmG ne5Q7xZjv9t6SDE7hjcp9MWwdFK0vhkqmq6k6Y9FbVs/bLLpJqQoJHfvkl6uDiVYuxwO JkEwISgitGneRel+RJM/h88dzhTT2C7IE8RtxENhsNeoOVGscoK2gDJiV2OD2EdB6MWI 0CFCsab4XrN7CTqsdBYm7UglwtIZm6yjI7ky1/PAmeUBTyI7sGdAAn7AgWECJT7WBeiU BFKQ== X-Gm-Message-State: APjAAAXRQbyBJriHZ9LiLSAc5Bc7b9pK8ydcKMoOKXsGvRSQplOGOFmX 23I34hYAhBFwT7j2fAAmlc05J/2pTzI= X-Received: by 2002:a17:902:b093:: with SMTP id p19mr3194304plr.139.1551819286365; Tue, 05 Mar 2019 12:54:46 -0800 (PST) Received: from localhost.localdomain ([2601:1c2:680:1319:4e72:b9ff:fe99:466a]) by smtp.gmail.com with ESMTPSA id i4sm13411788pfo.158.2019.03.05.12.54.44 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Tue, 05 Mar 2019 12:54:45 -0800 (PST) From: John Stultz To: lkml Cc: John Stultz , Laura Abbott , Benjamin Gaignard , Greg KH , Sumit Semwal , Liam Mark , Brian Starkey , "Andrew F . Davis" , Chenbo Feng , Alistair Strachan , dri-devel@lists.freedesktop.org Subject: [RFC][PATCH 3/5 v2] dma-buf: heaps: Add system heap to dmabuf heaps Date: Tue, 5 Mar 2019 12:54:31 -0800 Message-Id: <1551819273-640-4-git-send-email-john.stultz@linaro.org> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1551819273-640-1-git-send-email-john.stultz@linaro.org> References: <1551819273-640-1-git-send-email-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: Greg KH Cc: Sumit Semwal Cc: Liam Mark Cc: Brian Starkey Cc: Andrew F. Davis Cc: Chenbo Feng Cc: Alistair Strachan Cc: dri-devel@lists.freedesktop.org Signed-off-by: John Stultz --- v2: * Switch allocate to return dmabuf fd * Simplify init code * Checkpatch fixups * Droped dead system-contig code --- drivers/dma-buf/Kconfig | 2 + drivers/dma-buf/heaps/Kconfig | 6 ++ drivers/dma-buf/heaps/Makefile | 1 + drivers/dma-buf/heaps/system_heap.c | 132 ++++++++++++++++++++++++++++++++++++ 4 files changed, 141 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 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..e001661 --- /dev/null +++ b/drivers/dma-buf/heaps/system_heap.c @@ -0,0 +1,132 @@ +// 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; +}; + + +static void system_heap_free(struct heap_helper_buffer *buffer) +{ + int i; + struct scatterlist *sg; + struct sg_table *table = buffer->sg_table; + + for_each_sg(table->sgl, sg, table->nents, i) + __free_page(sg_page(sg)); + + sg_free_table(table); + kfree(table); + kfree(buffer); +} + +static int system_heap_allocate(struct dma_heap *heap, + unsigned long len, + unsigned long flags) +{ + struct heap_helper_buffer *helper_buffer; + struct sg_table *table; + struct scatterlist *sg; + int i, j; + int npages = PAGE_ALIGN(len) / PAGE_SIZE; + DEFINE_DMA_BUF_EXPORT_INFO(exp_info); + struct dma_buf *dmabuf; + int ret = -ENOMEM; + + 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; + + table = kmalloc(sizeof(struct sg_table), GFP_KERNEL); + if (!table) + goto err0; + + i = sg_alloc_table(table, npages, GFP_KERNEL); + if (i) + goto err1; + for_each_sg(table->sgl, sg, table->nents, i) { + struct page *page; + + page = alloc_page(GFP_KERNEL); + if (!page) + goto err2; + sg_set_page(sg, page, PAGE_SIZE, 0); + } + + /* 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 err2; + } + + helper_buffer->heap_buffer.dmabuf = dmabuf; + helper_buffer->sg_table = table; + + 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; + +err2: + for_each_sg(table->sgl, sg, i, j) + __free_page(sg_page(sg)); + sg_free_table(table); +err1: + kfree(table); +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 system_heap *sys_heap; + + sys_heap = kzalloc(sizeof(*sys_heap), GFP_KERNEL); + if (!sys_heap) + return -ENOMEM; + sys_heap->heap.name = "system_heap"; + sys_heap->heap.ops = &system_heap_ops; + + dma_heap_add(&sys_heap->heap); + + return 0; +} +device_initcall(system_heap_create); -- 2.7.4