mirror of
https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git
synced 2025-12-22 12:17:45 +01:00
Reimplement k[v]realloc_node() to be able to set node and alignment should a user need to do so. In order to do that while retaining the maximal backward compatibility, add k[v]realloc_node_align() functions and redefine the rest of API using these new ones. While doing that, we also keep the number of _noprof variants to a minimum, which implies some changes to the existing users of older _noprof functions, that basically being bcachefs. With that change we also provide the ability for the Rust part of the kernel to set node and alignment in its K[v]xxx [re]allocations. Link: https://lkml.kernel.org/r/20250806124147.1724658-1-vitaly.wool@konsulko.se Signed-off-by: Vitaly Wool <vitaly.wool@konsulko.se> Reviewed-by: Vlastimil Babka <vbabka@suse.cz> Cc: Alice Ryhl <aliceryhl@google.com> Cc: Danilo Krummrich <dakr@kernel.org> Cc: Herbert Xu <herbert@gondor.apana.org.au> Cc: Jann Horn <jannh@google.com> Cc: Kent Overstreet <kent.overstreet@linux.dev> Cc: Liam Howlett <liam.howlett@oracle.com> Cc: Lorenzo Stoakes <lorenzo.stoakes@oracle.com> Cc: Uladzislau Rezki (Sony) <urezki@gmail.com> Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
90 lines
2.1 KiB
C
90 lines
2.1 KiB
C
/* SPDX-License-Identifier: GPL-2.0-only */
|
|
/* A pointer that can point to either kernel or userspace memory. */
|
|
#ifndef _LINUX_BPFPTR_H
|
|
#define _LINUX_BPFPTR_H
|
|
|
|
#include <linux/mm.h>
|
|
#include <linux/sockptr.h>
|
|
|
|
typedef sockptr_t bpfptr_t;
|
|
|
|
static inline bool bpfptr_is_kernel(bpfptr_t bpfptr)
|
|
{
|
|
return bpfptr.is_kernel;
|
|
}
|
|
|
|
static inline bpfptr_t KERNEL_BPFPTR(void *p)
|
|
{
|
|
return (bpfptr_t) { .kernel = p, .is_kernel = true };
|
|
}
|
|
|
|
static inline bpfptr_t USER_BPFPTR(void __user *p)
|
|
{
|
|
return (bpfptr_t) { .user = p };
|
|
}
|
|
|
|
static inline bpfptr_t make_bpfptr(u64 addr, bool is_kernel)
|
|
{
|
|
if (is_kernel)
|
|
return KERNEL_BPFPTR((void*) (uintptr_t) addr);
|
|
else
|
|
return USER_BPFPTR(u64_to_user_ptr(addr));
|
|
}
|
|
|
|
static inline bool bpfptr_is_null(bpfptr_t bpfptr)
|
|
{
|
|
if (bpfptr_is_kernel(bpfptr))
|
|
return !bpfptr.kernel;
|
|
return !bpfptr.user;
|
|
}
|
|
|
|
static inline void bpfptr_add(bpfptr_t *bpfptr, size_t val)
|
|
{
|
|
if (bpfptr_is_kernel(*bpfptr))
|
|
bpfptr->kernel += val;
|
|
else
|
|
bpfptr->user += val;
|
|
}
|
|
|
|
static inline int copy_from_bpfptr_offset(void *dst, bpfptr_t src,
|
|
size_t offset, size_t size)
|
|
{
|
|
if (!bpfptr_is_kernel(src))
|
|
return copy_from_user(dst, src.user + offset, size);
|
|
return copy_from_kernel_nofault(dst, src.kernel + offset, size);
|
|
}
|
|
|
|
static inline int copy_from_bpfptr(void *dst, bpfptr_t src, size_t size)
|
|
{
|
|
return copy_from_bpfptr_offset(dst, src, 0, size);
|
|
}
|
|
|
|
static inline int copy_to_bpfptr_offset(bpfptr_t dst, size_t offset,
|
|
const void *src, size_t size)
|
|
{
|
|
return copy_to_sockptr_offset((sockptr_t) dst, offset, src, size);
|
|
}
|
|
|
|
static inline void *kvmemdup_bpfptr_noprof(bpfptr_t src, size_t len)
|
|
{
|
|
void *p = kvmalloc_node_align_noprof(len, 1, GFP_USER | __GFP_NOWARN, NUMA_NO_NODE);
|
|
|
|
if (!p)
|
|
return ERR_PTR(-ENOMEM);
|
|
if (copy_from_bpfptr(p, src, len)) {
|
|
kvfree(p);
|
|
return ERR_PTR(-EFAULT);
|
|
}
|
|
return p;
|
|
}
|
|
#define kvmemdup_bpfptr(...) alloc_hooks(kvmemdup_bpfptr_noprof(__VA_ARGS__))
|
|
|
|
static inline long strncpy_from_bpfptr(char *dst, bpfptr_t src, size_t count)
|
|
{
|
|
if (bpfptr_is_kernel(src))
|
|
return strncpy_from_kernel_nofault(dst, src.kernel, count);
|
|
return strncpy_from_user(dst, src.user, count);
|
|
}
|
|
|
|
#endif /* _LINUX_BPFPTR_H */
|