All of lore.kernel.org
 help / color / mirror / Atom feed
From: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
To: linux-kernel@vger.kernel.org
Cc: Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
	stable@vger.kernel.org,
	Rainer Weikusat <rweikusat@cyberadapt.com>,
	Al Viro <viro@zeniv.linux.org.uk>,
	Linus Torvalds <torvalds@linux-foundation.org>,
	Hannes Frederic Sowa <hannes@stressinduktion.org>,
	"David S. Miller" <davem@davemloft.net>
Subject: [PATCH 4.4 34/73] af_unix: split u->readlock into two: iolock and bindlock
Date: Wed, 28 Sep 2016 11:05:04 +0200	[thread overview]
Message-ID: <20160928090437.116875474@linuxfoundation.org> (raw)
In-Reply-To: <20160928090434.509091655@linuxfoundation.org>

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

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

From: Linus Torvalds <torvalds@linux-foundation.org>

commit 6e1ce3c3451291142a57c4f3f6f999a29fb5b3bc upstream.

Right now we use the 'readlock' both for protecting some of the af_unix
IO path and for making the bind be single-threaded.

The two are independent, but using the same lock makes for a nasty
deadlock due to ordering with regards to filesystem locking.  The bind
locking would want to nest outside the VSF pathname locking, but the IO
locking wants to nest inside some of those same locks.

We tried to fix this earlier with commit c845acb324aa ("af_unix: Fix
splice-bind deadlock") which moved the readlock inside the vfs locks,
but that caused problems with overlayfs that will then call back into
filesystem routines that take the lock in the wrong order anyway.

Splitting the locks means that we can go back to having the bind lock be
the outermost lock, and we don't have any deadlocks with lock ordering.

Acked-by: Rainer Weikusat <rweikusat@cyberadapt.com>
Acked-by: Al Viro <viro@zeniv.linux.org.uk>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Acked-by: Hannes Frederic Sowa <hannes@stressinduktion.org>
Signed-off-by: David S. Miller <davem@davemloft.net>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>

---
 include/net/af_unix.h |    2 +-
 net/unix/af_unix.c    |   41 +++++++++++++++++++++--------------------
 2 files changed, 22 insertions(+), 21 deletions(-)

--- a/include/net/af_unix.h
+++ b/include/net/af_unix.h
@@ -52,7 +52,7 @@ struct unix_sock {
 	struct sock		sk;
 	struct unix_address     *addr;
 	struct path		path;
-	struct mutex		readlock;
+	struct mutex		iolock, bindlock;
 	struct sock		*peer;
 	struct list_head	link;
 	atomic_long_t		inflight;
--- a/net/unix/af_unix.c
+++ b/net/unix/af_unix.c
@@ -661,11 +661,11 @@ static int unix_set_peek_off(struct sock
 {
 	struct unix_sock *u = unix_sk(sk);
 
-	if (mutex_lock_interruptible(&u->readlock))
+	if (mutex_lock_interruptible(&u->iolock))
 		return -EINTR;
 
 	sk->sk_peek_off = val;
-	mutex_unlock(&u->readlock);
+	mutex_unlock(&u->iolock);
 
 	return 0;
 }
@@ -778,7 +778,8 @@ static struct sock *unix_create1(struct
 	spin_lock_init(&u->lock);
 	atomic_long_set(&u->inflight, 0);
 	INIT_LIST_HEAD(&u->link);
-	mutex_init(&u->readlock); /* single task reading lock */
+	mutex_init(&u->iolock); /* single task reading lock */
+	mutex_init(&u->bindlock); /* single task binding lock */
 	init_waitqueue_head(&u->peer_wait);
 	init_waitqueue_func_entry(&u->peer_wake, unix_dgram_peer_wake_relay);
 	unix_insert_socket(unix_sockets_unbound(sk), sk);
@@ -847,7 +848,7 @@ static int unix_autobind(struct socket *
 	int err;
 	unsigned int retries = 0;
 
-	err = mutex_lock_interruptible(&u->readlock);
+	err = mutex_lock_interruptible(&u->bindlock);
 	if (err)
 		return err;
 
@@ -894,7 +895,7 @@ retry:
 	spin_unlock(&unix_table_lock);
 	err = 0;
 
-out:	mutex_unlock(&u->readlock);
+out:	mutex_unlock(&u->bindlock);
 	return err;
 }
 
@@ -1008,7 +1009,7 @@ static int unix_bind(struct socket *sock
 		goto out;
 	addr_len = err;
 
-	err = mutex_lock_interruptible(&u->readlock);
+	err = mutex_lock_interruptible(&u->bindlock);
 	if (err)
 		goto out;
 
@@ -1062,7 +1063,7 @@ static int unix_bind(struct socket *sock
 out_unlock:
 	spin_unlock(&unix_table_lock);
 out_up:
-	mutex_unlock(&u->readlock);
+	mutex_unlock(&u->bindlock);
 out:
 	return err;
 }
@@ -1957,17 +1958,17 @@ static ssize_t unix_stream_sendpage(stru
 	if (false) {
 alloc_skb:
 		unix_state_unlock(other);
-		mutex_unlock(&unix_sk(other)->readlock);
+		mutex_unlock(&unix_sk(other)->iolock);
 		newskb = sock_alloc_send_pskb(sk, 0, 0, flags & MSG_DONTWAIT,
 					      &err, 0);
 		if (!newskb)
 			goto err;
 	}
 
-	/* we must acquire readlock as we modify already present
+	/* we must acquire iolock as we modify already present
 	 * skbs in the sk_receive_queue and mess with skb->len
 	 */
-	err = mutex_lock_interruptible(&unix_sk(other)->readlock);
+	err = mutex_lock_interruptible(&unix_sk(other)->iolock);
 	if (err) {
 		err = flags & MSG_DONTWAIT ? -EAGAIN : -ERESTARTSYS;
 		goto err;
@@ -2034,7 +2035,7 @@ alloc_skb:
 	}
 
 	unix_state_unlock(other);
-	mutex_unlock(&unix_sk(other)->readlock);
+	mutex_unlock(&unix_sk(other)->iolock);
 
 	other->sk_data_ready(other);
 	scm_destroy(&scm);
@@ -2043,7 +2044,7 @@ alloc_skb:
 err_state_unlock:
 	unix_state_unlock(other);
 err_unlock:
-	mutex_unlock(&unix_sk(other)->readlock);
+	mutex_unlock(&unix_sk(other)->iolock);
 err:
 	kfree_skb(newskb);
 	if (send_sigpipe && !(flags & MSG_NOSIGNAL))
@@ -2108,7 +2109,7 @@ static int unix_dgram_recvmsg(struct soc
 	if (flags&MSG_OOB)
 		goto out;
 
-	err = mutex_lock_interruptible(&u->readlock);
+	err = mutex_lock_interruptible(&u->iolock);
 	if (unlikely(err)) {
 		/* recvmsg() in non blocking mode is supposed to return -EAGAIN
 		 * sk_rcvtimeo is not honored by mutex_lock_interruptible()
@@ -2184,7 +2185,7 @@ static int unix_dgram_recvmsg(struct soc
 out_free:
 	skb_free_datagram(sk, skb);
 out_unlock:
-	mutex_unlock(&u->readlock);
+	mutex_unlock(&u->iolock);
 out:
 	return err;
 }
@@ -2279,7 +2280,7 @@ static int unix_stream_read_generic(stru
 	/* Lock the socket to prevent queue disordering
 	 * while sleeps in memcpy_tomsg
 	 */
-	mutex_lock(&u->readlock);
+	mutex_lock(&u->iolock);
 
 	if (flags & MSG_PEEK)
 		skip = sk_peek_offset(sk, flags);
@@ -2320,7 +2321,7 @@ again:
 				break;
 			}
 
-			mutex_unlock(&u->readlock);
+			mutex_unlock(&u->iolock);
 
 			timeo = unix_stream_data_wait(sk, timeo, last,
 						      last_len);
@@ -2331,7 +2332,7 @@ again:
 				goto out;
 			}
 
-			mutex_lock(&u->readlock);
+			mutex_lock(&u->iolock);
 			continue;
 unlock:
 			unix_state_unlock(sk);
@@ -2434,7 +2435,7 @@ unlock:
 		}
 	} while (size);
 
-	mutex_unlock(&u->readlock);
+	mutex_unlock(&u->iolock);
 	if (state->msg)
 		scm_recv(sock, state->msg, &scm, flags);
 	else
@@ -2475,9 +2476,9 @@ static ssize_t skb_unix_socket_splice(st
 	int ret;
 	struct unix_sock *u = unix_sk(sk);
 
-	mutex_unlock(&u->readlock);
+	mutex_unlock(&u->iolock);
 	ret = splice_to_pipe(pipe, spd);
-	mutex_lock(&u->readlock);
+	mutex_lock(&u->iolock);
 
 	return ret;
 }

  parent reply	other threads:[~2016-09-28  9:34 UTC|newest]

Thread overview: 69+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <CGME20160928090623uscas1p1076bd85a3fd981ed5a1284f5bebb1bbf@uscas1p1.samsung.com>
2016-09-28  9:04 ` [PATCH 4.4 00/73] 4.4.23-stable review Greg Kroah-Hartman
2016-09-28  9:04   ` [PATCH 4.4 01/73] include/linux/kernel.h: change abs() macro so it uses consistent return type Greg Kroah-Hartman
2016-09-28  9:04   ` [PATCH 4.4 02/73] Fix build warning in kernel/cpuset.c Greg Kroah-Hartman
2016-09-28  9:04   ` [PATCH 4.4 03/73] reiserfs: fix "new_insert_key may be used uninitialized ..." Greg Kroah-Hartman
2016-09-28  9:04   ` [PATCH 4.4 09/73] crypto: arm64/aes-ctr - fix NULL dereference in tail processing Greg Kroah-Hartman
2016-09-28  9:04   ` [PATCH 4.4 10/73] crypto: arm/aes-ctr " Greg Kroah-Hartman
2016-09-28  9:04   ` [PATCH 4.4 11/73] crypto: skcipher - Fix blkcipher walk OOM crash Greg Kroah-Hartman
2016-09-28  9:04   ` [PATCH 4.4 12/73] crypto: echainiv - Replace chaining with multiplication Greg Kroah-Hartman
2016-09-28  9:04   ` [PATCH 4.4 13/73] ocfs2/dlm: fix race between convert and migration Greg Kroah-Hartman
2016-09-28  9:04   ` [PATCH 4.4 14/73] ocfs2: fix start offset to ocfs2_zero_range_for_truncate() Greg Kroah-Hartman
2016-09-28  9:04   ` [PATCH 4.4 15/73] kbuild: Do not run modules_install and install in paralel Greg Kroah-Hartman
2016-09-28  9:04   ` [PATCH 4.4 16/73] Makefile: revert "Makefile: Document ability to make file.lst and file.S" partially Greg Kroah-Hartman
2016-09-28  9:04   ` [PATCH 4.4 17/73] tools: Support relative directory path for O= Greg Kroah-Hartman
2016-09-28  9:04   ` [PATCH 4.4 18/73] kbuild: forbid kernel directory to contain spaces and colons Greg Kroah-Hartman
2016-09-28  9:04   ` [PATCH 4.4 19/73] Kbuild: disable maybe-uninitialized warning for CONFIG_PROFILE_ALL_BRANCHES Greg Kroah-Hartman
2016-09-28  9:04   ` [PATCH 4.4 20/73] gcov: disable -Wmaybe-uninitialized warning Greg Kroah-Hartman
2016-09-28  9:04   ` [PATCH 4.4 21/73] Disable "maybe-uninitialized" warning globally Greg Kroah-Hartman
2016-09-28  9:04   ` [PATCH 4.4 23/73] Makefile: Mute warning for __builtin_return_address(>0) for tracing only Greg Kroah-Hartman
2016-09-28  9:04   ` [PATCH 4.4 24/73] net: caif: fix misleading indentation Greg Kroah-Hartman
2016-09-28  9:04   ` [PATCH 4.4 26/73] [media] am437x-vfpe: fix typo in vpfe_get_app_input_index Greg Kroah-Hartman
2016-09-28  9:04   ` [PATCH 4.4 27/73] ath9k: fix misleading indentation Greg Kroah-Hartman
2016-09-28  9:04   ` [PATCH 4.4 28/73] iwlegacy: avoid warning about missing braces Greg Kroah-Hartman
2016-09-28  9:04   ` [PATCH 4.4 29/73] Staging: iio: adc: fix indent on break statement Greg Kroah-Hartman
2016-09-28  9:05   ` [PATCH 4.4 30/73] nouveau: fix nv40_perfctr_next() cleanup regression Greg Kroah-Hartman
2016-09-28  9:05   ` [PATCH 4.4 31/73] megaraid: fix null pointer check in megasas_detach_one() Greg Kroah-Hartman
2016-09-28  9:05   ` [PATCH 4.4 32/73] bonding: Fix bonding crash Greg Kroah-Hartman
2016-09-28  9:05   ` [PATCH 4.4 33/73] Revert "af_unix: Fix splice-bind deadlock" Greg Kroah-Hartman
2016-09-28  9:05   ` Greg Kroah-Hartman [this message]
2016-09-28  9:05   ` [PATCH 4.4 35/73] vti: flush x-netns xfrm cache when vti interface is removed Greg Kroah-Hartman
2016-09-28  9:05   ` [PATCH 4.4 36/73] net/irda: handle iriap_register_lsap() allocation failure Greg Kroah-Hartman
2016-09-28  9:05   ` [PATCH 4.4 37/73] tipc: fix NULL pointer dereference in shutdown() Greg Kroah-Hartman
2016-09-28  9:05   ` [PATCH 4.4 38/73] net/mlx5: Added missing check of msg length in verifying its signature Greg Kroah-Hartman
2016-09-28  9:05   ` [PATCH 4.4 39/73] net: dsa: bcm_sf2: Fix race condition while unmasking interrupts Greg Kroah-Hartman
2016-09-28  9:05   ` [PATCH 4.4 40/73] Revert "phy: IRQ cannot be shared" Greg Kroah-Hartman
2016-09-28  9:05   ` [PATCH 4.4 41/73] net: smc91x: fix SMC accesses Greg Kroah-Hartman
2016-09-28  9:05   ` [PATCH 4.4 42/73] bridge: re-introduce fix parsing of MLDv2 reports Greg Kroah-Hartman
2016-09-28  9:05   ` [PATCH 4.4 43/73] pwm: Mark all devices as "might sleep" Greg Kroah-Hartman
2016-09-28  9:05   ` [PATCH 4.4 44/73] autofs races Greg Kroah-Hartman
2016-09-28  9:05   ` [PATCH 4.4 45/73] autofs: use dentry flags to block walks during expire Greg Kroah-Hartman
2016-09-28  9:05   ` [PATCH 4.4 46/73] xfs: prevent dropping ioend completions during buftarg wait Greg Kroah-Hartman
2016-09-28  9:05   ` [PATCH 4.4 47/73] fsnotify: add a way to stop queueing events on group shutdown Greg Kroah-Hartman
2016-09-28  9:05   ` [PATCH 4.4 48/73] fanotify: fix list corruption in fanotify_get_response() Greg Kroah-Hartman
2016-09-28  9:05   ` [PATCH 4.4 49/73] fix fault_in_multipages_...() on architectures with no-op access_ok() Greg Kroah-Hartman
2016-09-28  9:05   ` [PATCH 4.4 50/73] mtd: maps: sa1100-flash: potential NULL dereference Greg Kroah-Hartman
2016-09-28  9:05   ` [PATCH 4.4 51/73] mtd: pmcmsp-flash: Allocating too much in init_msp_flash() Greg Kroah-Hartman
2016-09-28  9:05   ` [PATCH 4.4 52/73] power: reset: hisi-reboot: Unmap region obtained by of_iomap Greg Kroah-Hartman
2016-09-28  9:05   ` [PATCH 4.4 53/73] fix memory leaks in tracing_buffers_splice_read() Greg Kroah-Hartman
2016-09-28  9:05   ` [PATCH 4.4 54/73] tracing: Move mutex to protect against resetting of seq data Greg Kroah-Hartman
2016-09-28  9:05   ` [PATCH 4.4 55/73] mm: delete unnecessary and unsafe init_tlb_ubc() Greg Kroah-Hartman
2016-09-28  9:05   ` [PATCH 4.4 56/73] can: flexcan: fix resume function Greg Kroah-Hartman
2016-09-28  9:05   ` [PATCH 4.4 57/73] nl80211: validate number of probe response CSA counters Greg Kroah-Hartman
2016-09-28  9:05   ` [PATCH 4.4 58/73] btrfs: ensure that file descriptor used with subvol ioctls is a dir Greg Kroah-Hartman
2016-09-28  9:05   ` [PATCH 4.4 59/73] i2c-eg20t: fix race between i2c init and interrupt enable Greg Kroah-Hartman
2016-09-28  9:05   ` [PATCH 4.4 60/73] i2c: qup: skip qup_i2c_suspend if the device is already runtime suspended Greg Kroah-Hartman
2016-09-28  9:05   ` [PATCH 4.4 61/73] MIPS: Fix pre-r6 emulation FPU initialisation Greg Kroah-Hartman
2016-09-28  9:05   ` [PATCH 4.4 63/73] MIPS: vDSO: Fix Malta EVA mapping to vDSO page structs Greg Kroah-Hartman
2016-09-28  9:05   ` [PATCH 4.4 64/73] MIPS: Remove compact branch policy Kconfig entries Greg Kroah-Hartman
2016-09-28  9:05   ` [PATCH 4.4 65/73] MIPS: Avoid a BUG warning during prctl(PR_SET_FP_MODE, ...) Greg Kroah-Hartman
2016-09-28  9:05   ` [PATCH 4.4 66/73] MIPS: Add a missing ".set pop" in an early commit Greg Kroah-Hartman
2016-09-28  9:05   ` [PATCH 4.4 67/73] MIPS: paravirt: Fix undefined reference to smp_bootstrap Greg Kroah-Hartman
2016-09-28  9:05   ` [PATCH 4.4 68/73] PM / hibernate: Restore processor state before using per-CPU variables Greg Kroah-Hartman
2016-09-28  9:05   ` [PATCH 4.4 69/73] PM / hibernate: Fix rtree_next_node() to avoid walking off list ends Greg Kroah-Hartman
2016-09-28  9:05   ` [PATCH 4.4 70/73] power_supply: tps65217-charger: fix missing platform_set_drvdata() Greg Kroah-Hartman
2016-09-28  9:05   ` [PATCH 4.4 71/73] power: supply: max17042_battery: fix model download bug Greg Kroah-Hartman
2016-09-28  9:05   ` [PATCH 4.4 72/73] qxl: check for kmap failures Greg Kroah-Hartman
2016-09-28  9:05   ` [PATCH 4.4 73/73] hostfs: Freeing an ERR_PTR in hostfs_fill_sb_common() Greg Kroah-Hartman
2016-09-28 16:45   ` [PATCH 4.4 00/73] 4.4.23-stable review Shuah Khan
2016-09-28 22:43   ` Guenter Roeck
     [not found]   ` <57ec0f9e.07ddc20a.146f7.4be3@mx.google.com>
2016-09-29  9:01     ` Greg Kroah-Hartman

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=20160928090437.116875474@linuxfoundation.org \
    --to=gregkh@linuxfoundation.org \
    --cc=davem@davemloft.net \
    --cc=hannes@stressinduktion.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=rweikusat@cyberadapt.com \
    --cc=stable@vger.kernel.org \
    --cc=torvalds@linux-foundation.org \
    --cc=viro@zeniv.linux.org.uk \
    /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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.