简体   繁体   English

跨函数调用的指针值变化

[英]Pointer value changing across function call

I have the following structures in the kernel我在内核中有以下结构

struct state {
    /* Current algorithm iteration */
    int tune_id;
    /* Thread id */
    pid_t tid;
#ifndef __KERNEL__
    /* Paths */
    char *stats_path;
    char *budget_path;
    char *controller_path;
#endif  /* __KERNEL__ */
    int budget;
    /* Stats */
    struct statistics prev_stats;
    struct parameters current_params;
    u64 cur_time;
    /* Algorithm specific data */
    void *data;
};

struct tuning {
    struct algorithm *algorithm;
    struct state *state;
    struct energy energy;
};

I've defined a function tune() as follows:我定义了一个函数tune()如下:

void tune(struct task_struct *task) {
    struct statistics stats;
    struct state *state;
    get_current_stats(&stats);
    state = task->tuning.state;

    get_current_params(&state->current_params);
    compute_energy(&stats, state);
}

The other functions are defined as:其他函数定义为:

void get_current_params(struct parameters *params)
{
    printk(KERN_DEBUG "get_current_params: parameters:0x%X\n", (unsigned int) params);
    params->cpu_frequency_MHZ = (cpufreq_get(0) + 500) / 1000;
    params->mem_frequency_MHZ = (memfreq_get() + 500) / 1000;
}

void compute_energy(struct statistics *stats, struct state *state)
{
    struct statistics *diffs;
    struct frontier *frontier;
    u64 energy_budget;
    int threshold;

    int i,j;
    struct configuration s;
    struct configuration emin;

#ifdef TIMING
    u64 ns;
    ns = get_thread_time();
 #endif

#ifdef DEBUG
#ifdef __KERNEL__
    printk(KERN_DEBUG "compute_energy: parameters:0x%X\n", (unsigned int) &state->current_params);
#endif  /* __KERNEL__ */
#endif
}

When I call tune() , the output is as follows:当我调用tune() ,输出如下:

[    7.160139] get_current_params: parameters:0xBF396BA0
[    7.160298] compute_energy: parameters:0xBF396B98

I don't understand why the addresses differ by 0x8 .我不明白为什么地址相差0x8 This in turn causes a divide by 0 exception in the kernel since the struct parameters seems to have values of 0 instead of what was initialized by get_current_params这反过来导致内核中的除以 0 异常,因为struct parameters值似乎为0而不是由get_current_params初始化的值

Why is it that the address of the member current_params of struct state changes across function calls?为什么struct state的成员current_params的地址跨函数调用会发生变化?

Update :更新
I've verified that this bug only occurs for PID 0.我已经验证此错误仅发生在 PID 0 上。
Looking at include/linux/init_task.h , I see that PID 0 is statically initialized.查看include/linux/init_task.h ,我看到 PID 0 是静态初始化的。 This is the only difference I could find between PID 0 and the other tasks.这是我在 PID 0 和其他任务之间找到的唯一区别。 Could this somehow be responsible for the issue I'm having?这可能以某种方式对我遇到的问题负责吗?

For what I can see, you are right in that both addresses should be the same.就我所见,您是对的,两个地址应该相同。 So there can only be one option: task information changes in the kernel in the meanwhile.所以只能有一个选择:同时内核中的任务信息发生变化。

Considering this snippet of your code:考虑到您的代码片段:

void tune(struct task_struct *task) {
    ...
    struct state *state;
    ...
    state = task->tuning.state;

You are managing two structs over which you may have no control (you should check that):您正在管理两个您可能无法控制的结构(您应该检查一下):

(*task): struct task_struct

and

(*task->tuning.state): struct state

So when in tune() you call所以当你调用tune()

get_current_params(&state->current_params);
compute_energy(&stats, state);

something could happen between both printk functions, so there is where I think you have to put your focus in. Try saving task->tuning.state before the call to get_current_params() so you could check that it continues to be the same value after call to compute_energy() .两个printk函数之间可能会发生一些事情,所以我认为你必须把重点放在那里。尝试在调用get_current_params()之前保存task->tuning.state ,这样你就可以检查它在之后是否继续是相同的值调用compute_energy()

Hope this helps.希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM