最新的 container_of 宏中 __mptr 的用途是什么?

What is the purpose of __mptr in latest container_of macro?

我正在阅读 linux 内核 5.17.5,现在正在查看 container_of() 宏。

/**
 * container_of - cast a member of a structure out to the containing structure
 * @ptr:    the pointer to the member.
 * @type:   the type of the container struct this is embedded in.
 * @member: the name of the member within the struct.
 *
 */
#define container_of(ptr, type, member) ({              \
    void *__mptr = (void *)(ptr);                   \
    static_assert(__same_type(*(ptr), ((type *)0)->member) ||   \
              __same_type(*(ptr), void),            \
              "pointer type mismatch in container_of()");   \
    ((type *)(__mptr - offsetof(type, member))); })

我的问题很简单:__mptr 的目的是什么,我可以像这样用 (void *)ptr 替换 __mptr 吗?

#define container_of(ptr, type, member) ({          \
    static_assert(__same_type(*(ptr), ((type *)0)->member) ||   \
              __same_type(*(ptr), void),            \
              "pointer type mismatch in container_of()");   \
    ((type *)((void*)ptr - offsetof(type, member))); })

因为 “kernel.h: 在 container_of()” 中更好地处理指向数组的指针,__mptr 不用于类型检查了。它用于消除scripts/gcc-plugins/randomize_layout_plugin.c的信息性消息,如inform(gimple_location(stmt), "found mismatched ssa struct pointer types: %qT and %qT\n", ptr_lhs_type, ptr_rhs_type);。该文件的其中一项工作是:

/*
 * iterate over all statements to find "bad" casts:
 * those where the address of the start of a structure is cast
 * to a pointer of a structure of a different type, or a
 * structure pointer type is cast to a different structure pointer type
 */

如果缺少 __mptr,宏将包含您所说的代码:

(type *)((void*)ptr - offsetof(type, member)))

(PS:char *这里比较好,因为iso c标准保证对offsetof是1个字节,void *是gnu c保证的仅)

如果 offsetof 为零,并且 ptr 将包含结构类型 member 的起始地址,那么这将被转换为完全不同的类型:struct type *。此表单根据 scripts/gcc-plugins/randomize_layout_plugin.c 出现故障,将被它检测到。

随着 void *__mptr = (void *)(ptr); 的引入,编译器不再知道 __mptr 的类型,所以 scripts/gcc-plugins/randomize_layout_plugin.c 在转换 void * 时不会报错 __mptr(type *) 您可以从https://lkml.org/lkml/2017/6/20/873

查看真实案例和相关修复补丁

__mptr 用于类型检查时,以下是 4.13 之前内核的原始答案:

让我们简化 container_of 并查看此案例:

#include <stdio.h>
#include <stddef.h>
#define container_of(ptr, type, member) ({          \
    const typeof( ((type *)0)->member ) *__mptr = (ptr);    \
    (type *)( (char *)__mptr - offsetof(type,member) );})
#define container_of_without_typechecking(ptr, type, member) ({         \
    (type *)( (char *)ptr - offsetof(type,member) );})
struct Foo
{
    int a;
};
int main(int argc, char *argv[]) {
    struct Foo foo;
    int *a;

    printf("foo addr: %p\n", &foo);
    a = &foo.a;
    printf("container of a: %p\n", container_of_without_typechecking((unsigned long long*)a, struct Foo, a));
    printf("typecheck: container of a: %p\n", container_of((unsigned long long*)a, struct Foo, a));
    return 0;
}

container_of_without_typechecking 没有 __mptrcontainer_of 有。

编译时:

a.c: In function ‘main’:
a.c:13:55: warning: initialization of ‘const int *’ from incompatible pointer type ‘long long unsigned int *’ [-Wincompatible-pointer-types]
   13 |         const typeof( ((type *)0)->member ) *__mptr = (ptr);    \
      |                                                       ^
a.c:28:47: note: in expansion of macro ‘container_of’
   28 |     printf("typecheck: container of a: %p\n", container_of((unsigned long long*)a, struct Foo, a));
      |                                               ^~~~~~~~~~~~

如您所见,container_of 会抛出 incompatible pointer type 警告,而 container_of_without_typechecking 不会,因此它仅用于类型检查。

此外,请注意 Linux 内核将此警告视为错误:

KBUILD_CFLAGS   += $(call cc-option,-Werror=incompatible-pointer-types)

因此,如果您将错误的类型传递给 container_of,您将收到 错误 而不是警告。