mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Luis Henriques <luis.henriques@canonical.com>
To: linux-kernel@vger.kernel.org, stable@vger.kernel.org,
	kernel-team@lists.ubuntu.com
Cc: Martin Peschke <mpeschke@linux.vnet.ibm.com>,
	Steffen Maier <maier@linux.vnet.ibm.com>,
	James Bottomley <JBottomley@Parallels.com>,
	Luis Henriques <luis.henriques@canonical.com>
Subject: [PATCH 45/58] [SCSI] zfcp: fix lock imbalance by reworking request queue locking
Date: Mon,  2 Sep 2013 09:54:30 +0100	[thread overview]
Message-ID: <1378112083-9475-46-git-send-email-luis.henriques@canonical.com> (raw)
In-Reply-To: <1378112083-9475-1-git-send-email-luis.henriques@canonical.com>

3.5.7.21 -stable review patch.  If anyone has any objections, please let me know.

------------------

From: Martin Peschke <mpeschke@linux.vnet.ibm.com>

commit d79ff142624e1be080ad8d09101f7004d79c36e1 upstream.

This patch adds wait_event_interruptible_lock_irq_timeout(), which is a
straight-forward descendant of wait_event_interruptible_timeout() and
wait_event_interruptible_lock_irq().

The zfcp driver used to call wait_event_interruptible_timeout()
in combination with some intricate and error-prone locking. Using
wait_event_interruptible_lock_irq_timeout() as a replacement
nicely cleans up that locking.

This rework removes a situation that resulted in a locking imbalance
in zfcp_qdio_sbal_get():

BUG: workqueue leaked lock or atomic: events/1/0xffffff00/10
    last function: zfcp_fc_wka_port_offline+0x0/0xa0 [zfcp]

It was introduced by commit c2af7545aaff3495d9bf9a7608c52f0af86fb194
"[SCSI] zfcp: Do not wait for SBALs on stopped queue", which had a new
code path related to ZFCP_STATUS_ADAPTER_QDIOUP that took an early exit
without a required lock being held. The problem occured when a
special, non-SCSI I/O request was being submitted in process context,
when the adapter's queues had been torn down. In this case the bug
surfaced when the Fibre Channel port connection for a well-known address
was closed during a concurrent adapter shut-down procedure, which is a
rare constellation.

This patch also fixes these warnings from the sparse tool (make C=1):

drivers/s390/scsi/zfcp_qdio.c:224:12: warning: context imbalance in
 'zfcp_qdio_sbal_check' - wrong count at exit
drivers/s390/scsi/zfcp_qdio.c:244:5: warning: context imbalance in
 'zfcp_qdio_sbal_get' - unexpected unlock

Last but not least, we get rid of that crappy lock-unlock-lock
sequence at the beginning of the critical section.

It is okay to call zfcp_erp_adapter_reopen() with req_q_lock held.

Reported-by: Mikulas Patocka <mpatocka@redhat.com>
Reported-by: Heiko Carstens <heiko.carstens@de.ibm.com>
Signed-off-by: Martin Peschke <mpeschke@linux.vnet.ibm.com>
Signed-off-by: Steffen Maier <maier@linux.vnet.ibm.com>
Signed-off-by: James Bottomley <JBottomley@Parallels.com>
[ luis: backported to 3.5: adjusted context ]
Signed-off-by: Luis Henriques <luis.henriques@canonical.com>
---
 drivers/s390/scsi/zfcp_qdio.c |  8 ++----
 include/linux/wait.h          | 58 +++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 60 insertions(+), 6 deletions(-)

diff --git a/drivers/s390/scsi/zfcp_qdio.c b/drivers/s390/scsi/zfcp_qdio.c
index e76d003..52c6b59 100644
--- a/drivers/s390/scsi/zfcp_qdio.c
+++ b/drivers/s390/scsi/zfcp_qdio.c
@@ -224,11 +224,9 @@ int zfcp_qdio_sbals_from_sg(struct zfcp_qdio *qdio, struct zfcp_qdio_req *q_req,
 
 static int zfcp_qdio_sbal_check(struct zfcp_qdio *qdio)
 {
-	spin_lock_irq(&qdio->req_q_lock);
 	if (atomic_read(&qdio->req_q_free) ||
 	    !(atomic_read(&qdio->adapter->status) & ZFCP_STATUS_ADAPTER_QDIOUP))
 		return 1;
-	spin_unlock_irq(&qdio->req_q_lock);
 	return 0;
 }
 
@@ -246,9 +244,8 @@ int zfcp_qdio_sbal_get(struct zfcp_qdio *qdio)
 {
 	long ret;
 
-	spin_unlock_irq(&qdio->req_q_lock);
-	ret = wait_event_interruptible_timeout(qdio->req_q_wq,
-			       zfcp_qdio_sbal_check(qdio), 5 * HZ);
+	ret = wait_event_interruptible_lock_irq_timeout(qdio->req_q_wq,
+		       zfcp_qdio_sbal_check(qdio), qdio->req_q_lock, 5 * HZ);
 
 	if (!(atomic_read(&qdio->adapter->status) & ZFCP_STATUS_ADAPTER_QDIOUP))
 		return -EIO;
@@ -262,7 +259,6 @@ int zfcp_qdio_sbal_get(struct zfcp_qdio *qdio)
 		zfcp_erp_adapter_reopen(qdio->adapter, 0, "qdsbg_1");
 	}
 
-	spin_lock_irq(&qdio->req_q_lock);
 	return -EIO;
 }
 
diff --git a/include/linux/wait.h b/include/linux/wait.h
index 6c6c20e..d331566 100644
--- a/include/linux/wait.h
+++ b/include/linux/wait.h
@@ -572,6 +572,64 @@ do {									\
 	__ret;								\
 })
 
+#define __wait_event_interruptible_lock_irq_timeout(wq, condition,	\
+						    lock, ret)		\
+do {									\
+	DEFINE_WAIT(__wait);						\
+									\
+	for (;;) {							\
+		prepare_to_wait(&wq, &__wait, TASK_INTERRUPTIBLE);	\
+		if (condition)						\
+			break;						\
+		if (signal_pending(current)) {				\
+			ret = -ERESTARTSYS;				\
+			break;						\
+		}							\
+		spin_unlock_irq(&lock);					\
+		ret = schedule_timeout(ret);				\
+		spin_lock_irq(&lock);					\
+		if (!ret)						\
+			break;						\
+	}								\
+	finish_wait(&wq, &__wait);					\
+} while (0)
+
+/**
+ * wait_event_interruptible_lock_irq_timeout - sleep until a condition gets true or a timeout elapses.
+ *		The condition is checked under the lock. This is expected
+ *		to be called with the lock taken.
+ * @wq: the waitqueue to wait on
+ * @condition: a C expression for the event to wait for
+ * @lock: a locked spinlock_t, which will be released before schedule()
+ *	  and reacquired afterwards.
+ * @timeout: timeout, in jiffies
+ *
+ * The process is put to sleep (TASK_INTERRUPTIBLE) until the
+ * @condition evaluates to true or signal is received. The @condition is
+ * checked each time the waitqueue @wq is woken up.
+ *
+ * wake_up() has to be called after changing any variable that could
+ * change the result of the wait condition.
+ *
+ * This is supposed to be called while holding the lock. The lock is
+ * dropped before going to sleep and is reacquired afterwards.
+ *
+ * The function returns 0 if the @timeout elapsed, -ERESTARTSYS if it
+ * was interrupted by a signal, and the remaining jiffies otherwise
+ * if the condition evaluated to true before the timeout elapsed.
+ */
+#define wait_event_interruptible_lock_irq_timeout(wq, condition, lock,	\
+						  timeout)		\
+({									\
+	int __ret = timeout;						\
+									\
+	if (!(condition))						\
+		__wait_event_interruptible_lock_irq_timeout(		\
+					wq, condition, lock, __ret);	\
+	__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.
-- 
1.8.3.2


  parent reply	other threads:[~2013-09-02  9:01 UTC|newest]

Thread overview: 59+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-09-02  8:53 [ 3.5.y.z extended stable ] Linux 3.5.7.21 stable review Luis Henriques
2013-09-02  8:53 ` [PATCH 01/58] drm/i915/lvds: ditch ->prepare special case Luis Henriques
2013-09-02  8:53 ` [PATCH 02/58] ALSA: usb: Parse UAC2 extension unit like for UAC1 Luis Henriques
2013-09-02  8:53 ` [PATCH 03/58] serial: mxs: fix buffer overflow Luis Henriques
2013-09-02  8:53 ` [PATCH 04/58] fs/proc/task_mmu.c: fix buffer overflow in add_page_map() Luis Henriques
2013-09-02  8:53 ` [PATCH 05/58] af_key: initialize satype in key_notify_policy_flush() Luis Henriques
2013-09-02  8:53 ` [PATCH 06/58] vm: add no-mmu vm_iomap_memory() stub Luis Henriques
2013-09-02  8:53 ` [PATCH 07/58] iwl4965: set power mode early Luis Henriques
2013-09-02  8:53 ` [PATCH 08/58] iwl4965: reset firmware after rfkill off Luis Henriques
2013-09-02  8:53 ` [PATCH 09/58] ASoC: cs42l52: Reorder Min/Max and update to SX_TLV for Beep Volume Luis Henriques
2013-09-02  8:53 ` [PATCH 10/58] can: pcan_usb: fix wrong memcpy() bytes length Luis Henriques
2013-09-02  8:53 ` [PATCH 11/58] ALSA: 6fire: make buffers DMA-able (pcm) Luis Henriques
2013-09-02  8:53 ` [PATCH 12/58] ALSA: 6fire: make buffers DMA-able (midi) Luis Henriques
2013-09-02  8:53 ` [PATCH 13/58] jbd2: Fix use after free after error in jbd2_journal_dirty_metadata() Luis Henriques
2013-09-02  8:53 ` [PATCH 14/58] USB-Serial: Fix error handling of usb_wwan Luis Henriques
2013-09-02  8:54 ` [PATCH 15/58] USB: mos7840: fix big-endian probe Luis Henriques
2013-09-02  8:54 ` [PATCH 16/58] USB: adutux: fix big-endian device-type reporting Luis Henriques
2013-09-02  8:54 ` [PATCH 17/58] USB: ti_usb_3410_5052: fix big-endian firmware handling Luis Henriques
2013-09-02  8:54 ` [PATCH 18/58] ARM: 7809/1: perf: fix event validation for software group leaders Luis Henriques
2013-09-02  8:54 ` [PATCH 19/58] m68k/atari: ARAnyM - Fix NatFeat module support Luis Henriques
2013-09-02  8:54 ` [PATCH 20/58] m68k: Truncate base in do_div() Luis Henriques
2013-09-02  8:54 ` [PATCH 21/58] usb: add two quirky touchscreen Luis Henriques
2013-09-02  8:54 ` [PATCH 22/58] USB: mos7720: fix broken control requests Luis Henriques
2013-09-02  8:54 ` [PATCH 23/58] USB: keyspan: fix null-deref at disconnect and release Luis Henriques
2013-09-02  8:54 ` [PATCH 24/58] MIPS: Expose missing pci_io{map,unmap} declarations Luis Henriques
2013-09-02  8:54 ` [PATCH 25/58] microblaze: Update microblaze defconfigs Luis Henriques
2013-09-02  8:54 ` [PATCH 26/58] sound: Fix make allmodconfig on MIPS Luis Henriques
2013-09-02  8:54 ` [PATCH 27/58] sound: Fix make allmodconfig on MIPS correctly Luis Henriques
2013-09-02  8:54 ` [PATCH 28/58] alpha: makefile: don't enforce small data model for kernel builds Luis Henriques
2013-09-02  8:54 ` [PATCH 29/58] MIPS: Rewrite pfn_valid to work in modules, too Luis Henriques
2013-09-02  8:54 ` [PATCH 30/58] xtensa: fix linker script transformation for .text.unlikely Luis Henriques
2013-09-02  8:54 ` [PATCH 31/58] wusbcore: fix kernel panic when disconnecting a wireless USB->serial device Luis Henriques
2013-09-02  8:54 ` [PATCH 32/58] ARM: 7810/1: perf: Fix array out of bounds access in armpmu_map_hw_event() Luis Henriques
2013-09-02  8:54 ` [PATCH 33/58] iwlwifi: dvm: fix calling ieee80211_chswitch_done() with NULL Luis Henriques
2013-09-02  8:54 ` [PATCH 34/58] iwlwifi: pcie: disable L1 Active after pci_enable_device Luis Henriques
2013-09-02  8:54 ` [PATCH 35/58] zd1201: do not use stack as URB transfer_buffer Luis Henriques
2013-09-02  8:54 ` [PATCH 36/58] Hostap: copying wrong data prism2_ioctl_giwaplist() Luis Henriques
2013-09-02  8:54 ` [PATCH 37/58] ARM: at91/DT: fix at91sam9n12ek memory node Luis Henriques
2013-09-02  8:54 ` [PATCH 38/58] drm/i915: Invalidate TLBs for the rings after a reset Luis Henriques
2013-09-02  8:54 ` [PATCH 39/58] libata: apply behavioral quirks to sil3826 PMP Luis Henriques
2013-09-02  8:54 ` [PATCH 40/58] ARM: davinci: nand: specify ecc strength Luis Henriques
2013-09-02  8:54 ` [PATCH 41/58] ARM: 7816/1: CONFIG_KUSER_HELPERS: fix help text Luis Henriques
2013-09-02  8:54 ` [PATCH 42/58] sata_fsl: save irqs while coalescing Luis Henriques
2013-09-02  8:54 ` [PATCH 43/58] xen/events: initialize local per-cpu mask for all possible events Luis Henriques
2013-09-02  8:54 ` [PATCH 44/58] of: fdt: fix memory initialization for expanded DT Luis Henriques
2013-09-02  8:54 ` Luis Henriques [this message]
2013-09-02  8:54 ` [PATCH 46/58] [SCSI] zfcp: fix schedule-inside-lock in scsi_device list loops Luis Henriques
2013-09-02  8:54 ` [PATCH 47/58] nilfs2: remove double bio_put() in nilfs_end_bio_write() for BIO_EOPNOTSUPP error Luis Henriques
2013-09-02  8:54 ` [PATCH 48/58] nilfs2: fix issue with counting number of bio requests for BIO_EOPNOTSUPP error detection Luis Henriques
2013-09-02  8:54 ` [PATCH 49/58] Bluetooth: Add support for Atheros [0cf3:3121] Luis Henriques
2013-09-02  8:54 ` [PATCH 50/58] efivars: explicitly calculate length of VariableName Luis Henriques
2013-09-02  8:54 ` [PATCH 51/58] workqueue: fix possible stall on try_to_grab_pending() of a delayed work item Luis Henriques
2013-09-02  8:54 ` [PATCH 52/58] x86/xen: do not identity map UNUSABLE regions in the machine E820 Luis Henriques
2013-09-02  8:54 ` [PATCH 53/58] jfs: fix readdir cookie incompatibility with NFSv4 Luis Henriques
2013-09-02  8:54 ` [PATCH 54/58] powerpc: Don't Oops when accessing /proc/powerpc/lparcfg without hypervisor Luis Henriques
2013-09-02  8:54 ` [PATCH 55/58] powerpc: Work around gcc miscompilation of __pa() on 64-bit Luis Henriques
2013-09-02  8:54 ` [PATCH 56/58] powerpc/hvsi: Increase handshake timeout from 200ms to 400ms Luis Henriques
2013-09-02  8:54 ` [PATCH 57/58] drivers/base/memory.c: fix show_mem_removable() to handle missing sections Luis Henriques
2013-09-02  8:54 ` [PATCH 58/58] x86 get_unmapped_area: Access mmap_legacy_base through mm_struct member Luis Henriques

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1378112083-9475-46-git-send-email-luis.henriques@canonical.com \
    --to=luis.henriques@canonical.com \
    --cc=JBottomley@Parallels.com \
    --cc=kernel-team@lists.ubuntu.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=maier@linux.vnet.ibm.com \
    --cc=mpeschke@linux.vnet.ibm.com \
    --cc=stable@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®