Skip to content

Commit

Permalink
[cgroupv2_freeze] 优化 MIUI 内核
Browse files Browse the repository at this point in the history
  • Loading branch information
lzghzr committed Aug 18, 2024
1 parent d583617 commit 1de221e
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 25 deletions.
2 changes: 1 addition & 1 deletion cgroupv2_freeze/Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
MYKPM_VERSION := 1.0.9
MYKPM_VERSION := 1.0.10

ifndef KP_DIR
KP_DIR = ../KernelPatch
Expand Down
2 changes: 2 additions & 0 deletions cgroupv2_freeze/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
为低版本内核添加 cgroup.freeze

## 更新记录
### 1.0.10
优化 `MIUI` 内核
### 1.0.9
v2 检测函数由 `do_freezer_trap` 改为 `cgroup_freeze_write`
### 1.0.8
Expand Down
80 changes: 56 additions & 24 deletions cgroupv2_freeze/cgroupv2_freeze.c
Original file line number Diff line number Diff line change
Expand Up @@ -375,20 +375,27 @@ static void do_filp_open_after(hook_fargs3_t* args, void* udata) {
struct filename* pathname = (struct filename*)args->arg1;
if (!memcmp(pathname->name, apm, sizeof(apm) - 1)) {
char* cmd[] = {
"if [ ! -d \"/sys/fs/cgroup/uid_0\" ]; then\
umount /sys/fs/cgroup/freezer;\
umount /sys/fs/cgroup;\
\
chown system:system /sys/fs/cgroup/;\
mount -t cgroup -o nosuid,nodev,noexec,cpuacct none /sys/fs/cgroup/;\
\
mkdir /sys/fs/cgroup/frozen/;\
chown -R system:system /sys/fs/cgroup/frozen/;\
echo 1 > /sys/fs/cgroup/frozen/cgroup.freeze;\
\
mkdir /sys/fs/cgroup/unfrozen/;\
chown -R system:system /sys/fs/cgroup/unfrozen/;\
fi",
"if [ ! -d \"/sys/fs/cgroup/uid_0\" ]; then\
umount /sys/fs/cgroup/freezer;\
umount /sys/fs/cgroup;\
\
chown system:system /sys/fs/cgroup/;\
\
if [ -d \"/dev/cg2_bpf/uid_0\" ]; then\
mount -t cgroup2 none /sys/fs/cgroup/;\
elif [ -d \"/acct/uid_0\" ]; then\
mount -t cgroup -o cpuacct none /sys/fs/cgroup/;\
else\
exit;\
fi;\
\
mkdir /sys/fs/cgroup/frozen/;\
chown -R system:system /sys/fs/cgroup/frozen/;\
echo 1 > /sys/fs/cgroup/frozen/cgroup.freeze;\
\
mkdir /sys/fs/cgroup/unfrozen/;\
chown -R system:system /sys/fs/cgroup/unfrozen/;\
fi",
NULL
};
run_cmd(cmd);
Expand Down Expand Up @@ -648,21 +655,46 @@ static long calculate_offsets() {
}
// 获取 css_set->dfl_cgrp
void (*link_css_set)(struct list_head* tmp_links, struct css_set* cset, struct cgroup* cgrp);
lookup_name(link_css_set);
link_css_set = (typeof(link_css_set))kallsyms_lookup_name("link_css_set");

uint32_t* link_css_set_src = (uint32_t*)link_css_set;
for (u32 i = 0; i < 0x20; i++) {
if (link_css_set) {
uint32_t* link_css_set_src = (uint32_t*)link_css_set;
for (u32 i = 0; i < 0x20; i++) {
#ifdef CONFIG_DEBUG
logkm("link_css_set %x %llx\n", i, link_css_set_src[i]);
logkm("link_css_set %x %llx\n", i, link_css_set_src[i]);
#endif /* CONFIG_DEBUG */
if (link_css_set_src[i] == ARM64_RET) {
break;
} else if ((link_css_set_src[i] & MASK_STR_64) == INST_STR_64) {
uint64_t imm12 = bits32(link_css_set_src[i], 21, 10);
css_set_dfl_cgrp_offset = sign64_extend((imm12 << 0b11u), 16u);
break;
if (link_css_set_src[i] == ARM64_RET) {
break;
} else if ((link_css_set_src[i] & MASK_STR_64) == INST_STR_64) {
uint64_t imm12 = bits32(link_css_set_src[i], 21, 10);
css_set_dfl_cgrp_offset = sign64_extend((imm12 << 0b11u), 16u);
break;
}
}
} else {
unsigned long long (*bpf_get_current_cgroup_id)(void);
bpf_get_current_cgroup_id = (typeof(bpf_get_current_cgroup_id))kallsyms_lookup_name("bpf_get_current_cgroup_id");

if (bpf_get_current_cgroup_id) {
uint32_t* bpf_get_current_cgroup_id_src = (uint32_t*)bpf_get_current_cgroup_id;
for (u32 i = 0; i < 0x10; i++) {
#ifdef CONFIG_DEBUG
logkm("bpf_get_current_cgroup_id %x %llx\n", i, bpf_get_current_cgroup_id_src[i]);
#endif /* CONFIG_DEBUG */
if (bpf_get_current_cgroup_id_src[i] == ARM64_RET) {
break;
} else if ((bpf_get_current_cgroup_id_src[i] & MASK_LDR_64_) == INST_LDR_64_) {
uint64_t imm12 = bits32(bpf_get_current_cgroup_id_src[i], 21, 10);
uint64_t offset = sign64_extend((imm12 << 0b11u), 16u);
if (offset < 0x100) {
css_set_dfl_cgrp_offset = offset;
break;
}
}
}
}
}

#ifdef CONFIG_DEBUG
logkm("css_set_dfl_cgrp_offset=0x%llx\n", css_set_dfl_cgrp_offset);
#endif /* CONFIG_DEBUG */
Expand Down

0 comments on commit 1de221e

Please sign in to comment.