Skip to content

Commit 4e1a7df

Browse files
James Morsectmarinas
authored andcommitted
cpumask: Add enabled cpumask for present CPUs that can be brought online
The 'offline' file in sysfs shows all offline CPUs, including those that aren't present. User-space is expected to remove not-present CPUs from this list to learn which CPUs could be brought online. CPUs can be present but not-enabled. These CPUs can't be brought online until the firmware policy changes, which comes with an ACPI notification that will register the CPUs. With only the offline and present files, user-space is unable to determine which CPUs it can try to bring online. Add a new CPU mask that shows this based on all the registered CPUs. Signed-off-by: James Morse <[email protected]> Tested-by: Miguel Luis <[email protected]> Tested-by: Vishnu Pajjuri <[email protected]> Tested-by: Jianyong Wu <[email protected]> Acked-by: Thomas Gleixner <[email protected]> Signed-off-by: Russell King (Oracle) <[email protected]> Reviewed-by: Gavin Shan <[email protected]> Signed-off-by: Jonathan Cameron <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Catalin Marinas <[email protected]>
1 parent 828ce92 commit 4e1a7df

File tree

4 files changed

+44
-0
lines changed

4 files changed

+44
-0
lines changed

Documentation/ABI/testing/sysfs-devices-system-cpu

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -694,3 +694,9 @@ Description:
694694
(RO) indicates whether or not the kernel directly supports
695695
modifying the crash elfcorehdr for CPU hot un/plug and/or
696696
on/offline changes.
697+
698+
What: /sys/devices/system/cpu/enabled
699+
Date: Nov 2022
700+
Contact: Linux kernel mailing list <[email protected]>
701+
Description:
702+
(RO) the list of CPUs that can be brought online.

drivers/base/cpu.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ void unregister_cpu(struct cpu *cpu)
9595
{
9696
int logical_cpu = cpu->dev.id;
9797

98+
set_cpu_enabled(logical_cpu, false);
9899
unregister_cpu_under_node(logical_cpu, cpu_to_node(logical_cpu));
99100

100101
device_unregister(&cpu->dev);
@@ -273,6 +274,13 @@ static ssize_t print_cpus_offline(struct device *dev,
273274
}
274275
static DEVICE_ATTR(offline, 0444, print_cpus_offline, NULL);
275276

277+
static ssize_t print_cpus_enabled(struct device *dev,
278+
struct device_attribute *attr, char *buf)
279+
{
280+
return sysfs_emit(buf, "%*pbl\n", cpumask_pr_args(cpu_enabled_mask));
281+
}
282+
static DEVICE_ATTR(enabled, 0444, print_cpus_enabled, NULL);
283+
276284
static ssize_t print_cpus_isolated(struct device *dev,
277285
struct device_attribute *attr, char *buf)
278286
{
@@ -413,6 +421,7 @@ int register_cpu(struct cpu *cpu, int num)
413421
register_cpu_under_node(num, cpu_to_node(num));
414422
dev_pm_qos_expose_latency_limit(&cpu->dev,
415423
PM_QOS_RESUME_LATENCY_NO_CONSTRAINT);
424+
set_cpu_enabled(num, true);
416425

417426
return 0;
418427
}
@@ -494,6 +503,7 @@ static struct attribute *cpu_root_attrs[] = {
494503
&cpu_attrs[2].attr.attr,
495504
&dev_attr_kernel_max.attr,
496505
&dev_attr_offline.attr,
506+
&dev_attr_enabled.attr,
497507
&dev_attr_isolated.attr,
498508
#ifdef CONFIG_NO_HZ_FULL
499509
&dev_attr_nohz_full.attr,

include/linux/cpumask.h

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ static inline void set_nr_cpu_ids(unsigned int nr)
9393
*
9494
* cpu_possible_mask- has bit 'cpu' set iff cpu is populatable
9595
* cpu_present_mask - has bit 'cpu' set iff cpu is populated
96+
* cpu_enabled_mask - has bit 'cpu' set iff cpu can be brought online
9697
* cpu_online_mask - has bit 'cpu' set iff cpu available to scheduler
9798
* cpu_active_mask - has bit 'cpu' set iff cpu available to migration
9899
*
@@ -125,11 +126,13 @@ static inline void set_nr_cpu_ids(unsigned int nr)
125126

126127
extern struct cpumask __cpu_possible_mask;
127128
extern struct cpumask __cpu_online_mask;
129+
extern struct cpumask __cpu_enabled_mask;
128130
extern struct cpumask __cpu_present_mask;
129131
extern struct cpumask __cpu_active_mask;
130132
extern struct cpumask __cpu_dying_mask;
131133
#define cpu_possible_mask ((const struct cpumask *)&__cpu_possible_mask)
132134
#define cpu_online_mask ((const struct cpumask *)&__cpu_online_mask)
135+
#define cpu_enabled_mask ((const struct cpumask *)&__cpu_enabled_mask)
133136
#define cpu_present_mask ((const struct cpumask *)&__cpu_present_mask)
134137
#define cpu_active_mask ((const struct cpumask *)&__cpu_active_mask)
135138
#define cpu_dying_mask ((const struct cpumask *)&__cpu_dying_mask)
@@ -1075,6 +1078,7 @@ extern const DECLARE_BITMAP(cpu_all_bits, NR_CPUS);
10751078
#else
10761079
#define for_each_possible_cpu(cpu) for_each_cpu((cpu), cpu_possible_mask)
10771080
#define for_each_online_cpu(cpu) for_each_cpu((cpu), cpu_online_mask)
1081+
#define for_each_enabled_cpu(cpu) for_each_cpu((cpu), cpu_enabled_mask)
10781082
#define for_each_present_cpu(cpu) for_each_cpu((cpu), cpu_present_mask)
10791083
#endif
10801084

@@ -1092,6 +1096,15 @@ set_cpu_possible(unsigned int cpu, bool possible)
10921096
cpumask_clear_cpu(cpu, &__cpu_possible_mask);
10931097
}
10941098

1099+
static inline void
1100+
set_cpu_enabled(unsigned int cpu, bool can_be_onlined)
1101+
{
1102+
if (can_be_onlined)
1103+
cpumask_set_cpu(cpu, &__cpu_enabled_mask);
1104+
else
1105+
cpumask_clear_cpu(cpu, &__cpu_enabled_mask);
1106+
}
1107+
10951108
static inline void
10961109
set_cpu_present(unsigned int cpu, bool present)
10971110
{
@@ -1173,6 +1186,7 @@ static __always_inline unsigned int num_online_cpus(void)
11731186
return raw_atomic_read(&__num_online_cpus);
11741187
}
11751188
#define num_possible_cpus() cpumask_weight(cpu_possible_mask)
1189+
#define num_enabled_cpus() cpumask_weight(cpu_enabled_mask)
11761190
#define num_present_cpus() cpumask_weight(cpu_present_mask)
11771191
#define num_active_cpus() cpumask_weight(cpu_active_mask)
11781192

@@ -1181,6 +1195,11 @@ static inline bool cpu_online(unsigned int cpu)
11811195
return cpumask_test_cpu(cpu, cpu_online_mask);
11821196
}
11831197

1198+
static inline bool cpu_enabled(unsigned int cpu)
1199+
{
1200+
return cpumask_test_cpu(cpu, cpu_enabled_mask);
1201+
}
1202+
11841203
static inline bool cpu_possible(unsigned int cpu)
11851204
{
11861205
return cpumask_test_cpu(cpu, cpu_possible_mask);
@@ -1205,6 +1224,7 @@ static inline bool cpu_dying(unsigned int cpu)
12051224

12061225
#define num_online_cpus() 1U
12071226
#define num_possible_cpus() 1U
1227+
#define num_enabled_cpus() 1U
12081228
#define num_present_cpus() 1U
12091229
#define num_active_cpus() 1U
12101230

@@ -1218,6 +1238,11 @@ static inline bool cpu_possible(unsigned int cpu)
12181238
return cpu == 0;
12191239
}
12201240

1241+
static inline bool cpu_enabled(unsigned int cpu)
1242+
{
1243+
return cpu == 0;
1244+
}
1245+
12211246
static inline bool cpu_present(unsigned int cpu)
12221247
{
12231248
return cpu == 0;

kernel/cpu.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3069,6 +3069,9 @@ EXPORT_SYMBOL(__cpu_possible_mask);
30693069
struct cpumask __cpu_online_mask __read_mostly;
30703070
EXPORT_SYMBOL(__cpu_online_mask);
30713071

3072+
struct cpumask __cpu_enabled_mask __read_mostly;
3073+
EXPORT_SYMBOL(__cpu_enabled_mask);
3074+
30723075
struct cpumask __cpu_present_mask __read_mostly;
30733076
EXPORT_SYMBOL(__cpu_present_mask);
30743077

0 commit comments

Comments
 (0)