History

pinctrl: samsung: fix SMP race condition

Previously, samsung_gpio_drection_in/output function were not covered with a spinlock. For example, samsung_gpio_direction_output function consists of two functions. 1. samsung_gpio_set 2. samsung_gpio_set_direction When 2 CPUs try to control the same gpio pin heavily, (situation like i2c control with gpio emulation) This situation can cause below problem. CPU 0 | CPU1 | samsung_gpio_direction_output | samsung_gpio_set(pin A as 1) | samsung_gpio_direction_output | samsung_gpio_set(pin A as 0) samsung_gpio_set_direction | | samsung_gpio_set_direction The initial value of pin A will be set as 0 while we wanted to set pin A as 1. This patch modifies samsung_gpio_direction_in/output function to be done in one spinlock to fix race condition. Additionally, the new samsung_gpio_set_value was added to implement gpio set callback(samsung_gpio_set) with spinlock using this function.

Youngmin Nam committed 10 years ago

Commit d9ff0eb1 parent 7864d921 file · +35 −13Browse files

Unified · Split

drivers/pinctrl/samsung/pinctrl-samsung.c+35−13
@@ -514,25 +514,35 @@ static const struct pinconf_ops samsung_pinconf_ops = { · 513 unmodified lines
514514 .pin_config_group_set = samsung_pinconf_group_set,
515515 };
516516
517−/* gpiolib gpio_set callback function */
518−static void samsung_gpio_set(struct gpio_chip *gc, unsigned offset, int value)
517+/*
518+ * The samsung_gpio_set_vlaue() should be called with "bank->slock" held
519+ * to avoid race condition.
520+ */
521+static void samsung_gpio_set_value(struct gpio_chip *gc,
522+ unsigned offset, int value)
519523 {
520524 struct samsung_pin_bank *bank = gpiochip_get_data(gc);
521525 const struct samsung_pin_bank_type *type = bank->type;
522− unsigned long flags;
523526 void __iomem *reg;
524527 u32 data;
525528
526529 reg = bank->drvdata->virt_base + bank->pctl_offset;
527530
528− spin_lock_irqsave(&bank->slock, flags);
529−
530531 data = readl(reg + type->reg_offset[PINCFG_TYPE_DAT]);
531532 data &= ~(1 << offset);
532533 if (value)
533534 data |= 1 << offset;
534535 writel(data, reg + type->reg_offset[PINCFG_TYPE_DAT]);
536+}
537+
538+/* gpiolib gpio_set callback function */
539+static void samsung_gpio_set(struct gpio_chip *gc, unsigned offset, int value)
540+{
541+ struct samsung_pin_bank *bank = gpiochip_get_data(gc);
542+ unsigned long flags;
535543
544+ spin_lock_irqsave(&bank->slock, flags);
545+ samsung_gpio_set_value(gc, offset, value);
536546 spin_unlock_irqrestore(&bank->slock, flags);
537547 }
538548
@@ -553,6 +563,8 @@ static int samsung_gpio_get(struct gpio_chip *gc, unsigned offset) · 14 unmodified lines
553563 }
554564
555565 /*
566+ * The samsung_gpio_set_direction() should be called with "bank->slock" held
567+ * to avoid race condition.
556568 * The calls to gpio_direction_output() and gpio_direction_input()
557569 * leads to this function call.
558570 */
@@ -564,7 +576,6 @@ static int samsung_gpio_set_direction(struct gpio_chip *gc, · 5 unmodified lines
564576 struct samsung_pinctrl_drv_data *drvdata;
565577 void __iomem *reg;
566578 u32 data, mask, shift;
567− unsigned long flags;
568579
569580 bank = gpiochip_get_data(gc);
570581 type = bank->type;
@@ -581,31 +592,42 @@ static int samsung_gpio_set_direction(struct gpio_chip *gc, · 10 unmodified lines
581592 reg += 4;
582593 }
583594
584− spin_lock_irqsave(&bank->slock, flags);
585−
586595 data = readl(reg);
587596 data &= ~(mask << shift);
588597 if (!input)
589598 data |= FUNC_OUTPUT << shift;
590599 writel(data, reg);
591600
592− spin_unlock_irqrestore(&bank->slock, flags);
593−
594601 return 0;
595602 }
596603
597604 /* gpiolib gpio_direction_input callback function. */
598605 static int samsung_gpio_direction_input(struct gpio_chip *gc, unsigned offset)
599606 {
600− return samsung_gpio_set_direction(gc, offset, true);
607+ struct samsung_pin_bank *bank = gpiochip_get_data(gc);
608+ unsigned long flags;
609+ int ret;
610+
611+ spin_lock_irqsave(&bank->slock, flags);
612+ ret = samsung_gpio_set_direction(gc, offset, true);
613+ spin_unlock_irqrestore(&bank->slock, flags);
614+ return ret;
601615 }
602616
603617 /* gpiolib gpio_direction_output callback function. */
604618 static int samsung_gpio_direction_output(struct gpio_chip *gc, unsigned offset,
605619 int value)
606620 {
607− samsung_gpio_set(gc, offset, value);
608− return samsung_gpio_set_direction(gc, offset, false);
621+ struct samsung_pin_bank *bank = gpiochip_get_data(gc);
622+ unsigned long flags;
623+ int ret;
624+
625+ spin_lock_irqsave(&bank->slock, flags);
626+ samsung_gpio_set_value(gc, offset, value);
627+ ret = samsung_gpio_set_direction(gc, offset, false);
628+ spin_unlock_irqrestore(&bank->slock, flags);
629+
630+ return ret;
609631 }
610632
611633 /*

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t