Skip to content

Commit cde255b

Browse files
cms19859230182-langRbb666
authored andcommitted
[drivers][pin] publish legacy ISR handler under pin_irq_lock protection
Fix #11784 When the GPIO controller has no pin_attach_irq operation, pin_api_attach_irq() writes legacy_isr->hdr and legacy_isr->args as two independent stores. An interrupt arriving concurrently could make pin_pic_handle_isr() call the new handler with the old argument. Protect legacy_isr updates and clears in pin_api_attach_irq() and pin_api_detach_irq() with a dedicated spinlock pin_irq_lock (via rt_spin_lock_irqsave). In pin_pic_handle_isr(), snapshot the hdr and args fields under pin_irq_lock, then invoke the callback after releasing the lock to avoid executing user handlers while holding the spinlock.
1 parent 8bfdb97 commit cde255b

1 file changed

Lines changed: 26 additions & 12 deletions

File tree

‎components/drivers/pin/dev_pin_dm.c‎

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
static rt_size_t pin_total_nr = 0;
1414
static RT_DEFINE_SPINLOCK(pin_lock);
15+
static RT_DEFINE_SPINLOCK(pin_irq_lock);
1516
static rt_list_t pin_nodes = RT_LIST_OBJECT_INIT(pin_nodes);
1617

1718
static struct rt_device_pin *pin_device_find(rt_ubase_t pin)
@@ -68,7 +69,7 @@ static rt_ssize_t pin_api_read(struct rt_device *device, rt_base_t pin)
6869
}
6970

7071
static rt_err_t pin_api_attach_irq(struct rt_device *device, rt_base_t pin,
71-
rt_uint8_t mode, void (*hdr)(void *args), void *args)
72+
rt_uint8_t mode, void (*hdr)(void *args), void *args)
7273
{
7374
struct rt_device_pin *gpio = pin_device_find(pin);
7475

@@ -79,18 +80,22 @@ static rt_err_t pin_api_attach_irq(struct rt_device *device, rt_base_t pin,
7980
if (!gpio->ops->pin_attach_irq)
8081
{
8182
rt_err_t err;
83+
rt_base_t level;
8284
struct rt_pin_irq_hdr *legacy_isr;
8385

8486
if ((err = gpio->ops->pin_irq_mode(&gpio->parent, pin_index, mode)))
8587
{
8688
return err;
8789
}
8890

91+
/* publish the handler pair as one update for pin_pic_handle_isr() */
92+
level = rt_spin_lock_irqsave(&pin_irq_lock);
8993
legacy_isr = &gpio->legacy_isr[pin_index];
9094
legacy_isr->pin = pin_index;
9195
legacy_isr->mode = mode;
9296
legacy_isr->hdr = hdr;
9397
legacy_isr->args = args;
98+
rt_spin_unlock_irqrestore(&pin_irq_lock, level);
9499

95100
return RT_EOK;
96101
}
@@ -113,10 +118,14 @@ static rt_err_t pin_api_detach_irq(struct rt_device *device, rt_base_t pin)
113118

114119
if (!gpio->ops->pin_detach_irq)
115120
{
121+
rt_base_t level;
116122
struct rt_pin_irq_hdr *legacy_isr;
117123

124+
/* clear the handler pair as one update for pin_pic_handle_isr() */
125+
level = rt_spin_lock_irqsave(&pin_irq_lock);
118126
legacy_isr = &gpio->legacy_isr[pin_index];
119127
rt_memset(legacy_isr, 0, sizeof(*legacy_isr));
128+
rt_spin_unlock_irqrestore(&pin_irq_lock, level);
120129

121130
return RT_EOK;
122131
}
@@ -130,7 +139,7 @@ static rt_err_t pin_api_detach_irq(struct rt_device *device, rt_base_t pin)
130139
}
131140

132141
static rt_err_t pin_api_irq_enable(struct rt_device *device, rt_base_t pin,
133-
rt_uint8_t enabled)
142+
rt_uint8_t enabled)
134143
{
135144
struct rt_device_pin *gpio = pin_device_find(pin);
136145

@@ -163,7 +172,7 @@ static rt_base_t pin_api_get(const char *name)
163172
}
164173

165174
static rt_err_t pin_api_debounce(struct rt_device *device, rt_base_t pin,
166-
rt_uint32_t debounce)
175+
rt_uint32_t debounce)
167176
{
168177
struct rt_device_pin *gpio = pin_device_find(pin);
169178

@@ -176,7 +185,7 @@ static rt_err_t pin_api_debounce(struct rt_device *device, rt_base_t pin,
176185
}
177186

178187
static rt_err_t pin_api_irq_mode(struct rt_device *device, rt_base_t pin,
179-
rt_uint8_t mode)
188+
rt_uint8_t mode)
180189
{
181190
struct rt_device_pin *gpio = pin_device_find(pin);
182191

@@ -188,8 +197,7 @@ static rt_err_t pin_api_irq_mode(struct rt_device *device, rt_base_t pin,
188197
return -RT_EINVAL;
189198
}
190199

191-
static const struct rt_pin_ops pin_api_dm_ops =
192-
{
200+
static const struct rt_pin_ops pin_api_dm_ops = {
193201
.pin_mode = pin_api_mode,
194202
.pin_write = pin_api_write,
195203
.pin_read = pin_api_read,
@@ -300,7 +308,7 @@ static int pin_dm_irq_map(struct rt_pic *pic, int hwirq, rt_uint32_t mode)
300308
}
301309

302310
static rt_err_t pin_dm_irq_parse(struct rt_pic *pic,
303-
struct rt_ofw_cell_args *args, struct rt_pic_irq *out_pirq)
311+
struct rt_ofw_cell_args *args, struct rt_pic_irq *out_pirq)
304312
{
305313
rt_err_t err = RT_EOK;
306314

@@ -317,8 +325,7 @@ static rt_err_t pin_dm_irq_parse(struct rt_pic *pic,
317325
return err;
318326
}
319327

320-
const static struct rt_pic_ops pin_dm_ops =
321-
{
328+
const static struct rt_pic_ops pin_dm_ops = {
322329
.name = "GPIO",
323330
.irq_enable = pin_dm_irq_mask,
324331
.irq_disable = pin_dm_irq_unmask,
@@ -342,6 +349,9 @@ rt_err_t pin_pic_handle_isr(struct rt_device_pin *gpio, rt_base_t pin)
342349
{
343350
struct rt_pic_irq *pirq;
344351
struct rt_pin_irq_hdr *legacy_isr;
352+
void (*hdr)(void *args) = RT_NULL;
353+
void *args = RT_NULL;
354+
rt_base_t level;
345355

346356
pirq = rt_pic_find_irq(&irqchip->parent, pin_index);
347357

@@ -354,11 +364,15 @@ rt_err_t pin_pic_handle_isr(struct rt_device_pin *gpio, rt_base_t pin)
354364
err = -RT_EINVAL;
355365
}
356366

367+
level = rt_spin_lock_irqsave(&pin_irq_lock);
357368
legacy_isr = &gpio->legacy_isr[pin_index];
369+
hdr = legacy_isr->hdr;
370+
args = legacy_isr->args;
371+
rt_spin_unlock_irqrestore(&pin_irq_lock, level);
358372

359-
if (legacy_isr->hdr)
373+
if (hdr)
360374
{
361-
legacy_isr->hdr(legacy_isr->args);
375+
hdr(args);
362376
}
363377
}
364378
else
@@ -417,7 +431,7 @@ rt_err_t pin_pic_init(struct rt_device_pin *gpio, int pin_irq)
417431
}
418432

419433
rt_ssize_t rt_pin_get_named_pin(struct rt_device *dev, const char *propname, int index,
420-
rt_uint8_t *out_mode, rt_uint8_t *out_value)
434+
rt_uint8_t *out_mode, rt_uint8_t *out_value)
421435
{
422436
rt_ssize_t res = -RT_ENOSYS;
423437

0 commit comments

Comments
 (0)