2024-06-03 18:27:37

by Carlos Llamas

[permalink] [raw]
Subject: Header conflicts with shmget() and SHM_HUGE_2MB

Hi, I'm trying to figure out how one would use SHM_HUGE_{2MB/1GB}
defines through shmget() from userspace. After having a look at the
man-pages I thought the #include pattern would be similar to that of
mmap(), e.g.:

#include <sys/mman.h>
#include <linux/mman.h>
[...]
mmap(NULL, size, prot, flags | MAP_HUGETLB | MAP_HUGE_2MB,
fd, 0);

However, when doing the shmem equivalent with the headers I get several
redefinition conflicts. When attempting to compile something like this:

#include <sys/shm.h>
#include <linux/shm.h>
[...]
shmid = shmget(key, size, flags | SHM_HUGETLB | SHM_HUGE_2MB);

I run into the following type of issues:

/usr/include/linux/shm.h:26:8: error: redefinition of ‘struct shmid_ds’
26 | struct shmid_ds {
| ^~~~~~~~
In file included from /usr/include/x86_64-linux-gnu/bits/shm.h:45,
from /usr/include/x86_64-linux-gnu/sys/shm.h:30:
/usr/include/x86_64-linux-gnu/bits/types/struct_shmid_ds.h:24:8: note: originally defined here
24 | struct shmid_ds
| ^~~~~~~~

I can see such definitions are tagged as "obsolete" in the uapi headers.
Do we need some ifndef protection with the glibc headers?

What is the advice to follow for userspace? Skip <linux/shm.h> and
openly redefine the SHM_HUGE_* wherever needed?

Thanks,
--
Carlos Llamas


2024-06-03 23:32:19

by Andi Kleen

[permalink] [raw]
Subject: Re: Header conflicts with shmget() and SHM_HUGE_2MB

> I can see such definitions are tagged as "obsolete" in the uapi headers.
> Do we need some ifndef protection with the glibc headers?

They should be still supported, but also the more generic macro.
>
> What is the advice to follow for userspace? Skip <linux/shm.h> and
> openly redefine the SHM_HUGE_* wherever needed?

glibc (or other C libraries if not using linux/shm) should add the
defines. Short term you would need to redefine on your own yes.

-Andi