linux-fsdevel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
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 4/5] cpu/cpuns: Make sysfs CPU namespace aware
Date: Sat,  9 Oct 2021 20:42:42 +0530	[thread overview]
Message-ID: <20211009151243.8825-5-psampat@linux.ibm.com> (raw)
In-Reply-To: <20211009151243.8825-1-psampat@linux.ibm.com>

The commit adds support to sysfs files like online, offline, present to
be CPU namespace context aware. It presents virtualized CPU information
which is coherent to the cgroup cpuset restrictions that are set upon
the tasks.

Signed-off-by: Pratik R. Sampat <psampat@linux.ibm.com>
---
 drivers/base/cpu.c | 35 ++++++++++++++++++++++++++++++++++-
 1 file changed, 34 insertions(+), 1 deletion(-)

diff --git a/drivers/base/cpu.c b/drivers/base/cpu.c
index 5ef14db97904..1487b23e5472 100644
--- a/drivers/base/cpu.c
+++ b/drivers/base/cpu.c
@@ -20,6 +20,7 @@
 #include <linux/tick.h>
 #include <linux/pm_qos.h>
 #include <linux/sched/isolation.h>
+#include <linux/cpu_namespace.h>
 
 #include "base.h"
 
@@ -203,6 +204,24 @@ struct cpu_attr {
 	const struct cpumask *const map;
 };
 
+#ifdef CONFIG_CPU_NS
+static ssize_t show_cpuns_cpus_attr(struct device *dev,
+			      struct device_attribute *attr,
+			      char *buf)
+{
+	struct cpu_attr *ca = container_of(attr, struct cpu_attr, attr);
+
+	if (current->nsproxy->cpu_ns == &init_cpu_ns)
+		return cpumap_print_to_pagebuf(true, buf, ca->map);
+
+	return cpumap_print_to_pagebuf(true, buf,
+				       &current->nsproxy->cpu_ns->v_cpuset_cpus);
+}
+
+#define _CPU_CPUNS_ATTR(name, map) \
+	{ __ATTR(name, 0444, show_cpuns_cpus_attr, NULL), map }
+#endif
+
 static ssize_t show_cpus_attr(struct device *dev,
 			      struct device_attribute *attr,
 			      char *buf)
@@ -217,9 +236,14 @@ static ssize_t show_cpus_attr(struct device *dev,
 
 /* Keep in sync with cpu_subsys_attrs */
 static struct cpu_attr cpu_attrs[] = {
+#ifdef CONFIG_CPU_NS
+	_CPU_CPUNS_ATTR(online, &__cpu_online_mask),
+	_CPU_CPUNS_ATTR(present, &__cpu_present_mask),
+#else
 	_CPU_ATTR(online, &__cpu_online_mask),
-	_CPU_ATTR(possible, &__cpu_possible_mask),
 	_CPU_ATTR(present, &__cpu_present_mask),
+#endif
+	_CPU_ATTR(possible, &__cpu_possible_mask),
 };
 
 /*
@@ -244,7 +268,16 @@ static ssize_t print_cpus_offline(struct device *dev,
 	/* display offline cpus < nr_cpu_ids */
 	if (!alloc_cpumask_var(&offline, GFP_KERNEL))
 		return -ENOMEM;
+#ifdef CONFIG_CPU_NS
+	if (current->nsproxy->cpu_ns == &init_cpu_ns) {
+		cpumask_andnot(offline, cpu_possible_mask, cpu_online_mask);
+	} else {
+		cpumask_andnot(offline, cpu_possible_mask,
+			       &current->nsproxy->cpu_ns->v_cpuset_cpus);
+	}
+#else
 	cpumask_andnot(offline, cpu_possible_mask, cpu_online_mask);
+#endif
 	len += sysfs_emit_at(buf, len, "%*pbl", cpumask_pr_args(offline));
 	free_cpumask_var(offline);
 
-- 
2.31.1


  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 ` [RFC 2/5] ns: Add scrambling functionality to CPU namespace Pratik R. Sampat
2021-10-09 15:12 ` [RFC 3/5] cpuset/cpuns: Make cgroup CPUset CPU namespace aware Pratik R. Sampat
2021-10-09 15:12 ` Pratik R. Sampat [this message]
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-5-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).