Skip to content

Commit 58bdc32

Browse files
a-darwishgregkh
authored andcommitted
scsi: libsas: Introduce a _gfp() variant of event notifiers
[ Upstream commit c2d0f1a ] sas_alloc_event() uses in_interrupt() to decide which allocation should be used. The usage of in_interrupt() in drivers is phased out and Linus clearly requested that code which changes behaviour depending on context should either be separated or the context be conveyed in an argument passed by the caller, which usually knows the context. The in_interrupt() check is also only partially correct, because it fails to choose the correct code path when just preemption or interrupts are disabled. For example, as in the following call chain: mvsas/mv_sas.c: mvs_work_queue() [process context] spin_lock_irqsave(mvs_info::lock, ) -> libsas/sas_event.c: sas_notify_phy_event() -> sas_alloc_event() -> in_interrupt() = false -> invalid GFP_KERNEL allocation -> libsas/sas_event.c: sas_notify_port_event() -> sas_alloc_event() -> in_interrupt() = false -> invalid GFP_KERNEL allocation Introduce sas_alloc_event_gfp(), sas_notify_port_event_gfp(), and sas_notify_phy_event_gfp(), which all behave like the non _gfp() variants but use a caller-passed GFP mask for allocations. For bisectability, all callers will be modified first to pass GFP context, then the non _gfp() libsas API variants will be modified to take a gfp_t by default. Link: https://lore.kernel.org/r/20210118100955.1761652-4-a.darwish@linutronix.de Fixes: 1c393b9 ("scsi: libsas: Use dynamic alloced work to avoid sas event lost") Cc: Jason Yan <yanaijie@huawei.com> Reviewed-by: John Garry <john.garry@huawei.com> Signed-off-by: Ahmed S. Darwish <a.darwish@linutronix.de> Signed-off-by: Martin K. Petersen <martin.petersen@oracle.com> Signed-off-by: Sasha Levin <sashal@kernel.org>
1 parent 18c3c04 commit 58bdc32

5 files changed

Lines changed: 79 additions & 17 deletions

File tree

‎Documentation/scsi/libsas.rst‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,8 @@ The event interface::
191191
/* LLDD calls these to notify the class of an event. */
192192
void sas_notify_port_event(struct sas_phy *, enum port_event);
193193
void sas_notify_phy_event(struct sas_phy *, enum phy_event);
194+
void sas_notify_port_event_gfp(struct sas_phy *, enum port_event, gfp_t);
195+
void sas_notify_phy_event_gfp(struct sas_phy *, enum phy_event, gfp_t);
194196

195197
The port notification::
196198

‎drivers/scsi/libsas/sas_event.c‎

Lines changed: 53 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -131,18 +131,15 @@ static void sas_phy_event_worker(struct work_struct *work)
131131
sas_free_event(ev);
132132
}
133133

134-
int sas_notify_port_event(struct asd_sas_phy *phy, enum port_event event)
134+
static int __sas_notify_port_event(struct asd_sas_phy *phy,
135+
enum port_event event,
136+
struct asd_sas_event *ev)
135137
{
136-
struct asd_sas_event *ev;
137138
struct sas_ha_struct *ha = phy->ha;
138139
int ret;
139140

140141
BUG_ON(event >= PORT_NUM_EVENTS);
141142

142-
ev = sas_alloc_event(phy);
143-
if (!ev)
144-
return -ENOMEM;
145-
146143
INIT_SAS_EVENT(ev, sas_port_event_worker, phy, event);
147144

148145
ret = sas_queue_event(event, &ev->work, ha);
@@ -151,20 +148,41 @@ int sas_notify_port_event(struct asd_sas_phy *phy, enum port_event event)
151148

152149
return ret;
153150
}
154-
EXPORT_SYMBOL_GPL(sas_notify_port_event);
155151

156-
int sas_notify_phy_event(struct asd_sas_phy *phy, enum phy_event event)
152+
int sas_notify_port_event_gfp(struct asd_sas_phy *phy, enum port_event event,
153+
gfp_t gfp_flags)
157154
{
158155
struct asd_sas_event *ev;
159-
struct sas_ha_struct *ha = phy->ha;
160-
int ret;
161156

162-
BUG_ON(event >= PHY_NUM_EVENTS);
157+
ev = sas_alloc_event_gfp(phy, gfp_flags);
158+
if (!ev)
159+
return -ENOMEM;
160+
161+
return __sas_notify_port_event(phy, event, ev);
162+
}
163+
EXPORT_SYMBOL_GPL(sas_notify_port_event_gfp);
164+
165+
int sas_notify_port_event(struct asd_sas_phy *phy, enum port_event event)
166+
{
167+
struct asd_sas_event *ev;
163168

164169
ev = sas_alloc_event(phy);
165170
if (!ev)
166171
return -ENOMEM;
167172

173+
return __sas_notify_port_event(phy, event, ev);
174+
}
175+
EXPORT_SYMBOL_GPL(sas_notify_port_event);
176+
177+
static inline int __sas_notify_phy_event(struct asd_sas_phy *phy,
178+
enum phy_event event,
179+
struct asd_sas_event *ev)
180+
{
181+
struct sas_ha_struct *ha = phy->ha;
182+
int ret;
183+
184+
BUG_ON(event >= PHY_NUM_EVENTS);
185+
168186
INIT_SAS_EVENT(ev, sas_phy_event_worker, phy, event);
169187

170188
ret = sas_queue_event(event, &ev->work, ha);
@@ -173,5 +191,28 @@ int sas_notify_phy_event(struct asd_sas_phy *phy, enum phy_event event)
173191

174192
return ret;
175193
}
176-
EXPORT_SYMBOL_GPL(sas_notify_phy_event);
177194

195+
int sas_notify_phy_event_gfp(struct asd_sas_phy *phy, enum phy_event event,
196+
gfp_t gfp_flags)
197+
{
198+
struct asd_sas_event *ev;
199+
200+
ev = sas_alloc_event_gfp(phy, gfp_flags);
201+
if (!ev)
202+
return -ENOMEM;
203+
204+
return __sas_notify_phy_event(phy, event, ev);
205+
}
206+
EXPORT_SYMBOL_GPL(sas_notify_phy_event_gfp);
207+
208+
int sas_notify_phy_event(struct asd_sas_phy *phy, enum phy_event event)
209+
{
210+
struct asd_sas_event *ev;
211+
212+
ev = sas_alloc_event(phy);
213+
if (!ev)
214+
return -ENOMEM;
215+
216+
return __sas_notify_phy_event(phy, event, ev);
217+
}
218+
EXPORT_SYMBOL_GPL(sas_notify_phy_event);

‎drivers/scsi/libsas/sas_init.c‎

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -584,16 +584,15 @@ sas_domain_attach_transport(struct sas_domain_function_template *dft)
584584
}
585585
EXPORT_SYMBOL_GPL(sas_domain_attach_transport);
586586

587-
588-
struct asd_sas_event *sas_alloc_event(struct asd_sas_phy *phy)
587+
static struct asd_sas_event *__sas_alloc_event(struct asd_sas_phy *phy,
588+
gfp_t gfp_flags)
589589
{
590590
struct asd_sas_event *event;
591-
gfp_t flags = in_interrupt() ? GFP_ATOMIC : GFP_KERNEL;
592591
struct sas_ha_struct *sas_ha = phy->ha;
593592
struct sas_internal *i =
594593
to_sas_internal(sas_ha->core.shost->transportt);
595594

596-
event = kmem_cache_zalloc(sas_event_cache, flags);
595+
event = kmem_cache_zalloc(sas_event_cache, gfp_flags);
597596
if (!event)
598597
return NULL;
599598

@@ -604,7 +603,8 @@ struct asd_sas_event *sas_alloc_event(struct asd_sas_phy *phy)
604603
if (cmpxchg(&phy->in_shutdown, 0, 1) == 0) {
605604
pr_notice("The phy%d bursting events, shut it down.\n",
606605
phy->id);
607-
sas_notify_phy_event(phy, PHYE_SHUTDOWN);
606+
sas_notify_phy_event_gfp(phy, PHYE_SHUTDOWN,
607+
gfp_flags);
608608
}
609609
} else {
610610
/* Do not support PHY control, stop allocating events */
@@ -618,6 +618,17 @@ struct asd_sas_event *sas_alloc_event(struct asd_sas_phy *phy)
618618
return event;
619619
}
620620

621+
struct asd_sas_event *sas_alloc_event(struct asd_sas_phy *phy)
622+
{
623+
return __sas_alloc_event(phy, in_interrupt() ? GFP_ATOMIC : GFP_KERNEL);
624+
}
625+
626+
struct asd_sas_event *sas_alloc_event_gfp(struct asd_sas_phy *phy,
627+
gfp_t gfp_flags)
628+
{
629+
return __sas_alloc_event(phy, gfp_flags);
630+
}
631+
621632
void sas_free_event(struct asd_sas_event *event)
622633
{
623634
struct asd_sas_phy *phy = event->phy;

‎drivers/scsi/libsas/sas_internal.h‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ int sas_register_phys(struct sas_ha_struct *sas_ha);
4949
void sas_unregister_phys(struct sas_ha_struct *sas_ha);
5050

5151
struct asd_sas_event *sas_alloc_event(struct asd_sas_phy *phy);
52+
struct asd_sas_event *sas_alloc_event_gfp(struct asd_sas_phy *phy,
53+
gfp_t gfp_flags);
5254
void sas_free_event(struct asd_sas_event *event);
5355

5456
int sas_register_ports(struct sas_ha_struct *sas_ha);
@@ -77,6 +79,8 @@ int sas_smp_phy_control(struct domain_device *dev, int phy_id,
7779
int sas_smp_get_phy_events(struct sas_phy *phy);
7880

7981
int sas_notify_phy_event(struct asd_sas_phy *phy, enum phy_event event);
82+
int sas_notify_phy_event_gfp(struct asd_sas_phy *phy, enum phy_event event,
83+
gfp_t flags);
8084
void sas_device_set_phy(struct domain_device *dev, struct sas_port *port);
8185
struct domain_device *sas_find_dev_by_rphy(struct sas_rphy *rphy);
8286
struct domain_device *sas_ex_to_ata(struct domain_device *ex_dev, int phy_id);

‎include/scsi/libsas.h‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -704,5 +704,9 @@ int sas_request_addr(struct Scsi_Host *shost, u8 *addr);
704704

705705
int sas_notify_port_event(struct asd_sas_phy *phy, enum port_event event);
706706
int sas_notify_phy_event(struct asd_sas_phy *phy, enum phy_event event);
707+
int sas_notify_port_event_gfp(struct asd_sas_phy *phy, enum port_event event,
708+
gfp_t gfp_flags);
709+
int sas_notify_phy_event_gfp(struct asd_sas_phy *phy, enum phy_event event,
710+
gfp_t gfp_flags);
707711

708712
#endif /* _SASLIB_H_ */

0 commit comments

Comments
 (0)