1

My question mostly about hardware, specifically the Intel i5-2500K CPU which Intel describes as having

# of Cores 4
# of Threads 4

Linux shows me 4 processors:

$ cat /proc/cpuinfo  | grep ^processor
processor       : 0
processor       : 1
processor       : 2
processor       : 3

Nevertheless, I've written a little kernel module that shows me 8 processors:

$ cat show_cpus_mod.c 
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/version.h>

#define CLASS_NAME "show_cpus_mod"

#define dbg( format, arg... )  do { if ( debug ) pr_info( CLASS_NAME ": %s: " format , __FUNCTION__ , ## arg ); } while ( 0 )
#define err( format, arg... )  pr_err(  CLASS_NAME ": " format, ## arg )
#define info( format, arg... ) pr_info( CLASS_NAME ": " format, ## arg )
#define warn( format, arg... ) pr_warn( CLASS_NAME ": " format, ## arg )

MODULE_DESCRIPTION( "shows all cpus" );
MODULE_VERSION( "0.1" );
MODULE_LICENSE( "GPL" );
MODULE_AUTHOR( "author <e@mail.mail>" );

static int show_cpus_mod_init( void ) {
        int cpu;
        info( "Start loading module show_cpus_mod.\n" );
        for_each_possible_cpu( cpu ) {
                info( "cpu = %d\n", cpu );
        }
        return 0;
}

static void show_cpus_mod_exit( void ) {
        info( "Module show_cpus_mod unloaded\n" );
}

module_init( show_cpus_mod_init );
module_exit( show_cpus_mod_exit );

Building:

$ cat Makefile 
CURRENT = $(shell uname -r)
KDIR = /lib/modules/$(CURRENT)/build
PWD = $(shell pwd)
TARGET = show_cpus_mod
obj-m := $(TARGET).o

default:
        $(MAKE) -C $(KDIR) M=$(PWD) modules

clean:
        @rm -f *.o .*.cmd .*.flags *.mod.c *.order
        @rm -f .*.*.cmd *.symvers *~ *.*~ TODO.*
        @rm -fR .tmp*
        @rm -rf .tmp_versions

Inserting:

# make
# cp show_cpus_mod.ko /lib/modules/4.14.0-kali3-amd64/
# depmod
# modprobe show_cpus_mod

syslog:

localhost kernel: [67596.578805] show_cpus_mod: Start loading module show_cpus_mod.
localhost kernel: [67596.578808] show_cpus_mod: cpu = 0
localhost kernel: [67596.578809] show_cpus_mod: cpu = 1
localhost kernel: [67596.578810] show_cpus_mod: cpu = 2
localhost kernel: [67596.578811] show_cpus_mod: cpu = 3
localhost kernel: [67596.578811] show_cpus_mod: cpu = 4
localhost kernel: [67596.578812] show_cpus_mod: cpu = 5
localhost kernel: [67596.578812] show_cpus_mod: cpu = 6
localhost kernel: [67596.578813] show_cpus_mod: cpu = 7
localhost kernel: [67607.725738] show_cpus_mod: Module show_cpus_mod unloaded

What am I missing in Intel's description? Why 8? Or what is wrong with my kernel module?

3

You should use for_each_online_cpu or for_each_present_cpu instead of for_each_possible_cpu. That will limit the output to CPUs which are really online or present, respectively.

  • yeah, thanx. possible is something wrong... – wc1eBdb56TamM Oct 1 '18 at 8:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.