From mboxrd@z Thu Jan 1 00:00:00 1970 From: Sukadev Bhattiprolu Subject: [RFC][v8][PATCH 4/10]: Add target_pid parameter to alloc_pidmap() Date: Mon, 12 Oct 2009 21:51:04 -0700 Message-ID: <20091013045104.GD28435@us.ibm.com> References: <20091013044925.GA28181@us.ibm.com> Mime-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Return-path: Content-Disposition: inline In-Reply-To: <20091013044925.GA28181-r/Jw6+rmf7HQT0dZR+AlfA@public.gmane.org> List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: containers-bounces-cunTk1MwBs9QetFLy7KEm3xJsTq8ys+cHZ5vskTnxNA@public.gmane.org Errors-To: containers-bounces-cunTk1MwBs9QetFLy7KEm3xJsTq8ys+cHZ5vskTnxNA@public.gmane.org To: linux-kernel-u79uwXL29TY76Z2rM5mHXA@public.gmane.org Cc: randy.dunlap-QHcLZuEGTsvQT0dZR+AlfA@public.gmane.org, arnd-r2nGTMty4D4@public.gmane.org, Containers , Nathan Lynch , Louis.Rilling-aw0BnHfMbSpBDgjK7y7TUQ@public.gmane.org, "Eric W. Biederman" , kosaki.motohiro-+CUm20s59erQFUHtdCDX3A@public.gmane.org, hpa-YMNOUZJC4hwAvxtiuMwx3w@public.gmane.org, mingo-X9Un+BFzKDI@public.gmane.org, linux-api-u79uwXL29TY76Z2rM5mHXA@public.gmane.org, torvalds-de/tnXTf+JLsfHDXvbKv3WD2FQJk+8+b@public.gmane.org, Alexey Dobriyan , roland-H+wXaHxf7aLQT0dZR+AlfA@public.gmane.org, Pavel Emelyanov List-Id: linux-api@vger.kernel.org Subject: [RFC][v8][PATCH 4/10]: Add target_pid parameter to alloc_pidmap() With support for setting a specific pid number for a process, alloc_pidmap() will need a 'target_pid' parameter. Changelog[v6]: - Separate target_pid > 0 case to minimize the number of checks needed. Changelog[v3]: - (Eric Biederman): Avoid set_pidmap() function. Added couple of checks for target_pid in alloc_pidmap() itself. Changelog[v2]: - (Serge Hallyn) Check for 'pid < 0' in set_pidmap().(Code actually checks for 'pid <= 0' for completeness). Signed-off-by: Sukadev Bhattiprolu Acked-by: Serge Hallyn --- kernel/pid.c | 26 +++++++++++++++++++++----- 1 file changed, 21 insertions(+), 5 deletions(-) Index: linux-2.6/kernel/pid.c =================================================================== --- linux-2.6.orig/kernel/pid.c 2009-09-09 19:07:20.000000000 -0700 +++ linux-2.6/kernel/pid.c 2009-09-10 10:23:27.000000000 -0700 @@ -146,16 +146,22 @@ static int alloc_pidmap_page(struct pidm return 0; } -static int alloc_pidmap(struct pid_namespace *pid_ns) +static int alloc_pidmap(struct pid_namespace *pid_ns, int target_pid) { int i, offset, max_scan, pid, last = pid_ns->last_pid; int rc = -EAGAIN; int pid_max = pid_ns->pid_max; struct pidmap *map; - pid = last + 1; - if (pid >= pid_max) - pid = RESERVED_PIDS; + if (target_pid) { + pid = target_pid; + if (pid < 0 || pid >= pid_max) + return -EINVAL; + } else { + pid = last + 1; + if (pid >= pid_max) + pid = RESERVED_PIDS; + } offset = pid & BITS_PER_PAGE_MASK; map = &pid_ns->pidmap[pid/BITS_PER_PAGE]; max_scan = (pid_max + BITS_PER_PAGE - 1)/BITS_PER_PAGE - !offset; @@ -164,6 +170,15 @@ static int alloc_pidmap(struct pid_names if (rc) break; + if (target_pid) { + rc = -EBUSY; + if (!test_and_set_bit(offset, map->page)) { + atomic_dec(&map->nr_free); + rc = pid; + } + return rc; + } + if (likely(atomic_read(&map->nr_free))) { do { if (!test_and_set_bit(offset, map->page)) { @@ -196,6 +211,7 @@ static int alloc_pidmap(struct pid_names } pid = mk_pid(pid_ns, map, offset); } + return rc; } @@ -272,7 +288,7 @@ struct pid *alloc_pid(struct pid_namespa tmp = ns; for (i = ns->level; i >= 0; i--) { - nr = alloc_pidmap(tmp); + nr = alloc_pidmap(tmp, 0); if (nr < 0) goto out_free;