Skip to content

Commit 87c8331

Browse files
djbwJames Bottomley
authored andcommitted
[SCSI] libsas: prevent domain rediscovery competing with ata error handling
libata error handling provides for a timeout for link recovery. libsas must not rescan for previously known devices in this interval otherwise it may remove a device that is simply waiting for its link to recover. Let libata-eh make the determination of when the link is stable and prevent libsas (host workqueue) from taking action while this determination is pending. Using a mutex (ha->disco_mutex) to flush and disable revalidation while eh is running requires any discovery action that may block on eh be moved to its own context outside the lock. Probing ATA devices explicitly waits on ata-eh and the cache-flush-io issued during device removal may also pend awaiting eh completion. Essentially any rphy add/remove activity needs to run outside the lock. This adds two new cleanup states for sas_unregister_domain_devices() 'allocated-but-not-probed', and 'flagged-for-destruction'. In the 'allocated-but-not-probed' state dev->rphy points to a rphy that is known to have not been through a sas_rphy_add() event. At domain teardown check if this device is still pending probe and cleanup accordingly. Similarly if a device has already been queued for removal then sas_unregister_domain_devices has nothing to do. Signed-off-by: Dan Williams <dan.j.williams@intel.com> Signed-off-by: James Bottomley <JBottomley@Parallels.com>
1 parent e139942 commit 87c8331

11 files changed

Lines changed: 174 additions & 18 deletions

File tree

‎drivers/scsi/libsas/sas_ata.c‎

Lines changed: 51 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -758,6 +758,35 @@ static int sas_discover_sata_pm(struct domain_device *dev)
758758
return -ENODEV;
759759
}
760760

761+
void sas_probe_sata(struct work_struct *work)
762+
{
763+
struct domain_device *dev, *n;
764+
struct sas_discovery_event *ev =
765+
container_of(work, struct sas_discovery_event, work);
766+
struct asd_sas_port *port = ev->port;
767+
768+
clear_bit(DISCE_PROBE, &port->disc.pending);
769+
770+
list_for_each_entry_safe(dev, n, &port->disco_list, disco_list_node) {
771+
int err;
772+
773+
spin_lock_irq(&port->dev_list_lock);
774+
list_add_tail(&dev->dev_list_node, &port->dev_list);
775+
spin_unlock_irq(&port->dev_list_lock);
776+
777+
err = sas_rphy_add(dev->rphy);
778+
779+
if (err) {
780+
SAS_DPRINTK("%s: for %s device %16llx returned %d\n",
781+
__func__, dev->parent ? "exp-attached" :
782+
"direct-attached",
783+
SAS_ADDR(dev->sas_addr), err);
784+
sas_unregister_dev(port, dev);
785+
} else
786+
list_del_init(&dev->disco_list_node);
787+
}
788+
}
789+
761790
/**
762791
* sas_discover_sata -- discover an STP/SATA domain device
763792
* @dev: pointer to struct domain_device of interest
@@ -794,17 +823,33 @@ int sas_discover_sata(struct domain_device *dev)
794823
break;
795824
}
796825
sas_notify_lldd_dev_gone(dev);
797-
if (!res) {
798-
sas_notify_lldd_dev_found(dev);
799-
res = sas_rphy_add(dev->rphy);
800-
}
826+
827+
if (res)
828+
return res;
829+
830+
res = sas_notify_lldd_dev_found(dev);
831+
if (res)
832+
return res;
833+
834+
sas_discover_event(dev->port, DISCE_PROBE);
801835

802836
return res;
803837
}
804838

805839
void sas_ata_strategy_handler(struct Scsi_Host *shost)
806840
{
807841
struct scsi_device *sdev;
842+
struct sas_ha_struct *sas_ha = SHOST_TO_SAS_HA(shost);
843+
844+
/* it's ok to defer revalidation events during ata eh, these
845+
* disks are in one of three states:
846+
* 1/ present for initial domain discovery, and these
847+
* resets will cause bcn flutters
848+
* 2/ hot removed, we'll discover that after eh fails
849+
* 3/ hot added after initial discovery, lost the race, and need
850+
* to catch the next train.
851+
*/
852+
sas_disable_revalidation(sas_ha);
808853

809854
shost_for_each_device(sdev, shost) {
810855
struct domain_device *ddev = sdev_to_domain_dev(sdev);
@@ -816,6 +861,8 @@ void sas_ata_strategy_handler(struct Scsi_Host *shost)
816861
ata_port_printk(ap, KERN_DEBUG, "sas eh calling libata port error handler");
817862
ata_scsi_port_error_handler(shost, ap);
818863
}
864+
865+
sas_enable_revalidation(sas_ha);
819866
}
820867

821868
int sas_ata_timed_out(struct scsi_cmnd *cmd, struct sas_task *task,

‎drivers/scsi/libsas/sas_discover.c‎

Lines changed: 57 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -148,9 +148,14 @@ static int sas_get_port_device(struct asd_sas_port *port)
148148
port->disc.max_level = 0;
149149

150150
dev->rphy = rphy;
151-
spin_lock_irq(&port->dev_list_lock);
152-
list_add_tail(&dev->dev_list_node, &port->dev_list);
153-
spin_unlock_irq(&port->dev_list_lock);
151+
152+
if (dev_is_sata(dev))
153+
list_add_tail(&dev->disco_list_node, &port->disco_list);
154+
else {
155+
spin_lock_irq(&port->dev_list_lock);
156+
list_add_tail(&dev->dev_list_node, &port->dev_list);
157+
spin_unlock_irq(&port->dev_list_lock);
158+
}
154159

155160
return 0;
156161
}
@@ -255,14 +260,43 @@ static void sas_unregister_common_dev(struct asd_sas_port *port, struct domain_d
255260
sas_put_device(dev);
256261
}
257262

258-
void sas_unregister_dev(struct asd_sas_port *port, struct domain_device *dev)
263+
static void sas_destruct_devices(struct work_struct *work)
259264
{
260-
if (dev->rphy) {
265+
struct domain_device *dev, *n;
266+
struct sas_discovery_event *ev =
267+
container_of(work, struct sas_discovery_event, work);
268+
struct asd_sas_port *port = ev->port;
269+
270+
clear_bit(DISCE_DESTRUCT, &port->disc.pending);
271+
272+
list_for_each_entry_safe(dev, n, &port->destroy_list, disco_list_node) {
273+
list_del_init(&dev->disco_list_node);
274+
261275
sas_remove_children(&dev->rphy->dev);
262276
sas_rphy_delete(dev->rphy);
263277
dev->rphy = NULL;
278+
sas_unregister_common_dev(port, dev);
279+
280+
sas_put_device(dev);
281+
}
282+
}
283+
284+
void sas_unregister_dev(struct asd_sas_port *port, struct domain_device *dev)
285+
{
286+
if (!test_bit(SAS_DEV_DESTROY, &dev->state) &&
287+
!list_empty(&dev->disco_list_node)) {
288+
/* this rphy never saw sas_rphy_add */
289+
list_del_init(&dev->disco_list_node);
290+
sas_rphy_free(dev->rphy);
291+
dev->rphy = NULL;
292+
sas_unregister_common_dev(port, dev);
293+
}
294+
295+
if (dev->rphy && !test_and_set_bit(SAS_DEV_DESTROY, &dev->state)) {
296+
sas_rphy_unlink(dev->rphy);
297+
list_move_tail(&dev->disco_list_node, &port->destroy_list);
298+
sas_discover_event(dev->port, DISCE_DESTRUCT);
264299
}
265-
sas_unregister_common_dev(port, dev);
266300
}
267301

268302
void sas_unregister_domain_devices(struct asd_sas_port *port)
@@ -271,6 +305,8 @@ void sas_unregister_domain_devices(struct asd_sas_port *port)
271305

272306
list_for_each_entry_safe_reverse(dev, n, &port->dev_list, dev_list_node)
273307
sas_unregister_dev(port, dev);
308+
list_for_each_entry_safe(dev, n, &port->disco_list, disco_list_node)
309+
sas_unregister_dev(port, dev);
274310

275311
port->port->rphy = NULL;
276312

@@ -335,6 +371,7 @@ static void sas_discover_domain(struct work_struct *work)
335371
sas_rphy_free(dev->rphy);
336372
dev->rphy = NULL;
337373

374+
list_del_init(&dev->disco_list_node);
338375
spin_lock_irq(&port->dev_list_lock);
339376
list_del_init(&dev->dev_list_node);
340377
spin_unlock_irq(&port->dev_list_lock);
@@ -353,16 +390,28 @@ static void sas_revalidate_domain(struct work_struct *work)
353390
struct sas_discovery_event *ev =
354391
container_of(work, struct sas_discovery_event, work);
355392
struct asd_sas_port *port = ev->port;
393+
struct sas_ha_struct *ha = port->ha;
394+
395+
/* prevent revalidation from finding sata links in recovery */
396+
mutex_lock(&ha->disco_mutex);
397+
if (test_bit(SAS_HA_ATA_EH_ACTIVE, &ha->state)) {
398+
SAS_DPRINTK("REVALIDATION DEFERRED on port %d, pid:%d\n",
399+
port->id, task_pid_nr(current));
400+
goto out;
401+
}
356402

357403
clear_bit(DISCE_REVALIDATE_DOMAIN, &port->disc.pending);
358404

359405
SAS_DPRINTK("REVALIDATING DOMAIN on port %d, pid:%d\n", port->id,
360406
task_pid_nr(current));
407+
361408
if (port->port_dev)
362409
res = sas_ex_revalidate_domain(port->port_dev);
363410

364411
SAS_DPRINTK("done REVALIDATING DOMAIN on port %d, pid:%d, res 0x%x\n",
365412
port->id, task_pid_nr(current), res);
413+
out:
414+
mutex_unlock(&ha->disco_mutex);
366415
}
367416

368417
/* ---------- Events ---------- */
@@ -414,6 +463,8 @@ void sas_init_disc(struct sas_discovery *disc, struct asd_sas_port *port)
414463
static const work_func_t sas_event_fns[DISC_NUM_EVENTS] = {
415464
[DISCE_DISCOVER_DOMAIN] = sas_discover_domain,
416465
[DISCE_REVALIDATE_DOMAIN] = sas_revalidate_domain,
466+
[DISCE_PROBE] = sas_probe_sata,
467+
[DISCE_DESTRUCT] = sas_destruct_devices,
417468
};
418469

419470
disc->pending = 0;

‎drivers/scsi/libsas/sas_event.c‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,32 @@ int sas_drain_work(struct sas_ha_struct *ha)
8181
}
8282
EXPORT_SYMBOL_GPL(sas_drain_work);
8383

84+
void sas_disable_revalidation(struct sas_ha_struct *ha)
85+
{
86+
mutex_lock(&ha->disco_mutex);
87+
set_bit(SAS_HA_ATA_EH_ACTIVE, &ha->state);
88+
mutex_unlock(&ha->disco_mutex);
89+
}
90+
91+
void sas_enable_revalidation(struct sas_ha_struct *ha)
92+
{
93+
int i;
94+
95+
mutex_lock(&ha->disco_mutex);
96+
clear_bit(SAS_HA_ATA_EH_ACTIVE, &ha->state);
97+
for (i = 0; i < ha->num_phys; i++) {
98+
struct asd_sas_port *port = ha->sas_port[i];
99+
const int ev = DISCE_REVALIDATE_DOMAIN;
100+
struct sas_discovery *d = &port->disc;
101+
102+
if (!test_and_clear_bit(ev, &d->pending))
103+
continue;
104+
105+
sas_queue_event(ev, &d->pending, &d->disc_work[ev].work, ha);
106+
}
107+
mutex_unlock(&ha->disco_mutex);
108+
}
109+
84110
static void notify_ha_event(struct sas_ha_struct *sas_ha, enum ha_event event)
85111
{
86112
BUG_ON(event >= HA_NUM_EVENTS);

‎drivers/scsi/libsas/sas_expander.c‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -704,9 +704,7 @@ static struct domain_device *sas_ex_discover_end_dev(
704704

705705
child->rphy = rphy;
706706

707-
spin_lock_irq(&parent->port->dev_list_lock);
708-
list_add_tail(&child->dev_list_node, &parent->port->dev_list);
709-
spin_unlock_irq(&parent->port->dev_list_lock);
707+
list_add_tail(&child->disco_list_node, &parent->port->disco_list);
710708

711709
res = sas_discover_sata(child);
712710
if (res) {
@@ -756,6 +754,7 @@ static struct domain_device *sas_ex_discover_end_dev(
756754
sas_rphy_free(child->rphy);
757755
child->rphy = NULL;
758756

757+
list_del(&child->disco_list_node);
759758
spin_lock_irq(&parent->port->dev_list_lock);
760759
list_del(&child->dev_list_node);
761760
spin_unlock_irq(&parent->port->dev_list_lock);

‎drivers/scsi/libsas/sas_init.c‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ int sas_register_ha(struct sas_ha_struct *sas_ha)
104104
{
105105
int error = 0;
106106

107+
mutex_init(&sas_ha->disco_mutex);
107108
spin_lock_init(&sas_ha->phy_port_lock);
108109
sas_hash_addr(sas_ha->hashed_sas_addr, sas_ha->sas_addr);
109110

@@ -168,6 +169,7 @@ int sas_unregister_ha(struct sas_ha_struct *sas_ha)
168169
sas_drain_work(sas_ha);
169170

170171
sas_unregister_ports(sas_ha);
172+
sas_drain_work(sas_ha);
171173

172174
if (sas_ha->lldd_max_execute_num > 1) {
173175
sas_shutdown_queue(sas_ha);

‎drivers/scsi/libsas/sas_internal.h‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@ enum blk_eh_timer_return sas_scsi_timed_out(struct scsi_cmnd *);
5656
int sas_init_queue(struct sas_ha_struct *sas_ha);
5757
int sas_init_events(struct sas_ha_struct *sas_ha);
5858
void sas_shutdown_queue(struct sas_ha_struct *sas_ha);
59+
void sas_disable_revalidation(struct sas_ha_struct *ha);
60+
void sas_enable_revalidation(struct sas_ha_struct *ha);
5961

6062
void sas_deform_port(struct asd_sas_phy *phy, int gone);
6163

@@ -138,6 +140,7 @@ static inline struct domain_device *sas_alloc_device(void)
138140
if (dev) {
139141
INIT_LIST_HEAD(&dev->siblings);
140142
INIT_LIST_HEAD(&dev->dev_list_node);
143+
INIT_LIST_HEAD(&dev->disco_list_node);
141144
kref_init(&dev->kref);
142145
}
143146
return dev;

‎drivers/scsi/libsas/sas_port.c‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,8 @@ static void sas_init_port(struct asd_sas_port *port,
277277
memset(port, 0, sizeof(*port));
278278
port->id = i;
279279
INIT_LIST_HEAD(&port->dev_list);
280+
INIT_LIST_HEAD(&port->disco_list);
281+
INIT_LIST_HEAD(&port->destroy_list);
280282
spin_lock_init(&port->phy_list_lock);
281283
INIT_LIST_HEAD(&port->phy_list);
282284
port->ha = sas_ha;

‎drivers/scsi/scsi_transport_sas.c‎

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1602,6 +1602,20 @@ sas_rphy_delete(struct sas_rphy *rphy)
16021602
}
16031603
EXPORT_SYMBOL(sas_rphy_delete);
16041604

1605+
/**
1606+
* sas_rphy_unlink - unlink SAS remote PHY
1607+
* @rphy: SAS remote phy to unlink from its parent port
1608+
*
1609+
* Removes port reference to an rphy
1610+
*/
1611+
void sas_rphy_unlink(struct sas_rphy *rphy)
1612+
{
1613+
struct sas_port *parent = dev_to_sas_port(rphy->dev.parent);
1614+
1615+
parent->rphy = NULL;
1616+
}
1617+
EXPORT_SYMBOL(sas_rphy_unlink);
1618+
16051619
/**
16061620
* sas_rphy_remove - remove SAS remote PHY
16071621
* @rphy: SAS remote phy to remove
@@ -1612,7 +1626,6 @@ void
16121626
sas_rphy_remove(struct sas_rphy *rphy)
16131627
{
16141628
struct device *dev = &rphy->dev;
1615-
struct sas_port *parent = dev_to_sas_port(dev->parent);
16161629

16171630
switch (rphy->identify.device_type) {
16181631
case SAS_END_DEVICE:
@@ -1626,10 +1639,9 @@ sas_rphy_remove(struct sas_rphy *rphy)
16261639
break;
16271640
}
16281641

1642+
sas_rphy_unlink(rphy);
16291643
transport_remove_device(dev);
16301644
device_del(dev);
1631-
1632-
parent->rphy = NULL;
16331645
}
16341646
EXPORT_SYMBOL(sas_rphy_remove);
16351647

‎include/scsi/libsas.h‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,9 @@ enum discover_event {
8686
DISCE_DISCOVER_DOMAIN = 0U,
8787
DISCE_REVALIDATE_DOMAIN = 1,
8888
DISCE_PORT_GONE = 2,
89-
DISC_NUM_EVENTS = 3,
89+
DISCE_PROBE = 3,
90+
DISCE_DESTRUCT = 4,
91+
DISC_NUM_EVENTS = 5,
9092
};
9193

9294
/* ---------- Expander Devices ---------- */
@@ -175,6 +177,7 @@ struct sata_device {
175177

176178
enum {
177179
SAS_DEV_GONE,
180+
SAS_DEV_DESTROY,
178181
};
179182

180183
struct domain_device {
@@ -191,6 +194,7 @@ struct domain_device {
191194
struct asd_sas_port *port; /* shortcut to root of the tree */
192195

193196
struct list_head dev_list_node;
197+
struct list_head disco_list_node; /* awaiting probe or destruct */
194198

195199
enum sas_protocol iproto;
196200
enum sas_protocol tproto;
@@ -226,7 +230,6 @@ struct sas_discovery {
226230
int max_level;
227231
};
228232

229-
230233
/* The port struct is Class:RW, driver:RO */
231234
struct asd_sas_port {
232235
/* private: */
@@ -236,6 +239,8 @@ struct asd_sas_port {
236239
struct domain_device *port_dev;
237240
spinlock_t dev_list_lock;
238241
struct list_head dev_list;
242+
struct list_head disco_list;
243+
struct list_head destroy_list;
239244
enum sas_linkrate linkrate;
240245

241246
struct sas_phy *phy;
@@ -334,6 +339,7 @@ struct sas_ha_event {
334339
enum sas_ha_state {
335340
SAS_HA_REGISTERED,
336341
SAS_HA_DRAINING,
342+
SAS_HA_ATA_EH_ACTIVE,
337343
};
338344

339345
struct sas_ha_struct {
@@ -346,6 +352,8 @@ struct sas_ha_struct {
346352
unsigned long state;
347353
spinlock_t state_lock;
348354

355+
struct mutex disco_mutex;
356+
349357
struct scsi_core core;
350358

351359
/* public: */

0 commit comments

Comments
 (0)