将 TASK_COMM_LEN Linux 内核限制增加到 pthread_setname_np

increasing TASK_COMM_LEN Linux kernel limit to pthread_setname_np

出于调试目的,我使用 Linux 内核的 pthread_setname_np(3) and pthread_getname_np. The name passed to them is limited by TASK_COMM_LEN (see this) which is #define-d to be 16 bytes in include/linux/sched.h。我想稍微增加 pthread_setname_np 的限制,例如到 24,这主要是为了方便(所以这对我来说不是什么大问题)。

(这只是为了调试和方便,我不愿意花一个多小时在上面;我可以不增加TASK_COMM_LEN,而且我有足够的 RAM - 32 GB - 所以每个进程破坏一个额外的 4K 页对我来说不是问题)

我正在编译最新的稳定版 Linux kernel,今天(2018 年 2 月 10 日)4.15.2 Debian/Sid/x86-64。 FWIW,我的 gcc 是版本 7.3.0

我刚刚在 include/linux/sched.h 中将第 167 行更改为

 #define TASK_COMM_LEN          24 /*was 16*/

使用 make deb-pkg 编译该内核时出现错误:

  CC      drivers/connector/cn_proc.o
In file included from ./include/linux/kernel.h:10:0,
                 from drivers/connector/cn_proc.c:25:
drivers/connector/cn_proc.c: In function ‘proc_comm_connector’:
./include/linux/compiler.h:324:38: error: call to ‘__compiletime_assert_240’ declared with attribute error: BUILD_BUG_ON failed: sizeof(ev->event_data.comm.comm) != TASK_COMM_LEN
  _compiletime_assert(condition, msg, __compiletime_assert_, __LINE__)
                                      ^
./include/linux/compiler.h:304:4: note: in definition of macro ‘__compiletime_assert’
    prefix ## suffix();    \
    ^~~~~~
./include/linux/compiler.h:324:2: note: in expansion of macro ‘_compiletime_assert’
  _compiletime_assert(condition, msg, __compiletime_assert_, __LINE__)
  ^~~~~~~~~~~~~~~~~~~
./include/linux/build_bug.h:47:37: note: in expansion of macro ‘compiletime_assert’
 #define BUILD_BUG_ON_MSG(cond, msg) compiletime_assert(!(cond), msg)
                                     ^~~~~~~~~~~~~~~~~~
./include/linux/build_bug.h:71:2: note: in expansion of macro ‘BUILD_BUG_ON_MSG’
  BUILD_BUG_ON_MSG(condition, "BUILD_BUG_ON failed: " #condition)
  ^~~~~~~~~~~~~~~~
./include/linux/sched.h:1497:2: note: in expansion of macro ‘BUILD_BUG_ON’
  BUILD_BUG_ON(sizeof(buf) != TASK_COMM_LEN); \
  ^~~~~~~~~~~~
drivers/connector/cn_proc.c:240:2: note: in expansion of macro ‘get_task_comm’
  get_task_comm(ev->event_data.comm.comm, task);
  ^~~~~~~~~~~~~
scripts/Makefile.build:316: recipe for target 'drivers/connector/cn_proc.o' failed
make[4]: *** [drivers/connector/cn_proc.o] Error 1
scripts/Makefile.build:575: recipe for target 'drivers/connector' failed
make[3]: *** [drivers/connector] Error 2
Makefile:1018: recipe for target 'drivers' failed
make[2]: *** [drivers] Error 2
scripts/package/Makefile:86: recipe for target 'deb-pkg' failed
make[1]: *** [deb-pkg] Error 2
Makefile:1345: recipe for target 'deb-pkg' failed
make: *** [deb-pkg] Error 2

有什么快速而肮脏的解决方案可以解决这个问题吗?

(我可以使用旧的 16 字节限制来管理 TASK_COMM_LEN;我想提高它只是为了方便。)

换句话说,为什么我必须在不止一个地方更改线程名称的大小?我不确定是否要以一些晦涩的方式破坏内核调度程序....

有问题的缓冲区(struct proc_event 中的event_data.comm.comm)是用户空间 API 和 ABI 的一部分(消息通过 netlink 系列 NETLINK_CONNECTOR 的 netlink 套接字发送) .这就是为什么它明确地将缓冲区大小指定为 16 - TASK_COMM_LEN 在用户空间 API 头文件中不可用,并且 event_data 中的缓冲区大小无论如何都无法更改,因为它是 ABI 的一部分。

您可以尝试在内核配置中禁用 "connector" 驱动程序 - 它位于 "Device Drivers" 菜单中,名称为 "Connector - unified userspace <-> kernelspace linker"。这是提供 NETLINK_CONNECTOR netlink 系列的驱动程序,如果您没有依赖该功能的任何用户空间代码,则不需要。这将防止此特定代码给您带来问题。

但是,您可能还会在其他地方发现类似的 ABI 问题。