Skip to contentSkip to conversation

btrfs: introduce offload_csum_mode to tweak checksum offloading behavior

PATCH In review Naohiro Aota 3 participants 4 messages Feb 5, 2024 – Feb 20, 2024 Btrfs file system

lore
Share & tools
lore
#0 lore raw

PATCH v3 1/1 btrfs: introduce offload_csum_mode to tweak checksum offloading behavior

We disable offloading checksum to workqueues and do it synchronously when the checksum algorithm is fast. However, as reported in the link below, RAID0 with multiple devices may suffer from the sync checksum, because "fast checksum" is still not fast enough to catch up RAID0 writing.

To measure the effectiveness of sync checksum and checksum offloading for developers, it would be better to have a switch for the checksum offloading under CONFIG_BTRFS_DEBUG hood.

This commit introduces fs_devices->offload_csum_mode for CONFIG_BTRFS_DEBUG, so that a btrfs developer can change the behavior by writing to /sys/fs/btrfs/<uuid>/offload_csum. The default is "auto" which is the same as the previous behavior. Or, you can set "on" or "off" (or "y" or "n" whatever kstrtobool() accepts) to always/never offload checksum.

More benchmark should be collected with this knob to implement a proper criteria to enable/disable checksum offloading.

---
 fs/btrfs/bio.c     | 14 +++++++++++++-
 fs/btrfs/sysfs.c   | 44 ++++++++++++++++++++++++++++++++++++++++++++
 fs/btrfs/volumes.h | 24 ++++++++++++++++++++++++
 3 files changed, 81 insertions(+), 1 deletion(-)
fs/btrfs/bio.c +13 -1
index 960b81718e29..477f350a8bd0 100644
@@ -608,8 +608,20 @@ static void run_one_async_done(struct btrfs_work *work, bool do_free)
 
 static bool should_async_write(struct btrfs_bio *bbio)
 {
+	bool auto_csum_mode = true;
+
+#ifdef CONFIG_BTRFS_DEBUG
+	struct btrfs_fs_devices *fs_devices = bbio->fs_info->fs_devices;
+	enum btrfs_offload_csum_mode csum_mode = READ_ONCE(fs_devices->offload_csum_mode);
+
+	if (csum_mode == BTRFS_OFFLOAD_CSUM_FORCE_OFF)
+		return false;
+
+	auto_csum_mode = (csum_mode == BTRFS_OFFLOAD_CSUM_AUTO);
+#endif
+
 	/* Submit synchronously if the checksum implementation is fast. */
-	if (test_bit(BTRFS_FS_CSUM_IMPL_FAST, &bbio->fs_info->flags))
+	if (auto_csum_mode && test_bit(BTRFS_FS_CSUM_IMPL_FAST, &bbio->fs_info->flags))
 		return false;
 
 	/*
fs/btrfs/sysfs.c +44 -0

56 lines not on this page · raw message

fs/btrfs/volumes.h +24 -0
index 53f87f398da7..1f6f73ce48f9 100644
@@ -276,6 +276,25 @@ enum btrfs_read_policy {
 	BTRFS_NR_READ_POLICY,
 };
 
+#ifdef CONFIG_BTRFS_DEBUG
+/*
+ * Checksum mode - offload it to workqueues or do it synchronously in
+ * btrfs_submit_chunk().
+ */
+enum btrfs_offload_csum_mode {
+	/*
+	 * Choose offloading checksum or do it synchronously automatically.
+	 * Do it synchronously if the checksum is fast, or offload to workqueues
+	 * otherwise.
+	 */
+	BTRFS_OFFLOAD_CSUM_AUTO,
+	/* Always offload checksum to workqueues. */
+	BTRFS_OFFLOAD_CSUM_FORCE_ON,
+	/* Never offload checksum to workqueues. */
+	BTRFS_OFFLOAD_CSUM_FORCE_OFF,
+};
+#endif
+
 struct btrfs_fs_devices {
 	u8 fsid[BTRFS_FSID_SIZE]; /* FS specific uuid */
 
@@ -380,6 +399,11 @@ struct btrfs_fs_devices {
 
 	/* Policy used to read the mirrored stripes. */
 	enum btrfs_read_policy read_policy;
+
+#ifdef CONFIG_BTRFS_DEBUG
+	/* Checksum mode - offload it or do it synchronously. */
+	enum btrfs_offload_csum_mode offload_csum_mode;
+#endif
 };
 
 #define BTRFS_MAX_DEVS(info) ((BTRFS_MAX_ITEM_SIZE(info)	\
Signature · 1 line
2.43.0
Reviewed-by on v3 1/1
#1 lore raw
Quoted 15 lines · Naohiro Aota

We disable offloading checksum to workqueues and do it synchronously when the checksum algorithm is fast. However, as reported in the link below, RAID0 with multiple devices may suffer from the sync checksum, because "fast checksum" is still not fast enough to catch up RAID0 writing.

To measure the effectiveness of sync checksum and checksum offloading for developers, it would be better to have a switch for the checksum offloading under CONFIG_BTRFS_DEBUG hood.

This commit introduces fs_devices->offload_csum_mode for CONFIG_BTRFS_DEBUG, so that a btrfs developer can change the behavior by writing to /sys/fs/btrfs/<uuid>/offload_csum. The default is "auto" which is the same as the previous behavior. Or, you can set "on" or "off" (or "y" or "n" whatever kstrtobool() accepts) to always/never offload checksum.

Quoted 6 lines

More benchmark should be collected with this knob to implement a proper criteria to enable/disable checksum offloading.

Thanks. Now to find a good heuristic for the auto mode.

#3 lore raw
In reply to Naohiro Aota
Quoted 15 lines · Naohiro Aota

We disable offloading checksum to workqueues and do it synchronously when the checksum algorithm is fast. However, as reported in the link below, RAID0 with multiple devices may suffer from the sync checksum, because "fast checksum" is still not fast enough to catch up RAID0 writing.

To measure the effectiveness of sync checksum and checksum offloading for developers, it would be better to have a switch for the checksum offloading under CONFIG_BTRFS_DEBUG hood.

This commit introduces fs_devices->offload_csum_mode for CONFIG_BTRFS_DEBUG, so that a btrfs developer can change the behavior by writing to /sys/fs/btrfs/<uuid>/offload_csum. The default is "auto" which is the same as the previous behavior. Or, you can set "on" or "off" (or "y" or "n" whatever kstrtobool() accepts) to always/never offload checksum.

Quoted 6 lines

More benchmark should be collected with this knob to implement a proper criteria to enable/disable checksum offloading.

I'm adding this to for-next.