From patchwork Wed Sep 20 08:45:05 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Benjamin Gaignard X-Patchwork-Id: 113088 Delivered-To: patch@linaro.org Received: by 10.80.163.150 with SMTP id s22csp426699edb; Wed, 20 Sep 2017 01:45:53 -0700 (PDT) X-Received: by 10.99.104.138 with SMTP id d132mr1443237pgc.268.1505897153865; Wed, 20 Sep 2017 01:45:53 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1505897153; cv=none; d=google.com; s=arc-20160816; b=AHIrPMQBNTgegUO+NZPz6QEjk/coEslKBSFDM9kldFMGS01Hl6EEopMa+3XwkXtbN2 G9LMjhIxn4ziYRkc+dgGemCrZHXcsb6BQcmtkDCsPufrtDgvtcbQMu6LlpXEX6j0uCnw pBiGlKBVqkLNKyK4G+0ok61qNfo10yd2r75T/XM5+LwAFuBl15N0Rsdtn9IPZLxmYAFx cZhGG1Mpkf79yJ+nkJN1KeHE2IlADKgk9CTKG0DG4miM1CVuNkbPztx5P/MWDxDocAze GopaaPRNXDg8u66bytKExBdzl6tAVEMh5+ZT6a6SHrQuwvybZOAasBMSWgzYSyUyFIr8 Qb8A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:mime-version :list-subscribe:list-help:list-post:list-archive:list-unsubscribe :list-id:precedence:cc:references:in-reply-to:message-id:date :subject:to:from:dkim-signature:delivered-to :arc-authentication-results; bh=zcOFb9eM8Vo4QidokRtuRcWxTObrDrZZHV/GBJdgVHc=; b=zKN1aGjVtgMxwu8csXanFD/R0bHdS3uKGQAYTx3ZfYAmbXGoV1sYpNOFTrjvCktWRF pdEirr8gu315ozSPxwabj8bi8Pruvf0KYtdhkdMPxgTwjsR6siYkFfd85ObbMDe0TIBS icLNYSy/b7CtcjQk5Gz24K4dsm4pdVjPerrFbHBLog4wEvCyJjMZHGzWicSpVrfkCBzs bexWfSc8rgoUEFBkQ4HRID9zwPuQXVXSCLfao2cbA+nblzCpx9Lm4Y3Hw4/179aOqjBs LVk8lQulK2utgzG9dzQzP7wxUfg65mDBbtEf46869D0mUHZZTP4gq+hKFv37f2uranZZ 51QA== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@linaro.org header.s=google header.b=gRWIY+tP; spf=pass (google.com: best guess record for domain of dri-devel-bounces@lists.freedesktop.org designates 131.252.210.177 as permitted sender) smtp.mailfrom=dri-devel-bounces@lists.freedesktop.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from gabe.freedesktop.org (gabe.freedesktop.org. [131.252.210.177]) by mx.google.com with ESMTPS id p1si861894pgs.254.2017.09.20.01.45.53 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 20 Sep 2017 01:45:53 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of dri-devel-bounces@lists.freedesktop.org designates 131.252.210.177 as permitted sender) client-ip=131.252.210.177; Authentication-Results: mx.google.com; dkim=neutral (body hash did not verify) header.i=@linaro.org header.s=google header.b=gRWIY+tP; spf=pass (google.com: best guess record for domain of dri-devel-bounces@lists.freedesktop.org designates 131.252.210.177 as permitted sender) smtp.mailfrom=dri-devel-bounces@lists.freedesktop.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id CA3CE6E6F2; Wed, 20 Sep 2017 08:45:22 +0000 (UTC) X-Original-To: dri-devel@lists.freedesktop.org Delivered-To: dri-devel@lists.freedesktop.org Received: from mail-wr0-x22e.google.com (mail-wr0-x22e.google.com [IPv6:2a00:1450:400c:c0c::22e]) by gabe.freedesktop.org (Postfix) with ESMTPS id 6A8936E6F2 for ; Wed, 20 Sep 2017 08:45:20 +0000 (UTC) Received: by mail-wr0-x22e.google.com with SMTP id v109so1525252wrc.1 for ; Wed, 20 Sep 2017 01:45:20 -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=nYWEK57xQeV2+If+QRIurfYWNkzIfnf59WgkhwkOgZ4=; b=gRWIY+tP6Us+HQtFcHIjEW0Qmt6GiTHa65qWIuJTGSMAxUrBDlUkTn7Sj7FW960vEQ dFioeaiEb4+ckgnUar+RHxjLbcs+kMjToYBm/qd42hNwSUM21K+3J4L8/N7T5dKvyC10 cq0lwPJ5ECeBuoKFmFpmQAccfpjbNQledVG9A= 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=nYWEK57xQeV2+If+QRIurfYWNkzIfnf59WgkhwkOgZ4=; b=tSIv8fXhj3jF12CrRGYbDn1XHfC8GePJ9PxZXwBUkwTgsyKs2DapgU6/RhLsgvntpn zKfnU9j/3wLilHJdwRSOsTb+PluqquyVoMFtW53hMZ3lUzjFAOCE2nkwhZY1M4aPCLXe 9EnYNTTDB9u8FjAGpNAlAL5f7f3qiV1VIUVCIN2BJoAf3COJ0QxVKOtsC6SPqGtF1d/T IoauppsAX+dQ+S114pAkc4ymqKo8POKSqp296G8F6I2/J1lricVK8GKFOtwTaJQ2uBIw zQORmwY1V4FTmkXg19hk+nZ+SP0GvcfAopsRxzIJ2twRG5EjciFU/hW7K/6J/E6eT0AV fq2g== X-Gm-Message-State: AHPjjUhrNwhpl5bUHfGnxGKWUZBwA94Dy7XRT0FOhCzJ0q145bztI50P YhOlMFp7cE81VH5maB0jrv2SEA== X-Google-Smtp-Source: AOwi7QBsezr+fdfEnX1kBXZ16mBxzckV7dOkco3i0XNUq+d4MZZ9QbfmVkdG5NDP21XeW/nanRl5Kw== X-Received: by 10.223.185.9 with SMTP id k9mr3734300wrf.265.1505897119113; Wed, 20 Sep 2017 01:45:19 -0700 (PDT) Received: from lmecxl0911.lme.st.com ([80.215.133.231]) by smtp.gmail.com with ESMTPSA id w9sm1188700wre.58.2017.09.20.01.45.16 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Wed, 20 Sep 2017 01:45:18 -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 v3 2/2] staging: ion: create one device entry per heap Date: Wed, 20 Sep 2017 10:45:05 +0200 Message-Id: <1505897105-23721-3-git-send-email-benjamin.gaignard@linaro.org> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1505897105-23721-1-git-send-email-benjamin.gaignard@linaro.org> References: <1505897105-23721-1-git-send-email-benjamin.gaignard@linaro.org> Cc: devel@driverdev.osuosl.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" Instead a getting 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. Deivce node major will also change and that may impact init scripts. Signed-off-by: Benjamin Gaignard --- version 3: - change ion_device_add_heap prototype to return a possible error version 2: - simplify ioctl check like propose by Dan - make sure that we don't register more than ION_DEV_MAX heaps drivers/staging/android/TODO | 1 - drivers/staging/android/ion/ion-ioctl.c | 11 +++++++++-- drivers/staging/android/ion/ion.c | 27 ++++++++++++++++++++------- drivers/staging/android/ion/ion.h | 12 ++++++++---- 4 files changed, 37 insertions(+), 14 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/ion-ioctl.c b/drivers/staging/android/ion/ion-ioctl.c index e26b786..c8c906c 100644 --- a/drivers/staging/android/ion/ion-ioctl.c +++ b/drivers/staging/android/ion/ion-ioctl.c @@ -25,8 +25,11 @@ 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) { + int mask = 1 << iminor(filp->f_inode); + switch (cmd) { case ION_IOC_HEAP_QUERY: if (arg->query.reserved0 || @@ -34,6 +37,10 @@ static int validate_ioctl_arg(unsigned int cmd, union ion_ioctl_arg *arg) arg->query.reserved2 ) return -EINVAL; break; + case ION_IOC_ALLOC: + if (!(arg->allocation.heap_id_mask & mask)) + return -EINVAL; + break; default: break; } @@ -69,7 +76,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..cc2d381 100644 --- a/drivers/staging/android/ion/ion.c +++ b/drivers/staging/android/ion/ion.c @@ -40,6 +40,8 @@ #include "ion.h" +#define ION_DEV_MAX 32 + static struct ion_device *internal_dev; static int heap_id; @@ -537,15 +539,28 @@ 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; 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.devt = MKDEV(MAJOR(dev->devt), heap_id); + dev_set_name(&heap->ddev, "ion%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 +598,8 @@ void ion_device_add_heap(struct ion_heap *heap) dev->heap_cnt++; up_write(&dev->lock); + + return 0; } EXPORT_SYMBOL(ion_device_add_heap); @@ -595,13 +612,9 @@ static int ion_device_create(void) if (!idev) return -ENOMEM; - idev->dev.minor = MISC_DYNAMIC_MINOR; - idev->dev.name = "ion"; - idev->dev.fops = &ion_fops; - idev->dev.parent = NULL; - ret = misc_register(&idev->dev); + ret = alloc_chrdev_region(&idev->devt, 0, ION_DEV_MAX, "ion"); if (ret) { - pr_err("ion: failed to register misc device.\n"); + pr_err("ion: unable to allocate major\n"); kfree(idev); return ret; } diff --git a/drivers/staging/android/ion/ion.h b/drivers/staging/android/ion/ion.h index 621e5f7..bb8efc5 100644 --- a/drivers/staging/android/ion/ion.h +++ b/drivers/staging/android/ion/ion.h @@ -17,6 +17,7 @@ #ifndef _ION_H #define _ION_H +#include #include #include #include @@ -26,7 +27,6 @@ #include #include #include -#include #include "../uapi/ion.h" @@ -90,13 +90,13 @@ void ion_buffer_destroy(struct ion_buffer *buffer); /** * struct ion_device - the metadata of the ion device node - * @dev: the actual misc device + * @dev: the actual 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 { - struct miscdevice dev; + dev_t devt; struct rb_root buffers; struct mutex buffer_lock; struct rw_semaphore lock; @@ -152,6 +152,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 +178,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 +216,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