Commit 71dd77fd authored by Ivan Khoronzhuk's avatar Ivan Khoronzhuk Committed by Daniel Borkmann

libbpf: use LFS (_FILE_OFFSET_BITS) instead of direct mmap2 syscall

Drop __NR_mmap2 fork in flavor of LFS, that is _FILE_OFFSET_BITS=64
(glibc & bionic) / LARGEFILE64_SOURCE (for musl) decision. It allows
mmap() to use 64bit offset that is passed to mmap2 syscall. As result
pgoff is not truncated and no need to use direct access to mmap2 for
32 bits systems.
Signed-off-by: default avatarIvan Khoronzhuk <ivan.khoronzhuk@linaro.org>
Acked-by: default avatarYonghong Song <yhs@fb.com>
Signed-off-by: default avatarDaniel Borkmann <daniel@iogearbox.net>
parent 51746f94
...@@ -108,6 +108,7 @@ override CFLAGS += -Werror -Wall ...@@ -108,6 +108,7 @@ override CFLAGS += -Werror -Wall
override CFLAGS += -fPIC override CFLAGS += -fPIC
override CFLAGS += $(INCLUDES) override CFLAGS += $(INCLUDES)
override CFLAGS += -fvisibility=hidden override CFLAGS += -fvisibility=hidden
override CFLAGS += -D_LARGEFILE64_SOURCE -D_FILE_OFFSET_BITS=64
ifeq ($(VERBOSE),1) ifeq ($(VERBOSE),1)
Q = Q =
......
...@@ -74,23 +74,6 @@ struct xsk_nl_info { ...@@ -74,23 +74,6 @@ struct xsk_nl_info {
int fd; int fd;
}; };
/* For 32-bit systems, we need to use mmap2 as the offsets are 64-bit.
* Unfortunately, it is not part of glibc.
*/
static inline void *xsk_mmap(void *addr, size_t length, int prot, int flags,
int fd, __u64 offset)
{
#ifdef __NR_mmap2
unsigned int page_shift = __builtin_ffs(getpagesize()) - 1;
long ret = syscall(__NR_mmap2, addr, length, prot, flags, fd,
(off_t)(offset >> page_shift));
return (void *)ret;
#else
return mmap(addr, length, prot, flags, fd, offset);
#endif
}
int xsk_umem__fd(const struct xsk_umem *umem) int xsk_umem__fd(const struct xsk_umem *umem)
{ {
return umem ? umem->fd : -EINVAL; return umem ? umem->fd : -EINVAL;
...@@ -210,10 +193,9 @@ int xsk_umem__create(struct xsk_umem **umem_ptr, void *umem_area, __u64 size, ...@@ -210,10 +193,9 @@ int xsk_umem__create(struct xsk_umem **umem_ptr, void *umem_area, __u64 size,
goto out_socket; goto out_socket;
} }
map = xsk_mmap(NULL, off.fr.desc + map = mmap(NULL, off.fr.desc + umem->config.fill_size * sizeof(__u64),
umem->config.fill_size * sizeof(__u64), PROT_READ | PROT_WRITE, MAP_SHARED | MAP_POPULATE, umem->fd,
PROT_READ | PROT_WRITE, MAP_SHARED | MAP_POPULATE, XDP_UMEM_PGOFF_FILL_RING);
umem->fd, XDP_UMEM_PGOFF_FILL_RING);
if (map == MAP_FAILED) { if (map == MAP_FAILED) {
err = -errno; err = -errno;
goto out_socket; goto out_socket;
...@@ -228,10 +210,9 @@ int xsk_umem__create(struct xsk_umem **umem_ptr, void *umem_area, __u64 size, ...@@ -228,10 +210,9 @@ int xsk_umem__create(struct xsk_umem **umem_ptr, void *umem_area, __u64 size,
fill->ring = map + off.fr.desc; fill->ring = map + off.fr.desc;
fill->cached_cons = umem->config.fill_size; fill->cached_cons = umem->config.fill_size;
map = xsk_mmap(NULL, map = mmap(NULL, off.cr.desc + umem->config.comp_size * sizeof(__u64),
off.cr.desc + umem->config.comp_size * sizeof(__u64), PROT_READ | PROT_WRITE, MAP_SHARED | MAP_POPULATE, umem->fd,
PROT_READ | PROT_WRITE, MAP_SHARED | MAP_POPULATE, XDP_UMEM_PGOFF_COMPLETION_RING);
umem->fd, XDP_UMEM_PGOFF_COMPLETION_RING);
if (map == MAP_FAILED) { if (map == MAP_FAILED) {
err = -errno; err = -errno;
goto out_mmap; goto out_mmap;
...@@ -552,11 +533,10 @@ int xsk_socket__create(struct xsk_socket **xsk_ptr, const char *ifname, ...@@ -552,11 +533,10 @@ int xsk_socket__create(struct xsk_socket **xsk_ptr, const char *ifname,
} }
if (rx) { if (rx) {
rx_map = xsk_mmap(NULL, off.rx.desc + rx_map = mmap(NULL, off.rx.desc +
xsk->config.rx_size * sizeof(struct xdp_desc), xsk->config.rx_size * sizeof(struct xdp_desc),
PROT_READ | PROT_WRITE, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_POPULATE,
MAP_SHARED | MAP_POPULATE, xsk->fd, XDP_PGOFF_RX_RING);
xsk->fd, XDP_PGOFF_RX_RING);
if (rx_map == MAP_FAILED) { if (rx_map == MAP_FAILED) {
err = -errno; err = -errno;
goto out_socket; goto out_socket;
...@@ -572,11 +552,10 @@ int xsk_socket__create(struct xsk_socket **xsk_ptr, const char *ifname, ...@@ -572,11 +552,10 @@ int xsk_socket__create(struct xsk_socket **xsk_ptr, const char *ifname,
xsk->rx = rx; xsk->rx = rx;
if (tx) { if (tx) {
tx_map = xsk_mmap(NULL, off.tx.desc + tx_map = mmap(NULL, off.tx.desc +
xsk->config.tx_size * sizeof(struct xdp_desc), xsk->config.tx_size * sizeof(struct xdp_desc),
PROT_READ | PROT_WRITE, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_POPULATE,
MAP_SHARED | MAP_POPULATE, xsk->fd, XDP_PGOFF_TX_RING);
xsk->fd, XDP_PGOFF_TX_RING);
if (tx_map == MAP_FAILED) { if (tx_map == MAP_FAILED) {
err = -errno; err = -errno;
goto out_mmap_rx; goto out_mmap_rx;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment