Skip to content

Commit 16e5726

Browse files
Eric Dumazetdavem330
authored andcommitted
af_unix: dont send SCM_CREDENTIALS by default
Since commit 7361c36 (af_unix: Allow credentials to work across user and pid namespaces) af_unix performance dropped a lot. This is because we now take a reference on pid and cred in each write(), and release them in read(), usually done from another process, eventually from another cpu. This triggers false sharing. # Events: 154K cycles # # Overhead Command Shared Object Symbol # ........ ....... .................. ......................... # 10.40% hackbench [kernel.kallsyms] [k] put_pid 8.60% hackbench [kernel.kallsyms] [k] unix_stream_recvmsg 7.87% hackbench [kernel.kallsyms] [k] unix_stream_sendmsg 6.11% hackbench [kernel.kallsyms] [k] do_raw_spin_lock 4.95% hackbench [kernel.kallsyms] [k] unix_scm_to_skb 4.87% hackbench [kernel.kallsyms] [k] pid_nr_ns 4.34% hackbench [kernel.kallsyms] [k] cred_to_ucred 2.39% hackbench [kernel.kallsyms] [k] unix_destruct_scm 2.24% hackbench [kernel.kallsyms] [k] sub_preempt_count 1.75% hackbench [kernel.kallsyms] [k] fget_light 1.51% hackbench [kernel.kallsyms] [k] __mutex_lock_interruptible_slowpath 1.42% hackbench [kernel.kallsyms] [k] sock_alloc_send_pskb This patch includes SCM_CREDENTIALS information in a af_unix message/skb only if requested by the sender, [man 7 unix for details how to include ancillary data using sendmsg() system call] Note: This might break buggy applications that expected SCM_CREDENTIAL from an unaware write() system call, and receiver not using SO_PASSCRED socket option. If SOCK_PASSCRED is set on source or destination socket, we still include credentials for mere write() syscalls. Performance boost in hackbench : more than 50% gain on a 16 thread machine (2 quad-core cpus, 2 threads per core) hackbench 20 thread 2000 4.228 sec instead of 9.102 sec Signed-off-by: Eric Dumazet <[email protected]> Acked-by: Tim Chen <[email protected]> Signed-off-by: David S. Miller <[email protected]>
1 parent a9e9fd7 commit 16e5726

File tree

4 files changed

+33
-11
lines changed

4 files changed

+33
-11
lines changed

include/net/scm.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ static __inline__ void scm_set_cred(struct scm_cookie *scm,
4949
struct pid *pid, const struct cred *cred)
5050
{
5151
scm->pid = get_pid(pid);
52-
scm->cred = get_cred(cred);
52+
scm->cred = cred ? get_cred(cred) : NULL;
5353
cred_to_ucred(pid, cred, &scm->creds);
5454
}
5555

@@ -73,8 +73,7 @@ static __inline__ void scm_destroy(struct scm_cookie *scm)
7373
static __inline__ int scm_send(struct socket *sock, struct msghdr *msg,
7474
struct scm_cookie *scm)
7575
{
76-
scm_set_cred(scm, task_tgid(current), current_cred());
77-
scm->fp = NULL;
76+
memset(scm, 0, sizeof(*scm));
7877
unix_get_peersec_dgram(sock, scm);
7978
if (msg->msg_controllen <= 0)
8079
return 0;

net/core/scm.c

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ int __scm_send(struct socket *sock, struct msghdr *msg, struct scm_cookie *p)
173173
if (err)
174174
goto error;
175175

176-
if (pid_vnr(p->pid) != p->creds.pid) {
176+
if (!p->pid || pid_vnr(p->pid) != p->creds.pid) {
177177
struct pid *pid;
178178
err = -ESRCH;
179179
pid = find_get_pid(p->creds.pid);
@@ -183,8 +183,9 @@ int __scm_send(struct socket *sock, struct msghdr *msg, struct scm_cookie *p)
183183
p->pid = pid;
184184
}
185185

186-
if ((p->cred->euid != p->creds.uid) ||
187-
(p->cred->egid != p->creds.gid)) {
186+
if (!p->cred ||
187+
(p->cred->euid != p->creds.uid) ||
188+
(p->cred->egid != p->creds.gid)) {
188189
struct cred *cred;
189190
err = -ENOMEM;
190191
cred = prepare_creds();
@@ -193,7 +194,8 @@ int __scm_send(struct socket *sock, struct msghdr *msg, struct scm_cookie *p)
193194

194195
cred->uid = cred->euid = p->creds.uid;
195196
cred->gid = cred->egid = p->creds.gid;
196-
put_cred(p->cred);
197+
if (p->cred)
198+
put_cred(p->cred);
197199
p->cred = cred;
198200
}
199201
break;

net/netlink/af_netlink.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1324,10 +1324,9 @@ static int netlink_sendmsg(struct kiocb *kiocb, struct socket *sock,
13241324
if (msg->msg_flags&MSG_OOB)
13251325
return -EOPNOTSUPP;
13261326

1327-
if (NULL == siocb->scm) {
1327+
if (NULL == siocb->scm)
13281328
siocb->scm = &scm;
1329-
memset(&scm, 0, sizeof(scm));
1330-
}
1329+
13311330
err = scm_send(sock, msg, siocb->scm);
13321331
if (err < 0)
13331332
return err;

net/unix/af_unix.c

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1381,8 +1381,10 @@ static int unix_attach_fds(struct scm_cookie *scm, struct sk_buff *skb)
13811381
static int unix_scm_to_skb(struct scm_cookie *scm, struct sk_buff *skb, bool send_fds)
13821382
{
13831383
int err = 0;
1384+
13841385
UNIXCB(skb).pid = get_pid(scm->pid);
1385-
UNIXCB(skb).cred = get_cred(scm->cred);
1386+
if (scm->cred)
1387+
UNIXCB(skb).cred = get_cred(scm->cred);
13861388
UNIXCB(skb).fp = NULL;
13871389
if (scm->fp && send_fds)
13881390
err = unix_attach_fds(scm, skb);
@@ -1391,6 +1393,24 @@ static int unix_scm_to_skb(struct scm_cookie *scm, struct sk_buff *skb, bool sen
13911393
return err;
13921394
}
13931395

1396+
/*
1397+
* Some apps rely on write() giving SCM_CREDENTIALS
1398+
* We include credentials if source or destination socket
1399+
* asserted SOCK_PASSCRED.
1400+
*/
1401+
static void maybe_add_creds(struct sk_buff *skb, const struct socket *sock,
1402+
const struct sock *other)
1403+
{
1404+
if (UNIXCB(skb).cred)
1405+
return;
1406+
if (test_bit(SOCK_PASSCRED, &sock->flags) ||
1407+
!other->sk_socket ||
1408+
test_bit(SOCK_PASSCRED, &other->sk_socket->flags)) {
1409+
UNIXCB(skb).pid = get_pid(task_tgid(current));
1410+
UNIXCB(skb).cred = get_current_cred();
1411+
}
1412+
}
1413+
13941414
/*
13951415
* Send AF_UNIX data.
13961416
*/
@@ -1538,6 +1558,7 @@ static int unix_dgram_sendmsg(struct kiocb *kiocb, struct socket *sock,
15381558

15391559
if (sock_flag(other, SOCK_RCVTSTAMP))
15401560
__net_timestamp(skb);
1561+
maybe_add_creds(skb, sock, other);
15411562
skb_queue_tail(&other->sk_receive_queue, skb);
15421563
if (max_level > unix_sk(other)->recursion_level)
15431564
unix_sk(other)->recursion_level = max_level;
@@ -1652,6 +1673,7 @@ static int unix_stream_sendmsg(struct kiocb *kiocb, struct socket *sock,
16521673
(other->sk_shutdown & RCV_SHUTDOWN))
16531674
goto pipe_err_free;
16541675

1676+
maybe_add_creds(skb, sock, other);
16551677
skb_queue_tail(&other->sk_receive_queue, skb);
16561678
if (max_level > unix_sk(other)->recursion_level)
16571679
unix_sk(other)->recursion_level = max_level;

0 commit comments

Comments
 (0)