Skip to content

Commit ed07536

Browse files
Peter ZijlstraLinus Torvalds
Peter Zijlstra
authored and
Linus Torvalds
committed
[PATCH] lockdep: annotate nfs/nfsd in-kernel sockets
Stick NFS sockets in their own class to avoid some lockdep warnings. NFS sockets are never exposed to user-space, and will hence not trigger certain code paths that would otherwise pose deadlock scenarios. [[email protected]: cleanups] Signed-off-by: Peter Zijlstra <[email protected]> Signed-off-by: Steven Dickson <[email protected]> Acked-by: Ingo Molnar <[email protected]> Cc: Trond Myklebust <[email protected]> Acked-by: Neil Brown <[email protected]> Cc: "David S. Miller" <[email protected]> Signed-off-by: Andrew Morton <[email protected]> [ Fixed patch corruption by quilt, pointed out by Peter Zijlstra ] Signed-off-by: Linus Torvalds <[email protected]>
1 parent b9d85b0 commit ed07536

File tree

5 files changed

+87
-18
lines changed

5 files changed

+87
-18
lines changed

include/net/sock.h

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -746,6 +746,25 @@ static inline int sk_stream_wmem_schedule(struct sock *sk, int size)
746746
*/
747747
#define sock_owned_by_user(sk) ((sk)->sk_lock.owner)
748748

749+
/*
750+
* Macro so as to not evaluate some arguments when
751+
* lockdep is not enabled.
752+
*
753+
* Mark both the sk_lock and the sk_lock.slock as a
754+
* per-address-family lock class.
755+
*/
756+
#define sock_lock_init_class_and_name(sk, sname, skey, name, key) \
757+
do { \
758+
sk->sk_lock.owner = NULL; \
759+
init_waitqueue_head(&sk->sk_lock.wq); \
760+
spin_lock_init(&(sk)->sk_lock.slock); \
761+
debug_check_no_locks_freed((void *)&(sk)->sk_lock, \
762+
sizeof((sk)->sk_lock)); \
763+
lockdep_set_class_and_name(&(sk)->sk_lock.slock, \
764+
(skey), (sname)); \
765+
lockdep_init_map(&(sk)->sk_lock.dep_map, (name), (key), 0); \
766+
} while (0)
767+
749768
extern void FASTCALL(lock_sock_nested(struct sock *sk, int subclass));
750769

751770
static inline void lock_sock(struct sock *sk)

kernel/lockdep.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2645,6 +2645,7 @@ void debug_check_no_locks_freed(const void *mem_from, unsigned long mem_len)
26452645
}
26462646
local_irq_restore(flags);
26472647
}
2648+
EXPORT_SYMBOL_GPL(debug_check_no_locks_freed);
26482649

26492650
static void print_held_locks_bug(struct task_struct *curr)
26502651
{

net/core/sock.c

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -810,24 +810,11 @@ int sock_getsockopt(struct socket *sock, int level, int optname,
810810
*/
811811
static void inline sock_lock_init(struct sock *sk)
812812
{
813-
spin_lock_init(&sk->sk_lock.slock);
814-
sk->sk_lock.owner = NULL;
815-
init_waitqueue_head(&sk->sk_lock.wq);
816-
/*
817-
* Make sure we are not reinitializing a held lock:
818-
*/
819-
debug_check_no_locks_freed((void *)&sk->sk_lock, sizeof(sk->sk_lock));
820-
821-
/*
822-
* Mark both the sk_lock and the sk_lock.slock as a
823-
* per-address-family lock class:
824-
*/
825-
lockdep_set_class_and_name(&sk->sk_lock.slock,
826-
af_family_slock_keys + sk->sk_family,
827-
af_family_slock_key_strings[sk->sk_family]);
828-
lockdep_init_map(&sk->sk_lock.dep_map,
829-
af_family_key_strings[sk->sk_family],
830-
af_family_keys + sk->sk_family, 0);
813+
sock_lock_init_class_and_name(sk,
814+
af_family_slock_key_strings[sk->sk_family],
815+
af_family_slock_keys + sk->sk_family,
816+
af_family_key_strings[sk->sk_family],
817+
af_family_keys + sk->sk_family);
831818
}
832819

833820
/**

net/sunrpc/svcsock.c

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,35 @@ static struct cache_deferred_req *svc_defer(struct cache_req *req);
8585
*/
8686
static int svc_conn_age_period = 6*60;
8787

88+
#ifdef CONFIG_DEBUG_LOCK_ALLOC
89+
static struct lock_class_key svc_key[2];
90+
static struct lock_class_key svc_slock_key[2];
91+
92+
static inline void svc_reclassify_socket(struct socket *sock)
93+
{
94+
struct sock *sk = sock->sk;
95+
BUG_ON(sk->sk_lock.owner != NULL);
96+
switch (sk->sk_family) {
97+
case AF_INET:
98+
sock_lock_init_class_and_name(sk, "slock-AF_INET-NFSD",
99+
&svc_slock_key[0], "sk_lock-AF_INET-NFSD", &svc_key[0]);
100+
break;
101+
102+
case AF_INET6:
103+
sock_lock_init_class_and_name(sk, "slock-AF_INET6-NFSD",
104+
&svc_slock_key[1], "sk_lock-AF_INET6-NFSD", &svc_key[1]);
105+
break;
106+
107+
default:
108+
BUG();
109+
}
110+
}
111+
#else
112+
static inline void svc_reclassify_socket(struct socket *sock)
113+
{
114+
}
115+
#endif
116+
88117
/*
89118
* Queue up an idle server thread. Must have pool->sp_lock held.
90119
* Note: this is really a stack rather than a queue, so that we only
@@ -1557,6 +1586,8 @@ svc_create_socket(struct svc_serv *serv, int protocol, struct sockaddr_in *sin)
15571586
if ((error = sock_create_kern(PF_INET, type, protocol, &sock)) < 0)
15581587
return error;
15591588

1589+
svc_reclassify_socket(sock);
1590+
15601591
if (type == SOCK_STREAM)
15611592
sock->sk->sk_reuse = 1; /* allow address reuse */
15621593
error = kernel_bind(sock, (struct sockaddr *) sin,

net/sunrpc/xprtsock.c

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1058,6 +1058,35 @@ static int xs_bindresvport(struct rpc_xprt *xprt, struct socket *sock)
10581058
return err;
10591059
}
10601060

1061+
#ifdef CONFIG_DEBUG_LOCK_ALLOC
1062+
static struct lock_class_key xs_key[2];
1063+
static struct lock_class_key xs_slock_key[2];
1064+
1065+
static inline void xs_reclassify_socket(struct socket *sock)
1066+
{
1067+
struct sock *sk = sock->sk;
1068+
BUG_ON(sk->sk_lock.owner != NULL);
1069+
switch (sk->sk_family) {
1070+
case AF_INET:
1071+
sock_lock_init_class_and_name(sk, "slock-AF_INET-NFS",
1072+
&xs_slock_key[0], "sk_lock-AF_INET-NFS", &xs_key[0]);
1073+
break;
1074+
1075+
case AF_INET6:
1076+
sock_lock_init_class_and_name(sk, "slock-AF_INET6-NFS",
1077+
&xs_slock_key[1], "sk_lock-AF_INET6-NFS", &xs_key[1]);
1078+
break;
1079+
1080+
default:
1081+
BUG();
1082+
}
1083+
}
1084+
#else
1085+
static inline void xs_reclassify_socket(struct socket *sock)
1086+
{
1087+
}
1088+
#endif
1089+
10611090
/**
10621091
* xs_udp_connect_worker - set up a UDP socket
10631092
* @work: RPC transport to connect
@@ -1081,6 +1110,7 @@ static void xs_udp_connect_worker(struct work_struct *work)
10811110
dprintk("RPC: can't create UDP transport socket (%d).\n", -err);
10821111
goto out;
10831112
}
1113+
xs_reclassify_socket(sock);
10841114

10851115
if (xprt->resvport && xs_bindresvport(xprt, sock) < 0) {
10861116
sock_release(sock);
@@ -1165,6 +1195,7 @@ static void xs_tcp_connect_worker(struct work_struct *work)
11651195
dprintk("RPC: can't create TCP transport socket (%d).\n", -err);
11661196
goto out;
11671197
}
1198+
xs_reclassify_socket(sock);
11681199

11691200
if (xprt->resvport && xs_bindresvport(xprt, sock) < 0) {
11701201
sock_release(sock);

0 commit comments

Comments
 (0)