* [PATCH] net: fix setsockopt() locking errors
@ 2009-01-24 22:49 Vegard Nossum
2009-01-26 11:50 ` Jarek Poplawski
0 siblings, 1 reply; 9+ messages in thread
From: Vegard Nossum @ 2009-01-24 22:49 UTC (permalink / raw)
To: David S. Miller; +Cc: Martin MOKREJŠ, netdev
Hi,
This survives basic testing here, but I don't know what that counts for
when I couldn't reproduce the lockdep report in the first place. Please
review.
Vegard
From cc8bcd1c4fd219a31d6d191aefa4b4b57dadb9b0 Mon Sep 17 00:00:00 2001
From: Vegard Nossum <vegard.nossum@gmail.com>
Date: Sat, 24 Jan 2009 22:44:16 +0100
Subject: [PATCH] net: fix setsockopt() locking errors
MIME-Version: 1.0
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: 8bit
Martin MOKREJŠ <mmokrejs@ribosome.natur.cuni.cz> reported:
> =======================================================
> [ INFO: possible circular locking dependency detected ]
> 2.6.29-rc2-git1 #1
> -------------------------------------------------------
> tcpdump/3734 is trying to acquire lock:
> (&mm->mmap_sem){----}, at: [<c1053294>] might_fault+0x30/0x6b
>
> but task is already holding lock:
> (sk_lock-AF_PACKET){--..}, at: [<c12798c8>] sock_setsockopt+0x12b/0x4a4
>
> which lock already depends on the new lock.
It turns out that sock_setsockopt() is calling copy_from_user() while
holding the lock on the socket. We fix it by splitting the ioctl code
so that one switch handles the ioctls that have their own code for
reading from userspace, and one switch handles the cases that require
no additional reading.
Reported-by: Martin MOKREJŠ <mmokrejs@ribosome.natur.cuni.cz>
Signed-off-by: Vegard Nossum <vegard.nossum@gmail.com>
---
net/core/sock.c | 134 +++++++++++++++++++++++++++++++++++-------------------
1 files changed, 87 insertions(+), 47 deletions(-)
diff --git a/net/core/sock.c b/net/core/sock.c
index f3a0d08..6bd618d 100644
--- a/net/core/sock.c
+++ b/net/core/sock.c
@@ -424,6 +424,80 @@ out:
return ret;
}
+static int sock_linger(struct sock *sk, char __user *optval, int optlen)
+{
+ struct linger ling;
+
+ if (optlen < sizeof(ling))
+ return -EINVAL; /* 1003.1g */
+ if (copy_from_user(&ling, optval, sizeof(ling)))
+ return -EFAULT;
+
+ lock_sock(sk);
+
+ if (!ling.l_onoff)
+ sock_reset_flag(sk, SOCK_LINGER);
+ else {
+#if (BITS_PER_LONG == 32)
+ if ((unsigned int) ling.l_linger >= MAX_SCHEDULE_TIMEOUT/HZ)
+ sk->sk_lingertime = MAX_SCHEDULE_TIMEOUT;
+ else
+#endif
+ sk->sk_lingertime = (unsigned int) ling.l_linger * HZ;
+ sock_set_flag(sk, SOCK_LINGER);
+ }
+
+ release_sock(sk);
+
+ return 0;
+}
+
+static int sock_set_rcvtimeo(struct sock *sk, char __user *optval, int optlen)
+{
+ int ret;
+ long rcvtimeo;
+
+ ret = sock_set_timeout(&rcvtimeo, optval, optlen);
+
+ lock_sock(sk);
+ sk->sk_rcvtimeo = rcvtimeo;
+ release_sock(sk);
+
+ return ret;
+}
+
+static int sock_set_sndtimeo(struct sock *sk, char __user *optval, int optlen)
+{
+ int ret;
+ long sndtimeo;
+
+ ret = sock_set_timeout(&sndtimeo, optval, optlen);
+
+ lock_sock(sk);
+ sk->sk_sndtimeo = sndtimeo;
+ release_sock(sk);
+
+ return ret;
+}
+
+static int sock_attach_filter(struct sock *sk, char __user *optval, int optlen)
+{
+ int ret;
+ struct sock_fprog fprog;
+
+ if (optlen != sizeof(struct sock_fprog))
+ return -EINVAL;
+
+ if (copy_from_user(&fprog, optval, sizeof(fprog)))
+ return -EFAULT;
+
+ lock_sock(sk);
+ ret = sk_attach_filter(&fprog, sk);
+ release_sock(sk);
+
+ return ret;
+}
+
static inline void sock_valbool_flag(struct sock *sk, int bit, int valbool)
{
if (valbool)
@@ -440,18 +514,27 @@ static inline void sock_valbool_flag(struct sock *sk, int bit, int valbool)
int sock_setsockopt(struct socket *sock, int level, int optname,
char __user *optval, int optlen)
{
- struct sock *sk=sock->sk;
+ struct sock *sk = sock->sk;
int val;
int valbool;
- struct linger ling;
int ret = 0;
/*
- * Options without arguments
+ * Options with special locking requirements
*/
- if (optname == SO_BINDTODEVICE)
+ switch (optname) {
+ case SO_BINDTODEVICE:
return sock_bindtodevice(sk, optval, optlen);
+ case SO_LINGER:
+ return sock_linger(sk, optval, optlen);
+ case SO_RCVTIMEO:
+ return sock_set_rcvtimeo(sk, optval, optlen);
+ case SO_SNDTIMEO:
+ return sock_set_sndtimeo(sk, optval, optlen);
+ case SO_ATTACH_FILTER:
+ return sock_attach_filter(sk, optval, optlen);
+ }
if (optlen < sizeof(int))
return -EINVAL;
@@ -573,28 +656,6 @@ set_rcvbuf:
ret = -EPERM;
break;
- case SO_LINGER:
- if (optlen < sizeof(ling)) {
- ret = -EINVAL; /* 1003.1g */
- break;
- }
- if (copy_from_user(&ling,optval,sizeof(ling))) {
- ret = -EFAULT;
- break;
- }
- if (!ling.l_onoff)
- sock_reset_flag(sk, SOCK_LINGER);
- else {
-#if (BITS_PER_LONG == 32)
- if ((unsigned int)ling.l_linger >= MAX_SCHEDULE_TIMEOUT/HZ)
- sk->sk_lingertime = MAX_SCHEDULE_TIMEOUT;
- else
-#endif
- sk->sk_lingertime = (unsigned int)ling.l_linger * HZ;
- sock_set_flag(sk, SOCK_LINGER);
- }
- break;
-
case SO_BSDCOMPAT:
sock_warn_obsolete_bsdism("setsockopt");
break;
@@ -627,27 +688,6 @@ set_rcvbuf:
sk->sk_rcvlowat = val ? : 1;
break;
- case SO_RCVTIMEO:
- ret = sock_set_timeout(&sk->sk_rcvtimeo, optval, optlen);
- break;
-
- case SO_SNDTIMEO:
- ret = sock_set_timeout(&sk->sk_sndtimeo, optval, optlen);
- break;
-
- case SO_ATTACH_FILTER:
- ret = -EINVAL;
- if (optlen == sizeof(struct sock_fprog)) {
- struct sock_fprog fprog;
-
- ret = -EFAULT;
- if (copy_from_user(&fprog, optval, sizeof(fprog)))
- break;
-
- ret = sk_attach_filter(&fprog, sk);
- }
- break;
-
case SO_DETACH_FILTER:
ret = sk_detach_filter(sk);
break;
--
1.5.6.6
^ permalink raw reply related [flat|nested] 9+ messages in thread
* Re: [PATCH] net: fix setsockopt() locking errors
2009-01-24 22:49 [PATCH] net: fix setsockopt() locking errors Vegard Nossum
@ 2009-01-26 11:50 ` Jarek Poplawski
2009-01-26 20:33 ` David Miller
2009-01-26 21:30 ` Martin MOKREJŠ
0 siblings, 2 replies; 9+ messages in thread
From: Jarek Poplawski @ 2009-01-26 11:50 UTC (permalink / raw)
To: Vegard Nossum
Cc: David S. Miller,
=?ISO-8859-2?Q?Martin_MOKR?= =?ISO-8859-2?Q?EJ=A9?=, netdev
On 24-01-2009 23:49, Vegard Nossum wrote:
> Hi,
>
> This survives basic testing here, but I don't know what that counts for
> when I couldn't reproduce the lockdep report in the first place. Please
> review.
>
>
> Vegard
>
>
> From cc8bcd1c4fd219a31d6d191aefa4b4b57dadb9b0 Mon Sep 17 00:00:00 2001
> From: Vegard Nossum <vegard.nossum@gmail.com>
> Date: Sat, 24 Jan 2009 22:44:16 +0100
> Subject: [PATCH] net: fix setsockopt() locking errors
> MIME-Version: 1.0
> Content-Type: text/plain; charset=utf-8
> Content-Transfer-Encoding: 8bit
>
> Martin MOKREJ. <mmokrejs@ribosome.natur.cuni.cz> reported:
>> =======================================================
>> [ INFO: possible circular locking dependency detected ]
>> 2.6.29-rc2-git1 #1
>> -------------------------------------------------------
>> tcpdump/3734 is trying to acquire lock:
>> (&mm->mmap_sem){----}, at: [<c1053294>] might_fault+0x30/0x6b
>>
>> but task is already holding lock:
>> (sk_lock-AF_PACKET){--..}, at: [<c12798c8>] sock_setsockopt+0x12b/0x4a4
>>
>> which lock already depends on the new lock.
>
> It turns out that sock_setsockopt() is calling copy_from_user() while
> holding the lock on the socket.
I guess it has been like this for some time, so it would be nice to
mention what scenario happens here, or IOW what exactly needs to get
these locks in reverse order.
> We fix it by splitting the ioctl code
> so that one switch handles the ioctls that have their own code for
> reading from userspace, and one switch handles the cases that require
> no additional reading.
>
> Reported-by: Martin MOKREJ. <mmokrejs@ribosome.natur.cuni.cz>
> Signed-off-by: Vegard Nossum <vegard.nossum@gmail.com>
> ---
> net/core/sock.c | 134 +++++++++++++++++++++++++++++++++++-------------------
> 1 files changed, 87 insertions(+), 47 deletions(-)
>
> diff --git a/net/core/sock.c b/net/core/sock.c
> index f3a0d08..6bd618d 100644
> --- a/net/core/sock.c
> +++ b/net/core/sock.c
> @@ -424,6 +424,80 @@ out:
> return ret;
> }
>
> +static int sock_linger(struct sock *sk, char __user *optval, int optlen)
...
> +static int sock_set_rcvtimeo(struct sock *sk, char __user *optval, int optlen)
> +{
> + int ret;
> + long rcvtimeo;
> +
> + ret = sock_set_timeout(&rcvtimeo, optval, optlen);
A check for error is needed here and below.
> +
> + lock_sock(sk);
> + sk->sk_rcvtimeo = rcvtimeo;
> + release_sock(sk);
> +
> + return ret;
> +}
> +
> +static int sock_set_sndtimeo(struct sock *sk, char __user *optval, int optlen)
> +{
> + int ret;
> + long sndtimeo;
> +
> + ret = sock_set_timeout(&sndtimeo, optval, optlen);
> +
> + lock_sock(sk);
> + sk->sk_sndtimeo = sndtimeo;
> + release_sock(sk);
> +
> + return ret;
> +}
...
Jarek P.
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: [PATCH] net: fix setsockopt() locking errors
2009-01-26 11:50 ` Jarek Poplawski
@ 2009-01-26 20:33 ` David Miller
2009-01-27 16:25 ` Vegard Nossum
2009-01-26 21:30 ` Martin MOKREJŠ
1 sibling, 1 reply; 9+ messages in thread
From: David Miller @ 2009-01-26 20:33 UTC (permalink / raw)
To: jarkao2; +Cc: vegard.nossum, mmokrejs, netdev
From: Jarek Poplawski <jarkao2@gmail.com>
Date: Mon, 26 Jan 2009 11:50:12 +0000
> On 24-01-2009 23:49, Vegard Nossum wrote:
> > +static int sock_linger(struct sock *sk, char __user *optval, int optlen)
> ...
> > +static int sock_set_rcvtimeo(struct sock *sk, char __user *optval, int optlen)
> > +{
> > + int ret;
> > + long rcvtimeo;
> > +
> > + ret = sock_set_timeout(&rcvtimeo, optval, optlen);
>
> A check for error is needed here and below.
Right, you cannot continue and update the socket state if this
sock_set_timeout() call returns an error.
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: [PATCH] net: fix setsockopt() locking errors
2009-01-26 11:50 ` Jarek Poplawski
2009-01-26 20:33 ` David Miller
@ 2009-01-26 21:30 ` Martin MOKREJŠ
2009-01-27 8:45 ` Jarek Poplawski
1 sibling, 1 reply; 9+ messages in thread
From: Martin MOKREJŠ @ 2009-01-26 21:30 UTC (permalink / raw)
To: Jarek Poplawski; +Cc: Vegard Nossum, David S. Miller, netdev
The patch really did not help:
http://bugzilla.kernel.org/show_bug.cgi?id=12515#c5
Martin
Jarek Poplawski wrote:
> On 24-01-2009 23:49, Vegard Nossum wrote:
>> Hi,
>>
>> This survives basic testing here, but I don't know what that counts for
>> when I couldn't reproduce the lockdep report in the first place. Please
>> review.
>>
>>
>> Vegard
>>
>>
>> From cc8bcd1c4fd219a31d6d191aefa4b4b57dadb9b0 Mon Sep 17 00:00:00 2001
>> From: Vegard Nossum <vegard.nossum@gmail.com>
>> Date: Sat, 24 Jan 2009 22:44:16 +0100
>> Subject: [PATCH] net: fix setsockopt() locking errors
>> MIME-Version: 1.0
>> Content-Type: text/plain; charset=utf-8
>> Content-Transfer-Encoding: 8bit
>>
>> Martin MOKREJ. <mmokrejs@ribosome.natur.cuni.cz> reported:
>>> =======================================================
>>> [ INFO: possible circular locking dependency detected ]
>>> 2.6.29-rc2-git1 #1
>>> -------------------------------------------------------
>>> tcpdump/3734 is trying to acquire lock:
>>> (&mm->mmap_sem){----}, at: [<c1053294>] might_fault+0x30/0x6b
>>>
>>> but task is already holding lock:
>>> (sk_lock-AF_PACKET){--..}, at: [<c12798c8>] sock_setsockopt+0x12b/0x4a4
>>>
>>> which lock already depends on the new lock.
>> It turns out that sock_setsockopt() is calling copy_from_user() while
>> holding the lock on the socket.
>
> I guess it has been like this for some time, so it would be nice to
> mention what scenario happens here, or IOW what exactly needs to get
> these locks in reverse order.
>
>> We fix it by splitting the ioctl code
>> so that one switch handles the ioctls that have their own code for
>> reading from userspace, and one switch handles the cases that require
>> no additional reading.
>>
>> Reported-by: Martin MOKREJ. <mmokrejs@ribosome.natur.cuni.cz>
>> Signed-off-by: Vegard Nossum <vegard.nossum@gmail.com>
>> ---
>> net/core/sock.c | 134 +++++++++++++++++++++++++++++++++++-------------------
>> 1 files changed, 87 insertions(+), 47 deletions(-)
>>
>> diff --git a/net/core/sock.c b/net/core/sock.c
>> index f3a0d08..6bd618d 100644
>> --- a/net/core/sock.c
>> +++ b/net/core/sock.c
>> @@ -424,6 +424,80 @@ out:
>> return ret;
>> }
>>
>> +static int sock_linger(struct sock *sk, char __user *optval, int optlen)
> ...
>> +static int sock_set_rcvtimeo(struct sock *sk, char __user *optval, int optlen)
>> +{
>> + int ret;
>> + long rcvtimeo;
>> +
>> + ret = sock_set_timeout(&rcvtimeo, optval, optlen);
>
> A check for error is needed here and below.
>
[cut]
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: [PATCH] net: fix setsockopt() locking errors
2009-01-26 21:30 ` Martin MOKREJŠ
@ 2009-01-27 8:45 ` Jarek Poplawski
2009-01-27 8:52 ` Peter Zijlstra
0 siblings, 1 reply; 9+ messages in thread
From: Jarek Poplawski @ 2009-01-27 8:45 UTC (permalink / raw)
To: Martin MOKREJŠ
Cc: Vegard Nossum, David S. Miller, netdev, Peter Zijlstra
On Mon, Jan 26, 2009 at 10:30:30PM +0100, Martin MOKREJŠ wrote:
> The patch really did not help:
> http://bugzilla.kernel.org/show_bug.cgi?id=12515#c5
> Martin
Actually, there is a little change: the warning triggerd in another
place (sock_setsockopt() -> sk_attach_filter()). So we could go deeper
with these changes, but I'm not sure this is the right way to fix.
It looks like the scenario is very old, but probably wasn't reported
(maybe there is some lockdep improvement):
A) sys_mmap2() -> mm->mmap_sem -> packet_mmap() -> sk_lock
B) sock_setsockopt() -> sk_lock -> copy_from_user() -> mm->mmap_sem
packet_mmap() (net/packet/af_packet.c) seems to be the only place in
net to implement mmap method, and using this lock order btw. On the
other hand copy_from_user() could be more popular under sk_lock, and
I'm not sure these changes are necessary.
Since I don't know enough neither sock/packet nor sys_mmap, I guess
some advice would be precious. It looks like Peter Zijlstra solved
similar problems in nfs, so I CC him.
Thanks,
Jarek P.
>
> Jarek Poplawski wrote:
> > On 24-01-2009 23:49, Vegard Nossum wrote:
> >> Hi,
> >>
> >> This survives basic testing here, but I don't know what that counts for
> >> when I couldn't reproduce the lockdep report in the first place. Please
> >> review.
> >>
> >>
> >> Vegard
> >>
> >>
> >> From cc8bcd1c4fd219a31d6d191aefa4b4b57dadb9b0 Mon Sep 17 00:00:00 2001
> >> From: Vegard Nossum <vegard.nossum@gmail.com>
> >> Date: Sat, 24 Jan 2009 22:44:16 +0100
> >> Subject: [PATCH] net: fix setsockopt() locking errors
> >> MIME-Version: 1.0
> >> Content-Type: text/plain; charset=utf-8
> >> Content-Transfer-Encoding: 8bit
> >>
> >> Martin MOKREJ. <mmokrejs@ribosome.natur.cuni.cz> reported:
> >>> =======================================================
> >>> [ INFO: possible circular locking dependency detected ]
> >>> 2.6.29-rc2-git1 #1
> >>> -------------------------------------------------------
> >>> tcpdump/3734 is trying to acquire lock:
> >>> (&mm->mmap_sem){----}, at: [<c1053294>] might_fault+0x30/0x6b
> >>>
> >>> but task is already holding lock:
> >>> (sk_lock-AF_PACKET){--..}, at: [<c12798c8>] sock_setsockopt+0x12b/0x4a4
> >>>
> >>> which lock already depends on the new lock.
> >> It turns out that sock_setsockopt() is calling copy_from_user() while
> >> holding the lock on the socket.
> >
> > I guess it has been like this for some time, so it would be nice to
> > mention what scenario happens here, or IOW what exactly needs to get
> > these locks in reverse order.
> >
> >> We fix it by splitting the ioctl code
> >> so that one switch handles the ioctls that have their own code for
> >> reading from userspace, and one switch handles the cases that require
> >> no additional reading.
> >>
> >> Reported-by: Martin MOKREJ. <mmokrejs@ribosome.natur.cuni.cz>
> >> Signed-off-by: Vegard Nossum <vegard.nossum@gmail.com>
> >> ---
> >> net/core/sock.c | 134 +++++++++++++++++++++++++++++++++++-------------------
> >> 1 files changed, 87 insertions(+), 47 deletions(-)
> >>
> >> diff --git a/net/core/sock.c b/net/core/sock.c
> >> index f3a0d08..6bd618d 100644
> >> --- a/net/core/sock.c
> >> +++ b/net/core/sock.c
> >> @@ -424,6 +424,80 @@ out:
> >> return ret;
> >> }
> >>
> >> +static int sock_linger(struct sock *sk, char __user *optval, int optlen)
> > ...
> >> +static int sock_set_rcvtimeo(struct sock *sk, char __user *optval, int optlen)
> >> +{
> >> + int ret;
> >> + long rcvtimeo;
> >> +
> >> + ret = sock_set_timeout(&rcvtimeo, optval, optlen);
> >
> > A check for error is needed here and below.
> >
> [cut]
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: [PATCH] net: fix setsockopt() locking errors
2009-01-27 8:45 ` Jarek Poplawski
@ 2009-01-27 8:52 ` Peter Zijlstra
2009-01-27 9:08 ` Jarek Poplawski
2009-01-30 6:12 ` Herbert Xu
0 siblings, 2 replies; 9+ messages in thread
From: Peter Zijlstra @ 2009-01-27 8:52 UTC (permalink / raw)
To: Jarek Poplawski
Cc: Martin MOKREJŠ, Vegard Nossum, David S. Miller, netdev
On Tue, 2009-01-27 at 08:45 +0000, Jarek Poplawski wrote:
> On Mon, Jan 26, 2009 at 10:30:30PM +0100, Martin MOKREJŠ wrote:
> > The patch really did not help:
> > http://bugzilla.kernel.org/show_bug.cgi?id=12515#c5
> > Martin
>
> Actually, there is a little change: the warning triggerd in another
> place (sock_setsockopt() -> sk_attach_filter()). So we could go deeper
> with these changes, but I'm not sure this is the right way to fix.
>
> It looks like the scenario is very old, but probably wasn't reported
> (maybe there is some lockdep improvement):
Yes, they likely are very old, and yes we added a lockdep annotation to
copy_to/from_user() to catch these.
> A) sys_mmap2() -> mm->mmap_sem -> packet_mmap() -> sk_lock
> B) sock_setsockopt() -> sk_lock -> copy_from_user() -> mm->mmap_sem
>
> packet_mmap() (net/packet/af_packet.c) seems to be the only place in
> net to implement mmap method, and using this lock order btw. On the
> other hand copy_from_user() could be more popular under sk_lock, and
> I'm not sure these changes are necessary.
>
> Since I don't know enough neither sock/packet nor sys_mmap, I guess
> some advice would be precious. It looks like Peter Zijlstra solved
> similar problems in nfs, so I CC him.
The NFS/sunrpc case was special in that it did copy_to/from_kernel, that
is, it never actually touched user memory -- we taught the might_fault()
annotation about that.
Can't you simply do the copy_from_user() before you take the sk_lock?
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: [PATCH] net: fix setsockopt() locking errors
2009-01-27 8:52 ` Peter Zijlstra
@ 2009-01-27 9:08 ` Jarek Poplawski
2009-01-30 6:12 ` Herbert Xu
1 sibling, 0 replies; 9+ messages in thread
From: Jarek Poplawski @ 2009-01-27 9:08 UTC (permalink / raw)
To: Peter Zijlstra
Cc: Martin MOKREJŠ, Vegard Nossum, David S. Miller, netdev
On Tue, Jan 27, 2009 at 09:52:49AM +0100, Peter Zijlstra wrote:
> On Tue, 2009-01-27 at 08:45 +0000, Jarek Poplawski wrote:
> > On Mon, Jan 26, 2009 at 10:30:30PM +0100, Martin MOKREJŠ wrote:
> > > The patch really did not help:
> > > http://bugzilla.kernel.org/show_bug.cgi?id=12515#c5
> > > Martin
> >
> > Actually, there is a little change: the warning triggerd in another
> > place (sock_setsockopt() -> sk_attach_filter()). So we could go deeper
> > with these changes, but I'm not sure this is the right way to fix.
> >
> > It looks like the scenario is very old, but probably wasn't reported
> > (maybe there is some lockdep improvement):
>
> Yes, they likely are very old, and yes we added a lockdep annotation to
> copy_to/from_user() to catch these.
>
> > A) sys_mmap2() -> mm->mmap_sem -> packet_mmap() -> sk_lock
> > B) sock_setsockopt() -> sk_lock -> copy_from_user() -> mm->mmap_sem
> >
> > packet_mmap() (net/packet/af_packet.c) seems to be the only place in
> > net to implement mmap method, and using this lock order btw. On the
> > other hand copy_from_user() could be more popular under sk_lock, and
> > I'm not sure these changes are necessary.
> >
> > Since I don't know enough neither sock/packet nor sys_mmap, I guess
> > some advice would be precious. It looks like Peter Zijlstra solved
> > similar problems in nfs, so I CC him.
>
> The NFS/sunrpc case was special in that it did copy_to/from_kernel, that
> is, it never actually touched user memory -- we taught the might_fault()
> annotation about that.
>
> Can't you simply do the copy_from_user() before you take the sk_lock?
>
Since it's really needed, and Vegard started doing it like this, I
guess he will try to add the missing pieces.
Thanks again,
Jarek P.
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: [PATCH] net: fix setsockopt() locking errors
2009-01-26 20:33 ` David Miller
@ 2009-01-27 16:25 ` Vegard Nossum
0 siblings, 0 replies; 9+ messages in thread
From: Vegard Nossum @ 2009-01-27 16:25 UTC (permalink / raw)
To: David Miller; +Cc: jarkao2, mmokrejs, netdev
On Mon, Jan 26, 2009 at 9:33 PM, David Miller <davem@davemloft.net> wrote:
> From: Jarek Poplawski <jarkao2@gmail.com>
> Date: Mon, 26 Jan 2009 11:50:12 +0000
>
>> On 24-01-2009 23:49, Vegard Nossum wrote:
>> > +static int sock_linger(struct sock *sk, char __user *optval, int optlen)
>> ...
>> > +static int sock_set_rcvtimeo(struct sock *sk, char __user *optval, int optlen)
>> > +{
>> > + int ret;
>> > + long rcvtimeo;
>> > +
>> > + ret = sock_set_timeout(&rcvtimeo, optval, optlen);
>>
>> A check for error is needed here and below.
>
> Right, you cannot continue and update the socket state if this
> sock_set_timeout() call returns an error.
>
Argh, thanks both. What a stupid mistake. What is the point in fixing
something if the fix introduces a different kind of error? :-(
I will fix the patch and also check for other places (like that
attach_filter) that need to be fixed.
Vegard
--
"The animistic metaphor of the bug that maliciously sneaked in while
the programmer was not looking is intellectually dishonest as it
disguises that the error is the programmer's own creation."
-- E. W. Dijkstra, EWD1036
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: [PATCH] net: fix setsockopt() locking errors
2009-01-27 8:52 ` Peter Zijlstra
2009-01-27 9:08 ` Jarek Poplawski
@ 2009-01-30 6:12 ` Herbert Xu
1 sibling, 0 replies; 9+ messages in thread
From: Herbert Xu @ 2009-01-30 6:12 UTC (permalink / raw)
To: Peter Zijlstra; +Cc: jarkao2, mmokrejs, vegard.nossum, davem, netdev
Peter Zijlstra <a.p.zijlstra@chello.nl> wrote:
>
> Can't you simply do the copy_from_user() before you take the sk_lock?
Well, doing the copy under sk_lock is pretty common through all
protocols. So I think it'd be safer to change the other path,
which is doing the odd thing here, i.e., ->mmap() grabbing the
socket lock while holding mmap_sem.
In fact, it would appear that we don't really need the socket lock
in ->mmap() since it only needs to ensure that pg_vec* doesn't
get yanked or changed. So this patch should work:
packet: Avoid lock_sock in mmap handler
As the mmap handler gets called under mmap_sem, and we may grab
mmap_sem elsewhere under the socket lock to access user data, we
should avoid grabbing the socket lock in the mmap handler.
Since the only thing we care about in the mmap handler is for
pg_vec* to be invariant, i.e., to exclude packet_set_ring, we
can achieve this by simply using sk_receive_queue.lock.
I resisted the temptation to create a new spin lock because the
mmap path isn't exactly common.
Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
diff --git a/net/packet/af_packet.c b/net/packet/af_packet.c
index 5f94db2..cac0a2b 100644
--- a/net/packet/af_packet.c
+++ b/net/packet/af_packet.c
@@ -1874,13 +1874,14 @@ static int packet_set_ring(struct sock *sk, struct tpacket_req *req, int closing
po->frame_max = (req->tp_frame_nr - 1);
po->head = 0;
po->frame_size = req->tp_frame_size;
- spin_unlock_bh(&sk->sk_receive_queue.lock);
- order = XC(po->pg_vec_order, order);
req->tp_block_nr = XC(po->pg_vec_len, req->tp_block_nr);
-
po->pg_vec_pages = req->tp_block_size/PAGE_SIZE;
+ spin_unlock_bh(&sk->sk_receive_queue.lock);
+
+ order = XC(po->pg_vec_order, order);
po->prot_hook.func = po->pg_vec ? tpacket_rcv : packet_rcv;
+
skb_queue_purge(&sk->sk_receive_queue);
#undef XC
if (atomic_read(&po->mapped))
@@ -1918,7 +1919,7 @@ static int packet_mmap(struct file *file, struct socket *sock, struct vm_area_st
size = vma->vm_end - vma->vm_start;
- lock_sock(sk);
+ spin_lock_bh(&sk->sk_receive_queue.lock);
if (po->pg_vec == NULL)
goto out;
if (size != po->pg_vec_len*po->pg_vec_pages*PAGE_SIZE)
@@ -1941,7 +1942,7 @@ static int packet_mmap(struct file *file, struct socket *sock, struct vm_area_st
err = 0;
out:
- release_sock(sk);
+ spin_unlock_bh(&sk->sk_receive_queue.lock);
return err;
}
#endif
Cheers,
--
Visit Openswan at http://www.openswan.org/
Email: Herbert Xu ~{PmV>HI~} <herbert@gondor.apana.org.au>
Home Page: http://gondor.apana.org.au/~herbert/
PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt
^ permalink raw reply related [flat|nested] 9+ messages in thread
end of thread, other threads:[~2009-01-30 6:13 UTC | newest]
Thread overview: 9+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2009-01-24 22:49 [PATCH] net: fix setsockopt() locking errors Vegard Nossum
2009-01-26 11:50 ` Jarek Poplawski
2009-01-26 20:33 ` David Miller
2009-01-27 16:25 ` Vegard Nossum
2009-01-26 21:30 ` Martin MOKREJŠ
2009-01-27 8:45 ` Jarek Poplawski
2009-01-27 8:52 ` Peter Zijlstra
2009-01-27 9:08 ` Jarek Poplawski
2009-01-30 6:12 ` Herbert Xu
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).