Skip to contentSkip to conversation

drivers: staging: wilc1000: Move spin lock to the start of critical section

PATCH In review Chandra S Gorentla 4 participants 11 messages Oct 3, 2015 – Oct 5, 2015 Staging

lore
Share & tools
lore
#0 lore raw

PATCH 1/2 drivers: staging: wilc1000: Move spin lock to the start of critical section

The spin_lock_irqsave is moved to just beginning of critical section. This change moves a couple of return statements out of the lock.

---
 drivers/staging/wilc1000/wilc_msgqueue.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/staging/wilc1000/wilc_msgqueue.c +2 -2
index d5ebd6d..284a3f5 100644
@@ -72,8 +72,6 @@ int wilc_mq_send(WILC_MsgQueueHandle *pHandle,
 		goto ERRORHANDLER;
 	}
 
-	spin_lock_irqsave(&pHandle->strCriticalSection, flags);
-
 	/* construct a new message */
 	pstrMessage = kmalloc(sizeof(Message), GFP_ATOMIC);
 	if (!pstrMessage)
@@ -87,6 +85,8 @@ int wilc_mq_send(WILC_MsgQueueHandle *pHandle,
 	}
 	memcpy(pstrMessage->pvBuffer, pvSendBuffer, u32SendBufferSize);
 
+	spin_lock_irqsave(&pHandle->strCriticalSection, flags);
+
 	/* add it to the message queue */
 	if (!pHandle->pstrMessageList) {
 		pHandle->pstrMessageList  = pstrMessage;
Signature · 1 line
2.1.4
#1 lore raw

PATCH 2/2 drivers: staging: wilc1000: Call kfree only for error cases

- kfree is being called for the members of the queue without de-queuing them; they are just inserted within this function; they are supposed to be de-queued and freed in a function for receiving the queue items
- goto statements are removed
- After kfree correction, there is no need for target block of goto statement; hence it is removed

---
 drivers/staging/wilc1000/wilc_msgqueue.c | 22 ++++++----------------
 1 file changed, 6 insertions(+), 16 deletions(-)
drivers/staging/wilc1000/wilc_msgqueue.c +6 -16
index 284a3f5..eae90be 100644
@@ -56,32 +56,30 @@ int wilc_mq_destroy(WILC_MsgQueueHandle *pHandle)
 int wilc_mq_send(WILC_MsgQueueHandle *pHandle,
 			     const void *pvSendBuffer, u32 u32SendBufferSize)
 {
-	int result = 0;
 	unsigned long flags;
 	Message *pstrMessage = NULL;
 
 	if ((!pHandle) || (u32SendBufferSize == 0) || (!pvSendBuffer)) {
 		PRINT_ER("pHandle or pvSendBuffer is null\n");
-		result = -EFAULT;
-		goto ERRORHANDLER;
+		return -EFAULT;
 	}
 
 	if (pHandle->bExiting) {
 		PRINT_ER("pHandle fail\n");
-		result = -EFAULT;
-		goto ERRORHANDLER;
+		return -EFAULT;
 	}
 
 	/* construct a new message */
 	pstrMessage = kmalloc(sizeof(Message), GFP_ATOMIC);
 	if (!pstrMessage)
 		return -ENOMEM;
+
 	pstrMessage->u32Length = u32SendBufferSize;
 	pstrMessage->pstrNext = NULL;
 	pstrMessage->pvBuffer = kmalloc(u32SendBufferSize, GFP_ATOMIC);
 	if (!pstrMessage->pvBuffer) {
-		result = -ENOMEM;
-		goto ERRORHANDLER;
+		kfree(pstrMessage);
+		return -ENOMEM;
 	}
 	memcpy(pstrMessage->pvBuffer, pvSendBuffer, u32SendBufferSize);
 
@@ -102,15 +100,7 @@ int wilc_mq_send(WILC_MsgQueueHandle *pHandle,
 	spin_unlock_irqrestore(&pHandle->strCriticalSection, flags);
 
 	up(&pHandle->hSem);
-
-ERRORHANDLER:
-	/* error occured, free any allocations */
-	if (pstrMessage) {
-		kfree(pstrMessage->pvBuffer);
-		kfree(pstrMessage);
-	}
-
-	return result;
+	return 0;
 }
 
 /*!
Signature · 1 line
2.1.4
maintainer
#7 lore raw
In reply to Chandra S Gorentla
Quoted 13 lines · Chandra S Gorentla

The spin_lock_irqsave is moved to just beginning of critical section. This change moves a couple of return statements out of the lock.

---
 drivers/staging/wilc1000/wilc_msgqueue.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/staging/wilc1000/wilc_msgqueue.c +0 -0
index d5ebd6d..284a3f5 100644

drivers/staging/wilc1000/wilc_msgqueue.c int wilc_mq_send(WILC_MsgQueueHandle *pHandle, · line 76

Quoted 7 lines
drivers/staging/wilc1000/wilc_msgqueue.c +0 -2
@@ -72,8 +72,6 @@ int wilc_mq_send(WILC_MsgQueueHandle *pHandle,
 		goto ERRORHANDLER;
 	}
 
-	spin_lock_irqsave(&pHandle->strCriticalSection, flags);
-
 	/* construct a new message */
 	pstrMessage = kmalloc(sizeof(Message), GFP_ATOMIC);

As you have moved the lock, can you also change this to GFP_KERNEL as well because we do not have a lock held?

And how have you tested that this is ok? What is this lock trying to protect?

thanks,

greg k-h

maintainer
#2 lore raw

Re: [PATCH 2/2] drivers: staging: wilc1000: Call kfree only for error cases

In reply to Chandra S Gorentla
Quoted 59 lines · Chandra S Gorentla

- kfree is being called for the members of the queue without de-queuing them; they are just inserted within this function; they are supposed to be de-queued and freed in a function for receiving the queue items
- goto statements are removed
- After kfree correction, there is no need for target block of goto statement; hence it is removed

---
 drivers/staging/wilc1000/wilc_msgqueue.c | 22 ++++++----------------
 1 file changed, 6 insertions(+), 16 deletions(-)
drivers/staging/wilc1000/wilc_msgqueue.c +5 -7
index 284a3f5..eae90be 100644
@@ -56,32 +56,30 @@ int wilc_mq_destroy(WILC_MsgQueueHandle *pHandle)
 int wilc_mq_send(WILC_MsgQueueHandle *pHandle,
 			     const void *pvSendBuffer, u32 u32SendBufferSize)
 {
-	int result = 0;
 	unsigned long flags;
 	Message *pstrMessage = NULL;
 
 	if ((!pHandle) || (u32SendBufferSize == 0) || (!pvSendBuffer)) {
 		PRINT_ER("pHandle or pvSendBuffer is null\n");
-		result = -EFAULT;
-		goto ERRORHANDLER;
+		return -EFAULT;
 	}
 
 	if (pHandle->bExiting) {
 		PRINT_ER("pHandle fail\n");
-		result = -EFAULT;
-		goto ERRORHANDLER;
+		return -EFAULT;
 	}
 
 	/* construct a new message */
 	pstrMessage = kmalloc(sizeof(Message), GFP_ATOMIC);
 	if (!pstrMessage)
 		return -ENOMEM;
+
 	pstrMessage->u32Length = u32SendBufferSize;
 	pstrMessage->pstrNext = NULL;
 	pstrMessage->pvBuffer = kmalloc(u32SendBufferSize, GFP_ATOMIC);
 	if (!pstrMessage->pvBuffer) {
-		result = -ENOMEM;
-		goto ERRORHANDLER;
+		kfree(pstrMessage);
+		return -ENOMEM;
 	}
 	memcpy(pstrMessage->pvBuffer, pvSendBuffer, u32SendBufferSize);
 
@@ -102,15 +100,7 @@ int wilc_mq_send(WILC_MsgQueueHandle *pHandle,
 	spin_unlock_irqrestore(&pHandle->strCriticalSection, flags);
 
 	up(&pHandle->hSem);

drivers/staging/wilc1000/wilc_msgqueue.c int wilc_mq_send(WILC_MsgQueueHandle *pHandle, · line 103

Quoted 10 lines
drivers/staging/wilc1000/wilc_msgqueue.c +1 -9
-
-ERRORHANDLER:
-	/* error occured, free any allocations */
-	if (pstrMessage) {
-		kfree(pstrMessage->pvBuffer);
-		kfree(pstrMessage);
-	}
-
-	return result;
+	return 0;

Aren't you now leaking memory as you aren't freeing pstrMessage and the buffer on the "normal" return path?

thanks,

greg k-h

#3 lore raw

Re: [PATCH 2/2] drivers: staging: wilc1000: Call kfree only for error cases

Quoted 2 lines · Greg KH

Aren't you now leaking memory as you aren't freeing pstrMessage and the buffer on the "normal" return path?

It's supposed to. It's a bug fix. I explained to him in the first version that his changelog sucks.

regards,
dan carpenter

#8 lore raw
In reply to Greg KH
Quoted 17 lines · Greg KH
Quoted 16 lines · Chandra S Gorentla

The spin_lock_irqsave is moved to just beginning of critical section. This change moves a couple of return statements out of the lock.

---
 drivers/staging/wilc1000/wilc_msgqueue.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/staging/wilc1000/wilc_msgqueue.c +0 -0
index d5ebd6d..284a3f5 100644
@@ -72,8 +72,6 @@ int wilc_mq_send(WILC_MsgQueueHandle *pHandle,
 		goto ERRORHANDLER;
 	}
 
Quoted 7 lines

drivers/staging/wilc1000/wilc_msgqueue.c int wilc_mq_send(WILC_MsgQueueHandle *pHandle, · line 76

Quoted 4 lines
drivers/staging/wilc1000/wilc_msgqueue.c +0 -2
-	spin_lock_irqsave(&pHandle->strCriticalSection, flags);
-
 	/* construct a new message */
 	pstrMessage = kmalloc(sizeof(Message), GFP_ATOMIC);

As you have moved the lock, can you also change this to GFP_KERNEL as well because we do not have a lock held?

Can 'the change to GFP_KERNEL' be done in a separate patch? The lock is to protect linked list manipulations; in this function items are added to the list.

Quoted 3 lines

And how have you tested that this is ok? What is this lock trying to protect?

I load this module on a notebook computer. I added some code to
wilc_debugfs.c to invoke the functions in the file wilc_msgqueue.c

Quoted 4 lines

thanks,

greg k-h

maintainer
#4 lore raw

Re: [PATCH 2/2] drivers: staging: wilc1000: Call kfree only for error cases

In reply to Dan Carpenter
Quoted 6 lines · Dan Carpenter
Quoted 2 lines · Greg KH

Aren't you now leaking memory as you aren't freeing pstrMessage and the buffer on the "normal" return path?

It's supposed to. It's a bug fix. I explained to him in the first version that his changelog sucks.

Ok, well, it still sucks :)

And it's out of my queue, so I'll wait for the next spin of this series...

greg k-h

maintainer
#9 lore raw
In reply to Chandra Gorentla
Quoted 18 lines · Chandra Gorentla
Quoted 17 lines · Greg KH
Quoted 16 lines · Chandra S Gorentla

The spin_lock_irqsave is moved to just beginning of critical section. This change moves a couple of return statements out of the lock.

---
 drivers/staging/wilc1000/wilc_msgqueue.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/staging/wilc1000/wilc_msgqueue.c +0 -0
index d5ebd6d..284a3f5 100644
@@ -72,8 +72,6 @@ int wilc_mq_send(WILC_MsgQueueHandle *pHandle,
 		goto ERRORHANDLER;
 	}
 
Quoted 8 lines
Quoted 7 lines

drivers/staging/wilc1000/wilc_msgqueue.c int wilc_mq_send(WILC_MsgQueueHandle *pHandle, · line 76

Quoted 4 lines
drivers/staging/wilc1000/wilc_msgqueue.c +0 -2
-	spin_lock_irqsave(&pHandle->strCriticalSection, flags);
-
 	/* construct a new message */
 	pstrMessage = kmalloc(sizeof(Message), GFP_ATOMIC);

As you have moved the lock, can you also change this to GFP_KERNEL as well because we do not have a lock held?

Can 'the change to GFP_KERNEL' be done in a separate patch?

Yes.

Quoted 2 lines

The lock is to protect linked list manipulations; in this function items are added to the list.

Ok, please add that description to the patch so we know what is going on, and that you know what is going on as well :)

thanks,

greg k-h

#5 lore raw

Re: [PATCH 2/2] drivers: staging: wilc1000: Call kfree only for error cases

In reply to Greg KH
Quoted 59 lines · Greg KH
Quoted 58 lines · Chandra S Gorentla

- kfree is being called for the members of the queue without de-queuing them; they are just inserted within this function; they are supposed to be de-queued and freed in a function for receiving the queue items
- goto statements are removed
- After kfree correction, there is no need for target block of goto statement; hence it is removed

---
 drivers/staging/wilc1000/wilc_msgqueue.c | 22 ++++++----------------
 1 file changed, 6 insertions(+), 16 deletions(-)
drivers/staging/wilc1000/wilc_msgqueue.c +5 -7
index 284a3f5..eae90be 100644
@@ -56,32 +56,30 @@ int wilc_mq_destroy(WILC_MsgQueueHandle *pHandle)
 int wilc_mq_send(WILC_MsgQueueHandle *pHandle,
 			     const void *pvSendBuffer, u32 u32SendBufferSize)
 {
-	int result = 0;
 	unsigned long flags;
 	Message *pstrMessage = NULL;
 
 	if ((!pHandle) || (u32SendBufferSize == 0) || (!pvSendBuffer)) {
 		PRINT_ER("pHandle or pvSendBuffer is null\n");
-		result = -EFAULT;
-		goto ERRORHANDLER;
+		return -EFAULT;
 	}
 
 	if (pHandle->bExiting) {
 		PRINT_ER("pHandle fail\n");
-		result = -EFAULT;
-		goto ERRORHANDLER;
+		return -EFAULT;
 	}
 
 	/* construct a new message */
 	pstrMessage = kmalloc(sizeof(Message), GFP_ATOMIC);
 	if (!pstrMessage)
 		return -ENOMEM;
+
 	pstrMessage->u32Length = u32SendBufferSize;
 	pstrMessage->pstrNext = NULL;
 	pstrMessage->pvBuffer = kmalloc(u32SendBufferSize, GFP_ATOMIC);
 	if (!pstrMessage->pvBuffer) {
-		result = -ENOMEM;
-		goto ERRORHANDLER;
+		kfree(pstrMessage);
+		return -ENOMEM;
 	}
 	memcpy(pstrMessage->pvBuffer, pvSendBuffer, u32SendBufferSize);
 
@@ -102,15 +100,7 @@ int wilc_mq_send(WILC_MsgQueueHandle *pHandle,
 	spin_unlock_irqrestore(&pHandle->strCriticalSection, flags);
 
Quoted 14 lines

drivers/staging/wilc1000/wilc_msgqueue.c int wilc_mq_send(WILC_MsgQueueHandle *pHandle, · line 103

Quoted 11 lines
drivers/staging/wilc1000/wilc_msgqueue.c +1 -9
 	up(&pHandle->hSem);
-
-ERRORHANDLER:
-	/* error occured, free any allocations */
-	if (pstrMessage) {
-		kfree(pstrMessage->pvBuffer);
-		kfree(pstrMessage);
-	}
-
-	return result;
+	return 0;

Aren't you now leaking memory as you aren't freeing pstrMessage and the buffer on the "normal" return path?

In the normal path kfree is called in a separate (wilc_mq_recv) function. The purpose of the currently modified function (wilc_mq_send) is to post a message to a queue by allocating memory for the message. The receiver function is supposed to remove the message from the queue and free the memory.

Quoted 4 lines

thanks,

greg k-h

#6 lore raw

Re: [PATCH 2/2] drivers: staging: wilc1000: Call kfree only for error cases

Quoted 73 lines · Chandra Gorentla
Quoted 59 lines · Greg KH
Quoted 58 lines · Chandra S Gorentla

- kfree is being called for the members of the queue without de-queuing them; they are just inserted within this function; they are supposed to be de-queued and freed in a function for receiving the queue items
- goto statements are removed
- After kfree correction, there is no need for target block of goto statement; hence it is removed

---
  drivers/staging/wilc1000/wilc_msgqueue.c | 22 ++++++----------------
  1 file changed, 6 insertions(+), 16 deletions(-)
drivers/staging/wilc1000/wilc_msgqueue.c +5 -7
index 284a3f5..eae90be 100644
@@ -56,32 +56,30 @@ int wilc_mq_destroy(WILC_MsgQueueHandle *pHandle)
  int wilc_mq_send(WILC_MsgQueueHandle *pHandle,
  			     const void *pvSendBuffer, u32 u32SendBufferSize)
  {
-	int result = 0;
  	unsigned long flags;
  	Message *pstrMessage = NULL;
 
  	if ((!pHandle) || (u32SendBufferSize == 0) || (!pvSendBuffer)) {
  		PRINT_ER("pHandle or pvSendBuffer is null\n");
-		result = -EFAULT;
-		goto ERRORHANDLER;
+		return -EFAULT;
  	}
 
  	if (pHandle->bExiting) {
  		PRINT_ER("pHandle fail\n");
-		result = -EFAULT;
-		goto ERRORHANDLER;
+		return -EFAULT;
  	}
 
  	/* construct a new message */
  	pstrMessage = kmalloc(sizeof(Message), GFP_ATOMIC);
  	if (!pstrMessage)
  		return -ENOMEM;
+
  	pstrMessage->u32Length = u32SendBufferSize;
  	pstrMessage->pstrNext = NULL;
  	pstrMessage->pvBuffer = kmalloc(u32SendBufferSize, GFP_ATOMIC);
  	if (!pstrMessage->pvBuffer) {
-		result = -ENOMEM;
-		goto ERRORHANDLER;
+		kfree(pstrMessage);
+		return -ENOMEM;
  	}
  	memcpy(pstrMessage->pvBuffer, pvSendBuffer, u32SendBufferSize);
 
@@ -102,15 +100,7 @@ int wilc_mq_send(WILC_MsgQueueHandle *pHandle,
  	spin_unlock_irqrestore(&pHandle->strCriticalSection, flags);
 
Quoted 13 lines

drivers/staging/wilc1000/wilc_msgqueue.c int wilc_mq_send(WILC_MsgQueueHandle *pHandle, · line 103

Quoted 11 lines
drivers/staging/wilc1000/wilc_msgqueue.c +1 -9
  	up(&pHandle->hSem);
-
-ERRORHANDLER:
-	/* error occured, free any allocations */
-	if (pstrMessage) {
-		kfree(pstrMessage->pvBuffer);
-		kfree(pstrMessage);
-	}
-
-	return result;
+	return 0;

Aren't you now leaking memory as you aren't freeing pstrMessage and the buffer on the "normal" return path?

Quoted 6 lines

In the normal path kfree is called in a separate (wilc_mq_recv) function. The purpose of the currently modified function (wilc_mq_send) is to post a message to a queue by allocating memory for the message. The receiver function is supposed to remove the message from the queue and free the memory.

This patch is reasonable and normal free is done in recv function as Chandra said.

Thanks,

Tony.

Quoted 8 lines
Quoted 3 lines

thanks,

greg k-h

--
To unsubscribe from this list: send the line "unsubscribe linux-wireless" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

#10 lore raw
In reply to Greg KH
Quoted 24 lines · Greg KH
Quoted 13 lines · Chandra S Gorentla

The spin_lock_irqsave is moved to just beginning of critical section. This change moves a couple of return statements out of the lock.

---
  drivers/staging/wilc1000/wilc_msgqueue.c | 4 ++--
  1 file changed, 2 insertions(+), 2 deletions(-)
drivers/staging/wilc1000/wilc_msgqueue.c +0 -0
index d5ebd6d..284a3f5 100644

drivers/staging/wilc1000/wilc_msgqueue.c int wilc_mq_send(WILC_MsgQueueHandle *pHandle, · line 76

Quoted 7 lines
drivers/staging/wilc1000/wilc_msgqueue.c +0 -2
@@ -72,8 +72,6 @@ int wilc_mq_send(WILC_MsgQueueHandle *pHandle,
  		goto ERRORHANDLER;
  	}
 
-	spin_lock_irqsave(&pHandle->strCriticalSection, flags);
-
  	/* construct a new message */
  	pstrMessage = kmalloc(sizeof(Message), GFP_ATOMIC);

As you have moved the lock, can you also change this to GFP_KERNEL as well because we do not have a lock held?

Quoted 2 lines

And how have you tested that this is ok? What is this lock trying to protect?

This function is called even in interrupt context, so GFP_ATOMIC should be called. The spinlock also should protect pstrMessage from allocating the memory, so we don't place it to the beginning of critical section as Chandra said.

Thanks,
Tony.

Quoted 7 lines

thanks,

greg k-h
--
To unsubscribe from this list: send the line "unsubscribe linux-wireless" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html