Re: [PATCH v2] btrfs: don't take dev_replace rwsem on task already

From: Johannes Thumshirn
Date: Tue Sep 10 2024 - 03:56:01 EST


This is to fold in if we decide to go by pid instead of a task_struct.

---
fs/btrfs/dev-replace.c | 4 ++--
fs/btrfs/fs.h | 3 ++-
fs/btrfs/volumes.c | 8 +++++---
3 files changed, 9 insertions(+), 6 deletions(-)

diff --git a/fs/btrfs/dev-replace.c b/fs/btrfs/dev-replace.c
index 604399e59a3d..a1efb8de8170 100644
--- a/fs/btrfs/dev-replace.c
+++ b/fs/btrfs/dev-replace.c
@@ -641,7 +641,7 @@ static int btrfs_dev_replace_start(struct btrfs_fs_info *fs_info,
return ret;

down_write(&dev_replace->rwsem);
- dev_replace->replace_task = current;
+ dev_replace->replace_task = current->pid;
switch (dev_replace->replace_state) {
case BTRFS_IOCTL_DEV_REPLACE_STATE_NEVER_STARTED:
case BTRFS_IOCTL_DEV_REPLACE_STATE_FINISHED:
@@ -995,7 +995,7 @@ static int btrfs_dev_replace_finishing(struct btrfs_fs_info *fs_info,
list_add(&tgt_device->dev_alloc_list, &fs_devices->alloc_list);
fs_devices->rw_devices++;

- dev_replace->replace_task = NULL;
+ dev_replace->replace_task = 0;
up_write(&dev_replace->rwsem);
btrfs_rm_dev_replace_blocked(fs_info);

diff --git a/fs/btrfs/fs.h b/fs/btrfs/fs.h
index cbfb225858a5..a30978f29a59 100644
--- a/fs/btrfs/fs.h
+++ b/fs/btrfs/fs.h
@@ -3,6 +3,7 @@
#ifndef BTRFS_FS_H
#define BTRFS_FS_H

+#include "linux/types.h"
#include <linux/blkdev.h>
#include <linux/sizes.h>
#include <linux/time64.h>
@@ -318,7 +319,7 @@ struct btrfs_dev_replace {
struct percpu_counter bio_counter;
wait_queue_head_t replace_wait;

- struct task_struct *replace_task;
+ pid_t replace_task;
};

/*
diff --git a/fs/btrfs/volumes.c b/fs/btrfs/volumes.c
index 995b0647f538..c3f53a59cbf3 100644
--- a/fs/btrfs/volumes.c
+++ b/fs/btrfs/volumes.c
@@ -6480,7 +6480,7 @@ int btrfs_map_block(struct btrfs_fs_info *fs_info, enum btrfs_map_op op,
max_len = btrfs_max_io_len(map, map_offset, &io_geom);
*length = min_t(u64, map->chunk_len - map_offset, max_len);

- if (dev_replace->replace_task != current)
+ if (dev_replace->replace_task != current->pid)
down_read(&dev_replace->rwsem);

dev_replace_is_ongoing = btrfs_dev_replace_is_ongoing(dev_replace);
@@ -6488,7 +6488,8 @@ int btrfs_map_block(struct btrfs_fs_info *fs_info, enum btrfs_map_op op,
* Hold the semaphore for read during the whole operation, write is
* requested at commit time but must wait.
*/
- if (!dev_replace_is_ongoing && dev_replace->replace_task != current)
+ if (!dev_replace_is_ongoing &&
+ dev_replace->replace_task != current->pid)
up_read(&dev_replace->rwsem);

switch (map->type & BTRFS_BLOCK_GROUP_PROFILE_MASK) {
@@ -6628,7 +6629,8 @@ int btrfs_map_block(struct btrfs_fs_info *fs_info, enum btrfs_map_op op,
bioc->mirror_num = io_geom.mirror_num;

out:
- if (dev_replace_is_ongoing && dev_replace->replace_task != current) {
+ if (dev_replace_is_ongoing &&
+ dev_replace->replace_task != current->pid) {
lockdep_assert_held(&dev_replace->rwsem);
/* Unlock and let waiting writers proceed */
up_read(&dev_replace->rwsem);
--
2.43.0