dm ioctl: optimize functions without variable params
[platform/adaptation/renesas_rcar/renesas_kernel.git] / drivers / md / dm-ioctl.c
index afd9598..7eb0682 100644 (file)
@@ -1067,6 +1067,7 @@ static void retrieve_status(struct dm_table *table,
        num_targets = dm_table_get_num_targets(table);
        for (i = 0; i < num_targets; i++) {
                struct dm_target *ti = dm_table_get_target(table, i);
+               size_t l;
 
                remaining = len - (outptr - outbuf);
                if (remaining <= sizeof(struct dm_target_spec)) {
@@ -1093,14 +1094,17 @@ static void retrieve_status(struct dm_table *table,
                if (ti->type->status) {
                        if (param->flags & DM_NOFLUSH_FLAG)
                                status_flags |= DM_STATUS_NOFLUSH_FLAG;
-                       if (ti->type->status(ti, type, status_flags, outptr, remaining)) {
-                               param->flags |= DM_BUFFER_FULL_FLAG;
-                               break;
-                       }
+                       ti->type->status(ti, type, status_flags, outptr, remaining);
                } else
                        outptr[0] = '\0';
 
-               outptr += strlen(outptr) + 1;
+               l = strlen(outptr) + 1;
+               if (l == remaining) {
+                       param->flags |= DM_BUFFER_FULL_FLAG;
+                       break;
+               }
+
+               outptr += l;
                used = param->data_start + (outptr - outbuf);
 
                outptr = align_ptr(outptr);
@@ -1474,39 +1478,52 @@ static int target_message(struct dm_ioctl *param, size_t param_size)
        return r;
 }
 
+/*
+ * The ioctl parameter block consists of two parts, a dm_ioctl struct
+ * followed by a data buffer.  This flag is set if the second part,
+ * which has a variable size, is not used by the function processing
+ * the ioctl.
+ */
+#define IOCTL_FLAGS_NO_PARAMS  1
+
 /*-----------------------------------------------------------------
  * Implementation of open/close/ioctl on the special char
  * device.
  *---------------------------------------------------------------*/
-static ioctl_fn lookup_ioctl(unsigned int cmd)
+static ioctl_fn lookup_ioctl(unsigned int cmd, int *ioctl_flags)
 {
        static struct {
                int cmd;
+               int flags;
                ioctl_fn fn;
        } _ioctls[] = {
-               {DM_VERSION_CMD, NULL}, /* version is dealt with elsewhere */
-               {DM_REMOVE_ALL_CMD, remove_all},
-               {DM_LIST_DEVICES_CMD, list_devices},
-
-               {DM_DEV_CREATE_CMD, dev_create},
-               {DM_DEV_REMOVE_CMD, dev_remove},
-               {DM_DEV_RENAME_CMD, dev_rename},
-               {DM_DEV_SUSPEND_CMD, dev_suspend},
-               {DM_DEV_STATUS_CMD, dev_status},
-               {DM_DEV_WAIT_CMD, dev_wait},
-
-               {DM_TABLE_LOAD_CMD, table_load},
-               {DM_TABLE_CLEAR_CMD, table_clear},
-               {DM_TABLE_DEPS_CMD, table_deps},
-               {DM_TABLE_STATUS_CMD, table_status},
-
-               {DM_LIST_VERSIONS_CMD, list_versions},
-
-               {DM_TARGET_MSG_CMD, target_message},
-               {DM_DEV_SET_GEOMETRY_CMD, dev_set_geometry}
+               {DM_VERSION_CMD, 0, NULL}, /* version is dealt with elsewhere */
+               {DM_REMOVE_ALL_CMD, IOCTL_FLAGS_NO_PARAMS, remove_all},
+               {DM_LIST_DEVICES_CMD, 0, list_devices},
+
+               {DM_DEV_CREATE_CMD, IOCTL_FLAGS_NO_PARAMS, dev_create},
+               {DM_DEV_REMOVE_CMD, IOCTL_FLAGS_NO_PARAMS, dev_remove},
+               {DM_DEV_RENAME_CMD, 0, dev_rename},
+               {DM_DEV_SUSPEND_CMD, IOCTL_FLAGS_NO_PARAMS, dev_suspend},
+               {DM_DEV_STATUS_CMD, IOCTL_FLAGS_NO_PARAMS, dev_status},
+               {DM_DEV_WAIT_CMD, 0, dev_wait},
+
+               {DM_TABLE_LOAD_CMD, 0, table_load},
+               {DM_TABLE_CLEAR_CMD, IOCTL_FLAGS_NO_PARAMS, table_clear},
+               {DM_TABLE_DEPS_CMD, 0, table_deps},
+               {DM_TABLE_STATUS_CMD, 0, table_status},
+
+               {DM_LIST_VERSIONS_CMD, 0, list_versions},
+
+               {DM_TARGET_MSG_CMD, 0, target_message},
+               {DM_DEV_SET_GEOMETRY_CMD, 0, dev_set_geometry}
        };
 
-       return (cmd >= ARRAY_SIZE(_ioctls)) ? NULL : _ioctls[cmd].fn;
+       if (unlikely(cmd >= ARRAY_SIZE(_ioctls)))
+               return NULL;
+
+       *ioctl_flags = _ioctls[cmd].flags;
+       return _ioctls[cmd].fn;
 }
 
 /*
@@ -1543,40 +1560,90 @@ static int check_version(unsigned int cmd, struct dm_ioctl __user *user)
        return r;
 }
 
-static int copy_params(struct dm_ioctl __user *user, struct dm_ioctl **param)
+#define DM_PARAMS_KMALLOC      0x0001  /* Params alloced with kmalloc */
+#define DM_PARAMS_VMALLOC      0x0002  /* Params alloced with vmalloc */
+#define DM_WIPE_BUFFER         0x0010  /* Wipe input buffer before returning from ioctl */
+
+static void free_params(struct dm_ioctl *param, size_t param_size, int param_flags)
+{
+       if (param_flags & DM_WIPE_BUFFER)
+               memset(param, 0, param_size);
+
+       if (param_flags & DM_PARAMS_KMALLOC)
+               kfree(param);
+       if (param_flags & DM_PARAMS_VMALLOC)
+               vfree(param);
+}
+
+static int copy_params(struct dm_ioctl __user *user, struct dm_ioctl *param_kernel,
+                      int ioctl_flags,
+                      struct dm_ioctl **param, int *param_flags)
 {
-       struct dm_ioctl tmp, *dmi;
+       struct dm_ioctl *dmi;
        int secure_data;
+       const size_t minimum_data_size = sizeof(*param_kernel) - sizeof(param_kernel->data);
 
-       if (copy_from_user(&tmp, user, sizeof(tmp) - sizeof(tmp.data)))
+       if (copy_from_user(param_kernel, user, minimum_data_size))
                return -EFAULT;
 
-       if (tmp.data_size < (sizeof(tmp) - sizeof(tmp.data)))
+       if (param_kernel->data_size < minimum_data_size)
                return -EINVAL;
 
-       secure_data = tmp.flags & DM_SECURE_DATA_FLAG;
+       secure_data = param_kernel->flags & DM_SECURE_DATA_FLAG;
+
+       *param_flags = secure_data ? DM_WIPE_BUFFER : 0;
+
+       if (ioctl_flags & IOCTL_FLAGS_NO_PARAMS) {
+               dmi = param_kernel;
+               dmi->data_size = minimum_data_size;
+               goto data_copied;
+       }
+
+       /*
+        * Try to avoid low memory issues when a device is suspended.
+        * Use kmalloc() rather than vmalloc() when we can.
+        */
+       dmi = NULL;
+       if (param_kernel->data_size <= KMALLOC_MAX_SIZE) {
+               dmi = kmalloc(param_kernel->data_size, GFP_NOIO | __GFP_NORETRY | __GFP_NOMEMALLOC | __GFP_NOWARN);
+               if (dmi)
+                       *param_flags |= DM_PARAMS_KMALLOC;
+       }
+
+       if (!dmi) {
+               dmi = __vmalloc(param_kernel->data_size, GFP_NOIO | __GFP_REPEAT | __GFP_HIGH, PAGE_KERNEL);
+               if (dmi)
+                       *param_flags |= DM_PARAMS_VMALLOC;
+       }
 
-       dmi = vmalloc(tmp.data_size);
        if (!dmi) {
-               if (secure_data && clear_user(user, tmp.data_size))
+               if (secure_data && clear_user(user, param_kernel->data_size))
                        return -EFAULT;
                return -ENOMEM;
        }
 
-       if (copy_from_user(dmi, user, tmp.data_size))
+       if (copy_from_user(dmi, user, param_kernel->data_size))
                goto bad;
 
+data_copied:
+       /*
+        * Abort if something changed the ioctl data while it was being copied.
+        */
+       if (dmi->data_size != param_kernel->data_size) {
+               DMERR("rejecting ioctl: data size modified while processing parameters");
+               goto bad;
+       }
+
        /* Wipe the user buffer so we do not return it to userspace */
-       if (secure_data && clear_user(user, tmp.data_size))
+       if (secure_data && clear_user(user, param_kernel->data_size))
                goto bad;
 
        *param = dmi;
        return 0;
 
 bad:
-       if (secure_data)
-               memset(dmi, 0, tmp.data_size);
-       vfree(dmi);
+       free_params(dmi, param_kernel->data_size, *param_flags);
+
        return -EFAULT;
 }
 
@@ -1613,11 +1680,13 @@ static int validate_params(uint cmd, struct dm_ioctl *param)
 static int ctl_ioctl(uint command, struct dm_ioctl __user *user)
 {
        int r = 0;
-       int wipe_buffer;
+       int ioctl_flags;
+       int param_flags;
        unsigned int cmd;
        struct dm_ioctl *uninitialized_var(param);
        ioctl_fn fn = NULL;
        size_t input_param_size;
+       struct dm_ioctl param_kernel;
 
        /* only root can play with this */
        if (!capable(CAP_SYS_ADMIN))
@@ -1642,31 +1711,21 @@ static int ctl_ioctl(uint command, struct dm_ioctl __user *user)
        if (cmd == DM_VERSION_CMD)
                return 0;
 
-       fn = lookup_ioctl(cmd);
+       fn = lookup_ioctl(cmd, &ioctl_flags);
        if (!fn) {
                DMWARN("dm_ctl_ioctl: unknown command 0x%x", command);
                return -ENOTTY;
        }
 
        /*
-        * Trying to avoid low memory issues when a device is
-        * suspended.
-        */
-       current->flags |= PF_MEMALLOC;
-
-       /*
         * Copy the parameters into kernel space.
         */
-       r = copy_params(user, &param);
-
-       current->flags &= ~PF_MEMALLOC;
+       r = copy_params(user, &param_kernel, ioctl_flags, &param, &param_flags);
 
        if (r)
                return r;
 
        input_param_size = param->data_size;
-       wipe_buffer = param->flags & DM_SECURE_DATA_FLAG;
-
        r = validate_params(cmd, param);
        if (r)
                goto out;
@@ -1674,6 +1733,10 @@ static int ctl_ioctl(uint command, struct dm_ioctl __user *user)
        param->data_size = sizeof(*param);
        r = fn(param, input_param_size);
 
+       if (unlikely(param->flags & DM_BUFFER_FULL_FLAG) &&
+           unlikely(ioctl_flags & IOCTL_FLAGS_NO_PARAMS))
+               DMERR("ioctl %d tried to output some data but has IOCTL_FLAGS_NO_PARAMS set", cmd);
+
        /*
         * Copy the results back to userland.
         */
@@ -1681,10 +1744,7 @@ static int ctl_ioctl(uint command, struct dm_ioctl __user *user)
                r = -EFAULT;
 
 out:
-       if (wipe_buffer)
-               memset(param, 0, input_param_size);
-
-       vfree(param);
+       free_params(param, input_param_size, param_flags);
        return r;
 }