Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

loop: Add LOOP_CONFIGURE ioctl

This allows userspace to completely setup a loop device with a single
ioctl, removing the in-between state where the device can be partially
configured - eg the loop device has a backing file associated with it,
but is reading from the wrong offset.

Besides removing the intermediate state, another big benefit of this
ioctl is that LOOP_SET_STATUS can be slow; the main reason for this
slowness is that LOOP_SET_STATUS(64) calls blk_mq_freeze_queue() to
freeze the associated queue; this requires waiting for RCU
synchronization, which I've measured can take about 15-20ms on this
device on average.

In addition to doing what LOOP_SET_STATUS can do, LOOP_CONFIGURE can
also be used to:
- Set the correct block size immediately by setting
loop_config.block_size (avoids LOOP_SET_BLOCK_SIZE)
- Explicitly request direct I/O mode by setting LO_FLAGS_DIRECT_IO
in loop_config.info.lo_flags (avoids LOOP_SET_DIRECT_IO)
- Explicitly request read-only mode by setting LO_FLAGS_READ_ONLY
in loop_config.info.lo_flags

Here's setting up ~70 regular loop devices with an offset on an x86
Android device, using LOOP_SET_FD and LOOP_SET_STATUS:

vsoc_x86:/system/apex # time for i in `seq 30 100`;
do losetup -r -o 4096 /dev/block/loop$i com.android.adbd.apex; done
0m03.40s real 0m00.02s user 0m00.03s system

Here's configuring ~70 devices in the same way, but using a modified
losetup that uses the new LOOP_CONFIGURE ioctl:

vsoc_x86:/system/apex # time for i in `seq 30 100`;
do losetup -r -o 4096 /dev/block/loop$i com.android.adbd.apex; done
0m01.94s real 0m00.01s user 0m00.01s system

Signed-off-by: Martijn Coenen <maco@android.com>
Reviewed-by: Christoph Hellwig <hch@lst.de>
Signed-off-by: Jens Axboe <axboe@kernel.dk>

authored by

Martijn Coenen and committed by
Jens Axboe
3448914e faf1d254

+97 -28
+76 -28
drivers/block/loop.c
··· 229 229 } 230 230 231 231 /** 232 + * loop_validate_block_size() - validates the passed in block size 233 + * @bsize: size to validate 234 + */ 235 + static int 236 + loop_validate_block_size(unsigned short bsize) 237 + { 238 + if (bsize < 512 || bsize > PAGE_SIZE || !is_power_of_2(bsize)) 239 + return -EINVAL; 240 + 241 + return 0; 242 + } 243 + 244 + /** 232 245 * loop_set_size() - sets device size and notifies userspace 233 246 * @lo: struct loop_device to set the size for 234 247 * @size: new size of the loop device ··· 1063 1050 return 0; 1064 1051 } 1065 1052 1066 - static int loop_set_fd(struct loop_device *lo, fmode_t mode, 1067 - struct block_device *bdev, unsigned int arg) 1053 + static int loop_configure(struct loop_device *lo, fmode_t mode, 1054 + struct block_device *bdev, 1055 + const struct loop_config *config) 1068 1056 { 1069 1057 struct file *file; 1070 1058 struct inode *inode; 1071 1059 struct address_space *mapping; 1072 1060 struct block_device *claimed_bdev = NULL; 1073 - int lo_flags = 0; 1074 1061 int error; 1075 1062 loff_t size; 1076 1063 bool partscan; 1064 + unsigned short bsize; 1077 1065 1078 1066 /* This is safe, since we have a reference from open(). */ 1079 1067 __module_get(THIS_MODULE); 1080 1068 1081 1069 error = -EBADF; 1082 - file = fget(arg); 1070 + file = fget(config->fd); 1083 1071 if (!file) 1084 1072 goto out; 1085 1073 ··· 1089 1075 * here to avoid changing device under exclusive owner. 1090 1076 */ 1091 1077 if (!(mode & FMODE_EXCL)) { 1092 - claimed_bdev = bd_start_claiming(bdev, loop_set_fd); 1078 + claimed_bdev = bd_start_claiming(bdev, loop_configure); 1093 1079 if (IS_ERR(claimed_bdev)) { 1094 1080 error = PTR_ERR(claimed_bdev); 1095 1081 goto out_putf; ··· 1111 1097 mapping = file->f_mapping; 1112 1098 inode = mapping->host; 1113 1099 1100 + size = get_loop_size(lo, file); 1101 + 1102 + if ((config->info.lo_flags & ~LOOP_CONFIGURE_SETTABLE_FLAGS) != 0) { 1103 + error = -EINVAL; 1104 + goto out_unlock; 1105 + } 1106 + 1107 + if (config->block_size) { 1108 + error = loop_validate_block_size(config->block_size); 1109 + if (error) 1110 + goto out_unlock; 1111 + } 1112 + 1113 + error = loop_set_status_from_info(lo, &config->info); 1114 + if (error) 1115 + goto out_unlock; 1116 + 1114 1117 if (!(file->f_mode & FMODE_WRITE) || !(mode & FMODE_WRITE) || 1115 1118 !file->f_op->write_iter) 1116 - lo_flags |= LO_FLAGS_READ_ONLY; 1117 - 1118 - size = get_loop_size(lo, file); 1119 + lo->lo_flags |= LO_FLAGS_READ_ONLY; 1119 1120 1120 1121 error = loop_prepare_queue(lo); 1121 1122 if (error) ··· 1138 1109 1139 1110 error = 0; 1140 1111 1141 - set_device_ro(bdev, (lo_flags & LO_FLAGS_READ_ONLY) != 0); 1112 + set_device_ro(bdev, (lo->lo_flags & LO_FLAGS_READ_ONLY) != 0); 1142 1113 1143 - lo->use_dio = false; 1114 + lo->use_dio = lo->lo_flags & LO_FLAGS_DIRECT_IO; 1144 1115 lo->lo_device = bdev; 1145 - lo->lo_flags = lo_flags; 1146 1116 lo->lo_backing_file = file; 1147 - lo->transfer = NULL; 1148 - lo->ioctl = NULL; 1149 - lo->lo_sizelimit = 0; 1150 1117 lo->old_gfp_mask = mapping_gfp_mask(mapping); 1151 1118 mapping_set_gfp_mask(mapping, lo->old_gfp_mask & ~(__GFP_IO|__GFP_FS)); 1152 1119 1153 - if (!(lo_flags & LO_FLAGS_READ_ONLY) && file->f_op->fsync) 1120 + if (!(lo->lo_flags & LO_FLAGS_READ_ONLY) && file->f_op->fsync) 1154 1121 blk_queue_write_cache(lo->lo_queue, true, false); 1155 1122 1156 - if (io_is_direct(lo->lo_backing_file) && inode->i_sb->s_bdev) { 1123 + if (config->block_size) 1124 + bsize = config->block_size; 1125 + else if (io_is_direct(lo->lo_backing_file) && inode->i_sb->s_bdev) 1157 1126 /* In case of direct I/O, match underlying block size */ 1158 - unsigned short bsize = bdev_logical_block_size( 1159 - inode->i_sb->s_bdev); 1127 + bsize = bdev_logical_block_size(inode->i_sb->s_bdev); 1128 + else 1129 + bsize = 512; 1160 1130 1161 - blk_queue_logical_block_size(lo->lo_queue, bsize); 1162 - blk_queue_physical_block_size(lo->lo_queue, bsize); 1163 - blk_queue_io_min(lo->lo_queue, bsize); 1164 - } 1131 + blk_queue_logical_block_size(lo->lo_queue, bsize); 1132 + blk_queue_physical_block_size(lo->lo_queue, bsize); 1133 + blk_queue_io_min(lo->lo_queue, bsize); 1165 1134 1166 1135 loop_update_rotational(lo); 1167 1136 loop_update_dio(lo); ··· 1182 1155 if (partscan) 1183 1156 loop_reread_partitions(lo, bdev); 1184 1157 if (claimed_bdev) 1185 - bd_abort_claiming(bdev, claimed_bdev, loop_set_fd); 1158 + bd_abort_claiming(bdev, claimed_bdev, loop_configure); 1186 1159 return 0; 1187 1160 1188 1161 out_unlock: 1189 1162 mutex_unlock(&loop_ctl_mutex); 1190 1163 out_bdev: 1191 1164 if (claimed_bdev) 1192 - bd_abort_claiming(bdev, claimed_bdev, loop_set_fd); 1165 + bd_abort_claiming(bdev, claimed_bdev, loop_configure); 1193 1166 out_putf: 1194 1167 fput(file); 1195 1168 out: ··· 1609 1582 if (lo->lo_state != Lo_bound) 1610 1583 return -ENXIO; 1611 1584 1612 - if (arg < 512 || arg > PAGE_SIZE || !is_power_of_2(arg)) 1613 - return -EINVAL; 1585 + err = loop_validate_block_size(arg); 1586 + if (err) 1587 + return err; 1614 1588 1615 1589 if (lo->lo_queue->limits.logical_block_size == arg) 1616 1590 return 0; ··· 1673 1645 int err; 1674 1646 1675 1647 switch (cmd) { 1676 - case LOOP_SET_FD: 1677 - return loop_set_fd(lo, mode, bdev, arg); 1648 + case LOOP_SET_FD: { 1649 + /* 1650 + * Legacy case - pass in a zeroed out struct loop_config with 1651 + * only the file descriptor set , which corresponds with the 1652 + * default parameters we'd have used otherwise. 1653 + */ 1654 + struct loop_config config; 1655 + 1656 + memset(&config, 0, sizeof(config)); 1657 + config.fd = arg; 1658 + 1659 + return loop_configure(lo, mode, bdev, &config); 1660 + } 1661 + case LOOP_CONFIGURE: { 1662 + struct loop_config config; 1663 + 1664 + if (copy_from_user(&config, argp, sizeof(config))) 1665 + return -EFAULT; 1666 + 1667 + return loop_configure(lo, mode, bdev, &config); 1668 + } 1678 1669 case LOOP_CHANGE_FD: 1679 1670 return loop_change_fd(lo, bdev, arg); 1680 1671 case LOOP_CLR_FD: ··· 1865 1818 case LOOP_CLR_FD: 1866 1819 case LOOP_GET_STATUS64: 1867 1820 case LOOP_SET_STATUS64: 1821 + case LOOP_CONFIGURE: 1868 1822 arg = (unsigned long) compat_ptr(arg); 1869 1823 /* fall through */ 1870 1824 case LOOP_SET_FD:
+21
include/uapi/linux/loop.h
··· 31 31 /* LO_FLAGS that can be cleared using LOOP_SET_STATUS(64) */ 32 32 #define LOOP_SET_STATUS_CLEARABLE_FLAGS (LO_FLAGS_AUTOCLEAR) 33 33 34 + /* LO_FLAGS that can be set using LOOP_CONFIGURE */ 35 + #define LOOP_CONFIGURE_SETTABLE_FLAGS (LO_FLAGS_READ_ONLY | LO_FLAGS_AUTOCLEAR \ 36 + | LO_FLAGS_PARTSCAN | LO_FLAGS_DIRECT_IO) 37 + 34 38 #include <asm/posix_types.h> /* for __kernel_old_dev_t */ 35 39 #include <linux/types.h> /* for __u64 */ 36 40 ··· 70 66 __u64 lo_init[2]; 71 67 }; 72 68 69 + /** 70 + * struct loop_config - Complete configuration for a loop device. 71 + * @fd: fd of the file to be used as a backing file for the loop device. 72 + * @block_size: block size to use; ignored if 0. 73 + * @info: struct loop_info64 to configure the loop device with. 74 + * 75 + * This structure is used with the LOOP_CONFIGURE ioctl, and can be used to 76 + * atomically setup and configure all loop device parameters at once. 77 + */ 78 + struct loop_config { 79 + __u32 fd; 80 + __u32 block_size; 81 + struct loop_info64 info; 82 + __u64 __reserved[8]; 83 + }; 84 + 73 85 /* 74 86 * Loop filter types 75 87 */ ··· 116 96 #define LOOP_SET_CAPACITY 0x4C07 117 97 #define LOOP_SET_DIRECT_IO 0x4C08 118 98 #define LOOP_SET_BLOCK_SIZE 0x4C09 99 + #define LOOP_CONFIGURE 0x4C0A 119 100 120 101 /* /dev/loop-control interface */ 121 102 #define LOOP_CTL_ADD 0x4C80