Skip to content

Commit dba4cdd

Browse files
manfred-colorfutorvalds
authored andcommitted
ipc: merge ipc_rcu and kern_ipc_perm
ipc has two management structures that exist for every id: - struct kern_ipc_perm, it contains e.g. the permissions. - struct ipc_rcu, it contains the rcu head for rcu handling and the refcount. The patch merges both structures. As a bonus, we may save one cacheline, because both structures are cacheline aligned. In addition, it reduces the number of casts, instead most codepaths can use container_of. To simplify code, the ipc_rcu_alloc initializes the allocation to 0. [manfred@colorfullife.com: really include the memset() into ipc_alloc_rcu()] Link: http://lkml.kernel.org/r/564f8612-0601-b267-514f-a9f650ec9b32@colorfullife.com Link: http://lkml.kernel.org/r/20170525185107.12869-3-manfred@colorfullife.com Signed-off-by: Manfred Spraul <manfred@colorfullife.com> Cc: Davidlohr Bueso <dave@stgolabs.net> Cc: Kees Cook <keescook@chromium.org> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent 1a23395 commit dba4cdd

6 files changed

Lines changed: 66 additions & 61 deletions

File tree

‎include/linux/ipc.h‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ struct kern_ipc_perm {
2020
umode_t mode;
2121
unsigned long seq;
2222
void *security;
23+
24+
struct rcu_head rcu;
25+
atomic_t refcount;
2326
} ____cacheline_aligned_in_smp;
2427

2528
#endif /* _LINUX_IPC_H */

‎ipc/msg.c‎

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -97,8 +97,8 @@ static inline void msg_rmid(struct ipc_namespace *ns, struct msg_queue *s)
9797

9898
static void msg_rcu_free(struct rcu_head *head)
9999
{
100-
struct ipc_rcu *p = container_of(head, struct ipc_rcu, rcu);
101-
struct msg_queue *msq = ipc_rcu_to_struct(p);
100+
struct kern_ipc_perm *p = container_of(head, struct kern_ipc_perm, rcu);
101+
struct msg_queue *msq = container_of(p, struct msg_queue, q_perm);
102102

103103
security_msg_queue_free(msq);
104104
ipc_rcu_free(head);
@@ -118,7 +118,10 @@ static int newque(struct ipc_namespace *ns, struct ipc_params *params)
118118
key_t key = params->key;
119119
int msgflg = params->flg;
120120

121-
msq = ipc_rcu_alloc(sizeof(*msq));
121+
BUILD_BUG_ON(offsetof(struct msg_queue, q_perm) != 0);
122+
123+
msq = container_of(ipc_rcu_alloc(sizeof(*msq)), struct msg_queue,
124+
q_perm);
122125
if (!msq)
123126
return -ENOMEM;
124127

@@ -128,7 +131,7 @@ static int newque(struct ipc_namespace *ns, struct ipc_params *params)
128131
msq->q_perm.security = NULL;
129132
retval = security_msg_queue_alloc(msq);
130133
if (retval) {
131-
ipc_rcu_putref(msq, ipc_rcu_free);
134+
ipc_rcu_putref(&msq->q_perm, ipc_rcu_free);
132135
return retval;
133136
}
134137

@@ -144,7 +147,7 @@ static int newque(struct ipc_namespace *ns, struct ipc_params *params)
144147
/* ipc_addid() locks msq upon success. */
145148
id = ipc_addid(&msg_ids(ns), &msq->q_perm, ns->msg_ctlmni);
146149
if (id < 0) {
147-
ipc_rcu_putref(msq, msg_rcu_free);
150+
ipc_rcu_putref(&msq->q_perm, msg_rcu_free);
148151
return id;
149152
}
150153

@@ -249,7 +252,7 @@ static void freeque(struct ipc_namespace *ns, struct kern_ipc_perm *ipcp)
249252
free_msg(msg);
250253
}
251254
atomic_sub(msq->q_cbytes, &ns->msg_bytes);
252-
ipc_rcu_putref(msq, msg_rcu_free);
255+
ipc_rcu_putref(&msq->q_perm, msg_rcu_free);
253256
}
254257

255258
/*
@@ -688,7 +691,7 @@ long do_msgsnd(int msqid, long mtype, void __user *mtext,
688691
/* enqueue the sender and prepare to block */
689692
ss_add(msq, &s, msgsz);
690693

691-
if (!ipc_rcu_getref(msq)) {
694+
if (!ipc_rcu_getref(&msq->q_perm)) {
692695
err = -EIDRM;
693696
goto out_unlock0;
694697
}
@@ -700,7 +703,7 @@ long do_msgsnd(int msqid, long mtype, void __user *mtext,
700703
rcu_read_lock();
701704
ipc_lock_object(&msq->q_perm);
702705

703-
ipc_rcu_putref(msq, msg_rcu_free);
706+
ipc_rcu_putref(&msq->q_perm, msg_rcu_free);
704707
/* raced with RMID? */
705708
if (!ipc_valid_object(&msq->q_perm)) {
706709
err = -EIDRM;

‎ipc/sem.c‎

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -260,8 +260,8 @@ static void merge_queues(struct sem_array *sma)
260260

261261
static void sem_rcu_free(struct rcu_head *head)
262262
{
263-
struct ipc_rcu *p = container_of(head, struct ipc_rcu, rcu);
264-
struct sem_array *sma = ipc_rcu_to_struct(p);
263+
struct kern_ipc_perm *p = container_of(head, struct kern_ipc_perm, rcu);
264+
struct sem_array *sma = container_of(p, struct sem_array, sem_perm);
265265

266266
security_sem_free(sma);
267267
ipc_rcu_free(head);
@@ -438,7 +438,7 @@ static inline struct sem_array *sem_obtain_object_check(struct ipc_namespace *ns
438438
static inline void sem_lock_and_putref(struct sem_array *sma)
439439
{
440440
sem_lock(sma, NULL, -1);
441-
ipc_rcu_putref(sma, sem_rcu_free);
441+
ipc_rcu_putref(&sma->sem_perm, sem_rcu_free);
442442
}
443443

444444
static inline void sem_rmid(struct ipc_namespace *ns, struct sem_array *s)
@@ -469,20 +469,20 @@ static int newary(struct ipc_namespace *ns, struct ipc_params *params)
469469
if (ns->used_sems + nsems > ns->sc_semmns)
470470
return -ENOSPC;
471471

472+
BUILD_BUG_ON(offsetof(struct sem_array, sem_perm) != 0);
473+
472474
size = sizeof(*sma) + nsems * sizeof(sma->sems[0]);
473-
sma = ipc_rcu_alloc(size);
475+
sma = container_of(ipc_rcu_alloc(size), struct sem_array, sem_perm);
474476
if (!sma)
475477
return -ENOMEM;
476478

477-
memset(sma, 0, size);
478-
479479
sma->sem_perm.mode = (semflg & S_IRWXUGO);
480480
sma->sem_perm.key = key;
481481

482482
sma->sem_perm.security = NULL;
483483
retval = security_sem_alloc(sma);
484484
if (retval) {
485-
ipc_rcu_putref(sma, ipc_rcu_free);
485+
ipc_rcu_putref(&sma->sem_perm, ipc_rcu_free);
486486
return retval;
487487
}
488488

@@ -502,7 +502,7 @@ static int newary(struct ipc_namespace *ns, struct ipc_params *params)
502502

503503
id = ipc_addid(&sem_ids(ns), &sma->sem_perm, ns->sc_semmni);
504504
if (id < 0) {
505-
ipc_rcu_putref(sma, sem_rcu_free);
505+
ipc_rcu_putref(&sma->sem_perm, sem_rcu_free);
506506
return id;
507507
}
508508
ns->used_sems += nsems;
@@ -1122,7 +1122,7 @@ static void freeary(struct ipc_namespace *ns, struct kern_ipc_perm *ipcp)
11221122

11231123
wake_up_q(&wake_q);
11241124
ns->used_sems -= sma->sem_nsems;
1125-
ipc_rcu_putref(sma, sem_rcu_free);
1125+
ipc_rcu_putref(&sma->sem_perm, sem_rcu_free);
11261126
}
11271127

11281128
static unsigned long copy_semid_to_user(void __user *buf, struct semid64_ds *in, int version)
@@ -1362,15 +1362,15 @@ static int semctl_main(struct ipc_namespace *ns, int semid, int semnum,
13621362
goto out_unlock;
13631363
}
13641364
if (nsems > SEMMSL_FAST) {
1365-
if (!ipc_rcu_getref(sma)) {
1365+
if (!ipc_rcu_getref(&sma->sem_perm)) {
13661366
err = -EIDRM;
13671367
goto out_unlock;
13681368
}
13691369
sem_unlock(sma, -1);
13701370
rcu_read_unlock();
13711371
sem_io = ipc_alloc(sizeof(ushort)*nsems);
13721372
if (sem_io == NULL) {
1373-
ipc_rcu_putref(sma, sem_rcu_free);
1373+
ipc_rcu_putref(&sma->sem_perm, sem_rcu_free);
13741374
return -ENOMEM;
13751375
}
13761376

@@ -1395,7 +1395,7 @@ static int semctl_main(struct ipc_namespace *ns, int semid, int semnum,
13951395
int i;
13961396
struct sem_undo *un;
13971397

1398-
if (!ipc_rcu_getref(sma)) {
1398+
if (!ipc_rcu_getref(&sma->sem_perm)) {
13991399
err = -EIDRM;
14001400
goto out_rcu_wakeup;
14011401
}
@@ -1404,20 +1404,20 @@ static int semctl_main(struct ipc_namespace *ns, int semid, int semnum,
14041404
if (nsems > SEMMSL_FAST) {
14051405
sem_io = ipc_alloc(sizeof(ushort)*nsems);
14061406
if (sem_io == NULL) {
1407-
ipc_rcu_putref(sma, sem_rcu_free);
1407+
ipc_rcu_putref(&sma->sem_perm, sem_rcu_free);
14081408
return -ENOMEM;
14091409
}
14101410
}
14111411

14121412
if (copy_from_user(sem_io, p, nsems*sizeof(ushort))) {
1413-
ipc_rcu_putref(sma, sem_rcu_free);
1413+
ipc_rcu_putref(&sma->sem_perm, sem_rcu_free);
14141414
err = -EFAULT;
14151415
goto out_free;
14161416
}
14171417

14181418
for (i = 0; i < nsems; i++) {
14191419
if (sem_io[i] > SEMVMX) {
1420-
ipc_rcu_putref(sma, sem_rcu_free);
1420+
ipc_rcu_putref(&sma->sem_perm, sem_rcu_free);
14211421
err = -ERANGE;
14221422
goto out_free;
14231423
}
@@ -1699,7 +1699,7 @@ static struct sem_undo *find_alloc_undo(struct ipc_namespace *ns, int semid)
16991699
}
17001700

17011701
nsems = sma->sem_nsems;
1702-
if (!ipc_rcu_getref(sma)) {
1702+
if (!ipc_rcu_getref(&sma->sem_perm)) {
17031703
rcu_read_unlock();
17041704
un = ERR_PTR(-EIDRM);
17051705
goto out;
@@ -1709,7 +1709,7 @@ static struct sem_undo *find_alloc_undo(struct ipc_namespace *ns, int semid)
17091709
/* step 2: allocate new undo structure */
17101710
new = kzalloc(sizeof(struct sem_undo) + sizeof(short)*nsems, GFP_KERNEL);
17111711
if (!new) {
1712-
ipc_rcu_putref(sma, sem_rcu_free);
1712+
ipc_rcu_putref(&sma->sem_perm, sem_rcu_free);
17131713
return ERR_PTR(-ENOMEM);
17141714
}
17151715

‎ipc/shm.c‎

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -174,9 +174,10 @@ static inline void shm_lock_by_ptr(struct shmid_kernel *ipcp)
174174

175175
static void shm_rcu_free(struct rcu_head *head)
176176
{
177-
struct ipc_rcu *p = container_of(head, struct ipc_rcu, rcu);
178-
struct shmid_kernel *shp = ipc_rcu_to_struct(p);
179-
177+
struct kern_ipc_perm *ptr = container_of(head, struct kern_ipc_perm,
178+
rcu);
179+
struct shmid_kernel *shp = container_of(ptr, struct shmid_kernel,
180+
shm_perm);
180181
security_shm_free(shp);
181182
ipc_rcu_free(head);
182183
}
@@ -241,7 +242,7 @@ static void shm_destroy(struct ipc_namespace *ns, struct shmid_kernel *shp)
241242
user_shm_unlock(i_size_read(file_inode(shm_file)),
242243
shp->mlock_user);
243244
fput(shm_file);
244-
ipc_rcu_putref(shp, shm_rcu_free);
245+
ipc_rcu_putref(&shp->shm_perm, shm_rcu_free);
245246
}
246247

247248
/*
@@ -542,7 +543,10 @@ static int newseg(struct ipc_namespace *ns, struct ipc_params *params)
542543
ns->shm_tot + numpages > ns->shm_ctlall)
543544
return -ENOSPC;
544545

545-
shp = ipc_rcu_alloc(sizeof(*shp));
546+
BUILD_BUG_ON(offsetof(struct shmid_kernel, shm_perm) != 0);
547+
548+
shp = container_of(ipc_rcu_alloc(sizeof(*shp)), struct shmid_kernel,
549+
shm_perm);
546550
if (!shp)
547551
return -ENOMEM;
548552

@@ -553,7 +557,7 @@ static int newseg(struct ipc_namespace *ns, struct ipc_params *params)
553557
shp->shm_perm.security = NULL;
554558
error = security_shm_alloc(shp);
555559
if (error) {
556-
ipc_rcu_putref(shp, ipc_rcu_free);
560+
ipc_rcu_putref(&shp->shm_perm, ipc_rcu_free);
557561
return error;
558562
}
559563

@@ -624,7 +628,7 @@ static int newseg(struct ipc_namespace *ns, struct ipc_params *params)
624628
user_shm_unlock(size, shp->mlock_user);
625629
fput(file);
626630
no_file:
627-
ipc_rcu_putref(shp, shm_rcu_free);
631+
ipc_rcu_putref(&shp->shm_perm, shm_rcu_free);
628632
return error;
629633
}
630634

‎ipc/util.c‎

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -418,46 +418,45 @@ void ipc_free(void *ptr)
418418
}
419419

420420
/**
421-
* ipc_rcu_alloc - allocate ipc and rcu space
421+
* ipc_rcu_alloc - allocate ipc space
422422
* @size: size desired
423423
*
424-
* Allocate memory for the rcu header structure + the object.
425-
* Returns the pointer to the object or NULL upon failure.
424+
* Allocate memory for an ipc object.
425+
* The first member must be struct kern_ipc_perm.
426426
*/
427-
void *ipc_rcu_alloc(int size)
427+
struct kern_ipc_perm *ipc_rcu_alloc(int size)
428428
{
429429
/*
430430
* We prepend the allocation with the rcu struct
431431
*/
432-
struct ipc_rcu *out = ipc_alloc(sizeof(struct ipc_rcu) + size);
432+
struct kern_ipc_perm *out = ipc_alloc(size);
433433
if (unlikely(!out))
434434
return NULL;
435+
436+
memset(out, 0, size);
435437
atomic_set(&out->refcount, 1);
436-
return out + 1;
438+
return out;
437439
}
438440

439-
int ipc_rcu_getref(void *ptr)
441+
int ipc_rcu_getref(struct kern_ipc_perm *ptr)
440442
{
441-
struct ipc_rcu *p = ((struct ipc_rcu *)ptr) - 1;
442-
443-
return atomic_inc_not_zero(&p->refcount);
443+
return atomic_inc_not_zero(&ptr->refcount);
444444
}
445445

446-
void ipc_rcu_putref(void *ptr, void (*func)(struct rcu_head *head))
446+
void ipc_rcu_putref(struct kern_ipc_perm *ptr,
447+
void (*func)(struct rcu_head *head))
447448
{
448-
struct ipc_rcu *p = ((struct ipc_rcu *)ptr) - 1;
449-
450-
if (!atomic_dec_and_test(&p->refcount))
449+
if (!atomic_dec_and_test(&ptr->refcount))
451450
return;
452451

453-
call_rcu(&p->rcu, func);
452+
call_rcu(&ptr->rcu, func);
454453
}
455454

456-
void ipc_rcu_free(struct rcu_head *head)
455+
void ipc_rcu_free(struct rcu_head *h)
457456
{
458-
struct ipc_rcu *p = container_of(head, struct ipc_rcu, rcu);
457+
struct kern_ipc_perm *ptr = container_of(h, struct kern_ipc_perm, rcu);
459458

460-
kvfree(p);
459+
kvfree(ptr);
461460
}
462461

463462
/**

‎ipc/util.h‎

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,6 @@ static inline void msg_exit_ns(struct ipc_namespace *ns) { }
4747
static inline void shm_exit_ns(struct ipc_namespace *ns) { }
4848
#endif
4949

50-
struct ipc_rcu {
51-
struct rcu_head rcu;
52-
atomic_t refcount;
53-
} ____cacheline_aligned_in_smp;
54-
55-
#define ipc_rcu_to_struct(p) ((void *)(p+1))
56-
5750
/*
5851
* Structure that holds the parameters needed by the ipc operations
5952
* (see after)
@@ -125,11 +118,14 @@ void ipc_free(void *ptr);
125118
* Objects are reference counted, they start with reference count 1.
126119
* getref increases the refcount, the putref call that reduces the recount
127120
* to 0 schedules the rcu destruction. Caller must guarantee locking.
121+
*
122+
* struct kern_ipc_perm must be the first member in the allocated structure.
128123
*/
129-
void *ipc_rcu_alloc(int size);
130-
int ipc_rcu_getref(void *ptr);
131-
void ipc_rcu_putref(void *ptr, void (*func)(struct rcu_head *head));
132-
void ipc_rcu_free(struct rcu_head *head);
124+
struct kern_ipc_perm *ipc_rcu_alloc(int size);
125+
int ipc_rcu_getref(struct kern_ipc_perm *ptr);
126+
void ipc_rcu_putref(struct kern_ipc_perm *ptr,
127+
void (*func)(struct rcu_head *head));
128+
void ipc_rcu_free(struct rcu_head *h);
133129

134130
struct kern_ipc_perm *ipc_lock(struct ipc_ids *, int);
135131
struct kern_ipc_perm *ipc_obtain_object_idr(struct ipc_ids *ids, int id);

0 commit comments

Comments
 (0)