mirror of
https://github.com/Fishwaldo/Star64_linux.git
synced 2025-06-21 14:11:20 +00:00
Merge branch 'bpf-array-mmap'
Andrii Nakryiko says: ==================== This patch set adds ability to memory-map BPF array maps (single- and multi-element). The primary use case is memory-mapping BPF array maps, created to back global data variables, created by libbpf implicitly. This allows for much better usability, along with avoiding syscalls to read or update data completely. Due to memory-mapping requirements, BPF array map that is supposed to be memory-mapped, has to be created with special BPF_F_MMAPABLE attribute, which triggers slightly different memory allocation strategy internally. See patch 1 for details. Libbpf is extended to detect kernel support for this flag, and if supported, will specify it for all global data maps automatically. Patch #1 refactors bpf_map_inc() and converts bpf_map's refcnt to atomic64_t to make refcounting never fail. Patch #2 does similar refactoring for bpf_prog_add()/bpf_prog_inc(). v5->v6: - add back uref counting (Daniel); v4->v5: - change bpf_prog's refcnt to atomic64_t (Daniel); v3->v4: - add mmap's open() callback to fix refcounting (Johannes); - switch to remap_vmalloc_pages() instead of custom fault handler (Johannes); - converted bpf_map's refcnt/usercnt into atomic64_t; - provide default bpf_map_default_vmops handling open/close properly; v2->v3: - change allocation strategy to avoid extra pointer dereference (Jakub); v1->v2: - fix map lookup code generation for BPF_F_MMAPABLE case; - prevent BPF_F_MMAPABLE flag for all but plain array map type; - centralize ref-counting in generic bpf_map_mmap(); - don't use uref counting (Alexei); - use vfree() directly; - print flags with %x (Song); - extend tests to verify bpf_map_{lookup,update}_elem() logic as well. ==================== Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
This commit is contained in:
commit
b97e12e594
25 changed files with 576 additions and 175 deletions
|
@ -3171,13 +3171,8 @@ static int bnxt_init_one_rx_ring(struct bnxt *bp, int ring_nr)
|
|||
bnxt_init_rxbd_pages(ring, type);
|
||||
|
||||
if (BNXT_RX_PAGE_MODE(bp) && bp->xdp_prog) {
|
||||
rxr->xdp_prog = bpf_prog_add(bp->xdp_prog, 1);
|
||||
if (IS_ERR(rxr->xdp_prog)) {
|
||||
int rc = PTR_ERR(rxr->xdp_prog);
|
||||
|
||||
rxr->xdp_prog = NULL;
|
||||
return rc;
|
||||
}
|
||||
bpf_prog_add(bp->xdp_prog, 1);
|
||||
rxr->xdp_prog = bp->xdp_prog;
|
||||
}
|
||||
prod = rxr->rx_prod;
|
||||
for (i = 0; i < bp->rx_ring_size; i++) {
|
||||
|
|
|
@ -1876,13 +1876,8 @@ static int nicvf_xdp_setup(struct nicvf *nic, struct bpf_prog *prog)
|
|||
|
||||
if (nic->xdp_prog) {
|
||||
/* Attach BPF program */
|
||||
nic->xdp_prog = bpf_prog_add(nic->xdp_prog, nic->rx_queues - 1);
|
||||
if (!IS_ERR(nic->xdp_prog)) {
|
||||
bpf_attached = true;
|
||||
} else {
|
||||
ret = PTR_ERR(nic->xdp_prog);
|
||||
nic->xdp_prog = NULL;
|
||||
}
|
||||
bpf_prog_add(nic->xdp_prog, nic->rx_queues - 1);
|
||||
bpf_attached = true;
|
||||
}
|
||||
|
||||
/* Calculate Tx queues needed for XDP and network stack */
|
||||
|
|
|
@ -1807,11 +1807,8 @@ static int setup_xdp(struct net_device *dev, struct bpf_prog *prog)
|
|||
if (prog && !xdp_mtu_valid(priv, dev->mtu))
|
||||
return -EINVAL;
|
||||
|
||||
if (prog) {
|
||||
prog = bpf_prog_add(prog, priv->num_channels);
|
||||
if (IS_ERR(prog))
|
||||
return PTR_ERR(prog);
|
||||
}
|
||||
if (prog)
|
||||
bpf_prog_add(prog, priv->num_channels);
|
||||
|
||||
up = netif_running(dev);
|
||||
need_update = (!!priv->xdp_prog != !!prog);
|
||||
|
|
|
@ -2286,11 +2286,7 @@ int mlx4_en_try_alloc_resources(struct mlx4_en_priv *priv,
|
|||
lockdep_is_held(&priv->mdev->state_lock));
|
||||
|
||||
if (xdp_prog && carry_xdp_prog) {
|
||||
xdp_prog = bpf_prog_add(xdp_prog, tmp->rx_ring_num);
|
||||
if (IS_ERR(xdp_prog)) {
|
||||
mlx4_en_free_resources(tmp);
|
||||
return PTR_ERR(xdp_prog);
|
||||
}
|
||||
bpf_prog_add(xdp_prog, tmp->rx_ring_num);
|
||||
for (i = 0; i < tmp->rx_ring_num; i++)
|
||||
rcu_assign_pointer(tmp->rx_ring[i]->xdp_prog,
|
||||
xdp_prog);
|
||||
|
@ -2782,11 +2778,9 @@ static int mlx4_xdp_set(struct net_device *dev, struct bpf_prog *prog)
|
|||
* program for a new one.
|
||||
*/
|
||||
if (priv->tx_ring_num[TX_XDP] == xdp_ring_num) {
|
||||
if (prog) {
|
||||
prog = bpf_prog_add(prog, priv->rx_ring_num - 1);
|
||||
if (IS_ERR(prog))
|
||||
return PTR_ERR(prog);
|
||||
}
|
||||
if (prog)
|
||||
bpf_prog_add(prog, priv->rx_ring_num - 1);
|
||||
|
||||
mutex_lock(&mdev->state_lock);
|
||||
for (i = 0; i < priv->rx_ring_num; i++) {
|
||||
old_prog = rcu_dereference_protected(
|
||||
|
@ -2807,13 +2801,8 @@ static int mlx4_xdp_set(struct net_device *dev, struct bpf_prog *prog)
|
|||
if (!tmp)
|
||||
return -ENOMEM;
|
||||
|
||||
if (prog) {
|
||||
prog = bpf_prog_add(prog, priv->rx_ring_num - 1);
|
||||
if (IS_ERR(prog)) {
|
||||
err = PTR_ERR(prog);
|
||||
goto out;
|
||||
}
|
||||
}
|
||||
if (prog)
|
||||
bpf_prog_add(prog, priv->rx_ring_num - 1);
|
||||
|
||||
mutex_lock(&mdev->state_lock);
|
||||
memcpy(&new_prof, priv->prof, sizeof(struct mlx4_en_port_profile));
|
||||
|
@ -2862,7 +2851,6 @@ static int mlx4_xdp_set(struct net_device *dev, struct bpf_prog *prog)
|
|||
|
||||
unlock_out:
|
||||
mutex_unlock(&mdev->state_lock);
|
||||
out:
|
||||
kfree(tmp);
|
||||
return err;
|
||||
}
|
||||
|
|
|
@ -408,12 +408,9 @@ static int mlx5e_alloc_rq(struct mlx5e_channel *c,
|
|||
rq->stats = &c->priv->channel_stats[c->ix].rq;
|
||||
INIT_WORK(&rq->recover_work, mlx5e_rq_err_cqe_work);
|
||||
|
||||
rq->xdp_prog = params->xdp_prog ? bpf_prog_inc(params->xdp_prog) : NULL;
|
||||
if (IS_ERR(rq->xdp_prog)) {
|
||||
err = PTR_ERR(rq->xdp_prog);
|
||||
rq->xdp_prog = NULL;
|
||||
goto err_rq_wq_destroy;
|
||||
}
|
||||
if (params->xdp_prog)
|
||||
bpf_prog_inc(params->xdp_prog);
|
||||
rq->xdp_prog = params->xdp_prog;
|
||||
|
||||
rq_xdp_ix = rq->ix;
|
||||
if (xsk)
|
||||
|
@ -4406,16 +4403,11 @@ static int mlx5e_xdp_set(struct net_device *netdev, struct bpf_prog *prog)
|
|||
/* no need for full reset when exchanging programs */
|
||||
reset = (!priv->channels.params.xdp_prog || !prog);
|
||||
|
||||
if (was_opened && !reset) {
|
||||
if (was_opened && !reset)
|
||||
/* num_channels is invariant here, so we can take the
|
||||
* batched reference right upfront.
|
||||
*/
|
||||
prog = bpf_prog_add(prog, priv->channels.num);
|
||||
if (IS_ERR(prog)) {
|
||||
err = PTR_ERR(prog);
|
||||
goto unlock;
|
||||
}
|
||||
}
|
||||
bpf_prog_add(prog, priv->channels.num);
|
||||
|
||||
if (was_opened && reset) {
|
||||
struct mlx5e_channels new_channels = {};
|
||||
|
|
|
@ -46,9 +46,7 @@ nfp_map_ptr_record(struct nfp_app_bpf *bpf, struct nfp_prog *nfp_prog,
|
|||
/* Grab a single ref to the map for our record. The prog destroy ndo
|
||||
* happens after free_used_maps().
|
||||
*/
|
||||
map = bpf_map_inc(map, false);
|
||||
if (IS_ERR(map))
|
||||
return PTR_ERR(map);
|
||||
bpf_map_inc(map);
|
||||
|
||||
record = kmalloc(sizeof(*record), GFP_KERNEL);
|
||||
if (!record) {
|
||||
|
|
|
@ -2107,12 +2107,8 @@ static int qede_start_queues(struct qede_dev *edev, bool clear_stats)
|
|||
if (rc)
|
||||
goto out;
|
||||
|
||||
fp->rxq->xdp_prog = bpf_prog_add(edev->xdp_prog, 1);
|
||||
if (IS_ERR(fp->rxq->xdp_prog)) {
|
||||
rc = PTR_ERR(fp->rxq->xdp_prog);
|
||||
fp->rxq->xdp_prog = NULL;
|
||||
goto out;
|
||||
}
|
||||
bpf_prog_add(edev->xdp_prog, 1);
|
||||
fp->rxq->xdp_prog = edev->xdp_prog;
|
||||
}
|
||||
|
||||
if (fp->type & QEDE_FASTPATH_TX) {
|
||||
|
|
|
@ -2445,11 +2445,8 @@ static int virtnet_xdp_set(struct net_device *dev, struct bpf_prog *prog,
|
|||
if (!prog && !old_prog)
|
||||
return 0;
|
||||
|
||||
if (prog) {
|
||||
prog = bpf_prog_add(prog, vi->max_queue_pairs - 1);
|
||||
if (IS_ERR(prog))
|
||||
return PTR_ERR(prog);
|
||||
}
|
||||
if (prog)
|
||||
bpf_prog_add(prog, vi->max_queue_pairs - 1);
|
||||
|
||||
/* Make sure NAPI is not using any XDP TX queues for RX. */
|
||||
if (netif_running(dev)) {
|
||||
|
|
|
@ -12,6 +12,7 @@
|
|||
#include <linux/err.h>
|
||||
#include <linux/rbtree_latch.h>
|
||||
#include <linux/numa.h>
|
||||
#include <linux/mm_types.h>
|
||||
#include <linux/wait.h>
|
||||
#include <linux/u64_stats_sync.h>
|
||||
#include <linux/refcount.h>
|
||||
|
@ -68,6 +69,7 @@ struct bpf_map_ops {
|
|||
u64 *imm, u32 off);
|
||||
int (*map_direct_value_meta)(const struct bpf_map *map,
|
||||
u64 imm, u32 *off);
|
||||
int (*map_mmap)(struct bpf_map *map, struct vm_area_struct *vma);
|
||||
};
|
||||
|
||||
struct bpf_map_memory {
|
||||
|
@ -96,17 +98,19 @@ struct bpf_map {
|
|||
u32 btf_value_type_id;
|
||||
struct btf *btf;
|
||||
struct bpf_map_memory memory;
|
||||
char name[BPF_OBJ_NAME_LEN];
|
||||
bool unpriv_array;
|
||||
bool frozen; /* write-once */
|
||||
/* 48 bytes hole */
|
||||
bool frozen; /* write-once; write-protected by freeze_mutex */
|
||||
/* 22 bytes hole */
|
||||
|
||||
/* The 3rd and 4th cacheline with misc members to avoid false sharing
|
||||
* particularly with refcounting.
|
||||
*/
|
||||
atomic_t refcnt ____cacheline_aligned;
|
||||
atomic_t usercnt;
|
||||
atomic64_t refcnt ____cacheline_aligned;
|
||||
atomic64_t usercnt;
|
||||
struct work_struct work;
|
||||
char name[BPF_OBJ_NAME_LEN];
|
||||
struct mutex freeze_mutex;
|
||||
u64 writecnt; /* writable mmap cnt; protected by freeze_mutex */
|
||||
};
|
||||
|
||||
static inline bool map_value_has_spin_lock(const struct bpf_map *map)
|
||||
|
@ -485,7 +489,7 @@ struct bpf_func_info_aux {
|
|||
};
|
||||
|
||||
struct bpf_prog_aux {
|
||||
atomic_t refcnt;
|
||||
atomic64_t refcnt;
|
||||
u32 used_map_cnt;
|
||||
u32 max_ctx_offset;
|
||||
u32 max_pkt_offset;
|
||||
|
@ -770,9 +774,9 @@ extern const struct bpf_verifier_ops xdp_analyzer_ops;
|
|||
struct bpf_prog *bpf_prog_get(u32 ufd);
|
||||
struct bpf_prog *bpf_prog_get_type_dev(u32 ufd, enum bpf_prog_type type,
|
||||
bool attach_drv);
|
||||
struct bpf_prog * __must_check bpf_prog_add(struct bpf_prog *prog, int i);
|
||||
void bpf_prog_add(struct bpf_prog *prog, int i);
|
||||
void bpf_prog_sub(struct bpf_prog *prog, int i);
|
||||
struct bpf_prog * __must_check bpf_prog_inc(struct bpf_prog *prog);
|
||||
void bpf_prog_inc(struct bpf_prog *prog);
|
||||
struct bpf_prog * __must_check bpf_prog_inc_not_zero(struct bpf_prog *prog);
|
||||
void bpf_prog_put(struct bpf_prog *prog);
|
||||
int __bpf_prog_charge(struct user_struct *user, u32 pages);
|
||||
|
@ -783,9 +787,9 @@ void bpf_map_free_id(struct bpf_map *map, bool do_idr_lock);
|
|||
|
||||
struct bpf_map *bpf_map_get_with_uref(u32 ufd);
|
||||
struct bpf_map *__bpf_map_get(struct fd f);
|
||||
struct bpf_map * __must_check bpf_map_inc(struct bpf_map *map, bool uref);
|
||||
struct bpf_map * __must_check bpf_map_inc_not_zero(struct bpf_map *map,
|
||||
bool uref);
|
||||
void bpf_map_inc(struct bpf_map *map);
|
||||
void bpf_map_inc_with_uref(struct bpf_map *map);
|
||||
struct bpf_map * __must_check bpf_map_inc_not_zero(struct bpf_map *map);
|
||||
void bpf_map_put_with_uref(struct bpf_map *map);
|
||||
void bpf_map_put(struct bpf_map *map);
|
||||
int bpf_map_charge_memlock(struct bpf_map *map, u32 pages);
|
||||
|
@ -795,6 +799,7 @@ void bpf_map_charge_finish(struct bpf_map_memory *mem);
|
|||
void bpf_map_charge_move(struct bpf_map_memory *dst,
|
||||
struct bpf_map_memory *src);
|
||||
void *bpf_map_area_alloc(size_t size, int numa_node);
|
||||
void *bpf_map_area_mmapable_alloc(size_t size, int numa_node);
|
||||
void bpf_map_area_free(void *base);
|
||||
void bpf_map_init_from_attr(struct bpf_map *map, union bpf_attr *attr);
|
||||
|
||||
|
@ -912,10 +917,8 @@ static inline struct bpf_prog *bpf_prog_get_type_dev(u32 ufd,
|
|||
return ERR_PTR(-EOPNOTSUPP);
|
||||
}
|
||||
|
||||
static inline struct bpf_prog * __must_check bpf_prog_add(struct bpf_prog *prog,
|
||||
int i)
|
||||
static inline void bpf_prog_add(struct bpf_prog *prog, int i)
|
||||
{
|
||||
return ERR_PTR(-EOPNOTSUPP);
|
||||
}
|
||||
|
||||
static inline void bpf_prog_sub(struct bpf_prog *prog, int i)
|
||||
|
@ -926,9 +929,8 @@ static inline void bpf_prog_put(struct bpf_prog *prog)
|
|||
{
|
||||
}
|
||||
|
||||
static inline struct bpf_prog * __must_check bpf_prog_inc(struct bpf_prog *prog)
|
||||
static inline void bpf_prog_inc(struct bpf_prog *prog)
|
||||
{
|
||||
return ERR_PTR(-EOPNOTSUPP);
|
||||
}
|
||||
|
||||
static inline struct bpf_prog *__must_check
|
||||
|
|
|
@ -93,6 +93,7 @@ extern void *vzalloc(unsigned long size);
|
|||
extern void *vmalloc_user(unsigned long size);
|
||||
extern void *vmalloc_node(unsigned long size, int node);
|
||||
extern void *vzalloc_node(unsigned long size, int node);
|
||||
extern void *vmalloc_user_node_flags(unsigned long size, int node, gfp_t flags);
|
||||
extern void *vmalloc_exec(unsigned long size);
|
||||
extern void *vmalloc_32(unsigned long size);
|
||||
extern void *vmalloc_32_user(unsigned long size);
|
||||
|
|
|
@ -348,6 +348,9 @@ enum bpf_attach_type {
|
|||
/* Clone map from listener for newly accepted socket */
|
||||
#define BPF_F_CLONE (1U << 9)
|
||||
|
||||
/* Enable memory-mapping BPF map */
|
||||
#define BPF_F_MMAPABLE (1U << 10)
|
||||
|
||||
/* flags for BPF_PROG_QUERY */
|
||||
#define BPF_F_QUERY_EFFECTIVE (1U << 0)
|
||||
|
||||
|
|
|
@ -14,7 +14,7 @@
|
|||
#include "map_in_map.h"
|
||||
|
||||
#define ARRAY_CREATE_FLAG_MASK \
|
||||
(BPF_F_NUMA_NODE | BPF_F_ACCESS_MASK)
|
||||
(BPF_F_NUMA_NODE | BPF_F_MMAPABLE | BPF_F_ACCESS_MASK)
|
||||
|
||||
static void bpf_array_free_percpu(struct bpf_array *array)
|
||||
{
|
||||
|
@ -59,6 +59,10 @@ int array_map_alloc_check(union bpf_attr *attr)
|
|||
(percpu && numa_node != NUMA_NO_NODE))
|
||||
return -EINVAL;
|
||||
|
||||
if (attr->map_type != BPF_MAP_TYPE_ARRAY &&
|
||||
attr->map_flags & BPF_F_MMAPABLE)
|
||||
return -EINVAL;
|
||||
|
||||
if (attr->value_size > KMALLOC_MAX_SIZE)
|
||||
/* if value_size is bigger, the user space won't be able to
|
||||
* access the elements.
|
||||
|
@ -102,10 +106,19 @@ static struct bpf_map *array_map_alloc(union bpf_attr *attr)
|
|||
}
|
||||
|
||||
array_size = sizeof(*array);
|
||||
if (percpu)
|
||||
if (percpu) {
|
||||
array_size += (u64) max_entries * sizeof(void *);
|
||||
else
|
||||
array_size += (u64) max_entries * elem_size;
|
||||
} else {
|
||||
/* rely on vmalloc() to return page-aligned memory and
|
||||
* ensure array->value is exactly page-aligned
|
||||
*/
|
||||
if (attr->map_flags & BPF_F_MMAPABLE) {
|
||||
array_size = PAGE_ALIGN(array_size);
|
||||
array_size += PAGE_ALIGN((u64) max_entries * elem_size);
|
||||
} else {
|
||||
array_size += (u64) max_entries * elem_size;
|
||||
}
|
||||
}
|
||||
|
||||
/* make sure there is no u32 overflow later in round_up() */
|
||||
cost = array_size;
|
||||
|
@ -117,7 +130,20 @@ static struct bpf_map *array_map_alloc(union bpf_attr *attr)
|
|||
return ERR_PTR(ret);
|
||||
|
||||
/* allocate all map elements and zero-initialize them */
|
||||
array = bpf_map_area_alloc(array_size, numa_node);
|
||||
if (attr->map_flags & BPF_F_MMAPABLE) {
|
||||
void *data;
|
||||
|
||||
/* kmalloc'ed memory can't be mmap'ed, use explicit vmalloc */
|
||||
data = bpf_map_area_mmapable_alloc(array_size, numa_node);
|
||||
if (!data) {
|
||||
bpf_map_charge_finish(&mem);
|
||||
return ERR_PTR(-ENOMEM);
|
||||
}
|
||||
array = data + PAGE_ALIGN(sizeof(struct bpf_array))
|
||||
- offsetof(struct bpf_array, value);
|
||||
} else {
|
||||
array = bpf_map_area_alloc(array_size, numa_node);
|
||||
}
|
||||
if (!array) {
|
||||
bpf_map_charge_finish(&mem);
|
||||
return ERR_PTR(-ENOMEM);
|
||||
|
@ -350,6 +376,11 @@ static int array_map_delete_elem(struct bpf_map *map, void *key)
|
|||
return -EINVAL;
|
||||
}
|
||||
|
||||
static void *array_map_vmalloc_addr(struct bpf_array *array)
|
||||
{
|
||||
return (void *)round_down((unsigned long)array, PAGE_SIZE);
|
||||
}
|
||||
|
||||
/* Called when map->refcnt goes to zero, either from workqueue or from syscall */
|
||||
static void array_map_free(struct bpf_map *map)
|
||||
{
|
||||
|
@ -365,7 +396,10 @@ static void array_map_free(struct bpf_map *map)
|
|||
if (array->map.map_type == BPF_MAP_TYPE_PERCPU_ARRAY)
|
||||
bpf_array_free_percpu(array);
|
||||
|
||||
bpf_map_area_free(array);
|
||||
if (array->map.map_flags & BPF_F_MMAPABLE)
|
||||
bpf_map_area_free(array_map_vmalloc_addr(array));
|
||||
else
|
||||
bpf_map_area_free(array);
|
||||
}
|
||||
|
||||
static void array_map_seq_show_elem(struct bpf_map *map, void *key,
|
||||
|
@ -444,6 +478,17 @@ static int array_map_check_btf(const struct bpf_map *map,
|
|||
return 0;
|
||||
}
|
||||
|
||||
int array_map_mmap(struct bpf_map *map, struct vm_area_struct *vma)
|
||||
{
|
||||
struct bpf_array *array = container_of(map, struct bpf_array, map);
|
||||
pgoff_t pgoff = PAGE_ALIGN(sizeof(*array)) >> PAGE_SHIFT;
|
||||
|
||||
if (!(map->map_flags & BPF_F_MMAPABLE))
|
||||
return -EINVAL;
|
||||
|
||||
return remap_vmalloc_range(vma, array_map_vmalloc_addr(array), pgoff);
|
||||
}
|
||||
|
||||
const struct bpf_map_ops array_map_ops = {
|
||||
.map_alloc_check = array_map_alloc_check,
|
||||
.map_alloc = array_map_alloc,
|
||||
|
@ -455,6 +500,7 @@ const struct bpf_map_ops array_map_ops = {
|
|||
.map_gen_lookup = array_map_gen_lookup,
|
||||
.map_direct_value_addr = array_map_direct_value_addr,
|
||||
.map_direct_value_meta = array_map_direct_value_meta,
|
||||
.map_mmap = array_map_mmap,
|
||||
.map_seq_show_elem = array_map_seq_show_elem,
|
||||
.map_check_btf = array_map_check_btf,
|
||||
};
|
||||
|
|
|
@ -31,10 +31,10 @@ static void *bpf_any_get(void *raw, enum bpf_type type)
|
|||
{
|
||||
switch (type) {
|
||||
case BPF_TYPE_PROG:
|
||||
raw = bpf_prog_inc(raw);
|
||||
bpf_prog_inc(raw);
|
||||
break;
|
||||
case BPF_TYPE_MAP:
|
||||
raw = bpf_map_inc(raw, true);
|
||||
bpf_map_inc_with_uref(raw);
|
||||
break;
|
||||
default:
|
||||
WARN_ON_ONCE(1);
|
||||
|
@ -534,7 +534,8 @@ static struct bpf_prog *__get_prog_inode(struct inode *inode, enum bpf_prog_type
|
|||
if (!bpf_prog_get_ok(prog, &type, false))
|
||||
return ERR_PTR(-EINVAL);
|
||||
|
||||
return bpf_prog_inc(prog);
|
||||
bpf_prog_inc(prog);
|
||||
return prog;
|
||||
}
|
||||
|
||||
struct bpf_prog *bpf_prog_get_type_path(const char *name, enum bpf_prog_type type)
|
||||
|
|
|
@ -98,7 +98,7 @@ void *bpf_map_fd_get_ptr(struct bpf_map *map,
|
|||
return inner_map;
|
||||
|
||||
if (bpf_map_meta_equal(map->inner_map_meta, inner_map))
|
||||
inner_map = bpf_map_inc(inner_map, false);
|
||||
bpf_map_inc(inner_map);
|
||||
else
|
||||
inner_map = ERR_PTR(-EINVAL);
|
||||
|
||||
|
|
|
@ -127,7 +127,7 @@ static struct bpf_map *find_and_alloc_map(union bpf_attr *attr)
|
|||
return map;
|
||||
}
|
||||
|
||||
void *bpf_map_area_alloc(size_t size, int numa_node)
|
||||
static void *__bpf_map_area_alloc(size_t size, int numa_node, bool mmapable)
|
||||
{
|
||||
/* We really just want to fail instead of triggering OOM killer
|
||||
* under memory pressure, therefore we set __GFP_NORETRY to kmalloc,
|
||||
|
@ -142,18 +142,33 @@ void *bpf_map_area_alloc(size_t size, int numa_node)
|
|||
const gfp_t flags = __GFP_NOWARN | __GFP_ZERO;
|
||||
void *area;
|
||||
|
||||
if (size <= (PAGE_SIZE << PAGE_ALLOC_COSTLY_ORDER)) {
|
||||
/* kmalloc()'ed memory can't be mmap()'ed */
|
||||
if (!mmapable && size <= (PAGE_SIZE << PAGE_ALLOC_COSTLY_ORDER)) {
|
||||
area = kmalloc_node(size, GFP_USER | __GFP_NORETRY | flags,
|
||||
numa_node);
|
||||
if (area != NULL)
|
||||
return area;
|
||||
}
|
||||
|
||||
if (mmapable) {
|
||||
BUG_ON(!PAGE_ALIGNED(size));
|
||||
return vmalloc_user_node_flags(size, numa_node, GFP_KERNEL |
|
||||
__GFP_RETRY_MAYFAIL | flags);
|
||||
}
|
||||
return __vmalloc_node_flags_caller(size, numa_node,
|
||||
GFP_KERNEL | __GFP_RETRY_MAYFAIL |
|
||||
flags, __builtin_return_address(0));
|
||||
}
|
||||
|
||||
void *bpf_map_area_alloc(size_t size, int numa_node)
|
||||
{
|
||||
return __bpf_map_area_alloc(size, numa_node, false);
|
||||
}
|
||||
|
||||
void *bpf_map_area_mmapable_alloc(size_t size, int numa_node)
|
||||
{
|
||||
return __bpf_map_area_alloc(size, numa_node, true);
|
||||
}
|
||||
|
||||
void bpf_map_area_free(void *area)
|
||||
{
|
||||
kvfree(area);
|
||||
|
@ -311,7 +326,7 @@ static void bpf_map_free_deferred(struct work_struct *work)
|
|||
|
||||
static void bpf_map_put_uref(struct bpf_map *map)
|
||||
{
|
||||
if (atomic_dec_and_test(&map->usercnt)) {
|
||||
if (atomic64_dec_and_test(&map->usercnt)) {
|
||||
if (map->ops->map_release_uref)
|
||||
map->ops->map_release_uref(map);
|
||||
}
|
||||
|
@ -322,7 +337,7 @@ static void bpf_map_put_uref(struct bpf_map *map)
|
|||
*/
|
||||
static void __bpf_map_put(struct bpf_map *map, bool do_idr_lock)
|
||||
{
|
||||
if (atomic_dec_and_test(&map->refcnt)) {
|
||||
if (atomic64_dec_and_test(&map->refcnt)) {
|
||||
/* bpf_map_free_id() must be called first */
|
||||
bpf_map_free_id(map, do_idr_lock);
|
||||
btf_put(map->btf);
|
||||
|
@ -425,6 +440,74 @@ static ssize_t bpf_dummy_write(struct file *filp, const char __user *buf,
|
|||
return -EINVAL;
|
||||
}
|
||||
|
||||
/* called for any extra memory-mapped regions (except initial) */
|
||||
static void bpf_map_mmap_open(struct vm_area_struct *vma)
|
||||
{
|
||||
struct bpf_map *map = vma->vm_file->private_data;
|
||||
|
||||
bpf_map_inc_with_uref(map);
|
||||
|
||||
if (vma->vm_flags & VM_WRITE) {
|
||||
mutex_lock(&map->freeze_mutex);
|
||||
map->writecnt++;
|
||||
mutex_unlock(&map->freeze_mutex);
|
||||
}
|
||||
}
|
||||
|
||||
/* called for all unmapped memory region (including initial) */
|
||||
static void bpf_map_mmap_close(struct vm_area_struct *vma)
|
||||
{
|
||||
struct bpf_map *map = vma->vm_file->private_data;
|
||||
|
||||
if (vma->vm_flags & VM_WRITE) {
|
||||
mutex_lock(&map->freeze_mutex);
|
||||
map->writecnt--;
|
||||
mutex_unlock(&map->freeze_mutex);
|
||||
}
|
||||
|
||||
bpf_map_put_with_uref(map);
|
||||
}
|
||||
|
||||
static const struct vm_operations_struct bpf_map_default_vmops = {
|
||||
.open = bpf_map_mmap_open,
|
||||
.close = bpf_map_mmap_close,
|
||||
};
|
||||
|
||||
static int bpf_map_mmap(struct file *filp, struct vm_area_struct *vma)
|
||||
{
|
||||
struct bpf_map *map = filp->private_data;
|
||||
int err;
|
||||
|
||||
if (!map->ops->map_mmap || map_value_has_spin_lock(map))
|
||||
return -ENOTSUPP;
|
||||
|
||||
if (!(vma->vm_flags & VM_SHARED))
|
||||
return -EINVAL;
|
||||
|
||||
mutex_lock(&map->freeze_mutex);
|
||||
|
||||
if ((vma->vm_flags & VM_WRITE) && map->frozen) {
|
||||
err = -EPERM;
|
||||
goto out;
|
||||
}
|
||||
|
||||
/* set default open/close callbacks */
|
||||
vma->vm_ops = &bpf_map_default_vmops;
|
||||
vma->vm_private_data = map;
|
||||
|
||||
err = map->ops->map_mmap(map, vma);
|
||||
if (err)
|
||||
goto out;
|
||||
|
||||
bpf_map_inc_with_uref(map);
|
||||
|
||||
if (vma->vm_flags & VM_WRITE)
|
||||
map->writecnt++;
|
||||
out:
|
||||
mutex_unlock(&map->freeze_mutex);
|
||||
return err;
|
||||
}
|
||||
|
||||
const struct file_operations bpf_map_fops = {
|
||||
#ifdef CONFIG_PROC_FS
|
||||
.show_fdinfo = bpf_map_show_fdinfo,
|
||||
|
@ -432,6 +515,7 @@ const struct file_operations bpf_map_fops = {
|
|||
.release = bpf_map_release,
|
||||
.read = bpf_dummy_read,
|
||||
.write = bpf_dummy_write,
|
||||
.mmap = bpf_map_mmap,
|
||||
};
|
||||
|
||||
int bpf_map_new_fd(struct bpf_map *map, int flags)
|
||||
|
@ -575,8 +659,9 @@ static int map_create(union bpf_attr *attr)
|
|||
if (err)
|
||||
goto free_map;
|
||||
|
||||
atomic_set(&map->refcnt, 1);
|
||||
atomic_set(&map->usercnt, 1);
|
||||
atomic64_set(&map->refcnt, 1);
|
||||
atomic64_set(&map->usercnt, 1);
|
||||
mutex_init(&map->freeze_mutex);
|
||||
|
||||
if (attr->btf_key_type_id || attr->btf_value_type_id) {
|
||||
struct btf *btf;
|
||||
|
@ -653,21 +738,19 @@ struct bpf_map *__bpf_map_get(struct fd f)
|
|||
return f.file->private_data;
|
||||
}
|
||||
|
||||
/* prog's and map's refcnt limit */
|
||||
#define BPF_MAX_REFCNT 32768
|
||||
|
||||
struct bpf_map *bpf_map_inc(struct bpf_map *map, bool uref)
|
||||
void bpf_map_inc(struct bpf_map *map)
|
||||
{
|
||||
if (atomic_inc_return(&map->refcnt) > BPF_MAX_REFCNT) {
|
||||
atomic_dec(&map->refcnt);
|
||||
return ERR_PTR(-EBUSY);
|
||||
}
|
||||
if (uref)
|
||||
atomic_inc(&map->usercnt);
|
||||
return map;
|
||||
atomic64_inc(&map->refcnt);
|
||||
}
|
||||
EXPORT_SYMBOL_GPL(bpf_map_inc);
|
||||
|
||||
void bpf_map_inc_with_uref(struct bpf_map *map)
|
||||
{
|
||||
atomic64_inc(&map->refcnt);
|
||||
atomic64_inc(&map->usercnt);
|
||||
}
|
||||
EXPORT_SYMBOL_GPL(bpf_map_inc_with_uref);
|
||||
|
||||
struct bpf_map *bpf_map_get_with_uref(u32 ufd)
|
||||
{
|
||||
struct fd f = fdget(ufd);
|
||||
|
@ -677,38 +760,30 @@ struct bpf_map *bpf_map_get_with_uref(u32 ufd)
|
|||
if (IS_ERR(map))
|
||||
return map;
|
||||
|
||||
map = bpf_map_inc(map, true);
|
||||
bpf_map_inc_with_uref(map);
|
||||
fdput(f);
|
||||
|
||||
return map;
|
||||
}
|
||||
|
||||
/* map_idr_lock should have been held */
|
||||
static struct bpf_map *__bpf_map_inc_not_zero(struct bpf_map *map,
|
||||
bool uref)
|
||||
static struct bpf_map *__bpf_map_inc_not_zero(struct bpf_map *map, bool uref)
|
||||
{
|
||||
int refold;
|
||||
|
||||
refold = atomic_fetch_add_unless(&map->refcnt, 1, 0);
|
||||
|
||||
if (refold >= BPF_MAX_REFCNT) {
|
||||
__bpf_map_put(map, false);
|
||||
return ERR_PTR(-EBUSY);
|
||||
}
|
||||
|
||||
refold = atomic64_fetch_add_unless(&map->refcnt, 1, 0);
|
||||
if (!refold)
|
||||
return ERR_PTR(-ENOENT);
|
||||
|
||||
if (uref)
|
||||
atomic_inc(&map->usercnt);
|
||||
atomic64_inc(&map->usercnt);
|
||||
|
||||
return map;
|
||||
}
|
||||
|
||||
struct bpf_map *bpf_map_inc_not_zero(struct bpf_map *map, bool uref)
|
||||
struct bpf_map *bpf_map_inc_not_zero(struct bpf_map *map)
|
||||
{
|
||||
spin_lock_bh(&map_idr_lock);
|
||||
map = __bpf_map_inc_not_zero(map, uref);
|
||||
map = __bpf_map_inc_not_zero(map, false);
|
||||
spin_unlock_bh(&map_idr_lock);
|
||||
|
||||
return map;
|
||||
|
@ -1173,6 +1248,13 @@ static int map_freeze(const union bpf_attr *attr)
|
|||
map = __bpf_map_get(f);
|
||||
if (IS_ERR(map))
|
||||
return PTR_ERR(map);
|
||||
|
||||
mutex_lock(&map->freeze_mutex);
|
||||
|
||||
if (map->writecnt) {
|
||||
err = -EBUSY;
|
||||
goto err_put;
|
||||
}
|
||||
if (READ_ONCE(map->frozen)) {
|
||||
err = -EBUSY;
|
||||
goto err_put;
|
||||
|
@ -1184,6 +1266,7 @@ static int map_freeze(const union bpf_attr *attr)
|
|||
|
||||
WRITE_ONCE(map->frozen, true);
|
||||
err_put:
|
||||
mutex_unlock(&map->freeze_mutex);
|
||||
fdput(f);
|
||||
return err;
|
||||
}
|
||||
|
@ -1349,7 +1432,7 @@ static void __bpf_prog_put_noref(struct bpf_prog *prog, bool deferred)
|
|||
|
||||
static void __bpf_prog_put(struct bpf_prog *prog, bool do_idr_lock)
|
||||
{
|
||||
if (atomic_dec_and_test(&prog->aux->refcnt)) {
|
||||
if (atomic64_dec_and_test(&prog->aux->refcnt)) {
|
||||
perf_event_bpf_event(prog, PERF_BPF_EVENT_PROG_UNLOAD, 0);
|
||||
/* bpf_prog_free_id() must be called first */
|
||||
bpf_prog_free_id(prog, do_idr_lock);
|
||||
|
@ -1455,13 +1538,9 @@ static struct bpf_prog *____bpf_prog_get(struct fd f)
|
|||
return f.file->private_data;
|
||||
}
|
||||
|
||||
struct bpf_prog *bpf_prog_add(struct bpf_prog *prog, int i)
|
||||
void bpf_prog_add(struct bpf_prog *prog, int i)
|
||||
{
|
||||
if (atomic_add_return(i, &prog->aux->refcnt) > BPF_MAX_REFCNT) {
|
||||
atomic_sub(i, &prog->aux->refcnt);
|
||||
return ERR_PTR(-EBUSY);
|
||||
}
|
||||
return prog;
|
||||
atomic64_add(i, &prog->aux->refcnt);
|
||||
}
|
||||
EXPORT_SYMBOL_GPL(bpf_prog_add);
|
||||
|
||||
|
@ -1472,13 +1551,13 @@ void bpf_prog_sub(struct bpf_prog *prog, int i)
|
|||
* path holds a reference to the program, thus atomic_sub() can
|
||||
* be safely used in such cases!
|
||||
*/
|
||||
WARN_ON(atomic_sub_return(i, &prog->aux->refcnt) == 0);
|
||||
WARN_ON(atomic64_sub_return(i, &prog->aux->refcnt) == 0);
|
||||
}
|
||||
EXPORT_SYMBOL_GPL(bpf_prog_sub);
|
||||
|
||||
struct bpf_prog *bpf_prog_inc(struct bpf_prog *prog)
|
||||
void bpf_prog_inc(struct bpf_prog *prog)
|
||||
{
|
||||
return bpf_prog_add(prog, 1);
|
||||
atomic64_inc(&prog->aux->refcnt);
|
||||
}
|
||||
EXPORT_SYMBOL_GPL(bpf_prog_inc);
|
||||
|
||||
|
@ -1487,12 +1566,7 @@ struct bpf_prog *bpf_prog_inc_not_zero(struct bpf_prog *prog)
|
|||
{
|
||||
int refold;
|
||||
|
||||
refold = atomic_fetch_add_unless(&prog->aux->refcnt, 1, 0);
|
||||
|
||||
if (refold >= BPF_MAX_REFCNT) {
|
||||
__bpf_prog_put(prog, false);
|
||||
return ERR_PTR(-EBUSY);
|
||||
}
|
||||
refold = atomic64_fetch_add_unless(&prog->aux->refcnt, 1, 0);
|
||||
|
||||
if (!refold)
|
||||
return ERR_PTR(-ENOENT);
|
||||
|
@ -1530,7 +1604,7 @@ static struct bpf_prog *__bpf_prog_get(u32 ufd, enum bpf_prog_type *attach_type,
|
|||
goto out;
|
||||
}
|
||||
|
||||
prog = bpf_prog_inc(prog);
|
||||
bpf_prog_inc(prog);
|
||||
out:
|
||||
fdput(f);
|
||||
return prog;
|
||||
|
@ -1721,7 +1795,7 @@ static int bpf_prog_load(union bpf_attr *attr, union bpf_attr __user *uattr)
|
|||
prog->orig_prog = NULL;
|
||||
prog->jited = 0;
|
||||
|
||||
atomic_set(&prog->aux->refcnt, 1);
|
||||
atomic64_set(&prog->aux->refcnt, 1);
|
||||
prog->gpl_compatible = is_gpl ? 1 : 0;
|
||||
|
||||
if (bpf_prog_is_dev_bound(prog->aux)) {
|
||||
|
|
|
@ -8179,11 +8179,7 @@ static int replace_map_fd_with_map_ptr(struct bpf_verifier_env *env)
|
|||
* will be used by the valid program until it's unloaded
|
||||
* and all maps are released in free_used_maps()
|
||||
*/
|
||||
map = bpf_map_inc(map, false);
|
||||
if (IS_ERR(map)) {
|
||||
fdput(f);
|
||||
return PTR_ERR(map);
|
||||
}
|
||||
bpf_map_inc(map);
|
||||
|
||||
aux->map_index = env->used_map_cnt;
|
||||
env->used_maps[env->used_map_cnt++] = map;
|
||||
|
|
|
@ -11,10 +11,8 @@
|
|||
|
||||
int xsk_map_inc(struct xsk_map *map)
|
||||
{
|
||||
struct bpf_map *m = &map->map;
|
||||
|
||||
m = bpf_map_inc(m, false);
|
||||
return PTR_ERR_OR_ZERO(m);
|
||||
bpf_map_inc(&map->map);
|
||||
return 0;
|
||||
}
|
||||
|
||||
void xsk_map_put(struct xsk_map *map)
|
||||
|
|
|
@ -10477,12 +10477,9 @@ perf_event_alloc(struct perf_event_attr *attr, int cpu,
|
|||
context = parent_event->overflow_handler_context;
|
||||
#if defined(CONFIG_BPF_SYSCALL) && defined(CONFIG_EVENT_TRACING)
|
||||
if (overflow_handler == bpf_overflow_handler) {
|
||||
struct bpf_prog *prog = bpf_prog_inc(parent_event->prog);
|
||||
struct bpf_prog *prog = parent_event->prog;
|
||||
|
||||
if (IS_ERR(prog)) {
|
||||
err = PTR_ERR(prog);
|
||||
goto err_ns;
|
||||
}
|
||||
bpf_prog_inc(prog);
|
||||
event->prog = prog;
|
||||
event->orig_overflow_handler =
|
||||
parent_event->orig_overflow_handler;
|
||||
|
|
20
mm/vmalloc.c
20
mm/vmalloc.c
|
@ -2671,6 +2671,26 @@ void *vzalloc_node(unsigned long size, int node)
|
|||
}
|
||||
EXPORT_SYMBOL(vzalloc_node);
|
||||
|
||||
/**
|
||||
* vmalloc_user_node_flags - allocate memory for userspace on a specific node
|
||||
* @size: allocation size
|
||||
* @node: numa node
|
||||
* @flags: flags for the page level allocator
|
||||
*
|
||||
* The resulting memory area is zeroed so it can be mapped to userspace
|
||||
* without leaking data.
|
||||
*
|
||||
* Return: pointer to the allocated memory or %NULL on error
|
||||
*/
|
||||
void *vmalloc_user_node_flags(unsigned long size, int node, gfp_t flags)
|
||||
{
|
||||
return __vmalloc_node_range(size, SHMLBA, VMALLOC_START, VMALLOC_END,
|
||||
flags | __GFP_ZERO, PAGE_KERNEL,
|
||||
VM_USERMAP, node,
|
||||
__builtin_return_address(0));
|
||||
}
|
||||
EXPORT_SYMBOL(vmalloc_user_node_flags);
|
||||
|
||||
/**
|
||||
* vmalloc_exec - allocate virtually contiguous, executable memory
|
||||
* @size: allocation size
|
||||
|
|
|
@ -798,7 +798,7 @@ int bpf_sk_storage_clone(const struct sock *sk, struct sock *newsk)
|
|||
* Try to grab map refcnt to make sure that it's still
|
||||
* alive and prevent concurrent removal.
|
||||
*/
|
||||
map = bpf_map_inc_not_zero(&smap->map, false);
|
||||
map = bpf_map_inc_not_zero(&smap->map);
|
||||
if (IS_ERR(map))
|
||||
continue;
|
||||
|
||||
|
|
|
@ -348,6 +348,9 @@ enum bpf_attach_type {
|
|||
/* Clone map from listener for newly accepted socket */
|
||||
#define BPF_F_CLONE (1U << 9)
|
||||
|
||||
/* Enable memory-mapping BPF map */
|
||||
#define BPF_F_MMAPABLE (1U << 10)
|
||||
|
||||
/* flags for BPF_PROG_QUERY */
|
||||
#define BPF_F_QUERY_EFFECTIVE (1U << 0)
|
||||
|
||||
|
|
|
@ -142,6 +142,8 @@ struct bpf_capabilities {
|
|||
__u32 btf_func:1;
|
||||
/* BTF_KIND_VAR and BTF_KIND_DATASEC support */
|
||||
__u32 btf_datasec:1;
|
||||
/* BPF_F_MMAPABLE is supported for arrays */
|
||||
__u32 array_mmap:1;
|
||||
};
|
||||
|
||||
/*
|
||||
|
@ -857,8 +859,6 @@ bpf_object__init_internal_map(struct bpf_object *obj, enum libbpf_map_type type,
|
|||
pr_warn("failed to alloc map name\n");
|
||||
return -ENOMEM;
|
||||
}
|
||||
pr_debug("map '%s' (global data): at sec_idx %d, offset %zu.\n",
|
||||
map_name, map->sec_idx, map->sec_offset);
|
||||
|
||||
def = &map->def;
|
||||
def->type = BPF_MAP_TYPE_ARRAY;
|
||||
|
@ -866,6 +866,12 @@ bpf_object__init_internal_map(struct bpf_object *obj, enum libbpf_map_type type,
|
|||
def->value_size = data->d_size;
|
||||
def->max_entries = 1;
|
||||
def->map_flags = type == LIBBPF_MAP_RODATA ? BPF_F_RDONLY_PROG : 0;
|
||||
if (obj->caps.array_mmap)
|
||||
def->map_flags |= BPF_F_MMAPABLE;
|
||||
|
||||
pr_debug("map '%s' (global data): at sec_idx %d, offset %zu, flags %x.\n",
|
||||
map_name, map->sec_idx, map->sec_offset, def->map_flags);
|
||||
|
||||
if (data_buff) {
|
||||
*data_buff = malloc(data->d_size);
|
||||
if (!*data_buff) {
|
||||
|
@ -2161,6 +2167,27 @@ static int bpf_object__probe_btf_datasec(struct bpf_object *obj)
|
|||
return 0;
|
||||
}
|
||||
|
||||
static int bpf_object__probe_array_mmap(struct bpf_object *obj)
|
||||
{
|
||||
struct bpf_create_map_attr attr = {
|
||||
.map_type = BPF_MAP_TYPE_ARRAY,
|
||||
.map_flags = BPF_F_MMAPABLE,
|
||||
.key_size = sizeof(int),
|
||||
.value_size = sizeof(int),
|
||||
.max_entries = 1,
|
||||
};
|
||||
int fd;
|
||||
|
||||
fd = bpf_create_map_xattr(&attr);
|
||||
if (fd >= 0) {
|
||||
obj->caps.array_mmap = 1;
|
||||
close(fd);
|
||||
return 1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int
|
||||
bpf_object__probe_caps(struct bpf_object *obj)
|
||||
{
|
||||
|
@ -2169,6 +2196,7 @@ bpf_object__probe_caps(struct bpf_object *obj)
|
|||
bpf_object__probe_global_data,
|
||||
bpf_object__probe_btf_func,
|
||||
bpf_object__probe_btf_datasec,
|
||||
bpf_object__probe_array_mmap,
|
||||
};
|
||||
int i, ret;
|
||||
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
// SPDX-License-Identifier: GPL-2.0
|
||||
#include <test_progs.h>
|
||||
#include "progs/core_reloc_types.h"
|
||||
#include <sys/mman.h>
|
||||
|
||||
#define STRUCT_TO_CHAR_PTR(struct_name) (const char *)&(struct struct_name)
|
||||
|
||||
|
@ -453,8 +454,15 @@ struct data {
|
|||
char out[256];
|
||||
};
|
||||
|
||||
static size_t roundup_page(size_t sz)
|
||||
{
|
||||
long page_size = sysconf(_SC_PAGE_SIZE);
|
||||
return (sz + page_size - 1) / page_size * page_size;
|
||||
}
|
||||
|
||||
void test_core_reloc(void)
|
||||
{
|
||||
const size_t mmap_sz = roundup_page(sizeof(struct data));
|
||||
struct bpf_object_load_attr load_attr = {};
|
||||
struct core_reloc_test_case *test_case;
|
||||
const char *tp_name, *probe_name;
|
||||
|
@ -463,8 +471,8 @@ void test_core_reloc(void)
|
|||
struct bpf_map *data_map;
|
||||
struct bpf_program *prog;
|
||||
struct bpf_object *obj;
|
||||
const int zero = 0;
|
||||
struct data data;
|
||||
struct data *data;
|
||||
void *mmap_data = NULL;
|
||||
|
||||
for (i = 0; i < ARRAY_SIZE(test_cases); i++) {
|
||||
test_case = &test_cases[i];
|
||||
|
@ -476,8 +484,7 @@ void test_core_reloc(void)
|
|||
);
|
||||
|
||||
obj = bpf_object__open_file(test_case->bpf_obj_file, &opts);
|
||||
if (CHECK(IS_ERR_OR_NULL(obj), "obj_open",
|
||||
"failed to open '%s': %ld\n",
|
||||
if (CHECK(IS_ERR(obj), "obj_open", "failed to open '%s': %ld\n",
|
||||
test_case->bpf_obj_file, PTR_ERR(obj)))
|
||||
continue;
|
||||
|
||||
|
@ -519,24 +526,22 @@ void test_core_reloc(void)
|
|||
if (CHECK(!data_map, "find_data_map", "data map not found\n"))
|
||||
goto cleanup;
|
||||
|
||||
memset(&data, 0, sizeof(data));
|
||||
memcpy(data.in, test_case->input, test_case->input_len);
|
||||
|
||||
err = bpf_map_update_elem(bpf_map__fd(data_map),
|
||||
&zero, &data, 0);
|
||||
if (CHECK(err, "update_data_map",
|
||||
"failed to update .data map: %d\n", err))
|
||||
mmap_data = mmap(NULL, mmap_sz, PROT_READ | PROT_WRITE,
|
||||
MAP_SHARED, bpf_map__fd(data_map), 0);
|
||||
if (CHECK(mmap_data == MAP_FAILED, "mmap",
|
||||
".bss mmap failed: %d", errno)) {
|
||||
mmap_data = NULL;
|
||||
goto cleanup;
|
||||
}
|
||||
data = mmap_data;
|
||||
|
||||
memset(mmap_data, 0, sizeof(*data));
|
||||
memcpy(data->in, test_case->input, test_case->input_len);
|
||||
|
||||
/* trigger test run */
|
||||
usleep(1);
|
||||
|
||||
err = bpf_map_lookup_elem(bpf_map__fd(data_map), &zero, &data);
|
||||
if (CHECK(err, "get_result",
|
||||
"failed to get output data: %d\n", err))
|
||||
goto cleanup;
|
||||
|
||||
equal = memcmp(data.out, test_case->output,
|
||||
equal = memcmp(data->out, test_case->output,
|
||||
test_case->output_len) == 0;
|
||||
if (CHECK(!equal, "check_result",
|
||||
"input/output data don't match\n")) {
|
||||
|
@ -548,12 +553,16 @@ void test_core_reloc(void)
|
|||
}
|
||||
for (j = 0; j < test_case->output_len; j++) {
|
||||
printf("output byte #%d: EXP 0x%02hhx GOT 0x%02hhx\n",
|
||||
j, test_case->output[j], data.out[j]);
|
||||
j, test_case->output[j], data->out[j]);
|
||||
}
|
||||
goto cleanup;
|
||||
}
|
||||
|
||||
cleanup:
|
||||
if (mmap_data) {
|
||||
CHECK_FAIL(munmap(mmap_data, mmap_sz));
|
||||
mmap_data = NULL;
|
||||
}
|
||||
if (!IS_ERR_OR_NULL(link)) {
|
||||
bpf_link__destroy(link);
|
||||
link = NULL;
|
||||
|
|
220
tools/testing/selftests/bpf/prog_tests/mmap.c
Normal file
220
tools/testing/selftests/bpf/prog_tests/mmap.c
Normal file
|
@ -0,0 +1,220 @@
|
|||
// SPDX-License-Identifier: GPL-2.0
|
||||
#include <test_progs.h>
|
||||
#include <sys/mman.h>
|
||||
|
||||
struct map_data {
|
||||
__u64 val[512 * 4];
|
||||
};
|
||||
|
||||
struct bss_data {
|
||||
__u64 in_val;
|
||||
__u64 out_val;
|
||||
};
|
||||
|
||||
static size_t roundup_page(size_t sz)
|
||||
{
|
||||
long page_size = sysconf(_SC_PAGE_SIZE);
|
||||
return (sz + page_size - 1) / page_size * page_size;
|
||||
}
|
||||
|
||||
void test_mmap(void)
|
||||
{
|
||||
const char *file = "test_mmap.o";
|
||||
const char *probe_name = "raw_tracepoint/sys_enter";
|
||||
const char *tp_name = "sys_enter";
|
||||
const size_t bss_sz = roundup_page(sizeof(struct bss_data));
|
||||
const size_t map_sz = roundup_page(sizeof(struct map_data));
|
||||
const int zero = 0, one = 1, two = 2, far = 1500;
|
||||
const long page_size = sysconf(_SC_PAGE_SIZE);
|
||||
int err, duration = 0, i, data_map_fd;
|
||||
struct bpf_program *prog;
|
||||
struct bpf_object *obj;
|
||||
struct bpf_link *link = NULL;
|
||||
struct bpf_map *data_map, *bss_map;
|
||||
void *bss_mmaped = NULL, *map_mmaped = NULL, *tmp1, *tmp2;
|
||||
volatile struct bss_data *bss_data;
|
||||
volatile struct map_data *map_data;
|
||||
__u64 val = 0;
|
||||
|
||||
obj = bpf_object__open_file("test_mmap.o", NULL);
|
||||
if (CHECK(IS_ERR(obj), "obj_open", "failed to open '%s': %ld\n",
|
||||
file, PTR_ERR(obj)))
|
||||
return;
|
||||
prog = bpf_object__find_program_by_title(obj, probe_name);
|
||||
if (CHECK(!prog, "find_probe", "prog '%s' not found\n", probe_name))
|
||||
goto cleanup;
|
||||
err = bpf_object__load(obj);
|
||||
if (CHECK(err, "obj_load", "failed to load prog '%s': %d\n",
|
||||
probe_name, err))
|
||||
goto cleanup;
|
||||
|
||||
bss_map = bpf_object__find_map_by_name(obj, "test_mma.bss");
|
||||
if (CHECK(!bss_map, "find_bss_map", ".bss map not found\n"))
|
||||
goto cleanup;
|
||||
data_map = bpf_object__find_map_by_name(obj, "data_map");
|
||||
if (CHECK(!data_map, "find_data_map", "data_map map not found\n"))
|
||||
goto cleanup;
|
||||
data_map_fd = bpf_map__fd(data_map);
|
||||
|
||||
bss_mmaped = mmap(NULL, bss_sz, PROT_READ | PROT_WRITE, MAP_SHARED,
|
||||
bpf_map__fd(bss_map), 0);
|
||||
if (CHECK(bss_mmaped == MAP_FAILED, "bss_mmap",
|
||||
".bss mmap failed: %d\n", errno)) {
|
||||
bss_mmaped = NULL;
|
||||
goto cleanup;
|
||||
}
|
||||
/* map as R/W first */
|
||||
map_mmaped = mmap(NULL, map_sz, PROT_READ | PROT_WRITE, MAP_SHARED,
|
||||
data_map_fd, 0);
|
||||
if (CHECK(map_mmaped == MAP_FAILED, "data_mmap",
|
||||
"data_map mmap failed: %d\n", errno)) {
|
||||
map_mmaped = NULL;
|
||||
goto cleanup;
|
||||
}
|
||||
|
||||
bss_data = bss_mmaped;
|
||||
map_data = map_mmaped;
|
||||
|
||||
CHECK_FAIL(bss_data->in_val);
|
||||
CHECK_FAIL(bss_data->out_val);
|
||||
CHECK_FAIL(map_data->val[0]);
|
||||
CHECK_FAIL(map_data->val[1]);
|
||||
CHECK_FAIL(map_data->val[2]);
|
||||
CHECK_FAIL(map_data->val[far]);
|
||||
|
||||
link = bpf_program__attach_raw_tracepoint(prog, tp_name);
|
||||
if (CHECK(IS_ERR(link), "attach_raw_tp", "err %ld\n", PTR_ERR(link)))
|
||||
goto cleanup;
|
||||
|
||||
bss_data->in_val = 123;
|
||||
val = 111;
|
||||
CHECK_FAIL(bpf_map_update_elem(data_map_fd, &zero, &val, 0));
|
||||
|
||||
usleep(1);
|
||||
|
||||
CHECK_FAIL(bss_data->in_val != 123);
|
||||
CHECK_FAIL(bss_data->out_val != 123);
|
||||
CHECK_FAIL(map_data->val[0] != 111);
|
||||
CHECK_FAIL(map_data->val[1] != 222);
|
||||
CHECK_FAIL(map_data->val[2] != 123);
|
||||
CHECK_FAIL(map_data->val[far] != 3 * 123);
|
||||
|
||||
CHECK_FAIL(bpf_map_lookup_elem(data_map_fd, &zero, &val));
|
||||
CHECK_FAIL(val != 111);
|
||||
CHECK_FAIL(bpf_map_lookup_elem(data_map_fd, &one, &val));
|
||||
CHECK_FAIL(val != 222);
|
||||
CHECK_FAIL(bpf_map_lookup_elem(data_map_fd, &two, &val));
|
||||
CHECK_FAIL(val != 123);
|
||||
CHECK_FAIL(bpf_map_lookup_elem(data_map_fd, &far, &val));
|
||||
CHECK_FAIL(val != 3 * 123);
|
||||
|
||||
/* data_map freeze should fail due to R/W mmap() */
|
||||
err = bpf_map_freeze(data_map_fd);
|
||||
if (CHECK(!err || errno != EBUSY, "no_freeze",
|
||||
"data_map freeze succeeded: err=%d, errno=%d\n", err, errno))
|
||||
goto cleanup;
|
||||
|
||||
/* unmap R/W mapping */
|
||||
err = munmap(map_mmaped, map_sz);
|
||||
map_mmaped = NULL;
|
||||
if (CHECK(err, "data_map_munmap", "data_map munmap failed: %d\n", errno))
|
||||
goto cleanup;
|
||||
|
||||
/* re-map as R/O now */
|
||||
map_mmaped = mmap(NULL, map_sz, PROT_READ, MAP_SHARED, data_map_fd, 0);
|
||||
if (CHECK(map_mmaped == MAP_FAILED, "data_mmap",
|
||||
"data_map R/O mmap failed: %d\n", errno)) {
|
||||
map_mmaped = NULL;
|
||||
goto cleanup;
|
||||
}
|
||||
map_data = map_mmaped;
|
||||
|
||||
/* map/unmap in a loop to test ref counting */
|
||||
for (i = 0; i < 10; i++) {
|
||||
int flags = i % 2 ? PROT_READ : PROT_WRITE;
|
||||
void *p;
|
||||
|
||||
p = mmap(NULL, map_sz, flags, MAP_SHARED, data_map_fd, 0);
|
||||
if (CHECK_FAIL(p == MAP_FAILED))
|
||||
goto cleanup;
|
||||
err = munmap(p, map_sz);
|
||||
if (CHECK_FAIL(err))
|
||||
goto cleanup;
|
||||
}
|
||||
|
||||
/* data_map freeze should now succeed due to no R/W mapping */
|
||||
err = bpf_map_freeze(data_map_fd);
|
||||
if (CHECK(err, "freeze", "data_map freeze failed: err=%d, errno=%d\n",
|
||||
err, errno))
|
||||
goto cleanup;
|
||||
|
||||
/* mapping as R/W now should fail */
|
||||
tmp1 = mmap(NULL, map_sz, PROT_READ | PROT_WRITE, MAP_SHARED,
|
||||
data_map_fd, 0);
|
||||
if (CHECK(tmp1 != MAP_FAILED, "data_mmap", "mmap succeeded\n")) {
|
||||
munmap(tmp1, map_sz);
|
||||
goto cleanup;
|
||||
}
|
||||
|
||||
bss_data->in_val = 321;
|
||||
usleep(1);
|
||||
CHECK_FAIL(bss_data->in_val != 321);
|
||||
CHECK_FAIL(bss_data->out_val != 321);
|
||||
CHECK_FAIL(map_data->val[0] != 111);
|
||||
CHECK_FAIL(map_data->val[1] != 222);
|
||||
CHECK_FAIL(map_data->val[2] != 321);
|
||||
CHECK_FAIL(map_data->val[far] != 3 * 321);
|
||||
|
||||
/* check some more advanced mmap() manipulations */
|
||||
|
||||
/* map all but last page: pages 1-3 mapped */
|
||||
tmp1 = mmap(NULL, 3 * page_size, PROT_READ, MAP_SHARED,
|
||||
data_map_fd, 0);
|
||||
if (CHECK(tmp1 == MAP_FAILED, "adv_mmap1", "errno %d\n", errno))
|
||||
goto cleanup;
|
||||
|
||||
/* unmap second page: pages 1, 3 mapped */
|
||||
err = munmap(tmp1 + page_size, page_size);
|
||||
if (CHECK(err, "adv_mmap2", "errno %d\n", errno)) {
|
||||
munmap(tmp1, map_sz);
|
||||
goto cleanup;
|
||||
}
|
||||
|
||||
/* map page 2 back */
|
||||
tmp2 = mmap(tmp1 + page_size, page_size, PROT_READ,
|
||||
MAP_SHARED | MAP_FIXED, data_map_fd, 0);
|
||||
if (CHECK(tmp2 == MAP_FAILED, "adv_mmap3", "errno %d\n", errno)) {
|
||||
munmap(tmp1, page_size);
|
||||
munmap(tmp1 + 2*page_size, page_size);
|
||||
goto cleanup;
|
||||
}
|
||||
CHECK(tmp1 + page_size != tmp2, "adv_mmap4",
|
||||
"tmp1: %p, tmp2: %p\n", tmp1, tmp2);
|
||||
|
||||
/* re-map all 4 pages */
|
||||
tmp2 = mmap(tmp1, 4 * page_size, PROT_READ, MAP_SHARED | MAP_FIXED,
|
||||
data_map_fd, 0);
|
||||
if (CHECK(tmp2 == MAP_FAILED, "adv_mmap5", "errno %d\n", errno)) {
|
||||
munmap(tmp1, 3 * page_size); /* unmap page 1 */
|
||||
goto cleanup;
|
||||
}
|
||||
CHECK(tmp1 != tmp2, "adv_mmap6", "tmp1: %p, tmp2: %p\n", tmp1, tmp2);
|
||||
|
||||
map_data = tmp2;
|
||||
CHECK_FAIL(bss_data->in_val != 321);
|
||||
CHECK_FAIL(bss_data->out_val != 321);
|
||||
CHECK_FAIL(map_data->val[0] != 111);
|
||||
CHECK_FAIL(map_data->val[1] != 222);
|
||||
CHECK_FAIL(map_data->val[2] != 321);
|
||||
CHECK_FAIL(map_data->val[far] != 3 * 321);
|
||||
|
||||
munmap(tmp2, 4 * page_size);
|
||||
cleanup:
|
||||
if (bss_mmaped)
|
||||
CHECK_FAIL(munmap(bss_mmaped, bss_sz));
|
||||
if (map_mmaped)
|
||||
CHECK_FAIL(munmap(map_mmaped, map_sz));
|
||||
if (!IS_ERR_OR_NULL(link))
|
||||
bpf_link__destroy(link);
|
||||
bpf_object__close(obj);
|
||||
}
|
45
tools/testing/selftests/bpf/progs/test_mmap.c
Normal file
45
tools/testing/selftests/bpf/progs/test_mmap.c
Normal file
|
@ -0,0 +1,45 @@
|
|||
// SPDX-License-Identifier: GPL-2.0
|
||||
// Copyright (c) 2019 Facebook
|
||||
|
||||
#include <linux/bpf.h>
|
||||
#include <stdint.h>
|
||||
#include "bpf_helpers.h"
|
||||
|
||||
char _license[] SEC("license") = "GPL";
|
||||
|
||||
struct {
|
||||
__uint(type, BPF_MAP_TYPE_ARRAY);
|
||||
__uint(max_entries, 512 * 4); /* at least 4 pages of data */
|
||||
__uint(map_flags, BPF_F_MMAPABLE);
|
||||
__type(key, __u32);
|
||||
__type(value, __u64);
|
||||
} data_map SEC(".maps");
|
||||
|
||||
static volatile __u64 in_val;
|
||||
static volatile __u64 out_val;
|
||||
|
||||
SEC("raw_tracepoint/sys_enter")
|
||||
int test_mmap(void *ctx)
|
||||
{
|
||||
int zero = 0, one = 1, two = 2, far = 1500;
|
||||
__u64 val, *p;
|
||||
|
||||
out_val = in_val;
|
||||
|
||||
/* data_map[2] = in_val; */
|
||||
bpf_map_update_elem(&data_map, &two, (const void *)&in_val, 0);
|
||||
|
||||
/* data_map[1] = data_map[0] * 2; */
|
||||
p = bpf_map_lookup_elem(&data_map, &zero);
|
||||
if (p) {
|
||||
val = (*p) * 2;
|
||||
bpf_map_update_elem(&data_map, &one, &val, 0);
|
||||
}
|
||||
|
||||
/* data_map[far] = in_val * 3; */
|
||||
val = in_val * 3;
|
||||
bpf_map_update_elem(&data_map, &far, &val, 0);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
Loading…
Add table
Add a link
Reference in a new issue