如何找到 64 位 Linux 内核的 task_size?

ano*_*ous 2 linux 64-bit linux-kernel

我想知道为内核保留了多少空间,为用户进程保留了多少空间。

deb*_*bug 5

TASK_SIZE定义在https://elixir.bootlin.com/linux/v4.16.7/source/arch/arm/include/asm/memory.h#L40中,可以通过以下代码访问。请将其编译为内核模块。


任务大小.c

#include <linux/module.h>
#include <linux/memory.h>
#include <linux/init.h>

MODULE_LICENSE("GPL");
MODULE_AUTHOR("Open Source");
MODULE_DESCRIPTION("Module for TASK_SIZE");

static int __init find_task_size_init(void);
static void __exit find_task_size_exit(void);

static int
__init find_task_size_init(void)
{
    printk(KERN_INFO "Memory TASK_SIZE: 0x%lx\n", TASK_SIZE);
    return 0;
}

static void
__exit find_task_size_exit(void)
{
    printk(KERN_INFO "module exits ok !\n");
}

module_init(find_task_size_init);
module_exit(find_task_size_exit);
Run Code Online (Sandbox Code Playgroud)

生成文件

ifneq ($(KERNELRELEASE),)
    # call from kernel build system
    lifo-objs := main.o
    obj-m     := TASK_SIZE.o
else
    KERNELDIR ?= /lib/modules/$(shell uname -r)/build
    PWD       := $(shell pwd)
modules:
    echo $(MAKE) -C $(KERNELDIR) M=$(PWD) LDDINC=$(PWD)/../include modules
    $(MAKE) -C $(KERNELDIR) M=$(PWD) LDDINC=$(PWD)/../include modules
endif

clean:  
    rm -rf *.o *~ core .depend *.mod.o .*.cmd *.ko *.mod.c .tmp_versions *.markers *.symvers modules.order

depend .depend dep:
    $(CC) $(CFLAGS) -M *.c > .depend

ifeq (.depend,$(wildcard .depend))
    include .depend
endif
Run Code Online (Sandbox Code Playgroud)
# dmesg -k -w          # In the first console shell

[ 2570.156990] Memory TASK_SIZE: 0x7ffffffff000
[ 2573.164671] module exits ok !
Run Code Online (Sandbox Code Playgroud)
# make                 # In the second console shell
# insmod TASK_SIZE.ko
# rmmod TASK_SIZE
Run Code Online (Sandbox Code Playgroud)