Skip to contentSkip to conversation

staging: unisys: visorbus: visorchannel: Refactor locking code to be statically deterministic.

PATCH Superseded Cathal Mullaney 3 participants 6 messages Oct 19, 2016 – Oct 20, 2016 Staging

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 staging: unisys: visorbus: visorchannel: Refactor locking code to be statically deterministic.

This patch makes locking in visorchannel_signalempty statically deterministic. As a result this patch fixes the sparse warning:
Context imbalance in 'visorchannel_signalempty' - different lock contexts for basic block.

The logic of the locking code doesn't change but the layout of the original code is "frowned upon" according to mails on sparse context checking.
Refactoring removes the warning and makes the code more readable.

---
 drivers/staging/unisys/visorbus/visorchannel.c | 26 +++++++++++++++++---------
 1 file changed, 17 insertions(+), 9 deletions(-)
drivers/staging/unisys/visorbus/visorchannel.c +17 -9
index a1381eb..1eea5d8 100644
@@ -300,22 +300,30 @@ EXPORT_SYMBOL_GPL(visorchannel_signalremove);
  * Return: boolean indicating whether any messages in the designated
  *         channel/queue are present
  */
+
+static bool
+queue_empty(struct visorchannel *channel, u32 queue)
+{
+	struct signal_queue_header sig_hdr;
+
+	if (sig_read_header(channel, queue, &sig_hdr))
+		return true;
+
+	return (sig_hdr.head == sig_hdr.tail);
+}
+
 bool
 visorchannel_signalempty(struct visorchannel *channel, u32 queue)
 {
 	unsigned long flags = 0;
-	struct signal_queue_header sig_hdr;
 	bool rc = false;
 
-	if (channel->needs_lock)
-		spin_lock_irqsave(&channel->remove_lock, flags);
+	if (!channel->needs_lock)
+		return queue_empty(channel, queue);
 
-	if (sig_read_header(channel, queue, &sig_hdr))
-		rc = true;
-	if (sig_hdr.head == sig_hdr.tail)
-		rc = true;
-	if (channel->needs_lock)
-		spin_unlock_irqrestore(&channel->remove_lock, flags);
+	spin_lock_irqsave(&channel->remove_lock, flags);
+	rc = queue_empty(channel, queue);
+	spin_unlock_irqrestore(&channel->remove_lock, flags);
 
 	return rc;
 }
Signature · 1 line
2.7.4
#1 lore raw
Quoted 22 lines · Cathal Mullaney

This patch makes locking in visorchannel_signalempty statically deterministic. As a result this patch fixes the sparse warning:
Context imbalance in 'visorchannel_signalempty' - different lock contexts for basic block.

The logic of the locking code doesn't change but the layout of the original code is "frowned upon"
according to mails on sparse context checking.
Refactoring removes the warning and makes the code more readable.

---
 drivers/staging/unisys/visorbus/visorchannel.c | 26 +++++++++++++++++---
------
 1 file changed, 17 insertions(+), 9 deletions(-)
drivers/staging/unisys/visorbus/visorchannel.c +0 -0
index a1381eb..1eea5d8 100644

drivers/staging/unisys/visorbus/visorchannel.c line 304

Quoted 7 lines
drivers/staging/unisys/visorbus/visorchannel.c +0 -2
@@ -300,22 +300,30 @@
---
 bool
 visorchannel_signalempty(struct visorchannel *channel, u32 queue)
 {
 	unsigned long flags = 0;
-	struct signal_queue_header sig_hdr;
 	bool rc = false;

It appears as if you no longer need to initialize 'rc' above.

Although this is NOT caused by your patch, it also looks like 'flags' is being unnecessarily initialized. You may want to fix that too while you're in the neighborhood.

(Kernel folks seem to frown on unnecessary variable initializations.)

Quoted 19 lines
drivers/staging/unisys/visorbus/visorchannel.c +5 -9
-	if (channel->needs_lock)
-		spin_lock_irqsave(&channel->remove_lock, flags);
+	if (!channel->needs_lock)
+		return queue_empty(channel, queue);
 
-	if (sig_read_header(channel, queue, &sig_hdr))
-		rc = true;
-	if (sig_hdr.head == sig_hdr.tail)
-		rc = true;
-	if (channel->needs_lock)
-		spin_unlock_irqrestore(&channel->remove_lock, flags);
+	spin_lock_irqsave(&channel->remove_lock, flags);
+	rc = queue_empty(channel, queue);
+	spin_unlock_irqrestore(&channel->remove_lock, flags);
 
 	return rc;
 }
--
Quoted 1 line

2.7.4

Besides that, your patch looks good to me. Thanks.

- Tim Sell

#2 lore raw
Quoted 55 lines · Timothy C
Quoted 22 lines · Cathal Mullaney

This patch makes locking in visorchannel_signalempty statically deterministic. As a result this patch fixes the sparse warning:
Context imbalance in 'visorchannel_signalempty' - different lock contexts for basic block.

The logic of the locking code doesn't change but the layout of the original code is "frowned upon"
according to mails on sparse context checking.
Refactoring removes the warning and makes the code more readable.

---
 drivers/staging/unisys/visorbus/visorchannel.c | 26 +++++++++++++++++---
------
 1 file changed, 17 insertions(+), 9 deletions(-)
drivers/staging/unisys/visorbus/visorchannel.c +0 -0
index a1381eb..1eea5d8 100644

drivers/staging/unisys/visorbus/visorchannel.c line 304

Quoted 7 lines
drivers/staging/unisys/visorbus/visorchannel.c +0 -2
@@ -300,22 +300,30 @@
---
 bool
 visorchannel_signalempty(struct visorchannel *channel, u32 queue)
 {
 	unsigned long flags = 0;
-	struct signal_queue_header sig_hdr;
 	bool rc = false;

It appears as if you no longer need to initialize 'rc' above.

Although this is NOT caused by your patch, it also looks like 'flags' is being unnecessarily initialized. You may want to fix that too while you're in the neighborhood.

(Kernel folks seem to frown on unnecessary variable initializations.)

Quoted 16 lines
drivers/staging/unisys/visorbus/visorchannel.c +5 -8
-	if (channel->needs_lock)
-		spin_lock_irqsave(&channel->remove_lock, flags);
+	if (!channel->needs_lock)
+		return queue_empty(channel, queue);
 
-	if (sig_read_header(channel, queue, &sig_hdr))
-		rc = true;
-	if (sig_hdr.head == sig_hdr.tail)
-		rc = true;
-	if (channel->needs_lock)
-		spin_unlock_irqrestore(&channel->remove_lock, flags);
+	spin_lock_irqsave(&channel->remove_lock, flags);
+	rc = queue_empty(channel, queue);
+	spin_unlock_irqrestore(&channel->remove_lock, flags);
 
Quoted 9 lines
Quoted 4 lines
drivers/staging/unisys/visorbus/visorchannel.c +0 -1
 	return rc;
 }
--

2.7.4

Besides that, your patch looks good to me. Thanks.

- Tim Sell

Thanks for your feedback Tim.
Submitted v2 with the suggested updates.
Thanks again.

Kind regards,
Cathal

#3 lore raw

PATCH v2 1/1 staging: unisys: visorbus: visorchannel: Refactor locking code to be statically deterministic.

In reply to Cathal Mullaney

This patch makes locking in visorchannel_signalempty statically deterministic.
As a result this patch fixes the sparse warning:
Context imbalance in 'visorchannel_signalempty' - different lock contexts for basic block.

The logic of the locking code doesn't change but the layout of the original code is "frowned upon"
according to mails on sparse context checking. Refactoring removes the warning and makes the code more readable.

---
V2: Removed unnecessary variable initialization, as suggested by Tim Sell <[email protected]>.

 drivers/staging/unisys/visorbus/visorchannel.c | 30 ++++++++++++++++----------
 1 file changed, 19 insertions(+), 11 deletions(-)
drivers/staging/unisys/visorbus/visorchannel.c +19 -11
index a1381eb..a411157 100644
@@ -300,22 +300,30 @@ EXPORT_SYMBOL_GPL(visorchannel_signalremove);
  * Return: boolean indicating whether any messages in the designated
  *         channel/queue are present
  */
+
+static bool
+queue_empty(struct visorchannel *channel, u32 queue)
+{
+	struct signal_queue_header sig_hdr;
+
+	if (sig_read_header(channel, queue, &sig_hdr))
+		return true;
+
+	return (sig_hdr.head == sig_hdr.tail);
+}
+
 bool
 visorchannel_signalempty(struct visorchannel *channel, u32 queue)
 {
-	unsigned long flags = 0;
-	struct signal_queue_header sig_hdr;
-	bool rc = false;
+	bool rc;
+	unsigned long flags;
 
-	if (channel->needs_lock)
-		spin_lock_irqsave(&channel->remove_lock, flags);
+	if (!channel->needs_lock)
+		return queue_empty(channel, queue);
 
-	if (sig_read_header(channel, queue, &sig_hdr))
-		rc = true;
-	if (sig_hdr.head == sig_hdr.tail)
-		rc = true;
-	if (channel->needs_lock)
-		spin_unlock_irqrestore(&channel->remove_lock, flags);
+	spin_lock_irqsave(&channel->remove_lock, flags);
+	rc = queue_empty(channel, queue);
+	spin_unlock_irqrestore(&channel->remove_lock, flags);
 
 	return rc;
 }
Signature · 1 line
2.7.4
Tested-by on v2 1/1
#4 lore raw
Quoted 11 lines

-----Original Message-----
From: Cathal Mullaney [mailto:[email protected]]
Subject: [PATCH v2] staging: unisys: visorbus: visorchannel: Refactor locking code to be statically deterministic.

This patch makes locking in visorchannel_signalempty statically deterministic.
As a result this patch fixes the sparse warning:
Context imbalance in 'visorchannel_signalempty' - different lock contexts for basic block.

Quoted 6 lines

The logic of the locking code doesn't change but the layout of the original code is "frowned upon"
according to mails on sparse context checking. Refactoring removes the warning and makes the code more readable.

Quoted 6 lines

---
V2: Removed unnecessary variable initialization, as suggested by Tim Sell <[email protected]>.

 drivers/staging/unisys/visorbus/visorchannel.c | 30 ++++++++++++++++----

------

#5 lore raw
Quoted 26 lines

-----Original Message-----
From: Kershner, David A
Sent: Wednesday, October 19, 2016 8:04 PM
To: Cathal Mullaney <[email protected]>

Subject: RE: [PATCH v2] staging: unisys: visorbus: visorchannel: Refactor locking code to be statically deterministic.

Quoted 11 lines

-----Original Message-----
From: Cathal Mullaney [mailto:[email protected]]
Subject: [PATCH v2] staging: unisys: visorbus: visorchannel: Refactor locking code to be statically deterministic.

This patch makes locking in visorchannel_signalempty statically deterministic.
As a result this patch fixes the sparse warning:
Context imbalance in 'visorchannel_signalempty' - different lock contexts for basic block.

Quoted 6 lines

The logic of the locking code doesn't change but the layout of the original code is "frowned upon"
according to mails on sparse context checking. Refactoring removes the warning and makes the code more readable.

Quoted 9 lines
Quoted 5 lines

---
V2: Removed unnecessary variable initialization, as suggested by Tim Sell <[email protected]>.

 drivers/staging/unisys/visorbus/visorchannel.c | 30 ++++++++++++++++--

--

Quoted 1 line

------

Thanks for the quick turnaround, folks.
v2 looks great to me.

- Tim Sell