Linux Container Development
 help / color / mirror / Atom feed
From: "Serge E. Hallyn" <serue-r/Jw6+rmf7HQT0dZR+AlfA@public.gmane.org>
To: Nadia.Derbey-6ktuUTfB/bM@public.gmane.org
Cc: containers-cunTk1MwBs9QetFLy7KEm3xJsTq8ys+cHZ5vskTnxNA@public.gmane.org,
	Pierre Peiffer <pierre.peiffer-6ktuUTfB/bM@public.gmane.org>
Subject: Re: [RFC PATCH 1/6] IPC/sem: RCU-protect the process semundo list
Date: Wed, 25 Jun 2008 15:33:19 -0500	[thread overview]
Message-ID: <20080625203319.GA16374@us.ibm.com> (raw)
In-Reply-To: <20080625135538.385496000-6ktuUTfB/bM@public.gmane.org>

Quoting Nadia.Derbey-6ktuUTfB/bM@public.gmane.org (Nadia.Derbey-6ktuUTfB/bM@public.gmane.org):
> PATCH [01/06]
> 
> Today, 'current' has an exclusive access to its sem_undo_list (anchored at
> current->sysvsem.undo_list):
>   . it is created during a semop() if the SEMUNDO flag is specified for one
>     of the semaphores.
>   . it can also be created during a copy_process() operation if the
>     CLONE_SYSVSEM flag is specified (in that case the undo_list is created/
>     copied from 'current' into the target task but that target task is not
>     running yet).
>   . it is freed during an unshare() or an exit() operation, if the caller
>     (current) is the last task using it.
> 
> 
> In order to allow a third party process to read a process' undo list, without
> a too big performance impact on the existing operations, this patch proposes
> to make the sem_undo_list structures rcu protected.
> 
> They can then be safely accessed by any task inside read critical section,
> this way:
> 
>         struct sem_undo_list *undo_list;
>         int ret;
>         ...
>         rcu_read_lock();
>         undo_list = rcu_dereference(task->sysvsem.undo_list);
>         if (undo_list)   
>                 ret = atomic_inc_not_zero(&undo_list->refcnt);
>         rcu_read_unlock();
>         ...
>         if (undo_list && ret) {
>                 /* section where undo_list can be used quietly */
>                 ...
>         }
>         ...
> 
> Signed-off-by: Pierre Peiffer <pierre.peiffer-6ktuUTfB/bM@public.gmane.org>
> Signed-off-by: Nadia Derbey <Nadia.Derbey-6ktuUTfB/bM@public.gmane.org>

Acked-by: Serge Hallyn <serue-r/Jw6+rmf7HQT0dZR+AlfA@public.gmane.org>

thanks,
-serge

> 
> ---
>  include/linux/sem.h |    4 +++-
>  ipc/sem.c           |   22 ++++++++++++++++++----
>  2 files changed, 21 insertions(+), 5 deletions(-)
> 
> Index: linux-2.6.26-rc5-mm3/include/linux/sem.h
> ===================================================================
> --- linux-2.6.26-rc5-mm3.orig/include/linux/sem.h	2008-06-24 09:04:21.000000000 +0200
> +++ linux-2.6.26-rc5-mm3/include/linux/sem.h	2008-06-24 10:01:50.000000000 +0200
> @@ -112,7 +112,8 @@ struct sem_queue {
>  };
> 
>  /* Each task has a list of undo requests. They are executed automatically
> - * when the process exits.
> + * when the last refcnt of sem_undo_list is released (ie when the process
> + * exits in the general case).
>   */
>  struct sem_undo {
>  	struct list_head	list_proc;	/* per-process list: all undos from one process. */
> @@ -131,6 +132,7 @@ struct sem_undo_list {
>  	atomic_t		refcnt;
>  	spinlock_t		lock;
>  	struct list_head	list_proc;
> +	struct rcu_head		rcu;
>  };
> 
>  struct sysv_sem {
> Index: linux-2.6.26-rc5-mm3/ipc/sem.c
> ===================================================================
> --- linux-2.6.26-rc5-mm3.orig/ipc/sem.c	2008-06-24 09:05:03.000000000 +0200
> +++ linux-2.6.26-rc5-mm3/ipc/sem.c	2008-06-24 09:37:33.000000000 +0200
> @@ -939,6 +939,10 @@ static inline int get_undo_list(struct s
>  {
>  	struct sem_undo_list *undo_list;
> 
> +	/*
> +	 * No need to have a rcu read critical section here: no one but
> +	 * current is accessing the undo_list.
> +	 */
>  	undo_list = current->sysvsem.undo_list;
>  	if (!undo_list) {
>  		undo_list = kzalloc(sizeof(*undo_list), GFP_KERNEL);
> @@ -948,7 +952,7 @@ static inline int get_undo_list(struct s
>  		atomic_set(&undo_list->refcnt, 1);
>  		INIT_LIST_HEAD(&undo_list->list_proc);
> 
> -		current->sysvsem.undo_list = undo_list;
> +		rcu_assign_pointer(current->sysvsem.undo_list, undo_list);
>  	}
>  	*undo_listp = undo_list;
>  	return 0;
> @@ -1268,10 +1272,15 @@ void exit_sem(struct task_struct *tsk)
>  {
>  	struct sem_undo_list *ulp;
> 
> -	ulp = tsk->sysvsem.undo_list;
> -	if (!ulp)
> +	rcu_read_lock();
> +	ulp = rcu_dereference(tsk->sysvsem.undo_list);
> +	if (!ulp) {
> +		rcu_read_unlock();
>  		return;
> -	tsk->sysvsem.undo_list = NULL;
> +	}
> +	rcu_read_unlock();
> +	rcu_assign_pointer(tsk->sysvsem.undo_list, NULL);
> +	synchronize_rcu();
> 
>  	if (!atomic_dec_and_test(&ulp->refcnt))
>  		return;
> @@ -1349,6 +1358,11 @@ void exit_sem(struct task_struct *tsk)
> 
>  		call_rcu(&un->rcu, free_un);
>  	}
> +	/*
> +	 * No need to call synchronize_rcu() here: we come here if the refcnt
> +	 * is 0 and this has been done into exit_sem after synchronizing. So
> +	 * nobody else can be referencing to the undo_list.
> +	 */
>  	kfree(ulp);
>  }
> 
> 
> --

  parent reply	other threads:[~2008-06-25 20:33 UTC|newest]

Thread overview: 20+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2008-06-25 13:49 [RFC PATCH 0/6] SYSVIPC/semaphores - allow saving/restoring a process' semundo_list Nadia.Derbey-6ktuUTfB/bM
2008-06-25 13:49 ` [RFC PATCH 1/6] IPC/sem: RCU-protect the process semundo list Nadia.Derbey-6ktuUTfB/bM
     [not found]   ` <20080625135538.385496000-6ktuUTfB/bM@public.gmane.org>
2008-06-25 20:33     ` Serge E. Hallyn [this message]
2008-06-25 13:49 ` [RFC PATCH 2/6] IPC/sem: per <pid> semundo file in procfs Nadia.Derbey-6ktuUTfB/bM
     [not found]   ` <20080625135538.762662000-6ktuUTfB/bM@public.gmane.org>
2008-06-25 20:33     ` Serge E. Hallyn
2008-06-26  5:08     ` Michael Kerrisk
     [not found]       ` <517f3f820806252208m1f5b91dfm38b8babff73adf72-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org>
2008-06-26  6:07         ` Nadia Derbey
2008-06-25 13:49 ` [RFC PATCH 3/6] IPC/sem: start/stop operations for /proc/pid/semundo Nadia.Derbey-6ktuUTfB/bM
     [not found]   ` <20080625135539.139605000-6ktuUTfB/bM@public.gmane.org>
2008-06-25 20:39     ` Serge E. Hallyn
2008-06-25 13:49 ` [RFC PATCH 4/6] IPC/sem: next " Nadia.Derbey-6ktuUTfB/bM
     [not found]   ` <20080625135539.519489000-6ktuUTfB/bM@public.gmane.org>
2008-06-25 20:57     ` Serge E. Hallyn
     [not found]       ` <20080625205719.GD16374-r/Jw6+rmf7HQT0dZR+AlfA@public.gmane.org>
2008-06-26  5:35         ` Nadia Derbey
2008-06-25 13:49 ` [RFC PATCH 5/6] IPC/sem: .show operation " Nadia.Derbey-6ktuUTfB/bM
     [not found]   ` <20080625135539.893049000-6ktuUTfB/bM@public.gmane.org>
2008-06-25 20:58     ` Serge E. Hallyn
2008-06-25 13:49 ` [RFC PATCH 6/6] IPC/sem: .write operation for /proc/<self>/semundo Nadia.Derbey-6ktuUTfB/bM
     [not found]   ` <20080625135540.271934000-6ktuUTfB/bM@public.gmane.org>
2008-06-25 21:09     ` Serge E. Hallyn
     [not found]       ` <20080625210937.GF16374-r/Jw6+rmf7HQT0dZR+AlfA@public.gmane.org>
2008-06-26  5:44         ` Nadia Derbey
2008-06-27 14:06     ` Serge E. Hallyn
     [not found]       ` <20080627140607.GA20581-r/Jw6+rmf7HQT0dZR+AlfA@public.gmane.org>
2008-06-27 14:12         ` Nadia Derbey
2008-06-30  8:37         ` Nadia Derbey

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20080625203319.GA16374@us.ibm.com \
    --to=serue-r/jw6+rmf7hqt0dzr+alfa@public.gmane.org \
    --cc=Nadia.Derbey-6ktuUTfB/bM@public.gmane.org \
    --cc=containers-cunTk1MwBs9QetFLy7KEm3xJsTq8ys+cHZ5vskTnxNA@public.gmane.org \
    --cc=pierre.peiffer-6ktuUTfB/bM@public.gmane.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox