Skip to content

Commit 070ad7e

Browse files
author
Jiri Kosina
committed
floppy: convert to delayed work and single-thread wq
There are several races in floppy driver between bottom half (scheduled_work) and timers (fd_timeout, fd_timer). Due to slowness of the actual floppy devices, those races are never (at least to my knowledge) triggered on a bare floppy metal. However on virtualized (emulated) floppy drives, which are of course magnitudes faster than the real ones, these races trigger reliably. They usually exhibit themselves as NULL pointer dereferences during DMA setup, such as BUG: unable to handle kernel NULL pointer dereference at 0000000a [ ... snip ... ] EIP: 0060:[<c02053d5>] EFLAGS: 00010293 CPU: 0 EAX: ffffe000 EBX: 0000000a ECX: 00000000 EDX: 0000000a ESI: c05d2718 EDI: 00000000 EBP: 00000000 ESP: f540fe44 DS: 007b ES: 007b FS: 00d8 GS: 0000 SS: 0068 Process swapper (pid: 0, ti=f540e000 task=c082d5a0 task.ti=c0826000) Stack: ffffe000 00001ffc 00000000 00000000 00000000 c05d2718 c0708b40 f540fe80 c020470f c05d2718 c0708b40 00000000 f540fe80 0000000a f540fee4 00000000 c0708b40 f540fee4 00000000 00000000 c020526b 00000000 c05d2718 c0708b40 Call Trace: [<c020470f>] dump_trace+0xaf/0x110 [<c020526b>] show_trace_log_lvl+0x4b/0x60 [<c0205298>] show_trace+0x18/0x20 [<c05c5811>] dump_stack+0x6d/0x72 [<c0248527>] warn_slowpath_common+0x77/0xb0 [<c02485f3>] warn_slowpath_fmt+0x33/0x40 [<f7ec593c>] setup_DMA+0x14c/0x210 [floppy] [<f7ecaa95>] setup_rw_floppy+0x105/0x190 [floppy] [<c0256d08>] run_timer_softirq+0x168/0x2a0 [<c024e762>] __do_softirq+0xc2/0x1c0 [<c02042ed>] do_softirq+0x7d/0xb0 [<f54d8a00>] 0xf54d89ff but other instances can be easily seen as well. This can be observed at least under VMWare, VirtualBox and KVM. This patch converts all the timers and bottom halfs to be processed in a single workqueue. This aproach has been already discussed back in 2010 if I remember correctly, and Acked by Linus [1], but it then never made it to the tree. This all is based on original idea and code of Stephen Hemminger. I have ported original Stepen's code to the current state of the floppy driver, and performed quite some testing (on real hardware), which didn't reveal any issues (this includes not only writing and reading data, but also formatting (unfortunately I didn't find any Double-Density disks any more)). Ability to handle errors properly (supplying known bad floppies) has also been verified. [1] http://kerneltrap.org/mailarchive/linux-kernel/2010/6/11/4582092 Based-on-patch-by: Stephen Hemminger <shemminger@vyatta.com> Acked-by: Linus Torvalds <torvalds@linux-foundation.org> Signed-off-by: Jiri Kosina <jkosina@suse.cz>
1 parent 0b7877d commit 070ad7e

1 file changed

Lines changed: 73 additions & 70 deletions

File tree

‎drivers/block/floppy.c‎

Lines changed: 73 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -551,7 +551,7 @@ static void floppy_ready(void);
551551
static void floppy_start(void);
552552
static void process_fd_request(void);
553553
static void recalibrate_floppy(void);
554-
static void floppy_shutdown(unsigned long);
554+
static void floppy_shutdown(struct work_struct *);
555555

556556
static int floppy_request_regions(int);
557557
static void floppy_release_regions(int);
@@ -588,6 +588,8 @@ static int buffer_max = -1;
588588
static struct floppy_fdc_state fdc_state[N_FDC];
589589
static int fdc; /* current fdc */
590590

591+
static struct workqueue_struct *floppy_wq;
592+
591593
static struct floppy_struct *_floppy = floppy_type;
592594
static unsigned char current_drive;
593595
static long current_count_sectors;
@@ -629,16 +631,15 @@ static inline void set_debugt(void) { }
629631
static inline void debugt(const char *func, const char *msg) { }
630632
#endif /* DEBUGT */
631633

632-
typedef void (*timeout_fn)(unsigned long);
633-
static DEFINE_TIMER(fd_timeout, floppy_shutdown, 0, 0);
634634

635+
static DECLARE_DELAYED_WORK(fd_timeout, floppy_shutdown);
635636
static const char *timeout_message;
636637

637638
static void is_alive(const char *func, const char *message)
638639
{
639640
/* this routine checks whether the floppy driver is "alive" */
640641
if (test_bit(0, &fdc_busy) && command_status < 2 &&
641-
!timer_pending(&fd_timeout)) {
642+
!delayed_work_pending(&fd_timeout)) {
642643
DPRINT("%s: timeout handler died. %s\n", func, message);
643644
}
644645
}
@@ -666,15 +667,18 @@ static int output_log_pos;
666667

667668
static void __reschedule_timeout(int drive, const char *message)
668669
{
670+
unsigned long delay;
671+
669672
if (drive == current_reqD)
670673
drive = current_drive;
671-
del_timer(&fd_timeout);
674+
672675
if (drive < 0 || drive >= N_DRIVE) {
673-
fd_timeout.expires = jiffies + 20UL * HZ;
676+
delay = 20UL * HZ;
674677
drive = 0;
675678
} else
676-
fd_timeout.expires = jiffies + UDP->timeout;
677-
add_timer(&fd_timeout);
679+
delay = UDP->timeout;
680+
681+
queue_delayed_work(floppy_wq, &fd_timeout, delay);
678682
if (UDP->flags & FD_DEBUG)
679683
DPRINT("reschedule timeout %s\n", message);
680684
timeout_message = message;
@@ -872,31 +876,23 @@ static int lock_fdc(int drive, bool interruptible)
872876

873877
command_status = FD_COMMAND_NONE;
874878

875-
__reschedule_timeout(drive, "lock fdc");
879+
reschedule_timeout(drive, "lock fdc");
876880
set_fdc(drive);
877881
return 0;
878882
}
879883

880884
/* unlocks the driver */
881885
static void unlock_fdc(void)
882886
{
883-
unsigned long flags;
884-
885-
raw_cmd = NULL;
886887
if (!test_bit(0, &fdc_busy))
887888
DPRINT("FDC access conflict!\n");
888889

889-
if (do_floppy)
890-
DPRINT("device interrupt still active at FDC release: %pf!\n",
891-
do_floppy);
890+
raw_cmd = NULL;
892891
command_status = FD_COMMAND_NONE;
893-
spin_lock_irqsave(&floppy_lock, flags);
894-
del_timer(&fd_timeout);
892+
__cancel_delayed_work(&fd_timeout);
893+
do_floppy = NULL;
895894
cont = NULL;
896895
clear_bit(0, &fdc_busy);
897-
if (current_req || set_next_request())
898-
do_fd_request(current_req->q);
899-
spin_unlock_irqrestore(&floppy_lock, flags);
900896
wake_up(&fdc_wait);
901897
}
902898

@@ -968,26 +964,24 @@ static DECLARE_WORK(floppy_work, NULL);
968964

969965
static void schedule_bh(void (*handler)(void))
970966
{
967+
WARN_ON(work_pending(&floppy_work));
968+
971969
PREPARE_WORK(&floppy_work, (work_func_t)handler);
972-
schedule_work(&floppy_work);
970+
queue_work(floppy_wq, &floppy_work);
973971
}
974972

975-
static DEFINE_TIMER(fd_timer, NULL, 0, 0);
973+
static DECLARE_DELAYED_WORK(fd_timer, NULL);
976974

977975
static void cancel_activity(void)
978976
{
979-
unsigned long flags;
980-
981-
spin_lock_irqsave(&floppy_lock, flags);
982977
do_floppy = NULL;
983-
PREPARE_WORK(&floppy_work, (work_func_t)empty);
984-
del_timer(&fd_timer);
985-
spin_unlock_irqrestore(&floppy_lock, flags);
978+
cancel_delayed_work_sync(&fd_timer);
979+
cancel_work_sync(&floppy_work);
986980
}
987981

988982
/* this function makes sure that the disk stays in the drive during the
989983
* transfer */
990-
static void fd_watchdog(void)
984+
static void fd_watchdog(struct work_struct *arg)
991985
{
992986
debug_dcl(DP->flags, "calling disk change from watchdog\n");
993987

@@ -997,21 +991,20 @@ static void fd_watchdog(void)
997991
cont->done(0);
998992
reset_fdc();
999993
} else {
1000-
del_timer(&fd_timer);
1001-
fd_timer.function = (timeout_fn)fd_watchdog;
1002-
fd_timer.expires = jiffies + HZ / 10;
1003-
add_timer(&fd_timer);
994+
cancel_delayed_work(&fd_timer);
995+
PREPARE_DELAYED_WORK(&fd_timer, fd_watchdog);
996+
queue_delayed_work(floppy_wq, &fd_timer, HZ / 10);
1004997
}
1005998
}
1006999

10071000
static void main_command_interrupt(void)
10081001
{
1009-
del_timer(&fd_timer);
1002+
cancel_delayed_work(&fd_timer);
10101003
cont->interrupt();
10111004
}
10121005

10131006
/* waits for a delay (spinup or select) to pass */
1014-
static int fd_wait_for_completion(unsigned long delay, timeout_fn function)
1007+
static int fd_wait_for_completion(unsigned long expires, work_func_t function)
10151008
{
10161009
if (FDCS->reset) {
10171010
reset_fdc(); /* do the reset during sleep to win time
@@ -1020,11 +1013,10 @@ static int fd_wait_for_completion(unsigned long delay, timeout_fn function)
10201013
return 1;
10211014
}
10221015

1023-
if (time_before(jiffies, delay)) {
1024-
del_timer(&fd_timer);
1025-
fd_timer.function = function;
1026-
fd_timer.expires = delay;
1027-
add_timer(&fd_timer);
1016+
if (time_before(jiffies, expires)) {
1017+
cancel_delayed_work(&fd_timer);
1018+
PREPARE_DELAYED_WORK(&fd_timer, function);
1019+
queue_delayed_work(floppy_wq, &fd_timer, expires - jiffies);
10281020
return 1;
10291021
}
10301022
return 0;
@@ -1342,7 +1334,7 @@ static int fdc_dtr(void)
13421334
*/
13431335
FDCS->dtr = raw_cmd->rate & 3;
13441336
return fd_wait_for_completion(jiffies + 2UL * HZ / 100,
1345-
(timeout_fn)floppy_ready);
1337+
(work_func_t)floppy_ready);
13461338
} /* fdc_dtr */
13471339

13481340
static void tell_sector(void)
@@ -1447,7 +1439,7 @@ static void setup_rw_floppy(void)
14471439
int flags;
14481440
int dflags;
14491441
unsigned long ready_date;
1450-
timeout_fn function;
1442+
work_func_t function;
14511443

14521444
flags = raw_cmd->flags;
14531445
if (flags & (FD_RAW_READ | FD_RAW_WRITE))
@@ -1461,9 +1453,9 @@ static void setup_rw_floppy(void)
14611453
*/
14621454
if (time_after(ready_date, jiffies + DP->select_delay)) {
14631455
ready_date -= DP->select_delay;
1464-
function = (timeout_fn)floppy_start;
1456+
function = (work_func_t)floppy_start;
14651457
} else
1466-
function = (timeout_fn)setup_rw_floppy;
1458+
function = (work_func_t)setup_rw_floppy;
14671459

14681460
/* wait until the floppy is spinning fast enough */
14691461
if (fd_wait_for_completion(ready_date, function))
@@ -1493,7 +1485,7 @@ static void setup_rw_floppy(void)
14931485
inr = result();
14941486
cont->interrupt();
14951487
} else if (flags & FD_RAW_NEED_DISK)
1496-
fd_watchdog();
1488+
fd_watchdog(NULL);
14971489
}
14981490

14991491
static int blind_seek;
@@ -1802,20 +1794,22 @@ static void show_floppy(void)
18021794
pr_info("do_floppy=%pf\n", do_floppy);
18031795
if (work_pending(&floppy_work))
18041796
pr_info("floppy_work.func=%pf\n", floppy_work.func);
1805-
if (timer_pending(&fd_timer))
1806-
pr_info("fd_timer.function=%pf\n", fd_timer.function);
1807-
if (timer_pending(&fd_timeout)) {
1808-
pr_info("timer_function=%pf\n", fd_timeout.function);
1809-
pr_info("expires=%lu\n", fd_timeout.expires - jiffies);
1810-
pr_info("now=%lu\n", jiffies);
1811-
}
1797+
if (delayed_work_pending(&fd_timer))
1798+
pr_info("delayed work.function=%p expires=%ld\n",
1799+
fd_timer.work.func,
1800+
fd_timer.timer.expires - jiffies);
1801+
if (delayed_work_pending(&fd_timeout))
1802+
pr_info("timer_function=%p expires=%ld\n",
1803+
fd_timeout.work.func,
1804+
fd_timeout.timer.expires - jiffies);
1805+
18121806
pr_info("cont=%p\n", cont);
18131807
pr_info("current_req=%p\n", current_req);
18141808
pr_info("command_status=%d\n", command_status);
18151809
pr_info("\n");
18161810
}
18171811

1818-
static void floppy_shutdown(unsigned long data)
1812+
static void floppy_shutdown(struct work_struct *arg)
18191813
{
18201814
unsigned long flags;
18211815

@@ -1868,7 +1862,7 @@ static int start_motor(void (*function)(void))
18681862

18691863
/* wait_for_completion also schedules reset if needed. */
18701864
return fd_wait_for_completion(DRS->select_date + DP->select_delay,
1871-
(timeout_fn)function);
1865+
(work_func_t)function);
18721866
}
18731867

18741868
static void floppy_ready(void)
@@ -2821,7 +2815,6 @@ static void redo_fd_request(void)
28212815
spin_lock_irq(&floppy_lock);
28222816
pending = set_next_request();
28232817
spin_unlock_irq(&floppy_lock);
2824-
28252818
if (!pending) {
28262819
do_floppy = NULL;
28272820
unlock_fdc();
@@ -2898,13 +2891,15 @@ static void do_fd_request(struct request_queue *q)
28982891
current_req->cmd_flags))
28992892
return;
29002893

2901-
if (test_bit(0, &fdc_busy)) {
2894+
if (test_and_set_bit(0, &fdc_busy)) {
29022895
/* fdc busy, this new request will be treated when the
29032896
current one is done */
29042897
is_alive(__func__, "old request running");
29052898
return;
29062899
}
2907-
lock_fdc(MAXTIMEOUT, false);
2900+
command_status = FD_COMMAND_NONE;
2901+
__reschedule_timeout(MAXTIMEOUT, "fd_request");
2902+
set_fdc(0);
29082903
process_fd_request();
29092904
is_alive(__func__, "");
29102905
}
@@ -4159,10 +4154,16 @@ static int __init floppy_init(void)
41594154
goto out_put_disk;
41604155
}
41614156

4157+
floppy_wq = alloc_ordered_workqueue("floppy", 0);
4158+
if (!floppy_wq) {
4159+
err = -ENOMEM;
4160+
goto out_put_disk;
4161+
}
4162+
41624163
disks[dr]->queue = blk_init_queue(do_fd_request, &floppy_lock);
41634164
if (!disks[dr]->queue) {
41644165
err = -ENOMEM;
4165-
goto out_put_disk;
4166+
goto out_destroy_workq;
41664167
}
41674168

41684169
blk_queue_max_hw_sectors(disks[dr]->queue, 64);
@@ -4213,7 +4214,7 @@ static int __init floppy_init(void)
42134214
use_virtual_dma = can_use_virtual_dma & 1;
42144215
fdc_state[0].address = FDC1;
42154216
if (fdc_state[0].address == -1) {
4216-
del_timer_sync(&fd_timeout);
4217+
cancel_delayed_work(&fd_timeout);
42174218
err = -ENODEV;
42184219
goto out_unreg_region;
42194220
}
@@ -4224,7 +4225,7 @@ static int __init floppy_init(void)
42244225
fdc = 0; /* reset fdc in case of unexpected interrupt */
42254226
err = floppy_grab_irq_and_dma();
42264227
if (err) {
4227-
del_timer_sync(&fd_timeout);
4228+
cancel_delayed_work(&fd_timeout);
42284229
err = -EBUSY;
42294230
goto out_unreg_region;
42304231
}
@@ -4281,13 +4282,13 @@ static int __init floppy_init(void)
42814282
user_reset_fdc(-1, FD_RESET_ALWAYS, false);
42824283
}
42834284
fdc = 0;
4284-
del_timer_sync(&fd_timeout);
4285+
cancel_delayed_work(&fd_timeout);
42854286
current_drive = 0;
42864287
initialized = true;
42874288
if (have_no_fdc) {
42884289
DPRINT("no floppy controllers found\n");
42894290
err = have_no_fdc;
4290-
goto out_flush_work;
4291+
goto out_release_dma;
42914292
}
42924293

42934294
for (drive = 0; drive < N_DRIVE; drive++) {
@@ -4302,7 +4303,7 @@ static int __init floppy_init(void)
43024303

43034304
err = platform_device_register(&floppy_device[drive]);
43044305
if (err)
4305-
goto out_flush_work;
4306+
goto out_release_dma;
43064307

43074308
err = device_create_file(&floppy_device[drive].dev,
43084309
&dev_attr_cmos);
@@ -4320,13 +4321,14 @@ static int __init floppy_init(void)
43204321

43214322
out_unreg_platform_dev:
43224323
platform_device_unregister(&floppy_device[drive]);
4323-
out_flush_work:
4324-
flush_work_sync(&floppy_work);
4324+
out_release_dma:
43254325
if (atomic_read(&usage_count))
43264326
floppy_release_irq_and_dma();
43274327
out_unreg_region:
43284328
blk_unregister_region(MKDEV(FLOPPY_MAJOR, 0), 256);
43294329
platform_driver_unregister(&floppy_driver);
4330+
out_destroy_workq:
4331+
destroy_workqueue(floppy_wq);
43304332
out_unreg_blkdev:
43314333
unregister_blkdev(FLOPPY_MAJOR, "fd");
43324334
out_put_disk:
@@ -4397,7 +4399,7 @@ static int floppy_grab_irq_and_dma(void)
43974399
* We might have scheduled a free_irq(), wait it to
43984400
* drain first:
43994401
*/
4400-
flush_work_sync(&floppy_work);
4402+
flush_workqueue(floppy_wq);
44014403

44024404
if (fd_request_irq()) {
44034405
DPRINT("Unable to grab IRQ%d for the floppy driver\n",
@@ -4488,9 +4490,9 @@ static void floppy_release_irq_and_dma(void)
44884490
pr_info("motor off timer %d still active\n", drive);
44894491
#endif
44904492

4491-
if (timer_pending(&fd_timeout))
4493+
if (delayed_work_pending(&fd_timeout))
44924494
pr_info("floppy timer still active:%s\n", timeout_message);
4493-
if (timer_pending(&fd_timer))
4495+
if (delayed_work_pending(&fd_timer))
44944496
pr_info("auxiliary floppy timer still active\n");
44954497
if (work_pending(&floppy_work))
44964498
pr_info("work still pending\n");
@@ -4560,8 +4562,9 @@ static void __exit floppy_module_exit(void)
45604562
put_disk(disks[drive]);
45614563
}
45624564

4563-
del_timer_sync(&fd_timeout);
4564-
del_timer_sync(&fd_timer);
4565+
cancel_delayed_work_sync(&fd_timeout);
4566+
cancel_delayed_work_sync(&fd_timer);
4567+
destroy_workqueue(floppy_wq);
45654568

45664569
if (atomic_read(&usage_count))
45674570
floppy_release_irq_and_dma();

0 commit comments

Comments
 (0)