88
88
89
89
static DEFINE_IDR (loop_index_idr );
90
90
static DEFINE_MUTEX (loop_ctl_mutex );
91
+ static DEFINE_MUTEX (loop_validate_mutex );
92
+
93
+ /**
94
+ * loop_global_lock_killable() - take locks for safe loop_validate_file() test
95
+ *
96
+ * @lo: struct loop_device
97
+ * @global: true if @lo is about to bind another "struct loop_device", false otherwise
98
+ *
99
+ * Returns 0 on success, -EINTR otherwise.
100
+ *
101
+ * Since loop_validate_file() traverses on other "struct loop_device" if
102
+ * is_loop_device() is true, we need a global lock for serializing concurrent
103
+ * loop_configure()/loop_change_fd()/__loop_clr_fd() calls.
104
+ */
105
+ static int loop_global_lock_killable (struct loop_device * lo , bool global )
106
+ {
107
+ int err ;
108
+
109
+ if (global ) {
110
+ err = mutex_lock_killable (& loop_validate_mutex );
111
+ if (err )
112
+ return err ;
113
+ }
114
+ err = mutex_lock_killable (& lo -> lo_mutex );
115
+ if (err && global )
116
+ mutex_unlock (& loop_validate_mutex );
117
+ return err ;
118
+ }
119
+
120
+ /**
121
+ * loop_global_unlock() - release locks taken by loop_global_lock_killable()
122
+ *
123
+ * @lo: struct loop_device
124
+ * @global: true if @lo was about to bind another "struct loop_device", false otherwise
125
+ */
126
+ static void loop_global_unlock (struct loop_device * lo , bool global )
127
+ {
128
+ mutex_unlock (& lo -> lo_mutex );
129
+ if (global )
130
+ mutex_unlock (& loop_validate_mutex );
131
+ }
91
132
92
133
static int max_part ;
93
134
static int part_shift ;
@@ -672,13 +713,15 @@ static int loop_validate_file(struct file *file, struct block_device *bdev)
672
713
while (is_loop_device (f )) {
673
714
struct loop_device * l ;
674
715
716
+ lockdep_assert_held (& loop_validate_mutex );
675
717
if (f -> f_mapping -> host -> i_rdev == bdev -> bd_dev )
676
718
return - EBADF ;
677
719
678
720
l = I_BDEV (f -> f_mapping -> host )-> bd_disk -> private_data ;
679
- if (l -> lo_state != Lo_bound ) {
721
+ if (l -> lo_state != Lo_bound )
680
722
return - EINVAL ;
681
- }
723
+ /* Order wrt setting lo->lo_backing_file in loop_configure(). */
724
+ rmb ();
682
725
f = l -> lo_backing_file ;
683
726
}
684
727
if (!S_ISREG (inode -> i_mode ) && !S_ISBLK (inode -> i_mode ))
@@ -697,13 +740,18 @@ static int loop_validate_file(struct file *file, struct block_device *bdev)
697
740
static int loop_change_fd (struct loop_device * lo , struct block_device * bdev ,
698
741
unsigned int arg )
699
742
{
700
- struct file * file = NULL , * old_file ;
701
- int error ;
702
- bool partscan ;
743
+ struct file * file = fget (arg );
744
+ struct file * old_file ;
745
+ int error ;
746
+ bool partscan ;
747
+ bool is_loop ;
703
748
704
- error = mutex_lock_killable (& lo -> lo_mutex );
749
+ if (!file )
750
+ return - EBADF ;
751
+ is_loop = is_loop_device (file );
752
+ error = loop_global_lock_killable (lo , is_loop );
705
753
if (error )
706
- return error ;
754
+ goto out_putf ;
707
755
error = - ENXIO ;
708
756
if (lo -> lo_state != Lo_bound )
709
757
goto out_err ;
@@ -713,11 +761,6 @@ static int loop_change_fd(struct loop_device *lo, struct block_device *bdev,
713
761
if (!(lo -> lo_flags & LO_FLAGS_READ_ONLY ))
714
762
goto out_err ;
715
763
716
- error = - EBADF ;
717
- file = fget (arg );
718
- if (!file )
719
- goto out_err ;
720
-
721
764
error = loop_validate_file (file , bdev );
722
765
if (error )
723
766
goto out_err ;
@@ -740,7 +783,16 @@ static int loop_change_fd(struct loop_device *lo, struct block_device *bdev,
740
783
loop_update_dio (lo );
741
784
blk_mq_unfreeze_queue (lo -> lo_queue );
742
785
partscan = lo -> lo_flags & LO_FLAGS_PARTSCAN ;
743
- mutex_unlock (& lo -> lo_mutex );
786
+ loop_global_unlock (lo , is_loop );
787
+
788
+ /*
789
+ * Flush loop_validate_file() before fput(), for l->lo_backing_file
790
+ * might be pointing at old_file which might be the last reference.
791
+ */
792
+ if (!is_loop ) {
793
+ mutex_lock (& loop_validate_mutex );
794
+ mutex_unlock (& loop_validate_mutex );
795
+ }
744
796
/*
745
797
* We must drop file reference outside of lo_mutex as dropping
746
798
* the file ref can take open_mutex which creates circular locking
@@ -752,9 +804,9 @@ static int loop_change_fd(struct loop_device *lo, struct block_device *bdev,
752
804
return 0 ;
753
805
754
806
out_err :
755
- mutex_unlock ( & lo -> lo_mutex );
756
- if ( file )
757
- fput (file );
807
+ loop_global_unlock ( lo , is_loop );
808
+ out_putf :
809
+ fput (file );
758
810
return error ;
759
811
}
760
812
@@ -1136,22 +1188,22 @@ static int loop_configure(struct loop_device *lo, fmode_t mode,
1136
1188
struct block_device * bdev ,
1137
1189
const struct loop_config * config )
1138
1190
{
1139
- struct file * file ;
1140
- struct inode * inode ;
1191
+ struct file * file = fget ( config -> fd ) ;
1192
+ struct inode * inode ;
1141
1193
struct address_space * mapping ;
1142
- int error ;
1143
- loff_t size ;
1144
- bool partscan ;
1145
- unsigned short bsize ;
1194
+ int error ;
1195
+ loff_t size ;
1196
+ bool partscan ;
1197
+ unsigned short bsize ;
1198
+ bool is_loop ;
1199
+
1200
+ if (!file )
1201
+ return - EBADF ;
1202
+ is_loop = is_loop_device (file );
1146
1203
1147
1204
/* This is safe, since we have a reference from open(). */
1148
1205
__module_get (THIS_MODULE );
1149
1206
1150
- error = - EBADF ;
1151
- file = fget (config -> fd );
1152
- if (!file )
1153
- goto out ;
1154
-
1155
1207
/*
1156
1208
* If we don't hold exclusive handle for the device, upgrade to it
1157
1209
* here to avoid changing device under exclusive owner.
@@ -1162,7 +1214,7 @@ static int loop_configure(struct loop_device *lo, fmode_t mode,
1162
1214
goto out_putf ;
1163
1215
}
1164
1216
1165
- error = mutex_lock_killable ( & lo -> lo_mutex );
1217
+ error = loop_global_lock_killable ( lo , is_loop );
1166
1218
if (error )
1167
1219
goto out_bdev ;
1168
1220
@@ -1242,6 +1294,9 @@ static int loop_configure(struct loop_device *lo, fmode_t mode,
1242
1294
size = get_loop_size (lo , file );
1243
1295
loop_set_size (lo , size );
1244
1296
1297
+ /* Order wrt reading lo_state in loop_validate_file(). */
1298
+ wmb ();
1299
+
1245
1300
lo -> lo_state = Lo_bound ;
1246
1301
if (part_shift )
1247
1302
lo -> lo_flags |= LO_FLAGS_PARTSCAN ;
@@ -1253,21 +1308,20 @@ static int loop_configure(struct loop_device *lo, fmode_t mode,
1253
1308
* put /dev/loopXX inode. Later in __loop_clr_fd() we bdput(bdev).
1254
1309
*/
1255
1310
bdgrab (bdev );
1256
- mutex_unlock ( & lo -> lo_mutex );
1311
+ loop_global_unlock ( lo , is_loop );
1257
1312
if (partscan )
1258
1313
loop_reread_partitions (lo );
1259
1314
if (!(mode & FMODE_EXCL ))
1260
1315
bd_abort_claiming (bdev , loop_configure );
1261
1316
return 0 ;
1262
1317
1263
1318
out_unlock :
1264
- mutex_unlock ( & lo -> lo_mutex );
1319
+ loop_global_unlock ( lo , is_loop );
1265
1320
out_bdev :
1266
1321
if (!(mode & FMODE_EXCL ))
1267
1322
bd_abort_claiming (bdev , loop_configure );
1268
1323
out_putf :
1269
1324
fput (file );
1270
- out :
1271
1325
/* This is safe: open() is still holding a reference. */
1272
1326
module_put (THIS_MODULE );
1273
1327
return error ;
@@ -1283,6 +1337,18 @@ static int __loop_clr_fd(struct loop_device *lo, bool release)
1283
1337
int lo_number ;
1284
1338
struct loop_worker * pos , * worker ;
1285
1339
1340
+ /*
1341
+ * Flush loop_configure() and loop_change_fd(). It is acceptable for
1342
+ * loop_validate_file() to succeed, for actual clear operation has not
1343
+ * started yet.
1344
+ */
1345
+ mutex_lock (& loop_validate_mutex );
1346
+ mutex_unlock (& loop_validate_mutex );
1347
+ /*
1348
+ * loop_validate_file() now fails because l->lo_state != Lo_bound
1349
+ * became visible.
1350
+ */
1351
+
1286
1352
mutex_lock (& lo -> lo_mutex );
1287
1353
if (WARN_ON_ONCE (lo -> lo_state != Lo_rundown )) {
1288
1354
err = - ENXIO ;
0 commit comments