linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH 1/7 linux-next] swim: replace current->state by set_current_state()
@ 2015-02-20 18:12 Fabian Frederick
  2015-02-20 18:12 ` [PATCH 2/7 linux-next] mISDN: " Fabian Frederick
                   ` (5 more replies)
  0 siblings, 6 replies; 19+ messages in thread
From: Fabian Frederick @ 2015-02-20 18:12 UTC (permalink / raw)
  To: linux-kernel
  Cc: Ingo Molnar, Peter Zijlstra, Greg Kroah-Hartman, Fabian Frederick

Use helper function to access current->state.
Direct assignments are prone to races and therefore buggy.

Thanks to Peter Zijlstra for the exact definition of the problem.

Signed-off-by: Fabian Frederick <fabf@skynet.be>
---
 drivers/block/swim.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/drivers/block/swim.c b/drivers/block/swim.c
index b5afd49..e9bb759 100644
--- a/drivers/block/swim.c
+++ b/drivers/block/swim.c
@@ -331,7 +331,7 @@ static inline void swim_motor(struct swim __iomem *base,
 			swim_select(base, RELAX);
 			if (swim_readbit(base, MOTOR_ON))
 				break;
-			current->state = TASK_INTERRUPTIBLE;
+			set_current_state(TASK_INTERRUPTIBLE);
 			schedule_timeout(1);
 		}
 	} else if (action == OFF) {
@@ -350,7 +350,7 @@ static inline void swim_eject(struct swim __iomem *base)
 		swim_select(base, RELAX);
 		if (!swim_readbit(base, DISK_IN))
 			break;
-		current->state = TASK_INTERRUPTIBLE;
+		set_current_state(TASK_INTERRUPTIBLE);
 		schedule_timeout(1);
 	}
 	swim_select(base, RELAX);
@@ -374,7 +374,7 @@ static inline int swim_step(struct swim __iomem *base)
 
 	for (wait = 0; wait < HZ; wait++) {
 
-		current->state = TASK_INTERRUPTIBLE;
+		set_current_state(TASK_INTERRUPTIBLE);
 		schedule_timeout(1);
 
 		swim_select(base, RELAX);
-- 
2.1.0


^ permalink raw reply related	[flat|nested] 19+ messages in thread

* [PATCH 2/7 linux-next] mISDN: replace current->state by set_current_state()
  2015-02-20 18:12 [PATCH 1/7 linux-next] swim: replace current->state by set_current_state() Fabian Frederick
@ 2015-02-20 18:12 ` Fabian Frederick
  2015-02-22 20:24   ` David Miller
  2015-02-20 18:12 ` [PATCH 3/7 linux-next] powerpc/pmac: " Fabian Frederick
                   ` (4 subsequent siblings)
  5 siblings, 1 reply; 19+ messages in thread
From: Fabian Frederick @ 2015-02-20 18:12 UTC (permalink / raw)
  To: linux-kernel
  Cc: Ingo Molnar, Peter Zijlstra, Greg Kroah-Hartman,
	Fabian Frederick, Karsten Keil, netdev

Use helper function to access current->state.
Direct assignments are prone to races and therefore buggy.

Thanks to Peter Zijlstra for the exact definition of the problem.

Signed-off-by: Fabian Frederick <fabf@skynet.be>
---
 drivers/isdn/hardware/mISDN/hfcpci.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/isdn/hardware/mISDN/hfcpci.c b/drivers/isdn/hardware/mISDN/hfcpci.c
index 3c92780..ff48da6 100644
--- a/drivers/isdn/hardware/mISDN/hfcpci.c
+++ b/drivers/isdn/hardware/mISDN/hfcpci.c
@@ -1755,7 +1755,7 @@ init_card(struct hfc_pci *hc)
 		enable_hwirq(hc);
 		spin_unlock_irqrestore(&hc->lock, flags);
 		/* Timeout 80ms */
-		current->state = TASK_UNINTERRUPTIBLE;
+		set_current_state(TASK_UNINTERRUPTIBLE);
 		schedule_timeout((80 * HZ) / 1000);
 		printk(KERN_INFO "HFC PCI: IRQ %d count %d\n",
 		       hc->irq, hc->irqcnt);
-- 
2.1.0


^ permalink raw reply related	[flat|nested] 19+ messages in thread

* [PATCH 3/7 linux-next] powerpc/pmac: replace current->state by set_current_state()
  2015-02-20 18:12 [PATCH 1/7 linux-next] swim: replace current->state by set_current_state() Fabian Frederick
  2015-02-20 18:12 ` [PATCH 2/7 linux-next] mISDN: " Fabian Frederick
@ 2015-02-20 18:12 ` Fabian Frederick
  2015-02-20 18:12 ` [PATCH 4/7 linux-next] saa7146: " Fabian Frederick
                   ` (3 subsequent siblings)
  5 siblings, 0 replies; 19+ messages in thread
From: Fabian Frederick @ 2015-02-20 18:12 UTC (permalink / raw)
  To: linux-kernel
  Cc: Ingo Molnar, Peter Zijlstra, Greg Kroah-Hartman,
	Fabian Frederick, Benjamin Herrenschmidt, linuxppc-dev

Use helper functions to access current->state.
Direct assignments are prone to races and therefore buggy.

current->state = TASK_RUNNING can be replaced by __set_current_state()

Thanks to Peter Zijlstra for the exact definition of the problem.

Suggested-By: Peter Zijlstra <peterz@infradead.org>
Signed-off-by: Fabian Frederick <fabf@skynet.be>
---
 drivers/macintosh/via-pmu.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/drivers/macintosh/via-pmu.c b/drivers/macintosh/via-pmu.c
index dee88e5..3c608d4 100644
--- a/drivers/macintosh/via-pmu.c
+++ b/drivers/macintosh/via-pmu.c
@@ -2109,7 +2109,7 @@ pmu_read(struct file *file, char __user *buf,
 
 	spin_lock_irqsave(&pp->lock, flags);
 	add_wait_queue(&pp->wait, &wait);
-	current->state = TASK_INTERRUPTIBLE;
+	set_current_state(TASK_INTERRUPTIBLE);
 
 	for (;;) {
 		ret = -EAGAIN;
@@ -2138,7 +2138,7 @@ pmu_read(struct file *file, char __user *buf,
 		schedule();
 		spin_lock_irqsave(&pp->lock, flags);
 	}
-	current->state = TASK_RUNNING;
+	__set_current_state(TASK_RUNNING);
 	remove_wait_queue(&pp->wait, &wait);
 	spin_unlock_irqrestore(&pp->lock, flags);
 	
-- 
2.1.0


^ permalink raw reply related	[flat|nested] 19+ messages in thread

* [PATCH 4/7 linux-next] saa7146: replace current->state by set_current_state()
  2015-02-20 18:12 [PATCH 1/7 linux-next] swim: replace current->state by set_current_state() Fabian Frederick
  2015-02-20 18:12 ` [PATCH 2/7 linux-next] mISDN: " Fabian Frederick
  2015-02-20 18:12 ` [PATCH 3/7 linux-next] powerpc/pmac: " Fabian Frederick
@ 2015-02-20 18:12 ` Fabian Frederick
  2015-02-20 18:12 ` [PATCH 5/7 linux-next] hso: replace current->state by __set_current_state() Fabian Frederick
                   ` (2 subsequent siblings)
  5 siblings, 0 replies; 19+ messages in thread
From: Fabian Frederick @ 2015-02-20 18:12 UTC (permalink / raw)
  To: linux-kernel
  Cc: Ingo Molnar, Peter Zijlstra, Greg Kroah-Hartman,
	Fabian Frederick, Hans Verkuil, Mauro Carvalho Chehab,
	linux-media

Use helper functions to access current->state.
Direct assignments are prone to races and therefore buggy.

current->state = TASK_RUNNING can be replaced by __set_current_state()

Thanks to Peter Zijlstra for the exact definition of the problem.

Suggested-By: Peter Zijlstra <peterz@infradead.org>
Signed-off-by: Fabian Frederick <fabf@skynet.be>
---
 drivers/media/common/saa7146/saa7146_vbi.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/drivers/media/common/saa7146/saa7146_vbi.c b/drivers/media/common/saa7146/saa7146_vbi.c
index 1e71e37..2da9957 100644
--- a/drivers/media/common/saa7146/saa7146_vbi.c
+++ b/drivers/media/common/saa7146/saa7146_vbi.c
@@ -95,7 +95,7 @@ static int vbi_workaround(struct saa7146_dev *dev)
 
 		/* prepare to wait to be woken up by the irq-handler */
 		add_wait_queue(&vv->vbi_wq, &wait);
-		current->state = TASK_INTERRUPTIBLE;
+		set_current_state(TASK_INTERRUPTIBLE);
 
 		/* start rps1 to enable workaround */
 		saa7146_write(dev, RPS_ADDR1, dev->d_rps1.dma_handle);
@@ -106,7 +106,7 @@ static int vbi_workaround(struct saa7146_dev *dev)
 		DEB_VBI("brs bug workaround %d/1\n", i);
 
 		remove_wait_queue(&vv->vbi_wq, &wait);
-		current->state = TASK_RUNNING;
+		__set_current_state(TASK_RUNNING);
 
 		/* disable rps1 irqs */
 		SAA7146_IER_DISABLE(dev,MASK_28);
-- 
2.1.0


^ permalink raw reply related	[flat|nested] 19+ messages in thread

* [PATCH 5/7 linux-next] hso: replace current->state by __set_current_state()
  2015-02-20 18:12 [PATCH 1/7 linux-next] swim: replace current->state by set_current_state() Fabian Frederick
                   ` (2 preceding siblings ...)
  2015-02-20 18:12 ` [PATCH 4/7 linux-next] saa7146: " Fabian Frederick
@ 2015-02-20 18:12 ` Fabian Frederick
  2015-02-22 20:25   ` David Miller
  2015-02-20 18:12 ` [PATCH 6/7 linux-next] wan: cosa: replace current->state by set_current_state() Fabian Frederick
  2015-02-20 18:12 ` [PATCH 7/7 linux-next] serial: core: replace current->state by __set_current_state() Fabian Frederick
  5 siblings, 1 reply; 19+ messages in thread
From: Fabian Frederick @ 2015-02-20 18:12 UTC (permalink / raw)
  To: linux-kernel
  Cc: Ingo Molnar, Peter Zijlstra, Greg Kroah-Hartman,
	Fabian Frederick, Jan Dumon, linux-usb, netdev

Use helper functions to access current->state.
Direct assignments are prone to races and therefore buggy.

Thanks to Peter Zijlstra for the exact definition of the problem.

Suggested-By: Peter Zijlstra <peterz@infradead.org>
Signed-off-by: Fabian Frederick <fabf@skynet.be>
---
 drivers/net/usb/hso.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/net/usb/hso.c b/drivers/net/usb/hso.c
index 9cdfb3f..778e915 100644
--- a/drivers/net/usb/hso.c
+++ b/drivers/net/usb/hso.c
@@ -1594,7 +1594,7 @@ hso_wait_modem_status(struct hso_serial *serial, unsigned long arg)
 		}
 		cprev = cnow;
 	}
-	current->state = TASK_RUNNING;
+	__set_current_state(TASK_RUNNING);
 	remove_wait_queue(&tiocmget->waitq, &wait);
 
 	return ret;
-- 
2.1.0


^ permalink raw reply related	[flat|nested] 19+ messages in thread

* [PATCH 6/7 linux-next] wan: cosa: replace current->state by set_current_state()
  2015-02-20 18:12 [PATCH 1/7 linux-next] swim: replace current->state by set_current_state() Fabian Frederick
                   ` (3 preceding siblings ...)
  2015-02-20 18:12 ` [PATCH 5/7 linux-next] hso: replace current->state by __set_current_state() Fabian Frederick
@ 2015-02-20 18:12 ` Fabian Frederick
  2015-02-20 18:26   ` Jan Yenya Kasprzak
                     ` (2 more replies)
  2015-02-20 18:12 ` [PATCH 7/7 linux-next] serial: core: replace current->state by __set_current_state() Fabian Frederick
  5 siblings, 3 replies; 19+ messages in thread
From: Fabian Frederick @ 2015-02-20 18:12 UTC (permalink / raw)
  To: linux-kernel
  Cc: Ingo Molnar, Peter Zijlstra, Greg Kroah-Hartman,
	Fabian Frederick, Jan "Yenya" Kasprzak, netdev

Use helper functions to access current->state.
Direct assignments are prone to races and therefore buggy.

current->state = TASK_RUNNING is replaced by __set_current_state()

Thanks to Peter Zijlstra for the exact definition of the problem.

Suggested-By: Peter Zijlstra <peterz@infradead.org>
Signed-off-by: Fabian Frederick <fabf@skynet.be>
---
 drivers/net/wan/cosa.c | 12 ++++++------
 1 file changed, 6 insertions(+), 6 deletions(-)

diff --git a/drivers/net/wan/cosa.c b/drivers/net/wan/cosa.c
index 83c39e2..88d121d 100644
--- a/drivers/net/wan/cosa.c
+++ b/drivers/net/wan/cosa.c
@@ -806,21 +806,21 @@ static ssize_t cosa_read(struct file *file,
 	spin_lock_irqsave(&cosa->lock, flags);
 	add_wait_queue(&chan->rxwaitq, &wait);
 	while (!chan->rx_status) {
-		current->state = TASK_INTERRUPTIBLE;
+		set_current_state(TASK_INTERRUPTIBLE);
 		spin_unlock_irqrestore(&cosa->lock, flags);
 		schedule();
 		spin_lock_irqsave(&cosa->lock, flags);
 		if (signal_pending(current) && chan->rx_status == 0) {
 			chan->rx_status = 1;
 			remove_wait_queue(&chan->rxwaitq, &wait);
-			current->state = TASK_RUNNING;
+			__set_current_state(TASK_RUNNING);
 			spin_unlock_irqrestore(&cosa->lock, flags);
 			mutex_unlock(&chan->rlock);
 			return -ERESTARTSYS;
 		}
 	}
 	remove_wait_queue(&chan->rxwaitq, &wait);
-	current->state = TASK_RUNNING;
+	__set_current_state(TASK_RUNNING);
 	kbuf = chan->rxdata;
 	count = chan->rxsize;
 	spin_unlock_irqrestore(&cosa->lock, flags);
@@ -890,14 +890,14 @@ static ssize_t cosa_write(struct file *file,
 	spin_lock_irqsave(&cosa->lock, flags);
 	add_wait_queue(&chan->txwaitq, &wait);
 	while (!chan->tx_status) {
-		current->state = TASK_INTERRUPTIBLE;
+		set_current_state(TASK_INTERRUPTIBLE);
 		spin_unlock_irqrestore(&cosa->lock, flags);
 		schedule();
 		spin_lock_irqsave(&cosa->lock, flags);
 		if (signal_pending(current) && chan->tx_status == 0) {
 			chan->tx_status = 1;
 			remove_wait_queue(&chan->txwaitq, &wait);
-			current->state = TASK_RUNNING;
+			__set_current_state(TASK_RUNNING);
 			chan->tx_status = 1;
 			spin_unlock_irqrestore(&cosa->lock, flags);
 			up(&chan->wsem);
@@ -905,7 +905,7 @@ static ssize_t cosa_write(struct file *file,
 		}
 	}
 	remove_wait_queue(&chan->txwaitq, &wait);
-	current->state = TASK_RUNNING;
+	__set_current_state(TASK_RUNNING);
 	up(&chan->wsem);
 	spin_unlock_irqrestore(&cosa->lock, flags);
 	kfree(kbuf);
-- 
2.1.0


^ permalink raw reply related	[flat|nested] 19+ messages in thread

* [PATCH 7/7 linux-next] serial: core: replace current->state by __set_current_state()
  2015-02-20 18:12 [PATCH 1/7 linux-next] swim: replace current->state by set_current_state() Fabian Frederick
                   ` (4 preceding siblings ...)
  2015-02-20 18:12 ` [PATCH 6/7 linux-next] wan: cosa: replace current->state by set_current_state() Fabian Frederick
@ 2015-02-20 18:12 ` Fabian Frederick
  5 siblings, 0 replies; 19+ messages in thread
From: Fabian Frederick @ 2015-02-20 18:12 UTC (permalink / raw)
  To: linux-kernel
  Cc: Ingo Molnar, Peter Zijlstra, Greg Kroah-Hartman,
	Fabian Frederick, Jiri Slaby, linux-serial

Use helper functions to access current->state.
Direct assignments are prone to races and therefore buggy.

Thanks to Peter Zijlstra for the exact definition of the problem.

Suggested-By: Peter Zijlstra <peterz@infradead.org>
Signed-off-by: Fabian Frederick <fabf@skynet.be>
---
 drivers/tty/serial/serial_core.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)

diff --git a/drivers/tty/serial/serial_core.c b/drivers/tty/serial/serial_core.c
index 6a1055a..63d2947 100644
--- a/drivers/tty/serial/serial_core.c
+++ b/drivers/tty/serial/serial_core.c
@@ -1118,8 +1118,7 @@ uart_wait_modem_status(struct uart_state *state, unsigned long arg)
 
 		cprev = cnow;
 	}
-
-	current->state = TASK_RUNNING;
+	__set_current_state(TASK_RUNNING);
 	remove_wait_queue(&port->delta_msr_wait, &wait);
 
 	return ret;
-- 
2.1.0


^ permalink raw reply related	[flat|nested] 19+ messages in thread

* Re: [PATCH 6/7 linux-next] wan: cosa: replace current->state by set_current_state()
  2015-02-20 18:12 ` [PATCH 6/7 linux-next] wan: cosa: replace current->state by set_current_state() Fabian Frederick
@ 2015-02-20 18:26   ` Jan Yenya Kasprzak
  2015-02-20 18:34   ` Sergei Shtylyov
  2015-02-22 20:25   ` David Miller
  2 siblings, 0 replies; 19+ messages in thread
From: Jan Yenya Kasprzak @ 2015-02-20 18:26 UTC (permalink / raw)
  To: Fabian Frederick
  Cc: linux-kernel, Ingo Molnar, Peter Zijlstra, Greg Kroah-Hartman, netdev

Fabian Frederick wrote:
: Use helper functions to access current->state.
: Direct assignments are prone to races and therefore buggy.
: 
: current->state = TASK_RUNNING is replaced by __set_current_state()
: 
: Thanks to Peter Zijlstra for the exact definition of the problem.

	OK, thanks. Although I wonder whether real users of COSA (an ISA-based
WAN card) do exist.

Acked-By: Jan "Yenya" Kasprzak <kas@fi.muni.cz>

-Y.

: 
: Suggested-By: Peter Zijlstra <peterz@infradead.org>
: Signed-off-by: Fabian Frederick <fabf@skynet.be>
: ---
:  drivers/net/wan/cosa.c | 12 ++++++------
:  1 file changed, 6 insertions(+), 6 deletions(-)
: 
: diff --git a/drivers/net/wan/cosa.c b/drivers/net/wan/cosa.c
: index 83c39e2..88d121d 100644
: --- a/drivers/net/wan/cosa.c
: +++ b/drivers/net/wan/cosa.c
: @@ -806,21 +806,21 @@ static ssize_t cosa_read(struct file *file,
:  	spin_lock_irqsave(&cosa->lock, flags);
:  	add_wait_queue(&chan->rxwaitq, &wait);
:  	while (!chan->rx_status) {
: -		current->state = TASK_INTERRUPTIBLE;
: +		set_current_state(TASK_INTERRUPTIBLE);
:  		spin_unlock_irqrestore(&cosa->lock, flags);
:  		schedule();
:  		spin_lock_irqsave(&cosa->lock, flags);
:  		if (signal_pending(current) && chan->rx_status == 0) {
:  			chan->rx_status = 1;
:  			remove_wait_queue(&chan->rxwaitq, &wait);
: -			current->state = TASK_RUNNING;
: +			__set_current_state(TASK_RUNNING);
:  			spin_unlock_irqrestore(&cosa->lock, flags);
:  			mutex_unlock(&chan->rlock);
:  			return -ERESTARTSYS;
:  		}
:  	}
:  	remove_wait_queue(&chan->rxwaitq, &wait);
: -	current->state = TASK_RUNNING;
: +	__set_current_state(TASK_RUNNING);
:  	kbuf = chan->rxdata;
:  	count = chan->rxsize;
:  	spin_unlock_irqrestore(&cosa->lock, flags);
: @@ -890,14 +890,14 @@ static ssize_t cosa_write(struct file *file,
:  	spin_lock_irqsave(&cosa->lock, flags);
:  	add_wait_queue(&chan->txwaitq, &wait);
:  	while (!chan->tx_status) {
: -		current->state = TASK_INTERRUPTIBLE;
: +		set_current_state(TASK_INTERRUPTIBLE);
:  		spin_unlock_irqrestore(&cosa->lock, flags);
:  		schedule();
:  		spin_lock_irqsave(&cosa->lock, flags);
:  		if (signal_pending(current) && chan->tx_status == 0) {
:  			chan->tx_status = 1;
:  			remove_wait_queue(&chan->txwaitq, &wait);
: -			current->state = TASK_RUNNING;
: +			__set_current_state(TASK_RUNNING);
:  			chan->tx_status = 1;
:  			spin_unlock_irqrestore(&cosa->lock, flags);
:  			up(&chan->wsem);
: @@ -905,7 +905,7 @@ static ssize_t cosa_write(struct file *file,
:  		}
:  	}
:  	remove_wait_queue(&chan->txwaitq, &wait);
: -	current->state = TASK_RUNNING;
: +	__set_current_state(TASK_RUNNING);
:  	up(&chan->wsem);
:  	spin_unlock_irqrestore(&cosa->lock, flags);
:  	kfree(kbuf);
: -- 
: 2.1.0

-- 
| Jan "Yenya" Kasprzak   <kas at {fi.muni.cz - work | yenya.net - private}> |
| New GPG 4096R/A45477D5 -- see http://www.fi.muni.cz/~kas/pgp-rollover.txt |
| http://www.fi.muni.cz/~kas/     Journal: http://www.fi.muni.cz/~kas/blog/ |
||| "New and improved" is only really improved if it also takes backwards |||
||| compatibility into account, rather than saying "now everybody must do |||
||| things the new and improved - and different - way"   --Linus Torvalds |||



^ permalink raw reply	[flat|nested] 19+ messages in thread

* Re: [PATCH 6/7 linux-next] wan: cosa: replace current->state by set_current_state()
  2015-02-20 18:12 ` [PATCH 6/7 linux-next] wan: cosa: replace current->state by set_current_state() Fabian Frederick
  2015-02-20 18:26   ` Jan Yenya Kasprzak
@ 2015-02-20 18:34   ` Sergei Shtylyov
  2015-02-20 18:51     ` Fabian Frederick
  2015-02-20 18:58     ` Peter Zijlstra
  2015-02-22 20:25   ` David Miller
  2 siblings, 2 replies; 19+ messages in thread
From: Sergei Shtylyov @ 2015-02-20 18:34 UTC (permalink / raw)
  To: Fabian Frederick, linux-kernel
  Cc: Ingo Molnar, Peter Zijlstra, Greg Kroah-Hartman,
	Jan "Yenya" Kasprzak, netdev

Hello.

On 02/20/2015 09:12 PM, Fabian Frederick wrote:

> Use helper functions to access current->state.
> Direct assignments are prone to races and therefore buggy.

> current->state = TASK_RUNNING is replaced by __set_current_state()

    You sometimes use __set_current_state() and sometimes set_current_state().

> Thanks to Peter Zijlstra for the exact definition of the problem.

> Suggested-By: Peter Zijlstra <peterz@infradead.org>
> Signed-off-by: Fabian Frederick <fabf@skynet.be>

[...]

WBR, Sergei


^ permalink raw reply	[flat|nested] 19+ messages in thread

* Re: [PATCH 6/7 linux-next] wan: cosa: replace current->state by set_current_state()
  2015-02-20 18:34   ` Sergei Shtylyov
@ 2015-02-20 18:51     ` Fabian Frederick
  2015-02-20 19:15       ` Sergei Shtylyov
  2015-02-20 18:58     ` Peter Zijlstra
  1 sibling, 1 reply; 19+ messages in thread
From: Fabian Frederick @ 2015-02-20 18:51 UTC (permalink / raw)
  To: Sergei Shtylyov, linux-kernel
  Cc: Ingo Molnar, Peter Zijlstra, Greg Kroah-Hartman, netdev,
	Jan "Yenya" Kasprzak



> On 20 February 2015 at 19:34 Sergei Shtylyov
> <sergei.shtylyov@cogentembedded.com> wrote:
>
>
> Hello.
>
> On 02/20/2015 09:12 PM, Fabian Frederick wrote:
>
> > Use helper functions to access current->state.
> > Direct assignments are prone to races and therefore buggy.
>
> > current->state = TASK_RUNNING is replaced by __set_current_state()
>
>     You sometimes use __set_current_state() and sometimes set_current_state().
Hello Sergei,

Peter suggested to use __set_current_state() for TASK_RUNNING :
http://marc.info/?l=linux-kernel&m=142442259719216&w=2

Regards,
Fabian

>
> > Thanks to Peter Zijlstra for the exact definition of the problem.
>
> > Suggested-By: Peter Zijlstra <peterz@infradead.org>
> > Signed-off-by: Fabian Frederick <fabf@skynet.be>
>
> [...]
>
> WBR, Sergei
>

^ permalink raw reply	[flat|nested] 19+ messages in thread

* Re: [PATCH 6/7 linux-next] wan: cosa: replace current->state by set_current_state()
  2015-02-20 18:34   ` Sergei Shtylyov
  2015-02-20 18:51     ` Fabian Frederick
@ 2015-02-20 18:58     ` Peter Zijlstra
  2015-02-20 19:31       ` Eric Dumazet
  2015-02-21  7:42       ` Fabian Frederick
  1 sibling, 2 replies; 19+ messages in thread
From: Peter Zijlstra @ 2015-02-20 18:58 UTC (permalink / raw)
  To: Sergei Shtylyov
  Cc: Fabian Frederick, linux-kernel, Ingo Molnar, Greg Kroah-Hartman,
	Jan "Yenya" Kasprzak, netdev

On Fri, Feb 20, 2015 at 09:34:28PM +0300, Sergei Shtylyov wrote:
> Hello.
> 
> On 02/20/2015 09:12 PM, Fabian Frederick wrote:
> 
> >Use helper functions to access current->state.
> >Direct assignments are prone to races and therefore buggy.
> 
> >current->state = TASK_RUNNING is replaced by __set_current_state()
> 
>    You sometimes use __set_current_state() and sometimes set_current_state().

It depends on which state; setting yourself TASK_RUNNING is free of
wakeup races -- you're already running after all, so it can safely use
__set_current_state().

Setting a blocking state otoh needs set_current_state() which issues a
full memory barriers with the store (critically in this case,
effectively after the store) such that it orders the state store with a
subsequent load in the condition check if it really needs to go to
sleep.


In full:

	current->state = TASK_UNINTERRUPTIBLE;		wait = false;
	smp_mb();					smp_wmb();
	if (wait)					p->state = TASK_RUNNING;
		schedule();

Without that smp_mb(); the following order is possible:

	if (wait)
							wait = false;
							smp_wmb();
							p->state = TASK_RUNNING;
	current->state = TASK_UNINTERRUPTIBLE;
		schedule();

And we'll wait forever more..


^ permalink raw reply	[flat|nested] 19+ messages in thread

* Re: [PATCH 6/7 linux-next] wan: cosa: replace current->state by set_current_state()
  2015-02-20 18:51     ` Fabian Frederick
@ 2015-02-20 19:15       ` Sergei Shtylyov
  0 siblings, 0 replies; 19+ messages in thread
From: Sergei Shtylyov @ 2015-02-20 19:15 UTC (permalink / raw)
  To: Fabian Frederick, linux-kernel
  Cc: Ingo Molnar, Peter Zijlstra, Greg Kroah-Hartman, netdev,
	Jan "Yenya" Kasprzak

On 02/20/2015 09:51 PM, Fabian Frederick wrote:

>>> Use helper functions to access current->state.
>>> Direct assignments are prone to races and therefore buggy.

>>> current->state = TASK_RUNNING is replaced by __set_current_state()

>>       You sometimes use __set_current_state() and sometimes set_current_state().

> Hello Sergei,

> Peter suggested to use __set_current_state() for TASK_RUNNING :
> http://marc.info/?l=linux-kernel&m=142442259719216&w=2

    I didn't even question your decisions, I (like Peter) just wanted a more 
coherent change-log. Thanks to Peter for the explanations though. :-)

> Regards,
> Fabian

>>> Thanks to Peter Zijlstra for the exact definition of the problem.

>>> Suggested-By: Peter Zijlstra <peterz@infradead.org>
>>> Signed-off-by: Fabian Frederick <fabf@skynet.be>

>> [...]

WBR, Sergei


^ permalink raw reply	[flat|nested] 19+ messages in thread

* Re: [PATCH 6/7 linux-next] wan: cosa: replace current->state by set_current_state()
  2015-02-20 18:58     ` Peter Zijlstra
@ 2015-02-20 19:31       ` Eric Dumazet
  2015-02-20 19:44         ` Eric Dumazet
  2015-02-20 20:02         ` Peter Zijlstra
  2015-02-21  7:42       ` Fabian Frederick
  1 sibling, 2 replies; 19+ messages in thread
From: Eric Dumazet @ 2015-02-20 19:31 UTC (permalink / raw)
  To: Peter Zijlstra
  Cc: Sergei Shtylyov, Fabian Frederick, linux-kernel, Ingo Molnar,
	Greg Kroah-Hartman, Jan "Yenya" Kasprzak, netdev

On Fri, 2015-02-20 at 19:58 +0100, Peter Zijlstra wrote:
> On Fri, Feb 20, 2015 at 09:34:28PM +0300, Sergei Shtylyov wrote:
> > Hello.
> > 
> > On 02/20/2015 09:12 PM, Fabian Frederick wrote:
> > 
> > >Use helper functions to access current->state.
> > >Direct assignments are prone to races and therefore buggy.
> > 
> > >current->state = TASK_RUNNING is replaced by __set_current_state()
> > 
> >    You sometimes use __set_current_state() and sometimes set_current_state().
> 
> It depends on which state; setting yourself TASK_RUNNING is free of
> wakeup races -- you're already running after all, so it can safely use
> __set_current_state().

Maybe this might be self documented in set_current_state(),
as we have about 120 calls to __set_current_state(TASK_RUNNING)

diff --git a/include/linux/sched.h b/include/linux/sched.h
index 41c60e5302d7..26133da6445e 100644
--- a/include/linux/sched.h
+++ b/include/linux/sched.h
@@ -275,7 +275,11 @@ extern char ___assert_task_state[1 - 2*!!(
 #define set_current_state(state_value)				\
 	do {							\
 		current->task_state_change = _THIS_IP_;		\
-		set_mb(current->state, (state_value));		\
+		if (__builtin_constant_p(state_value) &&	\
+		    (state_value) == TASK_RUNNING)		\
+			current->state = (state_value);		\
+		else						\
+			set_mb(current->state, (state_value));	\
 	} while (0)
 
 #else




^ permalink raw reply related	[flat|nested] 19+ messages in thread

* Re: [PATCH 6/7 linux-next] wan: cosa: replace current->state by set_current_state()
  2015-02-20 19:31       ` Eric Dumazet
@ 2015-02-20 19:44         ` Eric Dumazet
  2015-02-20 20:02         ` Peter Zijlstra
  1 sibling, 0 replies; 19+ messages in thread
From: Eric Dumazet @ 2015-02-20 19:44 UTC (permalink / raw)
  To: Peter Zijlstra
  Cc: Sergei Shtylyov, Fabian Frederick, linux-kernel, Ingo Molnar,
	Greg Kroah-Hartman, Jan "Yenya" Kasprzak, netdev

On Fri, 2015-02-20 at 11:31 -0800, Eric Dumazet wrote:
> On Fri, 2015-02-20 at 19:58 +0100, Peter Zijlstra wrote:
> > On Fri, Feb 20, 2015 at 09:34:28PM +0300, Sergei Shtylyov wrote:
> > > Hello.
> > > 
> > > On 02/20/2015 09:12 PM, Fabian Frederick wrote:
> > > 
> > > >Use helper functions to access current->state.
> > > >Direct assignments are prone to races and therefore buggy.
> > > 
> > > >current->state = TASK_RUNNING is replaced by __set_current_state()
> > > 
> > >    You sometimes use __set_current_state() and sometimes set_current_state().
> > 
> > It depends on which state; setting yourself TASK_RUNNING is free of
> > wakeup races -- you're already running after all, so it can safely use
> > __set_current_state().
> 
> Maybe this might be self documented in set_current_state(),
> as we have about 120 calls to __set_current_state(TASK_RUNNING)

And about 138 calls to set_current_state(TASK_RUNNING)



^ permalink raw reply	[flat|nested] 19+ messages in thread

* Re: [PATCH 6/7 linux-next] wan: cosa: replace current->state by set_current_state()
  2015-02-20 19:31       ` Eric Dumazet
  2015-02-20 19:44         ` Eric Dumazet
@ 2015-02-20 20:02         ` Peter Zijlstra
  1 sibling, 0 replies; 19+ messages in thread
From: Peter Zijlstra @ 2015-02-20 20:02 UTC (permalink / raw)
  To: Eric Dumazet
  Cc: Sergei Shtylyov, Fabian Frederick, linux-kernel, Ingo Molnar,
	Greg Kroah-Hartman, Jan "Yenya" Kasprzak, netdev

On Fri, Feb 20, 2015 at 11:31:53AM -0800, Eric Dumazet wrote:
> On Fri, 2015-02-20 at 19:58 +0100, Peter Zijlstra wrote:

> Maybe this might be self documented in set_current_state(),
> as we have about 120 calls to __set_current_state(TASK_RUNNING)
> 
> diff --git a/include/linux/sched.h b/include/linux/sched.h
> index 41c60e5302d7..26133da6445e 100644
> --- a/include/linux/sched.h
> +++ b/include/linux/sched.h
> @@ -275,7 +275,11 @@ extern char ___assert_task_state[1 - 2*!!(
>  #define set_current_state(state_value)				\
>  	do {							\
>  		current->task_state_change = _THIS_IP_;		\
> -		set_mb(current->state, (state_value));		\
> +		if (__builtin_constant_p(state_value) &&	\
> +		    (state_value) == TASK_RUNNING)		\
> +			current->state = (state_value);		\
> +		else						\
> +			set_mb(current->state, (state_value));	\
>  	} while (0)

lkml.kernel.org/r/20150206163947.GR21418@twins.programming.kicks-ass.net

The problem is that there _might_ be someone relying on that barrier.

Its (very) unlikely, but you don't want to risk subtle borkage just
because. And I'm too lazy to go audit all of them :/



^ permalink raw reply	[flat|nested] 19+ messages in thread

* Re: [PATCH 6/7 linux-next] wan: cosa: replace current->state by set_current_state()
  2015-02-20 18:58     ` Peter Zijlstra
  2015-02-20 19:31       ` Eric Dumazet
@ 2015-02-21  7:42       ` Fabian Frederick
  1 sibling, 0 replies; 19+ messages in thread
From: Fabian Frederick @ 2015-02-21  7:42 UTC (permalink / raw)
  To: Peter Zijlstra, Sergei Shtylyov
  Cc: Ingo Molnar, Greg Kroah-Hartman, netdev, linux-kernel,
	Jan "Yenya" Kasprzak



> On 20 February 2015 at 19:58 Peter Zijlstra <peterz@infradead.org> wrote:
>
>
> On Fri, Feb 20, 2015 at 09:34:28PM +0300, Sergei Shtylyov wrote:
> > Hello.
> >
> > On 02/20/2015 09:12 PM, Fabian Frederick wrote:
> >
> > >Use helper functions to access current->state.
> > >Direct assignments are prone to races and therefore buggy.
> >
> > >current->state = TASK_RUNNING is replaced by __set_current_state()
> >
> >    You sometimes use __set_current_state() and sometimes
> >set_current_state().
>
> It depends on which state; setting yourself TASK_RUNNING is free of
> wakeup races -- you're already running after all, so it can safely use
> __set_current_state().
>
> Setting a blocking state otoh needs set_current_state() which issues a
> full memory barriers with the store (critically in this case,
> effectively after the store) such that it orders the state store with a
> subsequent load in the condition check if it really needs to go to
> sleep.
>
>
> In full:
>
>       current->state = TASK_UNINTERRUPTIBLE;          wait = false;
>       smp_mb();                                       smp_wmb();
>       if (wait)                                       p->state = TASK_RUNNING;
>               schedule();
>
> Without that smp_mb(); the following order is possible:
>
>       if (wait)
>                                                       wait = false;
>                                                       smp_wmb();
>                                                       p->state = TASK_RUNNING;
>       current->state = TASK_UNINTERRUPTIBLE;
>               schedule();
>
> And we'll wait forever more..
Do I have to add more comments in changelogs or is it OK for you ?
Maybe something like:
"
current->state = TASK_RUNNING can safely be converted to __set_current_state()
as we're already in that state. Other assignments are converted to
set_current_state() (which uses set_mb()).

"
Regards,
Fabian
>

^ permalink raw reply	[flat|nested] 19+ messages in thread

* Re: [PATCH 2/7 linux-next] mISDN: replace current->state by set_current_state()
  2015-02-20 18:12 ` [PATCH 2/7 linux-next] mISDN: " Fabian Frederick
@ 2015-02-22 20:24   ` David Miller
  0 siblings, 0 replies; 19+ messages in thread
From: David Miller @ 2015-02-22 20:24 UTC (permalink / raw)
  To: fabf; +Cc: linux-kernel, mingo, peterz, gregkh, isdn, netdev

From: Fabian Frederick <fabf@skynet.be>
Date: Fri, 20 Feb 2015 19:12:52 +0100

> Use helper function to access current->state.
> Direct assignments are prone to races and therefore buggy.
> 
> Thanks to Peter Zijlstra for the exact definition of the problem.
> 
> Signed-off-by: Fabian Frederick <fabf@skynet.be>

Applied.

^ permalink raw reply	[flat|nested] 19+ messages in thread

* Re: [PATCH 5/7 linux-next] hso: replace current->state by __set_current_state()
  2015-02-20 18:12 ` [PATCH 5/7 linux-next] hso: replace current->state by __set_current_state() Fabian Frederick
@ 2015-02-22 20:25   ` David Miller
  0 siblings, 0 replies; 19+ messages in thread
From: David Miller @ 2015-02-22 20:25 UTC (permalink / raw)
  To: fabf; +Cc: linux-kernel, mingo, peterz, gregkh, j.dumon, linux-usb, netdev

From: Fabian Frederick <fabf@skynet.be>
Date: Fri, 20 Feb 2015 19:12:55 +0100

> Use helper functions to access current->state.
> Direct assignments are prone to races and therefore buggy.
> 
> Thanks to Peter Zijlstra for the exact definition of the problem.
> 
> Suggested-By: Peter Zijlstra <peterz@infradead.org>
> Signed-off-by: Fabian Frederick <fabf@skynet.be>

Applied.

^ permalink raw reply	[flat|nested] 19+ messages in thread

* Re: [PATCH 6/7 linux-next] wan: cosa: replace current->state by set_current_state()
  2015-02-20 18:12 ` [PATCH 6/7 linux-next] wan: cosa: replace current->state by set_current_state() Fabian Frederick
  2015-02-20 18:26   ` Jan Yenya Kasprzak
  2015-02-20 18:34   ` Sergei Shtylyov
@ 2015-02-22 20:25   ` David Miller
  2 siblings, 0 replies; 19+ messages in thread
From: David Miller @ 2015-02-22 20:25 UTC (permalink / raw)
  To: fabf; +Cc: linux-kernel, mingo, peterz, gregkh, kas, netdev

From: Fabian Frederick <fabf@skynet.be>
Date: Fri, 20 Feb 2015 19:12:56 +0100

> Use helper functions to access current->state.
> Direct assignments are prone to races and therefore buggy.
> 
> current->state = TASK_RUNNING is replaced by __set_current_state()
> 
> Thanks to Peter Zijlstra for the exact definition of the problem.
> 
> Suggested-By: Peter Zijlstra <peterz@infradead.org>
> Signed-off-by: Fabian Frederick <fabf@skynet.be>

Applied.

^ permalink raw reply	[flat|nested] 19+ messages in thread

end of thread, other threads:[~2015-02-22 20:25 UTC | newest]

Thread overview: 19+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-02-20 18:12 [PATCH 1/7 linux-next] swim: replace current->state by set_current_state() Fabian Frederick
2015-02-20 18:12 ` [PATCH 2/7 linux-next] mISDN: " Fabian Frederick
2015-02-22 20:24   ` David Miller
2015-02-20 18:12 ` [PATCH 3/7 linux-next] powerpc/pmac: " Fabian Frederick
2015-02-20 18:12 ` [PATCH 4/7 linux-next] saa7146: " Fabian Frederick
2015-02-20 18:12 ` [PATCH 5/7 linux-next] hso: replace current->state by __set_current_state() Fabian Frederick
2015-02-22 20:25   ` David Miller
2015-02-20 18:12 ` [PATCH 6/7 linux-next] wan: cosa: replace current->state by set_current_state() Fabian Frederick
2015-02-20 18:26   ` Jan Yenya Kasprzak
2015-02-20 18:34   ` Sergei Shtylyov
2015-02-20 18:51     ` Fabian Frederick
2015-02-20 19:15       ` Sergei Shtylyov
2015-02-20 18:58     ` Peter Zijlstra
2015-02-20 19:31       ` Eric Dumazet
2015-02-20 19:44         ` Eric Dumazet
2015-02-20 20:02         ` Peter Zijlstra
2015-02-21  7:42       ` Fabian Frederick
2015-02-22 20:25   ` David Miller
2015-02-20 18:12 ` [PATCH 7/7 linux-next] serial: core: replace current->state by __set_current_state() Fabian Frederick

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).