public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH] IPC: fix error case when idr-cache is empty in ipcget()
@ 2007-10-10  9:46 Pierre Peiffer
  2007-10-11  6:58 ` Nadia Derbey
  0 siblings, 1 reply; 4+ messages in thread
From: Pierre Peiffer @ 2007-10-10  9:46 UTC (permalink / raw)
  To: Andrew Morton, Nadia Derbey; +Cc: linux-kernel

With the use of idr to store the ipc, the case where the idr cache is
empty, when idr_get_new is called (this may happen even if we call
idr_pre_get() before), is not well handled: it lets semget()/shmget()/msgget()
return ENOSPC when this cache is empty, what 1. does not reflect the facts
and 2. does not conform to the man(s).

This patch fixes this by retrying the whole process of allocation in this case.

Note: we could directly return ENOMEM if idr_pre_get() fails, but it does not
mean that the cache is empty...

This patch applies on top of 2.6.23-rc8-mm2 and should probably be merged
in 2.6.24 if Nadia's patches are included.

Signed-off-by: Pierre Peiffer <pierre.peiffer@bull.net>

---
 ipc/msg.c  |    4 ++--
 ipc/sem.c  |    4 ++--
 ipc/shm.c  |    5 +++--
 ipc/util.c |   35 +++++++++++++++++++++--------------
 4 files changed, 28 insertions(+), 20 deletions(-)

Index: b/ipc/msg.c
===================================================================
--- a/ipc/msg.c
+++ b/ipc/msg.c
@@ -188,10 +188,10 @@ static int newque(struct ipc_namespace *
 	 * ipc_addid() locks msq
 	 */
 	id = ipc_addid(&msg_ids(ns), &msq->q_perm, ns->msg_ctlmni);
-	if (id == -1) {
+	if (id < 0) {
 		security_msg_queue_free(msq);
 		ipc_rcu_putref(msq);
-		return -ENOSPC;
+		return id;
 	}
 
 	msq->q_perm.id = msg_buildid(ns, id, msq->q_perm.seq);
Index: b/ipc/sem.c
===================================================================
--- a/ipc/sem.c
+++ b/ipc/sem.c
@@ -269,10 +269,10 @@ static int newary(struct ipc_namespace *
 	}
 
 	id = ipc_addid(&sem_ids(ns), &sma->sem_perm, ns->sc_semmni);
-	if(id == -1) {
+	if (id < 0) {
 		security_sem_free(sma);
 		ipc_rcu_putref(sma);
-		return -ENOSPC;
+		return id;
 	}
 	ns->used_sems += nsems;
 
Index: b/ipc/shm.c
===================================================================
--- a/ipc/shm.c
+++ b/ipc/shm.c
@@ -409,10 +409,11 @@ static int newseg(struct ipc_namespace *
 	if (IS_ERR(file))
 		goto no_file;
 
-	error = -ENOSPC;
 	id = shm_addid(ns, shp);
-	if(id == -1) 
+	if (id < 0) {
+		error = id;
 		goto no_id;
+	}
 
 	shp->shm_cprid = task_tgid_vnr(current);
 	shp->shm_lprid = 0;
Index: b/ipc/util.c
===================================================================
--- a/ipc/util.c
+++ b/ipc/util.c
@@ -261,7 +261,7 @@ int ipc_get_maxid(struct ipc_ids *ids)
  *	Add an entry 'new' to the IPC ids idr. The permissions object is
  *	initialised and the first free entry is set up and the id assigned
  *	is returned. The 'new' entry is returned in a locked state on success.
- *	On failure the entry is not locked and -1 is returned.
+ *	On failure the entry is not locked and a negative err-code is returned.
  *
  *	Called with ipc_ids.mutex held.
  */
@@ -274,11 +274,11 @@ int ipc_addid(struct ipc_ids* ids, struc
 		size = IPCMNI;
 
 	if (ids->in_use >= size)
-		return -1;
+		return -ENOSPC;
 
 	err = idr_get_new(&ids->ipcs_idr, new, &id);
 	if (err)
-		return -1;
+		return err;
 
 	ids->in_use++;
 
@@ -309,17 +309,20 @@ int ipc_addid(struct ipc_ids* ids, struc
 int ipcget_new(struct ipc_namespace *ns, struct ipc_ids *ids,
 		struct ipc_ops *ops, struct ipc_params *params)
 {
-	int err;
-
-	err = idr_pre_get(&ids->ipcs_idr, GFP_KERNEL);
-
-	if (!err)
-		return -ENOMEM;
+	int err, alloc;
+retry:
+	alloc = idr_pre_get(&ids->ipcs_idr, GFP_KERNEL);
 
 	mutex_lock(&ids->mutex);
 	err = ops->getnew(ns, params);
 	mutex_unlock(&ids->mutex);
 
+	if (err == -EAGAIN) {
+		if (alloc)
+			goto retry;
+		else
+			err = -ENOMEM;
+	}
 	return err;
 }
 
@@ -372,9 +375,9 @@ int ipcget_public(struct ipc_namespace *
 {
 	struct kern_ipc_perm *ipcp;
 	int flg = params->flg;
-	int err;
-
-	err = idr_pre_get(&ids->ipcs_idr, GFP_KERNEL);
+	int err, alloc;
+retry:
+	alloc = idr_pre_get(&ids->ipcs_idr, GFP_KERNEL);
 
 	mutex_lock(&ids->mutex);
 	ipcp = ipc_findkey(ids, params->key);
@@ -382,8 +385,6 @@ int ipcget_public(struct ipc_namespace *
 		/* key not used */
 		if (!(flg & IPC_CREAT))
 			err = -ENOENT;
-		else if (!err)
-			err = -ENOMEM;
 		else
 			err = ops->getnew(ns, params);
 	} else {
@@ -406,6 +407,12 @@ int ipcget_public(struct ipc_namespace *
 	}
 	mutex_unlock(&ids->mutex);
 
+	if (err == -EAGAIN) {
+		if (alloc)
+			goto retry;
+		else
+			err = -ENOMEM;
+	}
 	return err;
 }
 

-- 
Pierre Peiffer

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] IPC: fix error case when idr-cache is empty in ipcget()
  2007-10-10  9:46 [PATCH] IPC: fix error case when idr-cache is empty in ipcget() Pierre Peiffer
@ 2007-10-11  6:58 ` Nadia Derbey
  0 siblings, 0 replies; 4+ messages in thread
From: Nadia Derbey @ 2007-10-11  6:58 UTC (permalink / raw)
  To: Pierre Peiffer; +Cc: Andrew Morton, linux-kernel

Pierre Peiffer wrote:
> With the use of idr to store the ipc, the case where the idr cache is
> empty, when idr_get_new is called (this may happen even if we call
> idr_pre_get() before), is not well handled: it lets semget()/shmget()/msgget()
> return ENOSPC when this cache is empty, what 1. does not reflect the facts
> and 2. does not conform to the man(s).
> 
> This patch fixes this by retrying the whole process of allocation in this case.
> 
> Note: we could directly return ENOMEM if idr_pre_get() fails, but it does not
> mean that the cache is empty...
> 

Pierre,

I agree with everythying, but not on idr_pre_get() failure: I think we 
should give up in that case: the tests on idr_pre_get() return code 
should remain IMHO (see after).


><snip>
> 
> @@ -309,17 +309,20 @@ int ipc_addid(struct ipc_ids* ids, struc
>  int ipcget_new(struct ipc_namespace *ns, struct ipc_ids *ids,
>  		struct ipc_ops *ops, struct ipc_params *params)
>  {
> -	int err;
> -
> -	err = idr_pre_get(&ids->ipcs_idr, GFP_KERNEL);
> -
> -	if (!err)
> -		return -ENOMEM;
> +	int err, alloc;
> +retry:
> +	alloc = idr_pre_get(&ids->ipcs_idr, GFP_KERNEL);
>  
>  	mutex_lock(&ids->mutex);
>  	err = ops->getnew(ns, params);
>  	mutex_unlock(&ids->mutex);
>  
> +	if (err == -EAGAIN) {
> +		if (alloc)
> +			goto retry;
> +		else
> +			err = -ENOMEM;
> +	}
>  	return err;
>  }


ops->getnew will call idr_get_new() that should not be called if 
idr_pre_get() has failed. So I think that we should give up if 
idr_pre_get() fails and the test on idr_pre_get() return code should not 
be removed.

>  
> @@ -372,9 +375,9 @@ int ipcget_public(struct ipc_namespace *
>  {
>  	struct kern_ipc_perm *ipcp;
>  	int flg = params->flg;
> -	int err;
> -
> -	err = idr_pre_get(&ids->ipcs_idr, GFP_KERNEL);
> +	int err, alloc;
> +retry:
> +	alloc = idr_pre_get(&ids->ipcs_idr, GFP_KERNEL);
>  
>  	mutex_lock(&ids->mutex);
>  	ipcp = ipc_findkey(ids, params->key);
> @@ -382,8 +385,6 @@ int ipcget_public(struct ipc_namespace *
>  		/* key not used */
>  		if (!(flg & IPC_CREAT))
>  			err = -ENOENT;
> -		else if (!err)
> -			err = -ENOMEM;
>  		else
>  			err = ops->getnew(ns, params);

Same remark as above:
ops->getnew will call idr_get_new() that should not be called if 
idr_pre_get() has failed. So I think that we should give up if 
idr_pre_get() fails and this this test should not be removed.


Regards,
Nadia





^ permalink raw reply	[flat|nested] 4+ messages in thread

* [PATCH] IPC: fix error case when idr-cache is empty in ipcget()
@ 2007-10-11  7:32 Pierre Peiffer
  2007-10-15 20:13 ` Andrew Morton
  0 siblings, 1 reply; 4+ messages in thread
From: Pierre Peiffer @ 2007-10-11  7:32 UTC (permalink / raw)
  To: Andrew Morton, Nadia Derbey; +Cc: linux-kernel

I resend this patch, by taking into account Nadia's remarks.

With the use of idr to store the ipc, the case where the idr cache is
empty, when idr_get_new is called (this may happen even if we call
idr_pre_get() before), is not well handled: it lets semget()/shmget()/msgget()
return ENOSPC when this cache is empty, what 1. does not reflect the facts
and 2. does not conform to the man(s).

This patch fixes this by retrying the whole process of allocation in this case.

This patch applies on top of 2.6.23-rc8-mm2 and should probably be merged
in 2.6.24 if Nadia's patches are included.

Signed-off-by: Pierre Peiffer <pierre.peiffer@bull.net>

---
 ipc/msg.c  |    4 ++--
 ipc/sem.c  |    4 ++--
 ipc/shm.c  |    5 +++--
 ipc/util.c |   16 +++++++++++-----
 4 files changed, 18 insertions(+), 11 deletions(-)

Index: b/ipc/msg.c
===================================================================
--- a/ipc/msg.c
+++ b/ipc/msg.c
@@ -188,10 +188,10 @@ static int newque(struct ipc_namespace *
 	 * ipc_addid() locks msq
 	 */
 	id = ipc_addid(&msg_ids(ns), &msq->q_perm, ns->msg_ctlmni);
-	if (id == -1) {
+	if (id < 0) {
 		security_msg_queue_free(msq);
 		ipc_rcu_putref(msq);
-		return -ENOSPC;
+		return id;
 	}
 
 	msq->q_perm.id = msg_buildid(ns, id, msq->q_perm.seq);
Index: b/ipc/sem.c
===================================================================
--- a/ipc/sem.c
+++ b/ipc/sem.c
@@ -269,10 +269,10 @@ static int newary(struct ipc_namespace *
 	}
 
 	id = ipc_addid(&sem_ids(ns), &sma->sem_perm, ns->sc_semmni);
-	if(id == -1) {
+	if (id < 0) {
 		security_sem_free(sma);
 		ipc_rcu_putref(sma);
-		return -ENOSPC;
+		return id;
 	}
 	ns->used_sems += nsems;
 
Index: b/ipc/shm.c
===================================================================
--- a/ipc/shm.c
+++ b/ipc/shm.c
@@ -409,10 +409,11 @@ static int newseg(struct ipc_namespace *
 	if (IS_ERR(file))
 		goto no_file;
 
-	error = -ENOSPC;
 	id = shm_addid(ns, shp);
-	if(id == -1) 
+	if (id < 0) {
+		error = id;
 		goto no_id;
+	}
 
 	shp->shm_cprid = task_tgid_vnr(current);
 	shp->shm_lprid = 0;
Index: b/ipc/util.c
===================================================================
--- a/ipc/util.c
+++ b/ipc/util.c
@@ -261,7 +261,7 @@ int ipc_get_maxid(struct ipc_ids *ids)
  *	Add an entry 'new' to the IPC ids idr. The permissions object is
  *	initialised and the first free entry is set up and the id assigned
  *	is returned. The 'new' entry is returned in a locked state on success.
- *	On failure the entry is not locked and -1 is returned.
+ *	On failure the entry is not locked and a negative err-code is returned.
  *
  *	Called with ipc_ids.mutex held.
  */
@@ -274,11 +274,11 @@ int ipc_addid(struct ipc_ids* ids, struc
 		size = IPCMNI;
 
 	if (ids->in_use >= size)
-		return -1;
+		return -ENOSPC;
 
 	err = idr_get_new(&ids->ipcs_idr, new, &id);
 	if (err)
-		return -1;
+		return err;
 
 	ids->in_use++;
 
@@ -310,7 +310,7 @@ int ipcget_new(struct ipc_namespace *ns,
 		struct ipc_ops *ops, struct ipc_params *params)
 {
 	int err;
-
+retry:
 	err = idr_pre_get(&ids->ipcs_idr, GFP_KERNEL);
 
 	if (!err)
@@ -320,6 +320,9 @@ int ipcget_new(struct ipc_namespace *ns,
 	err = ops->getnew(ns, params);
 	mutex_unlock(&ids->mutex);
 
+	if (err == -EAGAIN)
+		goto retry;
+
 	return err;
 }
 
@@ -373,7 +376,7 @@ int ipcget_public(struct ipc_namespace *
 	struct kern_ipc_perm *ipcp;
 	int flg = params->flg;
 	int err;
-
+retry:
 	err = idr_pre_get(&ids->ipcs_idr, GFP_KERNEL);
 
 	mutex_lock(&ids->mutex);
@@ -406,6 +409,9 @@ int ipcget_public(struct ipc_namespace *
 	}
 	mutex_unlock(&ids->mutex);
 
+	if (err == -EAGAIN)
+		goto retry;
+
 	return err;
 }

-- 
Pierre Peiffer

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] IPC: fix error case when idr-cache is empty in ipcget()
  2007-10-11  7:32 Pierre Peiffer
@ 2007-10-15 20:13 ` Andrew Morton
  0 siblings, 0 replies; 4+ messages in thread
From: Andrew Morton @ 2007-10-15 20:13 UTC (permalink / raw)
  To: Pierre Peiffer; +Cc: Nadia.Derbey, linux-kernel

On Thu, 11 Oct 2007 09:32:22 +0200
Pierre Peiffer <Pierre.Peiffer@bull.net> wrote:

> @@ -373,7 +376,7 @@ int ipcget_public(struct ipc_namespace *
>  	struct kern_ipc_perm *ipcp;
>  	int flg = params->flg;
>  	int err;
> -
> +retry:
>  	err = idr_pre_get(&ids->ipcs_idr, GFP_KERNEL);
>  
>  	mutex_lock(&ids->mutex);
> @@ -406,6 +409,9 @@ int ipcget_public(struct ipc_namespace *
>  	}
>  	mutex_unlock(&ids->mutex);
>  
> +	if (err == -EAGAIN)
> +		goto retry;
> +
>  	return err;
>  }

Not pretty, is it?  The idr interface isn't a good one.  But then, any
storage library (aka container class) which allocates memory at insertion
time is a pain to handle in the kernel.

btw, idr_pre_get() should return 0 on success, else an errno - the current
interface is silly.


^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2007-10-15 20:13 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2007-10-10  9:46 [PATCH] IPC: fix error case when idr-cache is empty in ipcget() Pierre Peiffer
2007-10-11  6:58 ` Nadia Derbey
  -- strict thread matches above, loose matches on Subject: below --
2007-10-11  7:32 Pierre Peiffer
2007-10-15 20:13 ` Andrew Morton

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox