Skip to content

Commit 1ca0897

Browse files
Qu WenruoJosef Bacik
authored andcommitted
btrfs: Add high priority workqueue support for btrfs_workqueue_struct
Add high priority function to btrfs_workqueue. This is implemented by embedding a btrfs_workqueue into a btrfs_workqueue and use some helper functions to differ the normal priority wq and high priority wq. So the high priority wq is completely independent from the normal workqueue. Signed-off-by: Qu Wenruo <quwenruo@cn.fujitsu.com> Tested-by: David Sterba <dsterba@suse.cz> Signed-off-by: Josef Bacik <jbacik@fb.com>
1 parent 08a9ff3 commit 1ca0897

2 files changed

Lines changed: 83 additions & 13 deletions

File tree

‎fs/btrfs/async-thread.c‎

Lines changed: 79 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -730,7 +730,7 @@ void btrfs_queue_worker(struct btrfs_workers *workers, struct btrfs_work *work)
730730
spin_unlock_irqrestore(&worker->lock, flags);
731731
}
732732

733-
struct btrfs_workqueue_struct {
733+
struct __btrfs_workqueue_struct {
734734
struct workqueue_struct *normal_wq;
735735
/* List head pointing to ordered work list */
736736
struct list_head ordered_list;
@@ -739,6 +739,38 @@ struct btrfs_workqueue_struct {
739739
spinlock_t list_lock;
740740
};
741741

742+
struct btrfs_workqueue_struct {
743+
struct __btrfs_workqueue_struct *normal;
744+
struct __btrfs_workqueue_struct *high;
745+
};
746+
747+
static inline struct __btrfs_workqueue_struct
748+
*__btrfs_alloc_workqueue(char *name, int flags, int max_active)
749+
{
750+
struct __btrfs_workqueue_struct *ret = kzalloc(sizeof(*ret), GFP_NOFS);
751+
752+
if (unlikely(!ret))
753+
return NULL;
754+
755+
if (flags & WQ_HIGHPRI)
756+
ret->normal_wq = alloc_workqueue("%s-%s-high", flags,
757+
max_active, "btrfs", name);
758+
else
759+
ret->normal_wq = alloc_workqueue("%s-%s", flags,
760+
max_active, "btrfs", name);
761+
if (unlikely(!ret->normal_wq)) {
762+
kfree(ret);
763+
return NULL;
764+
}
765+
766+
INIT_LIST_HEAD(&ret->ordered_list);
767+
spin_lock_init(&ret->list_lock);
768+
return ret;
769+
}
770+
771+
static inline void
772+
__btrfs_destroy_workqueue(struct __btrfs_workqueue_struct *wq);
773+
742774
struct btrfs_workqueue_struct *btrfs_alloc_workqueue(char *name,
743775
int flags,
744776
int max_active)
@@ -748,19 +780,25 @@ struct btrfs_workqueue_struct *btrfs_alloc_workqueue(char *name,
748780
if (unlikely(!ret))
749781
return NULL;
750782

751-
ret->normal_wq = alloc_workqueue("%s-%s", flags, max_active,
752-
"btrfs", name);
753-
if (unlikely(!ret->normal_wq)) {
783+
ret->normal = __btrfs_alloc_workqueue(name, flags & ~WQ_HIGHPRI,
784+
max_active);
785+
if (unlikely(!ret->normal)) {
754786
kfree(ret);
755787
return NULL;
756788
}
757789

758-
INIT_LIST_HEAD(&ret->ordered_list);
759-
spin_lock_init(&ret->list_lock);
790+
if (flags & WQ_HIGHPRI) {
791+
ret->high = __btrfs_alloc_workqueue(name, flags, max_active);
792+
if (unlikely(!ret->high)) {
793+
__btrfs_destroy_workqueue(ret->normal);
794+
kfree(ret);
795+
return NULL;
796+
}
797+
}
760798
return ret;
761799
}
762800

763-
static void run_ordered_work(struct btrfs_workqueue_struct *wq)
801+
static void run_ordered_work(struct __btrfs_workqueue_struct *wq)
764802
{
765803
struct list_head *list = &wq->ordered_list;
766804
struct btrfs_work_struct *work;
@@ -804,7 +842,7 @@ static void run_ordered_work(struct btrfs_workqueue_struct *wq)
804842
static void normal_work_helper(struct work_struct *arg)
805843
{
806844
struct btrfs_work_struct *work;
807-
struct btrfs_workqueue_struct *wq;
845+
struct __btrfs_workqueue_struct *wq;
808846
int need_order = 0;
809847

810848
work = container_of(arg, struct btrfs_work_struct, normal_work);
@@ -840,8 +878,8 @@ void btrfs_init_work(struct btrfs_work_struct *work,
840878
work->flags = 0;
841879
}
842880

843-
void btrfs_queue_work(struct btrfs_workqueue_struct *wq,
844-
struct btrfs_work_struct *work)
881+
static inline void __btrfs_queue_work(struct __btrfs_workqueue_struct *wq,
882+
struct btrfs_work_struct *work)
845883
{
846884
unsigned long flags;
847885

@@ -854,13 +892,42 @@ void btrfs_queue_work(struct btrfs_workqueue_struct *wq,
854892
queue_work(wq->normal_wq, &work->normal_work);
855893
}
856894

857-
void btrfs_destroy_workqueue(struct btrfs_workqueue_struct *wq)
895+
void btrfs_queue_work(struct btrfs_workqueue_struct *wq,
896+
struct btrfs_work_struct *work)
897+
{
898+
struct __btrfs_workqueue_struct *dest_wq;
899+
900+
if (test_bit(WORK_HIGH_PRIO_BIT, &work->flags) && wq->high)
901+
dest_wq = wq->high;
902+
else
903+
dest_wq = wq->normal;
904+
__btrfs_queue_work(dest_wq, work);
905+
}
906+
907+
static inline void
908+
__btrfs_destroy_workqueue(struct __btrfs_workqueue_struct *wq)
858909
{
859910
destroy_workqueue(wq->normal_wq);
860911
kfree(wq);
861912
}
862913

914+
void btrfs_destroy_workqueue(struct btrfs_workqueue_struct *wq)
915+
{
916+
if (!wq)
917+
return;
918+
if (wq->high)
919+
__btrfs_destroy_workqueue(wq->high);
920+
__btrfs_destroy_workqueue(wq->normal);
921+
}
922+
863923
void btrfs_workqueue_set_max(struct btrfs_workqueue_struct *wq, int max)
864924
{
865-
workqueue_set_max_active(wq->normal_wq, max);
925+
workqueue_set_max_active(wq->normal->normal_wq, max);
926+
if (wq->high)
927+
workqueue_set_max_active(wq->high->normal_wq, max);
928+
}
929+
930+
void btrfs_set_work_high_priority(struct btrfs_work_struct *work)
931+
{
932+
set_bit(WORK_HIGH_PRIO_BIT, &work->flags);
866933
}

‎fs/btrfs/async-thread.h‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,8 @@ void btrfs_requeue_work(struct btrfs_work *work);
121121
void btrfs_set_work_high_prio(struct btrfs_work *work);
122122

123123
struct btrfs_workqueue_struct;
124+
/* Internal use only */
125+
struct __btrfs_workqueue_struct;
124126

125127
struct btrfs_work_struct {
126128
void (*func)(struct btrfs_work_struct *arg);
@@ -130,7 +132,7 @@ struct btrfs_work_struct {
130132
/* Don't touch things below */
131133
struct work_struct normal_work;
132134
struct list_head ordered_list;
133-
struct btrfs_workqueue_struct *wq;
135+
struct __btrfs_workqueue_struct *wq;
134136
unsigned long flags;
135137
};
136138

@@ -145,4 +147,5 @@ void btrfs_queue_work(struct btrfs_workqueue_struct *wq,
145147
struct btrfs_work_struct *work);
146148
void btrfs_destroy_workqueue(struct btrfs_workqueue_struct *wq);
147149
void btrfs_workqueue_set_max(struct btrfs_workqueue_struct *wq, int max);
150+
void btrfs_set_work_high_priority(struct btrfs_work_struct *work);
148151
#endif

0 commit comments

Comments
 (0)