Enable/disable cpu

Bjørn Mork bjorn at mork.no
Mon Oct 19 07:12:59 EDT 2015


Doug Wilson <doug.lkml at gmail.com> writes:

> All,
>
>   I have been looking at sysfs cpu side code. Had a quick question.
>
> On my x86 box:
> #ls /sys/devices/system/cpu/
> I see 4 cpus plus a few more fields such as online/present/possible etc.
>
> This come from drivers/base/cpu.c.
>
> My question here is, in each of those cpu0-3 folders, there's file by name
> online which one can use to disable/enable cpu using cpumask. Could
> someone point me to that code, I could not really pin point to where exactly
> this attribute is defined. Am just exploring cpu hotplug feature.


drivers/base/core.c has this generic code for creating that file:

        if (device_supports_offline(dev) && !dev->offline_disabled) {
                error = device_create_file(dev, &dev_attr_online);
                if (error)
                        goto err_remove_dev_groups;
        }


where device_supports_offline is:

static inline bool device_supports_offline(struct device *dev)
{
        return dev->bus && dev->bus->offline && dev->bus->online;
}

and the cpu "bus" is (from drivers/base/cpu.c):

struct bus_type cpu_subsys = {
        .name = "cpu",
        .dev_name = "cpu",
        .match = cpu_subsys_match,
#ifdef CONFIG_HOTPLUG_CPU
        .online = cpu_subsys_online,
        .offline = cpu_subsys_offline,
#endif
};


The fields of the cpu device are initialized in drivers/base/cpu.c:


int register_cpu(struct cpu *cpu, int num)
{
        int error;

        cpu->node_id = cpu_to_node(num);
        memset(&cpu->dev, 0x00, sizeof(struct device));
        cpu->dev.id = num;
        cpu->dev.bus = &cpu_subsys;
        cpu->dev.release = cpu_device_release;
        cpu->dev.offline_disabled = !cpu->hotpluggable;
        cpu->dev.offline = !cpu_online(num);
        cpu->dev.of_node = of_get_cpu_node(num, NULL);
#ifdef CONFIG_GENERIC_CPU_AUTOPROBE
        cpu->dev.bus->uevent = cpu_uevent;
#endif
        cpu->dev.groups = common_cpu_attr_groups;
        if (cpu->hotpluggable)
                cpu->dev.groups = hotplugable_cpu_attr_groups;
        error = device_register(&cpu->dev);
        if (!error)
                per_cpu(cpu_sys_devices, num) = &cpu->dev;
        if (!error)
                register_cpu_under_node(num, cpu_to_node(num));

        return error;
}


So you get the "online" file in the cpuX folders when CONFIG_HOTPLUG_CPU
is defined and cpu->hotpluggable is true.  Which will exclude cpu0 in
some cases. See arch/x86/kernel/topology.c for the x86 specific logic
concerning that.  I assume other platforms have similar exceptions


Bjørn



More information about the Kernelnewbies mailing list