Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ objs-$(CONFIG_SCHEDSTATS) += stats.o
objs-$(CONFIG_SCHED_DEBUG) += debug.o

obj-m += scheduler.o
scheduler-objs := $(objs-y) $(sidecar_objs) main.o sched_rebuild.o
scheduler-objs := $(objs-y) $(sidecar_objs) main.o tainted.o mempool.o sched_rebuild.o

search_cb := {if (/CALLBACK/) {print "__cb_"$$2} else {print $$2}}
search_und := grep "UND __mod_" | awk '{print substr($$8,7)}' | sort | uniq
Expand Down
23 changes: 7 additions & 16 deletions src/head_jump.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@
#ifndef __HEAD_JUMP_H
#define __HEAD_JUMP_H

#include <linux/cpu.h>
#include <linux/kallsyms.h>
#include "helper.h"

#define EXPORT_SIDECAR(fn, file, ...) EXPORT_PLUGSCHED(fn, __VA_ARGS__)
#define EXPORT_CALLBACK EXPORT_PLUGSCHED
Expand Down Expand Up @@ -53,8 +52,8 @@ static unsigned long mod_func_size[NR_INTERFACE_FN];
* 2) For functions set:
* 1. Add the function to export_jump.h file
* 2. Call jump_init_all() to init all functions data
* 3. Use JUMP_OPERATION(install) macro to replace the functions set
* 4. Use JUMP_OPERATION(remove) macro to restore the functions set
* 3. Use jump_install() to replace the functions set
* 4. Use jump_remove() to restore the functions set
*/

#ifdef CONFIG_X86_64
Expand Down Expand Up @@ -93,14 +92,6 @@ extern void __orig___fentry__(void);
#define JUMP_REMOVE_FUNC(func) \
memcpy((unsigned char *)__orig_##func, store_orig_##func, HEAD_LEN)


/* Must be used in stop machine context */
#define JUMP_OPERATION(ops) do { \
void *unused = disable_write_protect(NULL); \
jump_##ops(); \
enable_write_protect(); \
} while(0)

#else /* For ARM64 */
#define DEFINE_JUMP_FUNC(func) \
static u32 store_orig_##func; \
Expand All @@ -122,10 +113,6 @@ extern void __orig___fentry__(void);
#define JUMP_REMOVE_FUNC(func) \
aarch64_insn_patch_text_nosync(__orig_##func, store_orig_##func)

#define JUMP_OPERATION(ops) do { \
jump_##ops(); \
} while(0)

#endif /* CONFIG_X86_64 */

#define EXPORT_PLUGSCHED(fn, ...) DEFINE_JUMP_FUNC(fn);
Expand All @@ -135,14 +122,18 @@ extern void __orig___fentry__(void);
#define EXPORT_PLUGSCHED(fn, ...) JUMP_INSTALL_FUNC(fn);
static inline void jump_install(void)
{
disable_write_protect_global();
#include "export_jump.h"
enable_write_protect_global();
}
#undef EXPORT_PLUGSCHED

#define EXPORT_PLUGSCHED(fn, ...) JUMP_REMOVE_FUNC(fn);
static inline void jump_remove(void)
{
disable_write_protect_global();
#include "export_jump.h"
enable_write_protect_global();
}
#undef EXPORT_PLUGSCHED

Expand Down
16 changes: 16 additions & 0 deletions src/helper.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// Copyright 2019-2022 Alibaba Group Holding Limited.
// SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause

#ifndef __HELPER_H
#define __HELPER_H

/*
* helper function to communicate with vmlinux
Expand All @@ -14,6 +16,16 @@ static inline void do_write_cr0(unsigned long val)
asm volatile("mov %0,%%cr0": "+r" (val) : : "memory");
}

static inline void *disable_write_protect_global(void)
{
return disable_write_protect(NULL);
}

static inline void enable_write_protect_global(void)
{
enable_write_protect();
}

static inline void *disable_write_protect(void *addr)
{
BUG_ON(orig_cr0);
Expand All @@ -37,6 +49,9 @@ static inline void enable_write_protect(void)
#include <asm/memory.h>
#include <asm/cacheflush.h>

static inline void *disable_write_protect_global(void) { }
static inline void enable_write_protect_global(void) { }

static void *disable_write_protect(void *addr)
{
unsigned long uintaddr = (uintptr_t) addr;
Expand Down Expand Up @@ -147,3 +162,4 @@ static void addr_sort(unsigned long *addr, unsigned long *size, int n) {
}
}
}
#endif
48 changes: 4 additions & 44 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include "mempool.h"
#include "head_jump.h"
#include "stack_check.h"
#include "tainted.h"

#define CHECK_STACK_LAYOUT() \
BUILD_BUG_ON_MSG(MODULE_FRAME_POINTER != VMLINUX_FRAME_POINTER, \
Expand Down Expand Up @@ -70,23 +71,6 @@ extern void switch_sched_class(bool mod);
static int scheduler_enable = 0;
struct kobject *plugsched_dir, *plugsched_subdir, *vmlinux_moddir;

struct tainted_function {
char *name;
struct kobject *kobj;
};

#undef TAINTED_FUNCTION
#define TAINTED_FUNCTION(func,sympos) \
{ \
.name = #func "," #sympos, \
.kobj = NULL, \
},

struct tainted_function tainted_functions[] = {
#include "tainted_functions.h"
{}
};

static inline void parallel_state_check_init(void)
{
atomic_set(&cpu_finished, num_online_cpus());
Expand Down Expand Up @@ -188,7 +172,7 @@ static int __sync_sched_install(void *arg)

if (is_first_process()) {
switch_sched_class(true);
JUMP_OPERATION(install);
jump_install();
disable_stack_protector();
sched_alloc_extrapad();
reset_balance_callback();
Expand Down Expand Up @@ -236,7 +220,7 @@ static int __sync_sched_restore(void *arg)

if (is_first_process()) {
switch_sched_class(false);
JUMP_OPERATION(remove);
jump_remove();
reset_balance_callback();
sched_free_extrapad();
}
Expand Down Expand Up @@ -536,30 +520,6 @@ static void unregister_plugsched_enable(void)
kobject_put(plugsched_dir);
}

static int register_tainted_functions(void)
{
struct tainted_function *tf;

for (tf = tainted_functions; tf->name; tf++) {
tf->kobj = kobject_create_and_add(tf->name, vmlinux_moddir);
if (!tf->kobj)
return -ENOMEM;
}

return 0;
}

static void unregister_tainted_functions(void)
{
struct tainted_function *tf;

for (tf = tainted_functions; tf->name; tf++) {
if (!tf->kobj)
return;
kobject_put(tf->kobj);
}
}

static inline void unregister_plugsched_sysfs(void)
{
unregister_tainted_functions();
Expand All @@ -573,7 +533,7 @@ static int register_plugsched_sysfs(void)
return -ENOMEM;
}

if (register_tainted_functions()) {
if (register_tainted_functions(vmlinux_moddir)) {
printk("scheduler: Error: Register taint functions failed!\n");
unregister_plugsched_sysfs();
return -ENOMEM;
Expand Down
Loading