Skip to content

Commit 2a2ca6a

Browse files
committed
ide: replace the global ide_lock spinlock by per-hwgroup spinlocks (v2)
Now that (almost) all host drivers have been fixed not to abuse ide_lock and core code usage of ide_lock has been sanitized we may safely replace ide_lock by per-hwgroup locks. This patch is partially based on earlier patch from Ravikiran G Thirumalai. While at it: - don't use deprecated HWIF() and HWGROUP() macros - update locking documentation in ide.h v2: Add missing spin_lock_init(&hwgroup->lock). (Noticed by Elias Oltmanns) Cc: Vaibhav V. Nivargi <vaibhav.nivargi@gmail.com> Cc: Alok N. Kataria <alokk@calsoftinc.com> Cc: Shai Fultheim <shai@scalex86.org> Signed-off-by: Ravikiran Thirumalai <kiran@scalex86.org> Cc: Elias Oltmanns <eo@nebensachen.de> Signed-off-by: Bartlomiej Zolnierkiewicz <bzolnier@gmail.com>
1 parent 6ea5222 commit 2a2ca6a

8 files changed

Lines changed: 100 additions & 86 deletions

File tree

‎drivers/ide/ide-io.c‎

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -907,7 +907,7 @@ static inline ide_drive_t *choose_drive (ide_hwgroup_t *hwgroup)
907907

908908
/*
909909
* Issue a new request to a drive from hwgroup
910-
* Caller must have already done spin_lock_irqsave(&ide_lock, ..);
910+
* Caller must have already done spin_lock_irqsave(&hwgroup->lock, ..);
911911
*
912912
* A hwgroup is a serialized group of IDE interfaces. Usually there is
913913
* exactly one hwif (interface) per hwgroup, but buggy controllers (eg. CMD640)
@@ -919,7 +919,7 @@ static inline ide_drive_t *choose_drive (ide_hwgroup_t *hwgroup)
919919
* possibly along with many other devices. This is especially common in
920920
* PCI-based systems with off-board IDE controller cards.
921921
*
922-
* The IDE driver uses the single global ide_lock spinlock to protect
922+
* The IDE driver uses a per-hwgroup spinlock to protect
923923
* access to the request queues, and to protect the hwgroup->busy flag.
924924
*
925925
* The first thread into the driver for a particular hwgroup sets the
@@ -935,7 +935,7 @@ static inline ide_drive_t *choose_drive (ide_hwgroup_t *hwgroup)
935935
* will start the next request from the queue. If no more work remains,
936936
* the driver will clear the hwgroup->busy flag and exit.
937937
*
938-
* The ide_lock (spinlock) is used to protect all access to the
938+
* The per-hwgroup spinlock is used to protect all access to the
939939
* hwgroup->busy flag, but is otherwise not needed for most processing in
940940
* the driver. This makes the driver much more friendlier to shared IRQs
941941
* than previous designs, while remaining 100% (?) SMP safe and capable.
@@ -948,7 +948,7 @@ static void ide_do_request (ide_hwgroup_t *hwgroup, int masked_irq)
948948
ide_startstop_t startstop;
949949
int loops = 0;
950950

951-
/* caller must own ide_lock */
951+
/* caller must own hwgroup->lock */
952952
BUG_ON(!irqs_disabled());
953953

954954
while (!hwgroup->busy) {
@@ -1070,11 +1070,11 @@ static void ide_do_request (ide_hwgroup_t *hwgroup, int masked_irq)
10701070
*/
10711071
if (masked_irq != IDE_NO_IRQ && hwif->irq != masked_irq)
10721072
disable_irq_nosync(hwif->irq);
1073-
spin_unlock(&ide_lock);
1073+
spin_unlock(&hwgroup->lock);
10741074
local_irq_enable_in_hardirq();
10751075
/* allow other IRQs while we start this request */
10761076
startstop = start_request(drive, rq);
1077-
spin_lock_irq(&ide_lock);
1077+
spin_lock_irq(&hwgroup->lock);
10781078
if (masked_irq != IDE_NO_IRQ && hwif->irq != masked_irq)
10791079
enable_irq(hwif->irq);
10801080
if (startstop == ide_stopped)
@@ -1172,7 +1172,7 @@ void ide_timer_expiry (unsigned long data)
11721172
unsigned long flags;
11731173
unsigned long wait = -1;
11741174

1175-
spin_lock_irqsave(&ide_lock, flags);
1175+
spin_lock_irqsave(&hwgroup->lock, flags);
11761176

11771177
if (((handler = hwgroup->handler) == NULL) ||
11781178
(hwgroup->req_gen != hwgroup->req_gen_timer)) {
@@ -1205,7 +1205,7 @@ void ide_timer_expiry (unsigned long data)
12051205
hwgroup->timer.expires = jiffies + wait;
12061206
hwgroup->req_gen_timer = hwgroup->req_gen;
12071207
add_timer(&hwgroup->timer);
1208-
spin_unlock_irqrestore(&ide_lock, flags);
1208+
spin_unlock_irqrestore(&hwgroup->lock, flags);
12091209
return;
12101210
}
12111211
}
@@ -1215,7 +1215,7 @@ void ide_timer_expiry (unsigned long data)
12151215
* the handler() function, which means we need to
12161216
* globally mask the specific IRQ:
12171217
*/
1218-
spin_unlock(&ide_lock);
1218+
spin_unlock(&hwgroup->lock);
12191219
hwif = HWIF(drive);
12201220
/* disable_irq_nosync ?? */
12211221
disable_irq(hwif->irq);
@@ -1239,14 +1239,14 @@ void ide_timer_expiry (unsigned long data)
12391239
hwif->tp_ops->read_status(hwif));
12401240
}
12411241
drive->service_time = jiffies - drive->service_start;
1242-
spin_lock_irq(&ide_lock);
1242+
spin_lock_irq(&hwgroup->lock);
12431243
enable_irq(hwif->irq);
12441244
if (startstop == ide_stopped)
12451245
hwgroup->busy = 0;
12461246
}
12471247
}
12481248
ide_do_request(hwgroup, IDE_NO_IRQ);
1249-
spin_unlock_irqrestore(&ide_lock, flags);
1249+
spin_unlock_irqrestore(&hwgroup->lock, flags);
12501250
}
12511251

12521252
/**
@@ -1339,14 +1339,13 @@ irqreturn_t ide_intr (int irq, void *dev_id)
13391339
{
13401340
unsigned long flags;
13411341
ide_hwgroup_t *hwgroup = (ide_hwgroup_t *)dev_id;
1342-
ide_hwif_t *hwif;
1342+
ide_hwif_t *hwif = hwgroup->hwif;
13431343
ide_drive_t *drive;
13441344
ide_handler_t *handler;
13451345
ide_startstop_t startstop;
13461346
irqreturn_t irq_ret = IRQ_NONE;
13471347

1348-
spin_lock_irqsave(&ide_lock, flags);
1349-
hwif = hwgroup->hwif;
1348+
spin_lock_irqsave(&hwgroup->lock, flags);
13501349

13511350
if (!ide_ack_intr(hwif))
13521351
goto out;
@@ -1416,7 +1415,7 @@ irqreturn_t ide_intr (int irq, void *dev_id)
14161415
hwgroup->handler = NULL;
14171416
hwgroup->req_gen++;
14181417
del_timer(&hwgroup->timer);
1419-
spin_unlock(&ide_lock);
1418+
spin_unlock(&hwgroup->lock);
14201419

14211420
if (hwif->port_ops && hwif->port_ops->clear_irq)
14221421
hwif->port_ops->clear_irq(drive);
@@ -1427,7 +1426,7 @@ irqreturn_t ide_intr (int irq, void *dev_id)
14271426
/* service this interrupt, may set handler for next interrupt */
14281427
startstop = handler(drive);
14291428

1430-
spin_lock_irq(&ide_lock);
1429+
spin_lock_irq(&hwgroup->lock);
14311430
/*
14321431
* Note that handler() may have set things up for another
14331432
* interrupt to occur soon, but it cannot happen until
@@ -1448,7 +1447,7 @@ irqreturn_t ide_intr (int irq, void *dev_id)
14481447
out_handled:
14491448
irq_ret = IRQ_HANDLED;
14501449
out:
1451-
spin_unlock_irqrestore(&ide_lock, flags);
1450+
spin_unlock_irqrestore(&hwgroup->lock, flags);
14521451
return irq_ret;
14531452
}
14541453

‎drivers/ide/ide-iops.c‎

Lines changed: 23 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -835,10 +835,12 @@ static void __ide_set_handler (ide_drive_t *drive, ide_handler_t *handler,
835835
void ide_set_handler (ide_drive_t *drive, ide_handler_t *handler,
836836
unsigned int timeout, ide_expiry_t *expiry)
837837
{
838+
ide_hwgroup_t *hwgroup = drive->hwif->hwgroup;
838839
unsigned long flags;
839-
spin_lock_irqsave(&ide_lock, flags);
840+
841+
spin_lock_irqsave(&hwgroup->lock, flags);
840842
__ide_set_handler(drive, handler, timeout, expiry);
841-
spin_unlock_irqrestore(&ide_lock, flags);
843+
spin_unlock_irqrestore(&hwgroup->lock, flags);
842844
}
843845

844846
EXPORT_SYMBOL(ide_set_handler);
@@ -860,10 +862,11 @@ EXPORT_SYMBOL(ide_set_handler);
860862
void ide_execute_command(ide_drive_t *drive, u8 cmd, ide_handler_t *handler,
861863
unsigned timeout, ide_expiry_t *expiry)
862864
{
865+
ide_hwif_t *hwif = drive->hwif;
866+
ide_hwgroup_t *hwgroup = hwif->hwgroup;
863867
unsigned long flags;
864-
ide_hwif_t *hwif = HWIF(drive);
865868

866-
spin_lock_irqsave(&ide_lock, flags);
869+
spin_lock_irqsave(&hwgroup->lock, flags);
867870
__ide_set_handler(drive, handler, timeout, expiry);
868871
hwif->tp_ops->exec_command(hwif, cmd);
869872
/*
@@ -873,19 +876,20 @@ void ide_execute_command(ide_drive_t *drive, u8 cmd, ide_handler_t *handler,
873876
* FIXME: we could skip this delay with care on non shared devices
874877
*/
875878
ndelay(400);
876-
spin_unlock_irqrestore(&ide_lock, flags);
879+
spin_unlock_irqrestore(&hwgroup->lock, flags);
877880
}
878881
EXPORT_SYMBOL(ide_execute_command);
879882

880883
void ide_execute_pkt_cmd(ide_drive_t *drive)
881884
{
882885
ide_hwif_t *hwif = drive->hwif;
886+
ide_hwgroup_t *hwgroup = hwif->hwgroup;
883887
unsigned long flags;
884888

885-
spin_lock_irqsave(&ide_lock, flags);
889+
spin_lock_irqsave(&hwgroup->lock, flags);
886890
hwif->tp_ops->exec_command(hwif, ATA_CMD_PACKET);
887891
ndelay(400);
888-
spin_unlock_irqrestore(&ide_lock, flags);
892+
spin_unlock_irqrestore(&hwgroup->lock, flags);
889893
}
890894
EXPORT_SYMBOL_GPL(ide_execute_pkt_cmd);
891895

@@ -1076,22 +1080,16 @@ static void pre_reset(ide_drive_t *drive)
10761080
*/
10771081
static ide_startstop_t do_reset1 (ide_drive_t *drive, int do_not_try_atapi)
10781082
{
1079-
unsigned int unit;
1080-
unsigned long flags, timeout;
1081-
ide_hwif_t *hwif;
1082-
ide_hwgroup_t *hwgroup;
1083-
struct ide_io_ports *io_ports;
1084-
const struct ide_tp_ops *tp_ops;
1083+
ide_hwif_t *hwif = drive->hwif;
1084+
ide_hwgroup_t *hwgroup = hwif->hwgroup;
1085+
struct ide_io_ports *io_ports = &hwif->io_ports;
1086+
const struct ide_tp_ops *tp_ops = hwif->tp_ops;
10851087
const struct ide_port_ops *port_ops;
1088+
unsigned long flags, timeout;
1089+
unsigned int unit;
10861090
DEFINE_WAIT(wait);
10871091

1088-
spin_lock_irqsave(&ide_lock, flags);
1089-
hwif = HWIF(drive);
1090-
hwgroup = HWGROUP(drive);
1091-
1092-
io_ports = &hwif->io_ports;
1093-
1094-
tp_ops = hwif->tp_ops;
1092+
spin_lock_irqsave(&hwgroup->lock, flags);
10951093

10961094
/* We must not reset with running handlers */
10971095
BUG_ON(hwgroup->handler != NULL);
@@ -1106,7 +1104,7 @@ static ide_startstop_t do_reset1 (ide_drive_t *drive, int do_not_try_atapi)
11061104
hwgroup->poll_timeout = jiffies + WAIT_WORSTCASE;
11071105
hwgroup->polling = 1;
11081106
__ide_set_handler(drive, &atapi_reset_pollfunc, HZ/20, NULL);
1109-
spin_unlock_irqrestore(&ide_lock, flags);
1107+
spin_unlock_irqrestore(&hwgroup->lock, flags);
11101108
return ide_started;
11111109
}
11121110

@@ -1129,9 +1127,9 @@ static ide_startstop_t do_reset1 (ide_drive_t *drive, int do_not_try_atapi)
11291127
if (time_before_eq(timeout, now))
11301128
break;
11311129

1132-
spin_unlock_irqrestore(&ide_lock, flags);
1130+
spin_unlock_irqrestore(&hwgroup->lock, flags);
11331131
timeout = schedule_timeout_uninterruptible(timeout - now);
1134-
spin_lock_irqsave(&ide_lock, flags);
1132+
spin_lock_irqsave(&hwgroup->lock, flags);
11351133
} while (timeout);
11361134
finish_wait(&ide_park_wq, &wait);
11371135

@@ -1143,7 +1141,7 @@ static ide_startstop_t do_reset1 (ide_drive_t *drive, int do_not_try_atapi)
11431141
pre_reset(&hwif->drives[unit]);
11441142

11451143
if (io_ports->ctl_addr == 0) {
1146-
spin_unlock_irqrestore(&ide_lock, flags);
1144+
spin_unlock_irqrestore(&hwgroup->lock, flags);
11471145
ide_complete_drive_reset(drive, -ENXIO);
11481146
return ide_stopped;
11491147
}
@@ -1179,7 +1177,7 @@ static ide_startstop_t do_reset1 (ide_drive_t *drive, int do_not_try_atapi)
11791177
if (port_ops && port_ops->resetproc)
11801178
port_ops->resetproc(drive);
11811179

1182-
spin_unlock_irqrestore(&ide_lock, flags);
1180+
spin_unlock_irqrestore(&hwgroup->lock, flags);
11831181
return ide_started;
11841182
}
11851183

‎drivers/ide/ide-park.c‎

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,16 @@ DECLARE_WAIT_QUEUE_HEAD(ide_park_wq);
77

88
static void issue_park_cmd(ide_drive_t *drive, unsigned long timeout)
99
{
10+
ide_hwgroup_t *hwgroup = drive->hwif->hwgroup;
1011
struct request_queue *q = drive->queue;
1112
struct request *rq;
1213
int rc;
1314

1415
timeout += jiffies;
15-
spin_lock_irq(&ide_lock);
16+
spin_lock_irq(&hwgroup->lock);
1617
if (drive->dev_flags & IDE_DFLAG_PARKED) {
17-
ide_hwgroup_t *hwgroup = drive->hwif->hwgroup;
18-
int reset_timer;
18+
int reset_timer = time_before(timeout, drive->sleep);
1919

20-
reset_timer = time_before(timeout, drive->sleep);
2120
drive->sleep = timeout;
2221
wake_up_all(&ide_park_wq);
2322
if (reset_timer && hwgroup->sleeping &&
@@ -26,10 +25,10 @@ static void issue_park_cmd(ide_drive_t *drive, unsigned long timeout)
2625
hwgroup->busy = 0;
2726
blk_start_queueing(q);
2827
}
29-
spin_unlock_irq(&ide_lock);
28+
spin_unlock_irq(&hwgroup->lock);
3029
return;
3130
}
32-
spin_unlock_irq(&ide_lock);
31+
spin_unlock_irq(&hwgroup->lock);
3332

3433
rq = blk_get_request(q, READ, __GFP_WAIT);
3534
rq->cmd[0] = REQ_PARK_HEADS;
@@ -62,20 +61,21 @@ ssize_t ide_park_show(struct device *dev, struct device_attribute *attr,
6261
char *buf)
6362
{
6463
ide_drive_t *drive = to_ide_device(dev);
64+
ide_hwgroup_t *hwgroup = drive->hwif->hwgroup;
6565
unsigned long now;
6666
unsigned int msecs;
6767

6868
if (drive->dev_flags & IDE_DFLAG_NO_UNLOAD)
6969
return -EOPNOTSUPP;
7070

71-
spin_lock_irq(&ide_lock);
71+
spin_lock_irq(&hwgroup->lock);
7272
now = jiffies;
7373
if (drive->dev_flags & IDE_DFLAG_PARKED &&
7474
time_after(drive->sleep, now))
7575
msecs = jiffies_to_msecs(drive->sleep - now);
7676
else
7777
msecs = 0;
78-
spin_unlock_irq(&ide_lock);
78+
spin_unlock_irq(&hwgroup->lock);
7979

8080
return snprintf(buf, 20, "%u\n", msecs);
8181
}

0 commit comments

Comments
 (0)