获取空指针的容器

Getting container of a void pointer

我目前正在尝试摆脱以下功能所需的反向引用:

/**
 * amd_sfh_hid_poll - Updates the input report for a HID device.
 * @work:   The delayed work
 *
 * Polls input reports from the respective HID devices and submits
 * them by invoking hid_input_report() from hid-core.
 */
static void amd_sfh_hid_poll(struct work_struct *work)
{
    struct amd_sfh_hid_data *hid_data;
    struct hid_device *hid;
    size_t size;
    u8 *buf;

    hid_data = container_of(work, struct amd_sfh_hid_data, work.work);
    hid = hid_data->hid;
    size = get_descriptor_size(hid_data->sensor_idx, AMD_SFH_INPUT_REPORT);

    buf = kzalloc(size, GFP_KERNEL);
    if (!buf) {
        hid_err(hid, "Failed to allocate memory for input report!\n");
        goto reschedule;
    }

    size = get_input_report(hid_data->sensor_idx, 1, buf, size,
                            hid_data->cpu_addr);
    if (size < 0) {
        hid_err(hid, "Failed to get input report!\n");
        goto free_buf;
    }

    hid_input_report(hid, HID_INPUT_REPORT, buf, size, 0);

free_buf:
    kfree(buf);
reschedule:
    schedule_delayed_work(&hid_data->work, hid_data->interval);
}

struct amd_sfh_hid_datahid->driver_data下存储的驱动数据 struct hid_device。在工作队列中,我需要访问 HID 的驱动程序数据和 HID 设备,我目前通过访问 backref hid = hid_data->hid 来完成。 我现在尝试通过在驱动程序数据上使用 container_of 来摆脱 backref:

hid = container_of((void*)hid_data, struct hid_device, driver_data);

但这会导致页面错误,

hid = container_of((void**)*hid_data, struct hid_device, driver_data);

给定其成员 void *driver_data 使用 container_of 获得 struct hid_device * 的正确方法是什么?

What is the correct way to get a struct hid_device * given its member void *driver_data using container_of?

不行。

使用 container_of 意味着您有一个 指向成员 的指针,而不是成员的 ,因为在你的情况下。

简短示例:

struct B;
struct C;

struct A {
   struct B field1;
   struct C* field2;
}

有一个指向 field1 指针 ,使用 container_of 你可能会得到一个指向整个 struct A 对象的指针。

具有 field2value(即使它具有类型 struct C*,即指针),您无法获得指向 struct A对象。