Skip to content

Commit 0cfb6ae

Browse files
Guillaume Knispeltorvalds
authored andcommitted
ipc: optimize semget/shmget/msgget for lots of keys
ipc_findkey() used to scan all objects to look for the wanted key. This is slow when using a high number of keys. This change adds an rhashtable of kern_ipc_perm objects in ipc_ids, so that one lookup cease to be O(n). This change gives a 865% improvement of benchmark reaim.jobs_per_min on a 56 threads Intel(R) Xeon(R) CPU E5-2695 v3 @ 2.30GHz with 256G memory [1] Other (more micro) benchmark results, by the author: On an i5 laptop, the following loop executed right after a reboot took, without and with this change: for (int i = 0, k=0x424242; i < KEYS; ++i) semget(k++, 1, IPC_CREAT | 0600); total total max single max single KEYS without with call without call with 1 3.5 4.9 µs 3.5 4.9 10 7.6 8.6 µs 3.7 4.7 32 16.2 15.9 µs 4.3 5.3 100 72.9 41.8 µs 3.7 4.7 1000 5,630.0 502.0 µs * * 10000 1,340,000.0 7,240.0 µs * * 31900 17,600,000.0 22,200.0 µs * * *: unreliable measure: high variance The duration for a lookup-only usage was obtained by the same loop once the keys are present: total total max single max single KEYS without with call without call with 1 2.1 2.5 µs 2.1 2.5 10 4.5 4.8 µs 2.2 2.3 32 13.0 10.8 µs 2.3 2.8 100 82.9 25.1 µs * 2.3 1000 5,780.0 217.0 µs * * 10000 1,470,000.0 2,520.0 µs * * 31900 17,400,000.0 7,810.0 µs * * Finally, executing each semget() in a new process gave, when still summing only the durations of these syscalls: creation: total total KEYS without with 1 3.7 5.0 µs 10 32.9 36.7 µs 32 125.0 109.0 µs 100 523.0 353.0 µs 1000 20,300.0 3,280.0 µs 10000 2,470,000.0 46,700.0 µs 31900 27,800,000.0 219,000.0 µs lookup-only: total total KEYS without with 1 2.5 2.7 µs 10 25.4 24.4 µs 32 106.0 72.6 µs 100 591.0 352.0 µs 1000 22,400.0 2,250.0 µs 10000 2,510,000.0 25,700.0 µs 31900 28,200,000.0 115,000.0 µs [1] http://lkml.kernel.org/r/20170814060507.GE23258@yexl-desktop Link: http://lkml.kernel.org/r/20170815194954.ck32ta2z35yuzpwp@debix Signed-off-by: Guillaume Knispel <guillaume.knispel@supersonicimagine.com> Reviewed-by: Marc Pardo <marc.pardo@supersonicimagine.com> Cc: Davidlohr Bueso <dave@stgolabs.net> Cc: Kees Cook <keescook@chromium.org> Cc: Manfred Spraul <manfred@colorfullife.com> Cc: Alexey Dobriyan <adobriyan@gmail.com> Cc: "Eric W. Biederman" <ebiederm@xmission.com> Cc: "Peter Zijlstra (Intel)" <peterz@infradead.org> Cc: Ingo Molnar <mingo@kernel.org> Cc: Sebastian Andrzej Siewior <bigeasy@linutronix.de> Cc: Serge Hallyn <serge@hallyn.com> Cc: Andrey Vagin <avagin@openvz.org> Cc: Guillaume Knispel <guillaume.knispel@supersonicimagine.com> Cc: Marc Pardo <marc.pardo@supersonicimagine.com> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent e4243b8 commit 0cfb6ae

8 files changed

Lines changed: 130 additions & 50 deletions

File tree

‎include/linux/ipc.h‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
#include <linux/spinlock.h>
55
#include <linux/uidgid.h>
6+
#include <linux/rhashtable.h>
67
#include <uapi/linux/ipc.h>
78
#include <linux/refcount.h>
89

@@ -22,6 +23,8 @@ struct kern_ipc_perm {
2223
unsigned long seq;
2324
void *security;
2425

26+
struct rhash_head khtnode;
27+
2528
struct rcu_head rcu;
2629
refcount_t refcount;
2730
} ____cacheline_aligned_in_smp __randomize_layout;

‎include/linux/ipc_namespace.h‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,18 @@
88
#include <linux/nsproxy.h>
99
#include <linux/ns_common.h>
1010
#include <linux/refcount.h>
11+
#include <linux/rhashtable.h>
1112

1213
struct user_namespace;
1314

1415
struct ipc_ids {
1516
int in_use;
1617
unsigned short seq;
18+
bool tables_initialized;
1719
struct rw_semaphore rwsem;
1820
struct idr ipcs_idr;
1921
int next_id;
22+
struct rhashtable key_ht;
2023
};
2124

2225
struct ipc_namespace {

‎ipc/msg.c‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1011,22 +1011,23 @@ SYSCALL_DEFINE5(msgrcv, int, msqid, struct msgbuf __user *, msgp, size_t, msgsz,
10111011
}
10121012

10131013

1014-
void msg_init_ns(struct ipc_namespace *ns)
1014+
int msg_init_ns(struct ipc_namespace *ns)
10151015
{
10161016
ns->msg_ctlmax = MSGMAX;
10171017
ns->msg_ctlmnb = MSGMNB;
10181018
ns->msg_ctlmni = MSGMNI;
10191019

10201020
atomic_set(&ns->msg_bytes, 0);
10211021
atomic_set(&ns->msg_hdrs, 0);
1022-
ipc_init_ids(&ns->ids[IPC_MSG_IDS]);
1022+
return ipc_init_ids(&ns->ids[IPC_MSG_IDS]);
10231023
}
10241024

10251025
#ifdef CONFIG_IPC_NS
10261026
void msg_exit_ns(struct ipc_namespace *ns)
10271027
{
10281028
free_ipcs(ns, &msg_ids(ns), freeque);
10291029
idr_destroy(&ns->ids[IPC_MSG_IDS].ipcs_idr);
1030+
rhashtable_destroy(&ns->ids[IPC_MSG_IDS].key_ht);
10301031
}
10311032
#endif
10321033

@@ -1058,11 +1059,12 @@ static int sysvipc_msg_proc_show(struct seq_file *s, void *it)
10581059
}
10591060
#endif
10601061

1061-
void __init msg_init(void)
1062+
int __init msg_init(void)
10621063
{
1063-
msg_init_ns(&init_ipc_ns);
1064+
const int err = msg_init_ns(&init_ipc_ns);
10641065

10651066
ipc_init_proc_interface("sysvipc/msg",
10661067
" key msqid perms cbytes qnum lspid lrpid uid gid cuid cgid stime rtime ctime\n",
10671068
IPC_MSG_IDS, sysvipc_msg_proc_show);
1069+
return err;
10681070
}

‎ipc/namespace.c‎

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,16 +54,28 @@ static struct ipc_namespace *create_ipc_ns(struct user_namespace *user_ns,
5454
ns->user_ns = get_user_ns(user_ns);
5555
ns->ucounts = ucounts;
5656

57-
err = mq_init_ns(ns);
57+
err = sem_init_ns(ns);
5858
if (err)
5959
goto fail_put;
60+
err = msg_init_ns(ns);
61+
if (err)
62+
goto fail_destroy_sem;
63+
err = shm_init_ns(ns);
64+
if (err)
65+
goto fail_destroy_msg;
6066

61-
sem_init_ns(ns);
62-
msg_init_ns(ns);
63-
shm_init_ns(ns);
67+
err = mq_init_ns(ns);
68+
if (err)
69+
goto fail_destroy_shm;
6470

6571
return ns;
6672

73+
fail_destroy_shm:
74+
shm_exit_ns(ns);
75+
fail_destroy_msg:
76+
msg_exit_ns(ns);
77+
fail_destroy_sem:
78+
sem_exit_ns(ns);
6779
fail_put:
6880
put_user_ns(ns->user_ns);
6981
ns_free_inum(&ns->ns);

‎ipc/sem.c‎

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -183,30 +183,33 @@ static int sysvipc_sem_proc_show(struct seq_file *s, void *it);
183183
#define sc_semopm sem_ctls[2]
184184
#define sc_semmni sem_ctls[3]
185185

186-
void sem_init_ns(struct ipc_namespace *ns)
186+
int sem_init_ns(struct ipc_namespace *ns)
187187
{
188188
ns->sc_semmsl = SEMMSL;
189189
ns->sc_semmns = SEMMNS;
190190
ns->sc_semopm = SEMOPM;
191191
ns->sc_semmni = SEMMNI;
192192
ns->used_sems = 0;
193-
ipc_init_ids(&ns->ids[IPC_SEM_IDS]);
193+
return ipc_init_ids(&ns->ids[IPC_SEM_IDS]);
194194
}
195195

196196
#ifdef CONFIG_IPC_NS
197197
void sem_exit_ns(struct ipc_namespace *ns)
198198
{
199199
free_ipcs(ns, &sem_ids(ns), freeary);
200200
idr_destroy(&ns->ids[IPC_SEM_IDS].ipcs_idr);
201+
rhashtable_destroy(&ns->ids[IPC_SEM_IDS].key_ht);
201202
}
202203
#endif
203204

204-
void __init sem_init(void)
205+
int __init sem_init(void)
205206
{
206-
sem_init_ns(&init_ipc_ns);
207+
const int err = sem_init_ns(&init_ipc_ns);
208+
207209
ipc_init_proc_interface("sysvipc/sem",
208210
" key semid perms nsems uid gid cuid cgid otime ctime\n",
209211
IPC_SEM_IDS, sysvipc_sem_proc_show);
212+
return err;
210213
}
211214

212215
/**

‎ipc/shm.c‎

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -72,14 +72,14 @@ static void shm_destroy(struct ipc_namespace *ns, struct shmid_kernel *shp);
7272
static int sysvipc_shm_proc_show(struct seq_file *s, void *it);
7373
#endif
7474

75-
void shm_init_ns(struct ipc_namespace *ns)
75+
int shm_init_ns(struct ipc_namespace *ns)
7676
{
7777
ns->shm_ctlmax = SHMMAX;
7878
ns->shm_ctlall = SHMALL;
7979
ns->shm_ctlmni = SHMMNI;
8080
ns->shm_rmid_forced = 0;
8181
ns->shm_tot = 0;
82-
ipc_init_ids(&shm_ids(ns));
82+
return ipc_init_ids(&shm_ids(ns));
8383
}
8484

8585
/*
@@ -95,7 +95,7 @@ static void do_shm_rmid(struct ipc_namespace *ns, struct kern_ipc_perm *ipcp)
9595
if (shp->shm_nattch) {
9696
shp->shm_perm.mode |= SHM_DEST;
9797
/* Do not find it any more */
98-
shp->shm_perm.key = IPC_PRIVATE;
98+
ipc_set_key_private(&shm_ids(ns), &shp->shm_perm);
9999
shm_unlock(shp);
100100
} else
101101
shm_destroy(ns, shp);
@@ -106,13 +106,15 @@ void shm_exit_ns(struct ipc_namespace *ns)
106106
{
107107
free_ipcs(ns, &shm_ids(ns), do_shm_rmid);
108108
idr_destroy(&ns->ids[IPC_SHM_IDS].ipcs_idr);
109+
rhashtable_destroy(&ns->ids[IPC_SHM_IDS].key_ht);
109110
}
110111
#endif
111112

112113
static int __init ipc_ns_init(void)
113114
{
114-
shm_init_ns(&init_ipc_ns);
115-
return 0;
115+
const int err = shm_init_ns(&init_ipc_ns);
116+
WARN(err, "ipc: sysv shm_init_ns failed: %d\n", err);
117+
return err;
116118
}
117119

118120
pure_initcall(ipc_ns_init);

‎ipc/util.c‎

Lines changed: 76 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -83,27 +83,46 @@ struct ipc_proc_iface {
8383
*/
8484
static int __init ipc_init(void)
8585
{
86-
sem_init();
87-
msg_init();
86+
int err_sem, err_msg;
87+
88+
err_sem = sem_init();
89+
WARN(err_sem, "ipc: sysv sem_init failed: %d\n", err_sem);
90+
err_msg = msg_init();
91+
WARN(err_msg, "ipc: sysv msg_init failed: %d\n", err_msg);
8892
shm_init();
89-
return 0;
93+
94+
return err_msg ? err_msg : err_sem;
9095
}
9196
device_initcall(ipc_init);
9297

98+
static const struct rhashtable_params ipc_kht_params = {
99+
.head_offset = offsetof(struct kern_ipc_perm, khtnode),
100+
.key_offset = offsetof(struct kern_ipc_perm, key),
101+
.key_len = FIELD_SIZEOF(struct kern_ipc_perm, key),
102+
.locks_mul = 1,
103+
.automatic_shrinking = true,
104+
};
105+
93106
/**
94107
* ipc_init_ids - initialise ipc identifiers
95108
* @ids: ipc identifier set
96109
*
97110
* Set up the sequence range to use for the ipc identifier range (limited
98-
* below IPCMNI) then initialise the ids idr.
111+
* below IPCMNI) then initialise the keys hashtable and ids idr.
99112
*/
100-
void ipc_init_ids(struct ipc_ids *ids)
113+
int ipc_init_ids(struct ipc_ids *ids)
101114
{
115+
int err;
102116
ids->in_use = 0;
103117
ids->seq = 0;
104118
ids->next_id = -1;
105119
init_rwsem(&ids->rwsem);
120+
err = rhashtable_init(&ids->key_ht, &ipc_kht_params);
121+
if (err)
122+
return err;
106123
idr_init(&ids->ipcs_idr);
124+
ids->tables_initialized = true;
125+
return 0;
107126
}
108127

109128
#ifdef CONFIG_PROC_FS
@@ -147,28 +166,20 @@ void __init ipc_init_proc_interface(const char *path, const char *header,
147166
* Returns the locked pointer to the ipc structure if found or NULL
148167
* otherwise. If key is found ipc points to the owning ipc structure
149168
*
150-
* Called with ipc_ids.rwsem held.
169+
* Called with writer ipc_ids.rwsem held.
151170
*/
152171
static struct kern_ipc_perm *ipc_findkey(struct ipc_ids *ids, key_t key)
153172
{
154-
struct kern_ipc_perm *ipc;
155-
int next_id;
156-
int total;
157-
158-
for (total = 0, next_id = 0; total < ids->in_use; next_id++) {
159-
ipc = idr_find(&ids->ipcs_idr, next_id);
160-
161-
if (ipc == NULL)
162-
continue;
173+
struct kern_ipc_perm *ipcp = NULL;
163174

164-
if (ipc->key != key) {
165-
total++;
166-
continue;
167-
}
175+
if (likely(ids->tables_initialized))
176+
ipcp = rhashtable_lookup_fast(&ids->key_ht, &key,
177+
ipc_kht_params);
168178

179+
if (ipcp) {
169180
rcu_read_lock();
170-
ipc_lock_object(ipc);
171-
return ipc;
181+
ipc_lock_object(ipcp);
182+
return ipcp;
172183
}
173184

174185
return NULL;
@@ -221,13 +232,13 @@ int ipc_addid(struct ipc_ids *ids, struct kern_ipc_perm *new, int size)
221232
{
222233
kuid_t euid;
223234
kgid_t egid;
224-
int id;
235+
int id, err;
225236
int next_id = ids->next_id;
226237

227238
if (size > IPCMNI)
228239
size = IPCMNI;
229240

230-
if (ids->in_use >= size)
241+
if (!ids->tables_initialized || ids->in_use >= size)
231242
return -ENOSPC;
232243

233244
idr_preload(GFP_KERNEL);
@@ -246,6 +257,15 @@ int ipc_addid(struct ipc_ids *ids, struct kern_ipc_perm *new, int size)
246257
(next_id < 0) ? 0 : ipcid_to_idx(next_id), 0,
247258
GFP_NOWAIT);
248259
idr_preload_end();
260+
261+
if (id >= 0 && new->key != IPC_PRIVATE) {
262+
err = rhashtable_insert_fast(&ids->key_ht, &new->khtnode,
263+
ipc_kht_params);
264+
if (err < 0) {
265+
idr_remove(&ids->ipcs_idr, id);
266+
id = err;
267+
}
268+
}
249269
if (id < 0) {
250270
spin_unlock(&new->lock);
251271
rcu_read_unlock();
@@ -377,6 +397,20 @@ static int ipcget_public(struct ipc_namespace *ns, struct ipc_ids *ids,
377397
return err;
378398
}
379399

400+
/**
401+
* ipc_kht_remove - remove an ipc from the key hashtable
402+
* @ids: ipc identifier set
403+
* @ipcp: ipc perm structure containing the key to remove
404+
*
405+
* ipc_ids.rwsem (as a writer) and the spinlock for this ID are held
406+
* before this function is called, and remain locked on the exit.
407+
*/
408+
static void ipc_kht_remove(struct ipc_ids *ids, struct kern_ipc_perm *ipcp)
409+
{
410+
if (ipcp->key != IPC_PRIVATE)
411+
rhashtable_remove_fast(&ids->key_ht, &ipcp->khtnode,
412+
ipc_kht_params);
413+
}
380414

381415
/**
382416
* ipc_rmid - remove an ipc identifier
@@ -391,10 +425,25 @@ void ipc_rmid(struct ipc_ids *ids, struct kern_ipc_perm *ipcp)
391425
int lid = ipcid_to_idx(ipcp->id);
392426

393427
idr_remove(&ids->ipcs_idr, lid);
428+
ipc_kht_remove(ids, ipcp);
394429
ids->in_use--;
395430
ipcp->deleted = true;
396431
}
397432

433+
/**
434+
* ipc_set_key_private - switch the key of an existing ipc to IPC_PRIVATE
435+
* @ids: ipc identifier set
436+
* @ipcp: ipc perm structure containing the key to modify
437+
*
438+
* ipc_ids.rwsem (as a writer) and the spinlock for this ID are held
439+
* before this function is called, and remain locked on the exit.
440+
*/
441+
void ipc_set_key_private(struct ipc_ids *ids, struct kern_ipc_perm *ipcp)
442+
{
443+
ipc_kht_remove(ids, ipcp);
444+
ipcp->key = IPC_PRIVATE;
445+
}
446+
398447
int ipc_rcu_getref(struct kern_ipc_perm *ptr)
399448
{
400449
return refcount_inc_not_zero(&ptr->refcount);
@@ -485,7 +534,7 @@ void ipc64_perm_to_ipc_perm(struct ipc64_perm *in, struct ipc_perm *out)
485534
}
486535

487536
/**
488-
* ipc_obtain_object
537+
* ipc_obtain_object_idr
489538
* @ids: ipc identifier set
490539
* @id: ipc id to look for
491540
*
@@ -499,6 +548,9 @@ struct kern_ipc_perm *ipc_obtain_object_idr(struct ipc_ids *ids, int id)
499548
struct kern_ipc_perm *out;
500549
int lid = ipcid_to_idx(id);
501550

551+
if (unlikely(!ids->tables_initialized))
552+
return ERR_PTR(-EINVAL);
553+
502554
out = idr_find(&ids->ipcs_idr, lid);
503555
if (!out)
504556
return ERR_PTR(-EINVAL);

0 commit comments

Comments
 (0)