Skip to contentSkip to conversation

Adds PUI: process unic identifier

RFC In review [email protected] 2 participants 3 messages Jan 10, 2017 – Jan 13, 2017

Subsystems (5)
lore
Share & tools
lore
#0 lore raw

RFC 1/1 Adds PUI: process unic identifier

From: José Bollo <[email protected]>

Hi all,

I'd like to get your feeling about the idea exposed in that draft. Should continue or stop immediately? Is there already some existing work? How is the taken approach?

BR - José Bollo

[RFC DRAFT] Adds PUI: process unic identifier

The name 'pui' is choosen -instead of puid- to avoid confusion with either pid and uid. It is intended to identify uniquely each activated task item, accordling to namespaces.

64 bits seems to be a good deal for beginning. The count of second in a year is less than 2^25. So if a huge machine is able to create 2^31 processes per second (ex: 2^7 cores, each creating 2^24 processes -a nighmare-), then the unic id is over in 2^8 years. Far more than what a regular system upgrade needs.

The pui is represented as a hexadecimal value because it is much more efficient.

---
 fs/proc/array.c                   |  21 +++++
 include/linux/pid.h               |   5 ++
 include/linux/pid_namespace.h     |   4 +
 include/linux/pui.h               |  55 +++++++++++++
 include/uapi/asm-generic/socket.h |   2 +
 init/Kconfig                      |   7 ++
 kernel/Makefile                   |   1 +
 kernel/pid.c                      |  12 +++
 kernel/pid_namespace.c            |   3 +
 kernel/pui.c                      | 168 ++++++++++++++++++++++++++++++++++++++
 net/core/sock.c                   |  20 +++++
 11 files changed, 298 insertions(+)
 create mode 100644 include/linux/pui.h
 create mode 100644 kernel/pui.c
fs/proc/array.c +21 -0
index 51a4213..6350a1e 100644
@@ -153,6 +153,16 @@ static inline int get_task_umask(struct task_struct *tsk)
 	return umask;
 }
 
+#ifdef CONFIG_PUI
+static inline void put_pui(struct seq_file *m, pui_t pui)
+{
+	pui_str_t str;
+
+	pui_to_str(pui, str);
+	seq_puts(m, str);
+}
+#endif
+
 static inline void task_state(struct seq_file *m, struct pid_namespace *ns,
 				struct pid *pid, struct task_struct *p)
 {
@@ -226,6 +236,17 @@ static inline void task_state(struct seq_file *m, struct pid_namespace *ns,
 	for (g = ns->level; g <= pid->level; g++)
 		seq_put_decimal_ull(m, "\t", task_session_nr_ns(p, pid->numbers[g].ns));
 #endif
+#ifdef CONFIG_PUI
+	seq_puts(m, "\nPui:");
+	put_pui(m, pid->numbers[ns->level].pui);
+#ifdef CONFIG_PID_NS
+	seq_puts(m, "\nNSpui:");
+	for (g = ns->level; g <= pid->level; g++) {
+		seq_putc(m, '\t');
+		put_pui(m, pid->numbers[g].pui);
+	}
+#endif
+#endif
 	seq_putc(m, '\n');
 }
 
include/linux/pid.h +5 -0
index 23705a5..9bb131c 100644
@@ -2,6 +2,7 @@
 #define _LINUX_PID_H
 
 #include <linux/rcupdate.h>
+#include <linux/pui.h>
 
 enum pid_type
 {
@@ -52,6 +53,10 @@ struct upid {
 	int nr;
 	struct pid_namespace *ns;
 	struct hlist_node pid_chain;
+#ifdef CONFIG_PUI
+	struct hlist_node pui_chain;
+	pui_t pui;
+#endif
 };
 
 struct pid
include/linux/pid_namespace.h +4 -0
index 34cce96..cd4844c 100644
@@ -9,6 +9,7 @@
 #include <linux/nsproxy.h>
 #include <linux/kref.h>
 #include <linux/ns_common.h>
+#include <linux/pui.h>
 
 struct pidmap {
        atomic_t nr_free;
@@ -26,6 +27,9 @@ struct pid_namespace {
 	struct pidmap pidmap[PIDMAP_ENTRIES];
 	struct rcu_head rcu;
 	int last_pid;
+#ifdef CONFIG_PUI
+	pui_gen_t pui_generator;
+#endif
 	unsigned int nr_hashed;
 	struct task_struct *child_reaper;
 	struct kmem_cache *pid_cachep;
include/linux/pui.h new file +55 -0

55 lines not on this page · raw message

include/uapi/asm-generic/socket.h +2 -0
index 2c748dd..143bcbe 100644
@@ -94,4 +94,6 @@
 
 #define SCM_TIMESTAMPING_OPT_STATS	54
 
+#define SO_PEERPUI		55
+
 #endif /* __ASM_GENERIC_SOCKET_H */
init/Kconfig +7 -0
index 223b734..ff4dde2 100644
@@ -306,6 +306,12 @@ config USELIB
 	  earlier, you may need to enable this syscall.  Current systems
 	  running glibc can safely disable this.
 
+config PUI
+	bool "Enables Process Unic Identifier"
+	default n
+	help
+	  Provides a 64 bits unic identifier to processes and threads
+
 config AUDIT
 	bool "Auditing support"
 	depends on NET
@@ -2158,3 +2164,4 @@ config ASN1
 	  functions to call on what tags.
 
 source "kernel/Kconfig.locks"
+
kernel/Makefile +1 -0
index 12c679f..ebd1cd8 100644
@@ -71,6 +71,7 @@ obj-$(CONFIG_CPUSETS) += cpuset.o
 obj-$(CONFIG_UTS_NS) += utsname.o
 obj-$(CONFIG_USER_NS) += user_namespace.o
 obj-$(CONFIG_PID_NS) += pid_namespace.o
+obj-$(CONFIG_PUI) += pui.o
 obj-$(CONFIG_IKCONFIG) += configs.o
 obj-$(CONFIG_SMP) += stop_machine.o
 obj-$(CONFIG_KPROBES_SANITY_TEST) += test_kprobes.o
kernel/pid.c +12 -0
index f66162f..24b76fe 100644
@@ -75,6 +75,9 @@ struct pid_namespace init_pid_ns = {
 		[ 0 ... PIDMAP_ENTRIES-1] = { ATOMIC_INIT(BITS_PER_PAGE), NULL }
 	},
 	.last_pid = 0,
+#ifdef CONFIG_PUI
+	.pui_generator = PUI_GEN_INIT,
+#endif
 	.nr_hashed = PIDNS_HASH_ADDING,
 	.level = 0,
 	.child_reaper = &init_task,
@@ -267,6 +270,9 @@ void free_pid(struct pid *pid)
 		struct upid *upid = pid->numbers + i;
 		struct pid_namespace *ns = upid->ns;
 		hlist_del_rcu(&upid->pid_chain);
+#ifdef CONFIG_PUI
+		pui_del(upid);
+#endif
 		switch(--ns->nr_hashed) {
 		case 2:
 		case 1:
@@ -318,6 +324,9 @@ struct pid *alloc_pid(struct pid_namespace *ns)
 
 		pid->numbers[i].nr = nr;
 		pid->numbers[i].ns = tmp;
+#ifdef CONFIG_PUI
+		pui_make(&pid->numbers[i]);
+#endif
 		tmp = tmp->parent;
 	}
 
@@ -338,6 +347,9 @@ struct pid *alloc_pid(struct pid_namespace *ns)
 	for ( ; upid >= pid->numbers; --upid) {
 		hlist_add_head_rcu(&upid->pid_chain,
 				&pid_hash[pid_hashfn(upid->nr, upid->ns)]);
+#ifdef CONFIG_PUI
+		pui_add(upid);
+#endif
 		upid->ns->nr_hashed++;
 	}
 	spin_unlock_irq(&pidmap_lock);
kernel/pid_namespace.c +3 -0
index df9e8e9..ed17097 100644
@@ -130,6 +130,9 @@ static struct pid_namespace *create_pid_namespace(struct user_namespace *user_ns
 	ns->ucounts = ucounts;
 	ns->nr_hashed = PIDNS_HASH_ADDING;
 	INIT_WORK(&ns->proc_work, proc_cleanup_work);
+#ifdef CONFIG_PUI
+	pui_init_generator(&ns->pui_generator);
+#endif
 
 	set_bit(0, ns->pidmap[0].page);
 	atomic_set(&ns->pidmap[0].nr_free, BITS_PER_PAGE - 1);
kernel/pui.c new file +168 -0

168 lines not on this page · raw message

net/core/sock.c +20 -0
index f560e08..65f5dbb 100644
@@ -1271,6 +1271,26 @@ int sock_getsockopt(struct socket *sock, int level, int optname,
 		v.val = sk->sk_incoming_cpu;
 		break;
 
+#ifdef CONFIG_PUI
+	case SO_PEERPUI:
+	{
+		pui_str_t str;
+		pui_t pui;
+		int l;
+
+		pui = pui_vnr(sk->sk_peer_pid);
+		if (pui == PUI_INVALID)
+			return -ENOTCONN;
+		l = pui_to_str(pui, str);
+		if (len <= l)
+			return -EINVAL;
+		len = l + 1; /* includes leading zero */
+		if (copy_to_user(optval, str, len))
+			return -EFAULT;
+		goto lenout;
+	}
+#endif
+
 	default:
 		/* We implement the SO_SNDLOWAT etc to not be settable
 		 * (1003.1g 7).
Signature · 1 line
2.9.3
#1 lore raw
Quoted 23 lines · Jose Bollo

Hi all,

I'd like to get your feeling about the idea exposed in that draft. Should continue or stop immediately? Is there already some existing work? How is the taken approach?

BR - Jose Bollo

[RFC DRAFT] Adds PUI: process unic identifier

The name 'pui' is choosen -instead of puid- to avoid confusion with either pid and uid. It is intended to identify uniquely each activated task item, accordling to namespaces.

64 bits seems to be a good deal for beginning. The count of second in a year is less than 2^25. So if a huge machine is able to create 2^31 processes per second (ex: 2^7 cores, each creating 2^24 processes -a nighmare-), then the unic id is over in 2^8 years. Far more than what a regular system upgrade needs.

Quoted 2 lines

The pui is represented as a hexadecimal value because it is much more efficient.

Firstly, please explain the motivation why you need process unique identifier that is really unique. This patch involves userspace visible changes but not everybody is aware of what your PTAGS wants to do.

Secondly, please describe how userspace processes use process unique identifier. This patch adds "Pui:" line and "NSpui:" line to /proc/$pid/status and SO_PEERPUI option to getsockopt(), but nothing else. Why these lines and option are needed and why they are sufficient?

There is start_time field ("struct task_struct"->real_start_time) in /proc/$pid/stat which is already almost unique. Even if userspace processes use both $pid and start_time for identifying a process, is it unreliable? If userspace processes use both "struct task_struct"->pid and "struct task_struct"->real_start_time for identifying a process, is it still unreliable? I wonder why find_pui_ns() needs to be aware of CONFIG_PID_NS. How userspace processes use process unique identifier for identifying a "struct task_struct"?

There are %Lu %Ld %Lx %LX kstrtoull() kstrtoll() for converting string to 64bit integer, and %llu %lld %llx %llX for converting 64bit integer to string. You don't need to redefine 64bit types using typedef keyword.

#2 lore raw
Quoted 45 lines · Tetsuo Handa
Quoted 23 lines · Jose Bollo

Hi all,

I'd like to get your feeling about the idea exposed in that draft. Should continue or stop immediately? Is there already some existing work? How is the taken approach?

BR - Jose Bollo

[RFC DRAFT] Adds PUI: process unic identifier

The name 'pui' is choosen -instead of puid- to avoid confusion with either pid and uid. It is intended to identify uniquely each activated task item, accordling to namespaces.

64 bits seems to be a good deal for beginning. The count of second in a year is less than 2^25. So if a huge machine is able to create 2^31 processes per second (ex: 2^7 cores, each creating 2^24 processes -a nighmare-), then the unic id is over in 2^8 years. Far more than what a regular system upgrade needs.

Quoted 2 lines

The pui is represented as a hexadecimal value because it is much more efficient.

Firstly, please explain the motivation why you need process unique identifier that is really unique. This patch involves userspace visible changes but not everybody is aware of what your PTAGS wants to do.

Secondly, please describe how userspace processes use process unique identifier. This patch adds "Pui:" line and "NSpui:" line to /proc/$pid/status and SO_PEERPUI option to getsockopt(), but nothing else. Why these lines and option are needed and why they are sufficient?

There is start_time field ("struct task_struct"->real_start_time) in /proc/$pid/stat which is already almost unique. Even if userspace processes use both $pid and start_time for identifying a process, is it unreliable? If userspace processes use both "struct task_struct"->pid and "struct task_struct"->real_start_time for identifying a process, is it still unreliable? I wonder why find_pui_ns() needs to be aware of CONFIG_PID_NS. How userspace processes use process unique identifier for identifying a "struct task_struct"?

Quoted 4 lines

There are %Lu %Ld %Lx %LX kstrtoull() kstrtoll() for converting string to 64bit integer, and %llu %lld %llx %llX for converting 64bit integer to string. You don't need to redefine 64bit types using typedef keyword.

Hi Tetsuo-san,

Thank you for your review. It helps a lot!

After reading about the possible use of real_start_time, I was wondering if using a kind of clock+pid(ns) would be able to replace at lower resource cost what I proposed. That leads to use of 128 bits puids but without changing anything in {pid,pid_namespace}.[ch]. Great! But I still have to check.

I wanted to get advices first so I didn't sent the fs part of pui because it is not still existing. This fs part will allow access to task by their pui.

I'll probably send an update in a month or more.

Best regards
José Bollo