Skip to contentSkip to conversation

tidy up direct I/O flag handling in the loop driver

PATCH New Christoph Hellwig 2 participants 10 messages Jan 10, 2025 Block layer

lore
Share & tools
lore
#0 lore raw

Hi Jens,

this series fixes a few inconsistencies and cleans up the direct I/O flag handling in the loop driver. It sits on top of the recently posted "fix queue freeze and limit locking order v4" series.

Diffstat:

 loop.c |  123 ++++++++++++++++++++++++++++++++---------------------------------
 1 file changed, 61 insertions(+), 62 deletions(-)
#1 lore raw

PATCH 1/8 loop: move updating lo_flags out of loop_set_status_from_info

While loop_configure simplify assigns the flags passed in by userspace, loop_set_status only looks at the two changeable flags, and currently has to do a complicate dance to implement that.

Move assign lo->lo_flags out of loop_set_status_from_info into the callers and thus drastically simplify the lo_flags handling in loop_set_status.

---
 drivers/block/loop.c | 23 ++++++++---------------
 1 file changed, 8 insertions(+), 15 deletions(-)
drivers/block/loop.c +8 -15
index 15e486baa223..6ea729cdce71 100644
@@ -971,7 +971,6 @@ loop_set_status_from_info(struct loop_device *lo,
 
 	memcpy(lo->lo_file_name, info->lo_file_name, LO_NAME_SIZE);
 	lo->lo_file_name[LO_NAME_SIZE-1] = 0;
-	lo->lo_flags = info->lo_flags;
 	return 0;
 }
 
@@ -1069,6 +1068,7 @@ static int loop_configure(struct loop_device *lo, blk_mode_t mode,
 	error = loop_set_status_from_info(lo, &config->info);
 	if (error)
 		goto out_unlock;
+	lo->lo_flags = config->info.lo_flags;
 
 	if (!(file->f_mode & FMODE_WRITE) || !(mode & BLK_OPEN_WRITE) ||
 	    !file->f_op->write_iter)
@@ -1258,7 +1258,6 @@ static int
 loop_set_status(struct loop_device *lo, const struct loop_info64 *info)
 {
 	int err;
-	int prev_lo_flags;
 	bool partscan = false;
 	bool size_changed = false;
 
@@ -1280,18 +1279,16 @@ loop_set_status(struct loop_device *lo, const struct loop_info64 *info)
 	/* I/O need to be drained during transfer transition */
 	blk_mq_freeze_queue(lo->lo_queue);
 
-	prev_lo_flags = lo->lo_flags;
-
 	err = loop_set_status_from_info(lo, info);
 	if (err)
 		goto out_unfreeze;
 
-	/* Mask out flags that can't be set using LOOP_SET_STATUS. */
-	lo->lo_flags &= LOOP_SET_STATUS_SETTABLE_FLAGS;
-	/* For those flags, use the previous values instead */
-	lo->lo_flags |= prev_lo_flags & ~LOOP_SET_STATUS_SETTABLE_FLAGS;
-	/* For flags that can't be cleared, use previous values too */
-	lo->lo_flags |= prev_lo_flags & ~LOOP_SET_STATUS_CLEARABLE_FLAGS;
+	partscan = !(lo->lo_flags & LO_FLAGS_PARTSCAN) &&
+		(info->lo_flags & LO_FLAGS_PARTSCAN);
+
+	lo->lo_flags &= ~(LOOP_SET_STATUS_SETTABLE_FLAGS |
+			  LOOP_SET_STATUS_CLEARABLE_FLAGS);
+	lo->lo_flags |= (info->lo_flags & LOOP_SET_STATUS_SETTABLE_FLAGS);
 
 	if (size_changed) {
 		loff_t new_size = get_size(lo->lo_offset, lo->lo_sizelimit,
@@ -1304,12 +1301,8 @@ loop_set_status(struct loop_device *lo, const struct loop_info64 *info)
 
 out_unfreeze:
 	blk_mq_unfreeze_queue(lo->lo_queue);
-
-	if (!err && (lo->lo_flags & LO_FLAGS_PARTSCAN) &&
-	     !(prev_lo_flags & LO_FLAGS_PARTSCAN)) {
+	if (partscan)
 		clear_bit(GD_SUPPRESS_PART_SCAN, &lo->lo_disk->state);
-		partscan = true;
-	}
 out_unlock:
 	mutex_unlock(&lo->lo_mutex);
 	if (partscan)
Signature · 1 line
2.45.2
#2 lore raw

PATCH 2/8 loop: update commands in loop_set_status still referring to transfers

In reply to Christoph Hellwig

The concept of transfers is gone since commit 47e9624616c8 ("block:
remove support for cryptoloop and the xor transfer").

---
 drivers/block/loop.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/block/loop.c +2 -2
index 6ea729cdce71..0c7dfc6eee12 100644
@@ -1276,7 +1276,7 @@ loop_set_status(struct loop_device *lo, const struct loop_info64 *info)
 		invalidate_bdev(lo->lo_device);
 	}
 
-	/* I/O need to be drained during transfer transition */
+	/* I/O needs to be drained before changing lo_offset or lo_sizelimit */
 	blk_mq_freeze_queue(lo->lo_queue);
 
 	err = loop_set_status_from_info(lo, info);
@@ -1296,7 +1296,7 @@ loop_set_status(struct loop_device *lo, const struct loop_info64 *info)
 		loop_set_size(lo, new_size);
 	}
 
-	/* update dio if lo_offset or transfer is changed */
+	/* update the direct I/O flag if lo_offset changed */
 	__loop_update_dio(lo, lo->use_dio);
 
 out_unfreeze:
Signature · 1 line
2.45.2
#3 lore raw

PATCH 3/8 loop: create a lo_can_use_dio helper

In reply to Christoph Hellwig

Factor out a part of __loop_update_dio in preparation for further refactoring.

---
 drivers/block/loop.c | 25 ++++++++++++++-----------
 1 file changed, 14 insertions(+), 11 deletions(-)
drivers/block/loop.c +14 -11
index 0c7dfc6eee12..55bea9c95b45 100644
@@ -182,26 +182,29 @@ static bool lo_bdev_can_use_dio(struct loop_device *lo,
 	return true;
 }
 
-static void __loop_update_dio(struct loop_device *lo, bool dio)
+static bool lo_can_use_dio(struct loop_device *lo)
 {
-	struct file *file = lo->lo_backing_file;
-	struct inode *inode = file->f_mapping->host;
-	struct block_device *backing_bdev = NULL;
-	bool use_dio;
+	struct inode *inode = lo->lo_backing_file->f_mapping->host;
+
+	if (!(lo->lo_backing_file->f_mode & FMODE_CAN_ODIRECT))
+		return false;
 
 	if (S_ISBLK(inode->i_mode))
-		backing_bdev = I_BDEV(inode);
-	else if (inode->i_sb->s_bdev)
-		backing_bdev = inode->i_sb->s_bdev;
+		return lo_bdev_can_use_dio(lo, I_BDEV(inode));
+	if (inode->i_sb->s_bdev)
+		return lo_bdev_can_use_dio(lo, inode->i_sb->s_bdev);
+	return true;
+}
 
-	use_dio = dio && (file->f_mode & FMODE_CAN_ODIRECT) &&
-		(!backing_bdev || lo_bdev_can_use_dio(lo, backing_bdev));
+static void __loop_update_dio(struct loop_device *lo, bool dio)
+{
+	bool use_dio = dio && lo_can_use_dio(lo);
 
 	if (lo->use_dio == use_dio)
 		return;
 
 	/* flush dirty pages before changing direct IO */
-	vfs_fsync(file, 0);
+	vfs_fsync(lo->lo_backing_file, 0);
 
 	/*
 	 * The flag of LO_FLAGS_DIRECT_IO is handled similarly with
Signature · 1 line
2.45.2
#4 lore raw

PATCH 4/8 loop: only write back pagecache when starting to to use direct I/O

In reply to Christoph Hellwig

There is no point in doing an fdatasync to write out pages when switching away from direct I/O, as there won't be any. The writeback is only needed when switching to direct I/O, which would have to invalidate the pagecache less efficiently from the I/O path.

---
 drivers/block/loop.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
drivers/block/loop.c +3 -2
index 55bea9c95b45..cf80cdf5e440 100644
@@ -203,8 +203,9 @@ static void __loop_update_dio(struct loop_device *lo, bool dio)
 	if (lo->use_dio == use_dio)
 		return;
 
-	/* flush dirty pages before changing direct IO */
-	vfs_fsync(lo->lo_backing_file, 0);
+	/* flush dirty pages before starting to use direct I/O */
+	if (use_dio)
+		vfs_fsync(lo->lo_backing_file, 0);
 
 	/*
 	 * The flag of LO_FLAGS_DIRECT_IO is handled similarly with
Signature · 1 line
2.45.2
#5 lore raw

PATCH 5/8 loop: open code the direct I/O flag update in loop_set_dio

In reply to Christoph Hellwig

loop_set_dio is different from the other (__)loop_update_dio callers in that it doesn't take any implicit conditions into account and wants to update the direct I/O flag to the user passed in value and fail if that can't be done.

Open code the logic here to prepare for simplifying the other direct I/O flag updates and to make the error handling less convoluted.

---
 drivers/block/loop.c | 28 ++++++++++++++++++++--------
 1 file changed, 20 insertions(+), 8 deletions(-)
drivers/block/loop.c +20 -8
index cf80cdf5e440..6eb6d901151c 100644
@@ -1455,16 +1455,28 @@ static int loop_set_capacity(struct loop_device *lo)
 
 static int loop_set_dio(struct loop_device *lo, unsigned long arg)
 {
-	int error = -ENXIO;
-	if (lo->lo_state != Lo_bound)
-		goto out;
+	bool use_dio = !!arg;
 
-	__loop_update_dio(lo, !!arg);
-	if (lo->use_dio == !!arg)
+	if (lo->lo_state != Lo_bound)
+		return -ENXIO;
+	if (use_dio == lo->use_dio)
 		return 0;
-	error = -EINVAL;
- out:
-	return error;
+
+	if (use_dio) {
+		if (!lo_can_use_dio(lo))
+			return -EINVAL;
+		/* flush dirty pages before starting to use direct I/O */
+		vfs_fsync(lo->lo_backing_file, 0);
+	}
+
+	blk_mq_freeze_queue(lo->lo_queue);
+	lo->use_dio = use_dio;
+	if (use_dio)
+		lo->lo_flags |= LO_FLAGS_DIRECT_IO;
+	else
+		lo->lo_flags &= ~LO_FLAGS_DIRECT_IO;
+	blk_mq_unfreeze_queue(lo->lo_queue);
+	return 0;
 }
 
 static int loop_set_block_size(struct loop_device *lo, unsigned long arg)
Signature · 1 line
2.45.2
#6 lore raw

PATCH 6/8 loop: allow loop_set_status to re-enable direct I/O

In reply to Christoph Hellwig

Unlike all other calls of (__)loop_update_dio, loop_set_status never looks at the O_DIRECT flag of the backing file, and thus doesn't re-enable direct I/O on an O_DIRECT backing file if e.g. the new block size would allow it. Fix that and remove the need for the separate __loop_update_dio flag.

---
 drivers/block/loop.c | 11 +++--------
 1 file changed, 3 insertions(+), 8 deletions(-)
drivers/block/loop.c +3 -8
index 6eb6d901151c..2e1f8aa045a9 100644
@@ -196,8 +196,9 @@ static bool lo_can_use_dio(struct loop_device *lo)
 	return true;
 }
 
-static void __loop_update_dio(struct loop_device *lo, bool dio)
+static inline void loop_update_dio(struct loop_device *lo)
 {
+	bool dio = lo->use_dio || (lo->lo_backing_file->f_flags & O_DIRECT);
 	bool use_dio = dio && lo_can_use_dio(lo);
 
 	if (lo->use_dio == use_dio)
@@ -531,12 +532,6 @@ static int do_req_filebacked(struct loop_device *lo, struct request *rq)
 	}
 }
 
-static inline void loop_update_dio(struct loop_device *lo)
-{
-	__loop_update_dio(lo, (lo->lo_backing_file->f_flags & O_DIRECT) |
-				lo->use_dio);
-}
-
 static void loop_reread_partitions(struct loop_device *lo)
 {
 	int rc;
@@ -1301,7 +1296,7 @@ loop_set_status(struct loop_device *lo, const struct loop_info64 *info)
 	}
 
 	/* update the direct I/O flag if lo_offset changed */
-	__loop_update_dio(lo, lo->use_dio);
+	loop_update_dio(lo);
 
 out_unfreeze:
 	blk_mq_unfreeze_queue(lo->lo_queue);
Signature · 1 line
2.45.2
#7 lore raw

PATCH 7/8 loop: don't freeze the queue in loop_update_dio

In reply to Christoph Hellwig

All callers of loop_update_dio except for loop_configure already have the queue frozen, and loop_configure works on an unbound device. Remove the superfluous recursive freezing in loop_update_dio and add asserts for the locking and freezing state instead.

---
 drivers/block/loop.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
drivers/block/loop.c +4 -4
index 2e1f8aa045a9..acb1a0cdfb27 100644
@@ -201,6 +201,10 @@ static inline void loop_update_dio(struct loop_device *lo)
 	bool dio = lo->use_dio || (lo->lo_backing_file->f_flags & O_DIRECT);
 	bool use_dio = dio && lo_can_use_dio(lo);
 
+	lockdep_assert_held(&lo->lo_mutex);
+	WARN_ON_ONCE(lo->lo_state == Lo_bound &&
+		     lo->lo_queue->mq_freeze_depth == 0);
+
 	if (lo->use_dio == use_dio)
 		return;
 
@@ -213,15 +217,11 @@ static inline void loop_update_dio(struct loop_device *lo)
 	 * LO_FLAGS_READ_ONLY, both are set from kernel, and losetup
 	 * will get updated by ioctl(LOOP_GET_STATUS)
 	 */
-	if (lo->lo_state == Lo_bound)
-		blk_mq_freeze_queue(lo->lo_queue);
 	lo->use_dio = use_dio;
 	if (use_dio)
 		lo->lo_flags |= LO_FLAGS_DIRECT_IO;
 	else
 		lo->lo_flags &= ~LO_FLAGS_DIRECT_IO;
-	if (lo->lo_state == Lo_bound)
-		blk_mq_unfreeze_queue(lo->lo_queue);
 }
 
 /**
Signature · 1 line
2.45.2
#8 lore raw

PATCH 8/8 loop: remove the use_dio field in struct loop_device

In reply to Christoph Hellwig

This field duplicate the LO_FLAGS_DIRECT_IO flag in lo_flags. Remove it to have a single source of truth about using direct I/O.

---
 drivers/block/loop.c | 39 +++++++++++++++++----------------------
 1 file changed, 17 insertions(+), 22 deletions(-)
drivers/block/loop.c +17 -22
index acb1a0cdfb27..1ec7417c7f00 100644
@@ -68,7 +68,6 @@ struct loop_device {
 	struct list_head        idle_worker_list;
 	struct rb_root          worker_tree;
 	struct timer_list       timer;
-	bool			use_dio;
 	bool			sysfs_inited;
 
 	struct request_queue	*lo_queue;
@@ -196,32 +195,30 @@ static bool lo_can_use_dio(struct loop_device *lo)
 	return true;
 }
 
+/*
+ * Direct I/O can be enabled either by using an O_DIRECT file descriptor, or by
+ * passing in the LO_FLAGS_DIRECT_IO flag from userspace.  It will be silently
+ * disabled when the device block size is too small or the offset is unaligned.
+ *
+ * loop_get_status will always report the effective LO_FLAGS_DIRECT_IO flag and
+ * not the originally passed in one.
+ */
 static inline void loop_update_dio(struct loop_device *lo)
 {
-	bool dio = lo->use_dio || (lo->lo_backing_file->f_flags & O_DIRECT);
-	bool use_dio = dio && lo_can_use_dio(lo);
+	bool dio_in_use = lo->lo_flags & LO_FLAGS_DIRECT_IO;
 
 	lockdep_assert_held(&lo->lo_mutex);
 	WARN_ON_ONCE(lo->lo_state == Lo_bound &&
 		     lo->lo_queue->mq_freeze_depth == 0);
 
-	if (lo->use_dio == use_dio)
-		return;
-
-	/* flush dirty pages before starting to use direct I/O */
-	if (use_dio)
-		vfs_fsync(lo->lo_backing_file, 0);
-
-	/*
-	 * The flag of LO_FLAGS_DIRECT_IO is handled similarly with
-	 * LO_FLAGS_READ_ONLY, both are set from kernel, and losetup
-	 * will get updated by ioctl(LOOP_GET_STATUS)
-	 */
-	lo->use_dio = use_dio;
-	if (use_dio)
+	if (lo->lo_backing_file->f_flags & O_DIRECT)
 		lo->lo_flags |= LO_FLAGS_DIRECT_IO;
-	else
+	if ((lo->lo_flags & LO_FLAGS_DIRECT_IO) && !lo_can_use_dio(lo))
 		lo->lo_flags &= ~LO_FLAGS_DIRECT_IO;
+
+	/* flush dirty pages before starting to issue direct I/O */
+	if ((lo->lo_flags & LO_FLAGS_DIRECT_IO) && !dio_in_use)
+		vfs_fsync(lo->lo_backing_file, 0);
 }
 
 /**
@@ -1089,7 +1086,6 @@ static int loop_configure(struct loop_device *lo, blk_mode_t mode,
 	disk_force_media_change(lo->lo_disk);
 	set_disk_ro(lo->lo_disk, (lo->lo_flags & LO_FLAGS_READ_ONLY) != 0);
 
-	lo->use_dio = lo->lo_flags & LO_FLAGS_DIRECT_IO;
 	lo->lo_device = bdev;
 	lo->lo_backing_file = file;
 	lo->old_gfp_mask = mapping_gfp_mask(mapping);
@@ -1454,7 +1450,7 @@ static int loop_set_dio(struct loop_device *lo, unsigned long arg)
 
 	if (lo->lo_state != Lo_bound)
 		return -ENXIO;
-	if (use_dio == lo->use_dio)
+	if (use_dio == !!(lo->lo_flags & LO_FLAGS_DIRECT_IO))
 		return 0;
 
 	if (use_dio) {
@@ -1465,7 +1461,6 @@ static int loop_set_dio(struct loop_device *lo, unsigned long arg)
 	}
 
 	blk_mq_freeze_queue(lo->lo_queue);
-	lo->use_dio = use_dio;
 	if (use_dio)
 		lo->lo_flags |= LO_FLAGS_DIRECT_IO;
 	else
@@ -1876,7 +1871,7 @@ static blk_status_t loop_queue_rq(struct blk_mq_hw_ctx *hctx,
 		cmd->use_aio = false;
 		break;
 	default:
-		cmd->use_aio = lo->use_dio;
+		cmd->use_aio = lo->lo_flags & LO_FLAGS_DIRECT_IO;
 		break;
 	}
 
Signature · 1 line
2.45.2
maintainer Applied
#9 lore raw
In reply to Christoph Hellwig
Quoted 9 lines · Christoph Hellwig

this series fixes a few inconsistencies and cleans up the direct I/O flag handling in the loop driver. It sits on top of the recently posted "fix queue freeze and limit locking order v4" series.

Diffstat:

 loop.c |  123 ++++++++++++++++++++++++++++++++---------------------------------
 1 file changed, 61 insertions(+), 62 deletions(-)

[...]

Applied, thanks!

[1/8] loop: move updating lo_flags out of loop_set_status_from_info

      commit: ae074d07a0e5c05769f1a9a2faa260c36d69465e
[2/8] loop: update commands in loop_set_status still referring to transfers
      commit: 4155adb01e7406653f6b01aaca916a59567cfbfa
[3/8] loop: create a lo_can_use_dio helper
      commit: 781fc49a0e5c111b1a210bd1b3499c89bb21cd81
[4/8] loop: only write back pagecache when starting to to use direct I/O
      commit: 09ccf5549d7809671af34774bb30c8f935d6ed2b
[5/8] loop: open code the direct I/O flag update in loop_set_dio
      commit: dc909525daec7c7c5d628683c99d26e281c1a7bb
[6/8] loop: allow loop_set_status to re-enable direct I/O
      commit: 3a693110afd7127400cc9f779c885f01cf16d0f2
[7/8] loop: don't freeze the queue in loop_update_dio
      commit: 0cd719aa63def1d57316e8e903f01f4af0641a46
[8/8] loop: remove the use_dio field in struct loop_device
      commit: afd69d5c4a1049230fa91c9b54fdd8132f755503

Best regards,

Signature · 1 line
Jens Axboe