(resend) IPC: code rewrite
cover (resend) IPC: code rewrite
Hi Andrew,
This is a resend of the first part of the patchset sent 2 weeks
ago. This is the part about the IPC which (again) proposes to consolidate some part of the existing code.
It does not change the behavior of the existing code, but
improves it in term of readability and maintainability as it consolidates it a little. As there was no objection, I think you can include them in your -mm tree.
The patchset applies on top of "2.6.24-mm1 + previous patches about
IPC" sent the last days (ie Nadia's patches + mine).
For information, here is the global diffstat:
ipc/msg.c | 184 +++++++++++++++++++++++-------------------------------------- ipc/sem.c | 156 ++++++++++++++++++--------------------------------- ipc/shm.c | 176 ++++++++++++++++++---------------------------------------- ipc/util.c | 64 +++++++++++++++++++++ ipc/util.h | 3 5 files changed, 249 insertions(+), 334 deletions(-)
and the size of the resulting kernel:
- without the patchset:
$ size obj/vmlinux.ori text data bss dec hex filename 1903257 175820 122880 2201957 219965 obj/vmlinux.ori
- with the patchset:
$ size obj/vmlinux text data bss dec hex filename 1902917 175820 122880 2201617 219811 obj/vmlinux
Signature · 1 line
Pierre Peiffer
PATCH 1/8 (resend) IPC/semaphores: code factorisation
From: Pierre Peiffer <[email protected]>
Trivial patch which adds some small locking functions and makes use of them to factorize some part of the code and to make it cleaner.
---
ipc/sem.c | 61 +++++++++++++++++++++++++++++++------------------------------ 1 file changed, 31 insertions(+), 30 deletions(-)
ipc/sem.c +31 -30
@@ -180,6 +180,25 @@ static inline struct sem_array *sem_lock return container_of(ipcp, struct sem_array, sem_perm); } +static inline void sem_lock_and_putref (struct sem_array *sma) +{ +ipc_lock_by_ptr (&sma->sem_perm); +ipc_rcu_putref (sma); +} + +static inline void sem_getref_and_unlock (struct sem_array *sma) +{ +ipc_rcu_getref (sma); +ipc_unlock (&(sma)->sem_perm); +} + +static inline void sem_putref (struct sem_array *sma) +{ +ipc_lock_by_ptr (&sma->sem_perm); +ipc_rcu_putref (sma); +ipc_unlock (&(sma)->sem_perm); +} +static inline void sem_rmid (struct ipc_namespace *ns,struct sem_array *s) {ipc_rmid (&sem_ids (ns), &s->sem_perm);
@@ -698,19 +717,15 @@ static int semctl_main(struct ipc_namespint i;if (nsems >SEMMSL_FAST ) { -ipc_rcu_getref (sma); -sem_unlock (sma); +sem_getref_and_unlock (sma); sem_io =ipc_alloc (sizeof (ushort)*nsems);if (sem_io ==NULL ) { -ipc_lock_by_ptr (&sma->sem_perm); -ipc_rcu_putref (sma); -sem_unlock (sma); +sem_putref (sma);return -ENOMEM ; } -ipc_lock_by_ptr (&sma->sem_perm); -ipc_rcu_putref (sma); +sem_lock_and_putref (sma);if (sma->sem_perm.deleted) {sem_unlock (sma); err = -EIDRM ;
@@ -731,38 +746,30 @@ static int semctl_main(struct ipc_namespint i;struct sem_undo *un; -ipc_rcu_getref (sma); -sem_unlock (sma); +sem_getref_and_unlock (sma);if (nsems >SEMMSL_FAST ) { sem_io =ipc_alloc (sizeof (ushort)*nsems);if (sem_io ==NULL ) { -ipc_lock_by_ptr (&sma->sem_perm); -ipc_rcu_putref (sma); -sem_unlock (sma); +sem_putref (sma);return -ENOMEM ; } }if (copy_from_user (sem_io, arg.array, nsems*sizeof (ushort))) { -ipc_lock_by_ptr (&sma->sem_perm); -ipc_rcu_putref (sma); -sem_unlock (sma); +sem_putref (sma); err = -EFAULT ; goto out_free; }for (i =0 ; i < nsems; i++) {if (sem_io[i] >SEMVMX ) { -ipc_lock_by_ptr (&sma->sem_perm); -ipc_rcu_putref (sma); -sem_unlock (sma); +sem_putref (sma); err = -ERANGE ; goto out_free; } } -ipc_lock_by_ptr (&sma->sem_perm); -ipc_rcu_putref (sma); +sem_lock_and_putref (sma);if (sma->sem_perm.deleted) {sem_unlock (sma); err = -EIDRM ;
@@ -1042,14 +1049,11 @@ static struct sem_undo *find_undo(structreturn ERR_PTR (PTR_ERR (sma)); nsems = sma->sem_nsems; -ipc_rcu_getref (sma); -sem_unlock (sma); +sem_getref_and_unlock (sma); new =kzalloc (sizeof (struct sem_undo ) +sizeof (short )*nsems,GFP_KERNEL );if (!new) { -ipc_lock_by_ptr (&sma->sem_perm); -ipc_rcu_putref (sma); -sem_unlock (sma); +sem_putref (sma);return ERR_PTR (-ENOMEM ); } new->semadj = (short *) &new[1 ];
@@ -1060,13 +1064,10 @@ static struct sem_undo *find_undo(structif (un) {spin_unlock (&ulp->lock);kfree (new); -ipc_lock_by_ptr (&sma->sem_perm); -ipc_rcu_putref (sma); -sem_unlock (sma); +sem_putref (sma); goto out; } -ipc_lock_by_ptr (&sma->sem_perm); -ipc_rcu_putref (sma); +sem_lock_and_putref (sma);if (sma->sem_perm.deleted) {sem_unlock (sma);spin_unlock (&ulp->lock);
Signature · 1 line
Pierre Peiffer
Re: [PATCH 2.6.24-mm1 1/8] (resend) IPC/semaphores: code factorisation
Quoted 2 lines · On Tue, Feb 12, 2008 at 05:13:41PM +0100, [email protected] wrote:
Trivial patch which adds some small locking functions and makes use of them to factorize some part of the code and to make it cleaner.
What's wrong with consolidation activity in general is that one need to follow tags many times to realise what on earth function really does.
Quoted 10 lines
ipc/sem.c +4
-0@@ -180,6 +180,25 @@ static inline struct sem_array *sem_lock return container_of(ipcp, struct sem_array, sem_perm); } +static inline void sem_lock_and_putref (struct sem_array *sma) +{ +ipc_lock_by_ptr (&sma->sem_perm); +ipc_rcu_putref (sma);
ipc/sem.c static inline struct sem_array *sem_lock · line 196
Quoted 10 lines
ipc/sem.c +10
-0+} + +static inline void sem_getref_and_unlock (struct sem_array *sma) +{ +ipc_rcu_getref (sma); +ipc_unlock (&(sma)->sem_perm); +} + +static inline void sem_putref (struct sem_array *sma) +{
It should be called sem_lock_putref_and_unlock() at which point it becomes obvious that this patch is from useless-wrappers land.
ipc/sem.c static inline struct sem_array *sem_lock · line 192
Quoted 3 lines
ipc/sem.c +3
-0+ipc_lock_by_ptr (&sma->sem_perm); +ipc_rcu_putref (sma); +ipc_unlock (&(sma)->sem_perm);
On the other hand PUT is associated with atomic_dec_and_test() which does no locking itself.
Re: [PATCH 2.6.24-mm1 1/8] (resend) IPC/semaphores: code factorisation
Quoted 6 lines · Alexey Dobriyan
Quoted 2 lines · On Tue, Feb 12, 2008 at 05:13:41PM +0100, [email protected] wrote:
Trivial patch which adds some small locking functions and makes use of them to factorize some part of the code and to make it cleaner.
What's wrong with consolidation activity in general is that one need to follow tags many times to realise what on earth function really does.
Funny...
What's right with consolidation in general is that it avoids the readers to read again and again the same piece of code and helps them to focus on what the code really does.
Signature · 1 line
Pierre
PATCH 2/8 (resend) IPC/shared memory: introduce shmctl_down
In reply to [email protected]From: Pierre Peiffer <[email protected]>
Currently, the way the different commands are handled in sys_shmctl introduces some duplicated code.
This patch introduces the shmctl_down function to handle all the commands requiring the rwmutex to be taken in write mode (ie IPC_SET and IPC_RMID for now). It is the equivalent function of semctl_down for shared memory.
This removes some duplicated code for handling these both commands and harmonizes the way they are handled among all IPCs.
---
ipc/shm.c | 160 +++++++++++++++++++++++++++----------------------------------- 1 file changed, 72 insertions(+), 88 deletions(-)
ipc/shm.c +72 -88
@@ -617,10 +617,78 @@ static void shm_get_stat(struct ipc_name } } -asmlinkage long sys_shmctl (int shmid,int cmd,struct shmid_ds __user *buf) +/* +* This function handles some shmctl commands which require the rw_mutex +* to be held in write mode. +* NOTE: no locks must be held, the rw_mutex is taken inside this function. +*/ +static int shmctl_down (struct ipc_namespace *ns,int shmid,int cmd, +struct shmid_ds __user *buf,int version) { +struct kern_ipc_perm *ipcp;struct shm_setbuf setbuf;struct shmid_kernel *shp; +int err; + +if (cmd ==IPC_SET ) { +if (copy_shmid_from_user (&setbuf, buf, version)) +return -EFAULT ; + } + +down_write (&shm_ids (ns).rw_mutex); + shp =shm_lock_check_down (ns, shmid); +if (IS_ERR (shp)) { + err =PTR_ERR (shp); + goto out_up; + } + + ipcp = &shp->shm_perm; + + err =audit_ipc_obj (ipcp); +if (err) + goto out_unlock; + +if (cmd ==IPC_SET ) { + err =audit_ipc_set_perm (0 , setbuf.uid, + setbuf.gid, setbuf.mode); +if (err) + goto out_unlock; + } + +if (current->euid != ipcp->uid && + current->euid != ipcp->cuid && + !capable (CAP_SYS_ADMIN )) { + err = -EPERM ; + goto out_unlock; + } + + err =security_shm_shmctl (shp, cmd); +if (err) + goto out_unlock; +switch (cmd) { +case IPC_RMID : +do_shm_rmid (ns, ipcp); + goto out_up; +case IPC_SET : + ipcp->uid = setbuf.uid; + ipcp->gid = setbuf.gid; + ipcp->mode = (ipcp->mode & ~S_IRWXUGO ) + | (setbuf.mode &S_IRWXUGO ); + shp->shm_ctim =get_seconds (); +break ; +default : + err = -EINVAL ; + } +out_unlock: +shm_unlock (shp); +out_up: +up_write (&shm_ids (ns).rw_mutex); +return err; +} + +asmlinkage long sys_shmctl (int shmid,int cmd,struct shmid_ds __user *buf) +{ +struct shmid_kernel *shp;int err, version;struct ipc_namespace *ns;
@@ -776,97 +844,13 @@ asmlinkage long sys_shmctl (int shmid, i goto out; }case IPC_RMID : - { -/* -* We cannot simply remove the file. The SVID states -* that the block remains until the last person -* detaches from it, then is deleted. A shmat() on -* an RMID segment is legal in older Linux and if -* we change it apps break... -* -* Instead we set a destroyed flag, and then blow -* the name away when the usage hits zero. -*/ -down_write (&shm_ids (ns).rw_mutex); - shp =shm_lock_check_down (ns, shmid); -if (IS_ERR (shp)) { - err =PTR_ERR (shp); - goto out_up; - } - - err =audit_ipc_obj (&(shp->shm_perm)); -if (err) - goto out_unlock_up; - -if (current->euid != shp->shm_perm.uid && - current->euid != shp->shm_perm.cuid && - !capable (CAP_SYS_ADMIN )) { - err=-EPERM ; - goto out_unlock_up; - } - - err =security_shm_shmctl (shp, cmd); -if (err) - goto out_unlock_up; - -do_shm_rmid (ns, &shp->shm_perm); -up_write (&shm_ids (ns).rw_mutex); - goto out; - } -case IPC_SET : - { -if (!buf) { - err = -EFAULT ; - goto out; - } - -if (copy_shmid_from_user (&setbuf, buf, version)) { - err = -EFAULT ; - goto out; - } -down_write (&shm_ids (ns).rw_mutex); - shp =shm_lock_check_down (ns, shmid); -if (IS_ERR (shp)) { - err =PTR_ERR (shp); - goto out_up; - } - err =audit_ipc_obj (&(shp->shm_perm)); -if (err) - goto out_unlock_up; - err =audit_ipc_set_perm (0 , setbuf.uid, setbuf.gid, setbuf.mode); -if (err) - goto out_unlock_up; - err=-EPERM ; -if (current->euid != shp->shm_perm.uid && - current->euid != shp->shm_perm.cuid && - !capable (CAP_SYS_ADMIN )) { - goto out_unlock_up; - } - - err =security_shm_shmctl (shp, cmd); -if (err) - goto out_unlock_up; - - shp->shm_perm.uid = setbuf.uid; - shp->shm_perm.gid = setbuf.gid; - shp->shm_perm.mode = (shp->shm_perm.mode & ~S_IRWXUGO ) - | (setbuf.mode &S_IRWXUGO ); - shp->shm_ctim =get_seconds (); -break ; - } - + err =shmctl_down (ns, shmid, cmd, buf, version); +return err;default : - err = -EINVAL ; - goto out; +return -EINVAL ; } - err =0 ; -out_unlock_up: -shm_unlock (shp); -out_up: -up_write (&shm_ids (ns).rw_mutex); - goto out; out_unlock:shm_unlock (shp); out:
Signature · 1 line
Pierre Peiffer
PATCH 3/8 (resend) IPC/message queues: introduce msgctl_down
In reply to [email protected]From: Pierre Peiffer <[email protected]>
Currently, sys_msgctl is not easy to read.
This patch tries to improve that by introducing the msgctl_down function to handle all commands requiring the rwmutex to be taken in write mode (ie IPC_SET and IPC_RMID for now). It is the equivalent function of semctl_down for message queues.
This greatly changes the readability of sys_msgctl and also harmonizes the way these commands are handled among all IPCs.
---
ipc/msg.c | 162 ++++++++++++++++++++++++++++++++++---------------------------- 1 file changed, 89 insertions(+), 73 deletions(-)
ipc/msg.c +89 -73
@@ -436,10 +436,95 @@ copy_msqid_from_user(struct msq_setbuf * } } -asmlinkage long sys_msgctl (int msqid,int cmd,struct msqid_ds __user *buf) +/* +* This function handles some msgctl commands which require the rw_mutex +* to be held in write mode. +* NOTE: no locks must be held, the rw_mutex is taken inside this function. +*/ +static int msgctl_down (struct ipc_namespace *ns,int msqid,int cmd, +struct msqid_ds __user *buf,int version) {struct kern_ipc_perm *ipcp; -struct msq_setbuf uninitialized_var (setbuf ); +struct msq_setbuf setbuf; +struct msg_queue *msq; +int err; + +if (cmd ==IPC_SET ) { +if (copy_msqid_from_user (&setbuf, buf, version)) +return -EFAULT ; + } + +down_write (&msg_ids (ns).rw_mutex); + msq =msg_lock_check_down (ns, msqid); +if (IS_ERR (msq)) { + err =PTR_ERR (msq); + goto out_up; + } + + ipcp = &msq->q_perm; + + err =audit_ipc_obj (ipcp); +if (err) + goto out_unlock; + +if (cmd ==IPC_SET ) { + err =audit_ipc_set_perm (setbuf.qbytes, setbuf.uid, setbuf.gid, + setbuf.mode); +if (err) + goto out_unlock; + } + +if (current->euid != ipcp->cuid && + current->euid != ipcp->uid && + !capable (CAP_SYS_ADMIN )) { +/* We _could_ check for CAP_CHOWN above, but we don't */ + err = -EPERM ; + goto out_unlock; + } + + err =security_msg_queue_msgctl (msq, cmd); +if (err) + goto out_unlock; + +switch (cmd) { +case IPC_RMID : +freeque (ns, ipcp); + goto out_up; +case IPC_SET : +if (setbuf.qbytes > ns->msg_ctlmnb && + !capable (CAP_SYS_RESOURCE )) { + err = -EPERM ; + goto out_unlock; + } + + msq->q_qbytes = setbuf.qbytes; + + ipcp->uid = setbuf.uid; + ipcp->gid = setbuf.gid; + ipcp->mode = (ipcp->mode & ~S_IRWXUGO ) | + (S_IRWXUGO & setbuf.mode); + msq->q_ctime =get_seconds (); +/* sleeping receivers might be excluded by +* stricter permissions. +*/ +expunge_all (msq, -EAGAIN ); +/* sleeping senders might be able to send +* due to a larger queue size. +*/ +ss_wakeup (&msq->q_senders,0 ); +break ; +default : + err = -EINVAL ; + } +out_unlock: +msg_unlock (msq); +out_up: +up_write (&msg_ids (ns).rw_mutex); +return err; +} + +asmlinkage long sys_msgctl (int msqid,int cmd,struct msqid_ds __user *buf) +{struct msg_queue *msq;int err, version;struct ipc_namespace *ns;
@@ -535,82 +620,13 @@ asmlinkage long sys_msgctl(int msqid, inreturn success_return; }case IPC_SET : -if (!buf) -return -EFAULT ; -if (copy_msqid_from_user (&setbuf, buf, version)) -return -EFAULT ; -break ;case IPC_RMID : -break ; + err =msgctl_down (ns, msqid, cmd, buf, version); +return err;default :return -EINVAL ; } -down_write (&msg_ids (ns).rw_mutex); - msq =msg_lock_check_down (ns, msqid); -if (IS_ERR (msq)) { - err =PTR_ERR (msq); - goto out_up; - } - - ipcp = &msq->q_perm; - - err =audit_ipc_obj (ipcp); -if (err) - goto out_unlock_up; -if (cmd ==IPC_SET ) { - err =audit_ipc_set_perm (setbuf.qbytes, setbuf.uid, setbuf.gid, - setbuf.mode); -if (err) - goto out_unlock_up; - } - - err = -EPERM ; -if (current->euid != ipcp->cuid && - current->euid != ipcp->uid && !capable (CAP_SYS_ADMIN )) -/* We _could_ check for CAP_CHOWN above, but we don't */ - goto out_unlock_up; - - err =security_msg_queue_msgctl (msq, cmd); -if (err) - goto out_unlock_up; - -switch (cmd) { -case IPC_SET : - { - err = -EPERM ; -if (setbuf.qbytes > ns->msg_ctlmnb && !capable (CAP_SYS_RESOURCE )) - goto out_unlock_up; - - msq->q_qbytes = setbuf.qbytes; - - ipcp->uid = setbuf.uid; - ipcp->gid = setbuf.gid; - ipcp->mode = (ipcp->mode & ~S_IRWXUGO ) | - (S_IRWXUGO & setbuf.mode); - msq->q_ctime =get_seconds (); -/* sleeping receivers might be excluded by -* stricter permissions. -*/ -expunge_all (msq, -EAGAIN ); -/* sleeping senders might be able to send -* due to a larger queue size. -*/ -ss_wakeup (&msq->q_senders,0 ); -msg_unlock (msq); -break ; - } -case IPC_RMID : -freeque (ns, &msq->q_perm); -break ; - } - err =0 ; -out_up: -up_write (&msg_ids (ns).rw_mutex); -return err; -out_unlock_up: -msg_unlock (msq); - goto out_up; out_unlock:msg_unlock (msq);return err;
Signature · 1 line
Pierre Peiffer
PATCH 4/8 (resend) IPC/semaphores: move the rwmutex handling inside semctl_down
In reply to [email protected]From: Pierre Peiffer <[email protected]>
semctl_down is called with the rwmutex (the one which protects the list of ipcs) taken in write mode.
This patch moves this rwmutex taken in write-mode inside semctl_down. This has the advantages of reducing a little bit the window during which this rwmutex is taken, clarifying sys_semctl, and finally of having a coherent behaviour with [shm|msg]ctl_down
---
ipc/sem.c | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-)
ipc/sem.c +13 -11
@@ -875,6 +875,11 @@ static inline unsigned long copy_semid_f } } +/* +* This function handles some semctl commands which require the rw_mutex +* to be held in write mode. +* NOTE: no locks must be held, the rw_mutex is taken inside this function. +*/ static int semctl_down (struct ipc_namespace *ns,int semid,int semnum,int cmd,int version,union semun arg) {
@@ -887,9 +892,12 @@ static int semctl_down(struct ipc_namespif (copy_semid_from_user (&setbuf, arg.buf, version))return -EFAULT ; } +down_write (&sem_ids (ns).rw_mutex); sma =sem_lock_check_down (ns, semid); -if (IS_ERR (sma)) -return PTR_ERR (sma); +if (IS_ERR (sma)) { + err =PTR_ERR (sma); + goto out_up; + } ipcp = &sma->sem_perm;
@@ -915,26 +923,22 @@ static int semctl_down(struct ipc_namespswitch (cmd){case IPC_RMID :freeary (ns, ipcp); - err =0 ; -break ; + goto out_up;case IPC_SET : ipcp->uid = setbuf.uid; ipcp->gid = setbuf.gid; ipcp->mode = (ipcp->mode & ~S_IRWXUGO ) | (setbuf.mode &S_IRWXUGO ); sma->sem_ctime =get_seconds (); -sem_unlock (sma); - err =0 ;break ;default : -sem_unlock (sma); err = -EINVAL ; -break ; } -return err; out_unlock:sem_unlock (sma); +out_up: +up_write (&sem_ids (ns).rw_mutex);return err; }
@@ -968,9 +972,7 @@ asmlinkage long sys_semctl (int semid, ireturn err;case IPC_RMID :case IPC_SET : -down_write (&sem_ids (ns).rw_mutex); err =semctl_down (ns,semid,semnum,cmd,version,arg); -up_write (&sem_ids (ns).rw_mutex);return err;default :return -EINVAL ;
Signature · 1 line
Pierre Peiffer
PATCH 5/8 (resend) IPC/semaphores: remove one unused parameter from semctl_down()
In reply to [email protected]From: Pierre Peiffer <[email protected]>
semctl_down() takes one unused parameter: semnum.
This patch proposes to get rid of it.
--- ipc/sem.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)
ipc/sem.c +3 -3
@@ -880,8 +880,8 @@ static inline unsigned long copy_semid_f* to be held in write mode. * NOTE: no locks must be held, the rw_mutex is taken inside this function. */ -static int semctl_down (struct ipc_namespace *ns,int semid,int semnum, -int cmd,int version,union semun arg) +static int semctl_down (struct ipc_namespace *ns,int semid, +int cmd,int version,union semun arg) {struct sem_array *sma;int err;
@@ -972,7 +972,7 @@ asmlinkage long sys_semctl (int semid, ireturn err;case IPC_RMID :case IPC_SET : - err =semctl_down (ns,semid,semnum,cmd,version,arg); + err =semctl_down (ns, semid, cmd, version, arg);return err;default :return -EINVAL ;
Signature · 1 line
Pierre Peiffer
PATCH 6/8 (resend) IPC: get rid of the use *_setbuf structure.
In reply to [email protected]From: Pierre Peiffer <[email protected]>
All IPCs make use of an intermetiate *_setbuf structure to handle the IPC_SET command. This is not really needed and, moreover, it complicates a little bit the code.
This patch get rid of the use of it and uses directly the semid64_ds/ msgid64_ds/shmid64_ds structure.
In addition of removing one struture declaration, it also simplifies and improves a little bit the common 64-bits path.
---
ipc/msg.c | 51 ++++++++++++++++++--------------------------------- ipc/sem.c | 40 ++++++++++++++-------------------------- ipc/shm.c | 41 ++++++++++++++--------------------------- 3 files changed, 46 insertions(+), 86 deletions(-)
ipc/msg.c +18 -33
@@ -388,31 +388,14 @@ copy_msqid_to_user(void __user *buf, str } } -struct msq_setbuf { -unsigned long qbytes; -uid_t uid; -gid_t gid; -mode_t mode; -}; -static inline unsigned long -copy_msqid_from_user (struct msq_setbuf *out,void __user *buf,int version) +copy_msqid_from_user (struct msqid64_ds *out,void __user *buf,int version) {switch (version) {case IPC_64 : - { -struct msqid64_ds tbuf; - -if (copy_from_user (&tbuf, buf,sizeof (tbuf))) +if (copy_from_user (out, buf,sizeof (*out)))return -EFAULT ; - - out->qbytes = tbuf.msg_qbytes; - out->uid = tbuf.msg_perm.uid; - out->gid = tbuf.msg_perm.gid; - out->mode = tbuf.msg_perm.mode; -return 0 ; - }case IPC_OLD : {struct msqid_ds tbuf_old;
@@ -420,14 +403,14 @@ copy_msqid_from_user(struct msq_setbuf *if (copy_from_user (&tbuf_old, buf,sizeof (tbuf_old)))return -EFAULT ; - out->uid = tbuf_old.msg_perm.uid; - out->gid = tbuf_old.msg_perm.gid; - out->mode = tbuf_old.msg_perm.mode; + out->msg_perm.uid = tbuf_old.msg_perm.uid; + out->msg_perm.gid = tbuf_old.msg_perm.gid; + out->msg_perm.mode = tbuf_old.msg_perm.mode;if (tbuf_old.msg_qbytes ==0 ) - out->qbytes = tbuf_old.msg_lqbytes; + out->msg_qbytes = tbuf_old.msg_lqbytes;else - out->qbytes = tbuf_old.msg_qbytes; + out->msg_qbytes = tbuf_old.msg_qbytes;return 0 ; }
@@ -445,12 +428,12 @@ static int msgctl_down(struct ipc_namesp struct msqid_ds __user *buf, int version) {struct kern_ipc_perm *ipcp; -struct msq_setbuf setbuf; +struct msqid64_ds msqid64;struct msg_queue *msq;int err;if (cmd ==IPC_SET ) { -if (copy_msqid_from_user (&setbuf, buf, version)) +if (copy_msqid_from_user (&msqid64, buf, version))return -EFAULT ; }
@@ -468,8 +451,10 @@ static int msgctl_down(struct ipc_namesp goto out_unlock;if (cmd ==IPC_SET ) { - err =audit_ipc_set_perm (setbuf.qbytes, setbuf.uid, setbuf.gid, - setbuf.mode); + err =audit_ipc_set_perm (msqid64.msg_qbytes, + msqid64.msg_perm.uid, + msqid64.msg_perm.gid, + msqid64.msg_perm.mode);if (err) goto out_unlock; }
@@ -491,18 +476,18 @@ static int msgctl_down(struct ipc_namespfreeque (ns, ipcp); goto out_up;case IPC_SET : -if (setbuf.qbytes > ns->msg_ctlmnb && +if (msqid64.msg_qbytes > ns->msg_ctlmnb && !capable (CAP_SYS_RESOURCE )) { err = -EPERM ; goto out_unlock; } - msq->q_qbytes = setbuf.qbytes; + msq->q_qbytes = msqid64.msg_qbytes; - ipcp->uid = setbuf.uid; - ipcp->gid = setbuf.gid; + ipcp->uid = msqid64.msg_perm.uid; + ipcp->gid = msqid64.msg_perm.gid; ipcp->mode = (ipcp->mode & ~S_IRWXUGO ) | - (S_IRWXUGO & setbuf.mode); + (S_IRWXUGO & msqid64.msg_perm.mode); msq->q_ctime =get_seconds ();/* sleeping receivers might be excluded by * stricter permissions.
ipc/sem.c +14 -26
@@ -835,28 +835,14 @@ out_free:return err; } -struct sem_setbuf { -uid_t uid; -gid_t gid; -mode_t mode; -}; - -static inline unsigned long copy_semid_from_user (struct sem_setbuf *out,void __user *buf,int version) +static inline unsigned long +copy_semid_from_user (struct semid64_ds *out,void __user *buf,int version) {switch (version) {case IPC_64 : - { -struct semid64_ds tbuf; - -if (copy_from_user (&tbuf, buf,sizeof (tbuf))) +if (copy_from_user (out, buf,sizeof (*out)))return -EFAULT ; - - out->uid = tbuf.sem_perm.uid; - out->gid = tbuf.sem_perm.gid; - out->mode = tbuf.sem_perm.mode; -return 0 ; - }case IPC_OLD : {struct semid_ds tbuf_old;
@@ -864,9 +850,9 @@ static inline unsigned long copy_semid_fif (copy_from_user (&tbuf_old, buf,sizeof (tbuf_old)))return -EFAULT ; - out->uid = tbuf_old.sem_perm.uid; - out->gid = tbuf_old.sem_perm.gid; - out->mode = tbuf_old.sem_perm.mode; + out->sem_perm.uid = tbuf_old.sem_perm.uid; + out->sem_perm.gid = tbuf_old.sem_perm.gid; + out->sem_perm.mode = tbuf_old.sem_perm.mode;return 0 ; }
@@ -885,11 +871,11 @@ static int semctl_down(struct ipc_namesp {struct sem_array *sma;int err; -struct sem_setbuf uninitialized_var (setbuf ); +struct semid64_ds semid64;struct kern_ipc_perm *ipcp;if (cmd ==IPC_SET ) { -if (copy_semid_from_user (&setbuf, arg.buf, version)) +if (copy_semid_from_user (&semid64, arg.buf, version))return -EFAULT ; }down_write (&sem_ids (ns).rw_mutex);
@@ -906,7 +892,9 @@ static int semctl_down(struct ipc_namesp goto out_unlock;if (cmd ==IPC_SET ) { - err =audit_ipc_set_perm (0 , setbuf.uid, setbuf.gid, setbuf.mode); + err =audit_ipc_set_perm (0 , semid64.sem_perm.uid, + semid64.sem_perm.gid, + semid64.sem_perm.mode);if (err) goto out_unlock; }
@@ -925,10 +913,10 @@ static int semctl_down(struct ipc_namespfreeary (ns, ipcp); goto out_up;case IPC_SET : - ipcp->uid = setbuf.uid; - ipcp->gid = setbuf.gid; + ipcp->uid = semid64.sem_perm.uid; + ipcp->gid = semid64.sem_perm.gid; ipcp->mode = (ipcp->mode & ~S_IRWXUGO ) - | (setbuf.mode &S_IRWXUGO ); + | (semid64.sem_perm.mode &S_IRWXUGO ); sma->sem_ctime =get_seconds ();break ;default :
ipc/shm.c +14 -27
81 lines not on this page · raw message
Signature · 1 line
Pierre Peiffer
PATCH 7/8 (resend) IPC: introduce ipc_update_perm()
In reply to [email protected]From: Pierre Peiffer <[email protected]>
The IPC_SET command performs the same permission setting for all IPCs. This patch introduces a common ipc_update_perm() function to update these permissions and makes use of it for all IPCs.
---
ipc/msg.c | 5 +---- ipc/sem.c | 5 +---- ipc/shm.c | 5 +---- ipc/util.c | 13 +++++++++++++ ipc/util.h | 1 + 5 files changed, 17 insertions(+), 12 deletions(-)
ipc/msg.c +1 -4
@@ -484,10 +484,7 @@ static int msgctl_down(struct ipc_namesp msq->q_qbytes = msqid64.msg_qbytes; - ipcp->uid = msqid64.msg_perm.uid; - ipcp->gid = msqid64.msg_perm.gid; - ipcp->mode = (ipcp->mode & ~S_IRWXUGO ) | - (S_IRWXUGO & msqid64.msg_perm.mode); +ipc_update_perm (&msqid64.msg_perm, ipcp); msq->q_ctime =get_seconds ();/* sleeping receivers might be excluded by * stricter permissions.
ipc/sem.c +1 -4
@@ -913,10 +913,7 @@ static int semctl_down(struct ipc_namespfreeary (ns, ipcp); goto out_up;case IPC_SET : - ipcp->uid = semid64.sem_perm.uid; - ipcp->gid = semid64.sem_perm.gid; - ipcp->mode = (ipcp->mode & ~S_IRWXUGO ) - | (semid64.sem_perm.mode &S_IRWXUGO ); +ipc_update_perm (&semid64.sem_perm, ipcp); sma->sem_ctime =get_seconds ();break ;default :
ipc/shm.c +1 -4
@@ -657,10 +657,7 @@ static int shmctl_down(struct ipc_namespdo_shm_rmid (ns, ipcp); goto out_up;case IPC_SET : - ipcp->uid = shmid64.shm_perm.uid; - ipcp->gid = shmid64.shm_perm.gid; - ipcp->mode = (ipcp->mode & ~S_IRWXUGO ) - | (shmid64.shm_perm.mode &S_IRWXUGO ); +ipc_update_perm (&shmid64.shm_perm, ipcp); shp->shm_ctim =get_seconds ();break ;default :
ipc/util.c +13 -0
@@ -811,6 +811,19 @@ int ipcget(struct ipc_namespace *ns, strreturn ipcget_public (ns, ids, ops, params); } +/** +* ipc_update_perm - update the permissions of an IPC. +* @in: the permission given as input. +* @out: the permission of the ipc to set. +*/ +void ipc_update_perm (struct ipc64_perm *in,struct kern_ipc_perm *out) +{ + out->uid = in->uid; + out->gid = in->gid; + out->mode = (out->mode & ~S_IRWXUGO ) + | (in->mode &S_IRWXUGO ); +} +#ifdef __ARCH_WANT_IPC_PARSE_VERSION
ipc/util.h +1 -0
@@ -112,6 +112,7 @@ struct kern_ipc_perm *ipc_lock(struct ipvoid kernel_to_ipc64_perm (struct kern_ipc_perm *in,struct ipc64_perm *out);void ipc64_perm_to_ipc_perm (struct ipc64_perm *in,struct ipc_perm *out); +void ipc_update_perm (struct ipc64_perm *in,struct kern_ipc_perm *out);#if defined(__ia64__) || defined(__x86_64__) || defined(__hppa__) || defined(__XTENSA__)/* On IA-64, we always use the "64-bit version" of the IPC structures. */
Signature · 1 line
Pierre Peiffer
PATCH 8/8 (resend) IPC: consolidate all xxxctl_down() functions
In reply to [email protected]semctl_down(), msgctl_down() and shmctl_down() are used to handle the same set of commands for each kind of IPC. They all start to do the same job (they retrieve the ipc and do some permission checks) before handling the commands on their own.
This patch proposes to consolidate this by moving these same pieces of code into one common function called ipcctl_pre_down().
It simplifies a little these xxxctl_down() functions and increases a little the maintainability.
--- ipc/msg.c | 48 +++++------------------------------------------- ipc/sem.c | 42 ++++-------------------------------------- ipc/shm.c | 42 ++++-------------------------------------- ipc/util.c | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++ ipc/util.h | 2 ++ 5 files changed, 66 insertions(+), 119 deletions(-)
ipc/sem.c +4 -38
@@ -141,21 +141,6 @@ void __init sem_init (void) }/* -* This routine is called in the paths where the rw_mutex is held to protect -* access to the idr tree. -*/ -static inline struct sem_array *sem_lock_check_down (struct ipc_namespace *ns, -int id) -{ -struct kern_ipc_perm *ipcp =ipc_lock_check_down (&sem_ids (ns), id); - -if (IS_ERR (ipcp)) -return (struct sem_array *)ipcp; - - return container_of(ipcp, struct sem_array, sem_perm); -} - -/* * sem_lock_(check_) routines are called in the paths where the rw_mutex * is not held. */
@@ -878,31 +863,12 @@ static int semctl_down(struct ipc_namespif (copy_semid_from_user (&semid64, arg.buf, version))return -EFAULT ; } -down_write (&sem_ids (ns).rw_mutex); - sma =sem_lock_check_down (ns, semid); -if (IS_ERR (sma)) { - err =PTR_ERR (sma); - goto out_up; - } - - ipcp = &sma->sem_perm; - err =audit_ipc_obj (ipcp); -if (err) - goto out_unlock; + ipcp =ipcctl_pre_down (&sem_ids (ns), semid, cmd, &semid64.sem_perm,0 ); +if (IS_ERR (ipcp)) +return PTR_ERR (ipcp); -if (cmd ==IPC_SET ) { - err =audit_ipc_set_perm (0 , semid64.sem_perm.uid, - semid64.sem_perm.gid, - semid64.sem_perm.mode); -if (err) - goto out_unlock; - } -if (current->euid != ipcp->cuid && - current->euid != ipcp->uid && !capable (CAP_SYS_ADMIN )) { - err=-EPERM ; - goto out_unlock; - } + sma = container_of(ipcp, struct sem_array, sem_perm); err =security_sem_semctl (sma, cmd);if (err)
ipc/util.c +51 -0
57 lines not on this page · raw message
ipc/util.h +2 -0
@@ -113,6 +113,8 @@ struct kern_ipc_perm *ipc_lock(struct ipvoid kernel_to_ipc64_perm (struct kern_ipc_perm *in,struct ipc64_perm *out);void ipc64_perm_to_ipc_perm (struct ipc64_perm *in,struct ipc_perm *out);void ipc_update_perm (struct ipc64_perm *in,struct kern_ipc_perm *out); +struct kern_ipc_perm *ipcctl_pre_down (struct ipc_ids *ids,int id,int cmd, +struct ipc64_perm *perm,int extrat_perm);#if defined(__ia64__) || defined(__x86_64__) || defined(__hppa__) || defined(__XTENSA__)/* On IA-64, we always use the "64-bit version" of the IPC structures. */
ipc/msg.c +5 -43
61 lines not on this page · raw message
ipc/shm.c +4 -38
55 lines not on this page · raw message
Signature · 1 line
Pierre Peiffer
Re: [PATCH 2.6.24-mm1 8/8] (resend) IPC: consolidate all xxxctl_down() functions
Hi,
kernel-doc here causes a warning in 2.6.25-rc3-mm1:
Warning(linux-2.6.25-rc3-mm1//ipc/util.c:845): No description found for parameter 'extrat_perm'
Quoted 5 lines · On Tue, 12 Feb 2008 17:13:48 +0100 [email protected] wrote:
Index:
b/ipc/util.c
===================================================================
ipc/util.c void ipc_update_perm(struct ipc64_perm * · line 833
Quoted 10 lines
ipc/util.c +7
-0@@ -824,6 +824,57 @@ void ipc_update_perm(struct ipc64_perm * | (in->mode & S_IRWXUGO); } +/** +* ipcctl_pre_down - retrieve an ipc and check permissions for some IPC_XXX cmd +* @ids: the table of ids where to look for the ipc +* @id: the id of the ipc to retrieve +* @cmd: the cmd to check +* @perm: the permission to set +* @extra_perm: one extra permission parameter used by msq
change this ^^^^^ or ... (preferably the next changes)
ipc/util.c void ipc_update_perm(struct ipc64_perm * · line 844
Quoted 11 lines
ipc/util.c +11
-0+* +* This function does some common audit and permissions check for some IPC_XXX +* cmd and is called from semctl_down, shmctl_down and msgctl_down. +* It must be called without any lock held and +* - retrieves the ipc with the given id in the given table. +* - performs some audit and permission check, depending on the given cmd +* - returns the ipc with both ipc and rw_mutex locks held in case of success +* or an err-code without any lock held otherwise. +*/ +struct kern_ipc_perm *ipcctl_pre_down (struct ipc_ids *ids,int id,int cmd, +struct ipc64_perm *perm,int extrat_perm)
that last parameter and ...
Quoted 1 line
ipc/util.c +1
-0+{
...
Quoted 10 lines
ipc/util.c +2
-0+} +#ifdef __ARCH_WANT_IPC_PARSE_VERSIONIndex:
b/ipc/util.h
===================================================================
ipc/util.h struct kern_ipc_perm *ipc_lock(struct ip · line 117
Quoted 5 lines
ipc/util.h +2
-0@@ -113,6 +113,8 @@ struct kern_ipc_perm *ipc_lock(struct ipvoid kernel_to_ipc64_perm (struct kern_ipc_perm *in,struct ipc64_perm *out);void ipc64_perm_to_ipc_perm (struct ipc64_perm *in,struct ipc_perm *out);void ipc_update_perm (struct ipc64_perm *in,struct kern_ipc_perm *out); +struct kern_ipc_perm *ipcctl_pre_down (struct ipc_ids *ids,int id,int cmd, +struct ipc64_perm *perm,int extrat_perm);
that last parameter.
ipc/util.h struct kern_ipc_perm *ipc_lock(struct ip · line 120
Quoted 3 lines
ipc/util.h +0
-0#if defined(__ia64__) || defined(__x86_64__) || defined(__hppa__) || defined(__XTENSA__)/* On IA-64, we always use the "64-bit version" of the IPC structures. */
---
~Randy
Quoted 11 lines · [email protected] writes:
This is a resend of the first part of the patchset sent 2 weeks
ago. This is the part about the IPC which (again) proposes to consolidate some part of the existing code.It does not change the behavior of the existing code, but
improves it in term of readability and maintainability as it consolidates it a little. As there was no objection, I think you can include them in your -mm tree.The patchset applies on top of "2.6.24-mm1 + previous patches about
IPC" sent the last days (ie Nadia's patches + mine).
While I have not read everything in detail from a quick overview the whole patch series looks like a nice and valuable cleanup to me.
I was a bit sceptical about all the interface enhancements your original patchkit had, but this one looks just fine.
-Andi
Quoted 11 lines · Andi Kleen
Quoted 9 lines · [email protected] writes:
This is a resend of the first part of the patchset sent 2 weeks
ago. This is the part about the IPC which (again) proposes to consolidate some part of the existing code.It does not change the behavior of the existing code, but
improves it in term of readability and maintainability as it consolidates it a little. As there was no objection, I think you can include them in your -mm tree.
Quoted 9 lines
Quoted 2 lines
The patchset applies on top of "2.6.24-mm1 + previous patches about
IPC" sent the last days (ie Nadia's patches + mine).While I have not read everything in detail from a quick overview the whole patch series looks like a nice and valuable cleanup to me.
I was a bit sceptical about all the interface enhancements your original patchkit had, but this one looks just fine.
Thanks Andi for spending time on this review.
All kind of comments (positive or negative) are always welcome to make progress, but of course, I particularly appreciate such positive feedbacks ;)
Signature · 1 line
Pierre