dm ioctl: optimize functions without variable params

Device-mapper ioctls receive and send data in a buffer supplied
by userspace.  The buffer has two parts.  The first part contains
a 'struct dm_ioctl' and has a fixed size.  The second part depends
on the ioctl and has a variable size.

This patch recognises the specific ioctls that do not use the variable
part of the buffer and skips allocating memory for it.

In particular, when a device is suspended and a resume ioctl is sent,
this now avoid memory allocation completely.

The variable "struct dm_ioctl tmp" is moved from the function
copy_params to its caller ctl_ioctl and renamed to param_kernel.
It is used directly when the ioctl function doesn't need any arguments.

Signed-off-by: Mikulas Patocka <mpatocka@redhat.com>
Signed-off-by: Alasdair G Kergon <agk@redhat.com>
This commit is contained in:
Mikulas Patocka 2013-03-01 22:45:49 +00:00 коммит произвёл Alasdair G Kergon
Родитель e2914cc26b
Коммит 02cde50b7e
2 изменённых файлов: 39 добавлений и 23 удалений

Просмотреть файл

@ -1560,7 +1560,8 @@ static int check_version(unsigned int cmd, struct dm_ioctl __user *user)
return r; return r;
} }
#define DM_PARAMS_VMALLOC 0x0001 /* Params alloced with vmalloc not kmalloc */ #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 */ #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) static void free_params(struct dm_ioctl *param, size_t param_size, int param_flags)
@ -1568,66 +1569,80 @@ static void free_params(struct dm_ioctl *param, size_t param_size, int param_fla
if (param_flags & DM_WIPE_BUFFER) if (param_flags & DM_WIPE_BUFFER)
memset(param, 0, param_size); memset(param, 0, param_size);
if (param_flags & DM_PARAMS_KMALLOC)
kfree(param);
if (param_flags & DM_PARAMS_VMALLOC) if (param_flags & DM_PARAMS_VMALLOC)
vfree(param); vfree(param);
else
kfree(param);
} }
static int copy_params(struct dm_ioctl __user *user, struct dm_ioctl **param, int *param_flags) 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; 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; return -EFAULT;
if (tmp.data_size < (sizeof(tmp) - sizeof(tmp.data))) if (param_kernel->data_size < minimum_data_size)
return -EINVAL; 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; *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. * Try to avoid low memory issues when a device is suspended.
* Use kmalloc() rather than vmalloc() when we can. * Use kmalloc() rather than vmalloc() when we can.
*/ */
dmi = NULL; dmi = NULL;
if (tmp.data_size <= KMALLOC_MAX_SIZE) if (param_kernel->data_size <= KMALLOC_MAX_SIZE) {
dmi = kmalloc(tmp.data_size, GFP_NOIO | __GFP_NORETRY | __GFP_NOMEMALLOC | __GFP_NOWARN); dmi = kmalloc(param_kernel->data_size, GFP_NOIO | __GFP_NORETRY | __GFP_NOMEMALLOC | __GFP_NOWARN);
if (dmi)
if (!dmi) { *param_flags |= DM_PARAMS_KMALLOC;
dmi = __vmalloc(tmp.data_size, GFP_NOIO | __GFP_REPEAT | __GFP_HIGH, PAGE_KERNEL);
*param_flags |= DM_PARAMS_VMALLOC;
} }
if (!dmi) { if (!dmi) {
if (secure_data && clear_user(user, tmp.data_size)) dmi = __vmalloc(param_kernel->data_size, GFP_NOIO | __GFP_REPEAT | __GFP_HIGH, PAGE_KERNEL);
if (dmi)
*param_flags |= DM_PARAMS_VMALLOC;
}
if (!dmi) {
if (secure_data && clear_user(user, param_kernel->data_size))
return -EFAULT; return -EFAULT;
return -ENOMEM; return -ENOMEM;
} }
if (copy_from_user(dmi, user, tmp.data_size)) if (copy_from_user(dmi, user, param_kernel->data_size))
goto bad; goto bad;
data_copied:
/* /*
* Abort if something changed the ioctl data while it was being copied. * Abort if something changed the ioctl data while it was being copied.
*/ */
if (dmi->data_size != tmp.data_size) { if (dmi->data_size != param_kernel->data_size) {
DMERR("rejecting ioctl: data size modified while processing parameters"); DMERR("rejecting ioctl: data size modified while processing parameters");
goto bad; goto bad;
} }
/* Wipe the user buffer so we do not return it to userspace */ /* 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; goto bad;
*param = dmi; *param = dmi;
return 0; return 0;
bad: bad:
free_params(dmi, tmp.data_size, *param_flags); free_params(dmi, param_kernel->data_size, *param_flags);
return -EFAULT; return -EFAULT;
} }
@ -1671,6 +1686,7 @@ static int ctl_ioctl(uint command, struct dm_ioctl __user *user)
struct dm_ioctl *uninitialized_var(param); struct dm_ioctl *uninitialized_var(param);
ioctl_fn fn = NULL; ioctl_fn fn = NULL;
size_t input_param_size; size_t input_param_size;
struct dm_ioctl param_kernel;
/* only root can play with this */ /* only root can play with this */
if (!capable(CAP_SYS_ADMIN)) if (!capable(CAP_SYS_ADMIN))
@ -1704,7 +1720,7 @@ static int ctl_ioctl(uint command, struct dm_ioctl __user *user)
/* /*
* Copy the parameters into kernel space. * Copy the parameters into kernel space.
*/ */
r = copy_params(user, &param, &param_flags); r = copy_params(user, &param_kernel, ioctl_flags, &param, &param_flags);
if (r) if (r)
return r; return r;

Просмотреть файл

@ -267,9 +267,9 @@ enum {
#define DM_DEV_SET_GEOMETRY _IOWR(DM_IOCTL, DM_DEV_SET_GEOMETRY_CMD, struct dm_ioctl) #define DM_DEV_SET_GEOMETRY _IOWR(DM_IOCTL, DM_DEV_SET_GEOMETRY_CMD, struct dm_ioctl)
#define DM_VERSION_MAJOR 4 #define DM_VERSION_MAJOR 4
#define DM_VERSION_MINOR 23 #define DM_VERSION_MINOR 24
#define DM_VERSION_PATCHLEVEL 1 #define DM_VERSION_PATCHLEVEL 0
#define DM_VERSION_EXTRA "-ioctl (2012-12-18)" #define DM_VERSION_EXTRA "-ioctl (2013-01-15)"
/* Status bits */ /* Status bits */
#define DM_READONLY_FLAG (1 << 0) /* In/Out */ #define DM_READONLY_FLAG (1 << 0) /* In/Out */