From: Nadia.Derbey-6ktuUTfB/bM@public.gmane.org
To: containers-cunTk1MwBs9QetFLy7KEm3xJsTq8ys+cHZ5vskTnxNA@public.gmane.org
Cc: Nadia Derbey <Nadia.Derbey-6ktuUTfB/bM@public.gmane.org>,
xemul-GEFAQzZX7r8dnm+yROfE0A@public.gmane.org
Subject: [RFC][PATCH 4/4] PID: use the target ID specified in procfs
Date: Fri, 28 Mar 2008 10:53:13 +0100 [thread overview]
Message-ID: <20080328095547.794858000@bull.net> (raw)
In-Reply-To: 20080328095309.117134000@bull.net
[-- Attachment #1: upidnr_use_next_id.patch --]
[-- Type: text/plain, Size: 6380 bytes --]
[PATCH 04/04]
This patch makes use of the target ids specified by a previous write to
/proc/self/next_id as the ids to use to allocate the next upid nrs.
Upper levels upid nrs that are not specified in next_pids file are left to the
kernel choice.
Signed-off-by: Nadia Derbey <Nadia.Derbey-6ktuUTfB/bM@public.gmane.org>
---
include/linux/pid.h | 2
kernel/fork.c | 3 -
kernel/pid.c | 141 +++++++++++++++++++++++++++++++++++++++++++++-------
3 files changed, 126 insertions(+), 20 deletions(-)
Index: linux-2.6.25-rc3-mm1/include/linux/pid.h
===================================================================
--- linux-2.6.25-rc3-mm1.orig/include/linux/pid.h 2008-03-28 08:18:53.000000000 +0100
+++ linux-2.6.25-rc3-mm1/include/linux/pid.h 2008-03-28 08:21:19.000000000 +0100
@@ -119,7 +119,7 @@ extern struct pid *find_get_pid(int nr);
extern struct pid *find_ge_pid(int nr, struct pid_namespace *);
int next_pidmap(struct pid_namespace *pid_ns, int last);
-extern struct pid *alloc_pid(struct pid_namespace *ns);
+extern struct pid *alloc_pid(struct pid_namespace *ns, int *retval);
extern void free_pid(struct pid *pid);
/*
Index: linux-2.6.25-rc3-mm1/kernel/fork.c
===================================================================
--- linux-2.6.25-rc3-mm1.orig/kernel/fork.c 2008-03-28 08:18:53.000000000 +0100
+++ linux-2.6.25-rc3-mm1/kernel/fork.c 2008-03-28 08:21:19.000000000 +0100
@@ -1199,8 +1199,7 @@ static struct task_struct *copy_process(
goto bad_fork_cleanup_io;
if (pid != &init_struct_pid) {
- retval = -ENOMEM;
- pid = alloc_pid(task_active_pid_ns(p));
+ pid = alloc_pid(task_active_pid_ns(p), &retval);
if (!pid)
goto bad_fork_cleanup_io;
Index: linux-2.6.25-rc3-mm1/kernel/pid.c
===================================================================
--- linux-2.6.25-rc3-mm1.orig/kernel/pid.c 2008-03-28 08:18:53.000000000 +0100
+++ linux-2.6.25-rc3-mm1/kernel/pid.c 2008-03-28 08:21:19.000000000 +0100
@@ -122,6 +122,26 @@ static void free_pidmap(struct upid *upi
atomic_inc(&map->nr_free);
}
+static inline int alloc_pidmap_page(struct pidmap *map)
+{
+ if (unlikely(!map->page)) {
+ void *page = kzalloc(PAGE_SIZE, GFP_KERNEL);
+ /*
+ * Free the page if someone raced with us
+ * installing it:
+ */
+ spin_lock_irq(&pidmap_lock);
+ if (map->page)
+ kfree(page);
+ else
+ map->page = page;
+ spin_unlock_irq(&pidmap_lock);
+ if (unlikely(!map->page))
+ return -1;
+ }
+ return 0;
+}
+
static int alloc_pidmap(struct pid_namespace *pid_ns)
{
int i, offset, max_scan, pid, last = pid_ns->last_pid;
@@ -134,21 +154,8 @@ static int alloc_pidmap(struct pid_names
map = &pid_ns->pidmap[pid/BITS_PER_PAGE];
max_scan = (pid_max + BITS_PER_PAGE - 1)/BITS_PER_PAGE - !offset;
for (i = 0; i <= max_scan; ++i) {
- if (unlikely(!map->page)) {
- void *page = kzalloc(PAGE_SIZE, GFP_KERNEL);
- /*
- * Free the page if someone raced with us
- * installing it:
- */
- spin_lock_irq(&pidmap_lock);
- if (map->page)
- kfree(page);
- else
- map->page = page;
- spin_unlock_irq(&pidmap_lock);
- if (unlikely(!map->page))
- break;
- }
+ if (unlikely(alloc_pidmap_page(map)))
+ break;
if (likely(atomic_read(&map->nr_free))) {
do {
if (!test_and_set_bit(offset, map->page)) {
@@ -182,6 +189,35 @@ static int alloc_pidmap(struct pid_names
return -1;
}
+/*
+ * Return a predefined pid value if successful (ID_AT(pid_l, level)),
+ * -errno else
+ */
+static int alloc_fixed_pidmap(struct pid_namespace *pid_ns,
+ struct sys_id *pid_l, int level)
+{
+ int offset, pid;
+ struct pidmap *map;
+
+ pid = ID_AT(pid_l, level);
+ if (pid < RESERVED_PIDS || pid >= pid_max)
+ return -EINVAL;
+
+ map = &pid_ns->pidmap[pid / BITS_PER_PAGE];
+
+ if (unlikely(alloc_pidmap_page(map)))
+ return -ENOMEM;
+
+ offset = pid & BITS_PER_PAGE_MASK;
+ if (test_and_set_bit(offset, map->page))
+ return -EBUSY;
+
+ atomic_dec(&map->nr_free);
+ pid_ns->last_pid = max(pid_ns->last_pid, pid);
+
+ return pid;
+}
+
int next_pidmap(struct pid_namespace *pid_ns, int last)
{
int offset;
@@ -243,20 +279,91 @@ void free_pid(struct pid *pid)
call_rcu(&pid->rcu, delayed_put_pid);
}
-struct pid *alloc_pid(struct pid_namespace *ns)
+/*
+ * Called by alloc_pid() to use a list of predefined ids for the calling
+ * process' upper ns levels.
+ * Returns next pid ns to visit if successful (may be NULL if walked through
+ * the entire pid ns hierarchy).
+ * i is filled with next level to be visited (useful for the error cases).
+ */
+static struct pid_namespace *set_predefined_pids(struct pid_namespace *ns,
+ struct pid *pid,
+ struct sys_id *pid_l,
+ int *next_level)
+{
+ struct pid_namespace *tmp;
+ int rel_level, i, nr;
+
+ rel_level = pid_l->nids - 1;
+ if (rel_level > ns->level)
+ return ERR_PTR(-EINVAL);
+
+ tmp = ns;
+
+ /*
+ * Use the predefined upid nrs for levels ns->level down to
+ * ns->level - rel_level
+ */
+ for (i = ns->level ; rel_level >= 0; i--, rel_level--) {
+ nr = alloc_fixed_pidmap(tmp, pid_l, rel_level);
+ if (nr < 0) {
+ tmp = ERR_PTR(nr);
+ goto out;
+ }
+
+ pid->numbers[i].nr = nr;
+ pid->numbers[i].ns = tmp;
+ tmp = tmp->parent;
+ }
+
+ id_blocks_free(pid_l);
+
+out:
+ *next_level = i;
+ return tmp;
+}
+
+struct pid *alloc_pid(struct pid_namespace *ns, int *retval)
{
struct pid *pid;
enum pid_type type;
int i, nr;
struct pid_namespace *tmp;
struct upid *upid;
+ struct sys_id *pid_l;
+ *retval = -ENOMEM;
pid = kmem_cache_alloc(ns->pid_cachep, GFP_KERNEL);
if (!pid)
goto out;
tmp = ns;
- for (i = ns->level; i >= 0; i--) {
+ i = ns->level;
+
+ /*
+ * If there is a list of upid nrs specified, use it instead of letting
+ * the kernel chose the upid nrs for us.
+ */
+ pid_l = current->next_id;
+ if (pid_l && pid_l->nids) {
+ /*
+ * returns the next ns to be visited in the following loop
+ * (or NULL if we are done).
+ * i is filled in with the next level to be visited. We need
+ * it to undo things in the error cases.
+ */
+ tmp = set_predefined_pids(ns, pid, pid_l, &i);
+ if (IS_ERR(tmp)) {
+ *retval = PTR_ERR(tmp);
+ goto out_free;
+ }
+ }
+
+ *retval = -ENOMEM;
+ /*
+ * Let the lower levels upid nrs be automatically allocated
+ */
+ for ( ; i >= 0; i--) {
nr = alloc_pidmap(tmp);
if (nr < 0)
goto out_free;
--
next prev parent reply other threads:[~2008-03-28 9:53 UTC|newest]
Thread overview: 26+ messages / expand[flat|nested] mbox.gz Atom feed top
2008-03-28 9:53 [RFC][PATCH 0/4] Object creation with a pre-defined id (v2) Nadia.Derbey-6ktuUTfB/bM
2008-03-28 9:53 ` [RFC][PATCH 1/4] Provide a new procfs interface to set next id Nadia.Derbey-6ktuUTfB/bM
[not found] ` <20080328095546.729576000-6ktuUTfB/bM@public.gmane.org>
2008-03-28 19:12 ` Oren Laadan
2008-03-28 9:53 ` [RFC][PATCH 2/4] Provide a new procfs interface to set next upid nr(s) Nadia.Derbey-6ktuUTfB/bM
2008-03-28 9:53 ` [RFC][PATCH 3/4] IPC: use the target ID specified in procfs Nadia.Derbey-6ktuUTfB/bM
2008-03-28 9:53 ` Nadia.Derbey-6ktuUTfB/bM [this message]
[not found] ` <20080328095309.117134000-6ktuUTfB/bM@public.gmane.org>
2008-03-29 0:09 ` [RFC][PATCH 0/4] Object creation with a pre-defined id (v2) Oren Laadan
[not found] ` <Pine.LNX.4.64.0803282001280.1670-qCy6tK2KoXYxx4h7iLwZUsysmGwsrwg7TsgBfEgxSsDMrJhsLK8IO4dd74u8MsAO@public.gmane.org>
2008-04-02 15:56 ` Serge E. Hallyn
2008-03-29 0:13 ` Oren Laadan
-- strict thread matches above, loose matches on Subject: below --
2008-04-04 14:51 [RFC][PATCH 0/4] Object creation with a specified id Nadia.Derbey
2008-04-04 14:51 ` [RFC][PATCH 4/4] PID: use the target ID specified in procfs Nadia.Derbey
2008-04-04 14:51 ` Nadia.Derbey-6ktuUTfB/bM
2008-03-10 13:50 [RFC][PATCH 0/4] Object creation with a specified id Nadia.Derbey-6ktuUTfB/bM
2008-03-10 13:50 ` [RFC][PATCH 4/4] PID: use the target ID specified in procfs Nadia.Derbey-6ktuUTfB/bM
[not found] ` <20080310135209.769712000-6ktuUTfB/bM@public.gmane.org>
2008-03-11 12:04 ` Pavel Emelyanov
[not found] ` <47D67557.7080506-GEFAQzZX7r8dnm+yROfE0A@public.gmane.org>
2008-03-11 15:28 ` Nadia Derbey
[not found] ` <47D6A52D.6030701-6ktuUTfB/bM@public.gmane.org>
2008-03-11 15:37 ` Pavel Emelyanov
[not found] ` <47D6A741.9080708-GEFAQzZX7r8dnm+yROfE0A@public.gmane.org>
2008-03-11 15:55 ` Nadia Derbey
2008-03-11 16:47 ` Serge E. Hallyn
[not found] ` <20080311164725.GA12918-6s5zFf/epYLPQpwDFJZrxKsjOiXwFzmk@public.gmane.org>
2008-03-11 16:55 ` Pavel Emelyanov
[not found] ` <47D6B990.4080400-GEFAQzZX7r8dnm+yROfE0A@public.gmane.org>
2008-03-11 17:53 ` Serge E. Hallyn
[not found] ` <20080311175328.GA14171-6s5zFf/epYLPQpwDFJZrxKsjOiXwFzmk@public.gmane.org>
2008-03-12 19:58 ` Eric W. Biederman
[not found] ` <m1zlt3yarj.fsf-T1Yj925okcoyDheHMi7gv2pdwda3JcWeAL8bYrjMMd8@public.gmane.org>
2008-03-13 10:41 ` Nadia Derbey
[not found] ` <47D904E4.4000208-6ktuUTfB/bM@public.gmane.org>
2008-03-13 17:40 ` Eric W. Biederman
[not found] ` <m1r6eewmj2.fsf-T1Yj925okcoyDheHMi7gv2pdwda3JcWeAL8bYrjMMd8@public.gmane.org>
2008-03-13 19:06 ` Serge E. Hallyn
2008-03-13 20:01 ` Oren Laadan
[not found] ` <47D987FE.9040909-eQaUEPhvms7ENvBUuze7eA@public.gmane.org>
2008-03-13 23:12 ` Eric W. Biederman
[not found] ` <m163vqw74n.fsf-T1Yj925okcoyDheHMi7gv2pdwda3JcWeAL8bYrjMMd8@public.gmane.org>
2008-03-13 23:24 ` Oren Laadan
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=20080328095547.794858000@bull.net \
--to=nadia.derbey-6ktuutfb/bm@public.gmane.org \
--cc=containers-cunTk1MwBs9QetFLy7KEm3xJsTq8ys+cHZ5vskTnxNA@public.gmane.org \
--cc=xemul-GEFAQzZX7r8dnm+yROfE0A@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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.