内核(Android)到用户空间消息多播错误:netlink_broadcast_filtered + 0x24 / 0x3d4

时间:2019-04-26 15:20:02

标签: android c linux-kernel scheduler netlink

我正在尝试开发一个功能,该功能将使内核(Android内核:4.9.59)能够向一个/许多用户空间应用程序发送消息,并且我遵循了以下示例:Kernel to userspace application communication

但是,当我尝试从调度程序调用消息传送功能时,出现以下错误:

  

致命异常

     

PC位于netlink_broadcast_filtered + 0x24 / 0x3d4

     

LR位于netlink_broadcast + 0x14 / 0x20

在内核调度程序(内核/调度程序)内部,我创建了一个标头(custom_code.h),该标头包含用于从内核发送消息的功能。我不使用内核模块来注入它,因为Android不支持模块。 custom_code.h中的代码如下:

#include <linux/sched.h>
#include <net/sock.h>
#include <linux/netlink.h>
#include <linux/skbuff.h>
#include <linux/string.h>

#define MY_GROUP    1 //For netlink socket
struct sock* socket; //For netlink socket
struct sk_buff* socket_buff; //For netlink socket

static inline void nl_receive_callback (struct sk_buff *skb)
{
    nlmsg_free(skb);
}

struct netlink_kernel_cfg cfg = {
    .input  = nl_receive_callback,
    .groups = 1,
};

static inline int kernel_send_nl_msg(void)
{
    struct nlmsghdr *nlsk_mh;
    char* msg = "hello from kernel";

    socket = netlink_kernel_create(&init_net, NETLINK_USERSOCK, &cfg);

    socket_buff = nlmsg_new(256, GFP_KERNEL);
    nlsk_mh = nlmsg_put(socket_buff, 0, 0, NLMSG_DONE, strlen(msg), 0);
    //NETLINK_CB(socket_buff).pid = 0;    // kernel pid pid is deprecated
    NETLINK_CB(socket_buff).portid = 0;
    NETLINK_CB(socket_buff).dst_group = MY_GROUP;
    strcpy(nlmsg_data(nlsk_mh), msg);

    nlmsg_multicast(socket, socket_buff, 0, MY_GROUP, GFP_KERNEL);
    pr_info("%s", msg);//Print out the message to kernel

    return 0;
}

我正在从cpufreq_schedutil.c(kernel / sched / cpufreq_schedutil.c)内的sugov_start(struct cpufreq_policy * policy)函数调用kernel_send_nl_msg,并且已经构建了整个内核并将其刷新到Android设备上。

我在kernel / sched / cpufreq_schedutil.c中修改的代码如下:

static int sugov_start(struct cpufreq_policy *policy)
{
    struct sugov_policy *sg_policy = policy->governor_data;
    unsigned int cpu;

    sg_policy->up_rate_delay_ns =
        sg_policy->tunables->up_rate_limit_us * NSEC_PER_USEC;
    sg_policy->down_rate_delay_ns =
        sg_policy->tunables->down_rate_limit_us * NSEC_PER_USEC;
    update_min_rate_limit_us(sg_policy);
    sg_policy->last_freq_update_time = 0;
    sg_policy->next_freq = UINT_MAX;
    sg_policy->work_in_progress = false;
    sg_policy->need_freq_update = false;
    sg_policy->cached_raw_freq = 0;

    for_each_cpu(cpu, policy->cpus) {
        struct sugov_cpu *sg_cpu = &per_cpu(sugov_cpu, cpu);

        memset(sg_cpu, 0, sizeof(*sg_cpu));
        sg_cpu->sg_policy = sg_policy;
        sg_cpu->flags = 0;
        sugov_start_slack(cpu);
        sg_cpu->iowait_boost_max = policy->cpuinfo.max_freq;
        cpufreq_add_update_util_hook(cpu, &sg_cpu->update_util,
                         policy_is_shared(policy) ?
                            sugov_update_shared :
                            sugov_update_single);
    }
        if(kernel_send_nl_msg() != 0)
            pr_info("Error sending message from Kernel using socket");

    return 0;
}

将内核映像刷新到设备并尝试引导后,设备会传达上述错误(即使没有引导)。

我对错误的疑问如下:

1)导致该错误的原因可能是什么?

2)如何将消息从Android内核成功发送到一个/许多用户空间应用程序?任何建议都会很有帮助。

0 个答案:

没有答案