From patchwork Mon Oct 23 15:55:37 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Benjamin Gaignard X-Patchwork-Id: 10022737 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 75AA560381 for ; Mon, 23 Oct 2017 15:57:05 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 6590527F91 for ; Mon, 23 Oct 2017 15:57:05 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 596EE28706; Mon, 23 Oct 2017 15:57:05 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-4.1 required=2.0 tests=BAYES_00,DKIM_SIGNED, RCVD_IN_DNSWL_MED,T_DKIM_INVALID autolearn=ham version=3.3.1 Received: from gabe.freedesktop.org (gabe.freedesktop.org [131.252.210.177]) (using TLSv1.2 with cipher DHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id 98F8E27F91 for ; Mon, 23 Oct 2017 15:57:04 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id 027316E3A2; Mon, 23 Oct 2017 15:57:04 +0000 (UTC) X-Original-To: dri-devel@lists.freedesktop.org Delivered-To: dri-devel@lists.freedesktop.org Received: from mail-wm0-x243.google.com (mail-wm0-x243.google.com [IPv6:2a00:1450:400c:c09::243]) by gabe.freedesktop.org (Postfix) with ESMTPS id 797FF6E3A9 for ; Mon, 23 Oct 2017 15:57:02 +0000 (UTC) Received: by mail-wm0-x243.google.com with SMTP id b189so10415416wmd.4 for ; Mon, 23 Oct 2017 08:57:02 -0700 (PDT) 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=b3B8+NodwSF5+AWawh2zlq4vCD9imKVO0eKJ6unWqks=; b=TpyCFB/GTwtCZS9JrJ4/PCh2gDTnp+8YQaVEkMaDHxt2cNvtD13csXaKWXxBR2ukaP tgDSqEC/siBNIconvxfbIYZ7iHKtzFe7+Z8OwVIV6l0Tp6NOpP7GQ0Co3jys++hL4n/s a0NwLaJ4hzDCm7xUSUxDAdsyWLVJAC5kn41+g= 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=b3B8+NodwSF5+AWawh2zlq4vCD9imKVO0eKJ6unWqks=; b=B9T96UCZqOJPZlZoNts5+gstOSAOKGcFMZQqcvj0fzMS/fJZjezxwEBfyDNZG98vu2 bw/jSUnQiAgZz2bSo+PgNaUkmJ9znvrEcAJyMH8g4Hk3uWGMHe/soQ7Gzx9XJrjrV34z iW/UUvHmuk/UD3b32nPilODI0Xdhy1oFj07uXzLiLuWAtqoKdk8vJ0YluqwA44Ldp89V Ic1XWB1+sLVAp99pLCyOzaS3a6+uWO4x3eFP4TdAxGUU1L88YzU10G6AlvHfGjUT2Puh ar6WnxLO1mKWUa42pKbzPRrlwpdmBBDsfFGHwEQq2flHJwYmuBJRQEUMsJqGfWP8SGOk Sv8A== X-Gm-Message-State: AMCzsaWHpB1d6PYhR2HwJZnZ17Mu4K5/E/ML0mH6EXZFnyTJ41YLUfnq tQKBhDtI56Uq4SoG2Zp8q/0fCA== X-Google-Smtp-Source: ABhQp+Sb7dbZFWXgwe3pDjqAouLKO50vplixeLjtTIi8z1G+wplbnRfVCgx1Xb+P9V2a8sJQy6t/tQ== X-Received: by 10.28.197.65 with SMTP id v62mr5431577wmf.9.1508774220847; Mon, 23 Oct 2017 08:57:00 -0700 (PDT) Received: from lmecxl0911.lme.st.com ([80.214.19.18]) by smtp.gmail.com with ESMTPSA id 55sm8717335wrw.60.2017.10.23.08.56.56 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Mon, 23 Oct 2017 08:57:00 -0700 (PDT) From: Benjamin Gaignard To: labbott@redhat.com, sumit.semwal@linaro.org, gregkh@linuxfoundation.org, arve@android.com, riandrews@android.com, broonie@kernel.org, dan.carpenter@oracle.com Subject: [PATCH v6 2/2] staging: ion: create one device entry per heap Date: Mon, 23 Oct 2017 17:55:37 +0200 Message-Id: <1508774137-13835-3-git-send-email-benjamin.gaignard@linaro.org> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1508774137-13835-1-git-send-email-benjamin.gaignard@linaro.org> References: <1508774137-13835-1-git-send-email-benjamin.gaignard@linaro.org> Cc: devel@driverdev.osuosl.org, linux-api@vger.kernel.org, linux-kernel@vger.kernel.org, dri-devel@lists.freedesktop.org X-BeenThere: dri-devel@lists.freedesktop.org X-Mailman-Version: 2.1.18 Precedence: list List-Id: Direct Rendering Infrastructure - Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Errors-To: dri-devel-bounces@lists.freedesktop.org Sender: "dri-devel" X-Virus-Scanned: ClamAV using ClamSMTP Instead a getting only one common device "/dev/ion" for all the heaps this patch allow to create one device entry ("/dev/ionX") per heap. Getting an entry per heap could allow to set security rules per heap and global ones for all heaps. Allocation requests will be only allowed if the mask_id match with device minor. Query request could be done on any of the devices. Ion devices are parentless so it is need to add platform_bus as parent and platform_bus_type as bus to be put in /sys/device/paltform. Those two parameters need platform_device.h to be included but include files weren't in alphabetic order so I reorder them correctly. Signed-off-by: Benjamin Gaignard --- drivers/staging/android/TODO | 1 - drivers/staging/android/ion/Kconfig | 7 +++++ drivers/staging/android/ion/ion-ioctl.c | 18 +++++++++-- drivers/staging/android/ion/ion.c | 53 ++++++++++++++++++++++++++------- drivers/staging/android/ion/ion.h | 15 ++++++++-- 5 files changed, 79 insertions(+), 15 deletions(-) diff --git a/drivers/staging/android/TODO b/drivers/staging/android/TODO index 5f14247..d770ffa 100644 --- a/drivers/staging/android/TODO +++ b/drivers/staging/android/TODO @@ -9,7 +9,6 @@ TODO: ion/ - Add dt-bindings for remaining heaps (chunk and carveout heaps). This would involve putting appropriate bindings in a memory node for Ion to find. - - Split /dev/ion up into multiple nodes (e.g. /dev/ion/heap0) - Better test framework (integration with VGEM was suggested) Please send patches to Greg Kroah-Hartman and Cc: diff --git a/drivers/staging/android/ion/Kconfig b/drivers/staging/android/ion/Kconfig index a517b2d..cb4666e 100644 --- a/drivers/staging/android/ion/Kconfig +++ b/drivers/staging/android/ion/Kconfig @@ -10,6 +10,13 @@ menuconfig ION If you're not using Android its probably safe to say N here. +config ION_LEGACY_DEVICE_API + bool "Keep using Ion legacy misc device API" + depends on ION + help + Choose this option to keep using Ion legacy misc device API + i.e. /dev/ion + config ION_SYSTEM_HEAP bool "Ion system heap" depends on ION diff --git a/drivers/staging/android/ion/ion-ioctl.c b/drivers/staging/android/ion/ion-ioctl.c index e26b786..bb5c77b 100644 --- a/drivers/staging/android/ion/ion-ioctl.c +++ b/drivers/staging/android/ion/ion-ioctl.c @@ -25,7 +25,8 @@ union ion_ioctl_arg { struct ion_heap_query query; }; -static int validate_ioctl_arg(unsigned int cmd, union ion_ioctl_arg *arg) +static int validate_ioctl_arg(struct file *filp, + unsigned int cmd, union ion_ioctl_arg *arg) { switch (cmd) { case ION_IOC_HEAP_QUERY: @@ -34,6 +35,19 @@ static int validate_ioctl_arg(unsigned int cmd, union ion_ioctl_arg *arg) arg->query.reserved2 ) return -EINVAL; break; + + case ION_IOC_ALLOC: + { + int mask = 1 << iminor(filp->f_inode); + +#ifdef CONFIG_ION_LEGACY_DEVICE_API + if (imajor(filp->f_inode) == MISC_MAJOR) + return 0; +#endif + if (!(arg->allocation.heap_id_mask & mask)) + return -EINVAL; + break; + } default: break; } @@ -69,7 +83,7 @@ long ion_ioctl(struct file *filp, unsigned int cmd, unsigned long arg) if (copy_from_user(&data, (void __user *)arg, _IOC_SIZE(cmd))) return -EFAULT; - ret = validate_ioctl_arg(cmd, &data); + ret = validate_ioctl_arg(filp, cmd, &data); if (WARN_ON_ONCE(ret)) return ret; diff --git a/drivers/staging/android/ion/ion.c b/drivers/staging/android/ion/ion.c index 93e2c90..dd66f55 100644 --- a/drivers/staging/android/ion/ion.c +++ b/drivers/staging/android/ion/ion.c @@ -15,31 +15,35 @@ * */ +#include +#include #include +#include #include +#include #include #include #include -#include +#include #include #include #include #include -#include #include #include +#include #include -#include +#include #include +#include #include #include -#include -#include -#include -#include #include "ion.h" +#define ION_DEV_MAX 32 +#define ION_NAME "ion" + static struct ion_device *internal_dev; static int heap_id; @@ -537,15 +541,30 @@ static int debug_shrink_get(void *data, u64 *val) DEFINE_SIMPLE_ATTRIBUTE(debug_shrink_fops, debug_shrink_get, debug_shrink_set, "%llu\n"); -void ion_device_add_heap(struct ion_heap *heap) +int ion_device_add_heap(struct ion_heap *heap) { struct dentry *debug_file; struct ion_device *dev = internal_dev; + int ret = 0; if (!heap->ops->allocate || !heap->ops->free) pr_err("%s: can not add heap with invalid ops struct.\n", __func__); + if (heap_id >= ION_DEV_MAX) + return -EBUSY; + + heap->ddev.parent = &platform_bus; + heap->ddev.bus = &platform_bus_type; + heap->ddev.devt = MKDEV(MAJOR(internal_dev->devt), heap_id); + dev_set_name(&heap->ddev, ION_NAME"%d", heap_id); + device_initialize(&heap->ddev); + cdev_init(&heap->chrdev, &ion_fops); + heap->chrdev.owner = THIS_MODULE; + ret = cdev_device_add(&heap->chrdev, &heap->ddev); + if (ret < 0) + return ret; + spin_lock_init(&heap->free_lock); heap->free_list_size = 0; @@ -583,6 +602,8 @@ void ion_device_add_heap(struct ion_heap *heap) dev->heap_cnt++; up_write(&dev->lock); + + return ret; } EXPORT_SYMBOL(ion_device_add_heap); @@ -595,8 +616,9 @@ static int ion_device_create(void) if (!idev) return -ENOMEM; +#ifdef CONFIG_ION_LEGACY_DEVICE_API idev->dev.minor = MISC_DYNAMIC_MINOR; - idev->dev.name = "ion"; + idev->dev.name = ION_NAME; idev->dev.fops = &ion_fops; idev->dev.parent = NULL; ret = misc_register(&idev->dev); @@ -605,8 +627,19 @@ static int ion_device_create(void) kfree(idev); return ret; } +#endif + + ret = alloc_chrdev_region(&idev->devt, 0, ION_DEV_MAX, ION_NAME); + if (ret) { + pr_err("ion: unable to allocate device\n"); +#ifdef CONFIG_ION_LEGACY_DEVICE_API + misc_deregister(&idev->dev); +#endif + kfree(idev); + return ret; + } - idev->debug_root = debugfs_create_dir("ion", NULL); + idev->debug_root = debugfs_create_dir(ION_NAME, NULL); if (!idev->debug_root) { pr_err("ion: failed to create debugfs root directory.\n"); goto debugfs_done; diff --git a/drivers/staging/android/ion/ion.h b/drivers/staging/android/ion/ion.h index 621e5f7..2b00ccb 100644 --- a/drivers/staging/android/ion/ion.h +++ b/drivers/staging/android/ion/ion.h @@ -17,16 +17,19 @@ #ifndef _ION_H #define _ION_H +#include #include #include #include +#ifdef CONFIG_ION_LEGACY_DEVICE_API +#include +#endif #include #include #include #include #include #include -#include #include "../uapi/ion.h" @@ -91,12 +94,16 @@ void ion_buffer_destroy(struct ion_buffer *buffer); /** * struct ion_device - the metadata of the ion device node * @dev: the actual misc device + * @devt: Ion device * @buffers: an rb tree of all the existing buffers * @buffer_lock: lock protecting the tree of buffers * @lock: rwsem protecting the tree of heaps and clients */ struct ion_device { +#ifdef CONFIG_ION_LEGACY_DEVICE_API struct miscdevice dev; +#endif + dev_t devt; struct rb_root buffers; struct mutex buffer_lock; struct rw_semaphore lock; @@ -152,6 +159,8 @@ struct ion_heap_ops { * struct ion_heap - represents a heap in the system * @node: rb node to put the heap on the device's tree of heaps * @dev: back pointer to the ion_device + * @ddev: device structure + * @chrdev: associated character device * @type: type of heap * @ops: ops struct as above * @flags: flags @@ -176,6 +185,8 @@ struct ion_heap_ops { struct ion_heap { struct plist_node node; struct ion_device *dev; + struct device ddev; + struct cdev chrdev; enum ion_heap_type type; struct ion_heap_ops *ops; unsigned long flags; @@ -212,7 +223,7 @@ bool ion_buffer_fault_user_mappings(struct ion_buffer *buffer); * ion_device_add_heap - adds a heap to the ion device * @heap: the heap to add */ -void ion_device_add_heap(struct ion_heap *heap); +int ion_device_add_heap(struct ion_heap *heap); /** * some helpers for common operations on buffers using the sg_table