使用 Cpuset 将内核模块隔离到特定内核
Isolate Kernel Module to a Specific Core Using Cpuset
从 user-space 我们可以使用 cpuset 实际上 隔离 我们系统中的一个特定核心,并只对该核心执行一个特定进程。
我正在尝试对内核模块做同样的事情。所以我希望模块在一个独立的核心中执行。换句话说:如何在内核模块中使用 cpuset
? *
在我的内核模块中使用 linux/cpuset.h 不起作用。所以,我有一个这样的模块:
#include <linux/module.h>
#include <linux/cpuset.h>
...
#ifdef CONFIG_CPUSETS
printk(KERN_INFO, "cpusets is enabled!");
#endif
cpuset_init(); // this function is declared in cpuset.h
...
尝试加载此模块时,我收到(在 dmesg
中)以下消息 cpusets is enabled!
。但我也收到消息 Unknown symbol cpu_init (err 0)
.
同样,我尝试使用 linux/sched.h
中的 sched_setaffinity
以便将所有 运行ning 进程移动到特定核心,然后 运行 我的模块移动到独立核心.我收到相同的错误消息:Unknown symbol sched_setaffinity (err 0)
。我想我得到了 "unknown symbols" 因为这些函数在内核中没有 EXPORT_SYMBOL
。所以我去尝试调用 sys_sched_setaffinity
系统调用 (基于此 question)但再次收到此消息:Unknown symbol sys_sched_setaffinity (err 0)
!
此外,我不是在寻找使用 isolcpus
的解决方案,它是在启动时设置的。我只想加载模块,然后进行隔离。
- (更准确地说,我希望它的内核线程在 isolated 内核中执行。我知道我可以使用亲和力将线程绑定到特定内核,但这并不能保证我内核将被其他进程隔离运行在它们上面。)
So I want the module to get executed in an isolated core.
和
actually isolate a specific core in our system and execute just one
specific process to that core
这是在使用内核 3.16 的 Debian 机器上编译和测试的有效源代码。我先说下如何加载和卸载,以及传的参数是什么意思
所有资源都可以在 github 此处找到...
https://github.com/harryjackson/doc/tree/master/linux/kernel/toy/toy
构建并加载模块...
make
insmod toy param_cpu_id=2
卸载模块使用
rmmod toy
我没有使用 modprobe,因为它需要一些配置等。我们传递给 toy
内核模块的参数是我们想要隔离的 CPU。 None 被调用的设备操作将 运行 除非它们在 CPU.
上执行
加载模块后,您可以在此处找到它
/dev/toy
像
这样的简单操作
cat /dev/toy
创建内核模块捕获并产生一些输出的事件。您可以使用 dmesg
.
查看输出
源代码...
#include <linux/module.h>
#include <linux/fs.h>
#include <linux/miscdevice.h>
MODULE_LICENSE("GPL");
MODULE_AUTHOR("Harry");
MODULE_DESCRIPTION("toy kernel module");
MODULE_VERSION("0.1");
#define DEVICE_NAME "toy"
#define CLASS_NAME "toy"
static int param_cpu_id;
module_param(param_cpu_id , int, (S_IRUSR | S_IRGRP | S_IROTH));
MODULE_PARM_DESC(param_cpu_id, "CPU ID that operations run on");
//static void bar(void *arg);
//static void foo(void *cpu);
static int toy_open( struct inode *inodep, struct file *fp);
static ssize_t toy_read( struct file *fp , char *buffer, size_t len, loff_t * offset);
static ssize_t toy_write( struct file *fp , const char *buffer, size_t len, loff_t *);
static int toy_release(struct inode *inodep, struct file *fp);
static struct file_operations toy_fops = {
.owner = THIS_MODULE,
.open = toy_open,
.read = toy_read,
.write = toy_write,
.release = toy_release,
};
static struct miscdevice toy_device = {
.minor = MISC_DYNAMIC_MINOR,
.name = "toy",
.fops = &toy_fops
};
//static int CPU_IDS[64] = {0};
static int toy_open(struct inode *inodep, struct file *filep) {
int this_cpu = get_cpu();
printk(KERN_INFO "open: called on CPU:%d\n", this_cpu);
if(this_cpu == param_cpu_id) {
printk(KERN_INFO "open: is on requested CPU: %d\n", smp_processor_id());
}
else {
printk(KERN_INFO "open: not on requested CPU:%d\n", smp_processor_id());
}
put_cpu();
return 0;
}
static ssize_t toy_read(struct file *filep, char *buffer, size_t len, loff_t *offset){
int this_cpu = get_cpu();
printk(KERN_INFO "read: called on CPU:%d\n", this_cpu);
if(this_cpu == param_cpu_id) {
printk(KERN_INFO "read: is on requested CPU: %d\n", smp_processor_id());
}
else {
printk(KERN_INFO "read: not on requested CPU:%d\n", smp_processor_id());
}
put_cpu();
return 0;
}
static ssize_t toy_write(struct file *filep, const char *buffer, size_t len, loff_t *offset){
int this_cpu = get_cpu();
printk(KERN_INFO "write called on CPU:%d\n", this_cpu);
if(this_cpu == param_cpu_id) {
printk(KERN_INFO "write: is on requested CPU: %d\n", smp_processor_id());
}
else {
printk(KERN_INFO "write: not on requested CPU:%d\n", smp_processor_id());
}
put_cpu();
return 0;
}
static int toy_release(struct inode *inodep, struct file *filep){
int this_cpu = get_cpu();
printk(KERN_INFO "release called on CPU:%d\n", this_cpu);
if(this_cpu == param_cpu_id) {
printk(KERN_INFO "release: is on requested CPU: %d\n", smp_processor_id());
}
else {
printk(KERN_INFO "release: not on requested CPU:%d\n", smp_processor_id());
}
put_cpu();
return 0;
}
static int __init toy_init(void) {
int cpu_id;
if(param_cpu_id < 0 || param_cpu_id > 4) {
printk(KERN_INFO "toy: unable to load module without cpu parameter\n");
return -1;
}
printk(KERN_INFO "toy: loading to device driver, param_cpu_id: %d\n", param_cpu_id);
//preempt_disable(); // See notes below
cpu_id = get_cpu();
printk(KERN_INFO "toy init called and running on CPU: %d\n", cpu_id);
misc_register(&toy_device);
//preempt_enable(); // See notes below
put_cpu();
//smp_call_function_single(1,foo,(void *)(uintptr_t) 1,1);
return 0;
}
static void __exit toy_exit(void) {
misc_deregister(&toy_device);
printk(KERN_INFO "toy exit called\n");
}
module_init(toy_init);
module_exit(toy_exit);
上面的代码包含您要求的两种方法,即隔离 CPU 和 init
运行 隔离核心。
On init get_cpu
禁用抢占,即它之后发生的任何事情都不会被内核抢占,并且会 运行 在一个核心上。请注意,这是使用 3.16 内核完成的,您的里程可能会因内核版本而异,但我认为这些 API 已经存在很长时间了
这是生成文件...
obj-m += toy.o
all:
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules
clean:
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean
注释。 get_cpu
在 linux/smp.h
中声明为
#define get_cpu() ({ preempt_disable(); smp_processor_id(); })
#define put_cpu() preempt_enable()
所以您实际上不需要在调用 get_cpu
之前调用 preempt_disable
。
get_cpu 调用是对以下调用序列的包装...
preempt_count_inc();
barrier();
和 put_cpu 确实在这样做...
barrier();
if (unlikely(preempt_count_dec_and_test())) {
__preempt_schedule();
}
您可以使用上面的方法随心所欲。几乎所有这些都来自以下来源..
Google 对于... smp_call_function_single
Linux 内核开发,Robert Love 着。
http://derekmolloy.ie/writing-a-linux-kernel-module-part-2-a-character-device/
你试过 work_struct 和
struct workqueue_attrs {
cpumask_var_t cpumask; /* allowed CPUs */
}
首先cpu应该通过(例如cpu 0x1)
隔离
setenv bootargs isolcpus=\"0x1"\
接下来
struct lkm_sample {
struct work_struct lkm_work_struct;
struct workqueue_struct *lkm_wq_struct;
...
};
static struct lkm_sample lkm_smpl;
static void work(struct work_struct *work)
{
struct lkm_sample *tmp = container_of(work, struct lkm_sample, lkm_work_struct);
....
return;
}
static int __init lkm_init(void)
{
//see: https://lwn.net/Articles/540999/
lkm_smpl.lkm_wq_struct = create_singlethread_workqueue("you_wq_name");
INIT_WORK(&lkm_smpl.lkm_wq_struct, work);
}
如果你想在孤立的 cpu:
上开始 (运行 __init) lkm
setenv bootargs isolcpus=\"0x1"\
lsmod helper_module.ko 与
call_usermodehelper_setup
结构 subprocess_info * call_usermodehelper_setup ( char * 路径,
char ** argv, /*任务集 0x00000001 helper_application */
字符 ** 环境,
gfp_t gfp_mask,
int (*init) (struct subprocess_info *info, struct cred *new),
void (*cleanup) (struct subprocess_info *info),
无效*数据);
使用辅助内核模块,它应该 运行 用户空间程序 (helper_application) 通过任务集,掩码应该来自 isolcpus。
辅助模块应该 运行 只有 __init function() 和 return -1 因为只有一个任务:运行 userspace app on isolated cpu.
用户空间助手应用程序接下来应该只是:goal_module.ko 的 lsmod,
goal_module 应该从同一个孤立的 cpu.
开始
在孤立的cpu.
上使用workqueue继续运行孤立的模块
您提出问题:
I guess I got the "unknown symbols" because those functions have no EXPORT_SYMBOL in the kernel
我认为这是你问题的重点。我看到您包括了定义方法的文件 linux/cpuset.h
:cpuset_init
等。然而,在编译和使用命令 nm
期间,我们可以看到指示我们该功能不可用的指标:
正在编译:
root@hectorvp-pc:/home/hectorvp/cpuset/cpuset_try# make
make -C /lib/modules/3.19.0-31-generic/build M=/home/hectorvp/cpuset/cpuset_try modules
make[1]: Entering directory '/usr/src/linux-headers-3.19.0-31-generic'
CC [M] /home/hectorvp/cpuset/cpuset_try/cpuset_try.o
Building modules, stage 2.
MODPOST 1 modules
WARNING: "cpuset_init" [/home/hectorvp/cpuset/cpuset_try/cpuset_try.ko] undefined!
CC /home/hectorvp/cpuset/cpuset_try/cpuset_try.mod.o
LD [M] /home/hectorvp/cpuset/cpuset_try/cpuset_try.ko
make[1]: Leaving directory '/usr/src/linux-headers-3.19.0-31-generic'
见WARNING: "cupset_init" [...] undefined!
。并且 使用 nm
:
root@hectorvp-pc:/home/hectorvp/cpuset/cpuset_try# nm cpuset_try.ko
0000000000000030 T cleanup_module
U cpuset_init
U __fentry__
0000000000000000 T init_module
000000000000002f r __module_depends
U printk
0000000000000000 D __this_module
0000000000000000 r __UNIQUE_ID_license0
000000000000000c r __UNIQUE_ID_srcversion1
0000000000000038 r __UNIQUE_ID_vermagic0
0000000000000000 r ____versions
(注:U
代表'undefined')
但是,我一直在探索内核的符号如下:
root@hectorvp-pc:/home/hectorvp/cpuset/cpuset_try# cat /proc/kallsyms | grep cpuset_init
ffffffff8110dc40 T cpuset_init_current_mems_allowed
ffffffff81d722ae T cpuset_init
ffffffff81d72342 T cpuset_init_smp
我看到它已导出,但在 /lib/modules/$(uname -r)/build/Module.symvers
中不可用。所以你是对的。
经过进一步调查,我发现它实际上定义在:
http://lxr.free-electrons.com/source/kernel/cpuset.c#L2101
这是您需要调用的函数,因为它在内核中可用 space。因此,您不需要访问用户 space.
的第二个答案中报告了我发现的使模块能够调用此符号的变通方法。请注意,您 不再需要包含 linux/cpuset.h
:
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>
//#include <linux/cpuset.h>
#include <linux/kallsyms.h>
int init_module(void)
{
static void (*cpuset_init_p)(void);
cpuset_init_p = (void*) kallsyms_lookup_name("cpuset_init");
printk(KERN_INFO "Starting ...\n");
#ifdef CONFIG_CPUSETS
printk(KERN_INFO "cpusets is enabled!");
#endif
(*cpuset_init_p)();
/*
* A non 0 return means init_module failed; module can't be loaded.
*/
return 0;
}
void cleanup_module(void)
{
printk(KERN_INFO "Ending ...\n");
}
MODULE_LICENSE("GPL");
编译成功,用insmod
安装。下面是我在 dmesg
:
中得到的输出
[ 1713.738925] Starting ...
[ 1713.738929] cpusets is enabled!
[ 1713.738943] kernel tried to execute NX-protected page - exploit attempt? (uid: 0)
[ 1713.739042] BUG: unable to handle kernel paging request at ffffffff81d7237b
[ 1713.739074] IP: [<ffffffff81d7237b>] cpuset_init+0x0/0x94
[ 1713.739102] PGD 1c16067 PUD 1c17063 PMD 30bc74063 PTE 8000000001d72163
[ 1713.739136] Oops: 0011 [#1] SMP
[ 1713.739153] Modules linked in: cpuset_try(OE+) xt_conntrack ipt_MASQUERADE nf_nat_masquerade_ipv4 iptable_nat nf_conntrack_ipv4 nf_defrag_ipv4 nf_nat_ipv4 xt_addrtype iptable_filter ip_tables x_tables nf_nat nf_conntrack br_netfilter bridge stp llc pci_stub vboxpci(OE) vboxnetadp(OE) vboxnetflt(OE) vboxdrv(OE) aufs binfmt_misc cfg80211 nls_iso8859_1 snd_hda_codec_hdmi snd_hda_codec_realtek intel_rapl snd_hda_codec_generic iosf_mbi snd_hda_intel x86_pkg_temp_thermal intel_powerclamp snd_hda_controller snd_hda_codec snd_hwdep coretemp kvm_intel amdkfd kvm snd_pcm snd_seq_midi snd_seq_midi_event amd_iommu_v2 snd_rawmidi radeon snd_seq crct10dif_pclmul crc32_pclmul snd_seq_device aesni_intel ttm aes_x86_64 drm_kms_helper drm snd_timer i2c_algo_bit dcdbas mei_me lrw gf128mul mei snd glue_helper ablk_helper
[ 1713.739533] cryptd soundcore shpchp lpc_ich serio_raw 8250_fintek mac_hid video parport_pc ppdev lp parport autofs4 hid_generic usbhid hid e1000e ahci psmouse ptp libahci pps_core
[ 1713.739628] CPU: 2 PID: 24679 Comm: insmod Tainted: G OE 3.19.0-56-generic #62-Ubuntu
[ 1713.739663] Hardware name: Dell Inc. OptiPlex 9020/0PC5F7, BIOS A03 09/17/2013
[ 1713.739693] task: ffff8800d29f09d0 ti: ffff88009177c000 task.ti: ffff88009177c000
[ 1713.739723] RIP: 0010:[<ffffffff81d7237b>] [<ffffffff81d7237b>] cpuset_init+0x0/0x94
[ 1713.739757] RSP: 0018:ffff88009177fd10 EFLAGS: 00010292
[ 1713.739779] RAX: 0000000000000013 RBX: ffffffff81c1a080 RCX: 0000000000000013
[ 1713.739808] RDX: 000000000000c928 RSI: 0000000000000246 RDI: 0000000000000246
[ 1713.739836] RBP: ffff88009177fd18 R08: 000000000000000a R09: 00000000000003db
[ 1713.739865] R10: 0000000000000092 R11: 00000000000003db R12: ffff8800ad1aaee0
[ 1713.739893] R13: 0000000000000000 R14: ffffffffc0947000 R15: ffff88009177fef8
[ 1713.739923] FS: 00007fbf45be8700(0000) GS:ffff88031dd00000(0000) knlGS:0000000000000000
[ 1713.739955] CS: 0010 DS: 0000 ES: 0000 CR0: 0000000080050033
[ 1713.739979] CR2: ffffffff81d7237b CR3: 00000000a3733000 CR4: 00000000001407e0
[ 1713.740007] Stack:
[ 1713.740016] ffffffffc094703e ffff88009177fd98 ffffffff81002148 0000000000000001
[ 1713.740052] 0000000000000001 ffff8802479de200 0000000000000001 ffff88009177fd78
[ 1713.740087] ffffffff811d79e9 ffffffff810fb058 0000000000000018 ffffffffc0949000
[ 1713.740122] Call Trace:
[ 1713.740137] [<ffffffffc094703e>] ? init_module+0x3e/0x50 [cpuset_try]
[ 1713.740175] [<ffffffff81002148>] do_one_initcall+0xd8/0x210
[ 1713.740190] [<ffffffff811d79e9>] ? kmem_cache_alloc_trace+0x189/0x200
[ 1713.740207] [<ffffffff810fb058>] ? load_module+0x15b8/0x1d00
[ 1713.740222] [<ffffffff810fb092>] load_module+0x15f2/0x1d00
[ 1713.740236] [<ffffffff810f6850>] ? store_uevent+0x40/0x40
[ 1713.740250] [<ffffffff810fb916>] SyS_finit_module+0x86/0xb0
[ 1713.740265] [<ffffffff817ce10d>] system_call_fastpath+0x16/0x1b
[ 1713.740280] Code: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 0c 53 58 31 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 <00> 00 00 00 00 1c 00 00 00 c0 92 2c 7d c0 92 2c 7d a0 fc 69 ee
[ 1713.740398] RIP [<ffffffff81d7237b>] cpuset_init+0x0/0x94
[ 1713.740413] RSP <ffff88009177fd10>
[ 1713.740421] CR2: ffffffff81d7237b
[ 1713.746177] ---[ end trace 25614103c0658b94 ]---
尽管有错误,我想说我已经回答了你最初的问题:
How do I use cpuset's from inside a kernel module? *
可能不是最优雅的方式,因为我根本不是专家。您需要从这里继续。
此致
使用 on_each_cpu()
并过滤所需的 CPU 有效:
targetcpu.c
#include <linux/init.h>
#include <linux/module.h>
#include <linux/kernel.h>
const static int TARGET_CPU = 4;
static void func(void *info){
int cpu = get_cpu();
if(cpu == TARGET_CPU){
printk("on target cpu: %d\n", cpu);
}
put_cpu();
}
int init_module(void) {
printk("enter\n");
on_each_cpu(func, NULL, 1);
return 0;
}
void cleanup_module(void) {
printk("exit\n");
}
Makefile
obj-m += targetcpu.o
all:
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules
clean:
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean
从 user-space 我们可以使用 cpuset 实际上 隔离 我们系统中的一个特定核心,并只对该核心执行一个特定进程。
我正在尝试对内核模块做同样的事情。所以我希望模块在一个独立的核心中执行。换句话说:如何在内核模块中使用 cpuset
? *
在我的内核模块中使用 linux/cpuset.h 不起作用。所以,我有一个这样的模块:
#include <linux/module.h>
#include <linux/cpuset.h>
...
#ifdef CONFIG_CPUSETS
printk(KERN_INFO, "cpusets is enabled!");
#endif
cpuset_init(); // this function is declared in cpuset.h
...
尝试加载此模块时,我收到(在 dmesg
中)以下消息 cpusets is enabled!
。但我也收到消息 Unknown symbol cpu_init (err 0)
.
同样,我尝试使用 linux/sched.h
中的 sched_setaffinity
以便将所有 运行ning 进程移动到特定核心,然后 运行 我的模块移动到独立核心.我收到相同的错误消息:Unknown symbol sched_setaffinity (err 0)
。我想我得到了 "unknown symbols" 因为这些函数在内核中没有 EXPORT_SYMBOL
。所以我去尝试调用 sys_sched_setaffinity
系统调用 (基于此 question)但再次收到此消息:Unknown symbol sys_sched_setaffinity (err 0)
!
此外,我不是在寻找使用 isolcpus
的解决方案,它是在启动时设置的。我只想加载模块,然后进行隔离。
- (更准确地说,我希望它的内核线程在 isolated 内核中执行。我知道我可以使用亲和力将线程绑定到特定内核,但这并不能保证我内核将被其他进程隔离运行在它们上面。)
So I want the module to get executed in an isolated core.
和
actually isolate a specific core in our system and execute just one specific process to that core
这是在使用内核 3.16 的 Debian 机器上编译和测试的有效源代码。我先说下如何加载和卸载,以及传的参数是什么意思
所有资源都可以在 github 此处找到...
https://github.com/harryjackson/doc/tree/master/linux/kernel/toy/toy
构建并加载模块...
make
insmod toy param_cpu_id=2
卸载模块使用
rmmod toy
我没有使用 modprobe,因为它需要一些配置等。我们传递给 toy
内核模块的参数是我们想要隔离的 CPU。 None 被调用的设备操作将 运行 除非它们在 CPU.
加载模块后,您可以在此处找到它
/dev/toy
像
这样的简单操作cat /dev/toy
创建内核模块捕获并产生一些输出的事件。您可以使用 dmesg
.
源代码...
#include <linux/module.h>
#include <linux/fs.h>
#include <linux/miscdevice.h>
MODULE_LICENSE("GPL");
MODULE_AUTHOR("Harry");
MODULE_DESCRIPTION("toy kernel module");
MODULE_VERSION("0.1");
#define DEVICE_NAME "toy"
#define CLASS_NAME "toy"
static int param_cpu_id;
module_param(param_cpu_id , int, (S_IRUSR | S_IRGRP | S_IROTH));
MODULE_PARM_DESC(param_cpu_id, "CPU ID that operations run on");
//static void bar(void *arg);
//static void foo(void *cpu);
static int toy_open( struct inode *inodep, struct file *fp);
static ssize_t toy_read( struct file *fp , char *buffer, size_t len, loff_t * offset);
static ssize_t toy_write( struct file *fp , const char *buffer, size_t len, loff_t *);
static int toy_release(struct inode *inodep, struct file *fp);
static struct file_operations toy_fops = {
.owner = THIS_MODULE,
.open = toy_open,
.read = toy_read,
.write = toy_write,
.release = toy_release,
};
static struct miscdevice toy_device = {
.minor = MISC_DYNAMIC_MINOR,
.name = "toy",
.fops = &toy_fops
};
//static int CPU_IDS[64] = {0};
static int toy_open(struct inode *inodep, struct file *filep) {
int this_cpu = get_cpu();
printk(KERN_INFO "open: called on CPU:%d\n", this_cpu);
if(this_cpu == param_cpu_id) {
printk(KERN_INFO "open: is on requested CPU: %d\n", smp_processor_id());
}
else {
printk(KERN_INFO "open: not on requested CPU:%d\n", smp_processor_id());
}
put_cpu();
return 0;
}
static ssize_t toy_read(struct file *filep, char *buffer, size_t len, loff_t *offset){
int this_cpu = get_cpu();
printk(KERN_INFO "read: called on CPU:%d\n", this_cpu);
if(this_cpu == param_cpu_id) {
printk(KERN_INFO "read: is on requested CPU: %d\n", smp_processor_id());
}
else {
printk(KERN_INFO "read: not on requested CPU:%d\n", smp_processor_id());
}
put_cpu();
return 0;
}
static ssize_t toy_write(struct file *filep, const char *buffer, size_t len, loff_t *offset){
int this_cpu = get_cpu();
printk(KERN_INFO "write called on CPU:%d\n", this_cpu);
if(this_cpu == param_cpu_id) {
printk(KERN_INFO "write: is on requested CPU: %d\n", smp_processor_id());
}
else {
printk(KERN_INFO "write: not on requested CPU:%d\n", smp_processor_id());
}
put_cpu();
return 0;
}
static int toy_release(struct inode *inodep, struct file *filep){
int this_cpu = get_cpu();
printk(KERN_INFO "release called on CPU:%d\n", this_cpu);
if(this_cpu == param_cpu_id) {
printk(KERN_INFO "release: is on requested CPU: %d\n", smp_processor_id());
}
else {
printk(KERN_INFO "release: not on requested CPU:%d\n", smp_processor_id());
}
put_cpu();
return 0;
}
static int __init toy_init(void) {
int cpu_id;
if(param_cpu_id < 0 || param_cpu_id > 4) {
printk(KERN_INFO "toy: unable to load module without cpu parameter\n");
return -1;
}
printk(KERN_INFO "toy: loading to device driver, param_cpu_id: %d\n", param_cpu_id);
//preempt_disable(); // See notes below
cpu_id = get_cpu();
printk(KERN_INFO "toy init called and running on CPU: %d\n", cpu_id);
misc_register(&toy_device);
//preempt_enable(); // See notes below
put_cpu();
//smp_call_function_single(1,foo,(void *)(uintptr_t) 1,1);
return 0;
}
static void __exit toy_exit(void) {
misc_deregister(&toy_device);
printk(KERN_INFO "toy exit called\n");
}
module_init(toy_init);
module_exit(toy_exit);
上面的代码包含您要求的两种方法,即隔离 CPU 和 init
运行 隔离核心。
On init get_cpu
禁用抢占,即它之后发生的任何事情都不会被内核抢占,并且会 运行 在一个核心上。请注意,这是使用 3.16 内核完成的,您的里程可能会因内核版本而异,但我认为这些 API 已经存在很长时间了
这是生成文件...
obj-m += toy.o
all:
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules
clean:
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean
注释。 get_cpu
在 linux/smp.h
中声明为
#define get_cpu() ({ preempt_disable(); smp_processor_id(); })
#define put_cpu() preempt_enable()
所以您实际上不需要在调用 get_cpu
之前调用 preempt_disable
。
get_cpu 调用是对以下调用序列的包装...
preempt_count_inc();
barrier();
和 put_cpu 确实在这样做...
barrier();
if (unlikely(preempt_count_dec_and_test())) {
__preempt_schedule();
}
您可以使用上面的方法随心所欲。几乎所有这些都来自以下来源..
Google 对于... smp_call_function_single
Linux 内核开发,Robert Love 着。
http://derekmolloy.ie/writing-a-linux-kernel-module-part-2-a-character-device/
你试过 work_struct 和
struct workqueue_attrs {
cpumask_var_t cpumask; /* allowed CPUs */
}
首先cpu应该通过(例如cpu 0x1)
隔离setenv bootargs isolcpus=\"0x1"\
接下来
struct lkm_sample {
struct work_struct lkm_work_struct;
struct workqueue_struct *lkm_wq_struct;
...
};
static struct lkm_sample lkm_smpl;
static void work(struct work_struct *work)
{
struct lkm_sample *tmp = container_of(work, struct lkm_sample, lkm_work_struct);
....
return;
}
static int __init lkm_init(void)
{
//see: https://lwn.net/Articles/540999/
lkm_smpl.lkm_wq_struct = create_singlethread_workqueue("you_wq_name");
INIT_WORK(&lkm_smpl.lkm_wq_struct, work);
}
如果你想在孤立的 cpu:
上开始 (运行 __init) lkmsetenv bootargs isolcpus=\"0x1"\
lsmod helper_module.ko 与
call_usermodehelper_setup 结构 subprocess_info * call_usermodehelper_setup ( char * 路径, char ** argv, /*任务集 0x00000001 helper_application */ 字符 ** 环境, gfp_t gfp_mask, int (*init) (struct subprocess_info *info, struct cred *new), void (*cleanup) (struct subprocess_info *info), 无效*数据); 使用辅助内核模块,它应该 运行 用户空间程序 (helper_application) 通过任务集,掩码应该来自 isolcpus。 辅助模块应该 运行 只有 __init function() 和 return -1 因为只有一个任务:运行 userspace app on isolated cpu.
用户空间助手应用程序接下来应该只是:goal_module.ko 的 lsmod, goal_module 应该从同一个孤立的 cpu.
开始
在孤立的cpu.
上使用workqueue继续运行孤立的模块
您提出问题:
I guess I got the "unknown symbols" because those functions have no EXPORT_SYMBOL in the kernel
我认为这是你问题的重点。我看到您包括了定义方法的文件 linux/cpuset.h
:cpuset_init
等。然而,在编译和使用命令 nm
期间,我们可以看到指示我们该功能不可用的指标:
正在编译:
root@hectorvp-pc:/home/hectorvp/cpuset/cpuset_try# make
make -C /lib/modules/3.19.0-31-generic/build M=/home/hectorvp/cpuset/cpuset_try modules
make[1]: Entering directory '/usr/src/linux-headers-3.19.0-31-generic'
CC [M] /home/hectorvp/cpuset/cpuset_try/cpuset_try.o
Building modules, stage 2.
MODPOST 1 modules
WARNING: "cpuset_init" [/home/hectorvp/cpuset/cpuset_try/cpuset_try.ko] undefined!
CC /home/hectorvp/cpuset/cpuset_try/cpuset_try.mod.o
LD [M] /home/hectorvp/cpuset/cpuset_try/cpuset_try.ko
make[1]: Leaving directory '/usr/src/linux-headers-3.19.0-31-generic'
见WARNING: "cupset_init" [...] undefined!
。并且 使用 nm
:
root@hectorvp-pc:/home/hectorvp/cpuset/cpuset_try# nm cpuset_try.ko
0000000000000030 T cleanup_module
U cpuset_init
U __fentry__
0000000000000000 T init_module
000000000000002f r __module_depends
U printk
0000000000000000 D __this_module
0000000000000000 r __UNIQUE_ID_license0
000000000000000c r __UNIQUE_ID_srcversion1
0000000000000038 r __UNIQUE_ID_vermagic0
0000000000000000 r ____versions
(注:U
代表'undefined')
但是,我一直在探索内核的符号如下:
root@hectorvp-pc:/home/hectorvp/cpuset/cpuset_try# cat /proc/kallsyms | grep cpuset_init
ffffffff8110dc40 T cpuset_init_current_mems_allowed
ffffffff81d722ae T cpuset_init
ffffffff81d72342 T cpuset_init_smp
我看到它已导出,但在 /lib/modules/$(uname -r)/build/Module.symvers
中不可用。所以你是对的。
经过进一步调查,我发现它实际上定义在:
http://lxr.free-electrons.com/source/kernel/cpuset.c#L2101
这是您需要调用的函数,因为它在内核中可用 space。因此,您不需要访问用户 space.
linux/cpuset.h
:
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>
//#include <linux/cpuset.h>
#include <linux/kallsyms.h>
int init_module(void)
{
static void (*cpuset_init_p)(void);
cpuset_init_p = (void*) kallsyms_lookup_name("cpuset_init");
printk(KERN_INFO "Starting ...\n");
#ifdef CONFIG_CPUSETS
printk(KERN_INFO "cpusets is enabled!");
#endif
(*cpuset_init_p)();
/*
* A non 0 return means init_module failed; module can't be loaded.
*/
return 0;
}
void cleanup_module(void)
{
printk(KERN_INFO "Ending ...\n");
}
MODULE_LICENSE("GPL");
编译成功,用insmod
安装。下面是我在 dmesg
:
[ 1713.738925] Starting ...
[ 1713.738929] cpusets is enabled!
[ 1713.738943] kernel tried to execute NX-protected page - exploit attempt? (uid: 0)
[ 1713.739042] BUG: unable to handle kernel paging request at ffffffff81d7237b
[ 1713.739074] IP: [<ffffffff81d7237b>] cpuset_init+0x0/0x94
[ 1713.739102] PGD 1c16067 PUD 1c17063 PMD 30bc74063 PTE 8000000001d72163
[ 1713.739136] Oops: 0011 [#1] SMP
[ 1713.739153] Modules linked in: cpuset_try(OE+) xt_conntrack ipt_MASQUERADE nf_nat_masquerade_ipv4 iptable_nat nf_conntrack_ipv4 nf_defrag_ipv4 nf_nat_ipv4 xt_addrtype iptable_filter ip_tables x_tables nf_nat nf_conntrack br_netfilter bridge stp llc pci_stub vboxpci(OE) vboxnetadp(OE) vboxnetflt(OE) vboxdrv(OE) aufs binfmt_misc cfg80211 nls_iso8859_1 snd_hda_codec_hdmi snd_hda_codec_realtek intel_rapl snd_hda_codec_generic iosf_mbi snd_hda_intel x86_pkg_temp_thermal intel_powerclamp snd_hda_controller snd_hda_codec snd_hwdep coretemp kvm_intel amdkfd kvm snd_pcm snd_seq_midi snd_seq_midi_event amd_iommu_v2 snd_rawmidi radeon snd_seq crct10dif_pclmul crc32_pclmul snd_seq_device aesni_intel ttm aes_x86_64 drm_kms_helper drm snd_timer i2c_algo_bit dcdbas mei_me lrw gf128mul mei snd glue_helper ablk_helper
[ 1713.739533] cryptd soundcore shpchp lpc_ich serio_raw 8250_fintek mac_hid video parport_pc ppdev lp parport autofs4 hid_generic usbhid hid e1000e ahci psmouse ptp libahci pps_core
[ 1713.739628] CPU: 2 PID: 24679 Comm: insmod Tainted: G OE 3.19.0-56-generic #62-Ubuntu
[ 1713.739663] Hardware name: Dell Inc. OptiPlex 9020/0PC5F7, BIOS A03 09/17/2013
[ 1713.739693] task: ffff8800d29f09d0 ti: ffff88009177c000 task.ti: ffff88009177c000
[ 1713.739723] RIP: 0010:[<ffffffff81d7237b>] [<ffffffff81d7237b>] cpuset_init+0x0/0x94
[ 1713.739757] RSP: 0018:ffff88009177fd10 EFLAGS: 00010292
[ 1713.739779] RAX: 0000000000000013 RBX: ffffffff81c1a080 RCX: 0000000000000013
[ 1713.739808] RDX: 000000000000c928 RSI: 0000000000000246 RDI: 0000000000000246
[ 1713.739836] RBP: ffff88009177fd18 R08: 000000000000000a R09: 00000000000003db
[ 1713.739865] R10: 0000000000000092 R11: 00000000000003db R12: ffff8800ad1aaee0
[ 1713.739893] R13: 0000000000000000 R14: ffffffffc0947000 R15: ffff88009177fef8
[ 1713.739923] FS: 00007fbf45be8700(0000) GS:ffff88031dd00000(0000) knlGS:0000000000000000
[ 1713.739955] CS: 0010 DS: 0000 ES: 0000 CR0: 0000000080050033
[ 1713.739979] CR2: ffffffff81d7237b CR3: 00000000a3733000 CR4: 00000000001407e0
[ 1713.740007] Stack:
[ 1713.740016] ffffffffc094703e ffff88009177fd98 ffffffff81002148 0000000000000001
[ 1713.740052] 0000000000000001 ffff8802479de200 0000000000000001 ffff88009177fd78
[ 1713.740087] ffffffff811d79e9 ffffffff810fb058 0000000000000018 ffffffffc0949000
[ 1713.740122] Call Trace:
[ 1713.740137] [<ffffffffc094703e>] ? init_module+0x3e/0x50 [cpuset_try]
[ 1713.740175] [<ffffffff81002148>] do_one_initcall+0xd8/0x210
[ 1713.740190] [<ffffffff811d79e9>] ? kmem_cache_alloc_trace+0x189/0x200
[ 1713.740207] [<ffffffff810fb058>] ? load_module+0x15b8/0x1d00
[ 1713.740222] [<ffffffff810fb092>] load_module+0x15f2/0x1d00
[ 1713.740236] [<ffffffff810f6850>] ? store_uevent+0x40/0x40
[ 1713.740250] [<ffffffff810fb916>] SyS_finit_module+0x86/0xb0
[ 1713.740265] [<ffffffff817ce10d>] system_call_fastpath+0x16/0x1b
[ 1713.740280] Code: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 0c 53 58 31 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 <00> 00 00 00 00 1c 00 00 00 c0 92 2c 7d c0 92 2c 7d a0 fc 69 ee
[ 1713.740398] RIP [<ffffffff81d7237b>] cpuset_init+0x0/0x94
[ 1713.740413] RSP <ffff88009177fd10>
[ 1713.740421] CR2: ffffffff81d7237b
[ 1713.746177] ---[ end trace 25614103c0658b94 ]---
尽管有错误,我想说我已经回答了你最初的问题:
How do I use cpuset's from inside a kernel module? *
可能不是最优雅的方式,因为我根本不是专家。您需要从这里继续。
此致
使用 on_each_cpu()
并过滤所需的 CPU 有效:
targetcpu.c
#include <linux/init.h>
#include <linux/module.h>
#include <linux/kernel.h>
const static int TARGET_CPU = 4;
static void func(void *info){
int cpu = get_cpu();
if(cpu == TARGET_CPU){
printk("on target cpu: %d\n", cpu);
}
put_cpu();
}
int init_module(void) {
printk("enter\n");
on_each_cpu(func, NULL, 1);
return 0;
}
void cleanup_module(void) {
printk("exit\n");
}
Makefile
obj-m += targetcpu.o
all:
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules
clean:
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean