Skip to contentSkip to conversation

sleep_on removal

RFC PATCH In review Arnd Bergmann 10 participants 80 messages Jan 2, 2014 – Feb 26, 2014

Subsystems (17)
lore
Share & tools
lore

Arnd Bergmann

author maintainer
#0 lore raw

cover sleep_on removal

The functions sleep_on, sleep_on_timeout, interruptible_sleep_on and interruptible_sleep_on_timeout have been deprecated for as long as I can remember, and a number of people have contributed patches in the past to remove them from various drivers.

This has recently popped up again and I decided to spend some of my time on tracking down the last users and kill it for good. The work is somewhat related to the BKL removal I did a couple of years ago, since most of these drivers were using the BKL in a way that actually made the sleep_on function safe to call. As the BKL was converted into mutexes, the semantics changed slightly and typically opened up a race between checking a condition and going to sleep.

I don't have any of the hardware that I'm sending the patches for, so it would be great if someone could test them before they get applied. Otherwise please review very carefully.

I'm definitely happy for any patches to go into maintainer trees right away. Obviously the final patch cannot go in until everything else gets merged first and I suspect there will be a series of patches for maintainerless drivers that will go along with it.

Arnd

Arnd Bergmann (30):
ataflop: fix sleep_on races
scsi: atari_scsi: fix sleep_on race
DAC960: remove sleep_on usage
swim3: fix interruptible_sleep_on race
[media] omap_vout: avoid sleep_on race
[media] usbvision: remove bogus sleep_on_timeout [media] radio-cadet: avoid interruptible_sleep_on race [media] arv: fix sleep_on race
staging: serqt_usb2: don't use sleep_on
staging: gdm72xx: fix interruptible_sleep_on race
staging: panel: fix interruptible_sleep_on race
parport: fix interruptible_sleep_on race
cris: sync_serial: remove interruptible_sleep_on tty/amiserial: avoid interruptible_sleep_on
usbserial: stop using interruptible_sleep_on
tty: synclink: avoid sleep_on race
atm: nicstar: remove interruptible_sleep_on_timeout
atm: firestream: fix interruptible_sleep_on race
isdn: pcbit: fix interruptible_sleep_on race
isdn: hisax/elsa: fix sleep_on race in elsa FSM
isdn: divert, hysdn: fix interruptible_sleep_on race
isdn: fix multiple sleep_on races
oss: msnd_pinnacle: avoid interruptible_sleep_on_timeout
oss: midibuf: fix sleep_on races
oss: vwsnd: avoid interruptible_sleep_on
oss: dmasound: kill SLEEP() macro to avoid race
oss: remove last sleep_on users
sgi-xp: open-code interruptible_sleep_on_timeout
char: nwbutton: open-code interruptible_sleep_on
sched: remove sleep_on() and friends

 Documentation/DocBook/kernel-hacking.tmpl    | 10 ------
 arch/cris/arch-v10/drivers/sync_serial.c     |  4 ++-
 arch/cris/arch-v32/drivers/sync_serial.c     |  4 ++-
 drivers/atm/firestream.c                     |  4 +--
 drivers/atm/nicstar.c                        | 13 ++++----
 drivers/block/DAC960.c                       | 34 +++++++++----------
 drivers/block/ataflop.c                      | 16 ++++-----
 drivers/block/swim3.c                        | 18 ++++++----
 drivers/char/nwbutton.c                      |  5 ++-
 drivers/char/pcmcia/synclink_cs.c            |  4 +--
 drivers/isdn/divert/divert_procfs.c          |  7 ++--
 drivers/isdn/hisax/elsa.c                    |  9 +++--
 drivers/isdn/hisax/elsa_ser.c                |  3 +-
 drivers/isdn/hysdn/hysdn_proclog.c           |  7 ++--
 drivers/isdn/i4l/isdn_common.c               | 13 +++++---
 drivers/isdn/pcbit/drv.c                     |  6 ++--
 drivers/media/platform/arv.c                 |  4 +--
 drivers/media/platform/omap/omap_vout_vrfb.c |  3 +-
 drivers/media/radio/radio-cadet.c            | 12 +++++--
 drivers/media/usb/usbvision/usbvision.h      |  4 +--
 drivers/misc/sgi-xp/xpc_channel.c            |  5 ++-
 drivers/parport/share.c                      |  3 +-
 drivers/scsi/atari_scsi.c                    | 12 +++++--
 drivers/staging/gdm72xx/gdm_usb.c            |  5 +--
 drivers/staging/panel/panel.c                |  4 +--
 drivers/staging/serqt_usb2/serqt_usb2.c      | 17 ++++------
 drivers/tty/amiserial.c                      | 26 ++++++++++-----
 drivers/tty/synclink.c                       |  4 +--
 drivers/tty/synclink_gt.c                    |  4 +--
 drivers/tty/synclinkmp.c                     |  4 +--
 drivers/usb/serial/ch341.c                   | 29 +++++++++++-----
 drivers/usb/serial/cypress_m8.c              | 49 ++++++++++++++++++----------
 drivers/usb/serial/f81232.c                  | 29 +++++++++++-----
 drivers/usb/serial/pl2303.c                  | 29 +++++++++++-----
 include/linux/wait.h                         | 11 -------
 kernel/sched/core.c                          | 46 --------------------------
 sound/oss/dmabuf.c                           | 14 ++++----
 sound/oss/dmasound/dmasound.h                |  1 -
 sound/oss/dmasound/dmasound_core.c           | 28 +++++++++++-----
 sound/oss/midibuf.c                          | 18 +++++-----
 sound/oss/msnd_pinnacle.c                    | 31 ++++++++++--------
 sound/oss/sequencer.c                        | 16 ++++-----
 sound/oss/sleep.h                            | 18 ++++++++++
 sound/oss/swarm_cs4297a.c                    | 14 ++++----
 sound/oss/vwsnd.c                            | 14 +++++---
 45 files changed, 334 insertions(+), 277 deletions(-)
 create mode 100644 sound/oss/sleep.h
Signature · 26 lines
1.8.3.2

Cc: [email protected]
Cc: [email protected]
Cc: [email protected]
Cc: [email protected]
Cc: [email protected]
Cc: [email protected]
Cc: [email protected]
Cc: [email protected]
Cc: [email protected]
Cc: [email protected]
Cc: [email protected]
Cc: [email protected]
Cc: [email protected]
Cc: [email protected]
Cc: [email protected]
Cc: [email protected]
Cc: [email protected]
Cc: [email protected]
Cc: [email protected]
Cc: [email protected]
Cc: [email protected]
Cc: [email protected]
Cc: [email protected]
Cc: [email protected]
author maintainer
#1 lore raw

RFC PATCH 1/30 ataflop: fix sleep_on races

sleep_on() is inherently racy, and has been deprecated for a long time. This fixes two instances in the atari floppy driver:

* fdc_wait/fdc_busy becomes an open-coded mutex. We cannot use the regular mutex since it gets released in interrupt context. The open-coded version using wait_event() and cmpxchg() is equivalent to the existing code but does the checks atomically, and we can now safely check the condition with irqs enabled.

* format_wait becomes a completion, which is the natural structure here. The format ioctl waits for the background task to either complete or abort.

This does not attempt to fix the preexisting bug of calling schedule with local interrupts disabled.

Cc Jens Axboe <[email protected]>
Cc Geert Uytterhoeven <[email protected]>
---
 drivers/block/ataflop.c | 16 ++++++++--------
 1 file changed, 8 insertions(+), 8 deletions(-)
drivers/block/ataflop.c +8 -8
index 0e30c6e..96b629e 100644
@@ -68,6 +68,8 @@
 #include <linux/init.h>
 #include <linux/blkdev.h>
 #include <linux/mutex.h>
+#include <linux/completion.h>
+#include <linux/wait.h>
 
 #include <asm/atafd.h>
 #include <asm/atafdreg.h>
@@ -301,7 +303,7 @@ module_param_array(UserSteprate, int, NULL, 0);
 /* Synchronization of FDC access. */
 static volatile int fdc_busy = 0;
 static DECLARE_WAIT_QUEUE_HEAD(fdc_wait);
-static DECLARE_WAIT_QUEUE_HEAD(format_wait);
+static DECLARE_COMPLETION(format_wait);
 
 static unsigned long changed_floppies = 0xff, fake_change = 0;
 #define	CHECK_CHANGE_DELAY	HZ/2
@@ -608,7 +610,7 @@ static void fd_error( void )
 	if (IsFormatting) {
 		IsFormatting = 0;
 		FormatError = 1;
-		wake_up( &format_wait );
+		complete(&format_wait);
 		return;
 	}
 
@@ -650,9 +652,8 @@ static int do_format(int drive, int type, struct atari_format_descr *desc)
 	DPRINT(("do_format( dr=%d tr=%d he=%d offs=%d )\n",
 		drive, desc->track, desc->head, desc->sect_offset ));
 
+	wait_event(fdc_wait, cmpxchg(&fdc_busy, 0, 1) == 0);
 	local_irq_save(flags);
-	while( fdc_busy ) sleep_on( &fdc_wait );
-	fdc_busy = 1;
 	stdma_lock(floppy_irq, NULL);
 	atari_turnon_irq( IRQ_MFP_FDC ); /* should be already, just to be sure */
 	local_irq_restore(flags);
@@ -706,7 +707,7 @@ static int do_format(int drive, int type, struct atari_format_descr *desc)
 	ReqSide  = desc->head;
 	do_fd_action( drive );
 
-	sleep_on( &format_wait );
+	wait_for_completion(&format_wait);
 
 	redo_fd_request();
 	return( FormatError ? -EIO : 0 );	
@@ -1229,7 +1230,7 @@ static void fd_writetrack_done( int status )
 		goto err_end;
 	}
 
-	wake_up( &format_wait );
+	complete(&format_wait);
 	return;
 
   err_end:
@@ -1497,8 +1498,7 @@ repeat:
 void do_fd_request(struct request_queue * q)
 {
 	DPRINT(("do_fd_request for pid %d\n",current->pid));
-	while( fdc_busy ) sleep_on( &fdc_wait );
-	fdc_busy = 1;
+	wait_event(fdc_wait, cmpxchg(&fdc_busy, 0, 1) == 0);
 	stdma_lock(floppy_irq, NULL);
 
 	atari_disable_irq( IRQ_MFP_FDC );
Signature · 1 line
1.8.3.2
#2 lore raw

RFC PATCH 1/30 ataflop: fix sleep_on races

---------- Forwarded message ----------
From: Arnd Bergmann <[email protected]>
Date: Thu, Jan 2, 2014 at 1:07 PM
Subject: [PATCH, RFC 01/30] ataflop: fix sleep_on races
To: [email protected]
Cc: Arnd Bergmann <[email protected]>, Jens Axboe <[email protected]>, Geert Uytterhoeven <[email protected]>

sleep_on() is inherently racy, and has been deprecated for a long time. This fixes two instances in the atari floppy driver:

* fdc_wait/fdc_busy becomes an open-coded mutex. We cannot use the regular mutex since it gets released in interrupt context. The open-coded version using wait_event() and cmpxchg() is equivalent to the existing code but does the checks atomically, and we can now safely check the condition with irqs enabled.

* format_wait becomes a completion, which is the natural structure here. The format ioctl waits for the background task to either complete or abort.

This does not attempt to fix the preexisting bug of calling schedule with local interrupts disabled.

Cc Jens Axboe <[email protected]>
Cc Geert Uytterhoeven <[email protected]>
---
 drivers/block/ataflop.c | 16 ++++++++--------
 1 file changed, 8 insertions(+), 8 deletions(-)
drivers/block/ataflop.c +4 -2
index 0e30c6e..96b629e 100644
@@ -68,6 +68,8 @@
 #include <linux/init.h>
 #include <linux/blkdev.h>
 #include <linux/mutex.h>
+#include <linux/completion.h>
+#include <linux/wait.h>
 
 #include <asm/atafd.h>
 #include <asm/atafdreg.h>
@@ -301,7 +303,7 @@ module_param_array(UserSteprate, int, NULL, 0);
 /* Synchronization of FDC access. */
 static volatile int fdc_busy = 0;
 static DECLARE_WAIT_QUEUE_HEAD(fdc_wait);
-static DECLARE_WAIT_QUEUE_HEAD(format_wait);
+static DECLARE_COMPLETION(format_wait);
 
 static unsigned long changed_floppies = 0xff, fake_change = 0;
 #define        CHECK_CHANGE_DELAY      HZ/2
@@ -608,7 +610,7 @@ static void fd_error( void )
        if (IsFormatting) {
                IsFormatting = 0;
                FormatError = 1;
-               wake_up( &format_wait );
+               complete(&format_wait);
                return;
        }
 
@@ -650,9 +652,8 @@ static int do_format(int drive, int type, struct

atari_format_descr *desc)

        DPRINT(("do_format( dr=%d tr=%d he=%d offs=%d )\n",
                drive, desc->track, desc->head, desc->sect_offset ));
+       wait_event(fdc_wait, cmpxchg(&fdc_busy, 0, 1) == 0);
        local_irq_save(flags);

- while( fdc_busy ) sleep_on( &fdc_wait );
- fdc_busy = 1;
stdma_lock(floppy_irq, NULL);
atari_turnon_irq( IRQ_MFP_FDC ); /* should be already, just to
be sure */

        local_irq_restore(flags);
@@ -706,7 +707,7 @@ static int do_format(int drive, int type, struct
atari_format_descr *desc)
        ReqSide  = desc->head;
        do_fd_action( drive );

- sleep_on( &format_wait );
+ wait_for_completion(&format_wait);

        redo_fd_request();
        return( FormatError ? -EIO : 0 );
@@ -1229,7 +1230,7 @@ static void fd_writetrack_done( int status )
                goto err_end;
        }

- wake_up( &format_wait );

+       complete(&format_wait);
        return;

err_end:
@@ -1497,8 +1498,7 @@ repeat:
void do_fd_request(struct request_queue * q)

 {
        DPRINT(("do_fd_request for pid %d\n",current->pid));
-       while( fdc_busy ) sleep_on( &fdc_wait );
-       fdc_busy = 1;
+       wait_event(fdc_wait, cmpxchg(&fdc_busy, 0, 1) == 0);
        stdma_lock(floppy_irq, NULL);

        atari_disable_irq( IRQ_MFP_FDC );

--
1.8.3.2

#3 lore raw

Re: [PATCH, RFC 01/30] ataflop: fix sleep_on races

Geert,

will look at this one as well, thanks.

Cheers,

Michael

Am 03.01.2014 um 01:27 schrieb Geert Uytterhoeven:

Quoted 111 lines

---------- Forwarded message ----------
From: Arnd Bergmann <[email protected]>
Date: Thu, Jan 2, 2014 at 1:07 PM
Subject: [PATCH, RFC 01/30] ataflop: fix sleep_on races
To: [email protected]
Cc: Arnd Bergmann <[email protected]>, Jens Axboe <[email protected]>, Geert Uytterhoeven <[email protected]>

sleep_on() is inherently racy, and has been deprecated for a long time. This fixes two instances in the atari floppy driver:

* fdc_wait/fdc_busy becomes an open-coded mutex. We cannot use the regular mutex since it gets released in interrupt context. The open-coded version using wait_event() and cmpxchg() is equivalent to the existing code but does the checks atomically, and we can now safely check the condition with irqs enabled.

* format_wait becomes a completion, which is the natural structure here. The format ioctl waits for the background task to either complete or abort.

This does not attempt to fix the preexisting bug of calling schedule with local interrupts disabled.

Cc Jens Axboe <[email protected]>
Cc Geert Uytterhoeven <[email protected]>
---
 drivers/block/ataflop.c | 16 ++++++++--------
 1 file changed, 8 insertions(+), 8 deletions(-)
drivers/block/ataflop.c +5 -4
index 0e30c6e..96b629e 100644
@@ -68,6 +68,8 @@
 #include <linux/init.h>
 #include <linux/blkdev.h>
 #include <linux/mutex.h>
+#include <linux/completion.h>
+#include <linux/wait.h>
 
 #include <asm/atafd.h>
 #include <asm/atafdreg.h>
@@ -301,7 +303,7 @@ module_param_array(UserSteprate, int, NULL, 0);
 /* Synchronization of FDC access. */
 static volatile int fdc_busy = 0;
 static DECLARE_WAIT_QUEUE_HEAD(fdc_wait);
-static DECLARE_WAIT_QUEUE_HEAD(format_wait);
+static DECLARE_COMPLETION(format_wait);
 
 static unsigned long changed_floppies = 0xff, fake_change = 0;
 #define        CHECK_CHANGE_DELAY      HZ/2
@@ -608,7 +610,7 @@ static void fd_error( void )
        if (IsFormatting) {
                IsFormatting = 0;
                FormatError = 1;
-               wake_up( &format_wait );
+               complete(&format_wait);
                return;
        }
 
@@ -650,9 +652,8 @@ static int do_format(int drive, int type, struct
 atari_format_descr *desc)
        DPRINT(("do_format( dr=%d tr=%d he=%d offs=%d )\n",
                drive, desc->track, desc->head, desc->sect_offset ));
 
+       wait_event(fdc_wait, cmpxchg(&fdc_busy, 0, 1) == 0);
        local_irq_save(flags);
-       while( fdc_busy ) sleep_on( &fdc_wait );
-       fdc_busy = 1;
        stdma_lock(floppy_irq, NULL);
        atari_turnon_irq( IRQ_MFP_FDC ); /* should be already, just to

be sure */

        local_irq_restore(flags);
drivers/block/ataflop.c +1 -1
@@ -706,7 +707,7 @@ static int do_format(int drive, int type, struct
 atari_format_descr *desc)
        ReqSide  = desc->head;
        do_fd_action( drive );
 
-       sleep_on( &format_wait );
+       wait_for_completion(&format_wait);
 
        redo_fd_request();
        return( FormatError ? -EIO : 0 );
drivers/block/ataflop.c +2 -4
@@ -1229,7 +1230,7 @@ static void fd_writetrack_done( int status )
                goto err_end;
        }
 
-       wake_up( &format_wait );
+       complete(&format_wait);
        return;
 
   err_end:
@@ -1497,8 +1498,7 @@ repeat:
 void do_fd_request(struct request_queue * q)
 {
        DPRINT(("do_fd_request for pid %d\n",current->pid));
-       while( fdc_busy ) sleep_on( &fdc_wait );
-       fdc_busy = 1;
+       wait_event(fdc_wait, cmpxchg(&fdc_busy, 0, 1) == 0);
        stdma_lock(floppy_irq, NULL);
 
        atari_disable_irq( IRQ_MFP_FDC );
--

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

author maintainer
#4 lore raw

RFC PATCH 2/30 scsi: atari_scsi: fix sleep_on race

In reply to Arnd Bergmann

sleep_on is known broken and going away. The atari_scsi driver is one of two remaining users in the falcon_get_lock() function, which is a rather crazy piece of code. This does not attempt to fix the driver's locking scheme in general, but at least prevents falcon_get_lock from going to sleep when no other thread holds the same lock or tries to get it, and we no longer schedule with irqs disabled.

Cc Geert Uytterhoeven <[email protected]>
Cc James E.J. Bottomley <[email protected]>
---
 drivers/scsi/atari_scsi.c | 12 +++++++++---
 1 file changed, 9 insertions(+), 3 deletions(-)
drivers/scsi/atari_scsi.c +9 -3
index a3e6c8a..b55a58a 100644
@@ -90,6 +90,7 @@
 #include <linux/init.h>
 #include <linux/nvram.h>
 #include <linux/bitops.h>
+#include <linux/wait.h>
 
 #include <asm/setup.h>
 #include <asm/atarihw.h>
@@ -549,8 +550,10 @@ static void falcon_get_lock(void)
 
 	local_irq_save(flags);
 
-	while (!in_irq() && falcon_got_lock && stdma_others_waiting())
-		sleep_on(&falcon_fairness_wait);
+	wait_event_cmd(falcon_fairness_wait,
+		       !in_irq() && falcon_got_lock && stdma_others_waiting(),
+		       local_irq_restore(flags),
+		       local_irq_save(flags));
 
 	while (!falcon_got_lock) {
 		if (in_irq())
@@ -562,7 +565,10 @@ static void falcon_get_lock(void)
 			falcon_trying_lock = 0;
 			wake_up(&falcon_try_wait);
 		} else {
-			sleep_on(&falcon_try_wait);
+			wait_event_cmd(falcon_try_wait,
+				       !falcon_got_lock && !falcon_trying_lock,
+				       local_irq_restore(flags),
+				       local_irq_save(flags));
 		}
 	}
 
Signature · 1 line
1.8.3.2
#5 lore raw

RFC PATCH 2/30 scsi: atari_scsi: fix sleep_on race

---------- Forwarded message ----------
From: Arnd Bergmann <[email protected]>
Date: Thu, Jan 2, 2014 at 1:07 PM
Subject: [PATCH, RFC 02/30] scsi: atari_scsi: fix sleep_on race
To: [email protected]
Cc: Arnd Bergmann <[email protected]>, Geert Uytterhoeven <[email protected]>, "James E.J. Bottomley" <[email protected]>, [email protected]

sleep_on is known broken and going away. The atari_scsi driver is one of two remaining users in the falcon_get_lock() function, which is a rather crazy piece of code. This does not attempt to fix the driver's locking scheme in general, but at least prevents falcon_get_lock from going to sleep when no other thread holds the same lock or tries to get it, and we no longer schedule with irqs disabled.

Cc Geert Uytterhoeven <[email protected]>
Cc James E.J. Bottomley <[email protected]>
---
 drivers/scsi/atari_scsi.c | 12 +++++++++---
 1 file changed, 9 insertions(+), 3 deletions(-)
drivers/scsi/atari_scsi.c +9 -3
index a3e6c8a..b55a58a 100644
@@ -90,6 +90,7 @@
 #include <linux/init.h>
 #include <linux/nvram.h>
 #include <linux/bitops.h>
+#include <linux/wait.h>
 
 #include <asm/setup.h>
 #include <asm/atarihw.h>
@@ -549,8 +550,10 @@ static void falcon_get_lock(void)
 
        local_irq_save(flags);
 
-       while (!in_irq() && falcon_got_lock && stdma_others_waiting())
-               sleep_on(&falcon_fairness_wait);
+       wait_event_cmd(falcon_fairness_wait,
+                      !in_irq() && falcon_got_lock && stdma_others_waiting(),
+                      local_irq_restore(flags),
+                      local_irq_save(flags));
 
        while (!falcon_got_lock) {
                if (in_irq())
@@ -562,7 +565,10 @@ static void falcon_get_lock(void)
                        falcon_trying_lock = 0;
                        wake_up(&falcon_try_wait);
                } else {
-                       sleep_on(&falcon_try_wait);
+                       wait_event_cmd(falcon_try_wait,
+                                      !falcon_got_lock && !falcon_trying_lock,
+                                      local_irq_restore(flags),
+                                      local_irq_save(flags));
                }
        }
 

--
1.8.3.2

#6 lore raw

Re: [PATCH, RFC 02/30] scsi: atari_scsi: fix sleep_on race

Geert,

thanks for passing that on - I had in fact disabled that feature of the driver for a good part of the 3.x series of kernels while attempting to figure out what else is wrong with the locking scheme.

I'll take a fresh stab at this.

Cheers,

Michael

Am 03.01.2014 um 01:26 schrieb Geert Uytterhoeven:

Quoted 70 lines

---------- Forwarded message ----------
From: Arnd Bergmann <[email protected]>
Date: Thu, Jan 2, 2014 at 1:07 PM
Subject: [PATCH, RFC 02/30] scsi: atari_scsi: fix sleep_on race
To: [email protected]
Cc: Arnd Bergmann <[email protected]>, Geert Uytterhoeven <[email protected]>, "James E.J. Bottomley" <[email protected]>, [email protected]

sleep_on is known broken and going away. The atari_scsi driver is one of two remaining users in the falcon_get_lock() function, which is a rather crazy piece of code. This does not attempt to fix the driver's locking scheme in general, but at least prevents falcon_get_lock from going to sleep when no other thread holds the same lock or tries to get it, and we no longer schedule with irqs disabled.

Cc Geert Uytterhoeven <[email protected]>
Cc James E.J. Bottomley <[email protected]>
---
 drivers/scsi/atari_scsi.c | 12 +++++++++---
 1 file changed, 9 insertions(+), 3 deletions(-)
drivers/scsi/atari_scsi.c +5 -2
index a3e6c8a..b55a58a 100644
@@ -90,6 +90,7 @@
 #include <linux/init.h>
 #include <linux/nvram.h>
 #include <linux/bitops.h>
+#include <linux/wait.h>
 
 #include <asm/setup.h>
 #include <asm/atarihw.h>
@@ -549,8 +550,10 @@ static void falcon_get_lock(void)
 
        local_irq_save(flags);
 
-       while (!in_irq() && falcon_got_lock && stdma_others_waiting())
-               sleep_on(&falcon_fairness_wait);
+       wait_event_cmd(falcon_fairness_wait,
+                      !in_irq() && falcon_got_lock && 
 stdma_others_waiting(),
+                      local_irq_restore(flags),
+                      local_irq_save(flags));
 
        while (!falcon_got_lock) {
                if (in_irq())
drivers/scsi/atari_scsi.c +4 -2
@@ -562,7 +565,10 @@ static void falcon_get_lock(void)
                        falcon_trying_lock = 0;
                        wake_up(&falcon_try_wait);
                } else {
-                       sleep_on(&falcon_try_wait);
+                       wait_event_cmd(falcon_try_wait,
+                                      !falcon_got_lock && 
 !falcon_trying_lock,
+                                      local_irq_restore(flags),
+                                      local_irq_save(flags));
                }
        }
 
--

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

maintainer
#7 lore raw

Re: [PATCH, RFC 02/30] scsi: atari_scsi: fix sleep_on race

Arnd,

your patch breaks the Atari NCR5380 SCSI driver (easily verified using ARAnyM). Last console output:

Quoted 14 lines

scsi0: options CAN_QUEUE=8 CMD_PER_LUN=1 SCAT-GAT=0 TAGGED-QUEUING=no HOSTID=7 generic options AUTOSENSE REAL DMA SCSI-2 TAGGED QUEUING generic release=7 scsi0 : Atari native SCSI
blk_queue_max_segments: set to minimum 1
scsi0: aborting command
scsi 0:0:0:0: CDB:
Inquiry: 12 00 00 00 24 00

NCR5380 core release=7.
NCR5380: coroutine isn't running.
scsi0: no currently connected command
scsi0: issue_queue
scsi0: disconnected_queue
random: nonblocking pool is initialized

Quoted 2 lines

scsi0: !!BINGO!! Falcon has no lock in NCR5380_abort
scsi0: warning : SCSI command probably completed successfully before abortion

No targets are connected, so the driver is expected to time out and abort each INQUIRY command. This works fine with the unmodified version of the driver (even though its locking scheme is well beyond crazy - I grant you that much). Note that the unmodified version also works fine on my actual Falcon hardware under moderate load (meaning to say, no hardware instabilities are triggered that would cause the SCSI chip to lock up and require a bus reset).

I'll debug this a bit to see where it gets stuck. Thanks for looking into this ugly mess of code!

Cheers,

Michael

Quoted 84 lines · Michael Schmitz

Geert,

thanks for passing that on - I had in fact disabled that feature of the driver for a good part of the 3.x series of kernels while attempting to figure out what else is wrong with the locking scheme.

I'll take a fresh stab at this.

Cheers,

Michael

Am 03.01.2014 um 01:26 schrieb Geert Uytterhoeven:

Quoted 70 lines

---------- Forwarded message ----------
From: Arnd Bergmann <[email protected]>
Date: Thu, Jan 2, 2014 at 1:07 PM
Subject: [PATCH, RFC 02/30] scsi: atari_scsi: fix sleep_on race
To: [email protected]
Cc: Arnd Bergmann <[email protected]>, Geert Uytterhoeven <[email protected]>, "James E.J. Bottomley" <[email protected]>, [email protected]

sleep_on is known broken and going away. The atari_scsi driver is one of two remaining users in the falcon_get_lock() function, which is a rather crazy piece of code. This does not attempt to fix the driver's locking scheme in general, but at least prevents falcon_get_lock from going to sleep when no other thread holds the same lock or tries to get it, and we no longer schedule with irqs disabled.

Cc Geert Uytterhoeven <[email protected]>
Cc James E.J. Bottomley <[email protected]>
---
 drivers/scsi/atari_scsi.c | 12 +++++++++---
 1 file changed, 9 insertions(+), 3 deletions(-)
drivers/scsi/atari_scsi.c +5 -2
index a3e6c8a..b55a58a 100644
@@ -90,6 +90,7 @@
 #include <linux/init.h>
 #include <linux/nvram.h>
 #include <linux/bitops.h>
+#include <linux/wait.h>
 
 #include <asm/setup.h>
 #include <asm/atarihw.h>
@@ -549,8 +550,10 @@ static void falcon_get_lock(void)
 
        local_irq_save(flags);
 
-       while (!in_irq() && falcon_got_lock && stdma_others_waiting())
-               sleep_on(&falcon_fairness_wait);
+       wait_event_cmd(falcon_fairness_wait,
+                      !in_irq() && falcon_got_lock && 
 stdma_others_waiting(),
+                      local_irq_restore(flags),
+                      local_irq_save(flags));
 
        while (!falcon_got_lock) {
                if (in_irq())
drivers/scsi/atari_scsi.c +4 -1
@@ -562,7 +565,10 @@ static void falcon_get_lock(void)
                        falcon_trying_lock = 0;
                        wake_up(&falcon_try_wait);
                } else {
-                       sleep_on(&falcon_try_wait);
+                       wait_event_cmd(falcon_try_wait,
+                                      !falcon_got_lock && 
 !falcon_trying_lock,
+                                      local_irq_restore(flags),
+                                      local_irq_save(flags));
                }
        }
Signature · 6 lines
-- 
1.8.3.2
-- 
To unsubscribe from this list: send the line "unsubscribe linux-m68k" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html
author maintainer
#8 lore raw

Re: [PATCH, RFC 02/30] scsi: atari_scsi: fix sleep_on race

Quoted 5 lines · Michael Schmitz

Arnd,

your patch breaks the Atari NCR5380 SCSI driver (easily verified using ARAnyM). Last console output:

Thanks so much for testing and sorry for your troubles. It seems I got the wrong polarity on at least one of the conditions when converting from a while()-style loop to an until()-style wait_event loop. I did double-check all the patches before, but this one must have slipped through because the use is so obscure.

Arnd

maintainer
#9 lore raw

Re: [PATCH, RFC 02/30] scsi: atari_scsi: fix sleep_on race

Arnd,

Quoted 5 lines
Quoted 3 lines

your patch breaks the Atari NCR5380 SCSI driver (easily verified using ARAnyM). Last console output:

Thanks so much for testing and sorry for your troubles. It seems I

Not to worry - without the occasional incentive to work on it, the driver would just keep on bitrotting more. Not that I ever officially took it on ...

Quoted 2 lines

got the wrong polarity on at least one of the conditions when converting from a while()-style loop to an until()-style wait_event loop.

Yep, that's my hunch too. From what I've seen so far it blocks at the falcon_fairness_wait stage, but more tests are needed.

Quoted 2 lines

I did double-check all the patches before, but this one must have slipped through because the use is so obscure.

Just say it - it is a nightmare. The code dates back to the times of the Big Kernel Lock, and interrupt priorities on Atari made it necessary to enable interrupts while in the SCSI inthandler at that time (after masking off the SCSI interrupt).

Sharing the interrupt with DMA completion, floppy and IDE without a way to properly check which one raised it just does not help.

If you want me to try something else, let me know.

Cheers,

Michael

maintainer
#10 lore raw

Re: [PATCH, RFC 02/30] scsi: atari_scsi: fix sleep_on race

In reply to Arnd Bergmann

Geert,

I will submit patches to the Atari SCSI driver based on Arnd's patch later this week. Has Arnd's initial patch made it into mainline yet?

While working on the driver, I noticed my Falcon ran into a memory squeeze once a day, resulting in oom-killing processes and rendering the system unusable. Most likely culprit to trigger this is the daily updatedb run. This sort of trouble started pretty much with my work on the SCSI driver, based on commit aa5311c454ed0ff959adca29c65be2157f52a84c (3.13-rc7). Do you know of any memory leak affecting m68k, introduced between last November and 3.13-rc7?

Cheers,

Michael

Quoted 15 lines
Quoted 5 lines

Arnd,

your patch breaks the Atari NCR5380 SCSI driver (easily verified using ARAnyM). Last console output:

Thanks so much for testing and sorry for your troubles. It seems I got the wrong polarity on at least one of the conditions when converting from a while()-style loop to an until()-style wait_event loop. I did double-check all the patches before, but this one must have slipped through because the use is so obscure.

Arnd

author maintainer
#11 lore raw

Re: [PATCH, RFC 02/30] scsi: atari_scsi: fix sleep_on race

Quoted 2 lines · Michael Schmitz

I will submit patches to the Atari SCSI driver based on Arnd's patch later this week. Has Arnd's initial patch made it into mainline yet?

I've been a little occupied with other work recently, so I have never gotten around to post a non-RFC version of the sleep_on patches. Some have been picked up by various maintainers anyway, but this one has not.

Thanks so much for taking care of this one!

Arnd

#12 lore raw

Re: [PATCH, RFC 02/30] scsi: atari_scsi: fix sleep_on race

Hi Arnd,

Quoted 10 lines · Michael Schmitz
Quoted 3 lines

I will submit patches to the Atari SCSI driver based on Arnd's patch later this week. Has Arnd's initial patch made it into mainline yet?

I've been a little occupied with other work recently, so I have never gotten around to post a non-RFC version of the sleep_on patches. Some have been picked up by various maintainers anyway, but this one has not.

OK, I will resend a merged patch for you or Geert to submit further up the food chain.

Quoted 2 lines

Thanks so much for taking care of this one!

No matter - thanks for taking the time to look into arcane stuff like Atari SCSI drivers!

Cheers,

Michael

Quoted 2 lines

Arnd

#13 lore raw

Re: [PATCH, RFC 02/30] scsi: atari_scsi: fix sleep_on race

In reply to Michael Schmitz

Hi Michael,

Quoted 7 lines · Michael Schmitz

While working on the driver, I noticed my Falcon ran into a memory squeeze once a day, resulting in oom-killing processes and rendering the system unusable. Most likely culprit to trigger this is the daily updatedb run. This sort of trouble started pretty much with my work on the SCSI driver, based on commit aa5311c454ed0ff959adca29c65be2157f52a84c (3.13-rc7). Do you know of any memory leak affecting m68k, introduced between last November and 3.13-rc7?

How much RAM and swap do you have?

Modern kernels just require more memory...

Gr{oetje,eeting}s,

Geert

--
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- [email protected]

In personal conversations with technical people, I call myself a hacker. But when I'm talking to journalists I just say "programmer" or something like that.
-- Linus Torvalds

#14 lore raw

Re: [PATCH, RFC 02/30] scsi: atari_scsi: fix sleep_on race

Hi Geert,

Quoted 11 lines
Quoted 8 lines

While working on the driver, I noticed my Falcon ran into a memory squeeze once a day, resulting in oom-killing processes and rendering the system unusable. Most likely culprit to trigger this is the daily updatedb run. This sort of trouble started pretty much with my work on the SCSI driver, based on commit aa5311c454ed0ff959adca29c65be2157f52a84c (3.13-rc7). Do you know of any memory leak affecting m68k, introduced between last November and 3.13-rc7?

How much RAM and swap do you have?

14 MB ST-RAM, 512 MB FastRAM, 2GB swap.

Quoted 2 lines

Modern kernels just require more memory...

Surely not that much more, I hope?

Cheers,

Michael

Quoted 1 line
#15 lore raw

Re: [PATCH, RFC 02/30] scsi: atari_scsi: fix sleep_on race

Quoted 11 lines · schmitz
Quoted 11 lines
Quoted 10 lines

While working on the driver, I noticed my Falcon ran into a memory squeeze
once a day, resulting in oom-killing processes and rendering the system unusable. Most likely culprit to trigger this is the daily updatedb run. This sort of trouble started pretty much with my work on the SCSI driver, based on commit aa5311c454ed0ff959adca29c65be2157f52a84c (3.13-rc7). Do you
know of any memory leak affecting m68k, introduced between last November and
3.13-rc7?

Quoted 7 lines
Quoted 1 line

How much RAM and swap do you have?

14 MB ST-RAM, 512 MB FastRAM, 2GB swap.

Quoted 1 line

Modern kernels just require more memory...

Surely not that much more, I hope?

512 MB should be plenty ;-)
My poor m68k box has 500 MB less...

Gr{oetje,eeting}s,

Geert

--
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- [email protected]

In personal conversations with technical people, I call myself a hacker. But when I'm talking to journalists I just say "programmer" or something like that.
-- Linus Torvalds

#16 lore raw

Re: [PATCH, RFC 02/30] scsi: atari_scsi: fix sleep_on race

In reply to Michael Schmitz

Hi Geert,

Quoted 2 lines

While working on the driver, I noticed my Falcon ran into a memory squeeze once a day, resulting in oom-killing processes and rendering the system unusable. Most likely culprit to trigger this is the daily updatedb run. This sort of trouble started pretty much with my work on the SCSI driver, based on commit aa5311c454ed0ff959adca29c65be2157f52a84c (3.13-rc7). Do you know of any memory leak affecting m68k, introduced between last November and 3.13-rc7?

Looks like Arnd's patch is not to blame, as I get a memory leak without it as well.

Straight after boot:

schmitz@hobbes:~$ cat /proc/interrupts
13:     316007     atari  timer
15:       2151     atari  ST-DMA floppy,ACSI,IDE,Falcon-SCSI, ide0
schmitz@hobbes:~$ free
            total       used       free     shared    buffers     cached
Mem:        526996      41928     485068          0       1944       9548
-/+ buffers/cache:      30436     496560

One day with no significant activity:

13:    8895122     atari  timer
15:      17483     atari  ST-DMA floppy,ACSI,IDE,Falcon-SCSI, ide0
Mem:        526996      83616     443380          0      17324      12732
-/+ buffers/cache:      53560     473436

Two days ...

13:   17385461     atari  timer
15:      31264     atari  ST-DMA floppy,ACSI,IDE,Falcon-SCSI, ide0
Mem:        526996      99528     427468          0      25136      12780
-/+ buffers/cache:      61612     465384

I'll try and go backwards to see when this all started. I switched toolchains in December, but that should not cause such behaviour, I suppose?

Cheers,

Michael

Quoted 24 lines

Cheers,

Michael

Quoted 15 lines
Quoted 5 lines

Arnd,

your patch breaks the Atari NCR5380 SCSI driver (easily verified using ARAnyM). Last console output:

Thanks so much for testing and sorry for your troubles. It seems I got the wrong polarity on at least one of the conditions when converting from a while()-style loop to an until()-style wait_event loop. I did double-check all the patches before, but this one must have slipped through because the use is so obscure.

Arnd

Signature · 3 lines
To unsubscribe from this list: send the line "unsubscribe linux-m68k" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html
#17 lore raw

Re: [PATCH, RFC 02/30] scsi: atari_scsi: fix sleep_on race

In reply to Michael Schmitz

Arnd,

Quoted 62 lines

I'll debug this a bit to see where it gets stuck. Thanks for looking into this ugly mess of code!

Cheers,

Michael

Quoted 52 lines · Michael Schmitz

Geert,

thanks for passing that on - I had in fact disabled that feature of the driver for a good part of the 3.x series of kernels while attempting to figure out what else is wrong with the locking scheme.

I'll take a fresh stab at this.

Cheers,

Michael

Am 03.01.2014 um 01:26 schrieb Geert Uytterhoeven:

Quoted 39 lines

---------- Forwarded message ----------
From: Arnd Bergmann <[email protected]>
Date: Thu, Jan 2, 2014 at 1:07 PM
Subject: [PATCH, RFC 02/30] scsi: atari_scsi: fix sleep_on race
To: [email protected]
Cc: Arnd Bergmann <[email protected]>, Geert Uytterhoeven <[email protected]>, "James E.J. Bottomley" <[email protected]>, [email protected]

sleep_on is known broken and going away. The atari_scsi driver is one of two remaining users in the falcon_get_lock() function, which is a rather crazy piece of code. This does not attempt to fix the driver's locking scheme in general, but at least prevents falcon_get_lock from going to sleep when no other thread holds the same lock or tries to get it, and we no longer schedule with irqs disabled.

Cc Geert Uytterhoeven <[email protected]>
Cc James E.J. Bottomley <[email protected]>
---
 drivers/scsi/atari_scsi.c | 12 +++++++++---
 1 file changed, 9 insertions(+), 3 deletions(-)
drivers/scsi/atari_scsi.c +1 -0
index a3e6c8a..b55a58a 100644
@@ -90,6 +90,7 @@
 #include <linux/init.h>
 #include <linux/nvram.h>
 #include <linux/bitops.h>
+#include <linux/wait.h>
 
 #include <asm/setup.h>
 #include <asm/atarihw.h>
@@ -549,8 +550,10 @@ static void falcon_get_lock(void)
 
Quoted 7 lines
Quoted 7 lines

drivers/scsi/atari_scsi.c static void falcon_get_lock(void)

Quoted 7 lines
drivers/scsi/atari_scsi.c +2 -2
        local_irq_save(flags);
 
-       while (!in_irq() && falcon_got_lock && stdma_others_waiting())
-               sleep_on(&falcon_fairness_wait);
+       wait_event_cmd(falcon_fairness_wait,
+                      !in_irq() && falcon_got_lock && 
 stdma_others_waiting(),

Replace by:

                     in_irq() || !falcon_got_lock || 

!stdma_others_waiting(),

Much better now. Needs stress testing on real hardware, but that'll have to wait until the weekend I guess.

Cheers,

Michael

maintainer
#18 lore raw

Re: [PATCH, RFC 02/30] scsi: atari_scsi: fix sleep_on race

In reply to Michael Schmitz

Arnd,

this one looks like it should be changed as well:

Quoted 8 lines
Quoted 8 lines

drivers/scsi/atari_scsi.c static void falcon_get_lock(void) · line 570

Quoted 8 lines
drivers/scsi/atari_scsi.c +2 -1
@@ -562,7 +565,10 @@ static void falcon_get_lock(void)
                        falcon_trying_lock = 0;
                        wake_up(&falcon_try_wait);
                } else {
-                       sleep_on(&falcon_try_wait);
+                       wait_event_cmd(falcon_try_wait,
+                                      !falcon_got_lock && 
 !falcon_trying_lock,

I think using falcon_got_lock && !falcon_trying_lock here reflects what we try to achieve - the if branch above this will set that exact condition, then wake the wait queue. Any other calls to falcon_get_lock that have been waiting on the lock should then continue on as the lock is now held by the driver. Thoughts?

I've not see that case used in a cursory test on hardware, more stress testing is needed (logging lock history to a kernel buffer instead of clogging up the console).

Cheers,

Michael

Quoted 13 lines
Quoted 12 lines
Quoted 11 lines
drivers/scsi/atari_scsi.c +2 -0
+ local_irq_restore(flags),
+                                      local_irq_save(flags));
                }
        }
Signature · 6 lines
-- 
1.8.3.2
-- 
To unsubscribe from this list: send the line "unsubscribe linux-m68k" in
the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
maintainer
#19 lore raw

Re: [PATCH, RFC 02/30] scsi: atari_scsi: fix sleep_on race

Arnd, Geert,

Quoted 4 lines · Michael Schmitz

Arnd,

this one looks like it should be changed as well:

Quoted 15 lines
Quoted 8 lines
Quoted 8 lines

drivers/scsi/atari_scsi.c static void falcon_get_lock(void) · line 570

Quoted 8 lines
drivers/scsi/atari_scsi.c +2 -1
@@ -562,7 +565,10 @@ static void falcon_get_lock(void)
                        falcon_trying_lock = 0;
                        wake_up(&falcon_try_wait);
                } else {
-                       sleep_on(&falcon_try_wait);
+                       wait_event_cmd(falcon_try_wait,
+                                      !falcon_got_lock &&
 !falcon_trying_lock,

I think using falcon_got_lock && !falcon_trying_lock here reflects what we try to achieve - the if branch above this will set that exact condition, then wake the wait queue. Any other calls to falcon_get_lock that have been waiting on the lock should then continue on
as the lock is now held by the driver. Thoughts?

I can confirm that the condition (falcon_got_lock && !falcon_trying_lock) does work here - no problems for basic operations. Arnd, do you want to handle this, or should I send the updated patch via Geert?

A bit of stress testing using concurrent SCSI and IDE I/O has revealed the driver still deadlocks when SCSI commands are queued from softirq context and the ST-DMA lock is held by the IDE or floppy driver (presumably; haven't verified that). I will try a few likely workarounds that had worked in the past, but the real question is this:

Does stdma_lock need the uninterruptible wait? The comment there says it is needed to protect locked buffers, but this was all set up back in the dark ages. IDE takes the host lock in addition to the ST-DMA lock, SCSI still locks out interrupts where critical (I need to change that) so it appears all should be safe? Comments?

Cheers,

Michael

maintainer
#20 lore raw

cover m68k/atari - Atari NCR5380 SCSI driver fixes

In reply to Geert Uytterhoeven

Geert,

this patch series brings the Atari NCR5380 driver up to date with current 3.x (or even 2.6) series SCSI midlevel and error handling changes.

The first patch fixes completion condition errors contained in Arnd Bergmann's patch ([PATCH, RFC 02/30] scsi: atari_scsi: fix sleep_on race). The locking scheme still isn't 100% race free that way, but much improved, and I've seen the driver successfully handle error recovery from aborting a disconnected command via bus reset with all three patches applied. Never managed to do that before. Not sure whether this one should be taken through your tree or handled by Arnd rather.

The other two are a rehash of old patches I had prepared somewhere along the 2.6 series - somehow or other these were lost when one of my old git trees went pear shaped. Abort and reset handler return codes have changed, and the SCSI midlevel now queues commands (in particular error handling ones) from softirq context - the Falcon locking scheme was ill equipped to deal with that.

Another issue that surfaced while debugging SCSI deadlocks was the odd fact that two interrupts get registered for the ST-DMA/floppy/SCSI/IDE interrupt:
the stdma_int multiplexer, and the IDE interrupt handler itself. I'll send a separate patch for that.

Cheers,

Michael

[PATCH 1/3] m68k/atari - atari_scsi: fix wait_event completion conditions [PATCH 2/3] m68k/atari - atari_scsi: change abort/reset return codes [PATCH 3/3] m68k/atari - atari_scsi: punt if deadlocked on stdma_lock

#21 lore raw

Re: [PATCH 0/3] m68k/atari - Atari NCR5380 SCSI driver fixes

Hi Michael,

Quoted 11 lines · Michael Schmitz

this patch series brings the Atari NCR5380 driver up to date with current 3.x (or even 2.6) series SCSI midlevel and error handling changes.

The first patch fixes completion condition errors contained in Arnd Bergmann's patch ([PATCH, RFC 02/30] scsi: atari_scsi: fix sleep_on race). The locking scheme still isn't 100% race free that way, but much improved, and I've seen the driver successfully handle error recovery from aborting a disconnected command via bus reset with all three patches applied. Never managed to do that before. Not sure whether this one should be taken through your tree or handled by Arnd rather.

Quoted 5 lines

The other two are a rehash of old patches I had prepared somewhere along the 2.6 series - somehow or other these were lost when one of my old git trees went pear shaped. Abort and reset handler return codes have changed, and the SCSI midlevel now queues commands (in particular error handling ones) from softirq context - the Falcon locking scheme was ill equipped to deal with that.

Can you please resend the series including
1. the SCSI maintainer,
2. the SCSI mailing list,
3. Arnd Bergmann?

scripts/get_maintainer.pl is your friend.

Thanks!

Gr{oetje,eeting}s,

Geert

--
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- [email protected]

In personal conversations with technical people, I call myself a hacker. But when I'm talking to journalists I just say "programmer" or something like that.
-- Linus Torvalds

maintainer
#22 lore raw

PATCH 1/3 m68k/atari - atari_scsi: fix wait_event completion conditions

In reply to Geert Uytterhoeven

Fix patch by ArndB changing falcon_get_lock to use wait_event. Some of the completion conditions had been missed when converting from while() {} to do {} until() logic.

---
 drivers/scsi/atari_scsi.c |   14 +++++++-------
 1 files changed, 7 insertions(+), 7 deletions(-)
drivers/scsi/atari_scsi.c +7 -7
index 1986ecb..cc1b013 100644
@@ -550,10 +550,10 @@ static void falcon_get_lock(void)
 
 	local_irq_save(flags);
 
-       wait_event_cmd(falcon_fairness_wait,
-                      !in_irq() && falcon_got_lock && stdma_others_waiting(),
-                      local_irq_restore(flags),
-                      local_irq_save(flags));
+	wait_event_cmd(falcon_fairness_wait,
+		in_irq() || !falcon_got_lock || !stdma_others_waiting(),
+		local_irq_restore(flags),
+		local_irq_save(flags));
 
 	while (!falcon_got_lock) {
 		if (in_irq())
@@ -566,9 +566,9 @@ static void falcon_get_lock(void)
 			wake_up(&falcon_try_wait);
 		} else {
                        wait_event_cmd(falcon_try_wait,
-                                      !falcon_got_lock && !falcon_trying_lock,
-                                      local_irq_restore(flags),
-                                      local_irq_save(flags));
+				falcon_got_lock && !falcon_trying_lock,
+				local_irq_restore(flags),
+				local_irq_save(flags));
 		}
 	}
 
Signature · 1 line
1.7.0.4
maintainer
#23 lore raw

PATCH 2/3 m68k/atari - atari_scsi: change abort/reset return codes

In reply to Geert Uytterhoeven

The abort/reset lowlevel return codes had changed with the new error SCSI handling - update Atari NCR5380 driver to reflect this.

Change reset handling to clear queues only, do not attempt to call done() on each command aborted by the reset. The EH code should do that for us.
Queues _must_ be cleared, otherwise atari_scsi_bus_reset will not release the ST-DMA lock, deadlocking further error recovery.

---
 drivers/scsi/atari_NCR5380.c |   29 ++++++++++++++++++-----------
 drivers/scsi/atari_scsi.c    |    2 +-
 2 files changed, 19 insertions(+), 12 deletions(-)
drivers/scsi/atari_NCR5380.c +18 -11
index 0f3cdbc..465e63d 100644
@@ -2683,11 +2683,11 @@ int NCR5380_abort(Scsi_Cmnd *cmd)
 			local_irq_restore(flags);
 			cmd->scsi_done(cmd);
 			falcon_release_lock_if_possible(hostdata);
-			return SCSI_ABORT_SUCCESS;
+			return SUCCESS;
 		} else {
 /*			local_irq_restore(flags); */
 			printk("scsi%d: abort of connected command failed!\n", HOSTNO);
-			return SCSI_ABORT_ERROR;
+			return FAILED;
 		}
 	}
 #endif
@@ -2711,7 +2711,7 @@ int NCR5380_abort(Scsi_Cmnd *cmd)
 			 * yet... */
 			tmp->scsi_done(tmp);
 			falcon_release_lock_if_possible(hostdata);
-			return SCSI_ABORT_SUCCESS;
+			return SUCCESS;
 		}
 	}
 
@@ -2729,7 +2729,7 @@ int NCR5380_abort(Scsi_Cmnd *cmd)
 	if (hostdata->connected) {
 		local_irq_restore(flags);
 		ABRT_PRINTK("scsi%d: abort failed, command connected.\n", HOSTNO);
-		return SCSI_ABORT_SNOOZE;
+		return FAILED;
 	}
 
 	/*
@@ -2764,7 +2764,7 @@ int NCR5380_abort(Scsi_Cmnd *cmd)
 			ABRT_PRINTK("scsi%d: aborting disconnected command.\n", HOSTNO);
 
 			if (NCR5380_select(instance, cmd, (int)cmd->tag))
-				return SCSI_ABORT_BUSY;
+				return FAILED;
 
 			ABRT_PRINTK("scsi%d: nexus reestablished.\n", HOSTNO);
 
@@ -2791,7 +2791,7 @@ int NCR5380_abort(Scsi_Cmnd *cmd)
 					local_irq_restore(flags);
 					tmp->scsi_done(tmp);
 					falcon_release_lock_if_possible(hostdata);
-					return SCSI_ABORT_SUCCESS;
+					return SUCCESS;
 				}
 			}
 		}
@@ -2816,7 +2816,7 @@ int NCR5380_abort(Scsi_Cmnd *cmd)
 	 */
 	falcon_release_lock_if_possible(hostdata);
 
-	return SCSI_ABORT_NOT_RUNNING;
+	return FAILED;
 }
 
 
@@ -2834,7 +2834,7 @@ static int NCR5380_bus_reset(Scsi_Cmnd *cmd)
 	SETUP_HOSTDATA(cmd->device->host);
 	int i;
 	unsigned long flags;
-#if 1
+#if defined(RESET_RUN_DONE)
 	Scsi_Cmnd *connected, *disconnected_queue;
 #endif
 
@@ -2859,7 +2859,14 @@ static int NCR5380_bus_reset(Scsi_Cmnd *cmd)
 	 * through anymore ... */
 	(void)NCR5380_read(RESET_PARITY_INTERRUPT_REG);
 
-#if 1	/* XXX Should now be done by midlevel code, but it's broken XXX */
+	/* MSch 20140115 - looking at the generic NCR5380 driver, all of this
+	 * should go.
+	 * Catch-22: if we don't clear all queues, the SCSI driver lock will
+	 * not be reset by atari_scsi_reset()!
+	 */
+
+#if defined(RESET_RUN_DONE)
+	/* XXX Should now be done by midlevel code, but it's broken XXX */
 	/* XXX see below                                            XXX */
 
 	/* MSch: old-style reset: actually abort all command processing here */
@@ -2915,7 +2922,7 @@ static int NCR5380_bus_reset(Scsi_Cmnd *cmd)
 	 * the midlevel code that the reset was SUCCESSFUL, and there is no
 	 * need to 'wake up' the commands by a request_sense
 	 */
-	return SCSI_RESET_SUCCESS | SCSI_RESET_BUS_RESET;
+	return SUCCESS;
 #else /* 1 */
 
 	/* MSch: new-style reset handling: let the mid-level do what it can */
@@ -2963,6 +2970,6 @@ static int NCR5380_bus_reset(Scsi_Cmnd *cmd)
 	local_irq_restore(flags);
 
 	/* we did no complete reset of all commands, so a wakeup is required */
-	return SCSI_RESET_WAKEUP | SCSI_RESET_BUS_RESET;
+	return SUCCESS;
 #endif /* 1 */
 }
drivers/scsi/atari_scsi.c +1 -1
index cc1b013..5e19509 100644
@@ -827,7 +827,7 @@ static int atari_scsi_bus_reset(Scsi_Cmnd *cmd)
 	} else {
 		atari_turnon_irq(IRQ_MFP_FSCSI);
 	}
-	if ((rv & SCSI_RESET_ACTION) == SCSI_RESET_SUCCESS)
+	if (rv == SUCCESS)
 		falcon_release_lock_if_possible(hostdata);
 
 	return rv;
Signature · 1 line
1.7.0.4
maintainer
#24 lore raw

PATCH 3/3 m68k/atari - atari_scsi lock fixes: punt if deadlocked

In reply to Geert Uytterhoeven

In case a SCSI command is queued from softirq context, and another driver currently holds the ST-DMA lock, tell the SCSI midlevel to hold off queueing commands for now. Something will hopefully resume play later.

---
 drivers/scsi/atari_NCR5380.c |   12 +++++++++---
 drivers/scsi/atari_scsi.c    |   26 +++++++++++++++++++++-----
 2 files changed, 30 insertions(+), 8 deletions(-)
drivers/scsi/atari_NCR5380.c +9 -3
index 465e63d..90a90e8 100644
@@ -967,9 +967,15 @@ static int NCR5380_queue_command_lck(Scsi_Cmnd *cmd, void (*done)(Scsi_Cmnd *))
 	 * alter queues and touch the lock.
 	 */
 	if (!IS_A_TT()) {
-		/* perhaps stop command timer here */
-		falcon_get_lock();
-		/* perhaps restart command timer here */
+		/* MSch 20140119: check whether obtaining the ST-DMA lock did
+		 * succeed.
+		 * If the lock could not be acquired without risking to
+		 * deadlock, i.e. from softirq context with ST-DMA currently
+		 * otherwise locked, defer queueing further commands.
+		 */
+		if (falcon_get_lock()) {
+			return SCSI_MLQUEUE_HOST_BUSY;
+		}
 	}
 	if (!(hostdata->issue_queue) || (cmd->cmnd[0] == REQUEST_SENSE)) {
 		LIST(cmd, hostdata->issue_queue);
drivers/scsi/atari_scsi.c +21 -5
index 5e19509..2b62ce2 100644
@@ -197,7 +197,7 @@ static unsigned long atari_dma_xfer_len(unsigned long wanted_len,
 static irqreturn_t scsi_tt_intr(int irq, void *dummy);
 static irqreturn_t scsi_falcon_intr(int irq, void *dummy);
 static void falcon_release_lock_if_possible(struct NCR5380_hostdata *hostdata);
-static void falcon_get_lock(void);
+static int falcon_get_lock(void);
 #ifdef CONFIG_ATARI_SCSI_RESET_BOOT
 static void atari_scsi_reset_boot(void);
 #endif
@@ -541,24 +541,39 @@ static void falcon_release_lock_if_possible(struct NCR5380_hostdata *hostdata)
  * Complicated, complicated.... Sigh...
  */
 
-static void falcon_get_lock(void)
+static int falcon_get_lock(void)
 {
 	unsigned long flags;
 
 	if (IS_A_TT())
-		return;
+		return 0;
 
 	local_irq_save(flags);
 
 	wait_event_cmd(falcon_fairness_wait,
-		in_irq() || !falcon_got_lock || !stdma_others_waiting(),
+		in_interrupt() || !falcon_got_lock || !stdma_others_waiting(),
 		local_irq_restore(flags),
 		local_irq_save(flags));
 
 	while (!falcon_got_lock) {
 		if (in_irq())
-			panic("Falcon SCSI hasn't ST-DMA lock in interrupt");
+			panic("Falcon SCSI hasn't got ST-DMA lock in irq");
 		if (!falcon_trying_lock) {
+			if (in_interrupt()) {
+				if (stdma_islocked()) {
+					/* MSch 20140119: problem -
+					 * cannot schedule in interrupt!
+					 * Unless stdma_lock can be modified
+					 * to interruptible wait, this will
+					 * be needed to avoid deadlocking here!
+					 * Return error to indicate command
+					 * queueing would deadlock, and allow
+					 * queue_command() to stall queueing.
+					 */
+					local_irq_restore(flags);
+					return 1;
+				}
+			}
 			falcon_trying_lock = 1;
 			stdma_lock(scsi_falcon_intr, NULL);
 			falcon_got_lock = 1;
@@ -575,6 +590,7 @@ static void falcon_get_lock(void)
 	local_irq_restore(flags);
 	if (!falcon_got_lock)
 		panic("Falcon SCSI: someone stole the lock :-(\n");
+	return 0;
 }
 
 
Signature · 1 line
1.7.0.4
maintainer
#25 lore raw

cover m68k/atari - Atari NCR5380 SCSI driver fixes (resent)

In reply to Arnd Bergmann

Geert,

this patch series brings the Atari NCR5380 driver up to date with current 3.x (or perhaps even 2.6) series SCSI midlevel and error handling changes.

The first patch fixes completion condition errors contained in Arnd Bergmann's patch ([PATCH, RFC 02/30] scsi: atari_scsi: fix sleep_on race). The locking scheme still isn't 100% race free that way, but much improved, and I've seen the driver successfully handle error recovery from aborting a disconnected command via bus reset with all three patches applied (never managed to do that since 2.4 days).
Not sure whether this one should be taken through your tree or handled by Arnd rather.

The other two are a rehash of old patches I had prepared somewhere along the 2.6 series - somehow or other these were lost when one of my old git trees went pear shaped.

Abort and reset handlers nowadays return SUCCESS or FAILED only, and the SCSI midlevel now queues commands (in particular error handling ones) from soft interrupt context - the Falcon locking scheme still is ill equipped to handle that. Return codes have been changed and the reset handler in particular will leave running command completion handlers to the midlevel now.

If a SCSI command is queued while IDE or floppy have reserved the shared DMA and interrupt, queue_command() may not wait for lock release and instead will return SCSI_MLQUEUE_HOST_BUSY to indicate that.

CC to linux-scsi and the SCSI maintainer added as per your request.

Cheers,

Michael

maintainer
#26 lore raw

PATCH 1/3 m68k/atari - atari_scsi: fix wait_event completion conditions

In reply to Arnd Bergmann

Fix patch by ArndB changing falcon_get_lock to use wait_event. Some of the completion conditions had been missed when converting from while() {} to do {} until() logic.

---
 drivers/scsi/atari_scsi.c |   14 +++++++-------
 1 files changed, 7 insertions(+), 7 deletions(-)
drivers/scsi/atari_scsi.c +7 -7
index 1986ecb..cc1b013 100644
@@ -550,10 +550,10 @@ static void falcon_get_lock(void)
 
 	local_irq_save(flags);
 
-       wait_event_cmd(falcon_fairness_wait,
-                      !in_irq() && falcon_got_lock && stdma_others_waiting(),
-                      local_irq_restore(flags),
-                      local_irq_save(flags));
+	wait_event_cmd(falcon_fairness_wait,
+		in_irq() || !falcon_got_lock || !stdma_others_waiting(),
+		local_irq_restore(flags),
+		local_irq_save(flags));
 
 	while (!falcon_got_lock) {
 		if (in_irq())
@@ -566,9 +566,9 @@ static void falcon_get_lock(void)
 			wake_up(&falcon_try_wait);
 		} else {
                        wait_event_cmd(falcon_try_wait,
-                                      !falcon_got_lock && !falcon_trying_lock,
-                                      local_irq_restore(flags),
-                                      local_irq_save(flags));
+				falcon_got_lock && !falcon_trying_lock,
+				local_irq_restore(flags),
+				local_irq_save(flags));
 		}
 	}
 
Signature · 1 line
1.7.0.4
maintainer
#27 lore raw

PATCH 2/3 m68k/atari - atari_scsi: change abort/reset return codes

In reply to Arnd Bergmann

The abort/reset lowlevel return codes had changed with the new error SCSI handling - update Atari NCR5380 driver to reflect this.

Change reset handling to clear queues only, do not attempt to call done() on each command aborted by the reset. The EH code should do that for us.
Queues _must_ be cleared, otherwise atari_scsi_bus_reset will not release the ST-DMA lock, deadlocking further error recovery.

---
 drivers/scsi/atari_NCR5380.c |   29 ++++++++++++++++++-----------
 drivers/scsi/atari_scsi.c    |    2 +-
 2 files changed, 19 insertions(+), 12 deletions(-)
drivers/scsi/atari_NCR5380.c +18 -11
index 0f3cdbc..465e63d 100644
@@ -2683,11 +2683,11 @@ int NCR5380_abort(Scsi_Cmnd *cmd)
 			local_irq_restore(flags);
 			cmd->scsi_done(cmd);
 			falcon_release_lock_if_possible(hostdata);
-			return SCSI_ABORT_SUCCESS;
+			return SUCCESS;
 		} else {
 /*			local_irq_restore(flags); */
 			printk("scsi%d: abort of connected command failed!\n", HOSTNO);
-			return SCSI_ABORT_ERROR;
+			return FAILED;
 		}
 	}
 #endif
@@ -2711,7 +2711,7 @@ int NCR5380_abort(Scsi_Cmnd *cmd)
 			 * yet... */
 			tmp->scsi_done(tmp);
 			falcon_release_lock_if_possible(hostdata);
-			return SCSI_ABORT_SUCCESS;
+			return SUCCESS;
 		}
 	}
 
@@ -2729,7 +2729,7 @@ int NCR5380_abort(Scsi_Cmnd *cmd)
 	if (hostdata->connected) {
 		local_irq_restore(flags);
 		ABRT_PRINTK("scsi%d: abort failed, command connected.\n", HOSTNO);
-		return SCSI_ABORT_SNOOZE;
+		return FAILED;
 	}
 
 	/*
@@ -2764,7 +2764,7 @@ int NCR5380_abort(Scsi_Cmnd *cmd)
 			ABRT_PRINTK("scsi%d: aborting disconnected command.\n", HOSTNO);
 
 			if (NCR5380_select(instance, cmd, (int)cmd->tag))
-				return SCSI_ABORT_BUSY;
+				return FAILED;
 
 			ABRT_PRINTK("scsi%d: nexus reestablished.\n", HOSTNO);
 
@@ -2791,7 +2791,7 @@ int NCR5380_abort(Scsi_Cmnd *cmd)
 					local_irq_restore(flags);
 					tmp->scsi_done(tmp);
 					falcon_release_lock_if_possible(hostdata);
-					return SCSI_ABORT_SUCCESS;
+					return SUCCESS;
 				}
 			}
 		}
@@ -2816,7 +2816,7 @@ int NCR5380_abort(Scsi_Cmnd *cmd)
 	 */
 	falcon_release_lock_if_possible(hostdata);
 
-	return SCSI_ABORT_NOT_RUNNING;
+	return FAILED;
 }
 
 
@@ -2834,7 +2834,7 @@ static int NCR5380_bus_reset(Scsi_Cmnd *cmd)
 	SETUP_HOSTDATA(cmd->device->host);
 	int i;
 	unsigned long flags;
-#if 1
+#if defined(RESET_RUN_DONE)
 	Scsi_Cmnd *connected, *disconnected_queue;
 #endif
 
@@ -2859,7 +2859,14 @@ static int NCR5380_bus_reset(Scsi_Cmnd *cmd)
 	 * through anymore ... */
 	(void)NCR5380_read(RESET_PARITY_INTERRUPT_REG);
 
-#if 1	/* XXX Should now be done by midlevel code, but it's broken XXX */
+	/* MSch 20140115 - looking at the generic NCR5380 driver, all of this
+	 * should go.
+	 * Catch-22: if we don't clear all queues, the SCSI driver lock will
+	 * not be reset by atari_scsi_reset()!
+	 */
+
+#if defined(RESET_RUN_DONE)
+	/* XXX Should now be done by midlevel code, but it's broken XXX */
 	/* XXX see below                                            XXX */
 
 	/* MSch: old-style reset: actually abort all command processing here */
@@ -2915,7 +2922,7 @@ static int NCR5380_bus_reset(Scsi_Cmnd *cmd)
 	 * the midlevel code that the reset was SUCCESSFUL, and there is no
 	 * need to 'wake up' the commands by a request_sense
 	 */
-	return SCSI_RESET_SUCCESS | SCSI_RESET_BUS_RESET;
+	return SUCCESS;
 #else /* 1 */
 
 	/* MSch: new-style reset handling: let the mid-level do what it can */
@@ -2963,6 +2970,6 @@ static int NCR5380_bus_reset(Scsi_Cmnd *cmd)
 	local_irq_restore(flags);
 
 	/* we did no complete reset of all commands, so a wakeup is required */
-	return SCSI_RESET_WAKEUP | SCSI_RESET_BUS_RESET;
+	return SUCCESS;
 #endif /* 1 */
 }
drivers/scsi/atari_scsi.c +1 -1
index cc1b013..5e19509 100644
@@ -827,7 +827,7 @@ static int atari_scsi_bus_reset(Scsi_Cmnd *cmd)
 	} else {
 		atari_turnon_irq(IRQ_MFP_FSCSI);
 	}
-	if ((rv & SCSI_RESET_ACTION) == SCSI_RESET_SUCCESS)
+	if (rv == SUCCESS)
 		falcon_release_lock_if_possible(hostdata);
 
 	return rv;
Signature · 1 line
1.7.0.4
maintainer
#28 lore raw

PATCH 3/3 m68k/atari - atari_scsi lock fixes: punt if deadlocked

In reply to Arnd Bergmann

In case a SCSI command is queued from softirq context, and another driver currently holds the ST-DMA lock, tell the SCSI midlevel to hold off queueing commands for now. Something will hopefully resume play later.

---
 drivers/scsi/atari_NCR5380.c |   12 +++++++++---
 drivers/scsi/atari_scsi.c    |   26 +++++++++++++++++++++-----
 2 files changed, 30 insertions(+), 8 deletions(-)
drivers/scsi/atari_NCR5380.c +9 -3
index 465e63d..90a90e8 100644
@@ -967,9 +967,15 @@ static int NCR5380_queue_command_lck(Scsi_Cmnd *cmd, void (*done)(Scsi_Cmnd *))
 	 * alter queues and touch the lock.
 	 */
 	if (!IS_A_TT()) {
-		/* perhaps stop command timer here */
-		falcon_get_lock();
-		/* perhaps restart command timer here */
+		/* MSch 20140119: check whether obtaining the ST-DMA lock did
+		 * succeed.
+		 * If the lock could not be acquired without risking to
+		 * deadlock, i.e. from softirq context with ST-DMA currently
+		 * otherwise locked, defer queueing further commands.
+		 */
+		if (falcon_get_lock()) {
+			return SCSI_MLQUEUE_HOST_BUSY;
+		}
 	}
 	if (!(hostdata->issue_queue) || (cmd->cmnd[0] == REQUEST_SENSE)) {
 		LIST(cmd, hostdata->issue_queue);
drivers/scsi/atari_scsi.c +21 -5
index 5e19509..2b62ce2 100644
@@ -197,7 +197,7 @@ static unsigned long atari_dma_xfer_len(unsigned long wanted_len,
 static irqreturn_t scsi_tt_intr(int irq, void *dummy);
 static irqreturn_t scsi_falcon_intr(int irq, void *dummy);
 static void falcon_release_lock_if_possible(struct NCR5380_hostdata *hostdata);
-static void falcon_get_lock(void);
+static int falcon_get_lock(void);
 #ifdef CONFIG_ATARI_SCSI_RESET_BOOT
 static void atari_scsi_reset_boot(void);
 #endif
@@ -541,24 +541,39 @@ static void falcon_release_lock_if_possible(struct NCR5380_hostdata *hostdata)
  * Complicated, complicated.... Sigh...
  */
 
-static void falcon_get_lock(void)
+static int falcon_get_lock(void)
 {
 	unsigned long flags;
 
 	if (IS_A_TT())
-		return;
+		return 0;
 
 	local_irq_save(flags);
 
 	wait_event_cmd(falcon_fairness_wait,
-		in_irq() || !falcon_got_lock || !stdma_others_waiting(),
+		in_interrupt() || !falcon_got_lock || !stdma_others_waiting(),
 		local_irq_restore(flags),
 		local_irq_save(flags));
 
 	while (!falcon_got_lock) {
 		if (in_irq())
-			panic("Falcon SCSI hasn't ST-DMA lock in interrupt");
+			panic("Falcon SCSI hasn't got ST-DMA lock in irq");
 		if (!falcon_trying_lock) {
+			if (in_interrupt()) {
+				if (stdma_islocked()) {
+					/* MSch 20140119: problem -
+					 * cannot schedule in interrupt!
+					 * Unless stdma_lock can be modified
+					 * to interruptible wait, this will
+					 * be needed to avoid deadlocking here!
+					 * Return error to indicate command
+					 * queueing would deadlock, and allow
+					 * queue_command() to stall queueing.
+					 */
+					local_irq_restore(flags);
+					return 1;
+				}
+			}
 			falcon_trying_lock = 1;
 			stdma_lock(scsi_falcon_intr, NULL);
 			falcon_got_lock = 1;
@@ -575,6 +590,7 @@ static void falcon_get_lock(void)
 	local_irq_restore(flags);
 	if (!falcon_got_lock)
 		panic("Falcon SCSI: someone stole the lock :-(\n");
+	return 0;
 }
 
 
Signature · 1 line
1.7.0.4
author maintainer
#29 lore raw

RFC PATCH 3/30 DAC960: remove sleep_on usage

In reply to Arnd Bergmann

sleep_on and its variants are going away. The use of sleep_on() in DAC960_V2_ExecuteUserCommand seems to be bogus because the command by the time we get there, the command has completed already and we just enter the timeout. Based on this interpretation, I concluded that we can replace it with a simple msleep(1000) and rearrange the code around it slightly.

The interruptible_sleep_on_timeout in DAC960_gam_ioctl seems equivalent to the race-free version using wait_event_interruptible_timeout. I left the driver to return -EINTR rather than -ERESTARTSYS to preserve the timeout behavior.

---
 drivers/block/DAC960.c | 34 ++++++++++++++++------------------
 1 file changed, 16 insertions(+), 18 deletions(-)
drivers/block/DAC960.c +16 -18
index eb39501..125d845 100644
@@ -6411,12 +6411,12 @@ static bool DAC960_V2_ExecuteUserCommand(DAC960_Controller_T *Controller,
 					.ScatterGatherSegments[0]
 					.SegmentByteCount =
 	    CommandMailbox->ControllerInfo.DataTransferSize;
-	  DAC960_ExecuteCommand(Command);
-	  while (Controller->V2.NewControllerInformation->PhysicalScanActive)
-	    {
-	      DAC960_ExecuteCommand(Command);
-	      sleep_on_timeout(&Controller->CommandWaitQueue, HZ);
-	    }
+	  while (1) {
+	    DAC960_ExecuteCommand(Command);
+	    if (!Controller->V2.NewControllerInformation->PhysicalScanActive)
+		break;
+	    msleep(1000);
+	  }
 	  DAC960_UserCritical("Discovery Completed\n", Controller);
  	}
     }
@@ -7035,18 +7035,16 @@ static long DAC960_gam_ioctl(struct file *file, unsigned int Request,
 		ErrorCode = -EFAULT;
 		break;
 	}
-	while (Controller->V2.HealthStatusBuffer->StatusChangeCounter
-	       == HealthStatusBuffer.StatusChangeCounter &&
-	       Controller->V2.HealthStatusBuffer->NextEventSequenceNumber
-	       == HealthStatusBuffer.NextEventSequenceNumber)
-	  {
-	    interruptible_sleep_on_timeout(&Controller->HealthStatusWaitQueue,
-					   DAC960_MonitoringTimerInterval);
-	    if (signal_pending(current)) {
-	    	ErrorCode = -EINTR;
-	    	break;
-	    }
-	  }
+	ErrorCode = wait_event_interruptible_timeout(Controller->HealthStatusWaitQueue,
+			!(Controller->V2.HealthStatusBuffer->StatusChangeCounter
+			    == HealthStatusBuffer.StatusChangeCounter &&
+			  Controller->V2.HealthStatusBuffer->NextEventSequenceNumber
+			    == HealthStatusBuffer.NextEventSequenceNumber),
+			DAC960_MonitoringTimerInterval);
+	if (ErrorCode == -ERESTARTSYS) {
+		ErrorCode = -EINTR;
+		break;
+	}
 	if (copy_to_user(GetHealthStatus.HealthStatusBuffer,
 			 Controller->V2.HealthStatusBuffer,
 			 sizeof(DAC960_V2_HealthStatusBuffer_T)))
Signature · 1 line
1.8.3.2
author maintainer
#30 lore raw

RFC PATCH 4/30 swim3: fix interruptible_sleep_on race

In reply to Arnd Bergmann

interruptible_sleep_on is racy and going away. This replaces the one caller in the swim3 driver with the equivalent race-free wait_event_interruptible call. Since we're here already, this also fixes the case where we get interrupted from atomic context, which used to just spin in the loop.

---
 drivers/block/swim3.c | 18 +++++++++++-------
 1 file changed, 11 insertions(+), 7 deletions(-)
drivers/block/swim3.c +11 -7
index 20e061c..c74f7b5 100644
@@ -30,6 +30,7 @@
 #include <linux/mutex.h>
 #include <linux/module.h>
 #include <linux/spinlock.h>
+#include <linux/wait.h>
 #include <asm/io.h>
 #include <asm/dbdma.h>
 #include <asm/prom.h>
@@ -840,14 +841,17 @@ static int grab_drive(struct floppy_state *fs, enum swim_state state,
 	spin_lock_irqsave(&swim3_lock, flags);
 	if (fs->state != idle && fs->state != available) {
 		++fs->wanted;
-		while (fs->state != available) {
+		/* this will enable irqs in order to sleep */
+		if (!interruptible)
+			wait_event_lock_irq(fs->wait,
+                                        fs->state == available,
+                                        swim3_lock);
+		else if (wait_event_interruptible_lock_irq(fs->wait,
+					fs->state == available,
+					swim3_lock)) {
+			--fs->wanted;
 			spin_unlock_irqrestore(&swim3_lock, flags);
-			if (interruptible && signal_pending(current)) {
-				--fs->wanted;
-				return -EINTR;
-			}
-			interruptible_sleep_on(&fs->wait);
-			spin_lock_irqsave(&swim3_lock, flags);
+			return -EINTR;
 		}
 		--fs->wanted;
 	}
Signature · 1 line
1.8.3.2
author maintainer
#31 lore raw

RFC PATCH 5/30 omap_vout: avoid sleep_on race

In reply to Arnd Bergmann

sleep_on and its variants are broken and going away soon. This changes the omap vout driver to use interruptible_sleep_on_timeout instead, which fixes potential race where the dma is complete before we schedule.

---
 drivers/media/platform/omap/omap_vout_vrfb.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/media/platform/omap/omap_vout_vrfb.c +2 -1
index cf1c437..62e7e57 100644
@@ -270,7 +270,8 @@ int omap_vout_prepare_vrfb(struct omap_vout_device *vout,
 	omap_dma_set_global_params(DMA_DEFAULT_ARB_RATE, 0x20, 0);
 
 	omap_start_dma(tx->dma_ch);
-	interruptible_sleep_on_timeout(&tx->wait, VRFB_TX_TIMEOUT);
+	wait_event_interruptible_timeout(tx->wait, tx->tx_status == 1,
+					 VRFB_TX_TIMEOUT);
 
 	if (tx->tx_status == 0) {
 		omap_stop_dma(tx->dma_ch);
Signature · 1 line
1.8.3.2
Reviewed-by on v1 5/30
#32 lore raw

Re: [PATCH, RFC 05/30] [media] omap_vout: avoid sleep_on race

Hi Arnd,

Quoted 2 lines · Arnd Bergmann

sleep_on and its variants are broken and going away soon. This changes the omap vout driver to use interruptible_sleep_on_timeout instead,

I assume you mean wait_event_interruptible_timeout here :-)

If there are no other comments, then I plan to merge this next week.

Regards,

Hans

Quoted 25 lines

which fixes potential race where the dma is complete before we schedule.

---
 drivers/media/platform/omap/omap_vout_vrfb.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/media/platform/omap/omap_vout_vrfb.c +2 -1
index cf1c437..62e7e57 100644
@@ -270,7 +270,8 @@ int omap_vout_prepare_vrfb(struct omap_vout_device *vout,
 	omap_dma_set_global_params(DMA_DEFAULT_ARB_RATE, 0x20, 0);
 
 	omap_start_dma(tx->dma_ch);
-	interruptible_sleep_on_timeout(&tx->wait, VRFB_TX_TIMEOUT);
+	wait_event_interruptible_timeout(tx->wait, tx->tx_status == 1,
+					 VRFB_TX_TIMEOUT);
 
 	if (tx->tx_status == 0) {
 		omap_stop_dma(tx->dma_ch);
author maintainer
#33 lore raw

Re: [PATCH, RFC 05/30] [media] omap_vout: avoid sleep_on race

Quoted 10 lines · Hans Verkuil
Quoted 2 lines · Arnd Bergmann

sleep_on and its variants are broken and going away soon. This changes the omap vout driver to use interruptible_sleep_on_timeout instead,

I assume you mean wait_event_interruptible_timeout here :-)

If there are no other comments, then I plan to merge this next week.

Hi Hans,

Not sure if you merged the media patches into a local tree, but I see they are not in linux-next at the moment. I'll just re-send them, but please let me know if I can drop them on my end, or better make sure your tree is in linux-next if you have already picked them up.

Arnd

#34 lore raw

Re: [PATCH, RFC 05/30] [media] omap_vout: avoid sleep_on race

Quoted 12 lines · Arnd Bergmann
Quoted 10 lines · Hans Verkuil
Quoted 2 lines · Arnd Bergmann

sleep_on and its variants are broken and going away soon. This changes the omap vout driver to use interruptible_sleep_on_timeout instead,

I assume you mean wait_event_interruptible_timeout here :-)

If there are no other comments, then I plan to merge this next week.

Quoted 7 lines

Hi Hans,

Not sure if you merged the media patches into a local tree, but I see they are not in linux-next at the moment. I'll just re-send them, but please let me know if I can drop them on my end, or better make sure your tree is in linux-next if you have already picked them up.

I've picked it up, but it has not yet been merged. Mauro has been traveling so not much has been merged recently.

Regards,

Hans

author maintainer
#35 lore raw

RFC PATCH 6/30 usbvision: remove bogus sleep_on_timeout

In reply to Arnd Bergmann

There is no reason to use sleep_on_timeout here, and we want to get rid of that interface. Use the simpler msleep_interruptible instead.

Cc Hans Verkuil <[email protected]>
Cc Mauro Carvalho Chehab <[email protected]>
---
 drivers/media/usb/usbvision/usbvision.h | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)
drivers/media/usb/usbvision/usbvision.h +1 -3
index 8a25876..eb6dc8a 100644
@@ -205,10 +205,8 @@ enum {
 
 /* Debugging aid */
 #define USBVISION_SAY_AND_WAIT(what) { \
-	wait_queue_head_t wq; \
-	init_waitqueue_head(&wq); \
 	printk(KERN_INFO "Say: %s\n", what); \
-	interruptible_sleep_on_timeout(&wq, HZ * 3); \
+	msleep_interruptible(3000); \
 }
 
 /*
Signature · 1 line
1.8.3.2
#36 lore raw

Re: [PATCH, RFC 06/30] [media] usbvision: remove bogus sleep_on_timeout

Quoted 2 lines · Arnd Bergmann

There is no reason to use sleep_on_timeout here, and we want to get rid of that interface. Use the simpler msleep_interruptible instead.

Since this define is unused anyway, lets just remove it completely.

I'll post a patch for this.

Regards,

Hans

Quoted 26 lines
Cc Hans Verkuil <[email protected]>
Cc Mauro Carvalho Chehab <[email protected]>
---
 drivers/media/usb/usbvision/usbvision.h | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)
drivers/media/usb/usbvision/usbvision.h +1 -3
index 8a25876..eb6dc8a 100644
@@ -205,10 +205,8 @@ enum {
 
 /* Debugging aid */
 #define USBVISION_SAY_AND_WAIT(what) { \
-	wait_queue_head_t wq; \
-	init_waitqueue_head(&wq); \
 	printk(KERN_INFO "Say: %s\n", what); \
-	interruptible_sleep_on_timeout(&wq, HZ * 3); \
+	msleep_interruptible(3000); \
 }
 
 /*
author maintainer
#37 lore raw

RFC PATCH 7/30 radio-cadet: avoid interruptible_sleep_on race

In reply to Arnd Bergmann

interruptible_sleep_on is racy and going away. This replaces one use in the radio-cadet driver with an open-coded wait loop that lets us check the condition under the mutex but sleep without it.

Cc Hans Verkuil <[email protected]>
Cc Mauro Carvalho Chehab <[email protected]>
---
 drivers/media/radio/radio-cadet.c | 12 ++++++++++--
 1 file changed, 10 insertions(+), 2 deletions(-)
drivers/media/radio/radio-cadet.c +10 -2
index 545c04c..67b5bbf 100644
@@ -39,6 +39,7 @@
 #include <linux/pnp.h>
 #include <linux/sched.h>
 #include <linux/io.h>		/* outb, outb_p			*/
+#include <linux/wait.h>
 #include <media/v4l2-device.h>
 #include <media/v4l2-ioctl.h>
 #include <media/v4l2-ctrls.h>
@@ -323,25 +324,32 @@ static ssize_t cadet_read(struct file *file, char __user *data, size_t count, lo
 	struct cadet *dev = video_drvdata(file);
 	unsigned char readbuf[RDS_BUFFER];
 	int i = 0;
+	DEFINE_WAIT(wait);
 
 	mutex_lock(&dev->lock);
 	if (dev->rdsstat == 0)
 		cadet_start_rds(dev);
-	if (dev->rdsin == dev->rdsout) {
+	while (1) {
+		prepare_to_wait(&dev->read_queue, &wait, TASK_INTERRUPTIBLE);
+		if (dev->rdsin != dev->rdsout)
+			break;
+
 		if (file->f_flags & O_NONBLOCK) {
 			i = -EWOULDBLOCK;
 			goto unlock;
 		}
 		mutex_unlock(&dev->lock);
-		interruptible_sleep_on(&dev->read_queue);
+		schedule();
 		mutex_lock(&dev->lock);
 	}
+
 	while (i < count && dev->rdsin != dev->rdsout)
 		readbuf[i++] = dev->rdsbuf[dev->rdsout++];
 
 	if (i && copy_to_user(data, readbuf, i))
 		i = -EFAULT;
 unlock:
+	finish_wait(&dev->read_queue, &wait);
 	mutex_unlock(&dev->lock);
 	return i;
 }
Signature · 1 line
1.8.3.2
#38 lore raw

Re: [PATCH, RFC 07/30] [media] radio-cadet: avoid interruptible_sleep_on race

Hi Arnd!

Quoted 40 lines · Arnd Bergmann

interruptible_sleep_on is racy and going away. This replaces one use in the radio-cadet driver with an open-coded wait loop that lets us check the condition under the mutex but sleep without it.

Cc Hans Verkuil <[email protected]>
Cc Mauro Carvalho Chehab <[email protected]>
---
 drivers/media/radio/radio-cadet.c | 12 ++++++++++--
 1 file changed, 10 insertions(+), 2 deletions(-)
drivers/media/radio/radio-cadet.c +7 -1
index 545c04c..67b5bbf 100644
@@ -39,6 +39,7 @@
 #include <linux/pnp.h>
 #include <linux/sched.h>
 #include <linux/io.h>		/* outb, outb_p			*/
+#include <linux/wait.h>
 #include <media/v4l2-device.h>
 #include <media/v4l2-ioctl.h>
 #include <media/v4l2-ctrls.h>
@@ -323,25 +324,32 @@ static ssize_t cadet_read(struct file *file, char __user *data, size_t count, lo
 	struct cadet *dev = video_drvdata(file);
 	unsigned char readbuf[RDS_BUFFER];
 	int i = 0;
+	DEFINE_WAIT(wait);
 
 	mutex_lock(&dev->lock);
 	if (dev->rdsstat == 0)
 		cadet_start_rds(dev);
-	if (dev->rdsin == dev->rdsout) {
+	while (1) {
+		prepare_to_wait(&dev->read_queue, &wait, TASK_INTERRUPTIBLE);
+		if (dev->rdsin != dev->rdsout)
+			break;
+

drivers/media/radio/radio-cadet.c static ssize_t cadet_read(struct file *file, char __user *data, size_t count, lo · line 345

Quoted 10 lines
drivers/media/radio/radio-cadet.c +2 -1
 		if (file->f_flags & O_NONBLOCK) {
 			i = -EWOULDBLOCK;
 			goto unlock;
 		}
 		mutex_unlock(&dev->lock);
-		interruptible_sleep_on(&dev->read_queue);
+		schedule();
 		mutex_lock(&dev->lock);
 	}
+

This seems overly complicated. Isn't it enough to replace interruptible_sleep_on by 'wait_event_interruptible(&dev->read_queue, dev->rdsin != dev->rdsout);'?

Or am I missing something subtle?

Regards,

Hans

Quoted 11 lines
drivers/media/radio/radio-cadet.c +1 -0
 	while (i < count && dev->rdsin != dev->rdsout)
 		readbuf[i++] = dev->rdsbuf[dev->rdsout++];
 
 	if (i && copy_to_user(data, readbuf, i))
 		i = -EFAULT;
 unlock:
+	finish_wait(&dev->read_queue, &wait);
 	mutex_unlock(&dev->lock);
 	return i;
 }
author maintainer
#39 lore raw

Re: [PATCH, RFC 07/30] [media] radio-cadet: avoid interruptible_sleep_on race

Quoted 29 lines · Hans Verkuil
Quoted 15 lines
drivers/media/radio/radio-cadet.c +6 -1
@@ -323,25 +324,32 @@ static ssize_t cadet_read(struct file *file, char __user *data, size_t count, lo
      struct cadet *dev = video_drvdata(file);
      unsigned char readbuf[RDS_BUFFER];
      int i = 0;
+     DEFINE_WAIT(wait);
 
      mutex_lock(&dev->lock);
      if (dev->rdsstat == 0)
              cadet_start_rds(dev);
-     if (dev->rdsin == dev->rdsout) {
+     while (1) {
+             prepare_to_wait(&dev->read_queue, &wait, TASK_INTERRUPTIBLE);
+             if (dev->rdsin != dev->rdsout)
+                     break;
+

drivers/media/radio/radio-cadet.c static ssize_t cadet_read(struct file *file, char __user *data, size_t count, lo · line 345

Quoted 10 lines
drivers/media/radio/radio-cadet.c +2 -1
              if (file->f_flags & O_NONBLOCK) {
                      i = -EWOULDBLOCK;
                      goto unlock;
              }
              mutex_unlock(&dev->lock);
-             interruptible_sleep_on(&dev->read_queue);
+             schedule();
              mutex_lock(&dev->lock);
      }
+

This seems overly complicated. Isn't it enough to replace interruptible_sleep_on by 'wait_event_interruptible(&dev->read_queue, dev->rdsin != dev->rdsout);'?

Quoted 1 line

Or am I missing something subtle?

The existing code sleeps with &dev->lock released because the cadet_handler() function needs to grab (and release) the same lock before it can wake up the reader thread.

Doing the simple wait_event_interruptible() would result in a deadlock here.

Arnd

#40 lore raw

Re: [PATCH, RFC 07/30] [media] radio-cadet: avoid interruptible_sleep_on race

Hi Arnd!

Quoted 30 lines · Arnd Bergmann
Quoted 29 lines · Hans Verkuil
Quoted 15 lines
(quoted diff) +6 -1
@@ -323,25 +324,32 @@ static ssize_t cadet_read(struct file *file, char __user *data, size_t count, lo
      struct cadet *dev = video_drvdata(file);
      unsigned char readbuf[RDS_BUFFER];
      int i = 0;
+     DEFINE_WAIT(wait);
 
      mutex_lock(&dev->lock);
      if (dev->rdsstat == 0)
              cadet_start_rds(dev);
-     if (dev->rdsin == dev->rdsout) {
+     while (1) {
+             prepare_to_wait(&dev->read_queue, &wait, TASK_INTERRUPTIBLE);
+             if (dev->rdsin != dev->rdsout)
+                     break;
+

Review static ssize_t cadet_read(struct file *file, char __user *data, size_t count, lo · line 345

Quoted 10 lines
(quoted diff) +2 -1
              if (file->f_flags & O_NONBLOCK) {
                      i = -EWOULDBLOCK;
                      goto unlock;
              }
              mutex_unlock(&dev->lock);
-             interruptible_sleep_on(&dev->read_queue);
+             schedule();
              mutex_lock(&dev->lock);
      }
+

This seems overly complicated. Isn't it enough to replace interruptible_sleep_on by 'wait_event_interruptible(&dev->read_queue, dev->rdsin != dev->rdsout);'?

Quoted 7 lines
Quoted 1 line

Or am I missing something subtle?

The existing code sleeps with &dev->lock released because the cadet_handler() function needs to grab (and release) the same lock before it can wake up the reader thread.

Doing the simple wait_event_interruptible() would result in a deadlock here.

I don't see it. I propose this patch:

drivers/media/radio/radio-cadet.c +4 -2
index 545c04c..2f658c6 100644
@@ -327,13 +327,15 @@ static ssize_t cadet_read(struct file *file, char __user *data, size_t count, lo
 	mutex_lock(&dev->lock);
 	if (dev->rdsstat == 0)
 		cadet_start_rds(dev);
-	if (dev->rdsin == dev->rdsout) {
+	while (dev->rdsin == dev->rdsout) {
 		if (file->f_flags & O_NONBLOCK) {
 			i = -EWOULDBLOCK;
 			goto unlock;
 		}
 		mutex_unlock(&dev->lock);
-		interruptible_sleep_on(&dev->read_queue);
+		if (wait_event_interruptible(&dev->read_queue,
+					     dev->rdsin != dev->rdsout))
+			return -EINTR;
 		mutex_lock(&dev->lock);
 	}
 	while (i < count && dev->rdsin != dev->rdsout)

Tested with my radio-cadet card.

This looks good to me. If I am still missing something, let me know!

Regards,

Hans

#41 lore raw

Re: [PATCH, RFC 07/30] [media] radio-cadet: avoid interruptible_sleep_on race

Quoted 52 lines · Hans Verkuil

Hi Arnd!

Quoted 30 lines · Arnd Bergmann
Quoted 29 lines · Hans Verkuil
Quoted 15 lines
drivers/media/radio/radio-cadet.c +6 -1
@@ -323,25 +324,32 @@ static ssize_t cadet_read(struct file *file, char __user *data, size_t count, lo
      struct cadet *dev = video_drvdata(file);
      unsigned char readbuf[RDS_BUFFER];
      int i = 0;
+     DEFINE_WAIT(wait);
 
      mutex_lock(&dev->lock);
      if (dev->rdsstat == 0)
              cadet_start_rds(dev);
-     if (dev->rdsin == dev->rdsout) {
+     while (1) {
+             prepare_to_wait(&dev->read_queue, &wait, TASK_INTERRUPTIBLE);
+             if (dev->rdsin != dev->rdsout)
+                     break;
+

drivers/media/radio/radio-cadet.c static ssize_t cadet_read(struct file *file, char __user *data, size_t count, lo · line 345

Quoted 10 lines
drivers/media/radio/radio-cadet.c +2 -1
              if (file->f_flags & O_NONBLOCK) {
                      i = -EWOULDBLOCK;
                      goto unlock;
              }
              mutex_unlock(&dev->lock);
-             interruptible_sleep_on(&dev->read_queue);
+             schedule();
              mutex_lock(&dev->lock);
      }
+

This seems overly complicated. Isn't it enough to replace interruptible_sleep_on by 'wait_event_interruptible(&dev->read_queue, dev->rdsin != dev->rdsout);'?

Quoted 7 lines
Quoted 1 line

Or am I missing something subtle?

The existing code sleeps with &dev->lock released because the cadet_handler() function needs to grab (and release) the same lock before it can wake up the reader thread.

Doing the simple wait_event_interruptible() would result in a deadlock here.

I don't see it. I propose this patch:

drivers/media/radio/radio-cadet.c +0 -0
index 545c04c..2f658c6 100644
@@ -327,13 +327,15 @@ static ssize_t cadet_read(struct file *file, char __user *data, size_t count, lo
 	mutex_lock(&dev->lock);
 	if (dev->rdsstat == 0)

drivers/media/radio/radio-cadet.c static ssize_t cadet_read(struct file *file, char __user *data, size_t count, lo · line 336

Quoted 10 lines
drivers/media/radio/radio-cadet.c +2 -2
 		cadet_start_rds(dev);
-	if (dev->rdsin == dev->rdsout) {
+	while (dev->rdsin == dev->rdsout) {
 		if (file->f_flags & O_NONBLOCK) {
 			i = -EWOULDBLOCK;
 			goto unlock;
 		}
 		mutex_unlock(&dev->lock);
-		interruptible_sleep_on(&dev->read_queue);
+		if (wait_event_interruptible(&dev->read_queue,

Oops, that's without an '&' before dev->read_queue. I forgot to update my patch before posting, sorry about that.

Hans

Quoted 18 lines
drivers/media/radio/radio-cadet.c +2 -0
+					     dev->rdsin != dev->rdsout))
+			return -EINTR;
 		mutex_lock(&dev->lock);
 	}
 	while (i < count && dev->rdsin != dev->rdsout)

Tested with my radio-cadet card.

This looks good to me. If I am still missing something, let me know!

Regards,

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

author maintainer
#42 lore raw

Re: [PATCH, RFC 07/30] [media] radio-cadet: avoid interruptible_sleep_on race

In reply to Hans Verkuil
Quoted 8 lines · On Friday 07 February 2014 10:32:28 Hans Verkuil wrote:
        mutex_lock(&dev->lock);
        if (dev->rdsstat == 0)
                cadet_start_rds(dev);
drivers/media/radio/radio-cadet.c +1 -1
-       if (dev->rdsin == dev->rdsout) {
+       while (dev->rdsin == dev->rdsout) {
                if (file->f_flags & O_NONBLOCK) {
                        i = -EWOULDBLOCK;

drivers/media/radio/radio-cadet.c static ssize_t cadet_read(struct file *file, char __user *data, size_t count, lo · line 341

Quoted 10 lines
drivers/media/radio/radio-cadet.c +3 -1
                        goto unlock;
                }
                mutex_unlock(&dev->lock);
-               interruptible_sleep_on(&dev->read_queue);
+               if (wait_event_interruptible(&dev->read_queue,
+                                            dev->rdsin != dev->rdsout))
+                       return -EINTR;
                mutex_lock(&dev->lock);
        }
        while (i < count && dev->rdsin != dev->rdsout)

This will normally work, but now the mutex is no longer protecting the shared access to the dev->rdsin and dev->rdsout variables, which was evidently the intention of the author of the original code.

AFAICT, the possible result is a similar race as before:
if once CPU changes dev->rdsin after the process in cadet_read dropped the lock, the wakeup may get lost.

It's quite possible this race never happens in practice, but the code is probably still wrong.

If you think we don't actually need the lock to check "dev->rdsin != dev->rdsout", the code can be simplified further, to

	if ((dev->rdsin == dev->rdsout) && (file->f_flags & O_NONBLOCK)) {
	        return -EWOULDBLOCK;
	i = wait_event_interruptible(&dev->read_queue, dev->rdsin != dev->rdsout);
	if (i)
		return i;
	
	Arnd
#43 lore raw

Re: [PATCH, RFC 07/30] [media] radio-cadet: avoid interruptible_sleep_on race

Quoted 36 lines · Arnd Bergmann
Quoted 8 lines · On Friday 07 February 2014 10:32:28 Hans Verkuil wrote:
        mutex_lock(&dev->lock);
        if (dev->rdsstat == 0)
                cadet_start_rds(dev);
(quoted diff) +1 -1
-       if (dev->rdsin == dev->rdsout) {
+       while (dev->rdsin == dev->rdsout) {
                if (file->f_flags & O_NONBLOCK) {
                        i = -EWOULDBLOCK;

Review

Quoted 10 lines
(quoted diff) +3 -1
                        goto unlock;
                }
                mutex_unlock(&dev->lock);
-               interruptible_sleep_on(&dev->read_queue);
+               if (wait_event_interruptible(&dev->read_queue,
+                                            dev->rdsin != dev->rdsout))
+                       return -EINTR;
                mutex_lock(&dev->lock);
        }
        while (i < count && dev->rdsin != dev->rdsout)

This will normally work, but now the mutex is no longer protecting the shared access to the dev->rdsin and dev->rdsout variables, which was evidently the intention of the author of the original code.

AFAICT, the possible result is a similar race as before:
if once CPU changes dev->rdsin after the process in cadet_read dropped the lock, the wakeup may get lost.

It's quite possible this race never happens in practice, but the code is probably still wrong.

If you think we don't actually need the lock to check "dev->rdsin != dev->rdsout", the code can be simplified further, to

Quoted 8 lines
	if ((dev->rdsin == dev->rdsout) && (file->f_flags & O_NONBLOCK)) {
	        return -EWOULDBLOCK;
	i = wait_event_interruptible(&dev->read_queue, dev->rdsin != dev->rdsout);
	if (i)
		return i;
	
	Arnd

OK, let's try again. This patch is getting bigger and bigger, but it is always nice to know that your ISA card that almost no one else in the world has is really, really working well. :-)

Regards,

Hans

drivers/media/radio/radio-cadet.c +27 -19
index 545c04c..d27e8b2 100644
@@ -270,6 +270,16 @@ reset_rds:
 	outb(inb(dev->io + 1) & 0x7f, dev->io + 1);
 }
 
+static bool cadet_has_rds_data(struct cadet *dev)
+{
+	bool result;
+	
+	mutex_lock(&dev->lock);
+	result = dev->rdsin != dev->rdsout;
+	mutex_unlock(&dev->lock);
+	return result;
+}
+
 
 static void cadet_handler(unsigned long data)
 {
@@ -279,13 +289,12 @@ static void cadet_handler(unsigned long data)
 	if (mutex_trylock(&dev->lock)) {
 		outb(0x3, dev->io);       /* Select RDS Decoder Control */
 		if ((inb(dev->io + 1) & 0x20) != 0)
-			printk(KERN_CRIT "cadet: RDS fifo overflow\n");
+			pr_err("cadet: RDS fifo overflow\n");
 		outb(0x80, dev->io);      /* Select RDS fifo */
+
 		while ((inb(dev->io) & 0x80) != 0) {
 			dev->rdsbuf[dev->rdsin] = inb(dev->io + 1);
-			if (dev->rdsin + 1 == dev->rdsout)
-				printk(KERN_WARNING "cadet: RDS buffer overflow\n");
-			else
+			if (dev->rdsin + 1 != dev->rdsout)
 				dev->rdsin++;
 		}
 		mutex_unlock(&dev->lock);
@@ -294,7 +303,7 @@ static void cadet_handler(unsigned long data)
 	/*
 	 * Service pending read
 	 */
-	if (dev->rdsin != dev->rdsout)
+	if (cadet_has_rds_data(dev))
 		wake_up_interruptible(&dev->read_queue);
 
 	/*
@@ -327,22 +336,21 @@ static ssize_t cadet_read(struct file *file, char __user *data, size_t count, lo
 	mutex_lock(&dev->lock);
 	if (dev->rdsstat == 0)
 		cadet_start_rds(dev);
-	if (dev->rdsin == dev->rdsout) {
-		if (file->f_flags & O_NONBLOCK) {
-			i = -EWOULDBLOCK;
-			goto unlock;
-		}
-		mutex_unlock(&dev->lock);
-		interruptible_sleep_on(&dev->read_queue);
-		mutex_lock(&dev->lock);
-	}
+	mutex_unlock(&dev->lock);
+
+	if (!cadet_has_rds_data(dev) && (file->f_flags & O_NONBLOCK))
+		return -EWOULDBLOCK;
+	i = wait_event_interruptible(dev->read_queue, cadet_has_rds_data(dev));
+	if (i)
+		return i;
+
+	mutex_lock(&dev->lock);
 	while (i < count && dev->rdsin != dev->rdsout)
 		readbuf[i++] = dev->rdsbuf[dev->rdsout++];
+	mutex_unlock(&dev->lock);
 
 	if (i && copy_to_user(data, readbuf, i))
-		i = -EFAULT;
-unlock:
-	mutex_unlock(&dev->lock);
+		return -EFAULT;
 	return i;
 }
 
@@ -352,7 +360,7 @@ static int vidioc_querycap(struct file *file, void *priv,
 {
 	strlcpy(v->driver, "ADS Cadet", sizeof(v->driver));
 	strlcpy(v->card, "ADS Cadet", sizeof(v->card));
-	strlcpy(v->bus_info, "ISA", sizeof(v->bus_info));
+	strlcpy(v->bus_info, "ISA:radio-cadet", sizeof(v->bus_info));
 	v->device_caps = V4L2_CAP_TUNER | V4L2_CAP_RADIO |
 			  V4L2_CAP_READWRITE | V4L2_CAP_RDS_CAPTURE;
 	v->capabilities = v->device_caps | V4L2_CAP_DEVICE_CAPS;
@@ -491,7 +499,7 @@ static unsigned int cadet_poll(struct file *file, struct poll_table_struct *wait
 			cadet_start_rds(dev);
 		mutex_unlock(&dev->lock);
 	}
-	if (dev->rdsin != dev->rdsout)
+	if (cadet_has_rds_data(dev))
 		res |= POLLIN | POLLRDNORM;
 	return res;
 }
author maintainer Acked-by
#44 lore raw

Re: [PATCH, RFC 07/30] [media] radio-cadet: avoid interruptible_sleep_on race

Quoted 9 lines · Hans Verkuil

OK, let's try again. This patch is getting bigger and bigger, but it is always nice to know that your ISA card that almost no one else in the world has is really, really working well. :-)

Regards,

Hans

Looks good,

author maintainer
#45 lore raw

RFC PATCH 8/30 arv: fix sleep_on race

In reply to Arnd Bergmann

interruptible_sleep_on is racy and going away. In the arv driver that race has probably never caused problems since it would require a whole video frame to be captured before the read function has a chance to go to sleep, but using wait_event_interruptible lets us kill off the old interface. In order to do this, we have to slightly adapt the meaning of the ar->start_capture field to distinguish between not having started a frame and having completed it.

---
 drivers/media/platform/arv.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/media/platform/arv.c +2 -2
index e346d32d..32f6d70 100644
@@ -307,11 +307,11 @@ static ssize_t ar_read(struct file *file, char *buf, size_t count, loff_t *ppos)
 	/*
 	 * Okay, kick AR LSI to invoke an interrupt
 	 */
-	ar->start_capture = 0;
+	ar->start_capture = -1;
 	ar_outl(arvcr1 | ARVCR1_HIEN, ARVCR1);
 	local_irq_restore(flags);
 	/* .... AR interrupts .... */
-	interruptible_sleep_on(&ar->wait);
+	wait_event_interruptible(ar->wait, ar->start_capture == 0);
 	if (signal_pending(current)) {
 		printk(KERN_ERR "arv: interrupted while get frame data.\n");
 		ret = -EINTR;
Signature · 1 line
1.8.3.2
#46 lore raw

Re: [PATCH, RFC 08/30] [media] arv: fix sleep_on race

Quoted 20 lines · Arnd Bergmann

interruptible_sleep_on is racy and going away. In the arv driver that race has probably never caused problems since it would require a whole video frame to be captured before the read function has a chance to go to sleep, but using wait_event_interruptible lets us kill off the old interface. In order to do this, we have to slightly adapt the meaning of the ar->start_capture field to distinguish between not having started a frame and having completed it.

---
 drivers/media/platform/arv.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/media/platform/arv.c +0 -0
index e346d32d..32f6d70 100644

drivers/media/platform/arv.c static ssize_t ar_read(struct file *file, char *buf, size_t count, loff_t *ppos) · line 310

Quoted 5 lines
drivers/media/platform/arv.c +1 -1
@@ -307,11 +307,11 @@ static ssize_t ar_read(struct file *file, char *buf, size_t count, loff_t *ppos)
 	/*
 	 * Okay, kick AR LSI to invoke an interrupt
 	 */
-	ar->start_capture = 0;
+	ar->start_capture = -1;

start_capture is defined as an unsigned. Can you make a new patch that changes the type of start_capture to int?

Otherwise it looks fine.

Regards,

Hans

Quoted 9 lines
drivers/media/platform/arv.c +1 -1
 	ar_outl(arvcr1 | ARVCR1_HIEN, ARVCR1);
 	local_irq_restore(flags);
 	/* .... AR interrupts .... */
-	interruptible_sleep_on(&ar->wait);
+	wait_event_interruptible(ar->wait, ar->start_capture == 0);
 	if (signal_pending(current)) {
 		printk(KERN_ERR "arv: interrupted while get frame data.\n");
 		ret = -EINTR;
#47 lore raw

Re: [PATCH, RFC 08/30] [media] arv: fix sleep_on race

Quoted 30 lines · Hans Verkuil
Quoted 20 lines · Arnd Bergmann

interruptible_sleep_on is racy and going away. In the arv driver that race has probably never caused problems since it would require a whole video frame to be captured before the read function has a chance to go to sleep, but using wait_event_interruptible lets us kill off the old interface. In order to do this, we have to slightly adapt the meaning of the ar->start_capture field to distinguish between not having started a frame and having completed it.

---
 drivers/media/platform/arv.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/media/platform/arv.c +0 -0
index e346d32d..32f6d70 100644

drivers/media/platform/arv.c static ssize_t ar_read(struct file *file, char *buf, size_t count, loff_t *ppos) · line 310

Quoted 5 lines
drivers/media/platform/arv.c +1 -1
@@ -307,11 +307,11 @@ static ssize_t ar_read(struct file *file, char *buf, size_t count, loff_t *ppos)
 	/*
 	 * Okay, kick AR LSI to invoke an interrupt
 	 */
-	ar->start_capture = 0;
+	ar->start_capture = -1;

start_capture is defined as an unsigned. Can you make a new patch that changes the type of start_capture to int?

Quoted 1 line

Otherwise it looks fine.

ping!

Regards,

Hans

author maintainer
#48 lore raw

Re: [PATCH, RFC 08/30] [media] arv: fix sleep_on race

Quoted 16 lines · Hans Verkuil
Quoted 14 lines · Hans Verkuil

drivers/media/platform/arv.c static ssize_t ar_read(struct file *file, char *buf, size_t count, loff_t *ppos) · line 310

Quoted 10 lines
drivers/media/platform/arv.c +1 -1
index e346d32d..32f6d70 100644
@@ -307,11 +307,11 @@ static ssize_t ar_read(struct file *file, char *buf, size_t count, loff_t *ppos)
 	/*
 	 * Okay, kick AR LSI to invoke an interrupt
 	 */
-	ar->start_capture = 0;
+	ar->start_capture = -1;

start_capture is defined as an unsigned. Can you make a new patch that changes the type of start_capture to int?

Quoted 1 line
Quoted 1 line

Otherwise it looks fine.

Sorry for the delay. I've updated the patch now and will send it out today with the other remaining ones.

Arnd

author maintainer
#49 lore raw

RFC PATCH 9/30 staging: serqt_usb2: don't use sleep_on

In reply to Arnd Bergmann

sleep_on and related functions are going away and should not be used in this driver any more.

This removes the call to interruptible_sleep_on for a wait queue that is never woken up, and replaces an interruptible_sleep_on_timeout call with the equivalent wait_event_interruptible_timeout() to avoid a small race.

Both call sites still look fishy and need more work.

Signed-off-by Arnd Bergmann <arndb.de>
Cc Greg Kroah-Hartman <[email protected]>
Cc Bill Pemberton <[email protected]>
---
 drivers/staging/serqt_usb2/serqt_usb2.c | 17 +++++++----------
 1 file changed, 7 insertions(+), 10 deletions(-)
drivers/staging/serqt_usb2/serqt_usb2.c +7 -10
index 73fc3cc..e0c209e 100644
@@ -970,17 +970,11 @@ static void qt_block_until_empty(struct tty_struct *tty,
 {
 	int timeout = HZ / 10;
 	int wait = 30;
-	int count;
-
-	while (1) {
-
-		count = qt_chars_in_buffer(tty);
-
-		if (count <= 0)
-			return;
-
-		interruptible_sleep_on_timeout(&qt_port->wait, timeout);
 
+	/* returns if we get a signal, an error, or the buffer is empty */
+	while (wait_event_interruptible_timeout(qt_port->wait,
+					qt_chars_in_buffer(tty) <= 0,
+					timeout) == 0) {
 		wait--;
 		if (wait == 0) {
 			dev_dbg(&qt_port->port->dev, "%s - TIMEOUT", __func__);
@@ -1137,7 +1131,10 @@ static int qt_ioctl(struct tty_struct *tty,
 
 	if (cmd == TIOCMIWAIT) {
 		while (qt_port != NULL) {
+#if 0
+			/* this never wakes up */
 			interruptible_sleep_on(&qt_port->msr_wait);
+#endif
 			if (signal_pending(current))
 				return -ERESTARTSYS;
 			else {
Signature · 1 line
1.8.3.2
author maintainer
#50 lore raw

RFC PATCH 10/30 staging: gdm72xx: fix interruptible_sleep_on race

In reply to Arnd Bergmann

interruptible_sleep_on is racy and going away. This replaces the use in the gdm72xx driver with the appropriate wait_event_interruptible_lock_irq.

---
 drivers/staging/gdm72xx/gdm_usb.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
drivers/staging/gdm72xx/gdm_usb.c +3 -2
index e0cb2ff..f8788bf 100644
@@ -780,9 +780,10 @@ static int k_mode_thread(void *arg)
 
 			spin_lock_irqsave(&k_lock, flags2);
 		}
+		wait_event_interruptible_lock_irq(k_wait,
+						  !list_empty(&k_list) || k_mode_stop,
+						  k_lock);
 		spin_unlock_irqrestore(&k_lock, flags2);
-
-		interruptible_sleep_on(&k_wait);
 	}
 	return 0;
 }
Signature · 1 line
1.8.3.2
author maintainer
#51 lore raw

RFC PATCH 11/30 staging: panel: fix interruptible_sleep_on race

In reply to Arnd Bergmann

interruptible_sleep_on is racy and going away. This replaces the one caller in the panel driver with the appropriate wait_event_interruptible variant.

Cc Willy Tarreau <[email protected]>
Cc Greg Kroah-Hartman <[email protected]>
---
 drivers/staging/panel/panel.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/staging/panel/panel.c +2 -2
index cbc15c1..ec4b1fd 100644
@@ -1590,8 +1590,8 @@ static ssize_t keypad_read(struct file *file,
 		if (file->f_flags & O_NONBLOCK)
 			return -EAGAIN;
 
-		interruptible_sleep_on(&keypad_read_wait);
-		if (signal_pending(current))
+		if (wait_event_interruptible(keypad_read_wait,
+					     keypad_buflen != 0))
 			return -EINTR;
 	}
 
Signature · 1 line
1.8.3.2
author maintainer
#52 lore raw

RFC PATCH 12/30 parport: fix interruptible_sleep_on race

In reply to Arnd Bergmann

The interruptible_sleep_on function is can still lead to the deadlock mentioned in the comment above the caller, and we want to remove it soon, so replace it now with the race-free wait_event_interruptible.

---
 drivers/parport/share.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/parport/share.c +2 -1
index 6a83ee1..3fa6624 100644
@@ -905,7 +905,8 @@ int parport_claim_or_block(struct pardevice *dev)
 		/* If dev->waiting is clear now, an interrupt
 		   gave us the port and we would deadlock if we slept.  */
 		if (dev->waiting) {
-			interruptible_sleep_on (&dev->wait_q);
+			wait_event_interruptible(dev->wait_q,
+						 !dev->waiting);
 			if (signal_pending (current)) {
 				return -EINTR;
 			}
Signature · 1 line
1.8.3.2
author maintainer
#53 lore raw

RFC PATCH 13/30 cris: sync_serial: remove interruptible_sleep_on

In reply to Arnd Bergmann

sleep_on and its variants are racy and going away. This replaces the two uses in the cris sync_serial drivers with the equivalent but race-free wait_event_interruptible.

---
 arch/cris/arch-v10/drivers/sync_serial.c | 4 +++-
 arch/cris/arch-v32/drivers/sync_serial.c | 4 +++-
 2 files changed, 6 insertions(+), 2 deletions(-)
arch/cris/arch-v10/drivers/sync_serial.c +3 -1
index a1c498d..485f2bb 100644
@@ -22,6 +22,7 @@
 #include <linux/init.h>
 #include <linux/mutex.h>
 #include <linux/timer.h>
+#include <linux/wait.h>
 #include <asm/irq.h>
 #include <asm/dma.h>
 #include <asm/io.h>
@@ -1136,7 +1137,8 @@ static ssize_t sync_serial_read(struct file *file, char *buf,
 		if (file->f_flags & O_NONBLOCK)
 			return -EAGAIN;
 
-		interruptible_sleep_on(&port->in_wait_q);
+		wait_event_interruptible(port->in_wait_q,
+					 !(start == end && !port->full));
 		if (signal_pending(current))
 			return -EINTR;
 
arch/cris/arch-v32/drivers/sync_serial.c +3 -1
index 219f704..bbb806b 100644
@@ -19,6 +19,7 @@
 #include <linux/init.h>
 #include <linux/timer.h>
 #include <linux/spinlock.h>
+#include <linux/wait.h>
 
 #include <asm/io.h>
 #include <dma.h>
@@ -1144,7 +1145,8 @@ static ssize_t sync_serial_read(struct file * file, char * buf,
 		if (file->f_flags & O_NONBLOCK)
 			return -EAGAIN;
 
-		interruptible_sleep_on(&port->in_wait_q);
+		wait_event_interruptible(port->in_wait_q,
+					 !(start == end && !port->full));
 		if (signal_pending(current))
 			return -EINTR;
 
Signature · 1 line
1.8.3.2
#54 lore raw

Re: [PATCH, RFC 13/30] cris: sync_serial: remove interruptible_sleep_on

Quoted 3 lines · Arnd Bergmann

sleep_on and its variants are racy and going away. This replaces the two uses in the cris sync_serial drivers with the equivalent but race-free wait_event_interruptible.

Looks good, pulling it into the cris tree for 3.14.

/^JN - Jesper Nilsson

Signature · 1 line
               Jesper Nilsson -- [email protected]
author maintainer
#55 lore raw

RFC PATCH 14/30 tty/amiserial: avoid interruptible_sleep_on

In reply to Arnd Bergmann

interruptible_sleep_on is generally problematic and we want to get rid of it. In case of TIOCMIWAIT, that race is actually in user space and does not get fixed since we can only detect changes after entering the ioctl handler, but it removes one more caller.

This instance can not be trivially replaced with wait_event, so I chose to open-code the wait loop using prepare_to_wait/finish_wait.

Cc Geert Uytterhoeven <[email protected]>
Cc Greg Kroah-Hartman <[email protected]>
---
 drivers/tty/amiserial.c | 26 ++++++++++++++++++--------
 1 file changed, 18 insertions(+), 8 deletions(-)
drivers/tty/amiserial.c +18 -8
index 71630a2..979e7c3 100644
@@ -1248,6 +1248,8 @@ static int rs_ioctl(struct tty_struct *tty,
 	struct async_icount cprev, cnow;	/* kernel counter temps */
 	void __user *argp = (void __user *)arg;
 	unsigned long flags;
+	DEFINE_WAIT(wait);
+	int ret;
 
 	if (serial_paranoia_check(info, tty->name, "rs_ioctl"))
 		return -ENODEV;
@@ -1288,25 +1290,33 @@ static int rs_ioctl(struct tty_struct *tty,
 			cprev = info->icount;
 			local_irq_restore(flags);
 			while (1) {
-				interruptible_sleep_on(&info->tport.delta_msr_wait);
-				/* see if a signal did it */
-				if (signal_pending(current))
-					return -ERESTARTSYS;
+				prepare_to_wait(&info->tport.delta_msr_wait,
+						&wait, TASK_INTERRUPTIBLE);
 				local_irq_save(flags);
 				cnow = info->icount; /* atomic copy */
 				local_irq_restore(flags);
 				if (cnow.rng == cprev.rng && cnow.dsr == cprev.dsr && 
-				    cnow.dcd == cprev.dcd && cnow.cts == cprev.cts)
-					return -EIO; /* no change => error */
+				    cnow.dcd == cprev.dcd && cnow.cts == cprev.cts) {
+					ret = -EIO; /* no change => error */
+					break;
+				}
 				if ( ((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
 				     ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
 				     ((arg & TIOCM_CD)  && (cnow.dcd != cprev.dcd)) ||
 				     ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts)) ) {
-					return 0;
+					ret = 0;
+					break;
+				}
+				schedule();
+				/* see if a signal did it */
+				if (signal_pending(current)) {
+					ret = -ERESTARTSYS;
+					break;
 				}
 				cprev = cnow;
 			}
-			/* NOTREACHED */
+			finish_wait(&info->tport.delta_msr_wait, &wait);
+			return ret;
 
 		case TIOCSERGWILD:
 		case TIOCSERSWILD:
Signature · 1 line
1.8.3.2
#56 lore raw

RFC PATCH 14/30 tty/amiserial: avoid interruptible_sleep_on

---------- Forwarded message ----------
From: Arnd Bergmann <[email protected]>
Date: Thu, Jan 2, 2014 at 1:07 PM
Subject: [PATCH, RFC 14/30] tty/amiserial: avoid interruptible_sleep_on
To: [email protected]
Cc: Arnd Bergmann <[email protected]>, Geert Uytterhoeven <[email protected]>, Greg Kroah-Hartman <[email protected]>

interruptible_sleep_on is generally problematic and we want to get rid of it. In case of TIOCMIWAIT, that race is actually in user space and does not get fixed since we can only detect changes after entering the ioctl handler, but it removes one more caller.

This instance can not be trivially replaced with wait_event, so I chose to open-code the wait loop using prepare_to_wait/finish_wait.

Cc Geert Uytterhoeven <[email protected]>
Cc Greg Kroah-Hartman <[email protected]>
---
 drivers/tty/amiserial.c | 26 ++++++++++++++++++--------
 1 file changed, 18 insertions(+), 8 deletions(-)
drivers/tty/amiserial.c +2 -1
index 71630a2..979e7c3 100644
@@ -1248,6 +1248,8 @@ static int rs_ioctl(struct tty_struct *tty,
        struct async_icount cprev, cnow;        /* kernel counter temps */
        void __user *argp = (void __user *)arg;
        unsigned long flags;
+       DEFINE_WAIT(wait);
+       int ret;
 
        if (serial_paranoia_check(info, tty->name, "rs_ioctl"))
                return -ENODEV;
@@ -1288,25 +1290,33 @@ static int rs_ioctl(struct tty_struct *tty,
                        cprev = info->icount;
                        local_irq_restore(flags);
                        while (1) {
-

interruptible_sleep_on(&info->tport.delta_msr_wait);
- /* see if a signal did it */
- if (signal_pending(current))
- return -ERESTARTSYS;
+ prepare_to_wait(&info->tport.delta_msr_wait,

+                                               &wait, TASK_INTERRUPTIBLE);
                                local_irq_save(flags);
                                cnow = info->icount; /* atomic copy */
                                local_irq_restore(flags);
                                if (cnow.rng == cprev.rng && cnow.dsr

== cprev.dsr &&
- cnow.dcd == cprev.dcd && cnow.cts
== cprev.cts)
- return -EIO; /* no change => error */
+ cnow.dcd == cprev.dcd && cnow.cts
== cprev.cts) {
+ ret = -EIO; /* no change => error */
+ break;
+ }

                                if ( ((arg & TIOCM_RNG) && (cnow.rng
!= cprev.rng)) ||
                                     ((arg & TIOCM_DSR) && (cnow.dsr
!= cprev.dsr)) ||
                                     ((arg & TIOCM_CD)  && (cnow.dcd
!= cprev.dcd)) ||
                                     ((arg & TIOCM_CTS) && (cnow.cts
!= cprev.cts)) ) {
-                                       return 0;
+                                       ret = 0;
+                                       break;
+                               }
+                               schedule();
+                               /* see if a signal did it */
+                               if (signal_pending(current)) {
+                                       ret = -ERESTARTSYS;
+                                       break;
                                }
                                cprev = cnow;
                        }
-                       /* NOTREACHED */
+                       finish_wait(&info->tport.delta_msr_wait, &wait);
+                       return ret;

case TIOCSERGWILD:
case TIOCSERSWILD:
--
1.8.3.2

author maintainer
#57 lore raw

RFC PATCH 15/30 usbserial: stop using interruptible_sleep_on

In reply to Arnd Bergmann

We really want to kill off interruptible_sleep_on, which is defined in a way that is always racy. There are four usb-serial drivers using it to implement their tiocmiwait() functions, which is defined in a way that always has a race when called from user space.

This patch changes the four drivers in the same way to use an open-coded prepare_to_wait/finish_wait loop to get rid of the deprecated function call, but it does not address the fundamental race.

This particular method of implementing it was chosen because it is least invasive, a better but more invasive alternative would be to use usb_serial_generic_tiocmiwait, which is something I did not dare try without access to hardware.

Cc Johan Hovold <[email protected]>
Cc Greg Kroah-Hartman <[email protected]>
---
 drivers/usb/serial/ch341.c      | 29 ++++++++++++++++--------
 drivers/usb/serial/cypress_m8.c | 49 ++++++++++++++++++++++++++---------------
 drivers/usb/serial/f81232.c     | 29 ++++++++++++++++--------
 drivers/usb/serial/pl2303.c     | 29 ++++++++++++++++--------
 4 files changed, 91 insertions(+), 45 deletions(-)
drivers/usb/serial/ch341.c +20 -9
index c2a4171..f5e0eea 100644
@@ -508,20 +508,22 @@ static int ch341_tiocmiwait(struct tty_struct *tty, unsigned long arg)
 	u8 status;
 	u8 changed;
 	u8 multi_change = 0;
+	DEFINE_WAIT(wait);
+	int ret;
 
 	spin_lock_irqsave(&priv->lock, flags);
 	prevstatus = priv->line_status;
 	priv->multi_status_change = 0;
 	spin_unlock_irqrestore(&priv->lock, flags);
 
+	ret = 0;
 	while (!multi_change) {
-		interruptible_sleep_on(&port->port.delta_msr_wait);
-		/* see if a signal did it */
-		if (signal_pending(current))
-			return -ERESTARTSYS;
+		prepare_to_wait(&port->port.delta_msr_wait, &wait, TASK_INTERRUPTIBLE);
 
-		if (port->serial->disconnected)
-			return -EIO;
+		if (port->serial->disconnected) {
+			ret = -EIO;
+			break;
+		}
 
 		spin_lock_irqsave(&priv->lock, flags);
 		status = priv->line_status;
@@ -534,12 +536,21 @@ static int ch341_tiocmiwait(struct tty_struct *tty, unsigned long arg)
 		    ((arg & TIOCM_DSR) && (changed & CH341_BIT_DSR)) ||
 		    ((arg & TIOCM_CD)  && (changed & CH341_BIT_DCD)) ||
 		    ((arg & TIOCM_CTS) && (changed & CH341_BIT_CTS))) {
-			return 0;
+			break;
 		}
+
+		schedule();
+
+		/* see if a signal did it */
+		if (signal_pending(current)) {
+			ret = -ERESTARTSYS;
+			break;
+		}
+
 		prevstatus = status;
 	}
-
-	return 0;
+	finish_wait(&port->port.delta_msr_wait, &wait);
+	return ret;
 }
 
 static int ch341_tiocmget(struct tty_struct *tty)
drivers/usb/serial/cypress_m8.c +31 -18

69 lines not on this page · raw message

drivers/usb/serial/f81232.c +20 -9
index 639a18f..935f2e5 100644
@@ -249,19 +249,20 @@ static int f81232_tiocmiwait(struct tty_struct *tty, unsigned long arg)
 	unsigned int prevstatus;
 	unsigned int status;
 	unsigned int changed;
+	DEFINE_WAIT(wait);
+	int ret;
 
 	spin_lock_irqsave(&priv->lock, flags);
 	prevstatus = priv->line_status;
 	spin_unlock_irqrestore(&priv->lock, flags);
 
 	while (1) {
-		interruptible_sleep_on(&port->port.delta_msr_wait);
-		/* see if a signal did it */
-		if (signal_pending(current))
-			return -ERESTARTSYS;
+		prepare_to_wait(&port->port.delta_msr_wait, &wait, TASK_INTERRUPTIBLE);
 
-		if (port->serial->disconnected)
-			return -EIO;
+		if (port->serial->disconnected) {
+			ret = -EIO;
+			break;
+		}
 
 		spin_lock_irqsave(&priv->lock, flags);
 		status = priv->line_status;
@@ -273,12 +274,22 @@ static int f81232_tiocmiwait(struct tty_struct *tty, unsigned long arg)
 		    ((arg & TIOCM_DSR) && (changed & UART_DSR)) ||
 		    ((arg & TIOCM_CD)  && (changed & UART_DCD)) ||
 		    ((arg & TIOCM_CTS) && (changed & UART_CTS))) {
-			return 0;
+			ret = 0;
+			break;
+		}
+
+		schedule();
+
+		/* see if a signal did it */
+		if (signal_pending(current)) {
+			ret = -ERESTARTSYS;
+			break;
 		}
+
 		prevstatus = status;
 	}
-	/* NOTREACHED */
-	return 0;
+	finish_wait(&port->port.delta_msr_wait, &wait);
+	return ret;
 }
 
 static int f81232_ioctl(struct tty_struct *tty,
drivers/usb/serial/pl2303.c +20 -9
index 1e3318d..e8f30bc 100644
@@ -594,19 +594,20 @@ static int pl2303_tiocmiwait(struct tty_struct *tty, unsigned long arg)
 	unsigned int prevstatus;
 	unsigned int status;
 	unsigned int changed;
+	DEFINE_WAIT(wait);
+	int ret;
 
 	spin_lock_irqsave(&priv->lock, flags);
 	prevstatus = priv->line_status;
 	spin_unlock_irqrestore(&priv->lock, flags);
 
 	while (1) {
-		interruptible_sleep_on(&port->port.delta_msr_wait);
-		/* see if a signal did it */
-		if (signal_pending(current))
-			return -ERESTARTSYS;
+		prepare_to_wait(&port->port.delta_msr_wait, &wait, TASK_INTERRUPTIBLE);
 
-		if (port->serial->disconnected)
-			return -EIO;
+		if (port->serial->disconnected) {
+			ret = -EIO;
+			break;
+		}
 
 		spin_lock_irqsave(&priv->lock, flags);
 		status = priv->line_status;
@@ -618,12 +619,22 @@ static int pl2303_tiocmiwait(struct tty_struct *tty, unsigned long arg)
 		    ((arg & TIOCM_DSR) && (changed & UART_DSR)) ||
 		    ((arg & TIOCM_CD)  && (changed & UART_DCD)) ||
 		    ((arg & TIOCM_CTS) && (changed & UART_CTS))) {
-			return 0;
+			ret = 0;
+			break;
+		}
+
+		schedule();
+
+		/* see if a signal did it */
+		if (signal_pending(current)) {
+			ret = -ERESTARTSYS;
+			break;
 		}
+
 		prevstatus = status;
 	}
-	/* NOTREACHED */
-	return 0;
+	finish_wait(&port->port.delta_msr_wait, &wait);
+	return ret;
 }
 
 static int pl2303_ioctl(struct tty_struct *tty,
Signature · 1 line
1.8.3.2
#58 lore raw

Re: [PATCH, RFC 15/30] usbserial: stop using interruptible_sleep_on

Hi Arnd,

Quoted 5 lines · Arnd Bergmann

We really want to kill off interruptible_sleep_on, which is defined in a way that is always racy. There are four usb-serial drivers using it to implement their tiocmiwait() functions, which is defined in a way that always has a race when called from user space.

Quoted 8 lines

This patch changes the four drivers in the same way to use an open-coded prepare_to_wait/finish_wait loop to get rid of the deprecated function call, but it does not address the fundamental race.

This particular method of implementing it was chosen because it is least invasive, a better but more invasive alternative would be to use usb_serial_generic_tiocmiwait, which is something I did not dare try without access to hardware.

I'd prefer to just fix the race once and for all. These four drivers have been on my todo list since I converted the other usb-serial drivers about a year ago. In fact, I posted a fix for f81232 last week, and I've had a fix for pl2303 brewing as part of larger series for quite some time.

I'll post a conversion series to linux-usb shortly and make sure to keep you CC:ed on the sleep_on-killing patches.

Thanks,
Johan

Quoted 10 lines
Cc Johan Hovold <[email protected]>
Cc Greg Kroah-Hartman <[email protected]>
---
 drivers/usb/serial/ch341.c      | 29 ++++++++++++++++--------
 drivers/usb/serial/cypress_m8.c | 49 ++++++++++++++++++++++++++---------------
 drivers/usb/serial/f81232.c     | 29 ++++++++++++++++--------
 drivers/usb/serial/pl2303.c     | 29 ++++++++++++++++--------
 4 files changed, 91 insertions(+), 45 deletions(-)
author maintainer
#59 lore raw

Re: [PATCH, RFC 15/30] usbserial: stop using interruptible_sleep_on

Quoted 8 lines · Johan Hovold

I'd prefer to just fix the race once and for all. These four drivers have been on my todo list since I converted the other usb-serial drivers about a year ago. In fact, I posted a fix for f81232 last week, and I've had a fix for pl2303 brewing as part of larger series for quite some time.

I'll post a conversion series to linux-usb shortly and make sure to keep you CC:ed on the sleep_on-killing patches.

Ah, excellent!

Much better to see the job done properly than me adding more hacks on top.

Arnd

author maintainer
#60 lore raw

RFC PATCH 16/30 tty: synclink: avoid sleep_on race

In reply to Arnd Bergmann

The four variants of the synclink driver use the same code in their open() callback to wait for a port in process of being closed, using interruptible_sleep_on, which is racy and going away soon.

Making things worse, these functions hold the BTM while doing so, which means that if we ever enter this code path, we cannot actually continue since the other thread that is in process of closing the port can no longer get the BTM.

This addresses both issues by using wait_event_interruptible_tty() instead.

Cc Greg Kroah-Hartman <[email protected]>
Cc Jiri Slaby <[email protected]>
---
 drivers/char/pcmcia/synclink_cs.c | 4 ++--
 drivers/tty/synclink.c            | 4 ++--
 drivers/tty/synclink_gt.c         | 4 ++--
 drivers/tty/synclinkmp.c          | 4 ++--
 4 files changed, 8 insertions(+), 8 deletions(-)
drivers/char/pcmcia/synclink_cs.c +2 -2
index d39cca6..8320abd 100644
@@ -2511,8 +2511,8 @@ static int mgslpc_open(struct tty_struct *tty, struct file * filp)
 
 	/* If port is closing, signal caller to try again */
 	if (tty_hung_up_p(filp) || port->flags & ASYNC_CLOSING){
-		if (port->flags & ASYNC_CLOSING)
-			interruptible_sleep_on(&port->close_wait);
+		wait_event_interruptible_tty(tty, port->close_wait,
+					     !(port->flags & ASYNC_CLOSING));
 		retval = ((port->flags & ASYNC_HUP_NOTIFY) ?
 			-EAGAIN : -ERESTARTSYS);
 		goto cleanup;
drivers/tty/synclink.c +2 -2
index e1ce141..5ae14b4 100644
@@ -3404,8 +3404,8 @@ static int mgsl_open(struct tty_struct *tty, struct file * filp)
 
 	/* If port is closing, signal caller to try again */
 	if (tty_hung_up_p(filp) || info->port.flags & ASYNC_CLOSING){
-		if (info->port.flags & ASYNC_CLOSING)
-			interruptible_sleep_on(&info->port.close_wait);
+		wait_event_interruptible_tty(tty, info->port.close_wait,
+				     !(info->port.flags & ASYNC_CLOSING));
 		retval = ((info->port.flags & ASYNC_HUP_NOTIFY) ?
 			-EAGAIN : -ERESTARTSYS);
 		goto cleanup;
drivers/tty/synclink_gt.c +2 -2
index 1abf946..c359a91 100644
@@ -674,8 +674,8 @@ static int open(struct tty_struct *tty, struct file *filp)
 
 	/* If port is closing, signal caller to try again */
 	if (tty_hung_up_p(filp) || info->port.flags & ASYNC_CLOSING){
-		if (info->port.flags & ASYNC_CLOSING)
-			interruptible_sleep_on(&info->port.close_wait);
+		wait_event_interruptible_tty(tty, info->port.close_wait,
+					     !(info->port.flags & ASYNC_CLOSING));
 		retval = ((info->port.flags & ASYNC_HUP_NOTIFY) ?
 			-EAGAIN : -ERESTARTSYS);
 		goto cleanup;
drivers/tty/synclinkmp.c +2 -2
index dc6e969..144202e 100644
@@ -754,8 +754,8 @@ static int open(struct tty_struct *tty, struct file *filp)
 
 	/* If port is closing, signal caller to try again */
 	if (tty_hung_up_p(filp) || info->port.flags & ASYNC_CLOSING){
-		if (info->port.flags & ASYNC_CLOSING)
-			interruptible_sleep_on(&info->port.close_wait);
+		wait_event_interruptible_tty(tty, info->port.close_wait,
+					     !(info->port.flags & ASYNC_CLOSING));
 		retval = ((info->port.flags & ASYNC_HUP_NOTIFY) ?
 			-EAGAIN : -ERESTARTSYS);
 		goto cleanup;
Signature · 1 line
1.8.3.2
author maintainer
#61 lore raw

RFC PATCH 17/30 atm: nicstar: remove interruptible_sleep_on_timeout

In reply to Arnd Bergmann

We are trying to finally kill off interruptible_sleep_on_timeout. the two uses in the nicstar driver can be trivially replaced with wait_event_interruptible_lock_irq_timeout, which prevents the wake-up race and is able to check the buffer state with scq->lock held.

---
 drivers/atm/nicstar.c | 13 ++++++-------
 1 file changed, 6 insertions(+), 7 deletions(-)
drivers/atm/nicstar.c +6 -7
index 5aca5f4..3c079eb 100644
@@ -1739,10 +1739,9 @@ static int push_scqe(ns_dev * card, vc_map * vc, scq_info * scq, ns_scqe * tbd,
 		}
 
 		scq->full = 1;
-		spin_unlock_irqrestore(&scq->lock, flags);
-		interruptible_sleep_on_timeout(&scq->scqfull_waitq,
-					       SCQFULL_TIMEOUT);
-		spin_lock_irqsave(&scq->lock, flags);
+		wait_event_interruptible_lock_irq_timeout(scq->scqfull_waitq,
+				scq->tail != scq->next, scq->lock,
+				SCQFULL_TIMEOUT);
 
 		if (scq->full) {
 			spin_unlock_irqrestore(&scq->lock, flags);
@@ -1789,10 +1788,10 @@ static int push_scqe(ns_dev * card, vc_map * vc, scq_info * scq, ns_scqe * tbd,
 			scq->full = 1;
 			if (has_run++)
 				break;
-			spin_unlock_irqrestore(&scq->lock, flags);
-			interruptible_sleep_on_timeout(&scq->scqfull_waitq,
+			wait_event_interruptible_lock_irq_timeout(scq->scqfull_waitq,
+						       scq->tail != scq->next,
+						       scq->lock,
 						       SCQFULL_TIMEOUT);
-			spin_lock_irqsave(&scq->lock, flags);
 		}
 
 		if (!scq->full) {
Signature · 1 line
1.8.3.2
author maintainer
#62 lore raw

RFC PATCH 18/30 atm: firestream: fix interruptible_sleep_on race

In reply to Arnd Bergmann

interruptible_sleep_on is racy and going away. This replaces the one use in the firestream driver with the appropriate wait_event_interruptible variant.

---
 drivers/atm/firestream.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/atm/firestream.c +2 -2
index b41c948..f43e1c1 100644
@@ -736,8 +736,8 @@ static void process_txdone_queue (struct fs_dev *dev, struct queue *q)
       
 			skb = td->skb;
 			if (skb == FS_VCC (ATM_SKB(skb)->vcc)->last_skb) {
-				wake_up_interruptible (& FS_VCC (ATM_SKB(skb)->vcc)->close_wait);
 				FS_VCC (ATM_SKB(skb)->vcc)->last_skb = NULL;
+				wake_up_interruptible (& FS_VCC (ATM_SKB(skb)->vcc)->close_wait);
 			}
 			td->dev->ntxpckts--;
 
@@ -1123,7 +1123,7 @@ static void fs_close(struct atm_vcc *atm_vcc)
 		   this sleep_on, we'll lose any reference to these packets. Memory leak!
 		   On the other hand, it's awfully convenient that we can abort a "close" that
 		   is taking too long. Maybe just use non-interruptible sleep on? -- REW */
-		interruptible_sleep_on (& vcc->close_wait);
+		wait_event_interruptible(vcc->close_wait, !vcc->last_skb);
 	}
 
 	txtp = &atm_vcc->qos.txtp;
Signature · 1 line
1.8.3.2
author maintainer
#63 lore raw

RFC PATCH 19/30 isdn: pcbit: fix interruptible_sleep_on race

In reply to Arnd Bergmann

interruptible_sleep_on is racy and going away. In case of pcbit, the driver would run into a timeout if the card is initialized before we start waiting for it. This uses wait_event to fix the race. In order to do this, the state machine handling for the timeout case has to get trivially reorganized so we actually know whether the timeout has occorred or not.

---
 drivers/isdn/pcbit/drv.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
drivers/isdn/pcbit/drv.c +3 -3
index 1eaf622..f02cc50 100644
@@ -796,6 +796,7 @@ static void set_running_timeout(unsigned long ptr)
 #endif
 	dev = (struct pcbit_dev *) ptr;
 
+	dev->l2_state = L2_DOWN;
 	wake_up_interruptible(&dev->set_running_wq);
 }
 
@@ -818,7 +819,8 @@ static int set_protocol_running(struct pcbit_dev *dev)
 
 	add_timer(&dev->set_running_timer);
 
-	interruptible_sleep_on(&dev->set_running_wq);
+	wait_event(dev->set_running_wq, dev->l2_state == L2_RUNNING ||
+					dev->l2_state == L2_DOWN);
 
 	del_timer(&dev->set_running_timer);
 
@@ -842,8 +844,6 @@ static int set_protocol_running(struct pcbit_dev *dev)
 		printk(KERN_DEBUG "pcbit: initialization failed\n");
 		printk(KERN_DEBUG "pcbit: firmware not loaded\n");
 
-		dev->l2_state = L2_DOWN;
-
 #ifdef DEBUG
 		printk(KERN_DEBUG "Bank3 = %02x\n",
 		       readb(dev->sh_mem + BANK3));
Signature · 1 line
1.8.3.2
author maintainer
#64 lore raw

RFC PATCH 20/30 isdn: hisax/elsa: fix sleep_on race in elsa FSM

In reply to Arnd Bergmann

The state machine code in the elsa driver uses interruptible_sleep_on to wait for state changes, which is racy. A closer look at the possible states reveals that it is always used to wait for getting back into ARCOFI_NOP, so we can use wait_event_interruptible instead.

---
 drivers/isdn/hisax/elsa.c     | 9 ++++++---
 drivers/isdn/hisax/elsa_ser.c | 3 ++-
 2 files changed, 8 insertions(+), 4 deletions(-)
drivers/isdn/hisax/elsa.c +6 -3
index 2be1c8a..d8ef64d 100644
@@ -509,7 +509,8 @@ static void
 set_arcofi(struct IsdnCardState *cs, int bc) {
 	cs->dc.isac.arcofi_bc = bc;
 	arcofi_fsm(cs, ARCOFI_START, &ARCOFI_COP_5);
-	interruptible_sleep_on(&cs->dc.isac.arcofi_wait);
+	wait_event_interruptible(cs->dc.isac.arcofi_wait,
+				 cs->dc.isac.arcofi_state == ARCOFI_NOP);
 }
 
 static int
@@ -528,7 +529,8 @@ check_arcofi(struct IsdnCardState *cs)
 		}
 	cs->dc.isac.arcofi_bc = 0;
 	arcofi_fsm(cs, ARCOFI_START, &ARCOFI_VERSION);
-	interruptible_sleep_on(&cs->dc.isac.arcofi_wait);
+	wait_event_interruptible(cs->dc.isac.arcofi_wait,
+				 cs->dc.isac.arcofi_state == ARCOFI_NOP);
 	if (!test_and_clear_bit(FLG_ARCOFI_ERROR, &cs->HW_Flags)) {
 		debugl1(cs, "Arcofi response received %d bytes", cs->dc.isac.mon_rxp);
 		p = cs->dc.isac.mon_rx;
@@ -595,7 +597,8 @@ check_arcofi(struct IsdnCardState *cs)
 			       Elsa_Types[cs->subtyp],
 			       cs->hw.elsa.base + 8);
 		arcofi_fsm(cs, ARCOFI_START, &ARCOFI_XOP_0);
-		interruptible_sleep_on(&cs->dc.isac.arcofi_wait);
+		wait_event_interruptible(cs->dc.isac.arcofi_wait,
+				 cs->dc.isac.arcofi_state == ARCOFI_NOP);
 		return (1);
 	}
 	return (0);
drivers/isdn/hisax/elsa_ser.c +2 -1
index 3f84dd8..a2a358c 100644
@@ -573,7 +573,8 @@ modem_l2l1(struct PStack *st, int pr, void *arg)
 		test_and_clear_bit(BC_FLG_ACTIV, &bcs->Flag);
 		bcs->cs->dc.isac.arcofi_bc = st->l1.bc;
 		arcofi_fsm(bcs->cs, ARCOFI_START, &ARCOFI_XOP_0);
-		interruptible_sleep_on(&bcs->cs->dc.isac.arcofi_wait);
+		wait_event_interruptible(bcs->cs->dc.isac.arcofi_wait,
+				 bcs->cs->dc.isac.arcofi_state == ARCOFI_NOP);
 		bcs->cs->hw.elsa.MFlag = 1;
 	} else {
 		printk(KERN_WARNING "ElsaSer: unknown pr %x\n", pr);
Signature · 1 line
1.8.3.2
author maintainer
#65 lore raw

RFC PATCH 21/30 isdn: divert, hysdn: fix interruptible_sleep_on race

In reply to Arnd Bergmann

These two drivers use identical code for their procfs status file handling, which contains a small race against status data becoming available while reading the file.

This uses wait_event_interruptible instead to fix this particular race and eventually get rid of all sleep_on instances. There seems to be another race involving multiple concurrent readers of the same procfs file, which I don't try to fix here.

---
 drivers/isdn/divert/divert_procfs.c | 7 ++++---
 drivers/isdn/hysdn/hysdn_proclog.c  | 7 ++++---
 2 files changed, 8 insertions(+), 6 deletions(-)
drivers/isdn/divert/divert_procfs.c +4 -3
index fb4f1ba..1c5dc34 100644
@@ -86,12 +86,13 @@ isdn_divert_read(struct file *file, char __user *buf, size_t count, loff_t *off)
 	struct divert_info *inf;
 	int len;
 
-	if (!*((struct divert_info **) file->private_data)) {
+	if (!(inf = *((struct divert_info **) file->private_data))) {
 		if (file->f_flags & O_NONBLOCK)
 			return -EAGAIN;
-		interruptible_sleep_on(&(rd_queue));
+		wait_event_interruptible(rd_queue, (inf =
+			*((struct divert_info **) file->private_data)));
 	}
-	if (!(inf = *((struct divert_info **) file->private_data)))
+	if (!inf)
 		return (0);
 
 	inf->usage_cnt--;	/* new usage count */
drivers/isdn/hysdn/hysdn_proclog.c +4 -3
index b61e8d5..7b5fd8f 100644
@@ -175,14 +175,15 @@ hysdn_log_read(struct file *file, char __user *buf, size_t count, loff_t *off)
 	int len;
 	hysdn_card *card = PDE_DATA(file_inode(file));
 
-	if (!*((struct log_data **) file->private_data)) {
+	if (!(inf = *((struct log_data **) file->private_data))) {
 		struct procdata *pd = card->proclog;
 		if (file->f_flags & O_NONBLOCK)
 			return (-EAGAIN);
 
-		interruptible_sleep_on(&(pd->rd_queue));
+		wait_event_interruptible(pd->rd_queue, (inf =
+				*((struct log_data **) file->private_data)));
 	}
-	if (!(inf = *((struct log_data **) file->private_data)))
+	if (!inf)
 		return (0);
 
 	inf->usage_cnt--;	/* new usage count */
Signature · 1 line
1.8.3.2
#66 lore raw

Re: [PATCH, RFC 21/30] isdn: divert, hysdn: fix interruptible_sleep_on race

Hello.

Quoted 23 lines · Arnd Bergmann

These two drivers use identical code for their procfs status file handling, which contains a small race against status data becoming available while reading the file.

This uses wait_event_interruptible instead to fix this particular race and eventually get rid of all sleep_on instances. There seems to be another race involving multiple concurrent readers of the same procfs file, which I don't try to fix here.

---
  drivers/isdn/divert/divert_procfs.c | 7 ++++---
  drivers/isdn/hysdn/hysdn_proclog.c  | 7 ++++---
  2 files changed, 8 insertions(+), 6 deletions(-)
drivers/isdn/divert/divert_procfs.c +0 -0
index fb4f1ba..1c5dc34 100644

drivers/isdn/divert/divert_procfs.c isdn_divert_read(struct file *file, char __user *buf, size_t count, loff_t *off) · line 89

Quoted 5 lines
drivers/isdn/divert/divert_procfs.c +1 -1
@@ -86,12 +86,13 @@ isdn_divert_read(struct file *file, char __user *buf, size_t count, loff_t *off)
  	struct divert_info *inf;
  	int len;
 
-	if (!*((struct divert_info **) file->private_data)) {
+	if (!(inf = *((struct divert_info **) file->private_data))) {

checkpatch.pl shouldn't approve assignment inside *if*. Though you're
moving it from the existing code, it wouldn't hurt to fix it.

drivers/isdn/divert/divert_procfs.c isdn_divert_read(struct file *file, char __user *buf, size_t count, loff_t *off) · line 93

Quoted 5 lines
drivers/isdn/divert/divert_procfs.c +2 -1
  		if (file->f_flags & O_NONBLOCK)
  			return -EAGAIN;
-		interruptible_sleep_on(&(rd_queue));
+		wait_event_interruptible(rd_queue, (inf =
+			*((struct divert_info **) file->private_data)));

Parens around assignment are hardly useful.

Quoted 11 lines
drivers/isdn/divert/divert_procfs.c +1 -1
  	}
-	if (!(inf = *((struct divert_info **) file->private_data)))
+	if (!inf)
  		return (0);
 
  	inf->usage_cnt--;	/* new usage count */
drivers/isdn/hysdn/hysdn_proclog.c +0 -0
index b61e8d5..7b5fd8f 100644

drivers/isdn/hysdn/hysdn_proclog.c hysdn_log_read(struct file *file, char __user *buf, size_t count, loff_t *off) · line 178

Quoted 5 lines
drivers/isdn/hysdn/hysdn_proclog.c +1 -1
@@ -175,14 +175,15 @@ hysdn_log_read(struct file *file, char __user *buf, size_t count, loff_t *off)
  	int len;
  	hysdn_card *card = PDE_DATA(file_inode(file));
 
-	if (!*((struct log_data **) file->private_data)) {
+	if (!(inf = *((struct log_data **) file->private_data))) {

Here too checkpatch.pk should complain...

drivers/isdn/hysdn/hysdn_proclog.c hysdn_log_read(struct file *file, char __user *buf, size_t count, loff_t *off) · line 184

Quoted 7 lines
drivers/isdn/hysdn/hysdn_proclog.c +2 -1
  		struct procdata *pd = card->proclog;
  		if (file->f_flags & O_NONBLOCK)
  			return (-EAGAIN);
 
-		interruptible_sleep_on(&(pd->rd_queue));
+		wait_event_interruptible(pd->rd_queue, (inf =
+				*((struct log_data **) file->private_data)));

And parens hardly needed.

WBR, Sergei

author maintainer
#67 lore raw

Re: [PATCH, RFC 21/30] isdn: divert, hysdn: fix interruptible_sleep_on race

Quoted 8 lines · On Thursday 02 January 2014 19:01:15 Sergei Shtylyov wrote:
Quoted 8 lines
drivers/isdn/divert/divert_procfs.c +0 -0
index fb4f1ba..1c5dc34 100644
@@ -86,12 +86,13 @@ isdn_divert_read(struct file *file, char __user *buf, size_t count, loff_t *off)
      struct divert_info *inf;
      int len;
 
Quoted 5 lines

drivers/isdn/divert/divert_procfs.c isdn_divert_read(struct file *file, char __user *buf, size_t count, loff_t *off) · line 89

Quoted 2 lines
drivers/isdn/divert/divert_procfs.c +1 -1
-     if (!*((struct divert_info **) file->private_data)) {
+     if (!(inf = *((struct divert_info **) file->private_data))) {

checkpatch.pl shouldn't approve assignment inside *if*. Though you're
moving it from the existing code, it wouldn't hurt to fix it.

I tried to touch as little as possible, and while I wouldn't use that style myself, it is applied consistently in this driver, including the wait_event line I'm adding, where I feel it actually makes sense.

Quoted 7 lines

drivers/isdn/divert/divert_procfs.c isdn_divert_read(struct file *file, char __user *buf, size_t count, loff_t *off) · line 93

Quoted 5 lines
drivers/isdn/divert/divert_procfs.c +2 -1
              if (file->f_flags & O_NONBLOCK)
                      return -EAGAIN;
-             interruptible_sleep_on(&(rd_queue));
+             wait_event_interruptible(rd_queue, (inf =
+                     *((struct divert_info **) file->private_data)));

Parens around assignment are hardly useful.

We get a gcc warning without them:

drivers/isdn/divert/divert_procfs.c:95:14: warning: suggest parentheses around assignment used as truth value [-Wparentheses]

    *((struct divert_info **) file->private_data));

I can still change the first one (in both files) if you think it's important, but I'd rather not spend too much energy at coding style changes.

Thanks for taking a look.

Arnd

#68 lore raw

Re: [PATCH, RFC 21/30] isdn: divert, hysdn: fix interruptible_sleep_on race

Hello.

Quoted 14 lines · Arnd Bergmann
Quoted 8 lines
Quoted 8 lines
drivers/isdn/divert/divert_procfs.c +0 -0
index fb4f1ba..1c5dc34 100644
@@ -86,12 +86,13 @@ isdn_divert_read(struct file *file, char __user *buf, size_t count, loff_t *off)
       struct divert_info *inf;
       int len;
 
Quoted 5 lines

drivers/isdn/divert/divert_procfs.c isdn_divert_read(struct file *file, char __user *buf, size_t count, loff_t *off) · line 89

Quoted 2 lines
drivers/isdn/divert/divert_procfs.c +1 -1
-     if (!*((struct divert_info **) file->private_data)) {
+     if (!(inf = *((struct divert_info **) file->private_data))) {

checkpatch.pl shouldn't approve assignment inside *if*. Though you're
moving it from the existing code, it wouldn't hurt to fix it.

Quoted 3 lines

I tried to touch as little as possible, and while I wouldn't use that style myself, it is applied consistently in this driver, including the wait_event line I'm adding, where I feel it actually makes sense.

I wasn't feeling sure about that one. It seems to me now that it doesn't
make much sense -- we could do the assignment beforehand.

Quoted 6 lines
Quoted 5 lines
Quoted 5 lines
drivers/isdn/divert/divert_procfs.c +2 -1
               if (file->f_flags & O_NONBLOCK)
                       return -EAGAIN;
-             interruptible_sleep_on(&(rd_queue));
+             wait_event_interruptible(rd_queue, (inf =
+                     *((struct divert_info **) file->private_data)));
Quoted 6 lines
Quoted 1 line

Parens around assignment are hardly useful.

We get a gcc warning without them:

drivers/isdn/divert/divert_procfs.c:95:14: warning: suggest parentheses around assignment used as truth value [-Wparentheses]

     *((struct divert_info **) file->private_data));

Ah...

Quoted 1 line

I can still change the first one (in both files) if you think it's important,

I always prefer checkpatch.pl-clean patches, though some people do argue
when they are just moving the badly styled code around.

Quoted 1 line

but I'd rather not spend too much energy at coding style changes.

Let's wait for the maintainer's opinion on this.

Quoted 1 line

Arnd

WBR, Sergei

author maintainer
#69 lore raw

RFC PATCH 22/30 isdn: fix multiple sleep_on races

In reply to Arnd Bergmann

The isdn core code uses a couple of wait queues with interruptible_sleep_on, which is racy and about to get removed from the kernel. Fortunately, we know for each case what we are waiting for, so they can all be converted to the better wait_event_interruptible interface.

---
 drivers/isdn/i4l/isdn_common.c | 13 ++++++++-----
 1 file changed, 8 insertions(+), 5 deletions(-)
drivers/isdn/i4l/isdn_common.c +8 -5
index 9bb12ba..130f216 100644
@@ -777,7 +777,8 @@ isdn_readbchan(int di, int channel, u_char *buf, u_char *fp, int len, wait_queue
 		return 0;
 	if (skb_queue_empty(&dev->drv[di]->rpqueue[channel])) {
 		if (sleep)
-			interruptible_sleep_on(sleep);
+			wait_event_interruptible(*sleep,
+				!skb_queue_empty(&dev->drv[di]->rpqueue[channel]));
 		else
 			return 0;
 	}
@@ -1072,7 +1073,8 @@ isdn_read(struct file *file, char __user *buf, size_t count, loff_t *off)
 				retval = -EAGAIN;
 				goto out;
 			}
-			interruptible_sleep_on(&(dev->info_waitq));
+			wait_event_interruptible(dev->info_waitq,
+						 file->private_data);
 		}
 		p = isdn_statstr();
 		file->private_data = NULL;
@@ -1128,7 +1130,8 @@ isdn_read(struct file *file, char __user *buf, size_t count, loff_t *off)
 				retval = -EAGAIN;
 				goto out;
 			}
-			interruptible_sleep_on(&(dev->drv[drvidx]->st_waitq));
+			wait_event_interruptible(dev->drv[drvidx]->st_waitq,
+						 dev->drv[drvidx]->stavail);
 		}
 		if (dev->drv[drvidx]->interface->readstat) {
 			if (count > dev->drv[drvidx]->stavail)
@@ -1188,8 +1191,8 @@ isdn_write(struct file *file, const char __user *buf, size_t count, loff_t *off)
 			goto out;
 		}
 		chidx = isdn_minor2chan(minor);
-		while ((retval = isdn_writebuf_stub(drvidx, chidx, buf, count)) == 0)
-			interruptible_sleep_on(&dev->drv[drvidx]->snd_waitq[chidx]);
+		wait_event_interruptible(dev->drv[drvidx]->snd_waitq[chidx],
+			(retval = isdn_writebuf_stub(drvidx, chidx, buf, count)));
 		goto out;
 	}
 	if (minor <= ISDN_MINOR_CTRLMAX) {
Signature · 1 line
1.8.3.2
author maintainer
#70 lore raw

RFC PATCH 23/30 oss: msnd_pinnacle: avoid interruptible_sleep_on_timeout

In reply to Arnd Bergmann

We want to remove all sleep_on variants from the kernel because they are racy. In case of the pinnacle driver, we can replace interruptible_sleep_on_timeout with wait_event_interruptible_timeout by changing the meaning of a few flags used in the driver so they are cleared at wakeup time, which is a somewhat more appropriate way to do the same, although probably still racy.

Cc Andrew Veliath <[email protected]>
Cc Jaroslav Kysela <[email protected]>
Cc Takashi Iwai <[email protected]>
---
 sound/oss/msnd_pinnacle.c | 31 ++++++++++++++++++-------------
 1 file changed, 18 insertions(+), 13 deletions(-)
sound/oss/msnd_pinnacle.c +18 -13
index 11ff7c5..c23f9f9 100644
@@ -664,12 +664,15 @@ static long dev_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
 
 static void dsp_write_flush(void)
 {
+	int timeout = get_play_delay_jiffies(dev.DAPF.len);
+
 	if (!(dev.mode & FMODE_WRITE) || !test_bit(F_WRITING, &dev.flags))
 		return;
 	set_bit(F_WRITEFLUSH, &dev.flags);
-	interruptible_sleep_on_timeout(
-		&dev.writeflush,
-		get_play_delay_jiffies(dev.DAPF.len));
+	wait_event_interruptible_timeout(
+		dev.writeflush,
+		!test_bit(F_WRITEFLUSH, &dev.flags),
+		timeout);
 	clear_bit(F_WRITEFLUSH, &dev.flags);
 	if (!signal_pending(current)) {
 		current->state = TASK_INTERRUPTIBLE;
@@ -897,6 +900,7 @@ static int dsp_read(char __user *buf, size_t len)
 {
 	int count = len;
 	char *page = (char *)__get_free_page(GFP_KERNEL);
+	int timeout = get_rec_delay_jiffies(DAR_BUFF_SIZE);
 
 	if (!page)
 		return -ENOMEM;
@@ -936,11 +940,11 @@ static int dsp_read(char __user *buf, size_t len)
 
 		if (count > 0) {
 			set_bit(F_READBLOCK, &dev.flags);
-			if (!interruptible_sleep_on_timeout(
-				&dev.readblock,
-				get_rec_delay_jiffies(DAR_BUFF_SIZE)))
+			if (wait_event_interruptible_timeout(
+					dev.readblock,
+					test_bit(F_READBLOCK, &dev.flags),
+					timeout) <= 0)
 				clear_bit(F_READING, &dev.flags);
-			clear_bit(F_READBLOCK, &dev.flags);
 			if (signal_pending(current)) {
 				free_page((unsigned long)page);
 				return -EINTR;
@@ -955,6 +959,7 @@ static int dsp_write(const char __user *buf, size_t len)
 {
 	int count = len;
 	char *page = (char *)__get_free_page(GFP_KERNEL);
+	int timeout = get_play_delay_jiffies(DAP_BUFF_SIZE);
 
 	if (!page)
 		return -ENOMEM;
@@ -995,10 +1000,10 @@ static int dsp_write(const char __user *buf, size_t len)
 
 		if (count > 0) {
 			set_bit(F_WRITEBLOCK, &dev.flags);
-			interruptible_sleep_on_timeout(
-				&dev.writeblock,
-				get_play_delay_jiffies(DAP_BUFF_SIZE));
-			clear_bit(F_WRITEBLOCK, &dev.flags);
+			wait_event_interruptible_timeout(
+				dev.writeblock,
+				test_bit(F_WRITEBLOCK, &dev.flags),
+				timeout);
 			if (signal_pending(current)) {
 				free_page((unsigned long)page);
 				return -EINTR;
@@ -1044,7 +1049,7 @@ static __inline__ void eval_dsp_msg(register WORD wMessage)
 			clear_bit(F_WRITING, &dev.flags);
 		}
 
-		if (test_bit(F_WRITEBLOCK, &dev.flags))
+		if (test_and_clear_bit(F_WRITEBLOCK, &dev.flags))
 			wake_up_interruptible(&dev.writeblock);
 		break;
 
@@ -1055,7 +1060,7 @@ static __inline__ void eval_dsp_msg(register WORD wMessage)
 
 		pack_DARQ_to_DARF(dev.last_recbank);
 
-		if (test_bit(F_READBLOCK, &dev.flags))
+		if (test_and_clear_bit(F_READBLOCK, &dev.flags))
 			wake_up_interruptible(&dev.readblock);
 		break;
 
Signature · 1 line
1.8.3.2
#71 lore raw

Re: [PATCH, RFC 23/30] oss: msnd_pinnacle: avoid interruptible_sleep_on_timeout

At Thu, 2 Jan 2014 13:07:47 +0100,

Quoted 8 lines · Arnd Bergmann

We want to remove all sleep_on variants from the kernel because they are racy. In case of the pinnacle driver, we can replace interruptible_sleep_on_timeout with wait_event_interruptible_timeout by changing the meaning of a few flags used in the driver so they are cleared at wakeup time, which is a somewhat more appropriate way to do the same, although probably still racy.

Quoted 5 lines
Cc Andrew Veliath <[email protected]>
Cc Jaroslav Kysela <[email protected]>
Cc Takashi Iwai <[email protected]>

I applied all 5 patches (23, 25, 24, 26, and 27) for sound/oss/*, some with trivial space fixes. Thanks!

Takashi

author maintainer
#72 lore raw

RFC PATCH 24/30 oss: midibuf: fix sleep_on races

In reply to Arnd Bergmann

sleep_on is known to be racy and going away because of this. All instances of interruptible_sleep_on and interruptible_sleep_on_timeout in the midibuf driver can trivially be replaced with wait_event_interruptible and wait_event_interruptible_timeout.

---
 sound/oss/midibuf.c | 18 +++++++++---------
 1 file changed, 9 insertions(+), 9 deletions(-)
sound/oss/midibuf.c +9 -9
index 8cdb2cf..79615cf 100644
@@ -86,9 +86,8 @@ static void drain_midi_queue(int dev)
 	 */
 
 	if (midi_devs[dev]->buffer_status != NULL)
-		while (!signal_pending(current) && midi_devs[dev]->buffer_status(dev)) 
-			interruptible_sleep_on_timeout(&midi_sleeper[dev],
-						       HZ/10);
+		wait_event_interruptible_timeout(midi_sleeper[dev],
+				!midi_devs[dev]->buffer_status(dev), HZ/10);
 }
 
 static void midi_input_intr(int dev, unsigned char data)
@@ -233,8 +232,8 @@ void MIDIbuf_release(int dev, struct file *file)
 							   * devices
 							 */
 
-		while (!signal_pending(current) && DATA_AVAIL(midi_out_buf[dev]))
-			  interruptible_sleep_on(&midi_sleeper[dev]);
+		wait_event_interruptible(midi_sleeper[dev],
+					 !DATA_AVAIL(midi_out_buf[dev]));
 		/*
 		 *	Sync
 		 */
@@ -282,8 +281,8 @@ int MIDIbuf_write(int dev, struct file *file, const char __user *buf, int count)
 				goto out;
 			}
 
-			interruptible_sleep_on(&midi_sleeper[dev]);
-			if (signal_pending(current)) 
+			if (wait_event_interruptible(midi_sleeper[dev],
+						SPACE_AVAIL(midi_out_buf[dev])))
 			{
 				c = -EINTR;
 				goto out;
@@ -325,8 +324,9 @@ int MIDIbuf_read(int dev, struct file *file, char __user *buf, int count)
  			c = -EAGAIN;
 			goto out;
  		}
-		interruptible_sleep_on_timeout(&input_sleeper[dev],
-					       parms[dev].prech_timeout);
+		wait_event_interruptible_timeout(input_sleeper[dev],
+						 DATA_AVAIL(midi_in_buf[dev]),
+					         parms[dev].prech_timeout);
 
 		if (signal_pending(current))
 			c = -EINTR;	/* The user is getting restless */
Signature · 1 line
1.8.3.2
author maintainer
#73 lore raw

RFC PATCH 25/30 oss: vwsnd: avoid interruptible_sleep_on

In reply to Arnd Bergmann

Interruptible_sleep_on is racy and we want to remove it. This replaces the use in the vwsnd driver with an open-coded prepare_to_wait loop that fixes the race between concurrent open() and close() calls, and also drops the global mutex while waiting here, which restores the original behavior that was changed during the BKL removal.

---
 sound/oss/vwsnd.c | 14 ++++++++++----
 1 file changed, 10 insertions(+), 4 deletions(-)
sound/oss/vwsnd.c +10 -4
index 4bbcc0f..a077e9c 100644
@@ -2921,6 +2921,7 @@ static int vwsnd_audio_open(struct inode *inode, struct file *file)
 	vwsnd_dev_t *devc;
 	int minor = iminor(inode);
 	int sw_samplefmt;
+	DEFINE_WAIT(wait);
 
 	DBGE("(inode=0x%p, file=0x%p)\n", inode, file);
 
@@ -2937,21 +2938,26 @@ static int vwsnd_audio_open(struct inode *inode, struct file *file)
 	}
 
 	mutex_lock(&devc->open_mutex);
-	while (devc->open_mode & file->f_mode) {
+	while (1) {
+		prepare_to_wait(&devc->open_wait, &wait, TASK_INTERRUPTIBLE);
+		if (!(devc->open_mode & file->f_mode))
+			break;
+
 		mutex_unlock(&devc->open_mutex);
+		mutex_unlock(&vwsnd_mutex);
 		if (file->f_flags & O_NONBLOCK) {
 			DEC_USE_COUNT;
-			mutex_unlock(&vwsnd_mutex);
 			return -EBUSY;
 		}
-		interruptible_sleep_on(&devc->open_wait);
+		schedule();
 		if (signal_pending(current)) {
 			DEC_USE_COUNT;
-			mutex_unlock(&vwsnd_mutex);
 			return -ERESTARTSYS;
 		}
+		mutex_lock(&vwsnd_mutex);
 		mutex_lock(&devc->open_mutex);
 	}
+	finish_wait(&devc->open_wait, &wait);
 	devc->open_mode |= file->f_mode & (FMODE_READ | FMODE_WRITE);
 	mutex_unlock(&devc->open_mutex);
 
Signature · 1 line
1.8.3.2
author maintainer
#74 lore raw

RFC PATCH 26/30 oss: dmasound: kill SLEEP() macro to avoid race

In reply to Arnd Bergmann

The use of interruptible_sleep_on_timeout in the dmasound driver is questionable and we want to kill off all sleep_on variants. This replaces the calls with wait_event_interruptible_timeout where possible, to wait for a particular event instead of blocking in a racy way. In the sq_write function, the easiest solution is an open-coded prepare_to_wait loop.

---
 sound/oss/dmasound/dmasound.h      |  1 -
 sound/oss/dmasound/dmasound_core.c | 28 +++++++++++++++++++---------
 2 files changed, 19 insertions(+), 10 deletions(-)
sound/oss/dmasound/dmasound.h +0 -1
index 1308d8d..01019f0 100644
@@ -239,7 +239,6 @@ struct sound_queue {
     int busy, syncing, xruns, died;
 };
 
-#define SLEEP(queue)		interruptible_sleep_on_timeout(&queue, HZ)
 #define WAKE_UP(queue)		(wake_up_interruptible(&queue))
 
 extern struct sound_queue dmasound_write_sq;
sound/oss/dmasound/dmasound_core.c +19 -9
index bac43b5..f4ee85a 100644
@@ -619,15 +619,27 @@ static ssize_t sq_write(struct file *file, const char __user *src, size_t uLeft,
 	}
 
 	while (uLeft) {
+		DEFINE_WAIT(wait);
+
 		while (write_sq.count >= write_sq.max_active) {
+			prepare_to_wait(&write_sq.action_queue, &wait, TASK_INTERRUPTIBLE);
 			sq_play();
-			if (write_sq.non_blocking)
+			if (write_sq.non_blocking) {
+				finish_wait(&write_sq.action_queue, &wait);
 				return uWritten > 0 ? uWritten : -EAGAIN;
-			SLEEP(write_sq.action_queue);
-			if (signal_pending(current))
+			}
+			if (write_sq.count < write_sq.max_active)
+				break;
+
+			schedule_timeout(HZ);
+			if (signal_pending(current)) {
+				finish_wait(&write_sq.action_queue, &wait);
 				return uWritten > 0 ? uWritten : -EINTR;
+			}
 		}
 
+		finish_wait(&write_sq.action_queue, &wait);
+
 		/* Here, we can avoid disabling the interrupt by first
 		 * copying and translating the data, and then updating
 		 * the write_sq variables. Until this is done, the interrupt
@@ -707,11 +719,8 @@ static int sq_open2(struct sound_queue *sq, struct file *file, fmode_t mode,
 			if (file->f_flags & O_NONBLOCK)
 				return rc;
 			rc = -EINTR;
-			while (sq->busy) {
-				SLEEP(sq->open_queue);
-				if (signal_pending(current))
-					return rc;
-			}
+			if (wait_event_interruptible(sq->open_queue, !sq->busy))
+				return rc;
 			rc = 0;
 #else
 			/* OSS manual says we will return EBUSY regardless
@@ -844,7 +853,8 @@ static int sq_fsync(void)
 	sq_play();	/* there may be an incomplete frame waiting */
 
 	while (write_sq.active) {
-		SLEEP(write_sq.sync_queue);
+		wait_event_interruptible_timeout(write_sq.sync_queue,
+						 !write_sq.active, HZ);
 		if (signal_pending(current)) {
 			/* While waiting for audio output to drain, an
 			 * interrupt occurred.  Stop audio output immediately
Signature · 1 line
1.8.3.2
author maintainer
#75 lore raw

RFC PATCH 27/30 oss: remove last sleep_on users

In reply to Arnd Bergmann

There are three files in oss for which I could not find an easy way to replace interruptible_sleep_on_timeout with a non-racy version. This patch instead just adds a private implementation of the function, now named oss_broken_sleep_on, and changes over the remaining users in sound/oss/ so we can remove the global interface.

---
 sound/oss/dmabuf.c        | 14 ++++++--------
 sound/oss/sequencer.c     | 16 +++++++---------
 sound/oss/sleep.h         | 18 ++++++++++++++++++
 sound/oss/swarm_cs4297a.c | 14 ++++++++------
 4 files changed, 39 insertions(+), 23 deletions(-)
 create mode 100644 sound/oss/sleep.h
sound/oss/dmabuf.c +6 -8
index 461d94c..e3f2913 100644
@@ -28,6 +28,7 @@
 #include <linux/mm.h>
 #include <linux/gfp.h>
 #include "sound_config.h"
+#include "sleep.h"
 
 #define DMAP_FREE_ON_CLOSE      0
 #define DMAP_KEEP_ON_CLOSE      1
@@ -351,8 +352,7 @@ static void dma_reset_output(int dev)
 	if (!signal_pending(current) && adev->dmap_out->qlen && 
 	    adev->dmap_out->underrun_count == 0){
 		spin_unlock_irqrestore(&dmap->lock,flags);
-		interruptible_sleep_on_timeout(&adev->out_sleeper,
-					       dmabuf_timeout(dmap));
+		oss_broken_sleep_on(&adev->out_sleeper, dmabuf_timeout(dmap));
 		spin_lock_irqsave(&dmap->lock,flags);
 	}
 	adev->dmap_out->flags &= ~(DMA_SYNCING | DMA_ACTIVE);
@@ -446,7 +446,7 @@ int DMAbuf_sync(int dev)
 			long t = dmabuf_timeout(dmap);
 			spin_unlock_irqrestore(&dmap->lock,flags);
 			/* FIXME: not safe may miss events */
-			t = interruptible_sleep_on_timeout(&adev->out_sleeper, t);
+			t = oss_broken_sleep_on(&adev->out_sleeper, t);
 			spin_lock_irqsave(&dmap->lock,flags);
 			if (!t) {
 				adev->dmap_out->flags &= ~DMA_SYNCING;
@@ -466,7 +466,7 @@ int DMAbuf_sync(int dev)
 			while (!signal_pending(current) &&
 			       adev->d->local_qlen(dev)){
 				spin_unlock_irqrestore(&dmap->lock,flags);
-				interruptible_sleep_on_timeout(&adev->out_sleeper,
+				oss_broken_sleep_on(&adev->out_sleeper,
 							       dmabuf_timeout(dmap));
 				spin_lock_irqsave(&dmap->lock,flags);
 			}
@@ -587,8 +587,7 @@ int DMAbuf_getrdbuffer(int dev, char **buf, int *len, int dontblock)
 			timeout = dmabuf_timeout(dmap);
 
 		spin_unlock_irqrestore(&dmap->lock,flags);
-		timeout = interruptible_sleep_on_timeout(&adev->in_sleeper,
-							 timeout);
+		timeout = oss_broken_sleep_on(&adev->in_sleeper, timeout);
 		if (!timeout) {
 			/* FIXME: include device name */
 			err = -EIO;
@@ -768,8 +767,7 @@ static int output_sleep(int dev, int dontblock)
 		timeout_value = dmabuf_timeout(dmap);
 	else
 		timeout_value = MAX_SCHEDULE_TIMEOUT;
-	timeout_value = interruptible_sleep_on_timeout(&adev->out_sleeper,
-						       timeout_value);
+	timeout_value = oss_broken_sleep_on(&adev->out_sleeper, timeout_value);
 	if (timeout != MAX_SCHEDULE_TIMEOUT && !timeout_value) {
 		printk(KERN_WARNING "Sound: DMA (output) timed out - IRQ/DRQ config error?\n");
 		dma_reset_output(dev);
sound/oss/sequencer.c +7 -9
index 4ff60a6..c855a75 100644
@@ -19,6 +19,7 @@
 #include "sound_config.h"
 
 #include "midi_ctrl.h"
+#include "sleep.h"
 
 static int      sequencer_ok;
 static struct sound_timer_operations *tmr;
@@ -100,8 +101,7 @@ int sequencer_read(int dev, struct file *file, char __user *buf, int count)
   			return -EAGAIN;
   		}
 
- 		interruptible_sleep_on_timeout(&midi_sleeper,
-					       pre_event_timeout);
+ 		oss_broken_sleep_on(&midi_sleeper, pre_event_timeout);
 		spin_lock_irqsave(&lock,flags);
 		if (!iqlen)
 		{
@@ -343,7 +343,7 @@ static int seq_queue(unsigned char *note, char nonblock)
 		/*
 		 * Sleep until there is enough space on the queue
 		 */
-		interruptible_sleep_on(&seq_sleeper);
+		oss_broken_sleep_on(&seq_sleeper, MAX_SCHEDULE_TIMEOUT);
 	}
 	if (qlen >= SEQ_MAX_QUEUE)
 	{
@@ -1122,8 +1122,7 @@ static void seq_drain_midi_queues(void)
 		 */
 
  		if (n)
- 			interruptible_sleep_on_timeout(&seq_sleeper,
-						       HZ/10);
+ 			oss_broken_sleep_on(&seq_sleeper, HZ/10);
 	}
 }
 
@@ -1145,8 +1144,7 @@ void sequencer_release(int dev, struct file *file)
 		while (!signal_pending(current) && qlen > 0)
 		{
   			seq_sync();
- 			interruptible_sleep_on_timeout(&seq_sleeper,
-						       3*HZ);
+ 			oss_broken_sleep_on(&seq_sleeper, 3*HZ);
  			/* Extra delay */
 		}
 	}
@@ -1201,7 +1199,7 @@ static int seq_sync(void)
 		seq_startplay();
 
  	if (qlen > 0)
- 		interruptible_sleep_on_timeout(&seq_sleeper, HZ);
+ 		oss_broken_sleep_on(&seq_sleeper, HZ);
 	return qlen;
 }
 
@@ -1224,7 +1222,7 @@ static void midi_outc(int dev, unsigned char data)
 
 	spin_lock_irqsave(&lock,flags);
  	while (n && !midi_devs[dev]->outputc(dev, data)) {
- 		interruptible_sleep_on_timeout(&seq_sleeper, HZ/25);
+ 		oss_broken_sleep_on(&seq_sleeper, HZ/25);
   		n--;
   	}
 	spin_unlock_irqrestore(&lock,flags);
sound/oss/sleep.h new file +18 -0
new file mode 100644
index 0000000..a20fc92
@@ -0,0 +1,18 @@
+#include <linux/wait.h>
+
+/*
+ * Do not use. This is a replacement for the old
+ * "interruptible_sleep_on_timeout" function that has been
+ * deprecated for ages. All users should instead try to use
+ * wait_event_interruptible_timeout.
+ */
+
+static inline long
+oss_broken_sleep_on(wait_queue_head_t *q, long timeout)
+{
+	DEFINE_WAIT(wait);
+	prepare_to_wait(q, &wait, TASK_INTERRUPTIBLE);
+	timeout = schedule_timeout(timeout);
+	finish_wait(q, &wait);
+	return timeout;
+}
sound/oss/swarm_cs4297a.c +8 -6
index 7d8803a..59defdc 100644
@@ -90,6 +90,8 @@
 #include <asm/sibyte/sb1250_mac.h>
 #include <asm/sibyte/sb1250.h>
 
+#include "sleep.h"
+
 struct cs4297a_state;
 
 static DEFINE_MUTEX(swarm_cs4297a_mutex);
@@ -748,7 +750,7 @@ static int serdma_reg_access(struct cs4297a_state *s, u64 data)
                 /* Since a writer has the DSP open, we have to mux the
                    request in */
                 s->reg_request = data;
-                interruptible_sleep_on(&s->dma_dac.reg_wait);
+                oss_broken_sleep_on(&s->dma_dac.reg_wait, MAX_SCHEDULE_TIMEOUT);
                 /* XXXKW how can I deal with the starvation case where
                    the opener isn't writing? */
         } else {
@@ -790,7 +792,7 @@ static int cs4297a_read_ac97(struct cs4297a_state *s, u32 offset,
         if (serdma_reg_access(s, (0xCLL << 60) | (1LL << 47) | ((u64)(offset & 0x7F) << 40)))
                 return -1;
 
-        interruptible_sleep_on(&s->dma_adc.reg_wait);
+        oss_broken_sleep_on(&s->dma_adc.reg_wait, MAX_SCHEDULE_TIMEOUT);
         *value = s->read_value;
         CS_DBGOUT(CS_AC97, 2,
                   printk(KERN_INFO "cs4297a: rdr reg %x -> %x\n", s->read_reg, s->read_value));
@@ -1740,7 +1742,7 @@ static ssize_t cs4297a_read(struct file *file, char *buffer, size_t count,
 			start_adc(s);
 			if (file->f_flags & O_NONBLOCK)
 				return ret ? ret : -EAGAIN;
-			interruptible_sleep_on(&s->dma_adc.wait);
+			oss_broken_sleep_on(&s->dma_adc.wait, MAX_SCHEDULE_TIMEOUT);
 			if (signal_pending(current))
 				return ret ? ret : -ERESTARTSYS;
 			continue;
@@ -1836,7 +1838,7 @@ static ssize_t cs4297a_write(struct file *file, const char *buffer,
 			start_dac(s);
 			if (file->f_flags & O_NONBLOCK)
 				return ret ? ret : -EAGAIN;
-			interruptible_sleep_on(&d->wait);
+			oss_broken_sleep_on(&d->wait, MAX_SCHEDULE_TIMEOUT);
 			if (signal_pending(current))
 				return ret ? ret : -ERESTARTSYS;
 			continue;
@@ -2452,7 +2454,7 @@ static int cs4297a_locked_open(struct inode *inode, struct file *file)
 				return -EBUSY;
 			}
 			mutex_unlock(&s->open_sem_dac);
-			interruptible_sleep_on(&s->open_wait_dac);
+			oss_broken_sleep_on(&s->open_wait_dac, MAX_SCHEDULE_TIMEOUT);
 
 			if (signal_pending(current)) {
                                 printk("open - sig pending\n");
@@ -2469,7 +2471,7 @@ static int cs4297a_locked_open(struct inode *inode, struct file *file)
 				return -EBUSY;
 			}
 			mutex_unlock(&s->open_sem_adc);
-			interruptible_sleep_on(&s->open_wait_adc);
+			oss_broken_sleep_on(&s->open_wait_adc, MAX_SCHEDULE_TIMEOUT);
 
 			if (signal_pending(current)) {
                                 printk("open - sig pending\n");
Signature · 1 line
1.8.3.2
author maintainer
#76 lore raw

RFC PATCH 28/30 sgi-xp: open-code interruptible_sleep_on_timeout

In reply to Arnd Bergmann

interruptible_sleep_on_timeout is deprecated and going away soon. The use in the sgi-xp driver leaves me puzzled, so I'd prefer not to touch it. This patch replaces it with an open-coded prepare_to_wait and finish_wait pair, which should be completely equivalent, so it doesn't fix an existing race, but lets us get away with removing the function so we can not get any new users.

In order to remove the typical sleep_on race, one would have to replace the call with wait_event_interruptible_timeout and add a condition to wait for. The fact that there is a one-jiffy timeout suggests that we don't actually expect to get woken up properly and the caller just uses this as a short sleeping function if it doesn't wake up properly.

Cc Cliff Whickman <[email protected]>
Cc Robin Holt <[email protected]>
Cc Greg Kroah-Hartman <[email protected]>
---
 drivers/misc/sgi-xp/xpc_channel.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
drivers/misc/sgi-xp/xpc_channel.c +4 -1
index 652593f..128d561 100644
@@ -828,6 +828,7 @@ enum xp_retval
 xpc_allocate_msg_wait(struct xpc_channel *ch)
 {
 	enum xp_retval ret;
+	DEFINE_WAIT(wait);
 
 	if (ch->flags & XPC_C_DISCONNECTING) {
 		DBUG_ON(ch->reason == xpInterrupted);
@@ -835,7 +836,9 @@ xpc_allocate_msg_wait(struct xpc_channel *ch)
 	}
 
 	atomic_inc(&ch->n_on_msg_allocate_wq);
-	ret = interruptible_sleep_on_timeout(&ch->msg_allocate_wq, 1);
+	prepare_to_wait(&ch->msg_allocate_wq, &wait, TASK_INTERRUPTIBLE);
+	ret = schedule_timeout(1);
+	finish_wait(&ch->msg_allocate_wq, &wait);
 	atomic_dec(&ch->n_on_msg_allocate_wq);
 
 	if (ch->flags & XPC_C_DISCONNECTING) {
Signature · 1 line
1.8.3.2
Acked-by on v1 28/30
#77 lore raw

Re: [PATCH, RFC 28/30] sgi-xp: open-code interruptible_sleep_on_timeout

Quoted 48 lines · Arnd Bergmann

interruptible_sleep_on_timeout is deprecated and going away soon. The use in the sgi-xp driver leaves me puzzled, so I'd prefer not to touch it. This patch replaces it with an open-coded prepare_to_wait and finish_wait pair, which should be completely equivalent, so it doesn't fix an existing race, but lets us get away with removing the function so we can not get any new users.

In order to remove the typical sleep_on race, one would have to replace the call with wait_event_interruptible_timeout and add a condition to wait for. The fact that there is a one-jiffy timeout suggests that we don't actually expect to get woken up properly and the caller just uses this as a short sleeping function if it doesn't wake up properly.

Cc Cliff Whickman <[email protected]>
Cc Robin Holt <[email protected]>
Cc Greg Kroah-Hartman <[email protected]>
---
 drivers/misc/sgi-xp/xpc_channel.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
drivers/misc/sgi-xp/xpc_channel.c +4 -2
index 652593f..128d561 100644
@@ -828,6 +828,7 @@ enum xp_retval
 xpc_allocate_msg_wait(struct xpc_channel *ch)
 {
        enum xp_retval ret;
+       DEFINE_WAIT(wait);
 
        if (ch->flags & XPC_C_DISCONNECTING) {
                DBUG_ON(ch->reason == xpInterrupted);
@@ -835,7 +836,9 @@ xpc_allocate_msg_wait(struct xpc_channel *ch)
        }
 
        atomic_inc(&ch->n_on_msg_allocate_wq);
-       ret = interruptible_sleep_on_timeout(&ch->msg_allocate_wq, 1);
+       prepare_to_wait(&ch->msg_allocate_wq, &wait, TASK_INTERRUPTIBLE);
+       ret = schedule_timeout(1);
+       finish_wait(&ch->msg_allocate_wq, &wait);
        atomic_dec(&ch->n_on_msg_allocate_wq);
 
        if (ch->flags & XPC_C_DISCONNECTING) {
--

1.8.3.2

author maintainer
#78 lore raw

RFC PATCH 29/30 char: nwbutton: open-code interruptible_sleep_on

In reply to Arnd Bergmann

The nwbutton driver uses interruptible_sleep_on to wait for buttons getting pressed after we enter the read() function, which is inherently racy and cannot be fixed by using wait_event without changing the driver's user space interface.

Instead, this patch just uses an open-coded variant of the same interruptible_sleep_on() call, so the driver behavior doesn't change but we remove the sleep_on family from the kernel.

Cc Greg Kroah-Hartman <[email protected]>
---
 drivers/char/nwbutton.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
drivers/char/nwbutton.c +4 -1
index 1fd00dc..76c490f 100644
@@ -168,7 +168,10 @@ static irqreturn_t button_handler (int irq, void *dev_id)
 static int button_read (struct file *filp, char __user *buffer,
 			size_t count, loff_t *ppos)
 {
-	interruptible_sleep_on (&button_wait_queue);
+	DEFINE_WAIT(wait);
+	prepare_to_wait(&button_wait_queue, &wait, TASK_INTERRUPTIBLE);
+	schedule();
+	finish_wait(&button_wait_queue, &wait);
 	return (copy_to_user (buffer, &button_output_buffer, bcount))
 		 ? -EFAULT : bcount;
 }
Signature · 1 line
1.8.3.2
author maintainer
#79 lore raw

RFC PATCH 30/30 sched: remove sleep_on() and friends

In reply to Arnd Bergmann

We probably should have done this 15 years ago. I have created patches for every remaining user of the four sleep_on variants that are all broken, and this patch should get applied as soon as all of the other patches are merged.

If you are reading this changeset because you are looking for the functions I removed, please convert your code to use wait_event, wait_for_completion or an open-coded prepare_to_wait loop.

Cc Peter Zijlstra <[email protected]>
Cc Ingo Molnar <[email protected]>
---
 Documentation/DocBook/kernel-hacking.tmpl | 10 -------
 include/linux/wait.h                      | 11 --------
 kernel/sched/core.c                       | 46 -------------------------------
 3 files changed, 67 deletions(-)
Documentation/DocBook/kernel-hacking.tmpl +0 -10
index d0758b24..bd9015d 100644
@@ -850,16 +850,6 @@ printk(KERN_INFO "my ip: %pI4\n", &amp;ipaddress);
     <returnvalue>-ERESTARTSYS</returnvalue> if a signal is received.
     The <function>wait_event()</function> version ignores signals.
    </para>
-   <para>
-   Do not use the <function>sleep_on()</function> function family -
-   it is very easy to accidentally introduce races; almost certainly
-   one of the <function>wait_event()</function> family will do, or a
-   loop around <function>schedule_timeout()</function>. If you choose
-   to loop around <function>schedule_timeout()</function> remember
-   you must set the task state (with 
-   <function>set_current_state()</function>) on each iteration to avoid
-   busy-looping.
-   </para>
  
   </sect1>
 
include/linux/wait.h +0 -11
index eaa00b1..02c5a31 100644
@@ -803,17 +803,6 @@ do {									\
 	__ret;								\
 })
 
-
-/*
- * These are the old interfaces to sleep waiting for an event.
- * They are racy.  DO NOT use them, use the wait_event* interfaces above.
- * We plan to remove these interfaces.
- */
-extern void sleep_on(wait_queue_head_t *q);
-extern long sleep_on_timeout(wait_queue_head_t *q, signed long timeout);
-extern void interruptible_sleep_on(wait_queue_head_t *q);
-extern long interruptible_sleep_on_timeout(wait_queue_head_t *q, signed long timeout);
-
 /*
  * Waitqueues which are removed from the waitqueue_head at wakeup time
  */
kernel/sched/core.c +0 -46

52 lines not on this page · raw message

Signature · 1 line
1.8.3.2