From: "Pratik R. Sampat" <psampat@linux.ibm.com>
To: bristot@redhat.com, christian@brauner.io, ebiederm@xmission.com,
lizefan.x@bytedance.com, tj@kernel.org, hannes@cmpxchg.org,
mingo@kernel.org, juri.lelli@redhat.com,
linux-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org,
cgroups@vger.kernel.org, containers@lists.linux.dev,
containers@lists.linux-foundation.org, psampat@linux.ibm.com,
pratik.r.sampat@gmail.com
Subject: [RFC 2/5] ns: Add scrambling functionality to CPU namespace
Date: Sat, 9 Oct 2021 20:42:40 +0530 [thread overview]
Message-ID: <20211009151243.8825-3-psampat@linux.ibm.com> (raw)
In-Reply-To: <20211009151243.8825-1-psampat@linux.ibm.com>
The commit adds functionality to map every host CPU to a virtualized CPU
in the namespace.
Every CPU namespace apart from the init namespace has its CPU map
scrambled. The CPUs are mapped in a flat hierarchy. This means that
regardless of the parent-child hierarchy of the namespace, each
translation will map directly from the virtual namespace CPU to
physical CPU without the need of traversal along the hierarchy.
Signed-off-by: Pratik R. Sampat <psampat@linux.ibm.com>
---
kernel/cpu_namespace.c | 49 ++++++++++++++++++++++++++++++++++++++----
1 file changed, 45 insertions(+), 4 deletions(-)
diff --git a/kernel/cpu_namespace.c b/kernel/cpu_namespace.c
index 6c700522352a..7b8b28f3d0e7 100644
--- a/kernel/cpu_namespace.c
+++ b/kernel/cpu_namespace.c
@@ -27,6 +27,33 @@ static void destroy_cpu_namespace(struct cpu_namespace *ns)
put_user_ns(ns->user_ns);
}
+/*
+ * Shuffle
+ * Arrange the N elements of ARRAY in random order.
+ * Only effective if N is much smaller than RAND_MAX;
+ * if this may not be the case, use a better random
+ * number generator. -- Ben Pfaff.
+ */
+#define RAND_MAX 32767
+void shuffle(int *array, size_t n)
+{
+ unsigned int rnd_num;
+ int i, j, t;
+
+ if (n <= 1)
+ return;
+
+ for (i = 0; i < n-1; i++) {
+ get_random_bytes(&rnd_num, sizeof(rnd_num));
+ rnd_num = rnd_num % RAND_MAX;
+
+ j = i + rnd_num / (RAND_MAX / (n - i) + 1);
+ t = array[j];
+ array[j] = array[i];
+ array[i] = t;
+ }
+}
+
static struct ucounts *inc_cpu_namespaces(struct user_namespace *ns)
{
return inc_ucount(ns, current_euid(), UCOUNT_CPU_NAMESPACES);
@@ -37,8 +64,9 @@ static struct cpu_namespace *create_cpu_namespace(struct user_namespace *user_ns
{
struct cpu_namespace *ns;
struct ucounts *ucounts;
- int err, i, cpu;
+ int err, i, cpu, n = 0;
cpumask_t temp;
+ int *cpu_arr;
err = -EINVAL;
if (!in_userns(parent_cpu_ns->user_ns, user_ns))
@@ -62,10 +90,21 @@ static struct cpu_namespace *create_cpu_namespace(struct user_namespace *user_ns
ns->parent = get_cpu_ns(parent_cpu_ns);
ns->user_ns = get_user_ns(user_ns);
- for_each_present_cpu(cpu) {
- ns->p_v_trans_map[cpu] = ns->parent->p_v_trans_map[cpu];
- ns->v_p_trans_map[cpu] = ns->parent->v_p_trans_map[cpu];
+ cpu_arr = kmalloc_array(num_possible_cpus(), sizeof(int), GFP_KERNEL);
+ if (!cpu_arr)
+ goto out_free_ns;
+
+ for_each_possible_cpu(cpu) {
+ cpu_arr[n++] = cpu;
+ }
+
+ shuffle(cpu_arr, n);
+
+ for (i = 0; i < n; i++) {
+ ns->p_v_trans_map[i] = cpu_arr[i];
+ ns->v_p_trans_map[cpu_arr[i]] = i;
}
+
cpumask_clear(&temp);
cpumask_clear(&ns->v_cpuset_cpus);
@@ -80,6 +119,8 @@ static struct cpu_namespace *create_cpu_namespace(struct user_namespace *user_ns
set_cpus_allowed_ptr(current, &temp);
+ kfree(cpu_arr);
+
return ns;
out_free_ns:
--
2.31.1
next prev parent reply other threads:[~2021-10-09 15:13 UTC|newest]
Thread overview: 20+ messages / expand[flat|nested] mbox.gz Atom feed top
2021-10-09 15:12 [RFC 0/5] kernel: Introduce CPU Namespace Pratik R. Sampat
2021-10-09 15:12 ` [RFC 1/5] ns: " Pratik R. Sampat
2021-10-09 22:37 ` Peter Zijlstra
2021-10-09 15:12 ` Pratik R. Sampat [this message]
2021-10-09 15:12 ` [RFC 3/5] cpuset/cpuns: Make cgroup CPUset CPU namespace aware Pratik R. Sampat
2021-10-09 15:12 ` [RFC 4/5] cpu/cpuns: Make sysfs " Pratik R. Sampat
2021-10-09 15:12 ` [RFC 5/5] proc/cpuns: Make procfs load stats " Pratik R. Sampat
2021-10-09 22:41 ` [RFC 0/5] kernel: Introduce CPU Namespace Peter Zijlstra
2021-10-11 10:11 ` Christian Brauner
2021-10-11 14:17 ` Michal Koutný
2021-10-11 17:42 ` Tejun Heo
2021-10-12 8:42 ` Pratik Sampat
2021-10-14 22:14 ` Tejun Heo
2021-10-18 15:29 ` Pratik Sampat
2021-10-18 16:29 ` Tejun Heo
2021-10-20 10:44 ` Pratik Sampat
2021-10-20 16:35 ` Tejun Heo
2021-10-21 7:44 ` Pratik Sampat
2021-10-21 17:06 ` Tejun Heo
2021-10-21 17:15 ` Eric W. Biederman
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=20211009151243.8825-3-psampat@linux.ibm.com \
--to=psampat@linux.ibm.com \
--cc=bristot@redhat.com \
--cc=cgroups@vger.kernel.org \
--cc=christian@brauner.io \
--cc=containers@lists.linux-foundation.org \
--cc=containers@lists.linux.dev \
--cc=ebiederm@xmission.com \
--cc=hannes@cmpxchg.org \
--cc=juri.lelli@redhat.com \
--cc=linux-fsdevel@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=lizefan.x@bytedance.com \
--cc=mingo@kernel.org \
--cc=pratik.r.sampat@gmail.com \
--cc=tj@kernel.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;
as well as URLs for NNTP newsgroup(s).