Skip to contentSkip to conversation

counter: 104-quad-8: Add lock guards - differential encoder cable

PATCH Superseded Syed Nayyar Waris 2 participants 2 messages Mar 12, 2020

Subsystems (3)

v3 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

PATCH v3 2/3 counter: 104-quad-8: Add lock guards - differential encoder cable

Add lock protection from race conditions in the 104-quad-8 counter driver for differential encoder cable status changes. There is no IRQ handling so spin_lock calls are used for protection.

Fixes bbef69e088c3 ("counter: 104-quad-8: Support Differential Encoder

Cable Status")

Split the patch from generic driver interface and clock prescaler related code changes. Also, include more code statements for protection using spin_lock calls and remove protection from few code statements as they are unnecessary.

---
 drivers/counter/104-quad-8.c | 12 +++++++++++-
 1 file changed, 11 insertions(+), 1 deletion(-)
drivers/counter/104-quad-8.c +11 -1
index 9dab190..1ce9660 100644
@@ -1153,16 +1153,22 @@ static ssize_t quad8_signal_cable_fault_read(struct counter_device *counter,
 {
 	const struct quad8_iio *const priv = counter->priv;
 	const size_t channel_id = signal->id / 2;
-	const bool disabled = !(priv->cable_fault_enable & BIT(channel_id));
+	bool disabled;
 	unsigned int status;
 	unsigned int fault;
 
+	spin_lock(&((struct quad8_iio *)priv)->lock);
+
+	disabled = !(priv->cable_fault_enable & BIT(channel_id));
+
 	if (disabled)
 		return -EINVAL;
 
 	/* Logic 0 = cable fault */
 	status = inb(priv->base + QUAD8_DIFF_ENCODER_CABLE_STATUS);
 
+	spin_unlock(&((struct quad8_iio *)priv)->lock);
+
 	/* Mask respective channel and invert logic */
 	fault = !(status & BIT(channel_id));
 
@@ -1194,6 +1200,8 @@ static ssize_t quad8_signal_cable_fault_enable_write(
 	if (ret)
 		return ret;
 
+	spin_lock(&priv->lock);
+
 	if (enable)
 		priv->cable_fault_enable |= BIT(channel_id);
 	else
@@ -1204,6 +1212,8 @@ static ssize_t quad8_signal_cable_fault_enable_write(
 
 	outb(cable_fault_enable, priv->base + QUAD8_DIFF_ENCODER_CABLE_STATUS);
 
+	spin_unlock(&priv->lock);
+
 	return len;
 }
 
Signature · 1 line
2.7.4
#1 lore raw
Quoted 7 lines · Syed Nayyar Waris

Add lock protection from race conditions in the 104-quad-8 counter driver for differential encoder cable status changes. There is no IRQ handling so spin_lock calls are used for protection.

Fixes bbef69e088c3 ("counter: 104-quad-8: Support Differential Encoder

Cable Status")

Quoted 7 lines

Split the patch from generic driver interface and clock prescaler related code changes. Also, include more code statements for protection using spin_lock calls and remove protection from few code statements as they are unnecessary.
---

Hello Syed,

Just like in the first patch, move these comments below the "---" line.

Quoted 8 lines
 drivers/counter/104-quad-8.c | 12 +++++++++++-
 1 file changed, 11 insertions(+), 1 deletion(-)
drivers/counter/104-quad-8.c +0 -0
index 9dab190..1ce9660 100644

drivers/counter/104-quad-8.c static ssize_t quad8_signal_cable_fault_read(struct counter_device *counter, · line 1160

Quoted 9 lines
drivers/counter/104-quad-8.c +2 -1
@@ -1153,16 +1153,22 @@ static ssize_t quad8_signal_cable_fault_read(struct counter_device *counter,
 {
 	const struct quad8_iio *const priv = counter->priv;
 	const size_t channel_id = signal->id / 2;
-	const bool disabled = !(priv->cable_fault_enable & BIT(channel_id));
+	bool disabled;
 	unsigned int status;
 	unsigned int fault;
 
+	spin_lock(&((struct quad8_iio *)priv)->lock);

You can redeclare priv whenever you need to avoid these casts:

	struct quad8_iio *const priv = counter->priv;
	...
	spin_lock(&priv->lock);
	...
	spin_unlock(&priv->lock);

drivers/counter/104-quad-8.c static ssize_t quad8_signal_cable_fault_read(struct counter_device *counter, · line 1165

Quoted 5 lines
drivers/counter/104-quad-8.c +3 -0
+
+	disabled = !(priv->cable_fault_enable & BIT(channel_id));
+
 	if (disabled)
 		return -EINVAL;

This return statement can cause a deadlock. You can avoid that by calling spin_unlock before the return:

	if (disabled) {
		spin_unlock(&priv->lock);
		return -EINVAL;
	}

Sincerely,

William Breathitt Gray

Quoted 30 lines
drivers/counter/104-quad-8.c +6 -0
 	/* Logic 0 = cable fault */
 	status = inb(priv->base + QUAD8_DIFF_ENCODER_CABLE_STATUS);
 
+	spin_unlock(&((struct quad8_iio *)priv)->lock);
+
 	/* Mask respective channel and invert logic */
 	fault = !(status & BIT(channel_id));
 
@@ -1194,6 +1200,8 @@ static ssize_t quad8_signal_cable_fault_enable_write(
 	if (ret)
 		return ret;
 
+	spin_lock(&priv->lock);
+
 	if (enable)
 		priv->cable_fault_enable |= BIT(channel_id);
 	else
@@ -1204,6 +1212,8 @@ static ssize_t quad8_signal_cable_fault_enable_write(
 
 	outb(cable_fault_enable, priv->base + QUAD8_DIFF_ENCODER_CABLE_STATUS);
 
+	spin_unlock(&priv->lock);
+
 	return len;
 }
 
Signature · 1 line
2.7.4