Skip to content

Commit 28dad9a

Browse files
kdavegregkh
authored andcommitted
btrfs: fix crash when tracepoint arguments are freed by wq callbacks
commit ac0c7cf upstream. Enabling btrfs tracepoints leads to instant crash, as reported. The wq callbacks could free the memory and the tracepoints started to dereference the members to get to fs_info. The proposed fix https://marc.info/?l=linux-btrfs&m=148172436722606&w=2 removed the tracepoints but we could preserve them by passing only the required data in a safe way. Fixes: bc07452 ("btrfs: prefix fsid to all trace events") Reported-by: Sebastian Andrzej Siewior <bigeasy@linutronix.de> Reviewed-by: Qu Wenruo <quwenruo@cn.fujitsu.com> Signed-off-by: David Sterba <dsterba@suse.com> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent 4d0f302 commit 28dad9a

2 files changed

Lines changed: 24 additions & 13 deletions

File tree

‎fs/btrfs/async-thread.c‎

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,8 @@ static void run_ordered_work(struct __btrfs_workqueue *wq)
273273
unsigned long flags;
274274

275275
while (1) {
276+
void *wtag;
277+
276278
spin_lock_irqsave(lock, flags);
277279
if (list_empty(list))
278280
break;
@@ -299,18 +301,21 @@ static void run_ordered_work(struct __btrfs_workqueue *wq)
299301
spin_unlock_irqrestore(lock, flags);
300302

301303
/*
302-
* we don't want to call the ordered free functions
303-
* with the lock held though
304+
* We don't want to call the ordered free functions with the
305+
* lock held though. Save the work as tag for the trace event,
306+
* because the callback could free the structure.
304307
*/
308+
wtag = work;
305309
work->ordered_free(work);
306-
trace_btrfs_all_work_done(work);
310+
trace_btrfs_all_work_done(wq->fs_info, wtag);
307311
}
308312
spin_unlock_irqrestore(lock, flags);
309313
}
310314

311315
static void normal_work_helper(struct btrfs_work *work)
312316
{
313317
struct __btrfs_workqueue *wq;
318+
void *wtag;
314319
int need_order = 0;
315320

316321
/*
@@ -324,6 +329,8 @@ static void normal_work_helper(struct btrfs_work *work)
324329
if (work->ordered_func)
325330
need_order = 1;
326331
wq = work->wq;
332+
/* Safe for tracepoints in case work gets freed by the callback */
333+
wtag = work;
327334

328335
trace_btrfs_work_sched(work);
329336
thresh_exec_hook(wq);
@@ -333,7 +340,7 @@ static void normal_work_helper(struct btrfs_work *work)
333340
run_ordered_work(wq);
334341
}
335342
if (!need_order)
336-
trace_btrfs_all_work_done(work);
343+
trace_btrfs_all_work_done(wq->fs_info, wtag);
337344
}
338345

339346
void btrfs_init_work(struct btrfs_work *work, btrfs_work_func_t uniq_func,

‎include/trace/events/btrfs.h‎

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1162,22 +1162,26 @@ DECLARE_EVENT_CLASS(btrfs__work,
11621162
__entry->func, __entry->ordered_func, __entry->ordered_free)
11631163
);
11641164

1165-
/* For situiations that the work is freed */
1165+
/*
1166+
* For situiations when the work is freed, we pass fs_info and a tag that that
1167+
* matches address of the work structure so it can be paired with the
1168+
* scheduling event.
1169+
*/
11661170
DECLARE_EVENT_CLASS(btrfs__work__done,
11671171

1168-
TP_PROTO(struct btrfs_work *work),
1172+
TP_PROTO(struct btrfs_fs_info *fs_info, void *wtag),
11691173

1170-
TP_ARGS(work),
1174+
TP_ARGS(fs_info, wtag),
11711175

11721176
TP_STRUCT__entry_btrfs(
1173-
__field( void *, work )
1177+
__field( void *, wtag )
11741178
),
11751179

1176-
TP_fast_assign_btrfs(btrfs_work_owner(work),
1177-
__entry->work = work;
1180+
TP_fast_assign_btrfs(fs_info,
1181+
__entry->wtag = wtag;
11781182
),
11791183

1180-
TP_printk_btrfs("work->%p", __entry->work)
1184+
TP_printk_btrfs("work->%p", __entry->wtag)
11811185
);
11821186

11831187
DEFINE_EVENT(btrfs__work, btrfs_work_queued,
@@ -1196,9 +1200,9 @@ DEFINE_EVENT(btrfs__work, btrfs_work_sched,
11961200

11971201
DEFINE_EVENT(btrfs__work__done, btrfs_all_work_done,
11981202

1199-
TP_PROTO(struct btrfs_work *work),
1203+
TP_PROTO(struct btrfs_fs_info *fs_info, void *wtag),
12001204

1201-
TP_ARGS(work)
1205+
TP_ARGS(fs_info, wtag)
12021206
);
12031207

12041208
DEFINE_EVENT(btrfs__work, btrfs_ordered_sched,

0 commit comments

Comments
 (0)