ipc: constify ipc_ops
PATCH 1/1 ipc: constify ipc_ops
There is no need to recreate the very same ipc_ops structure on every kernel entry for msgget/semget/shmget. Just declare it static and be done with it.
While at it, constify it as we don't modify the structure at runtime.
Found in the PaX patch, written by the PaX Team.
--- ipc/msg.c | 9 ++++----- ipc/sem.c | 10 +++++----- ipc/shm.c | 10 +++++----- ipc/util.c | 8 ++++---- ipc/util.h | 2 +- 5 files changed, 19 insertions(+), 20 deletions(-)
ipc/msg.c +4 -5
@@ -306,15 +306,14 @@ static inline int msg_security(struct kern_ipc_perm *ipcp, int msgflg) SYSCALL_DEFINE2(msgget, key_t, key, int, msgflg) {struct ipc_namespace *ns; -struct ipc_ops msg_ops; +static const struct ipc_ops msg_ops = { + .getnew = newque, + .associate = msg_security, + };struct ipc_params msg_params; ns = current->nsproxy->ipc_ns; - msg_ops.getnew = newque; - msg_ops.associate = msg_security; - msg_ops.more_checks =NULL ; - msg_params.key = key; msg_params.flg = msgflg;
ipc/sem.c +5 -5
@@ -564,7 +564,11 @@ static inline int sem_more_checks(struct kern_ipc_perm *ipcp, SYSCALL_DEFINE3(semget, key_t, key, int, nsems, int, semflg) {struct ipc_namespace *ns; -struct ipc_ops sem_ops; +static const struct ipc_ops sem_ops = { + .getnew = newary, + .associate = sem_security, + .more_checks = sem_more_checks, + };struct ipc_params sem_params; ns = current->nsproxy->ipc_ns;
@@ -572,10 +576,6 @@ SYSCALL_DEFINE3(semget, key_t, key, int, nsems, int, semflg)if (nsems <0 || nsems > ns->sc_semmsl)return -EINVAL ; - sem_ops.getnew = newary; - sem_ops.associate = sem_security; - sem_ops.more_checks = sem_more_checks; - sem_params.key = key; sem_params.flg = semflg; sem_params.u.nsems = nsems;
ipc/shm.c +5 -5
@@ -609,15 +609,15 @@ static inline int shm_more_checks(struct kern_ipc_perm *ipcp, SYSCALL_DEFINE3(shmget, key_t, key, size_t, size, int, shmflg) {struct ipc_namespace *ns; -struct ipc_ops shm_ops; +static const struct ipc_ops shm_ops = { + .getnew = newseg, + .associate = shm_security, + .more_checks = shm_more_checks, + };struct ipc_params shm_params; ns = current->nsproxy->ipc_ns; - shm_ops.getnew = newseg; - shm_ops.associate = shm_security; - shm_ops.more_checks = shm_more_checks; - shm_params.key = key; shm_params.flg = shmflg; shm_params.u.size = size;
ipc/util.c +4 -4
@@ -317,7 +317,7 @@ int ipc_addid(struct ipc_ids *ids, struct kern_ipc_perm *new, int size)* when the key is IPC_PRIVATE. */ static int ipcget_new (struct ipc_namespace *ns,struct ipc_ids *ids, -struct ipc_ops *ops,struct ipc_params *params) +const struct ipc_ops *ops,struct ipc_params *params) {int err;
@@ -344,7 +344,7 @@ static int ipcget_new(struct ipc_namespace *ns, struct ipc_ids *ids,*/ static int ipc_check_perms (struct ipc_namespace *ns,struct kern_ipc_perm *ipcp, -struct ipc_ops *ops, +const struct ipc_ops *ops,struct ipc_params *params) {int err;
@@ -375,7 +375,7 @@ static int ipc_check_perms(struct ipc_namespace *ns,* On success, the ipc id is returned. */ static int ipcget_public (struct ipc_namespace *ns,struct ipc_ids *ids, -struct ipc_ops *ops,struct ipc_params *params) +const struct ipc_ops *ops,struct ipc_params *params) {struct kern_ipc_perm *ipcp;int flg = params->flg;
@@ -678,7 +678,7 @@ out:* Common routine called by sys_msgget(), sys_semget() and sys_shmget(). */ int ipcget (struct ipc_namespace *ns,struct ipc_ids *ids, -struct ipc_ops *ops,struct ipc_params *params) +const struct ipc_ops *ops,struct ipc_params *params) {if (params->key ==IPC_PRIVATE )return ipcget_new (ns, ids, ops, params);
ipc/util.h +1 -1
@@ -201,7 +201,7 @@ static inline bool ipc_valid_object(struct kern_ipc_perm *perm)struct kern_ipc_perm *ipc_obtain_object_check (struct ipc_ids *ids,int id);int ipcget (struct ipc_namespace *ns,struct ipc_ids *ids, -struct ipc_ops *ops,struct ipc_params *params); +const struct ipc_ops *ops,struct ipc_params *params);void free_ipcs (struct ipc_namespace *ns,struct ipc_ids *ids,void (*free)(struct ipc_namespace *,struct kern_ipc_perm *));#endif
Signature · 1 line
1.7.10.4
Quoted 4 lines · Mathias Krause
There is no need to recreate the very same
ipc_opsstructure on every kernel entry for msgget/semget/shmget. Just declare it static and be done with it.
While at it, constify it as we don't modify the structure at runtime.
Seems reasonable.
Quoted 7 lines
Found in the PaX patch, written by the PaX Team.
Cc PaX Team <[email protected]>Cc Nadia Derbey <[email protected]>Cc Andrew Morton <[email protected]>Signed-off-by Mathias Krause <[email protected]>
In the future, for ipc changes, please CC me and Manfred as well.
One comment below, otherwise:
Quoted 13 lines
--- ipc/msg.c | 9 ++++----- ipc/sem.c | 10 +++++----- ipc/shm.c | 10 +++++----- ipc/util.c | 8 ++++---- ipc/util.h | 2 +- 5 files changed, 19 insertions(+), 20 deletions(-)ipc/msg.c +0
-0
ipc/msg.c static inline int msg_security(struct kern_ipc_perm *ipcp, int msgflg) · line 311
Quoted 7 lines
ipc/msg.c +3
-1@@ -306,15 +306,14 @@ static inline int msg_security(struct kern_ipc_perm *ipcp, int msgflg) SYSCALL_DEFINE2(msgget, key_t, key, int, msgflg) {struct ipc_namespace *ns; -struct ipc_ops msg_ops; +static const struct ipc_ops msg_ops = { + .getnew = newque, + .associate = msg_security,
For completeness, please add .more_checks = NULL as well.
Quoted 118 lines
ipc/msg.c +1
-4+ };struct ipc_params msg_params; ns = current->nsproxy->ipc_ns; - msg_ops.getnew = newque; - msg_ops.associate = msg_security; - msg_ops.more_checks =NULL ; - msg_params.key = key; msg_params.flg = msgflg;ipc/sem.c +5
-5@@ -564,7 +564,11 @@ static inline int sem_more_checks(struct kern_ipc_perm *ipcp, SYSCALL_DEFINE3(semget, key_t, key, int, nsems, int, semflg) {struct ipc_namespace *ns; -struct ipc_ops sem_ops; +static const struct ipc_ops sem_ops = { + .getnew = newary, + .associate = sem_security, + .more_checks = sem_more_checks, + };struct ipc_params sem_params; ns = current->nsproxy->ipc_ns;@@ -572,10 +576,6 @@ SYSCALL_DEFINE3(semget, key_t, key, int, nsems, int, semflg)if (nsems <0 || nsems > ns->sc_semmsl)return -EINVAL ; - sem_ops.getnew = newary; - sem_ops.associate = sem_security; - sem_ops.more_checks = sem_more_checks; - sem_params.key = key; sem_params.flg = semflg; sem_params.u.nsems = nsems;ipc/shm.c +5
-5@@ -609,15 +609,15 @@ static inline int shm_more_checks(struct kern_ipc_perm *ipcp, SYSCALL_DEFINE3(shmget, key_t, key, size_t, size, int, shmflg) {struct ipc_namespace *ns; -struct ipc_ops shm_ops; +static const struct ipc_ops shm_ops = { + .getnew = newseg, + .associate = shm_security, + .more_checks = shm_more_checks, + };struct ipc_params shm_params; ns = current->nsproxy->ipc_ns; - shm_ops.getnew = newseg; - shm_ops.associate = shm_security; - shm_ops.more_checks = shm_more_checks; - shm_params.key = key; shm_params.flg = shmflg; shm_params.u.size = size;ipc/util.c +4
-4@@ -317,7 +317,7 @@ int ipc_addid(struct ipc_ids *ids, struct kern_ipc_perm *new, int size)* when the key is IPC_PRIVATE. */ static int ipcget_new (struct ipc_namespace *ns,struct ipc_ids *ids, -struct ipc_ops *ops,struct ipc_params *params) +const struct ipc_ops *ops,struct ipc_params *params) {int err;@@ -344,7 +344,7 @@ static int ipcget_new(struct ipc_namespace *ns, struct ipc_ids *ids,*/ static int ipc_check_perms (struct ipc_namespace *ns,struct kern_ipc_perm *ipcp, -struct ipc_ops *ops, +const struct ipc_ops *ops,struct ipc_params *params) {int err;@@ -375,7 +375,7 @@ static int ipc_check_perms(struct ipc_namespace *ns,* On success, the ipc id is returned. */ static int ipcget_public (struct ipc_namespace *ns,struct ipc_ids *ids, -struct ipc_ops *ops,struct ipc_params *params) +const struct ipc_ops *ops,struct ipc_params *params) {struct kern_ipc_perm *ipcp;int flg = params->flg;@@ -678,7 +678,7 @@ out:* Common routine called by sys_msgget(), sys_semget() and sys_shmget(). */ int ipcget (struct ipc_namespace *ns,struct ipc_ids *ids, -struct ipc_ops *ops,struct ipc_params *params) +const struct ipc_ops *ops,struct ipc_params *params) {if (params->key ==IPC_PRIVATE )return ipcget_new (ns, ids, ops, params);ipc/util.h +1
-1@@ -201,7 +201,7 @@ static inline bool ipc_valid_object(struct kern_ipc_perm *perm)struct kern_ipc_perm *ipc_obtain_object_check (struct ipc_ids *ids,int id);int ipcget (struct ipc_namespace *ns,struct ipc_ids *ids, -struct ipc_ops *ops,struct ipc_params *params); +const struct ipc_ops *ops,struct ipc_params *params);void free_ipcs (struct ipc_namespace *ns,struct ipc_ids *ids,void (*free)(struct ipc_namespace *,struct kern_ipc_perm *));#endif
Quoted 9 lines · Davidlohr Bueso
Quoted 4 lines · Mathias Krause
There is no need to recreate the very same
ipc_opsstructure on every kernel entry for msgget/semget/shmget. Just declare it static and be done with it.
While at it, constify it as we don't modify the structure at runtime.Seems reasonable.
Quoted 1 line
Quoted 8 lines
Quoted 6 lines
Found in the PaX patch, written by the PaX Team.
Cc PaX Team <[email protected]>Cc Nadia Derbey <[email protected]>Cc Andrew Morton <[email protected]>Signed-off-by Mathias Krause <[email protected]>In the future, for ipc changes, please CC me and Manfred as well.
As there is no maintainer entry for ipc/ in the MAINTAINERS file I looked at the recent commit history of ipc/ and found that most of the patches went in via Andrew and then Linus. But it's true, you and Manfred have quite some patches in the recent past. Though, having an official maintaner for ipc/ would accelerate the gyrating phase of whom to send the patches to in the future. Are you volunteering? :)
Quoted 12 lines
One comment below, otherwise:
Acked-by Davidlohr Bueso <[email protected]>Quoted 8 lines
--- ipc/msg.c | 9 ++++----- ipc/sem.c | 10 +++++----- ipc/shm.c | 10 +++++----- ipc/util.c | 8 ++++---- ipc/util.h | 2 +- 5 files changed, 19 insertions(+), 20 deletions(-)
Quoted 14 lines
ipc/msg.c static inline int msg_security(struct kern_ipc_perm *ipcp, int msgflg) · line 311
Quoted 12 lines
ipc/msg.c +3
-1@@ -306,15 +306,14 @@ static inline int msg_security(struct kern_ipc_perm *ipcp, int msgflg) SYSCALL_DEFINE2(msgget, key_t, key, int, msgflg) {struct ipc_namespace *ns; -struct ipc_ops msg_ops; +static const struct ipc_ops msg_ops = { + .getnew = newque, + .associate = msg_security,For completeness, please add .more_checks = NULL as well.
The C standard already ensures that .more_checks is initialized with NULL. So I don't see any benefit from doing it explicitly. We're not initializing global/static variables holding NULL pointers in the majority of the code base either, so this is just "best practice".
Also, in case somebody adds another member to struct ipc_ops, he also would have to add a '.new_member = NULL' to all places using ipc_ops but not needing the new member? Makes no sense to me. It would just add noise to that patch.
Quoted 112 lines
Quoted 112 lines
ipc/msg.c +1
-4+ };struct ipc_params msg_params; ns = current->nsproxy->ipc_ns; - msg_ops.getnew = newque; - msg_ops.associate = msg_security; - msg_ops.more_checks =NULL ; - msg_params.key = key; msg_params.flg = msgflg;ipc/sem.c +5
-5@@ -564,7 +564,11 @@ static inline int sem_more_checks(struct kern_ipc_perm *ipcp, SYSCALL_DEFINE3(semget, key_t, key, int, nsems, int, semflg) {struct ipc_namespace *ns; -struct ipc_ops sem_ops; +static const struct ipc_ops sem_ops = { + .getnew = newary, + .associate = sem_security, + .more_checks = sem_more_checks, + };struct ipc_params sem_params; ns = current->nsproxy->ipc_ns;@@ -572,10 +576,6 @@ SYSCALL_DEFINE3(semget, key_t, key, int, nsems, int, semflg)if (nsems <0 || nsems > ns->sc_semmsl)return -EINVAL ; - sem_ops.getnew = newary; - sem_ops.associate = sem_security; - sem_ops.more_checks = sem_more_checks; - sem_params.key = key; sem_params.flg = semflg; sem_params.u.nsems = nsems;ipc/shm.c +5
-5@@ -609,15 +609,15 @@ static inline int shm_more_checks(struct kern_ipc_perm *ipcp, SYSCALL_DEFINE3(shmget, key_t, key, size_t, size, int, shmflg) {struct ipc_namespace *ns; -struct ipc_ops shm_ops; +static const struct ipc_ops shm_ops = { + .getnew = newseg, + .associate = shm_security, + .more_checks = shm_more_checks, + };struct ipc_params shm_params; ns = current->nsproxy->ipc_ns; - shm_ops.getnew = newseg; - shm_ops.associate = shm_security; - shm_ops.more_checks = shm_more_checks; - shm_params.key = key; shm_params.flg = shmflg; shm_params.u.size = size;ipc/util.c +4
-4@@ -317,7 +317,7 @@ int ipc_addid(struct ipc_ids *ids, struct kern_ipc_perm *new, int size)* when the key is IPC_PRIVATE. */ static int ipcget_new (struct ipc_namespace *ns,struct ipc_ids *ids, -struct ipc_ops *ops,struct ipc_params *params) +const struct ipc_ops *ops,struct ipc_params *params) {int err;@@ -344,7 +344,7 @@ static int ipcget_new(struct ipc_namespace *ns, struct ipc_ids *ids,*/ static int ipc_check_perms (struct ipc_namespace *ns,struct kern_ipc_perm *ipcp, -struct ipc_ops *ops, +const struct ipc_ops *ops,struct ipc_params *params) {int err;@@ -375,7 +375,7 @@ static int ipc_check_perms(struct ipc_namespace *ns,* On success, the ipc id is returned. */ static int ipcget_public (struct ipc_namespace *ns,struct ipc_ids *ids, -struct ipc_ops *ops,struct ipc_params *params) +const struct ipc_ops *ops,struct ipc_params *params) {struct kern_ipc_perm *ipcp;int flg = params->flg;@@ -678,7 +678,7 @@ out:* Common routine called by sys_msgget(), sys_semget() and sys_shmget(). */ int ipcget (struct ipc_namespace *ns,struct ipc_ids *ids, -struct ipc_ops *ops,struct ipc_params *params) +const struct ipc_ops *ops,struct ipc_params *params) {if (params->key ==IPC_PRIVATE )return ipcget_new (ns, ids, ops, params);ipc/util.h +0
-0@@ -201,7 +201,7 @@ static inline bool ipc_valid_object(struct kern_ipc_perm *perm)
Quoted 9 lines
ipc/util.h static inline bool ipc_valid_object(struct kern_ipc_perm *perm) · line 207
Quoted 7 lines
ipc/util.h +1
-1struct kern_ipc_perm *ipc_obtain_object_check (struct ipc_ids *ids,int id);int ipcget (struct ipc_namespace *ns,struct ipc_ids *ids, -struct ipc_ops *ops,struct ipc_params *params); +const struct ipc_ops *ops,struct ipc_params *params);void free_ipcs (struct ipc_namespace *ns,struct ipc_ids *ids,void (*free)(struct ipc_namespace *,struct kern_ipc_perm *));#endif
Thanks,
Mathias
Quoted 53 lines · Mathias Krause
Quoted 9 lines · Davidlohr Bueso
Quoted 4 lines · Mathias Krause
There is no need to recreate the very same
ipc_opsstructure on every kernel entry for msgget/semget/shmget. Just declare it static and be done with it.
While at it, constify it as we don't modify the structure at runtime.Seems reasonable.
Quoted 1 line
Quoted 8 lines
Quoted 6 lines
Found in the PaX patch, written by the PaX Team.
Cc PaX Team <[email protected]>Cc Nadia Derbey <[email protected]>Cc Andrew Morton <[email protected]>Signed-off-by Mathias Krause <[email protected]>In the future, for ipc changes, please CC me and Manfred as well.
As there is no maintainer entry for
ipc/in theMAINTAINERSfile I looked at the recent commit history ofipc/and found that most of the patches went in via Andrew and then Linus. But it's true, you and Manfred have quite some patches in the recent past. Though, having an official maintaner foripc/would accelerate the gyrating phase of whom to send the patches to in the future. Are you volunteering? :)Quoted 12 lines
One comment below, otherwise:
Acked-by Davidlohr Bueso <[email protected]>Quoted 8 lines
--- ipc/msg.c | 9 ++++----- ipc/sem.c | 10 +++++----- ipc/shm.c | 10 +++++----- ipc/util.c | 8 ++++---- ipc/util.h | 2 +- 5 files changed, 19 insertions(+), 20 deletions(-)Quoted 14 lines
ipc/msg.c static inline int msg_security(struct kern_ipc_perm *ipcp, int msgflg) · line 311
Quoted 12 lines
ipc/msg.c +3
-1@@ -306,15 +306,14 @@ static inline int msg_security(struct kern_ipc_perm *ipcp, int msgflg) SYSCALL_DEFINE2(msgget, key_t, key, int, msgflg) {struct ipc_namespace *ns; -struct ipc_ops msg_ops; +static const struct ipc_ops msg_ops = { + .getnew = newque, + .associate = msg_security,For completeness, please add .more_checks = NULL as well.
Quoted 5 lines
The C standard already ensures that .more_checks is initialized with NULL. So I don't see any benefit from doing it explicitly. We're not initializing global/static variables holding NULL pointers in the majority of the code base either, so this is just "best practice".
Hence the word _completeness_.
Quoted 13 lines · On Mon, 31 Mar 2014 07:12:36 -0700 Davidlohr Bueso <[email protected]> wrote:
Quoted 13 lines
Quoted 12 lines
ipc/msg.c static inline int msg_security(struct kern_ipc_perm *ipcp, int msgflg) · line 311
Quoted 10 lines
ipc/msg.c +3
-1@@ -306,15 +306,14 @@ static inline int msg_security(struct kern_ipc_perm *ipcp, int msgflg) SYSCALL_DEFINE2(msgget, key_t, key, int, msgflg) {struct ipc_namespace *ns; -struct ipc_ops msg_ops; +static const struct ipc_ops msg_ops = { + .getnew = newque, + .associate = msg_security,For completeness, please add .more_checks = NULL as well.
Quoted 7 lines
Quoted 5 lines
The C standard already ensures that .more_checks is initialized with NULL. So I don't see any benefit from doing it explicitly. We're not initializing global/static variables holding NULL pointers in the majority of the code base either, so this is just "best practice".
Hence the word _completeness_.
It's unusual to fill in the zeroed fields. We could put it in explicitly as commentary to tell the reader "this exists - we thought about it and decided to leave it zero". Which presumably is along the lines of what you're thinking. Or not ;)
I'm easy either way. I'll shove the patch into my nice pile of things-to-look-at-after-rc1.