1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2011 Google, Inc.
6 #include <linux/kernel.h>
7 #include <linux/file.h>
9 #include <linux/uaccess.h>
14 struct ion_allocation_data allocation;
15 struct ion_heap_query query;
18 static int validate_ioctl_arg(unsigned int cmd, union ion_ioctl_arg *arg)
21 case ION_IOC_HEAP_QUERY:
22 if (arg->query.reserved0 ||
23 arg->query.reserved1 ||
34 /* fix up the cases where the ioctl direction bits are incorrect */
35 static unsigned int ion_ioctl_dir(unsigned int cmd)
43 long ion_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
47 union ion_ioctl_arg data;
49 dir = ion_ioctl_dir(cmd);
51 if (_IOC_SIZE(cmd) > sizeof(data))
55 * The copy_from_user is unconditional here for both read and write
56 * to do the validate. If there is no write for the ioctl, the
59 if (copy_from_user(&data, (void __user *)arg, _IOC_SIZE(cmd)))
62 ret = validate_ioctl_arg(cmd, &data);
64 pr_warn_once("%s: ioctl validate failed\n", __func__);
68 if (!(dir & _IOC_WRITE))
69 memset(&data, 0, sizeof(data));
76 fd = ion_alloc(data.allocation.len,
77 data.allocation.heap_id_mask,
78 data.allocation.flags);
82 data.allocation.fd = fd;
86 case ION_IOC_HEAP_QUERY:
87 ret = ion_query_heaps(&data.query);
93 if (dir & _IOC_READ) {
94 if (copy_to_user((void __user *)arg, &data, _IOC_SIZE(cmd)))