Received: by 2002:ac0:950c:0:0:0:0:0 with SMTP id f12csp3272236imc; Wed, 13 Mar 2019 13:21:31 -0700 (PDT) X-Google-Smtp-Source: APXvYqzEFVE9Fo4iO8Rq3FDaTTCcAGM1nDB0RXSClU2pBlUigOwt3PC8nDrvJ39B581qRblMKbhR X-Received: by 2002:a63:f407:: with SMTP id g7mr41668462pgi.413.1552508491140; Wed, 13 Mar 2019 13:21:31 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1552508491; cv=none; d=google.com; s=arc-20160816; b=h6tf77P1Sxo8GT/mhK5NFG+2/LEtuDI1G6f6s+TxIasTbGQQMJdeI83kikqGINb/PC rvMz3rU06K97wuVSlv79LatG8A+1lDJF6GP7QBS469BfPm0TIQ8BMeNvDrRmyeAyTev+ Fgn2SJAmHRWoOBtU+G8lyk4aKqWlyEcR0FdKQ3gFKXdhsgBBhdfqZXVb5qqtXfABfBy3 6tS5GCgnVKgTfbppz5TojhoFYHq6E5YGd/6W7Nwz3uqTXnocGc7cf4AARPbGKEpq4rSo ep0qtgn89v28ToH8erXBNNeU0Ch/eCRfMijRKieOfZFmc6O1d72dG7f/3UbTUE4p9w3R SKPg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:mime-version:user-agent:references :message-id:in-reply-to:subject:cc:to:from:date:dmarc-filter :dkim-signature:dkim-signature; bh=w0IC5bXTvkBgSsUviLX/fV8g5x8YXDZ/Mjsl9srA03g=; b=QU0CgqWjQiwQtoXt8neWOdqAjMTGmXXvtX4Ytp4LEBHnDJnBFN5v5SWMpfFOiIUVT/ Hiktd2ZHWMv1f0kywRZXMhab86z4S5Mdpjir5n7TK+YmumUMfVrzPISJPwPD+LwlWFfY 0hW9QMYRIiUQz6qlm02bzgZy/QQdKw/puSKJg2iOX+5qxdd+Rxz5CLpju1uPnv9fK1RG 1lkKl8Fmk+euB2deJAQGQGhExFlZA5u9f9ArJcKiAsFxQ3qRiFPvBfMm4vXps/f/OEtZ dfSwK/WIddTDnXCsCyd7MJce3CTqFugLKvQgqD+4GoS/au2ZqDYie4dnM1Cl5ptl8kuz aZLg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@codeaurora.org header.s=default header.b=nxI9dKn2; dkim=pass header.i=@codeaurora.org header.s=default header.b=NOGPotbj; 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 Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id f8si4813011plr.44.2019.03.13.13.21.15; Wed, 13 Mar 2019 13:21:31 -0700 (PDT) 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=@codeaurora.org header.s=default header.b=nxI9dKn2; dkim=pass header.i=@codeaurora.org header.s=default header.b=NOGPotbj; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727295AbfCMUUj (ORCPT + 99 others); Wed, 13 Mar 2019 16:20:39 -0400 Received: from smtp.codeaurora.org ([198.145.29.96]:44182 "EHLO smtp.codeaurora.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726892AbfCMUUi (ORCPT ); Wed, 13 Mar 2019 16:20:38 -0400 Received: by smtp.codeaurora.org (Postfix, from userid 1000) id F01DE60F3E; Wed, 13 Mar 2019 20:20:37 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=codeaurora.org; s=default; t=1552508437; bh=n/WH1Rw8wqoj+tLmiB3QKfQ7Ha5hclg3rlsk3cCnVrs=; h=Date:From:To:cc:Subject:In-Reply-To:References:From; b=nxI9dKn2yKps2iqjIuCrzr+3Fnf1LR7iJLxyLCXZZTaGtc+C0lQe8eD11JGQAUpx4 4By5zn4KYXR33/WflQMb7ylVeL4rNaW4lhqqdj8Jo/M6dTn8+WR/hO32rH5cpovt/d oobyfpfFrRE3yV4NcaRox9GMBgIwU1dSfToRjSI0= X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on pdx-caf-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-2.7 required=2.0 tests=ALL_TRUSTED,BAYES_00, DKIM_INVALID,DKIM_SIGNED autolearn=no autolearn_force=no version=3.4.0 Received: from lmark-linux.qualcomm.com (i-global254.qualcomm.com [199.106.103.254]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) (Authenticated sender: lmark@smtp.codeaurora.org) by smtp.codeaurora.org (Postfix) with ESMTPSA id C533360E7A; Wed, 13 Mar 2019 20:20:35 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=codeaurora.org; s=default; t=1552508436; bh=n/WH1Rw8wqoj+tLmiB3QKfQ7Ha5hclg3rlsk3cCnVrs=; h=Date:From:To:cc:Subject:In-Reply-To:References:From; b=NOGPotbjuj35gfyCGTUfKnBSkLjZgQSYDJxUch8UQIaa1yuo6F1+D7XTKpQtF8HS1 uupEQbcphyGeKInS+A8ZhLF8ET4B/Ma1qwWCDuwTqESoL4/nfFONtiEC7zexy/U4O8 1vv1nnWJ3MZKvbmzcUyK+honWBpIOe6Mc6ARRBvU= DMARC-Filter: OpenDMARC Filter v1.3.2 smtp.codeaurora.org C533360E7A Authentication-Results: pdx-caf-mail.web.codeaurora.org; dmarc=none (p=none dis=none) header.from=codeaurora.org Authentication-Results: pdx-caf-mail.web.codeaurora.org; spf=none smtp.mailfrom=lmark@codeaurora.org Date: Wed, 13 Mar 2019 13:20:35 -0700 (PDT) From: Liam Mark X-X-Sender: lmark@lmark-linux.qualcomm.com To: John Stultz cc: lkml , Laura Abbott , Benjamin Gaignard , Greg KH , Sumit Semwal , Brian Starkey , "Andrew F . Davis" , Chenbo Feng , Alistair Strachan , dri-devel@lists.freedesktop.org Subject: Re: [RFC][PATCH 3/5 v2] dma-buf: heaps: Add system heap to dmabuf heaps In-Reply-To: <1551819273-640-4-git-send-email-john.stultz@linaro.org> Message-ID: References: <1551819273-640-1-git-send-email-john.stultz@linaro.org> <1551819273-640-4-git-send-email-john.stultz@linaro.org> User-Agent: Alpine 2.10 (DEB 1266 2009-07-14) MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Tue, 5 Mar 2019, John Stultz wrote: > 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); Need to zero the allocation (add __GFP_ZERO) > + if (!page) > + goto err2; > + sg_set_page(sg, page, PAGE_SIZE, 0); > + } > + Can always be done later, but it may be helpful to also move this common code from here (and from the cma heap) to the heap helpers file as it reduces code but will also make it easier to introduce future debug features such as making the dma buf names unique to help make it easier to track down the source of memory leaks. > + /* 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 > > Qualcomm Innovation Center, Inc. is a member of Code Aurora Forum, a Linux Foundation Collaborative Project