Skip to content
This repository was archived by the owner on Nov 8, 2023. It is now read-only.

Commit a86d276

Browse files
author
Bartosz Golaszewski
committed
gpiolib: fix the speed of descriptor label setting with SRCU
Commit 1f2bcb8 ("gpio: protect the descriptor label with SRCU") caused a massive drop in performance of requesting GPIO lines due to the call to synchronize_srcu() on each label change. Rework the code to not wait until all read-only users are done with reading the label but instead atomically replace the label pointer and schedule its release after all read-only critical sections are done. To that end wrap the descriptor label in a struct that also contains the rcu_head struct required for deferring tasks using call_srcu() and stop using kstrdup_const() as we're required to allocate memory anyway. Just allocate enough for the label string and rcu_head in one go. Reported-by: Neil Armstrong <neil.armstrong@linaro.org> Closes: https://lore.kernel.org/linux-gpio/CAMRc=Mfig2oooDQYTqo23W3PXSdzhVO4p=G4+P8y1ppBOrkrJQ@mail.gmail.com/ Fixes: 1f2bcb8 ("gpio: protect the descriptor label with SRCU") Suggested-by: "Paul E. McKenney" <paulmck@kernel.org> Tested-by: Neil Armstrong <neil.armstrong@linaro.org> # on SM8650-QRD Acked-by: "Paul E. McKenney" <paulmck@kernel.org> Link: https://lore.kernel.org/r/20240507121346.16969-1-brgl@bgdev.pl Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
1 parent e67572c commit a86d276

File tree

2 files changed

+30
-8
lines changed

2 files changed

+30
-8
lines changed

drivers/gpio/gpiolib.c

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -101,30 +101,44 @@ static bool gpiolib_initialized;
101101

102102
const char *gpiod_get_label(struct gpio_desc *desc)
103103
{
104+
struct gpio_desc_label *label;
104105
unsigned long flags;
105106

106107
flags = READ_ONCE(desc->flags);
107108
if (test_bit(FLAG_USED_AS_IRQ, &flags) &&
108109
!test_bit(FLAG_REQUESTED, &flags))
109110
return "interrupt";
110111

111-
return test_bit(FLAG_REQUESTED, &flags) ?
112-
srcu_dereference(desc->label, &desc->srcu) : NULL;
112+
if (!test_bit(FLAG_REQUESTED, &flags))
113+
return NULL;
114+
115+
label = srcu_dereference_check(desc->label, &desc->srcu,
116+
srcu_read_lock_held(&desc->srcu));
117+
118+
return label->str;
119+
}
120+
121+
static void desc_free_label(struct rcu_head *rh)
122+
{
123+
kfree(container_of(rh, struct gpio_desc_label, rh));
113124
}
114125

115126
static int desc_set_label(struct gpio_desc *desc, const char *label)
116127
{
117-
const char *new = NULL, *old;
128+
struct gpio_desc_label *new = NULL, *old;
118129

119130
if (label) {
120-
new = kstrdup_const(label, GFP_KERNEL);
131+
new = kzalloc(struct_size(new, str, strlen(label) + 1),
132+
GFP_KERNEL);
121133
if (!new)
122134
return -ENOMEM;
135+
136+
strcpy(new->str, label);
123137
}
124138

125139
old = rcu_replace_pointer(desc->label, new, 1);
126-
synchronize_srcu(&desc->srcu);
127-
kfree_const(old);
140+
if (old)
141+
call_srcu(&desc->srcu, &old->rh, desc_free_label);
128142

129143
return 0;
130144
}
@@ -697,8 +711,11 @@ static void gpiodev_release(struct device *dev)
697711
struct gpio_device *gdev = to_gpio_device(dev);
698712
unsigned int i;
699713

700-
for (i = 0; i < gdev->ngpio; i++)
714+
for (i = 0; i < gdev->ngpio; i++) {
715+
/* Free pending label. */
716+
synchronize_srcu(&gdev->descs[i].srcu);
701717
cleanup_srcu_struct(&gdev->descs[i].srcu);
718+
}
702719

703720
ida_free(&gpio_ida, gdev->id);
704721
kfree_const(gdev->label);

drivers/gpio/gpiolib.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,11 @@ int gpiod_set_transitory(struct gpio_desc *desc, bool transitory);
137137

138138
void gpiod_line_state_notify(struct gpio_desc *desc, unsigned long action);
139139

140+
struct gpio_desc_label {
141+
struct rcu_head rh;
142+
char str[];
143+
};
144+
140145
/**
141146
* struct gpio_desc - Opaque descriptor for a GPIO
142147
*
@@ -177,7 +182,7 @@ struct gpio_desc {
177182
#define FLAG_EVENT_CLOCK_HTE 19 /* GPIO CDEV reports hardware timestamps in events */
178183

179184
/* Connection label */
180-
const char __rcu *label;
185+
struct gpio_desc_label __rcu *label;
181186
/* Name of the GPIO */
182187
const char *name;
183188
#ifdef CONFIG_OF_DYNAMIC

0 commit comments

Comments
 (0)