public inbox for netdev@vger.kernel.org 
 help / color / mirror / Atom feed
From: Daniel Borkmann <daniel@iogearbox•net>
To: Jesper Dangaard Brouer <brouer@redhat•com>, netdev@vger•kernel.org
Cc: jakub.kicinski@netronome•com,
	"Michael S. Tsirkin" <mst@redhat•com>,
	pavel.odintsov@gmail•com, Jason Wang <jasowang@redhat•com>,
	mchan@broadcom•com, John Fastabend <john.fastabend@gmail•com>,
	peter.waskiewicz.jr@intel•com,
	Daniel Borkmann <borkmann@iogearbox•net>,
	Alexei Starovoitov <alexei.starovoitov@gmail•com>,
	Andy Gospodarek <andy@greyhouse•net>
Subject: Re: [net-next V6 PATCH 1/5] bpf: introduce new bpf cpu map type BPF_MAP_TYPE_CPUMAP
Date: Wed, 11 Oct 2017 00:48:40 +0200	[thread overview]
Message-ID: <59DD4E48.3060403@iogearbox.net> (raw)
In-Reply-To: <150763965869.14394.6619644617101345170.stgit@firesoul>

On 10/10/2017 02:47 PM, Jesper Dangaard Brouer wrote:
[...]
> +static struct bpf_map *cpu_map_alloc(union bpf_attr *attr)
> +{
> +	struct bpf_cpu_map *cmap;
> +	int err = -ENOMEM;
> +	u64 cost;
> +	int ret;
> +
> +	if (!capable(CAP_SYS_ADMIN))
> +		return ERR_PTR(-EPERM);
> +
> +	/* check sanity of attributes */
> +	if (attr->max_entries == 0 || attr->key_size != 4 ||
> +	    attr->value_size != 4 || attr->map_flags & ~BPF_F_NUMA_NODE)
> +		return ERR_PTR(-EINVAL);
> +
> +	cmap = kzalloc(sizeof(*cmap), GFP_USER);
> +	if (!cmap)
> +		return ERR_PTR(-ENOMEM);
> +
> +	/* mandatory map attributes */
> +	cmap->map.map_type = attr->map_type;
> +	cmap->map.key_size = attr->key_size;
> +	cmap->map.value_size = attr->value_size;
> +	cmap->map.max_entries = attr->max_entries;
> +	cmap->map.map_flags = attr->map_flags;
> +	cmap->map.numa_node = bpf_map_attr_numa_node(attr);
> +
> +	/* Pre-limit array size based on NR_CPUS, not final CPU check */
> +	if (cmap->map.max_entries > NR_CPUS)
> +		return ERR_PTR(-E2BIG);

We still have a leak here, meaning kfree(cmap) is missing on above error.

> +
> +	/* make sure page count doesn't overflow */
> +	cost = (u64) cmap->map.max_entries * sizeof(struct bpf_cpu_map_entry *);
> +	cost += cpu_map_bitmap_size(attr) * num_possible_cpus();
> +	if (cost >= U32_MAX - PAGE_SIZE)
> +		goto free_cmap;
> +	cmap->map.pages = round_up(cost, PAGE_SIZE) >> PAGE_SHIFT;
> +
> +	/* Notice returns -EPERM on if map size is larger than memlock limit */
> +	ret = bpf_map_precharge_memlock(cmap->map.pages);
> +	if (ret) {
> +		err = ret;
> +		goto free_cmap;
> +	}
> +
> +	/* A per cpu bitfield with a bit per possible CPU in map  */
> +	cmap->flush_needed = __alloc_percpu(cpu_map_bitmap_size(attr),
> +					    __alignof__(unsigned long));
> +	if (!cmap->flush_needed)
> +		goto free_cmap;
> +
> +	/* Alloc array for possible remote "destination" CPUs */
> +	cmap->cpu_map = bpf_map_area_alloc(cmap->map.max_entries *
> +					   sizeof(struct bpf_cpu_map_entry *),
> +					   cmap->map.numa_node);
> +	if (!cmap->cpu_map)
> +		goto free_cmap;
> +
> +	return &cmap->map;
> +free_cmap:
> +	free_percpu(cmap->flush_needed);
> +	kfree(cmap);
> +	return ERR_PTR(err);
> +}

  reply	other threads:[~2017-10-10 22:48 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-10-10 12:47 [net-next V6 PATCH 0/5] New bpf cpumap type for XDP_REDIRECT Jesper Dangaard Brouer
2017-10-10 12:47 ` [net-next V6 PATCH 1/5] bpf: introduce new bpf cpu map type BPF_MAP_TYPE_CPUMAP Jesper Dangaard Brouer
2017-10-10 22:48   ` Daniel Borkmann [this message]
2017-10-11  5:36     ` Jesper Dangaard Brouer
2017-10-10 12:47 ` [net-next V6 PATCH 2/5] bpf: XDP_REDIRECT enable use of cpumap Jesper Dangaard Brouer
2017-10-10 12:47 ` [net-next V6 PATCH 3/5] bpf: cpumap xdp_buff to skb conversion and allocation Jesper Dangaard Brouer
2017-10-10 12:47 ` [net-next V6 PATCH 4/5] bpf: cpumap add tracepoints Jesper Dangaard Brouer
2017-10-10 12:47 ` [net-next V6 PATCH 5/5] samples/bpf: add cpumap sample program xdp_redirect_cpu Jesper Dangaard Brouer
2017-10-11  6:10 ` [net-next V6 PATCH 0/5] New bpf cpumap type for XDP_REDIRECT John Fastabend
2017-10-11  8:06   ` Jesper Dangaard Brouer

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=59DD4E48.3060403@iogearbox.net \
    --to=daniel@iogearbox$(echo .)net \
    --cc=alexei.starovoitov@gmail$(echo .)com \
    --cc=andy@greyhouse$(echo .)net \
    --cc=borkmann@iogearbox$(echo .)net \
    --cc=brouer@redhat$(echo .)com \
    --cc=jakub.kicinski@netronome$(echo .)com \
    --cc=jasowang@redhat$(echo .)com \
    --cc=john.fastabend@gmail$(echo .)com \
    --cc=mchan@broadcom$(echo .)com \
    --cc=mst@redhat$(echo .)com \
    --cc=netdev@vger$(echo .)kernel.org \
    --cc=pavel.odintsov@gmail$(echo .)com \
    --cc=peter.waskiewicz.jr@intel$(echo .)com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox