Skip to content

Commit 02cde50

Browse files
Mikulas Patockakergon
authored andcommitted
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>
1 parent e2914cc commit 02cde50

2 files changed

Lines changed: 37 additions & 21 deletions

File tree

‎drivers/md/dm-ioctl.c‎

Lines changed: 34 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1560,74 +1560,89 @@ static int check_version(unsigned int cmd, struct dm_ioctl __user *user)
15601560
return r;
15611561
}
15621562

1563-
#define DM_PARAMS_VMALLOC 0x0001 /* Params alloced with vmalloc not kmalloc */
1563+
#define DM_PARAMS_KMALLOC 0x0001 /* Params alloced with kmalloc */
1564+
#define DM_PARAMS_VMALLOC 0x0002 /* Params alloced with vmalloc */
15641565
#define DM_WIPE_BUFFER 0x0010 /* Wipe input buffer before returning from ioctl */
15651566

15661567
static void free_params(struct dm_ioctl *param, size_t param_size, int param_flags)
15671568
{
15681569
if (param_flags & DM_WIPE_BUFFER)
15691570
memset(param, 0, param_size);
15701571

1572+
if (param_flags & DM_PARAMS_KMALLOC)
1573+
kfree(param);
15711574
if (param_flags & DM_PARAMS_VMALLOC)
15721575
vfree(param);
1573-
else
1574-
kfree(param);
15751576
}
15761577

1577-
static int copy_params(struct dm_ioctl __user *user, struct dm_ioctl **param, int *param_flags)
1578+
static int copy_params(struct dm_ioctl __user *user, struct dm_ioctl *param_kernel,
1579+
int ioctl_flags,
1580+
struct dm_ioctl **param, int *param_flags)
15781581
{
1579-
struct dm_ioctl tmp, *dmi;
1582+
struct dm_ioctl *dmi;
15801583
int secure_data;
1584+
const size_t minimum_data_size = sizeof(*param_kernel) - sizeof(param_kernel->data);
15811585

1582-
if (copy_from_user(&tmp, user, sizeof(tmp) - sizeof(tmp.data)))
1586+
if (copy_from_user(param_kernel, user, minimum_data_size))
15831587
return -EFAULT;
15841588

1585-
if (tmp.data_size < (sizeof(tmp) - sizeof(tmp.data)))
1589+
if (param_kernel->data_size < minimum_data_size)
15861590
return -EINVAL;
15871591

1588-
secure_data = tmp.flags & DM_SECURE_DATA_FLAG;
1592+
secure_data = param_kernel->flags & DM_SECURE_DATA_FLAG;
15891593

15901594
*param_flags = secure_data ? DM_WIPE_BUFFER : 0;
15911595

1596+
if (ioctl_flags & IOCTL_FLAGS_NO_PARAMS) {
1597+
dmi = param_kernel;
1598+
dmi->data_size = minimum_data_size;
1599+
goto data_copied;
1600+
}
1601+
15921602
/*
15931603
* Try to avoid low memory issues when a device is suspended.
15941604
* Use kmalloc() rather than vmalloc() when we can.
15951605
*/
15961606
dmi = NULL;
1597-
if (tmp.data_size <= KMALLOC_MAX_SIZE)
1598-
dmi = kmalloc(tmp.data_size, GFP_NOIO | __GFP_NORETRY | __GFP_NOMEMALLOC | __GFP_NOWARN);
1607+
if (param_kernel->data_size <= KMALLOC_MAX_SIZE) {
1608+
dmi = kmalloc(param_kernel->data_size, GFP_NOIO | __GFP_NORETRY | __GFP_NOMEMALLOC | __GFP_NOWARN);
1609+
if (dmi)
1610+
*param_flags |= DM_PARAMS_KMALLOC;
1611+
}
15991612

16001613
if (!dmi) {
1601-
dmi = __vmalloc(tmp.data_size, GFP_NOIO | __GFP_REPEAT | __GFP_HIGH, PAGE_KERNEL);
1602-
*param_flags |= DM_PARAMS_VMALLOC;
1614+
dmi = __vmalloc(param_kernel->data_size, GFP_NOIO | __GFP_REPEAT | __GFP_HIGH, PAGE_KERNEL);
1615+
if (dmi)
1616+
*param_flags |= DM_PARAMS_VMALLOC;
16031617
}
16041618

16051619
if (!dmi) {
1606-
if (secure_data && clear_user(user, tmp.data_size))
1620+
if (secure_data && clear_user(user, param_kernel->data_size))
16071621
return -EFAULT;
16081622
return -ENOMEM;
16091623
}
16101624

1611-
if (copy_from_user(dmi, user, tmp.data_size))
1625+
if (copy_from_user(dmi, user, param_kernel->data_size))
16121626
goto bad;
16131627

1628+
data_copied:
16141629
/*
16151630
* Abort if something changed the ioctl data while it was being copied.
16161631
*/
1617-
if (dmi->data_size != tmp.data_size) {
1632+
if (dmi->data_size != param_kernel->data_size) {
16181633
DMERR("rejecting ioctl: data size modified while processing parameters");
16191634
goto bad;
16201635
}
16211636

16221637
/* Wipe the user buffer so we do not return it to userspace */
1623-
if (secure_data && clear_user(user, tmp.data_size))
1638+
if (secure_data && clear_user(user, param_kernel->data_size))
16241639
goto bad;
16251640

16261641
*param = dmi;
16271642
return 0;
16281643

16291644
bad:
1630-
free_params(dmi, tmp.data_size, *param_flags);
1645+
free_params(dmi, param_kernel->data_size, *param_flags);
16311646

16321647
return -EFAULT;
16331648
}
@@ -1671,6 +1686,7 @@ static int ctl_ioctl(uint command, struct dm_ioctl __user *user)
16711686
struct dm_ioctl *uninitialized_var(param);
16721687
ioctl_fn fn = NULL;
16731688
size_t input_param_size;
1689+
struct dm_ioctl param_kernel;
16741690

16751691
/* only root can play with this */
16761692
if (!capable(CAP_SYS_ADMIN))
@@ -1704,7 +1720,7 @@ static int ctl_ioctl(uint command, struct dm_ioctl __user *user)
17041720
/*
17051721
* Copy the parameters into kernel space.
17061722
*/
1707-
r = copy_params(user, &param, &param_flags);
1723+
r = copy_params(user, &param_kernel, ioctl_flags, &param, &param_flags);
17081724

17091725
if (r)
17101726
return r;

‎include/uapi/linux/dm-ioctl.h‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -267,9 +267,9 @@ enum {
267267
#define DM_DEV_SET_GEOMETRY _IOWR(DM_IOCTL, DM_DEV_SET_GEOMETRY_CMD, struct dm_ioctl)
268268

269269
#define DM_VERSION_MAJOR 4
270-
#define DM_VERSION_MINOR 23
271-
#define DM_VERSION_PATCHLEVEL 1
272-
#define DM_VERSION_EXTRA "-ioctl (2012-12-18)"
270+
#define DM_VERSION_MINOR 24
271+
#define DM_VERSION_PATCHLEVEL 0
272+
#define DM_VERSION_EXTRA "-ioctl (2013-01-15)"
273273

274274
/* Status bits */
275275
#define DM_READONLY_FLAG (1 << 0) /* In/Out */

0 commit comments

Comments
 (0)