Skip to content

Commit ed2ddbf

Browse files
Pierre PeifferLinus Torvalds
authored andcommitted
IPC: make struct ipc_ids static in ipc_namespace
Each ipc_namespace contains a table of 3 pointers to struct ipc_ids (3 for msg, sem and shm, structure used to store all ipcs) These 'struct ipc_ids' are dynamically allocated for each icp_namespace as the ipc_namespace itself (for the init namespace, they are initialized with pointers to static variables instead) It is so for historical reason: in fact, before the use of idr to store the ipcs, the ipcs were stored in tables of variable length, depending of the maximum number of ipc allowed. Now, these 'struct ipc_ids' have a fixed size. As they are allocated in any cases for each new ipc_namespace, there is no gain of memory in having them allocated separately of the struct ipc_namespace. This patch proposes to make this table static in the struct ipc_namespace. Thus, we can allocate all in once and get rid of all the code needed to allocate and free these ipc_ids separately. Signed-off-by: Pierre Peiffer <pierre.peiffer@bull.net> Acked-by: Cedric Le Goater <clg@fr.ibm.com> Cc: Pavel Emelyanov <xemul@openvz.org> Cc: Nadia Derbey <Nadia.Derbey@bull.net> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent 4b9fcb0 commit ed2ddbf

7 files changed

Lines changed: 34 additions & 104 deletions

File tree

‎include/linux/ipc_namespace.h‎

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,20 @@
22
#define __IPC_NAMESPACE_H__
33

44
#include <linux/err.h>
5+
#include <linux/idr.h>
6+
#include <linux/rwsem.h>
7+
8+
struct ipc_ids {
9+
int in_use;
10+
unsigned short seq;
11+
unsigned short seq_max;
12+
struct rw_semaphore rw_mutex;
13+
struct idr ipcs_idr;
14+
};
515

6-
struct ipc_ids;
716
struct ipc_namespace {
817
struct kref kref;
9-
struct ipc_ids *ids[3];
18+
struct ipc_ids ids[3];
1019

1120
int sem_ctls[4];
1221
int used_sems;

‎ipc/msg.c‎

Lines changed: 4 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,7 @@ struct msg_sender {
6767
#define SEARCH_NOTEQUAL 3
6868
#define SEARCH_LESSEQUAL 4
6969

70-
static struct ipc_ids init_msg_ids;
71-
72-
#define msg_ids(ns) (*((ns)->ids[IPC_MSG_IDS]))
70+
#define msg_ids(ns) ((ns)->ids[IPC_MSG_IDS])
7371

7472
#define msg_unlock(msq) ipc_unlock(&(msq)->q_perm)
7573
#define msg_buildid(id, seq) ipc_buildid(id, seq)
@@ -80,30 +78,17 @@ static int newque(struct ipc_namespace *, struct ipc_params *);
8078
static int sysvipc_msg_proc_show(struct seq_file *s, void *it);
8179
#endif
8280

83-
static void __msg_init_ns(struct ipc_namespace *ns, struct ipc_ids *ids)
81+
void msg_init_ns(struct ipc_namespace *ns)
8482
{
85-
ns->ids[IPC_MSG_IDS] = ids;
8683
ns->msg_ctlmax = MSGMAX;
8784
ns->msg_ctlmnb = MSGMNB;
8885
ns->msg_ctlmni = MSGMNI;
8986
atomic_set(&ns->msg_bytes, 0);
9087
atomic_set(&ns->msg_hdrs, 0);
91-
ipc_init_ids(ids);
88+
ipc_init_ids(&ns->ids[IPC_MSG_IDS]);
9289
}
9390

9491
#ifdef CONFIG_IPC_NS
95-
int msg_init_ns(struct ipc_namespace *ns)
96-
{
97-
struct ipc_ids *ids;
98-
99-
ids = kmalloc(sizeof(struct ipc_ids), GFP_KERNEL);
100-
if (ids == NULL)
101-
return -ENOMEM;
102-
103-
__msg_init_ns(ns, ids);
104-
return 0;
105-
}
106-
10792
void msg_exit_ns(struct ipc_namespace *ns)
10893
{
10994
struct msg_queue *msq;
@@ -126,15 +111,12 @@ void msg_exit_ns(struct ipc_namespace *ns)
126111
}
127112

128113
up_write(&msg_ids(ns).rw_mutex);
129-
130-
kfree(ns->ids[IPC_MSG_IDS]);
131-
ns->ids[IPC_MSG_IDS] = NULL;
132114
}
133115
#endif
134116

135117
void __init msg_init(void)
136118
{
137-
__msg_init_ns(&init_ipc_ns, &init_msg_ids);
119+
msg_init_ns(&init_ipc_ns);
138120
ipc_init_proc_interface("sysvipc/msg",
139121
" key msqid perms cbytes qnum lspid lrpid uid gid cuid cgid stime rtime ctime\n",
140122
IPC_MSG_IDS, sysvipc_msg_proc_show);

‎ipc/namespace.c‎

Lines changed: 4 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -14,35 +14,18 @@
1414

1515
static struct ipc_namespace *clone_ipc_ns(struct ipc_namespace *old_ns)
1616
{
17-
int err;
1817
struct ipc_namespace *ns;
1918

20-
err = -ENOMEM;
2119
ns = kmalloc(sizeof(struct ipc_namespace), GFP_KERNEL);
2220
if (ns == NULL)
23-
goto err_mem;
21+
return ERR_PTR(-ENOMEM);
2422

25-
err = sem_init_ns(ns);
26-
if (err)
27-
goto err_sem;
28-
err = msg_init_ns(ns);
29-
if (err)
30-
goto err_msg;
31-
err = shm_init_ns(ns);
32-
if (err)
33-
goto err_shm;
23+
sem_init_ns(ns);
24+
msg_init_ns(ns);
25+
shm_init_ns(ns);
3426

3527
kref_init(&ns->kref);
3628
return ns;
37-
38-
err_shm:
39-
msg_exit_ns(ns);
40-
err_msg:
41-
sem_exit_ns(ns);
42-
err_sem:
43-
kfree(ns);
44-
err_mem:
45-
return ERR_PTR(err);
4629
}
4730

4831
struct ipc_namespace *copy_ipcs(unsigned long flags, struct ipc_namespace *ns)

‎ipc/sem.c‎

Lines changed: 4 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -87,14 +87,12 @@
8787
#include <asm/uaccess.h>
8888
#include "util.h"
8989

90-
#define sem_ids(ns) (*((ns)->ids[IPC_SEM_IDS]))
90+
#define sem_ids(ns) ((ns)->ids[IPC_SEM_IDS])
9191

9292
#define sem_unlock(sma) ipc_unlock(&(sma)->sem_perm)
9393
#define sem_checkid(sma, semid) ipc_checkid(&sma->sem_perm, semid)
9494
#define sem_buildid(id, seq) ipc_buildid(id, seq)
9595

96-
static struct ipc_ids init_sem_ids;
97-
9896
static int newary(struct ipc_namespace *, struct ipc_params *);
9997
static void freeary(struct ipc_namespace *, struct sem_array *);
10098
#ifdef CONFIG_PROC_FS
@@ -118,30 +116,17 @@ static int sysvipc_sem_proc_show(struct seq_file *s, void *it);
118116
#define sc_semopm sem_ctls[2]
119117
#define sc_semmni sem_ctls[3]
120118

121-
static void __sem_init_ns(struct ipc_namespace *ns, struct ipc_ids *ids)
119+
void sem_init_ns(struct ipc_namespace *ns)
122120
{
123-
ns->ids[IPC_SEM_IDS] = ids;
124121
ns->sc_semmsl = SEMMSL;
125122
ns->sc_semmns = SEMMNS;
126123
ns->sc_semopm = SEMOPM;
127124
ns->sc_semmni = SEMMNI;
128125
ns->used_sems = 0;
129-
ipc_init_ids(ids);
126+
ipc_init_ids(&ns->ids[IPC_SEM_IDS]);
130127
}
131128

132129
#ifdef CONFIG_IPC_NS
133-
int sem_init_ns(struct ipc_namespace *ns)
134-
{
135-
struct ipc_ids *ids;
136-
137-
ids = kmalloc(sizeof(struct ipc_ids), GFP_KERNEL);
138-
if (ids == NULL)
139-
return -ENOMEM;
140-
141-
__sem_init_ns(ns, ids);
142-
return 0;
143-
}
144-
145130
void sem_exit_ns(struct ipc_namespace *ns)
146131
{
147132
struct sem_array *sma;
@@ -163,15 +148,12 @@ void sem_exit_ns(struct ipc_namespace *ns)
163148
total++;
164149
}
165150
up_write(&sem_ids(ns).rw_mutex);
166-
167-
kfree(ns->ids[IPC_SEM_IDS]);
168-
ns->ids[IPC_SEM_IDS] = NULL;
169151
}
170152
#endif
171153

172154
void __init sem_init (void)
173155
{
174-
__sem_init_ns(&init_ipc_ns, &init_sem_ids);
156+
sem_init_ns(&init_ipc_ns);
175157
ipc_init_proc_interface("sysvipc/sem",
176158
" key semid perms nsems uid gid cuid cgid otime ctime\n",
177159
IPC_SEM_IDS, sysvipc_sem_proc_show);

‎ipc/shm.c‎

Lines changed: 4 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,7 @@ struct shm_file_data {
5656
static const struct file_operations shm_file_operations;
5757
static struct vm_operations_struct shm_vm_ops;
5858

59-
static struct ipc_ids init_shm_ids;
60-
61-
#define shm_ids(ns) (*((ns)->ids[IPC_SHM_IDS]))
59+
#define shm_ids(ns) ((ns)->ids[IPC_SHM_IDS])
6260

6361
#define shm_unlock(shp) \
6462
ipc_unlock(&(shp)->shm_perm)
@@ -72,14 +70,13 @@ static void shm_destroy (struct ipc_namespace *ns, struct shmid_kernel *shp);
7270
static int sysvipc_shm_proc_show(struct seq_file *s, void *it);
7371
#endif
7472

75-
static void __shm_init_ns(struct ipc_namespace *ns, struct ipc_ids *ids)
73+
void shm_init_ns(struct ipc_namespace *ns)
7674
{
77-
ns->ids[IPC_SHM_IDS] = ids;
7875
ns->shm_ctlmax = SHMMAX;
7976
ns->shm_ctlall = SHMALL;
8077
ns->shm_ctlmni = SHMMNI;
8178
ns->shm_tot = 0;
82-
ipc_init_ids(ids);
79+
ipc_init_ids(&ns->ids[IPC_SHM_IDS]);
8380
}
8481

8582
/*
@@ -98,18 +95,6 @@ static void do_shm_rmid(struct ipc_namespace *ns, struct shmid_kernel *shp)
9895
}
9996

10097
#ifdef CONFIG_IPC_NS
101-
int shm_init_ns(struct ipc_namespace *ns)
102-
{
103-
struct ipc_ids *ids;
104-
105-
ids = kmalloc(sizeof(struct ipc_ids), GFP_KERNEL);
106-
if (ids == NULL)
107-
return -ENOMEM;
108-
109-
__shm_init_ns(ns, ids);
110-
return 0;
111-
}
112-
11398
void shm_exit_ns(struct ipc_namespace *ns)
11499
{
115100
struct shmid_kernel *shp;
@@ -131,15 +116,12 @@ void shm_exit_ns(struct ipc_namespace *ns)
131116
total++;
132117
}
133118
up_write(&shm_ids(ns).rw_mutex);
134-
135-
kfree(ns->ids[IPC_SHM_IDS]);
136-
ns->ids[IPC_SHM_IDS] = NULL;
137119
}
138120
#endif
139121

140122
void __init shm_init (void)
141123
{
142-
__shm_init_ns(&init_ipc_ns, &init_shm_ids);
124+
shm_init_ns(&init_ipc_ns);
143125
ipc_init_proc_interface("sysvipc/shm",
144126
" key shmid perms size cpid lpid nattch uid gid cuid cgid atime dtime ctime\n",
145127
IPC_SHM_IDS, sysvipc_shm_proc_show);

‎ipc/util.c‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -833,7 +833,7 @@ static void *sysvipc_proc_next(struct seq_file *s, void *it, loff_t *pos)
833833
if (ipc && ipc != SEQ_START_TOKEN)
834834
ipc_unlock(ipc);
835835

836-
return sysvipc_find_ipc(iter->ns->ids[iface->ids], *pos, pos);
836+
return sysvipc_find_ipc(&iter->ns->ids[iface->ids], *pos, pos);
837837
}
838838

839839
/*
@@ -846,7 +846,7 @@ static void *sysvipc_proc_start(struct seq_file *s, loff_t *pos)
846846
struct ipc_proc_iface *iface = iter->iface;
847847
struct ipc_ids *ids;
848848

849-
ids = iter->ns->ids[iface->ids];
849+
ids = &iter->ns->ids[iface->ids];
850850

851851
/*
852852
* Take the lock - this will be released by the corresponding
@@ -877,7 +877,7 @@ static void sysvipc_proc_stop(struct seq_file *s, void *it)
877877
if (ipc && ipc != SEQ_START_TOKEN)
878878
ipc_unlock(ipc);
879879

880-
ids = iter->ns->ids[iface->ids];
880+
ids = &iter->ns->ids[iface->ids];
881881
/* Release the lock we took in start() */
882882
up_read(&ids->rw_mutex);
883883
}

‎ipc/util.h‎

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
#ifndef _IPC_UTIL_H
1111
#define _IPC_UTIL_H
1212

13-
#include <linux/idr.h>
1413
#include <linux/err.h>
1514

1615
#define USHRT_MAX 0xffff
@@ -22,22 +21,14 @@ void shm_init (void);
2221

2322
struct ipc_namespace;
2423

25-
int sem_init_ns(struct ipc_namespace *ns);
26-
int msg_init_ns(struct ipc_namespace *ns);
27-
int shm_init_ns(struct ipc_namespace *ns);
24+
void sem_init_ns(struct ipc_namespace *ns);
25+
void msg_init_ns(struct ipc_namespace *ns);
26+
void shm_init_ns(struct ipc_namespace *ns);
2827

2928
void sem_exit_ns(struct ipc_namespace *ns);
3029
void msg_exit_ns(struct ipc_namespace *ns);
3130
void shm_exit_ns(struct ipc_namespace *ns);
3231

33-
struct ipc_ids {
34-
int in_use;
35-
unsigned short seq;
36-
unsigned short seq_max;
37-
struct rw_semaphore rw_mutex;
38-
struct idr ipcs_idr;
39-
};
40-
4132
/*
4233
* Structure that holds the parameters needed by the ipc operations
4334
* (see after)
@@ -68,6 +59,7 @@ struct ipc_ops {
6859
};
6960

7061
struct seq_file;
62+
struct ipc_ids;
7163

7264
void ipc_init_ids(struct ipc_ids *);
7365
#ifdef CONFIG_PROC_FS

0 commit comments

Comments
 (0)