* Re: [PATCH bpf-next 0/4] Convert filter.txt to RST
From: Tobin C. Harding @ 2018-08-09 7:27 UTC (permalink / raw)
To: Alexei Starovoitov
Cc: Daniel Borkmann, Alexei Starovoitov, Jonathan Corbet,
David S. Miller, Kees Cook, Andy Lutomirski, Will Drewry,
linux-doc, netdev, linux-kernel
In-Reply-To: <20180809060734.rtqqu4sexbwzuqmm@ast-mbp>
On Wed, Aug 08, 2018 at 11:07:35PM -0700, Alexei Starovoitov wrote:
> On Thu, Aug 09, 2018 at 03:23:24PM +1000, Tobin C. Harding wrote:
> >
> > Daniel and Alexei, can I please have permission to add GPLv2+ to the BPF
> > docs?
>
> kernel licensing is GPLv2 without +
According to process/license-rules.rst
GPL-2.0+ : GNU General Public License v2.0 or later
> every file (including docs) can potentially have a different
> compatible license, but since they were developed
> implicitly under v2 only you would need to get a buy-in from
> all contributors before making such change.
So if a file does not _explicitly_ state that it is under another
licence it is ok to add GPLv2?
thanks,
Tobin.
^ permalink raw reply
* Re: [PATCH bpf-next 1/3] bpf: add bpf queue map
From: Alexei Starovoitov @ 2018-08-09 4:48 UTC (permalink / raw)
To: Mauricio Vasquez; +Cc: Alexei Starovoitov, Daniel Borkmann, netdev
In-Reply-To: <cf74d278-180b-1db1-4f5b-10f4177a4361@polito.it>
On Wed, Aug 08, 2018 at 10:08:47PM -0500, Mauricio Vasquez wrote:
>
> > And how about adding three new helpers: push/pop/peek as well?
> > Reusing lookup/update is neat, but does lookup == pop
> > or does lookup == peek ?
> > I suspect it will be confusing.
> > Three new helpers cost nothing, but will make bpf progs easier to read.
> I agree. I have one doubt here, update/lookup/delete is implemented in all
> map types, if the operation is not supported it returns -EINVAL.
> For push/pop/peek, should we implement it in all maps or is it better to
> check the map type before invoking map->ops->push/pop/seek?
> (Maybe checking if map->ops->xxx is NULL will also work)
Since push/pop/peak are only for this queue/stack I thought we won't
be adding 'ops' for them and just call the helpers from progs.
But now I'm having second thoughts, since 3 new commands for syscall
feels like overkill.
At the same time I still don't feel that lookup == pop is the right alias.
Also what peak is going to alias to ?
May be let's go back to your original idea with a tweak:
push == update
peak == lookup
pop = lookup + delete
In other words in case of stack the bpf_map_lookup_elem will return
the pointer to value of top element in the stack and
bpf_map_delete_elem will delete that top element.
Then in user space we can introduce push/pop always_inline functions like:
void bpf_push(struct bpf_map *map, void *value)
{
bpf_map_update_elem(map, NULL/*key*/, value);
}
void *bpf_pop(struct bpf_map *map)
{
void * val = bpf_map_lookup_elem(map, NULL/*key*/);
bpf_map_delete_elem(map, NULL/*key*/);
return val;
}
Thoughts?
^ permalink raw reply
* Re: [PATCH v1 6/7] net: mvneta: Don't use GRO on Armada 3720
From: Jisheng Zhang @ 2018-08-09 4:40 UTC (permalink / raw)
To: Marek Behún, Thomas Petazzoni, Andrew Lunn
Cc: linux-arm-kernel, netdev, Gregory CLEMENT, Tomas Hlavacek,
Russell King - ARM Linux, David S. Miller
In-Reply-To: <20180808152706.21727-7-marek.behun@nic.cz>
+ more people
On Wed, 8 Aug 2018 17:27:05 +0200 Marek Behún wrote:
> For some reason on Armada 3720 boards (EspressoBin and Turris Mox) the
> networking driver behaves weirdly when using napi_gro_receive.
>
> For example downloading a big file from a local network (low ping) is
> fast, but when downloading from a remote server (higher ping), the
> download speed is at first high but drops rapidly to almost nothing or
> absolutely nothing.
We also met this issue on some berlin platforms. I tried to fix the bug,
but no clue so far.
>
> This is fixed when using netif_receive_skb instead of napi_gro_receive.
This is a workaround. The good news is this workaround also fixes the issue
we saw on berlin.
>
> Signed-off-by: Marek Behun <marek.behun@nic.cz>
> Cc: Russell King - ARM Linux <linux@armlinux.org.uk>
> Cc: netdev@vger.kernel.org
>
> diff --git a/drivers/net/ethernet/marvell/mvneta.c b/drivers/net/ethernet/marvell/mvneta.c
> index 0ad2f3f7da85..27f3017d94c5 100644
> --- a/drivers/net/ethernet/marvell/mvneta.c
> +++ b/drivers/net/ethernet/marvell/mvneta.c
> @@ -1959,7 +1959,10 @@ static int mvneta_rx_swbm(struct mvneta_port *pp, int rx_todo,
>
> skb->protocol = eth_type_trans(skb, dev);
> mvneta_rx_csum(pp, rx_status, skb);
> - napi_gro_receive(&port->napi, skb);
> + if (pp->neta_armada3700)
> + netif_receive_skb(skb);
> + else
> + napi_gro_receive(&port->napi, skb);
>
> rcvd_pkts++;
> rcvd_bytes += rx_bytes;
> @@ -2001,7 +2004,10 @@ static int mvneta_rx_swbm(struct mvneta_port *pp, int rx_todo,
>
> mvneta_rx_csum(pp, rx_status, skb);
>
> - napi_gro_receive(&port->napi, skb);
> + if (pp->neta_armada3700)
> + netif_receive_skb(skb);
> + else
> + napi_gro_receive(&port->napi, skb);
> }
>
> if (rcvd_pkts) {
^ permalink raw reply
* Re: [PATCH mlx5-next 5/6] net/mlx5: Unexport functions that need not be exported
From: Leon Romanovsky @ 2018-08-09 4:34 UTC (permalink / raw)
To: Saeed Mahameed
Cc: netdev, linux-rdma, Jason Gunthorpe, David S. Miller,
Doug Ledford, Eli Cohen
In-Reply-To: <20180808232353.15514-6-saeedm@mellanox.com>
[-- Attachment #1: Type: text/plain, Size: 2025 bytes --]
On Wed, Aug 08, 2018 at 04:23:52PM -0700, Saeed Mahameed wrote:
> From: Eli Cohen <eli@mellanox.com>
>
> mlx5_query_vport_state() and mlx5_modify_vport_admin_state() are used
> only from within mlx5_core - unexport them.
>
> Signed-off-by: Eli Cohen <eli@mellanox.com>
> Reviewed-by: Or Gerlitz <ogerlitz@mellanox.com>
> Signed-off-by: Saeed Mahameed <saeedm@mellanox.com>
> ---
> drivers/net/ethernet/mellanox/mlx5/core/vport.c | 2 --
> 1 file changed, 2 deletions(-)
>
> diff --git a/drivers/net/ethernet/mellanox/mlx5/core/vport.c b/drivers/net/ethernet/mellanox/mlx5/core/vport.c
> index 7ec85747c450..702e235962aa 100644
> --- a/drivers/net/ethernet/mellanox/mlx5/core/vport.c
> +++ b/drivers/net/ethernet/mellanox/mlx5/core/vport.c
> @@ -62,7 +62,6 @@ u8 mlx5_query_vport_state(struct mlx5_core_dev *mdev, u8 opmod, u16 vport)
>
> return MLX5_GET(query_vport_state_out, out, state);
> }
> -EXPORT_SYMBOL_GPL(mlx5_query_vport_state);
>
Hi Saeed,
I see that Dave is already applied it, so my comment is mostly for
future submission. This patch went to a right direction by reducing
export symbol pollution, but left function declarations in globally
visible include/linux/mlx5/vport.h header file.
Such situation leaves the mlx5 driver in limbo state
where mlx5_ib have access to those functions but can't use them.
Care to send followup patch to fix it?
> int mlx5_modify_vport_admin_state(struct mlx5_core_dev *mdev, u8 opmod,
> u16 vport, u8 state)
> @@ -80,7 +79,6 @@ int mlx5_modify_vport_admin_state(struct mlx5_core_dev *mdev, u8 opmod,
>
> return mlx5_cmd_exec(mdev, in, sizeof(in), out, sizeof(out));
> }
> -EXPORT_SYMBOL_GPL(mlx5_modify_vport_admin_state);
>
> static int mlx5_query_nic_vport_context(struct mlx5_core_dev *mdev, u16 vport,
> u32 *out, int outlen)
> --
> 2.17.0
>
> --
> To unsubscribe from this list: send the line "unsubscribe linux-rdma" in
> the body of a message to majordomo@vger.kernel.org
> More majordomo info at http://vger.kernel.org/majordomo-info.html
[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 801 bytes --]
^ permalink raw reply
* [PATCH net-next] ieee802154: Use kmemdup instead of duplicating it in ca8210_test_int_driver_write
From: YueHaibing @ 2018-08-09 6:44 UTC (permalink / raw)
To: davem, h.morris, alex.aring, stefan
Cc: linux-kernel, netdev, linux-wpan, YueHaibing
Replace calls to kmalloc followed by a memcpy with a direct call to
kmemdup.
Signed-off-by: YueHaibing <yuehaibing@huawei.com>
---
drivers/net/ieee802154/ca8210.c | 3 +--
1 file changed, 1 insertion(+), 2 deletions(-)
diff --git a/drivers/net/ieee802154/ca8210.c b/drivers/net/ieee802154/ca8210.c
index 58299fb..e21279d 100644
--- a/drivers/net/ieee802154/ca8210.c
+++ b/drivers/net/ieee802154/ca8210.c
@@ -634,10 +634,9 @@ static int ca8210_test_int_driver_write(
for (i = 0; i < len; i++)
dev_dbg(&priv->spi->dev, "%#03x\n", buf[i]);
- fifo_buffer = kmalloc(len, GFP_KERNEL);
+ fifo_buffer = kmemdup(buf, len, GFP_KERNEL);
if (!fifo_buffer)
return -ENOMEM;
- memcpy(fifo_buffer, buf, len);
kfifo_in(&test->up_fifo, &fifo_buffer, 4);
wake_up_interruptible(&priv->test.readq);
--
2.7.0
^ permalink raw reply related
* Re: [PATCH bpf] bpf: fix bpffs non-array map seq_show issue
From: Yonghong Song @ 2018-08-09 3:55 UTC (permalink / raw)
To: Alexei Starovoitov; +Cc: ast, daniel, netdev, kernel-team
In-Reply-To: <20180809022530.yprizizentv7frmt@ast-mbp>
On 8/8/18 7:25 PM, Alexei Starovoitov wrote:
> On Wed, Aug 08, 2018 at 06:25:19PM -0700, Yonghong Song wrote:
>> In function map_seq_next() of kernel/bpf/inode.c,
>> the first key will be the "0" regardless of the map type.
>> This works for array. But for hash type, if it happens
>> key "0" is in the map, the bpffs map show will miss
>> some items if the key "0" is not the first element of
>> the first bucket.
>>
>> This patch fixed the issue by guaranteeing to get
>> the first element, if the seq_show is just started,
>> by passing NULL pointer key to map_get_next_key() callback.
>> This way, no missing elements will occur for
>> bpffs hash table show even if key "0" is in the map.
Currently, map_seq_show_elem callback is only implemented
for arraymap. So the problem actually is not exposed.
The issue is discovered when I tried to implement
map_seq_show_elem for hash maps, and I will have followup
patches for it.
So this patch probably should apply to bpf-next or
I can include this patch in my later patch set
which implements map_seq_show_elem for hash map
which can demonstrate the problem.
Please let me know.
>>
>> Fixes: a26ca7c982cb5 ("bpf: btf: Add pretty print support to the basic arraymap")
>> Signed-off-by: Yonghong Song <yhs@fb.com>
>
> Acked-by: Alexei Starovoitov <ast@kernel.org>
>
^ permalink raw reply
* [PATCH net-next] xen-netfront: fix warn message as irq device name has '/'
From: Xiao Liang @ 2018-08-09 6:03 UTC (permalink / raw)
To: netdev, xen-devel, davem, jgross, boris.ostrovsky
Cc: linux-kernel, Xiao Liang
There is a call trace generated after commit 2d408c0d4574b01b9ed45e02516888bf925e11a9(
xen-netfront: fix queue name setting). There is no 'device/vif/xx-q0-tx' file found
under /proc/irq/xx/.
This patch only picks up device type and id as its name.
With the patch, now /proc/interrupts looks like and the warning message gone:
70: 21 0 0 0 xen-dyn -event vif0-q0-tx
71: 15 0 0 0 xen-dyn -event vif0-q0-rx
72: 14 0 0 0 xen-dyn -event vif0-q1-tx
73: 33 0 0 0 xen-dyn -event vif0-q1-rx
74: 12 0 0 0 xen-dyn -event vif0-q2-tx
75: 24 0 0 0 xen-dyn -event vif0-q2-rx
76: 19 0 0 0 xen-dyn -event vif0-q3-tx
77: 21 0 0 0 xen-dyn -event vif0-q3-rx
Below is call trace information without this patch:
name 'device/vif/0-q0-tx'
WARNING: CPU: 2 PID: 37 at fs/proc/generic.c:174 __xlate_proc_name+0x85/0xa0
RIP: 0010:__xlate_proc_name+0x85/0xa0
RSP: 0018:ffffb85c40473c18 EFLAGS: 00010286
RAX: 0000000000000000 RBX: 0000000000000006 RCX: 0000000000000006
RDX: 0000000000000007 RSI: 0000000000000096 RDI: ffff984c7f516930
RBP: ffffb85c40473cb8 R08: 000000000000002c R09: 0000000000000229
R10: 0000000000000000 R11: 0000000000000001 R12: ffffb85c40473c98
R13: ffffb85c40473cb8 R14: ffffb85c40473c50 R15: 0000000000000000
FS: 0000000000000000(0000) GS:ffff984c7f500000(0000) knlGS:0000000000000000
CS: 0010 DS: 0000 ES: 0000 CR0: 0000000080050033
CR2: 00007f69b6899038 CR3: 000000001c20a006 CR4: 00000000001606e0
Call Trace:
__proc_create+0x45/0x230
? snprintf+0x49/0x60
proc_mkdir_data+0x35/0x90
register_handler_proc+0xef/0x110
? proc_register+0xfc/0x110
? proc_create_data+0x70/0xb0
__setup_irq+0x39b/0x660
? request_threaded_irq+0xad/0x160
request_threaded_irq+0xf5/0x160
? xennet_tx_buf_gc+0x1d0/0x1d0 [xen_netfront]
bind_evtchn_to_irqhandler+0x3d/0x70
? xenbus_alloc_evtchn+0x41/0xa0
netback_changed+0xa46/0xcda [xen_netfront]
? find_watch+0x40/0x40
xenwatch_thread+0xc5/0x160
? finish_wait+0x80/0x80
kthread+0x112/0x130
? kthread_create_worker_on_cpu+0x70/0x70
ret_from_fork+0x35/0x40
Code: 81 5c 00 48 85 c0 75 cc 5b 49 89 2e 31 c0 5d 4d 89 3c 24 41 5c 41 5d 41 5e 41 5f c3 4c 89 ee 48 c7 c7 40 4f 0e b4 e8 65 ea d8 ff <0f> 0b b8 fe ff ff ff 5b 5d 41 5c 41 5d 41 5e 41 5f c3 66 0f 1f
---[ end trace 650e5561b0caab3a ]---
Signed-off-by: Xiao Liang <xiliang@redhat.com>
---
drivers/net/xen-netfront.c | 6 ++++--
1 file changed, 4 insertions(+), 2 deletions(-)
diff --git a/drivers/net/xen-netfront.c b/drivers/net/xen-netfront.c
index 799cba4624a5..6f40df4a452e 100644
--- a/drivers/net/xen-netfront.c
+++ b/drivers/net/xen-netfront.c
@@ -1604,14 +1604,16 @@ static int xennet_init_queue(struct netfront_queue *queue)
{
unsigned short i;
int err = 0;
+ int devid = 0;
spin_lock_init(&queue->tx_lock);
spin_lock_init(&queue->rx_lock);
timer_setup(&queue->rx_refill_timer, rx_refill_timeout, 0);
- snprintf(queue->name, sizeof(queue->name), "%s-q%u",
- queue->info->xbdev->nodename, queue->id);
+ kstrtoint(strrchr(queue->info->xbdev->nodename,'/')+1, 10, &devid);
+ snprintf(queue->name, sizeof(queue->name), "vif%d-q%u",
+ devid, queue->id);
/* Initialise tx_skbs as a free chain containing every entry. */
queue->tx_skb_freelist = 0;
--
2.17.1
^ permalink raw reply related
* Re: [PATCH bpf-next 1/3] bpf: add bpf queue map
From: Mauricio Vasquez @ 2018-08-09 3:08 UTC (permalink / raw)
To: Alexei Starovoitov; +Cc: Alexei Starovoitov, Daniel Borkmann, netdev
In-Reply-To: <20180807144226.pmalhe3mjvc3a45y@ast-mbp.dhcp.thefacebook.com>
On 08/07/2018 09:42 AM, Alexei Starovoitov wrote:
> On Mon, Aug 06, 2018 at 03:58:30PM +0200, Mauricio Vasquez B wrote:
>> Bpf queue implements a LIFO/FIFO data containers for ebpf programs.
> queue/stack datastructure would be a great addition.
>
>> It allows to push an element to the queue by using the update operation
>> and to pop an element from the queue by using the lookup operation.
>>
>> A use case for this is to keep track of a pool of elements, like
>> network ports in a SNAT.
>>
>> Signed-off-by: Mauricio Vasquez B <mauricio.vasquez@polito.it>
>> ---
>> include/linux/bpf_types.h | 1
>> include/uapi/linux/bpf.h | 5 +
>> kernel/bpf/Makefile | 2
>> kernel/bpf/queuemap.c | 287 +++++++++++++++++++++++++++++++++++++++++++++
>> kernel/bpf/syscall.c | 61 +++++++---
>> kernel/bpf/verifier.c | 16 ++-
>> 6 files changed, 353 insertions(+), 19 deletions(-)
>> create mode 100644 kernel/bpf/queuemap.c
>>
>> diff --git a/include/linux/bpf_types.h b/include/linux/bpf_types.h
>> index c5700c2d5549..6c7a62f3fe43 100644
>> --- a/include/linux/bpf_types.h
>> +++ b/include/linux/bpf_types.h
>> @@ -58,3 +58,4 @@ BPF_MAP_TYPE(BPF_MAP_TYPE_CPUMAP, cpu_map_ops)
>> BPF_MAP_TYPE(BPF_MAP_TYPE_XSKMAP, xsk_map_ops)
>> #endif
>> #endif
>> +BPF_MAP_TYPE(BPF_MAP_TYPE_QUEUE, queue_map_ops)
>> diff --git a/include/uapi/linux/bpf.h b/include/uapi/linux/bpf.h
>> index 0ebaaf7f3568..2c171c40eb45 100644
>> --- a/include/uapi/linux/bpf.h
>> +++ b/include/uapi/linux/bpf.h
>> @@ -120,6 +120,7 @@ enum bpf_map_type {
>> BPF_MAP_TYPE_CPUMAP,
>> BPF_MAP_TYPE_XSKMAP,
>> BPF_MAP_TYPE_SOCKHASH,
>> + BPF_MAP_TYPE_QUEUE,
>> };
>>
>> enum bpf_prog_type {
>> @@ -255,6 +256,10 @@ enum bpf_attach_type {
>> /* Flag for stack_map, store build_id+offset instead of pointer */
>> #define BPF_F_STACK_BUILD_ID (1U << 5)
>>
>> +/* Flags for queue_map, type of queue */
>> +#define BPF_F_QUEUE_FIFO (1U << 16)
>> +#define BPF_F_QUEUE_LIFO (2U << 16)
> the choice of flags looks odd.
> Why start at bit 16 and why waste two bits?
> It's either stack or queue.
> May be instead define two map_types:
> BPF_MAP_TYPE_QUEUE and BPF_MAP_TYPE_STACK
> that share common implementation?
I like this solution, I'll spin a v2 with two different types of maps.
> And how about adding three new helpers: push/pop/peek as well?
> Reusing lookup/update is neat, but does lookup == pop
> or does lookup == peek ?
> I suspect it will be confusing.
> Three new helpers cost nothing, but will make bpf progs easier to read.
I agree. I have one doubt here, update/lookup/delete is implemented in
all map types, if the operation is not supported it returns -EINVAL.
For push/pop/peek, should we implement it in all maps or is it better to
check the map type before invoking map->ops->push/pop/seek?
(Maybe checking if map->ops->xxx is NULL will also work)
> Could you also add a flag for replacement policy?
> In this implementation when max_entries limit is reached
> the map_update_elem (aka push) will fail with e2big.
> It would be useful to allow pushing and dropping elements from
> the other side then such queue/stack can be used to keep
> track of the last N events (the prog would unconditionally push
> and user space would swap the queue for new one via map-in-map
> and drain old queue).
I like it, will do.
> Speaking of map-in-map, please add a test to make sure
> queue/stack works with hash/array_of_maps.
Sure.
> selftests in patch2 needs to have kernel side as well.
> it's not enough to test syscall access only.
>
Will do.
^ permalink raw reply
* [PATCH net-next v2] net: allow to call netif_reset_xps_queues() under cpus_read_lock
From: Andrei Vagin @ 2018-08-09 3:07 UTC (permalink / raw)
To: David S. Miller
Cc: netdev, Andrei Vagin, Nambiar, Amritha, Michael S. Tsirkin,
Jason Wang
From: Andrei Vagin <avagin@gmail.com>
The definition of static_key_slow_inc() has cpus_read_lock in place. In the
virtio_net driver, XPS queues are initialized after setting the queue:cpu
affinity in virtnet_set_affinity() which is already protected within
cpus_read_lock. Lockdep prints a warning when we are trying to acquire
cpus_read_lock when it is already held.
This patch adds an ability to call __netif_set_xps_queue under
cpus_read_lock().
============================================
WARNING: possible recursive locking detected
4.18.0-rc3-next-20180703+ #1 Not tainted
--------------------------------------------
swapper/0/1 is trying to acquire lock:
00000000cf973d46 (cpu_hotplug_lock.rw_sem){++++}, at: static_key_slow_inc+0xe/0x20
but task is already holding lock:
00000000cf973d46 (cpu_hotplug_lock.rw_sem){++++}, at: init_vqs+0x513/0x5a0
other info that might help us debug this:
Possible unsafe locking scenario:
CPU0
----
lock(cpu_hotplug_lock.rw_sem);
lock(cpu_hotplug_lock.rw_sem);
*** DEADLOCK ***
May be due to missing lock nesting notation
3 locks held by swapper/0/1:
#0: 00000000244bc7da (&dev->mutex){....}, at: __driver_attach+0x5a/0x110
#1: 00000000cf973d46 (cpu_hotplug_lock.rw_sem){++++}, at: init_vqs+0x513/0x5a0
#2: 000000005cd8463f (xps_map_mutex){+.+.}, at: __netif_set_xps_queue+0x8d/0xc60
v2: move cpus_read_lock() out of __netif_set_xps_queue()
Cc: "Nambiar, Amritha" <amritha.nambiar@intel.com>
Cc: "Michael S. Tsirkin" <mst@redhat.com>
Cc: Jason Wang <jasowang@redhat.com>
Fixes: 8af2c06ff4b1 ("net-sysfs: Add interface for Rx queue(s) map per Tx queue")
Signed-off-by: Andrei Vagin <avagin@gmail.com>
---
drivers/net/virtio_net.c | 4 +++-
net/core/dev.c | 20 +++++++++++++++-----
net/core/net-sysfs.c | 4 ++++
3 files changed, 22 insertions(+), 6 deletions(-)
diff --git a/drivers/net/virtio_net.c b/drivers/net/virtio_net.c
index 62311dde6e71..39a7f4452587 100644
--- a/drivers/net/virtio_net.c
+++ b/drivers/net/virtio_net.c
@@ -1903,9 +1903,11 @@ static void virtnet_set_affinity(struct virtnet_info *vi)
i = 0;
for_each_online_cpu(cpu) {
+ const unsigned long *mask = cpumask_bits(cpumask_of(cpu));
+
virtqueue_set_affinity(vi->rq[i].vq, cpu);
virtqueue_set_affinity(vi->sq[i].vq, cpu);
- netif_set_xps_queue(vi->dev, cpumask_of(cpu), i);
+ __netif_set_xps_queue(vi->dev, mask, i, false);
i++;
}
diff --git a/net/core/dev.c b/net/core/dev.c
index f68122f0ab02..325fc5088370 100644
--- a/net/core/dev.c
+++ b/net/core/dev.c
@@ -2176,6 +2176,7 @@ static void netif_reset_xps_queues(struct net_device *dev, u16 offset,
if (!static_key_false(&xps_needed))
return;
+ cpus_read_lock();
mutex_lock(&xps_map_mutex);
if (static_key_false(&xps_rxqs_needed)) {
@@ -2199,10 +2200,11 @@ static void netif_reset_xps_queues(struct net_device *dev, u16 offset,
out_no_maps:
if (static_key_enabled(&xps_rxqs_needed))
- static_key_slow_dec(&xps_rxqs_needed);
+ static_key_slow_dec_cpuslocked(&xps_rxqs_needed);
- static_key_slow_dec(&xps_needed);
+ static_key_slow_dec_cpuslocked(&xps_needed);
mutex_unlock(&xps_map_mutex);
+ cpus_read_unlock();
}
static void netif_reset_xps_queues_gt(struct net_device *dev, u16 index)
@@ -2250,6 +2252,7 @@ static struct xps_map *expand_xps_map(struct xps_map *map, int attr_index,
return new_map;
}
+/* Must be called under cpus_read_lock */
int __netif_set_xps_queue(struct net_device *dev, const unsigned long *mask,
u16 index, bool is_rxqs_map)
{
@@ -2317,9 +2320,9 @@ int __netif_set_xps_queue(struct net_device *dev, const unsigned long *mask,
if (!new_dev_maps)
goto out_no_new_maps;
- static_key_slow_inc(&xps_needed);
+ static_key_slow_inc_cpuslocked(&xps_needed);
if (is_rxqs_map)
- static_key_slow_inc(&xps_rxqs_needed);
+ static_key_slow_inc_cpuslocked(&xps_rxqs_needed);
for (j = -1; j = netif_attrmask_next(j, possible_mask, nr_ids),
j < nr_ids;) {
@@ -2448,11 +2451,18 @@ int __netif_set_xps_queue(struct net_device *dev, const unsigned long *mask,
kfree(new_dev_maps);
return -ENOMEM;
}
+EXPORT_SYMBOL_GPL(__netif_set_xps_queue);
int netif_set_xps_queue(struct net_device *dev, const struct cpumask *mask,
u16 index)
{
- return __netif_set_xps_queue(dev, cpumask_bits(mask), index, false);
+ int ret;
+
+ cpus_read_lock();
+ ret = __netif_set_xps_queue(dev, cpumask_bits(mask), index, false);
+ cpus_read_unlock();
+
+ return ret;
}
EXPORT_SYMBOL(netif_set_xps_queue);
diff --git a/net/core/net-sysfs.c b/net/core/net-sysfs.c
index 0a95bcf64cdc..bd67c4d0fcfd 100644
--- a/net/core/net-sysfs.c
+++ b/net/core/net-sysfs.c
@@ -26,6 +26,7 @@
#include <linux/pm_runtime.h>
#include <linux/of.h>
#include <linux/of_net.h>
+#include <linux/cpu.h>
#include "net-sysfs.h"
@@ -1400,7 +1401,10 @@ static ssize_t xps_rxqs_store(struct netdev_queue *queue, const char *buf,
return err;
}
+ cpus_read_lock();
err = __netif_set_xps_queue(dev, mask, index, true);
+ cpus_read_unlock();
+
kfree(mask);
return err ? : len;
}
--
2.17.1
^ permalink raw reply related
* [PATCH bpf-next 4/4] docs: Remove filter.txt from the tree
From: Tobin C. Harding @ 2018-08-09 5:23 UTC (permalink / raw)
To: Daniel Borkmann, Alexei Starovoitov
Cc: Tobin C. Harding, Jonathan Corbet, David S. Miller, Kees Cook,
Andy Lutomirski, Will Drewry, linux-doc, netdev, linux-kernel
In-Reply-To: <20180809052328.27942-1-me@tobin.cc>
We just split up the content of filter.txt into three RST files. We can
remove filter.txt now.
Remove filter.txt
Signed-off-by: Tobin C. Harding <me@tobin.cc>
---
Documentation/networking/filter.txt | 1480 ---------------------------
1 file changed, 1480 deletions(-)
delete mode 100644 Documentation/networking/filter.txt
diff --git a/Documentation/networking/filter.txt b/Documentation/networking/filter.txt
deleted file mode 100644
index 1fe4adf9c4c6..000000000000
--- a/Documentation/networking/filter.txt
+++ /dev/null
@@ -1,1480 +0,0 @@
-Linux Socket Filtering aka Berkeley Packet Filter (BPF)
-=======================================================
-
-Introduction
-------------
-
-Linux Socket Filtering (LSF) is derived from the Berkeley Packet Filter.
-Though there are some distinct differences between the BSD and Linux
-Kernel filtering, but when we speak of BPF or LSF in Linux context, we
-mean the very same mechanism of filtering in the Linux kernel.
-
-BPF allows a user-space program to attach a filter onto any socket and
-allow or disallow certain types of data to come through the socket. LSF
-follows exactly the same filter code structure as BSD's BPF, so referring
-to the BSD bpf.4 manpage is very helpful in creating filters.
-
-On Linux, BPF is much simpler than on BSD. One does not have to worry
-about devices or anything like that. You simply create your filter code,
-send it to the kernel via the SO_ATTACH_FILTER option and if your filter
-code passes the kernel check on it, you then immediately begin filtering
-data on that socket.
-
-You can also detach filters from your socket via the SO_DETACH_FILTER
-option. This will probably not be used much since when you close a socket
-that has a filter on it the filter is automagically removed. The other
-less common case may be adding a different filter on the same socket where
-you had another filter that is still running: the kernel takes care of
-removing the old one and placing your new one in its place, assuming your
-filter has passed the checks, otherwise if it fails the old filter will
-remain on that socket.
-
-SO_LOCK_FILTER option allows locking of the filter attached to a socket.
-Once set, a filter cannot be removed or changed. This allows one process to
-setup a socket, attach a filter, lock it then drop privileges and be
-assured that the filter will be kept until the socket is closed.
-
-The biggest user of this construct might be libpcap. Issuing a high-level
-filter command like `tcpdump -i em1 port 22` passes through the libpcap
-internal compiler that generates a structure that can eventually be loaded
-via SO_ATTACH_FILTER to the kernel. `tcpdump -i em1 port 22 -ddd`
-displays what is being placed into this structure.
-
-Although we were only speaking about sockets here, BPF in Linux is used
-in many more places. There's xt_bpf for netfilter, cls_bpf in the kernel
-qdisc layer, SECCOMP-BPF (SECure COMPuting [1]), and lots of other places
-such as team driver, PTP code, etc where BPF is being used.
-
- [1] Documentation/userspace-api/seccomp_filter.rst
-
-Original BPF paper:
-
-Steven McCanne and Van Jacobson. 1993. The BSD packet filter: a new
-architecture for user-level packet capture. In Proceedings of the
-USENIX Winter 1993 Conference Proceedings on USENIX Winter 1993
-Conference Proceedings (USENIX'93). USENIX Association, Berkeley,
-CA, USA, 2-2. [http://www.tcpdump.org/papers/bpf-usenix93.pdf]
-
-Structure
----------
-
-User space applications include <linux/filter.h> which contains the
-following relevant structures:
-
-struct sock_filter { /* Filter block */
- __u16 code; /* Actual filter code */
- __u8 jt; /* Jump true */
- __u8 jf; /* Jump false */
- __u32 k; /* Generic multiuse field */
-};
-
-Such a structure is assembled as an array of 4-tuples, that contains
-a code, jt, jf and k value. jt and jf are jump offsets and k a generic
-value to be used for a provided code.
-
-struct sock_fprog { /* Required for SO_ATTACH_FILTER. */
- unsigned short len; /* Number of filter blocks */
- struct sock_filter __user *filter;
-};
-
-For socket filtering, a pointer to this structure (as shown in
-follow-up example) is being passed to the kernel through setsockopt(2).
-
-Example
--------
-
-#include <sys/socket.h>
-#include <sys/types.h>
-#include <arpa/inet.h>
-#include <linux/if_ether.h>
-/* ... */
-
-/* From the example above: tcpdump -i em1 port 22 -dd */
-struct sock_filter code[] = {
- { 0x28, 0, 0, 0x0000000c },
- { 0x15, 0, 8, 0x000086dd },
- { 0x30, 0, 0, 0x00000014 },
- { 0x15, 2, 0, 0x00000084 },
- { 0x15, 1, 0, 0x00000006 },
- { 0x15, 0, 17, 0x00000011 },
- { 0x28, 0, 0, 0x00000036 },
- { 0x15, 14, 0, 0x00000016 },
- { 0x28, 0, 0, 0x00000038 },
- { 0x15, 12, 13, 0x00000016 },
- { 0x15, 0, 12, 0x00000800 },
- { 0x30, 0, 0, 0x00000017 },
- { 0x15, 2, 0, 0x00000084 },
- { 0x15, 1, 0, 0x00000006 },
- { 0x15, 0, 8, 0x00000011 },
- { 0x28, 0, 0, 0x00000014 },
- { 0x45, 6, 0, 0x00001fff },
- { 0xb1, 0, 0, 0x0000000e },
- { 0x48, 0, 0, 0x0000000e },
- { 0x15, 2, 0, 0x00000016 },
- { 0x48, 0, 0, 0x00000010 },
- { 0x15, 0, 1, 0x00000016 },
- { 0x06, 0, 0, 0x0000ffff },
- { 0x06, 0, 0, 0x00000000 },
-};
-
-struct sock_fprog bpf = {
- .len = ARRAY_SIZE(code),
- .filter = code,
-};
-
-sock = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
-if (sock < 0)
- /* ... bail out ... */
-
-ret = setsockopt(sock, SOL_SOCKET, SO_ATTACH_FILTER, &bpf, sizeof(bpf));
-if (ret < 0)
- /* ... bail out ... */
-
-/* ... */
-close(sock);
-
-The above example code attaches a socket filter for a PF_PACKET socket
-in order to let all IPv4/IPv6 packets with port 22 pass. The rest will
-be dropped for this socket.
-
-The setsockopt(2) call to SO_DETACH_FILTER doesn't need any arguments
-and SO_LOCK_FILTER for preventing the filter to be detached, takes an
-integer value with 0 or 1.
-
-Note that socket filters are not restricted to PF_PACKET sockets only,
-but can also be used on other socket families.
-
-Summary of system calls:
-
- * setsockopt(sockfd, SOL_SOCKET, SO_ATTACH_FILTER, &val, sizeof(val));
- * setsockopt(sockfd, SOL_SOCKET, SO_DETACH_FILTER, &val, sizeof(val));
- * setsockopt(sockfd, SOL_SOCKET, SO_LOCK_FILTER, &val, sizeof(val));
-
-Normally, most use cases for socket filtering on packet sockets will be
-covered by libpcap in high-level syntax, so as an application developer
-you should stick to that. libpcap wraps its own layer around all that.
-
-Unless i) using/linking to libpcap is not an option, ii) the required BPF
-filters use Linux extensions that are not supported by libpcap's compiler,
-iii) a filter might be more complex and not cleanly implementable with
-libpcap's compiler, or iv) particular filter codes should be optimized
-differently than libpcap's internal compiler does; then in such cases
-writing such a filter "by hand" can be of an alternative. For example,
-xt_bpf and cls_bpf users might have requirements that could result in
-more complex filter code, or one that cannot be expressed with libpcap
-(e.g. different return codes for various code paths). Moreover, BPF JIT
-implementors may wish to manually write test cases and thus need low-level
-access to BPF code as well.
-
-BPF engine and instruction set
-------------------------------
-
-Under tools/bpf/ there's a small helper tool called bpf_asm which can
-be used to write low-level filters for example scenarios mentioned in the
-previous section. Asm-like syntax mentioned here has been implemented in
-bpf_asm and will be used for further explanations (instead of dealing with
-less readable opcodes directly, principles are the same). The syntax is
-closely modelled after Steven McCanne's and Van Jacobson's BPF paper.
-
-The BPF architecture consists of the following basic elements:
-
- Element Description
-
- A 32 bit wide accumulator
- X 32 bit wide X register
- M[] 16 x 32 bit wide misc registers aka "scratch memory
- store", addressable from 0 to 15
-
-A program, that is translated by bpf_asm into "opcodes" is an array that
-consists of the following elements (as already mentioned):
-
- op:16, jt:8, jf:8, k:32
-
-The element op is a 16 bit wide opcode that has a particular instruction
-encoded. jt and jf are two 8 bit wide jump targets, one for condition
-"jump if true", the other one "jump if false". Eventually, element k
-contains a miscellaneous argument that can be interpreted in different
-ways depending on the given instruction in op.
-
-The instruction set consists of load, store, branch, alu, miscellaneous
-and return instructions that are also represented in bpf_asm syntax. This
-table lists all bpf_asm instructions available resp. what their underlying
-opcodes as defined in linux/filter.h stand for:
-
- Instruction Addressing mode Description
-
- ld 1, 2, 3, 4, 10 Load word into A
- ldi 4 Load word into A
- ldh 1, 2 Load half-word into A
- ldb 1, 2 Load byte into A
- ldx 3, 4, 5, 10 Load word into X
- ldxi 4 Load word into X
- ldxb 5 Load byte into X
-
- st 3 Store A into M[]
- stx 3 Store X into M[]
-
- jmp 6 Jump to label
- ja 6 Jump to label
- jeq 7, 8 Jump on A == k
- jneq 8 Jump on A != k
- jne 8 Jump on A != k
- jlt 8 Jump on A < k
- jle 8 Jump on A <= k
- jgt 7, 8 Jump on A > k
- jge 7, 8 Jump on A >= k
- jset 7, 8 Jump on A & k
-
- add 0, 4 A + <x>
- sub 0, 4 A - <x>
- mul 0, 4 A * <x>
- div 0, 4 A / <x>
- mod 0, 4 A % <x>
- neg !A
- and 0, 4 A & <x>
- or 0, 4 A | <x>
- xor 0, 4 A ^ <x>
- lsh 0, 4 A << <x>
- rsh 0, 4 A >> <x>
-
- tax Copy A into X
- txa Copy X into A
-
- ret 4, 9 Return
-
-The next table shows addressing formats from the 2nd column:
-
- Addressing mode Syntax Description
-
- 0 x/%x Register X
- 1 [k] BHW at byte offset k in the packet
- 2 [x + k] BHW at the offset X + k in the packet
- 3 M[k] Word at offset k in M[]
- 4 #k Literal value stored in k
- 5 4*([k]&0xf) Lower nibble * 4 at byte offset k in the packet
- 6 L Jump label L
- 7 #k,Lt,Lf Jump to Lt if true, otherwise jump to Lf
- 8 #k,Lt Jump to Lt if predicate is true
- 9 a/%a Accumulator A
- 10 extension BPF extension
-
-The Linux kernel also has a couple of BPF extensions that are used along
-with the class of load instructions by "overloading" the k argument with
-a negative offset + a particular extension offset. The result of such BPF
-extensions are loaded into A.
-
-Possible BPF extensions are shown in the following table:
-
- Extension Description
-
- len skb->len
- proto skb->protocol
- type skb->pkt_type
- poff Payload start offset
- ifidx skb->dev->ifindex
- nla Netlink attribute of type X with offset A
- nlan Nested Netlink attribute of type X with offset A
- mark skb->mark
- queue skb->queue_mapping
- hatype skb->dev->type
- rxhash skb->hash
- cpu raw_smp_processor_id()
- vlan_tci skb_vlan_tag_get(skb)
- vlan_avail skb_vlan_tag_present(skb)
- vlan_tpid skb->vlan_proto
- rand prandom_u32()
-
-These extensions can also be prefixed with '#'.
-Examples for low-level BPF:
-
-** ARP packets:
-
- ldh [12]
- jne #0x806, drop
- ret #-1
- drop: ret #0
-
-** IPv4 TCP packets:
-
- ldh [12]
- jne #0x800, drop
- ldb [23]
- jneq #6, drop
- ret #-1
- drop: ret #0
-
-** (Accelerated) VLAN w/ id 10:
-
- ld vlan_tci
- jneq #10, drop
- ret #-1
- drop: ret #0
-
-** icmp random packet sampling, 1 in 4
- ldh [12]
- jne #0x800, drop
- ldb [23]
- jneq #1, drop
- # get a random uint32 number
- ld rand
- mod #4
- jneq #1, drop
- ret #-1
- drop: ret #0
-
-** SECCOMP filter example:
-
- ld [4] /* offsetof(struct seccomp_data, arch) */
- jne #0xc000003e, bad /* AUDIT_ARCH_X86_64 */
- ld [0] /* offsetof(struct seccomp_data, nr) */
- jeq #15, good /* __NR_rt_sigreturn */
- jeq #231, good /* __NR_exit_group */
- jeq #60, good /* __NR_exit */
- jeq #0, good /* __NR_read */
- jeq #1, good /* __NR_write */
- jeq #5, good /* __NR_fstat */
- jeq #9, good /* __NR_mmap */
- jeq #14, good /* __NR_rt_sigprocmask */
- jeq #13, good /* __NR_rt_sigaction */
- jeq #35, good /* __NR_nanosleep */
- bad: ret #0 /* SECCOMP_RET_KILL_THREAD */
- good: ret #0x7fff0000 /* SECCOMP_RET_ALLOW */
-
-The above example code can be placed into a file (here called "foo"), and
-then be passed to the bpf_asm tool for generating opcodes, output that xt_bpf
-and cls_bpf understands and can directly be loaded with. Example with above
-ARP code:
-
-$ ./bpf_asm foo
-4,40 0 0 12,21 0 1 2054,6 0 0 4294967295,6 0 0 0,
-
-In copy and paste C-like output:
-
-$ ./bpf_asm -c foo
-{ 0x28, 0, 0, 0x0000000c },
-{ 0x15, 0, 1, 0x00000806 },
-{ 0x06, 0, 0, 0xffffffff },
-{ 0x06, 0, 0, 0000000000 },
-
-In particular, as usage with xt_bpf or cls_bpf can result in more complex BPF
-filters that might not be obvious at first, it's good to test filters before
-attaching to a live system. For that purpose, there's a small tool called
-bpf_dbg under tools/bpf/ in the kernel source directory. This debugger allows
-for testing BPF filters against given pcap files, single stepping through the
-BPF code on the pcap's packets and to do BPF machine register dumps.
-
-Starting bpf_dbg is trivial and just requires issuing:
-
-# ./bpf_dbg
-
-In case input and output do not equal stdin/stdout, bpf_dbg takes an
-alternative stdin source as a first argument, and an alternative stdout
-sink as a second one, e.g. `./bpf_dbg test_in.txt test_out.txt`.
-
-Other than that, a particular libreadline configuration can be set via
-file "~/.bpf_dbg_init" and the command history is stored in the file
-"~/.bpf_dbg_history".
-
-Interaction in bpf_dbg happens through a shell that also has auto-completion
-support (follow-up example commands starting with '>' denote bpf_dbg shell).
-The usual workflow would be to ...
-
-> load bpf 6,40 0 0 12,21 0 3 2048,48 0 0 23,21 0 1 1,6 0 0 65535,6 0 0 0
- Loads a BPF filter from standard output of bpf_asm, or transformed via
- e.g. `tcpdump -iem1 -ddd port 22 | tr '\n' ','`. Note that for JIT
- debugging (next section), this command creates a temporary socket and
- loads the BPF code into the kernel. Thus, this will also be useful for
- JIT developers.
-
-> load pcap foo.pcap
- Loads standard tcpdump pcap file.
-
-> run [<n>]
-bpf passes:1 fails:9
- Runs through all packets from a pcap to account how many passes and fails
- the filter will generate. A limit of packets to traverse can be given.
-
-> disassemble
-l0: ldh [12]
-l1: jeq #0x800, l2, l5
-l2: ldb [23]
-l3: jeq #0x1, l4, l5
-l4: ret #0xffff
-l5: ret #0
- Prints out BPF code disassembly.
-
-> dump
-/* { op, jt, jf, k }, */
-{ 0x28, 0, 0, 0x0000000c },
-{ 0x15, 0, 3, 0x00000800 },
-{ 0x30, 0, 0, 0x00000017 },
-{ 0x15, 0, 1, 0x00000001 },
-{ 0x06, 0, 0, 0x0000ffff },
-{ 0x06, 0, 0, 0000000000 },
- Prints out C-style BPF code dump.
-
-> breakpoint 0
-breakpoint at: l0: ldh [12]
-> breakpoint 1
-breakpoint at: l1: jeq #0x800, l2, l5
- ...
- Sets breakpoints at particular BPF instructions. Issuing a `run` command
- will walk through the pcap file continuing from the current packet and
- break when a breakpoint is being hit (another `run` will continue from
- the currently active breakpoint executing next instructions):
-
- > run
- -- register dump --
- pc: [0] <-- program counter
- code: [40] jt[0] jf[0] k[12] <-- plain BPF code of current instruction
- curr: l0: ldh [12] <-- disassembly of current instruction
- A: [00000000][0] <-- content of A (hex, decimal)
- X: [00000000][0] <-- content of X (hex, decimal)
- M[0,15]: [00000000][0] <-- folded content of M (hex, decimal)
- -- packet dump -- <-- Current packet from pcap (hex)
- len: 42
- 0: 00 19 cb 55 55 a4 00 14 a4 43 78 69 08 06 00 01
- 16: 08 00 06 04 00 01 00 14 a4 43 78 69 0a 3b 01 26
- 32: 00 00 00 00 00 00 0a 3b 01 01
- (breakpoint)
- >
-
-> breakpoint
-breakpoints: 0 1
- Prints currently set breakpoints.
-
-> step [-<n>, +<n>]
- Performs single stepping through the BPF program from the current pc
- offset. Thus, on each step invocation, above register dump is issued.
- This can go forwards and backwards in time, a plain `step` will break
- on the next BPF instruction, thus +1. (No `run` needs to be issued here.)
-
-> select <n>
- Selects a given packet from the pcap file to continue from. Thus, on
- the next `run` or `step`, the BPF program is being evaluated against
- the user pre-selected packet. Numbering starts just as in Wireshark
- with index 1.
-
-> quit
-#
- Exits bpf_dbg.
-
-JIT compiler
-------------
-
-The Linux kernel has a built-in BPF JIT compiler for x86_64, SPARC, PowerPC,
-ARM, ARM64, MIPS and s390 which can be enabled through CONFIG_BPF_JIT. The JIT
-compiler is transparently invoked for each attached filter from user space
-or for internal kernel users if it has been previously enabled by root:
-
- echo 1 > /proc/sys/net/core/bpf_jit_enable
-
-For JIT developers, doing audits etc, each compile run can output the generated
-opcode image into the kernel log via:
-
- echo 2 > /proc/sys/net/core/bpf_jit_enable
-
-Example output from dmesg:
-
-[ 3389.935842] flen=6 proglen=70 pass=3 image=ffffffffa0069c8f
-[ 3389.935847] JIT code: 00000000: 55 48 89 e5 48 83 ec 60 48 89 5d f8 44 8b 4f 68
-[ 3389.935849] JIT code: 00000010: 44 2b 4f 6c 4c 8b 87 d8 00 00 00 be 0c 00 00 00
-[ 3389.935850] JIT code: 00000020: e8 1d 94 ff e0 3d 00 08 00 00 75 16 be 17 00 00
-[ 3389.935851] JIT code: 00000030: 00 e8 28 94 ff e0 83 f8 01 75 07 b8 ff ff 00 00
-[ 3389.935852] JIT code: 00000040: eb 02 31 c0 c9 c3
-
-When CONFIG_BPF_JIT_ALWAYS_ON is enabled, bpf_jit_enable is permanently set to 1 and
-setting any other value than that will return in failure. This is even the case for
-setting bpf_jit_enable to 2, since dumping the final JIT image into the kernel log
-is discouraged and introspection through bpftool (under tools/bpf/bpftool/) is the
-generally recommended approach instead.
-
-In the kernel source tree under tools/bpf/, there's bpf_jit_disasm for
-generating disassembly out of the kernel log's hexdump:
-
-# ./bpf_jit_disasm
-70 bytes emitted from JIT compiler (pass:3, flen:6)
-ffffffffa0069c8f + <x>:
- 0: push %rbp
- 1: mov %rsp,%rbp
- 4: sub $0x60,%rsp
- 8: mov %rbx,-0x8(%rbp)
- c: mov 0x68(%rdi),%r9d
- 10: sub 0x6c(%rdi),%r9d
- 14: mov 0xd8(%rdi),%r8
- 1b: mov $0xc,%esi
- 20: callq 0xffffffffe0ff9442
- 25: cmp $0x800,%eax
- 2a: jne 0x0000000000000042
- 2c: mov $0x17,%esi
- 31: callq 0xffffffffe0ff945e
- 36: cmp $0x1,%eax
- 39: jne 0x0000000000000042
- 3b: mov $0xffff,%eax
- 40: jmp 0x0000000000000044
- 42: xor %eax,%eax
- 44: leaveq
- 45: retq
-
-Issuing option `-o` will "annotate" opcodes to resulting assembler
-instructions, which can be very useful for JIT developers:
-
-# ./bpf_jit_disasm -o
-70 bytes emitted from JIT compiler (pass:3, flen:6)
-ffffffffa0069c8f + <x>:
- 0: push %rbp
- 55
- 1: mov %rsp,%rbp
- 48 89 e5
- 4: sub $0x60,%rsp
- 48 83 ec 60
- 8: mov %rbx,-0x8(%rbp)
- 48 89 5d f8
- c: mov 0x68(%rdi),%r9d
- 44 8b 4f 68
- 10: sub 0x6c(%rdi),%r9d
- 44 2b 4f 6c
- 14: mov 0xd8(%rdi),%r8
- 4c 8b 87 d8 00 00 00
- 1b: mov $0xc,%esi
- be 0c 00 00 00
- 20: callq 0xffffffffe0ff9442
- e8 1d 94 ff e0
- 25: cmp $0x800,%eax
- 3d 00 08 00 00
- 2a: jne 0x0000000000000042
- 75 16
- 2c: mov $0x17,%esi
- be 17 00 00 00
- 31: callq 0xffffffffe0ff945e
- e8 28 94 ff e0
- 36: cmp $0x1,%eax
- 83 f8 01
- 39: jne 0x0000000000000042
- 75 07
- 3b: mov $0xffff,%eax
- b8 ff ff 00 00
- 40: jmp 0x0000000000000044
- eb 02
- 42: xor %eax,%eax
- 31 c0
- 44: leaveq
- c9
- 45: retq
- c3
-
-For BPF JIT developers, bpf_jit_disasm, bpf_asm and bpf_dbg provides a useful
-toolchain for developing and testing the kernel's JIT compiler.
-
-BPF kernel internals
---------------------
-Internally, for the kernel interpreter, a different instruction set
-format with similar underlying principles from BPF described in previous
-paragraphs is being used. However, the instruction set format is modelled
-closer to the underlying architecture to mimic native instruction sets, so
-that better performance can be achieved (more details later). This new
-ISA is called 'eBPF' or 'internal BPF' interchangeably. (Note: eBPF which
-originates from [e]xtended BPF is not the same as BPF extensions! While
-eBPF is an ISA, BPF extensions date back to classic BPF's 'overloading'
-of BPF_LD | BPF_{B,H,W} | BPF_ABS instruction.)
-
-It is designed to be JITed with one to one mapping, which can also open up
-the possibility for GCC/LLVM compilers to generate optimized eBPF code through
-an eBPF backend that performs almost as fast as natively compiled code.
-
-The new instruction set was originally designed with the possible goal in
-mind to write programs in "restricted C" and compile into eBPF with a optional
-GCC/LLVM backend, so that it can just-in-time map to modern 64-bit CPUs with
-minimal performance overhead over two steps, that is, C -> eBPF -> native code.
-
-Currently, the new format is being used for running user BPF programs, which
-includes seccomp BPF, classic socket filters, cls_bpf traffic classifier,
-team driver's classifier for its load-balancing mode, netfilter's xt_bpf
-extension, PTP dissector/classifier, and much more. They are all internally
-converted by the kernel into the new instruction set representation and run
-in the eBPF interpreter. For in-kernel handlers, this all works transparently
-by using bpf_prog_create() for setting up the filter, resp.
-bpf_prog_destroy() for destroying it. The macro
-BPF_PROG_RUN(filter, ctx) transparently invokes eBPF interpreter or JITed
-code to run the filter. 'filter' is a pointer to struct bpf_prog that we
-got from bpf_prog_create(), and 'ctx' the given context (e.g.
-skb pointer). All constraints and restrictions from bpf_check_classic() apply
-before a conversion to the new layout is being done behind the scenes!
-
-Currently, the classic BPF format is being used for JITing on most 32-bit
-architectures, whereas x86-64, aarch64, s390x, powerpc64, sparc64, arm32 perform
-JIT compilation from eBPF instruction set.
-
-Some core changes of the new internal format:
-
-- Number of registers increase from 2 to 10:
-
- The old format had two registers A and X, and a hidden frame pointer. The
- new layout extends this to be 10 internal registers and a read-only frame
- pointer. Since 64-bit CPUs are passing arguments to functions via registers
- the number of args from eBPF program to in-kernel function is restricted
- to 5 and one register is used to accept return value from an in-kernel
- function. Natively, x86_64 passes first 6 arguments in registers, aarch64/
- sparcv9/mips64 have 7 - 8 registers for arguments; x86_64 has 6 callee saved
- registers, and aarch64/sparcv9/mips64 have 11 or more callee saved registers.
-
- Therefore, eBPF calling convention is defined as:
-
- * R0 - return value from in-kernel function, and exit value for eBPF program
- * R1 - R5 - arguments from eBPF program to in-kernel function
- * R6 - R9 - callee saved registers that in-kernel function will preserve
- * R10 - read-only frame pointer to access stack
-
- Thus, all eBPF registers map one to one to HW registers on x86_64, aarch64,
- etc, and eBPF calling convention maps directly to ABIs used by the kernel on
- 64-bit architectures.
-
- On 32-bit architectures JIT may map programs that use only 32-bit arithmetic
- and may let more complex programs to be interpreted.
-
- R0 - R5 are scratch registers and eBPF program needs spill/fill them if
- necessary across calls. Note that there is only one eBPF program (== one
- eBPF main routine) and it cannot call other eBPF functions, it can only
- call predefined in-kernel functions, though.
-
-- Register width increases from 32-bit to 64-bit:
-
- Still, the semantics of the original 32-bit ALU operations are preserved
- via 32-bit subregisters. All eBPF registers are 64-bit with 32-bit lower
- subregisters that zero-extend into 64-bit if they are being written to.
- That behavior maps directly to x86_64 and arm64 subregister definition, but
- makes other JITs more difficult.
-
- 32-bit architectures run 64-bit internal BPF programs via interpreter.
- Their JITs may convert BPF programs that only use 32-bit subregisters into
- native instruction set and let the rest be interpreted.
-
- Operation is 64-bit since on 64-bit architectures pointers are also
- 64-bit wide and we want to pass 64-bit values in/out of kernel functions.
- 32-bit eBPF registers would otherwise require us to define a register-pair
- ABI, thus we would not be able to use a direct eBPF register to HW register
- mapping and JIT would need to do combine/split/move operations for every
- register in and out of the function, which is complex, bug prone and slow.
- Another reason is the use of atomic 64-bit counters.
-
-- Conditional jt/jf targets replaced with jt/fall-through:
-
- While the original design has constructs such as "if (cond) jump_true;
- else jump_false;", they are being replaced into alternative constructs like
- "if (cond) jump_true; /* else fall-through */".
-
-- Introduces bpf_call insn and register passing convention for zero overhead
- calls from/to other kernel functions:
-
- Before an in-kernel function call, the internal BPF program needs to
- place function arguments into R1 to R5 registers to satisfy calling
- convention, then the interpreter will take them from registers and pass
- to in-kernel function. If R1 - R5 registers are mapped to CPU registers
- that are used for argument passing on given architecture, the JIT compiler
- doesn't need to emit extra moves. Function arguments will be in the correct
- registers and BPF_CALL instruction will be JITed as single 'call' HW
- instruction. This calling convention was picked to cover common call
- situations without performance penalty.
-
- After an in-kernel function call, R1 - R5 are reset to unreadable and R0 has
- the return value of the function. Since R6 - R9 are callee saved, their state
- is preserved across the call.
-
- For example, consider three C functions:
-
- u64 f1() { return (*_f2)(1); }
- u64 f2(u64 a) { return f3(a + 1, a); }
- u64 f3(u64 a, u64 b) { return a - b; }
-
- GCC can compile f1, f3 into x86_64:
-
- f1:
- movl $1, %edi
- movq _f2(%rip), %rax
- jmp *%rax
- f3:
- movq %rdi, %rax
- subq %rsi, %rax
- ret
-
- Function f2 in eBPF may look like:
-
- f2:
- bpf_mov R2, R1
- bpf_add R1, 1
- bpf_call f3
- bpf_exit
-
- If f2 is JITed and the pointer stored to '_f2'. The calls f1 -> f2 -> f3 and
- returns will be seamless. Without JIT, __bpf_prog_run() interpreter needs to
- be used to call into f2.
-
- For practical reasons all eBPF programs have only one argument 'ctx' which is
- already placed into R1 (e.g. on __bpf_prog_run() startup) and the programs
- can call kernel functions with up to 5 arguments. Calls with 6 or more arguments
- are currently not supported, but these restrictions can be lifted if necessary
- in the future.
-
- On 64-bit architectures all registers map to HW registers one to one. For
- example, x86_64 JIT compiler can map them as ...
-
- R0 - rax
- R1 - rdi
- R2 - rsi
- R3 - rdx
- R4 - rcx
- R5 - r8
- R6 - rbx
- R7 - r13
- R8 - r14
- R9 - r15
- R10 - rbp
-
- ... since x86_64 ABI mandates rdi, rsi, rdx, rcx, r8, r9 for argument passing
- and rbx, r12 - r15 are callee saved.
-
- Then the following internal BPF pseudo-program:
-
- bpf_mov R6, R1 /* save ctx */
- bpf_mov R2, 2
- bpf_mov R3, 3
- bpf_mov R4, 4
- bpf_mov R5, 5
- bpf_call foo
- bpf_mov R7, R0 /* save foo() return value */
- bpf_mov R1, R6 /* restore ctx for next call */
- bpf_mov R2, 6
- bpf_mov R3, 7
- bpf_mov R4, 8
- bpf_mov R5, 9
- bpf_call bar
- bpf_add R0, R7
- bpf_exit
-
- After JIT to x86_64 may look like:
-
- push %rbp
- mov %rsp,%rbp
- sub $0x228,%rsp
- mov %rbx,-0x228(%rbp)
- mov %r13,-0x220(%rbp)
- mov %rdi,%rbx
- mov $0x2,%esi
- mov $0x3,%edx
- mov $0x4,%ecx
- mov $0x5,%r8d
- callq foo
- mov %rax,%r13
- mov %rbx,%rdi
- mov $0x2,%esi
- mov $0x3,%edx
- mov $0x4,%ecx
- mov $0x5,%r8d
- callq bar
- add %r13,%rax
- mov -0x228(%rbp),%rbx
- mov -0x220(%rbp),%r13
- leaveq
- retq
-
- Which is in this example equivalent in C to:
-
- u64 bpf_filter(u64 ctx)
- {
- return foo(ctx, 2, 3, 4, 5) + bar(ctx, 6, 7, 8, 9);
- }
-
- In-kernel functions foo() and bar() with prototype: u64 (*)(u64 arg1, u64
- arg2, u64 arg3, u64 arg4, u64 arg5); will receive arguments in proper
- registers and place their return value into '%rax' which is R0 in eBPF.
- Prologue and epilogue are emitted by JIT and are implicit in the
- interpreter. R0-R5 are scratch registers, so eBPF program needs to preserve
- them across the calls as defined by calling convention.
-
- For example the following program is invalid:
-
- bpf_mov R1, 1
- bpf_call foo
- bpf_mov R0, R1
- bpf_exit
-
- After the call the registers R1-R5 contain junk values and cannot be read.
- An in-kernel eBPF verifier is used to validate internal BPF programs.
-
-Also in the new design, eBPF is limited to 4096 insns, which means that any
-program will terminate quickly and will only call a fixed number of kernel
-functions. Original BPF and the new format are two operand instructions,
-which helps to do one-to-one mapping between eBPF insn and x86 insn during JIT.
-
-The input context pointer for invoking the interpreter function is generic,
-its content is defined by a specific use case. For seccomp register R1 points
-to seccomp_data, for converted BPF filters R1 points to a skb.
-
-A program, that is translated internally consists of the following elements:
-
- op:16, jt:8, jf:8, k:32 ==> op:8, dst_reg:4, src_reg:4, off:16, imm:32
-
-So far 87 internal BPF instructions have been implemented. 8-bit 'op' opcode
-field has room for new instructions. Some of them may use 16/24/32 byte
-encoding. New instructions must be a multiple of 8 bytes to preserve backward
-compatibility.
-
-Internal BPF is a general purpose RISC instruction set. Not every register and
-every instruction are used during translation from original BPF to new format.
-For example, socket filters are not using 'exclusive add' instruction, but
-tracing filters may do to maintain counters of events, for example. Register R9
-is not used by socket filters either, but more complex filters may be running
-out of registers and would have to resort to spill/fill to stack.
-
-Internal BPF can used as generic assembler for last step performance
-optimizations, socket filters and seccomp are using it as assembler. Tracing
-filters may use it as assembler to generate code from kernel. In-kernel usage
-may not be bounded by security considerations, since generated internal BPF code
-may use an optimised internal code path and may not be being exposed to user
-space. Safety of internal BPF can come from a verifier (TBD). In such use cases
-as described, it may be used as safe as the instruction set.
-
-Just like the original BPF, the new format runs within a controlled environment,
-is deterministic and the kernel can easily prove that. The safety of the program
-can be determined in two steps: first step does depth-first-search to disallow
-loops and other CFG validation; second step starts from the first insn and
-descends all possible paths. It simulates execution of every insn and observes
-the state change of registers and stack.
-
-eBPF opcode encoding
---------------------
-
-eBPF is reusing most of the opcode encoding from classic to simplify conversion
-of classic BPF to eBPF. For arithmetic and jump instructions the 8-bit 'code'
-field is divided into three parts:
-
- +----------------+--------+--------------------+
- | 4 bits | 1 bit | 3 bits |
- | operation code | source | instruction class |
- +----------------+--------+--------------------+
- (MSB) (LSB)
-
-Three LSB bits store instruction class which is one of:
-
- Classic BPF classes: eBPF classes:
-
- BPF_LD 0x00 BPF_LD 0x00
- BPF_LDX 0x01 BPF_LDX 0x01
- BPF_ST 0x02 BPF_ST 0x02
- BPF_STX 0x03 BPF_STX 0x03
- BPF_ALU 0x04 BPF_ALU 0x04
- BPF_JMP 0x05 BPF_JMP 0x05
- BPF_RET 0x06 [ class 6 unused, for future if needed ]
- BPF_MISC 0x07 BPF_ALU64 0x07
-
-When BPF_CLASS(code) == BPF_ALU or BPF_JMP, 4th bit encodes source operand ...
-
- BPF_K 0x00
- BPF_X 0x08
-
- * in classic BPF, this means:
-
- BPF_SRC(code) == BPF_X - use register X as source operand
- BPF_SRC(code) == BPF_K - use 32-bit immediate as source operand
-
- * in eBPF, this means:
-
- BPF_SRC(code) == BPF_X - use 'src_reg' register as source operand
- BPF_SRC(code) == BPF_K - use 32-bit immediate as source operand
-
-... and four MSB bits store operation code.
-
-If BPF_CLASS(code) == BPF_ALU or BPF_ALU64 [ in eBPF ], BPF_OP(code) is one of:
-
- BPF_ADD 0x00
- BPF_SUB 0x10
- BPF_MUL 0x20
- BPF_DIV 0x30
- BPF_OR 0x40
- BPF_AND 0x50
- BPF_LSH 0x60
- BPF_RSH 0x70
- BPF_NEG 0x80
- BPF_MOD 0x90
- BPF_XOR 0xa0
- BPF_MOV 0xb0 /* eBPF only: mov reg to reg */
- BPF_ARSH 0xc0 /* eBPF only: sign extending shift right */
- BPF_END 0xd0 /* eBPF only: endianness conversion */
-
-If BPF_CLASS(code) == BPF_JMP, BPF_OP(code) is one of:
-
- BPF_JA 0x00
- BPF_JEQ 0x10
- BPF_JGT 0x20
- BPF_JGE 0x30
- BPF_JSET 0x40
- BPF_JNE 0x50 /* eBPF only: jump != */
- BPF_JSGT 0x60 /* eBPF only: signed '>' */
- BPF_JSGE 0x70 /* eBPF only: signed '>=' */
- BPF_CALL 0x80 /* eBPF only: function call */
- BPF_EXIT 0x90 /* eBPF only: function return */
- BPF_JLT 0xa0 /* eBPF only: unsigned '<' */
- BPF_JLE 0xb0 /* eBPF only: unsigned '<=' */
- BPF_JSLT 0xc0 /* eBPF only: signed '<' */
- BPF_JSLE 0xd0 /* eBPF only: signed '<=' */
-
-So BPF_ADD | BPF_X | BPF_ALU means 32-bit addition in both classic BPF
-and eBPF. There are only two registers in classic BPF, so it means A += X.
-In eBPF it means dst_reg = (u32) dst_reg + (u32) src_reg; similarly,
-BPF_XOR | BPF_K | BPF_ALU means A ^= imm32 in classic BPF and analogous
-src_reg = (u32) src_reg ^ (u32) imm32 in eBPF.
-
-Classic BPF is using BPF_MISC class to represent A = X and X = A moves.
-eBPF is using BPF_MOV | BPF_X | BPF_ALU code instead. Since there are no
-BPF_MISC operations in eBPF, the class 7 is used as BPF_ALU64 to mean
-exactly the same operations as BPF_ALU, but with 64-bit wide operands
-instead. So BPF_ADD | BPF_X | BPF_ALU64 means 64-bit addition i.e.
-dst_reg = dst_reg + src_reg
-
-Classic BPF wastes the whole BPF_RET class to represent a single 'ret'
-operation. Classic BPF_RET | BPF_K means copy imm32 into return register
-and perform function exit. eBPF is modeled to match CPU, so BPF_JMP | BPF_EXIT
-in eBPF means function exit only. The eBPF program needs to store return
-value into register R0 before doing a BPF_EXIT. Class 6 in eBPF is currently
-unused and reserved for future use.
-
-For load and store instructions the 8-bit 'code' field is divided as:
-
- +--------+--------+-------------------+
- | 3 bits | 2 bits | 3 bits |
- | mode | size | instruction class |
- +--------+--------+-------------------+
- (MSB) (LSB)
-
-Size modifier is one of ...
-
- BPF_W 0x00 /* word */
- BPF_H 0x08 /* half word */
- BPF_B 0x10 /* byte */
- BPF_DW 0x18 /* eBPF only, double word */
-
-... which encodes size of load/store operation:
-
- B - 1 byte
- H - 2 byte
- W - 4 byte
- DW - 8 byte (eBPF only)
-
-Mode modifier is one of:
-
- BPF_IMM 0x00 /* used for 32-bit mov in classic BPF and 64-bit in eBPF */
- BPF_ABS 0x20
- BPF_IND 0x40
- BPF_MEM 0x60
- BPF_LEN 0x80 /* classic BPF only, reserved in eBPF */
- BPF_MSH 0xa0 /* classic BPF only, reserved in eBPF */
- BPF_XADD 0xc0 /* eBPF only, exclusive add */
-
-eBPF has two non-generic instructions: (BPF_ABS | <size> | BPF_LD) and
-(BPF_IND | <size> | BPF_LD) which are used to access packet data.
-
-They had to be carried over from classic to have strong performance of
-socket filters running in eBPF interpreter. These instructions can only
-be used when interpreter context is a pointer to 'struct sk_buff' and
-have seven implicit operands. Register R6 is an implicit input that must
-contain pointer to sk_buff. Register R0 is an implicit output which contains
-the data fetched from the packet. Registers R1-R5 are scratch registers
-and must not be used to store the data across BPF_ABS | BPF_LD or
-BPF_IND | BPF_LD instructions.
-
-These instructions have implicit program exit condition as well. When
-eBPF program is trying to access the data beyond the packet boundary,
-the interpreter will abort the execution of the program. JIT compilers
-therefore must preserve this property. src_reg and imm32 fields are
-explicit inputs to these instructions.
-
-For example:
-
- BPF_IND | BPF_W | BPF_LD means:
-
- R0 = ntohl(*(u32 *) (((struct sk_buff *) R6)->data + src_reg + imm32))
- and R1 - R5 were scratched.
-
-Unlike classic BPF instruction set, eBPF has generic load/store operations:
-
-BPF_MEM | <size> | BPF_STX: *(size *) (dst_reg + off) = src_reg
-BPF_MEM | <size> | BPF_ST: *(size *) (dst_reg + off) = imm32
-BPF_MEM | <size> | BPF_LDX: dst_reg = *(size *) (src_reg + off)
-BPF_XADD | BPF_W | BPF_STX: lock xadd *(u32 *)(dst_reg + off16) += src_reg
-BPF_XADD | BPF_DW | BPF_STX: lock xadd *(u64 *)(dst_reg + off16) += src_reg
-
-Where size is one of: BPF_B or BPF_H or BPF_W or BPF_DW. Note that 1 and
-2 byte atomic increments are not supported.
-
-eBPF has one 16-byte instruction: BPF_LD | BPF_DW | BPF_IMM which consists of
-two consecutive 'struct bpf_insn' 8-byte blocks and is interpreted as single
-instruction that loads 64-bit immediate value into a dst_reg.
-
-Classic BPF has similar instruction: BPF_LD | BPF_W | BPF_IMM which loads
-32-bit immediate value into a register.
-
-eBPF verifier
--------------
-The safety of the eBPF program is determined in two steps.
-
-First step does DAG check to disallow loops and other CFG validation.
-In particular it will detect programs that have unreachable instructions
-(though classic BPF checker allows them).
-
-Second step starts from the first insn and descends all possible paths.
-It simulates execution of every insn and observes the state change of
-registers and stack.
-
-At the start of the program the register R1 contains a pointer to context
-and has type PTR_TO_CTX.
-If verifier sees an insn that does R2=R1, then R2 has now type
-PTR_TO_CTX as well and can be used on the right hand side of expression.
-If R1=PTR_TO_CTX and insn is R2=R1+R1, then R2=SCALAR_VALUE,
-since addition of two valid pointers makes invalid pointer.
-(In 'secure' mode verifier will reject any type of pointer arithmetic to make
-sure that kernel addresses don't leak to unprivileged users)
-
-If register was never written to, it's not readable:
- bpf_mov R0 = R2
- bpf_exit
-will be rejected, since R2 is unreadable at the start of the program.
-
-After kernel function call, R1-R5 are reset to unreadable and
-R0 has a return type of the function.
-
-Since R6-R9 are callee saved, their state is preserved across the call.
- bpf_mov R6 = 1
- bpf_call foo
- bpf_mov R0 = R6
- bpf_exit
-is a correct program. If there was R1 instead of R6, it would have
-been rejected.
-
-load/store instructions are allowed only with registers of valid types, which
-are PTR_TO_CTX, PTR_TO_MAP, PTR_TO_STACK. They are bounds and alignment checked.
-For example:
- bpf_mov R1 = 1
- bpf_mov R2 = 2
- bpf_xadd *(u32 *)(R1 + 3) += R2
- bpf_exit
-will be rejected, since R1 doesn't have a valid pointer type at the time of
-execution of instruction bpf_xadd.
-
-At the start R1 type is PTR_TO_CTX (a pointer to generic 'struct bpf_context')
-A callback is used to customize verifier to restrict eBPF program access to only
-certain fields within ctx structure with specified size and alignment.
-
-For example, the following insn:
- bpf_ld R0 = *(u32 *)(R6 + 8)
-intends to load a word from address R6 + 8 and store it into R0
-If R6=PTR_TO_CTX, via is_valid_access() callback the verifier will know
-that offset 8 of size 4 bytes can be accessed for reading, otherwise
-the verifier will reject the program.
-If R6=PTR_TO_STACK, then access should be aligned and be within
-stack bounds, which are [-MAX_BPF_STACK, 0). In this example offset is 8,
-so it will fail verification, since it's out of bounds.
-
-The verifier will allow eBPF program to read data from stack only after
-it wrote into it.
-Classic BPF verifier does similar check with M[0-15] memory slots.
-For example:
- bpf_ld R0 = *(u32 *)(R10 - 4)
- bpf_exit
-
-is an invalid program.
-
-Though R10 is correct read-only register and has type PTR_TO_STACK
-and R10 - 4 is within stack bounds, there were no stores into that location.
-
-Pointer register spill/fill is tracked as well, since four (R6-R9)
-callee saved registers may not be enough for some programs.
-
-Allowed function calls are customized with bpf_verifier_ops->get_func_proto()
-The eBPF verifier will check that registers match argument constraints.
-After the call register R0 will be set to return type of the function.
-
-Function calls is an important mechanism to extend functionality of eBPF
-programs. Socket filters may let programs call one set of functions,
-whereas tracing filters may allow a completely different set.
-
-If a function is made accessible to eBPF program, it needs to be thought
-through from a safety point of view. The verifier will guarantee that the
-function is called with valid arguments.
-
-seccomp vs socket filters have different security restrictions for classic BPF.
-Seccomp solves this by two stage verifier: classic BPF verifier is followed
-by seccomp verifier. In case of eBPF one configurable verifier is shared for
-all use cases.
-
-See details of eBPF verifier in kernel/bpf/verifier.c
-
-Register value tracking
------------------------
-In order to determine the safety of an eBPF program, the verifier must track
-the range of possible values in each register and also in each stack slot.
-This is done with 'struct bpf_reg_state', defined in include/linux/
-bpf_verifier.h, which unifies tracking of scalar and pointer values. Each
-register state has a type, which is either NOT_INIT (the register has not been
-written to), SCALAR_VALUE (some value which is not usable as a pointer), or a
-pointer type. The types of pointers describe their base, as follows:
- PTR_TO_CTX Pointer to bpf_context.
- CONST_PTR_TO_MAP Pointer to struct bpf_map. "Const" because arithmetic
- on these pointers is forbidden.
- PTR_TO_MAP_VALUE Pointer to the value stored in a map element.
- PTR_TO_MAP_VALUE_OR_NULL
- Either a pointer to a map value, or NULL; map accesses
- (see section 'eBPF maps', below) return this type,
- which becomes a PTR_TO_MAP_VALUE when checked != NULL.
- Arithmetic on these pointers is forbidden.
- PTR_TO_STACK Frame pointer.
- PTR_TO_PACKET skb->data.
- PTR_TO_PACKET_END skb->data + headlen; arithmetic forbidden.
-However, a pointer may be offset from this base (as a result of pointer
-arithmetic), and this is tracked in two parts: the 'fixed offset' and 'variable
-offset'. The former is used when an exactly-known value (e.g. an immediate
-operand) is added to a pointer, while the latter is used for values which are
-not exactly known. The variable offset is also used in SCALAR_VALUEs, to track
-the range of possible values in the register.
-The verifier's knowledge about the variable offset consists of:
-* minimum and maximum values as unsigned
-* minimum and maximum values as signed
-* knowledge of the values of individual bits, in the form of a 'tnum': a u64
-'mask' and a u64 'value'. 1s in the mask represent bits whose value is unknown;
-1s in the value represent bits known to be 1. Bits known to be 0 have 0 in both
-mask and value; no bit should ever be 1 in both. For example, if a byte is read
-into a register from memory, the register's top 56 bits are known zero, while
-the low 8 are unknown - which is represented as the tnum (0x0; 0xff). If we
-then OR this with 0x40, we get (0x40; 0xbf), then if we add 1 we get (0x0;
-0x1ff), because of potential carries.
-
-Besides arithmetic, the register state can also be updated by conditional
-branches. For instance, if a SCALAR_VALUE is compared > 8, in the 'true' branch
-it will have a umin_value (unsigned minimum value) of 9, whereas in the 'false'
-branch it will have a umax_value of 8. A signed compare (with BPF_JSGT or
-BPF_JSGE) would instead update the signed minimum/maximum values. Information
-from the signed and unsigned bounds can be combined; for instance if a value is
-first tested < 8 and then tested s> 4, the verifier will conclude that the value
-is also > 4 and s< 8, since the bounds prevent crossing the sign boundary.
-
-PTR_TO_PACKETs with a variable offset part have an 'id', which is common to all
-pointers sharing that same variable offset. This is important for packet range
-checks: after adding a variable to a packet pointer register A, if you then copy
-it to another register B and then add a constant 4 to A, both registers will
-share the same 'id' but the A will have a fixed offset of +4. Then if A is
-bounds-checked and found to be less than a PTR_TO_PACKET_END, the register B is
-now known to have a safe range of at least 4 bytes. See 'Direct packet access',
-below, for more on PTR_TO_PACKET ranges.
-
-The 'id' field is also used on PTR_TO_MAP_VALUE_OR_NULL, common to all copies of
-the pointer returned from a map lookup. This means that when one copy is
-checked and found to be non-NULL, all copies can become PTR_TO_MAP_VALUEs.
-As well as range-checking, the tracked information is also used for enforcing
-alignment of pointer accesses. For instance, on most systems the packet pointer
-is 2 bytes after a 4-byte alignment. If a program adds 14 bytes to that to jump
-over the Ethernet header, then reads IHL and adds (IHL * 4), the resulting
-pointer will have a variable offset known to be 4n+2 for some n, so adding the 2
-bytes (NET_IP_ALIGN) gives a 4-byte alignment and so word-sized accesses through
-that pointer are safe.
-
-Direct packet access
---------------------
-In cls_bpf and act_bpf programs the verifier allows direct access to the packet
-data via skb->data and skb->data_end pointers.
-Ex:
-1: r4 = *(u32 *)(r1 +80) /* load skb->data_end */
-2: r3 = *(u32 *)(r1 +76) /* load skb->data */
-3: r5 = r3
-4: r5 += 14
-5: if r5 > r4 goto pc+16
-R1=ctx R3=pkt(id=0,off=0,r=14) R4=pkt_end R5=pkt(id=0,off=14,r=14) R10=fp
-6: r0 = *(u16 *)(r3 +12) /* access 12 and 13 bytes of the packet */
-
-this 2byte load from the packet is safe to do, since the program author
-did check 'if (skb->data + 14 > skb->data_end) goto err' at insn #5 which
-means that in the fall-through case the register R3 (which points to skb->data)
-has at least 14 directly accessible bytes. The verifier marks it
-as R3=pkt(id=0,off=0,r=14).
-id=0 means that no additional variables were added to the register.
-off=0 means that no additional constants were added.
-r=14 is the range of safe access which means that bytes [R3, R3 + 14) are ok.
-Note that R5 is marked as R5=pkt(id=0,off=14,r=14). It also points
-to the packet data, but constant 14 was added to the register, so
-it now points to 'skb->data + 14' and accessible range is [R5, R5 + 14 - 14)
-which is zero bytes.
-
-More complex packet access may look like:
- R0=inv1 R1=ctx R3=pkt(id=0,off=0,r=14) R4=pkt_end R5=pkt(id=0,off=14,r=14) R10=fp
- 6: r0 = *(u8 *)(r3 +7) /* load 7th byte from the packet */
- 7: r4 = *(u8 *)(r3 +12)
- 8: r4 *= 14
- 9: r3 = *(u32 *)(r1 +76) /* load skb->data */
-10: r3 += r4
-11: r2 = r1
-12: r2 <<= 48
-13: r2 >>= 48
-14: r3 += r2
-15: r2 = r3
-16: r2 += 8
-17: r1 = *(u32 *)(r1 +80) /* load skb->data_end */
-18: if r2 > r1 goto pc+2
- R0=inv(id=0,umax_value=255,var_off=(0x0; 0xff)) R1=pkt_end R2=pkt(id=2,off=8,r=8) R3=pkt(id=2,off=0,r=8) R4=inv(id=0,umax_value=3570,var_off=(0x0; 0xfffe)) R5=pkt(id=0,off=14,r=14) R10=fp
-19: r1 = *(u8 *)(r3 +4)
-The state of the register R3 is R3=pkt(id=2,off=0,r=8)
-id=2 means that two 'r3 += rX' instructions were seen, so r3 points to some
-offset within a packet and since the program author did
-'if (r3 + 8 > r1) goto err' at insn #18, the safe range is [R3, R3 + 8).
-The verifier only allows 'add'/'sub' operations on packet registers. Any other
-operation will set the register state to 'SCALAR_VALUE' and it won't be
-available for direct packet access.
-Operation 'r3 += rX' may overflow and become less than original skb->data,
-therefore the verifier has to prevent that. So when it sees 'r3 += rX'
-instruction and rX is more than 16-bit value, any subsequent bounds-check of r3
-against skb->data_end will not give us 'range' information, so attempts to read
-through the pointer will give "invalid access to packet" error.
-Ex. after insn 'r4 = *(u8 *)(r3 +12)' (insn #7 above) the state of r4 is
-R4=inv(id=0,umax_value=255,var_off=(0x0; 0xff)) which means that upper 56 bits
-of the register are guaranteed to be zero, and nothing is known about the lower
-8 bits. After insn 'r4 *= 14' the state becomes
-R4=inv(id=0,umax_value=3570,var_off=(0x0; 0xfffe)), since multiplying an 8-bit
-value by constant 14 will keep upper 52 bits as zero, also the least significant
-bit will be zero as 14 is even. Similarly 'r2 >>= 48' will make
-R2=inv(id=0,umax_value=65535,var_off=(0x0; 0xffff)), since the shift is not sign
-extending. This logic is implemented in adjust_reg_min_max_vals() function,
-which calls adjust_ptr_min_max_vals() for adding pointer to scalar (or vice
-versa) and adjust_scalar_min_max_vals() for operations on two scalars.
-
-The end result is that bpf program author can access packet directly
-using normal C code as:
- void *data = (void *)(long)skb->data;
- void *data_end = (void *)(long)skb->data_end;
- struct eth_hdr *eth = data;
- struct iphdr *iph = data + sizeof(*eth);
- struct udphdr *udp = data + sizeof(*eth) + sizeof(*iph);
-
- if (data + sizeof(*eth) + sizeof(*iph) + sizeof(*udp) > data_end)
- return 0;
- if (eth->h_proto != htons(ETH_P_IP))
- return 0;
- if (iph->protocol != IPPROTO_UDP || iph->ihl != 5)
- return 0;
- if (udp->dest == 53 || udp->source == 9)
- ...;
-which makes such programs easier to write comparing to LD_ABS insn
-and significantly faster.
-
-eBPF maps
----------
-'maps' is a generic storage of different types for sharing data between kernel
-and userspace.
-
-The maps are accessed from user space via BPF syscall, which has commands:
-- create a map with given type and attributes
- map_fd = bpf(BPF_MAP_CREATE, union bpf_attr *attr, u32 size)
- using attr->map_type, attr->key_size, attr->value_size, attr->max_entries
- returns process-local file descriptor or negative error
-
-- lookup key in a given map
- err = bpf(BPF_MAP_LOOKUP_ELEM, union bpf_attr *attr, u32 size)
- using attr->map_fd, attr->key, attr->value
- returns zero and stores found elem into value or negative error
-
-- create or update key/value pair in a given map
- err = bpf(BPF_MAP_UPDATE_ELEM, union bpf_attr *attr, u32 size)
- using attr->map_fd, attr->key, attr->value
- returns zero or negative error
-
-- find and delete element by key in a given map
- err = bpf(BPF_MAP_DELETE_ELEM, union bpf_attr *attr, u32 size)
- using attr->map_fd, attr->key
-
-- to delete map: close(fd)
- Exiting process will delete maps automatically
-
-userspace programs use this syscall to create/access maps that eBPF programs
-are concurrently updating.
-
-maps can have different types: hash, array, bloom filter, radix-tree, etc.
-
-The map is defined by:
- . type
- . max number of elements
- . key size in bytes
- . value size in bytes
-
-Pruning
--------
-The verifier does not actually walk all possible paths through the program. For
-each new branch to analyse, the verifier looks at all the states it's previously
-been in when at this instruction. If any of them contain the current state as a
-subset, the branch is 'pruned' - that is, the fact that the previous state was
-accepted implies the current state would be as well. For instance, if in the
-previous state, r1 held a packet-pointer, and in the current state, r1 holds a
-packet-pointer with a range as long or longer and at least as strict an
-alignment, then r1 is safe. Similarly, if r2 was NOT_INIT before then it can't
-have been used by any path from that point, so any value in r2 (including
-another NOT_INIT) is safe. The implementation is in the function regsafe().
-Pruning considers not only the registers but also the stack (and any spilled
-registers it may hold). They must all be safe for the branch to be pruned.
-This is implemented in states_equal().
-
-Understanding eBPF verifier messages
-------------------------------------
-
-The following are few examples of invalid eBPF programs and verifier error
-messages as seen in the log:
-
-Program with unreachable instructions:
-static struct bpf_insn prog[] = {
- BPF_EXIT_INSN(),
- BPF_EXIT_INSN(),
-};
-Error:
- unreachable insn 1
-
-Program that reads uninitialized register:
- BPF_MOV64_REG(BPF_REG_0, BPF_REG_2),
- BPF_EXIT_INSN(),
-Error:
- 0: (bf) r0 = r2
- R2 !read_ok
-
-Program that doesn't initialize R0 before exiting:
- BPF_MOV64_REG(BPF_REG_2, BPF_REG_1),
- BPF_EXIT_INSN(),
-Error:
- 0: (bf) r2 = r1
- 1: (95) exit
- R0 !read_ok
-
-Program that accesses stack out of bounds:
- BPF_ST_MEM(BPF_DW, BPF_REG_10, 8, 0),
- BPF_EXIT_INSN(),
-Error:
- 0: (7a) *(u64 *)(r10 +8) = 0
- invalid stack off=8 size=8
-
-Program that doesn't initialize stack before passing its address into function:
- BPF_MOV64_REG(BPF_REG_2, BPF_REG_10),
- BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -8),
- BPF_LD_MAP_FD(BPF_REG_1, 0),
- BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0, BPF_FUNC_map_lookup_elem),
- BPF_EXIT_INSN(),
-Error:
- 0: (bf) r2 = r10
- 1: (07) r2 += -8
- 2: (b7) r1 = 0x0
- 3: (85) call 1
- invalid indirect read from stack off -8+0 size 8
-
-Program that uses invalid map_fd=0 while calling to map_lookup_elem() function:
- BPF_ST_MEM(BPF_DW, BPF_REG_10, -8, 0),
- BPF_MOV64_REG(BPF_REG_2, BPF_REG_10),
- BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -8),
- BPF_LD_MAP_FD(BPF_REG_1, 0),
- BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0, BPF_FUNC_map_lookup_elem),
- BPF_EXIT_INSN(),
-Error:
- 0: (7a) *(u64 *)(r10 -8) = 0
- 1: (bf) r2 = r10
- 2: (07) r2 += -8
- 3: (b7) r1 = 0x0
- 4: (85) call 1
- fd 0 is not pointing to valid bpf_map
-
-Program that doesn't check return value of map_lookup_elem() before accessing
-map element:
- BPF_ST_MEM(BPF_DW, BPF_REG_10, -8, 0),
- BPF_MOV64_REG(BPF_REG_2, BPF_REG_10),
- BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -8),
- BPF_LD_MAP_FD(BPF_REG_1, 0),
- BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0, BPF_FUNC_map_lookup_elem),
- BPF_ST_MEM(BPF_DW, BPF_REG_0, 0, 0),
- BPF_EXIT_INSN(),
-Error:
- 0: (7a) *(u64 *)(r10 -8) = 0
- 1: (bf) r2 = r10
- 2: (07) r2 += -8
- 3: (b7) r1 = 0x0
- 4: (85) call 1
- 5: (7a) *(u64 *)(r0 +0) = 0
- R0 invalid mem access 'map_value_or_null'
-
-Program that correctly checks map_lookup_elem() returned value for NULL, but
-accesses the memory with incorrect alignment:
- BPF_ST_MEM(BPF_DW, BPF_REG_10, -8, 0),
- BPF_MOV64_REG(BPF_REG_2, BPF_REG_10),
- BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -8),
- BPF_LD_MAP_FD(BPF_REG_1, 0),
- BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0, BPF_FUNC_map_lookup_elem),
- BPF_JMP_IMM(BPF_JEQ, BPF_REG_0, 0, 1),
- BPF_ST_MEM(BPF_DW, BPF_REG_0, 4, 0),
- BPF_EXIT_INSN(),
-Error:
- 0: (7a) *(u64 *)(r10 -8) = 0
- 1: (bf) r2 = r10
- 2: (07) r2 += -8
- 3: (b7) r1 = 1
- 4: (85) call 1
- 5: (15) if r0 == 0x0 goto pc+1
- R0=map_ptr R10=fp
- 6: (7a) *(u64 *)(r0 +4) = 0
- misaligned access off 4 size 8
-
-Program that correctly checks map_lookup_elem() returned value for NULL and
-accesses memory with correct alignment in one side of 'if' branch, but fails
-to do so in the other side of 'if' branch:
- BPF_ST_MEM(BPF_DW, BPF_REG_10, -8, 0),
- BPF_MOV64_REG(BPF_REG_2, BPF_REG_10),
- BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -8),
- BPF_LD_MAP_FD(BPF_REG_1, 0),
- BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0, BPF_FUNC_map_lookup_elem),
- BPF_JMP_IMM(BPF_JEQ, BPF_REG_0, 0, 2),
- BPF_ST_MEM(BPF_DW, BPF_REG_0, 0, 0),
- BPF_EXIT_INSN(),
- BPF_ST_MEM(BPF_DW, BPF_REG_0, 0, 1),
- BPF_EXIT_INSN(),
-Error:
- 0: (7a) *(u64 *)(r10 -8) = 0
- 1: (bf) r2 = r10
- 2: (07) r2 += -8
- 3: (b7) r1 = 1
- 4: (85) call 1
- 5: (15) if r0 == 0x0 goto pc+2
- R0=map_ptr R10=fp
- 6: (7a) *(u64 *)(r0 +0) = 0
- 7: (95) exit
-
- from 5 to 8: R0=imm0 R10=fp
- 8: (7a) *(u64 *)(r0 +0) = 1
- R0 invalid mem access 'imm'
-
-Testing
--------
-
-Next to the BPF toolchain, the kernel also ships a test module that contains
-various test cases for classic and internal BPF that can be executed against
-the BPF interpreter and JIT compiler. It can be found in lib/test_bpf.c and
-enabled via Kconfig:
-
- CONFIG_TEST_BPF=m
-
-After the module has been built and installed, the test suite can be executed
-via insmod or modprobe against 'test_bpf' module. Results of the test cases
-including timings in nsec can be found in the kernel log (dmesg).
-
-Misc
-----
-
-Also trinity, the Linux syscall fuzzer, has built-in support for BPF and
-SECCOMP-BPF kernel fuzzing.
-
-Written by
-----------
-
-The document was written in the hope that it is found useful and in order
-to give potential BPF hackers or security auditors a better overview of
-the underlying architecture.
-
-Jay Schulist <jschlst@samba.org>
-Daniel Borkmann <daniel@iogearbox.net>
-Alexei Starovoitov <ast@kernel.org>
--
2.17.1
^ permalink raw reply related
* [PATCH bpf-next 3/4] docs: Judiciously use double ticks
From: Tobin C. Harding @ 2018-08-09 5:23 UTC (permalink / raw)
To: Daniel Borkmann, Alexei Starovoitov
Cc: Tobin C. Harding, Jonathan Corbet, David S. Miller, Kees Cook,
Andy Lutomirski, Will Drewry, linux-doc, netdev, linux-kernel
In-Reply-To: <20180809052328.27942-1-me@tobin.cc>
Double ticks _can_ make the documentation easier to follow in HTML.
They can however make the documentation _harder_ to read in plain text.
We should use double ticks but judiciously.
Judiciously use double ticks to replace single ticks.
Signed-off-by: Tobin C. Harding <me@tobin.cc>
---
Documentation/userspace-api/eBPF.rst | 69 ++++++++++++++--------------
1 file changed, 34 insertions(+), 35 deletions(-)
diff --git a/Documentation/userspace-api/eBPF.rst b/Documentation/userspace-api/eBPF.rst
index 39fbe18a38fa..e4813d69de49 100644
--- a/Documentation/userspace-api/eBPF.rst
+++ b/Documentation/userspace-api/eBPF.rst
@@ -155,7 +155,7 @@ to in-kernel function. If R1 - R5 registers are mapped to CPU registers
that are used for argument passing on given architecture, the JIT
compiler doesn't need to emit extra moves. Function arguments will be
in the correct registers and BPF_CALL instruction will be JITed as
-single 'call' HW instruction. This calling convention was picked to
+single ``call`` HW instruction. This calling convention was picked to
cover common call situations without performance penalty.
After an in-kernel function call, R1 - R5 are reset to unreadable and R0
@@ -268,7 +268,7 @@ Which is in this example equivalent in C to::
In-kernel functions foo() and bar() with prototype: u64 (*)(u64 arg1,
u64 arg2, u64 arg3, u64 arg4, u64 arg5); will receive arguments in
-proper registers and place their return value into '%rax' which is R0 in
+proper registers and place their return value into ``%rax`` which is R0 in
eBPF. Prologue and epilogue are emitted by JIT and are implicit in the
interpreter. R0-R5 are scratch registers, so eBPF program needs to
preserve them across the calls as defined by calling convention.
@@ -425,7 +425,7 @@ exactly the same operations as BPF_ALU, but with 64-bit wide operands
instead. So BPF_ADD | BPF_X | BPF_ALU64 means 64-bit addition i.e.
dst_reg = dst_reg + src_reg
-Classic BPF wastes the whole BPF_RET class to represent a single 'ret'
+Classic BPF wastes the whole BPF_RET class to represent a single ``ret``
operation. Classic BPF_RET | BPF_K means copy imm32 into return
register and perform function exit. eBPF is modeled to match CPU, so
BPF_JMP | BPF_EXIT in eBPF means function exit only. The eBPF program
@@ -469,7 +469,7 @@ eBPF has two non-generic instructions: (BPF_ABS | <size> | BPF_LD) and
They had to be carried over from classic to have strong performance of
socket filters running in eBPF interpreter. These instructions can only
-be used when interpreter context is a pointer to 'struct sk_buff' and
+be used when interpreter context is a pointer to ``struct sk_buff`` and
have seven implicit operands. Register R6 is an implicit input that
must contain pointer to sk_buff. Register R0 is an implicit output
which contains the data fetched from the packet. Registers R1-R5 are
@@ -502,7 +502,7 @@ Where size is one of: BPF_B or BPF_H or BPF_W or BPF_DW. Note that 1
and 2 byte atomic increments are not supported.
eBPF has one 16-byte instruction: BPF_LD | BPF_DW | BPF_IMM which
-consists of two consecutive 'struct bpf_insn' 8-byte blocks and is
+consists of two consecutive ``struct bpf_insn`` 8-byte blocks and is
interpreted as single instruction that loads 64-bit immediate value into
a dst_reg.
@@ -565,8 +565,8 @@ alignment checked. For example::
will be rejected, since R1 doesn't have a valid pointer type at the time
of execution of instruction bpf_xadd.
-At the start R1 type is PTR_TO_CTX (a pointer to generic 'struct
-bpf_context'). A callback is used to customize verifier to restrict
+At the start R1 type is PTR_TO_CTX (a pointer to generic ``struct
+bpf_context``). A callback is used to customize verifier to restrict
eBPF program access to only certain fields within ctx structure with
specified size and alignment.
@@ -628,7 +628,7 @@ Register value tracking
In order to determine the safety of an eBPF program, the verifier must
track the range of possible values in each register and also in each
-stack slot. This is done with 'struct bpf_reg_state', defined in
+stack slot. This is done with ``struct bpf_reg_state``, defined in
include/linux/bpf_verifier.h, which unifies tracking of scalar and
pointer values. Each register state has a type, which is either
NOT_INIT (the register has not been written to), SCALAR_VALUE (some
@@ -715,7 +715,7 @@ data via skb->data and skb->data_end pointers, e.g.::
6: r0 = *(u16 *)(r3 +12) /* access 12 and 13 bytes of the packet */
this 2byte load from the packet is safe to do, since the program author
-did check 'if (skb->data + 14 > skb->data_end) goto err' at insn #5
+did check ``if (skb->data + 14 > skb->data_end) goto err`` at insn #5
which means that in the fall-through case the register R3 (which points
to skb->data) has at least 14 directly accessible bytes. The verifier
marks it as R3=pkt(id=0,off=0,r=14). id=0 means that no additional
@@ -723,8 +723,8 @@ variables were added to the register. off=0 means that no additional
constants were added. r=14 is the range of safe access which means that
bytes [R3, R3 + 14) are ok. Note that R5 is marked as
R5=pkt(id=0,off=14,r=14). It also points to the packet data, but
-constant 14 was added to the register, so it now points to 'skb->data +
-14' and accessible range is [R5, R5 + 14 - 14) which is zero bytes.
+constant 14 was added to the register, so it now points to ``skb->data +
+14`` and accessible range is [R5, R5 + 14 - 14) which is zero bytes.
More complex packet access may look like::
@@ -745,30 +745,29 @@ More complex packet access may look like::
R0=inv(id=0,umax_value=255,var_off=(0x0; 0xff)) R1=pkt_end R2=pkt(id=2,off=8,r=8) R3=pkt(id=2,off=0,r=8) R4=inv(id=0,umax_value=3570,var_off=(0x0; 0xfffe)) R5=pkt(id=0,off=14,r=14) R10=fp
19: r1 = *(u8 *)(r3 +4)
-The state of the register R3 is R3=pkt(id=2,off=0,r=8) id=2 means that
-two 'r3 += rX' instructions were seen, so r3 points to some offset
-within a packet and since the program author did 'if (r3 + 8 > r1) goto
-err' at insn #18, the safe range is [R3, R3 + 8). The verifier only
-allows 'add'/'sub' operations on packet registers. Any other operation
-will set the register state to 'SCALAR_VALUE' and it won't be available
-for direct packet access. Operation 'r3 += rX' may overflow and become
-less than original skb->data, therefore the verifier has to prevent
-that. So when it sees 'r3 += rX' instruction and rX is more than 16-bit
-value, any subsequent bounds-check of r3 against skb->data_end will not
-give us 'range' information, so attempts to read through the pointer
-will give "invalid access to packet" error. Ex. after insn 'r4 = *(u8
-*)(r3 +12)' (insn #7 above) the state of r4 is
-R4=inv(id=0,umax_value=255,var_off=(0x0; 0xff)) which means that upper
-56 bits of the register are guaranteed to be zero, and nothing is known
-about the lower 8 bits. After insn 'r4 *= 14' the state becomes
-R4=inv(id=0,umax_value=3570,var_off=(0x0; 0xfffe)), since multiplying an
-8-bit value by constant 14 will keep upper 52 bits as zero, also the
-least significant bit will be zero as 14 is even. Similarly 'r2 >>= 48'
-will make R2=inv(id=0,umax_value=65535,var_off=(0x0; 0xffff)), since the
-shift is not sign extending. This logic is implemented in
-adjust_reg_min_max_vals() function, which calls
-adjust_ptr_min_max_vals() for adding pointer to scalar (or vice versa)
-and adjust_scalar_min_max_vals() for operations on two scalars.
+The state of the register R3 is ``R3=pkt(id=2,off=0,r=8)`` id=2 means that
+two ``r3 += rX`` instructions were seen, so r3 points to some offset within
+a packet and since the program author did ``if (r3 + 8 > r1) goto err`` at
+insn #18, the safe range is [R3, R3 + 8). The verifier only allows
+'add'/'sub' operations on packet registers. Any other operation will set
+the register state to 'SCALAR_VALUE' and it won't be available for direct
+packet access. Operation ``r3 += rX`` may overflow and become less than
+original skb->data, therefore the verifier has to prevent that. So when it
+sees ``r3 += rX`` instruction and rX is more than 16-bit value, any
+subsequent bounds-check of r3 against skb->data_end will not give us
+'range' information, so attempts to read through the pointer will give
+"invalid access to packet" error. Ex. after insn ``r4 = *(u8 *)(r3 +12)``
+(insn #7 above) the state of r4 is R4=inv(id=0,umax_value=255,var_off=(0x0;
+0xff)) which means that upper 56 bits of the register are guaranteed to be
+zero, and nothing is known about the lower 8 bits. After insn ``r4 *= 14``
+the state becomes R4=inv(id=0,umax_value=3570,var_off=(0x0; 0xfffe)), since
+multiplying an 8-bit value by constant 14 will keep upper 52 bits as zero,
+also the least significant bit will be zero as 14 is even. Similarly ``r2
+>>= 48`` will make R2=inv(id=0,umax_value=65535,var_off=(0x0; 0xffff)),
+since the shift is not sign extending. This logic is implemented in
+adjust_reg_min_max_vals() function, which calls adjust_ptr_min_max_vals()
+for adding pointer to scalar (or vice versa) and
+adjust_scalar_min_max_vals() for operations on two scalars.
The end result is that bpf program author can access packet directly
using normal C code as::
--
2.17.1
^ permalink raw reply related
* [PATCH bpf-next 1/4] docs: net: Fix various minor typos
From: Tobin C. Harding @ 2018-08-09 5:23 UTC (permalink / raw)
To: Daniel Borkmann, Alexei Starovoitov
Cc: Tobin C. Harding, Jonathan Corbet, David S. Miller, Kees Cook,
Andy Lutomirski, Will Drewry, linux-doc, netdev, linux-kernel
In-Reply-To: <20180809052328.27942-1-me@tobin.cc>
Document contains a few minor typos and grammatical issues. We should
however try to keep the current flavour of the document.
Fix typos and grammar if fix is _really_ an improvement.
Signed-off-by: Tobin C. Harding <me@tobin.cc>
---
Documentation/networking/filter.txt | 66 +++++++++++++++--------------
1 file changed, 35 insertions(+), 31 deletions(-)
diff --git a/Documentation/networking/filter.txt b/Documentation/networking/filter.txt
index e6b4ebb2b243..1fe4adf9c4c6 100644
--- a/Documentation/networking/filter.txt
+++ b/Documentation/networking/filter.txt
@@ -29,8 +29,8 @@ removing the old one and placing your new one in its place, assuming your
filter has passed the checks, otherwise if it fails the old filter will
remain on that socket.
-SO_LOCK_FILTER option allows to lock the filter attached to a socket. Once
-set, a filter cannot be removed or changed. This allows one process to
+SO_LOCK_FILTER option allows locking of the filter attached to a socket.
+Once set, a filter cannot be removed or changed. This allows one process to
setup a socket, attach a filter, lock it then drop privileges and be
assured that the filter will be kept until the socket is closed.
@@ -463,7 +463,7 @@ JIT compiler
------------
The Linux kernel has a built-in BPF JIT compiler for x86_64, SPARC, PowerPC,
-ARM, ARM64, MIPS and s390 and can be enabled through CONFIG_BPF_JIT. The JIT
+ARM, ARM64, MIPS and s390 which can be enabled through CONFIG_BPF_JIT. The JIT
compiler is transparently invoked for each attached filter from user space
or for internal kernel users if it has been previously enabled by root:
@@ -572,7 +572,7 @@ Internally, for the kernel interpreter, a different instruction set
format with similar underlying principles from BPF described in previous
paragraphs is being used. However, the instruction set format is modelled
closer to the underlying architecture to mimic native instruction sets, so
-that a better performance can be achieved (more details later). This new
+that better performance can be achieved (more details later). This new
ISA is called 'eBPF' or 'internal BPF' interchangeably. (Note: eBPF which
originates from [e]xtended BPF is not the same as BPF extensions! While
eBPF is an ISA, BPF extensions date back to classic BPF's 'overloading'
@@ -647,12 +647,12 @@ Some core changes of the new internal format:
32-bit architectures run 64-bit internal BPF programs via interpreter.
Their JITs may convert BPF programs that only use 32-bit subregisters into
- native instruction set and let the rest being interpreted.
+ native instruction set and let the rest be interpreted.
- Operation is 64-bit, because on 64-bit architectures, pointers are also
- 64-bit wide, and we want to pass 64-bit values in/out of kernel functions,
- so 32-bit eBPF registers would otherwise require to define register-pair
- ABI, thus, there won't be able to use a direct eBPF register to HW register
+ Operation is 64-bit since on 64-bit architectures pointers are also
+ 64-bit wide and we want to pass 64-bit values in/out of kernel functions.
+ 32-bit eBPF registers would otherwise require us to define a register-pair
+ ABI, thus we would not be able to use a direct eBPF register to HW register
mapping and JIT would need to do combine/split/move operations for every
register in and out of the function, which is complex, bug prone and slow.
Another reason is the use of atomic 64-bit counters.
@@ -677,7 +677,7 @@ Some core changes of the new internal format:
situations without performance penalty.
After an in-kernel function call, R1 - R5 are reset to unreadable and R0 has
- a return value of the function. Since R6 - R9 are callee saved, their state
+ the return value of the function. Since R6 - R9 are callee saved, their state
is preserved across the call.
For example, consider three C functions:
@@ -715,7 +715,7 @@ Some core changes of the new internal format:
are currently not supported, but these restrictions can be lifted if necessary
in the future.
- On 64-bit architectures all register map to HW registers one to one. For
+ On 64-bit architectures all registers map to HW registers one to one. For
example, x86_64 JIT compiler can map them as ...
R0 - rax
@@ -814,9 +814,10 @@ A program, that is translated internally consists of the following elements:
op:16, jt:8, jf:8, k:32 ==> op:8, dst_reg:4, src_reg:4, off:16, imm:32
-So far 87 internal BPF instructions were implemented. 8-bit 'op' opcode field
-has room for new instructions. Some of them may use 16/24/32 byte encoding. New
-instructions must be multiple of 8 bytes to preserve backward compatibility.
+So far 87 internal BPF instructions have been implemented. 8-bit 'op' opcode
+field has room for new instructions. Some of them may use 16/24/32 byte
+encoding. New instructions must be a multiple of 8 bytes to preserve backward
+compatibility.
Internal BPF is a general purpose RISC instruction set. Not every register and
every instruction are used during translation from original BPF to new format.
@@ -827,11 +828,11 @@ out of registers and would have to resort to spill/fill to stack.
Internal BPF can used as generic assembler for last step performance
optimizations, socket filters and seccomp are using it as assembler. Tracing
-filters may use it as assembler to generate code from kernel. In kernel usage
+filters may use it as assembler to generate code from kernel. In-kernel usage
may not be bounded by security considerations, since generated internal BPF code
-may be optimizing internal code path and not being exposed to the user space.
-Safety of internal BPF can come from a verifier (TBD). In such use cases as
-described, it may be used as safe instruction set.
+may use an optimised internal code path and may not be being exposed to user
+space. Safety of internal BPF can come from a verifier (TBD). In such use cases
+as described, it may be used as safe as the instruction set.
Just like the original BPF, the new format runs within a controlled environment,
is deterministic and the kernel can easily prove that. The safety of the program
@@ -927,7 +928,7 @@ Classic BPF is using BPF_MISC class to represent A = X and X = A moves.
eBPF is using BPF_MOV | BPF_X | BPF_ALU code instead. Since there are no
BPF_MISC operations in eBPF, the class 7 is used as BPF_ALU64 to mean
exactly the same operations as BPF_ALU, but with 64-bit wide operands
-instead. So BPF_ADD | BPF_X | BPF_ALU64 means 64-bit addition, i.e.:
+instead. So BPF_ADD | BPF_X | BPF_ALU64 means 64-bit addition i.e.
dst_reg = dst_reg + src_reg
Classic BPF wastes the whole BPF_RET class to represent a single 'ret'
@@ -1005,9 +1006,10 @@ BPF_XADD | BPF_DW | BPF_STX: lock xadd *(u64 *)(dst_reg + off16) += src_reg
Where size is one of: BPF_B or BPF_H or BPF_W or BPF_DW. Note that 1 and
2 byte atomic increments are not supported.
-eBPF has one 16-byte instruction: BPF_LD | BPF_DW | BPF_IMM which consists
-of two consecutive 'struct bpf_insn' 8-byte blocks and interpreted as single
+eBPF has one 16-byte instruction: BPF_LD | BPF_DW | BPF_IMM which consists of
+two consecutive 'struct bpf_insn' 8-byte blocks and is interpreted as single
instruction that loads 64-bit immediate value into a dst_reg.
+
Classic BPF has similar instruction: BPF_LD | BPF_W | BPF_IMM which loads
32-bit immediate value into a register.
@@ -1016,8 +1018,8 @@ eBPF verifier
The safety of the eBPF program is determined in two steps.
First step does DAG check to disallow loops and other CFG validation.
-In particular it will detect programs that have unreachable instructions.
-(though classic BPF checker allows them)
+In particular it will detect programs that have unreachable instructions
+(though classic BPF checker allows them).
Second step starts from the first insn and descends all possible paths.
It simulates execution of every insn and observes the state change of
@@ -1078,7 +1080,9 @@ Classic BPF verifier does similar check with M[0-15] memory slots.
For example:
bpf_ld R0 = *(u32 *)(R10 - 4)
bpf_exit
-is invalid program.
+
+is an invalid program.
+
Though R10 is correct read-only register and has type PTR_TO_STACK
and R10 - 4 is within stack bounds, there were no stores into that location.
@@ -1089,13 +1093,13 @@ Allowed function calls are customized with bpf_verifier_ops->get_func_proto()
The eBPF verifier will check that registers match argument constraints.
After the call register R0 will be set to return type of the function.
-Function calls is a main mechanism to extend functionality of eBPF programs.
-Socket filters may let programs to call one set of functions, whereas tracing
-filters may allow completely different set.
+Function calls is an important mechanism to extend functionality of eBPF
+programs. Socket filters may let programs call one set of functions,
+whereas tracing filters may allow a completely different set.
-If a function made accessible to eBPF program, it needs to be thought through
-from safety point of view. The verifier will guarantee that the function is
-called with valid arguments.
+If a function is made accessible to eBPF program, it needs to be thought
+through from a safety point of view. The verifier will guarantee that the
+function is called with valid arguments.
seccomp vs socket filters have different security restrictions for classic BPF.
Seccomp solves this by two stage verifier: classic BPF verifier is followed
@@ -1167,7 +1171,7 @@ checked and found to be non-NULL, all copies can become PTR_TO_MAP_VALUEs.
As well as range-checking, the tracked information is also used for enforcing
alignment of pointer accesses. For instance, on most systems the packet pointer
is 2 bytes after a 4-byte alignment. If a program adds 14 bytes to that to jump
-over the Ethernet header, then reads IHL and addes (IHL * 4), the resulting
+over the Ethernet header, then reads IHL and adds (IHL * 4), the resulting
pointer will have a variable offset known to be 4n+2 for some n, so adding the 2
bytes (NET_IP_ALIGN) gives a 4-byte alignment and so word-sized accesses through
that pointer are safe.
--
2.17.1
^ permalink raw reply related
* [PATCH bpf-next 0/4] Convert filter.txt to RST
From: Tobin C. Harding @ 2018-08-09 5:23 UTC (permalink / raw)
To: Daniel Borkmann, Alexei Starovoitov
Cc: Tobin C. Harding, Jonathan Corbet, David S. Miller, Kees Cook,
Andy Lutomirski, Will Drewry, linux-doc, netdev, linux-kernel
Hi,
This is my latest attempt at converting Documentation/filter.txt to RST
format. Added to the CC list because of additional changes to
Documentation/userspace-api/seccomp_filter.rst
For reference this set is a progression of the follow sets (in order of
post date to LKML)
1. [PATCH bpf-next 00/13] docs: Convert BPF filter.txt to RST
2. [RFC bpf-next 0/3] docs: Convert filter.txt to RST
3. [RFC bpf-next v2 0/3] docs: Convert filter.txt to RST
Sending this to BPF maintainers for hopeful inclusion in the bpf-next
tree. As discussed on LKML this set does all the conversion in a single
patch. This includes adding SPDX licence comments
(inc. seccomp_filter), adding RST labels, and updating references.
Please note this set adds three files to the MAINTAINERS file for the
BPF maintainers. Is this rude?
Also this set adds a SPDX licence to all new files and also to
seccomp_filter.rst I did this because checkpatch asked me to but I am
unsure on the protocol, is it acceptable to add a licence to
documentation that someone else wrote?
For the Record;
Daniel and Alexei, can I please have permission to add GPLv2+ to the BPF
docs?
Kees, Andy, and Drewry, can I please have permission to add GPLv2+ to
seccomp_filter.rst
(added Cc: tag to patch 2 that adds the licence identifiers)
thanks,
Tobin.
Tobin C. Harding (4):
docs: net: Fix various minor typos
docs: Separate and convert filter.txt to RST
docs: Judiciously use double ticks
docs: Remove filter.txt from the tree
Documentation/networking/filter.txt | 1476 -----------------
Documentation/userspace-api/cBPF.rst | 432 +++++
Documentation/userspace-api/eBPF.rst | 1006 +++++++++++
Documentation/userspace-api/index.rst | 3 +
.../userspace-api/seccomp_filter.rst | 11 +
Documentation/userspace-api/socket_filter.rst | 183 ++
MAINTAINERS | 4 +-
7 files changed, 1638 insertions(+), 1477 deletions(-)
delete mode 100644 Documentation/networking/filter.txt
create mode 100644 Documentation/userspace-api/cBPF.rst
create mode 100644 Documentation/userspace-api/eBPF.rst
create mode 100644 Documentation/userspace-api/socket_filter.rst
--
2.17.1
^ permalink raw reply
* Re: [PATCH bpf-next 1/3] bpf: add bpf queue map
From: Mauricio Vasquez @ 2018-08-09 2:55 UTC (permalink / raw)
To: Daniel Borkmann, Alexei Starovoitov; +Cc: netdev
In-Reply-To: <7e8662a7-dc0d-a27e-921b-11d98c3ba1aa@iogearbox.net>
On 08/07/2018 08:52 AM, Daniel Borkmann wrote:
> On 08/06/2018 03:58 PM, Mauricio Vasquez B wrote:
>> Bpf queue implements a LIFO/FIFO data containers for ebpf programs.
>>
>> It allows to push an element to the queue by using the update operation
>> and to pop an element from the queue by using the lookup operation.
>>
>> A use case for this is to keep track of a pool of elements, like
>> network ports in a SNAT.
>>
>> Signed-off-by: Mauricio Vasquez B <mauricio.vasquez@polito.it>
> [...]
>> +static int prealloc_init(struct bpf_queue *queue)
>> +{
>> + u32 node_size = sizeof(struct queue_node) +
>> + round_up(queue->map.value_size, 8);
>> + u32 num_entries = queue->map.max_entries;
>> + int err;
>> +
>> + queue->nodes = bpf_map_area_alloc(node_size * num_entries,
>> + queue->map.numa_node);
> That doesn't work either. If you don't set numa node, then here in
> your case you'll always use numa node 0, which is unintentional.
> You need to get the node via bpf_map_attr_numa_node(attr) helper.
> Same issue in queue_map_update_elem().
>
This should work, map.numa_node is initialized using
bpf_map_attr_numa_node() in bpf_map_init_from_attr()
The htab does exactly the same.
^ permalink raw reply
* Re: [PATCH] fix build for MediaTek MT7610U USB wireless dongle
From: Kalle Valo @ 2018-08-09 5:16 UTC (permalink / raw)
To: valdis.kletnieks; +Cc: David S. Miller, netdev, linux-kernel, linux-wireless
In-Reply-To: <145751.1533759414@turing-police.cc.vt.edu>
valdis.kletnieks@vt.edu writes:
> The mt76x0 driver requires the mt76 core driver to actually function.
> So add a 'select' to avoid embarrassing 'symbol unknown' errors
> when attempting to modprobe it in a module tree that doesn't
> include mt76.ko
>
> Signed-off-by: Valdis Kletnieks <valdis.kletnieks@vt.edu>
I'll add "mt76:" prefix to the title and queue this for 4.19.
> Still need to figure out why it totally fails to see the 5gz band,
> it works find on 2.4 but ends up locked at 72mbits/sec. Works
> OK otherwise...
Great, thanks for the feedback.
--
Kalle Valo
^ permalink raw reply
* Re: [PATCH bpf-next 3/3] bpf: add sample for BPF_MAP_TYPE_QUEUE
From: Mauricio Vasquez @ 2018-08-09 2:52 UTC (permalink / raw)
To: Daniel Borkmann, Alexei Starovoitov; +Cc: netdev
In-Reply-To: <0e8bf8ce-9993-1aa7-b8b1-a041d4e66597@iogearbox.net>
On 08/07/2018 08:44 AM, Daniel Borkmann wrote:
> On 08/06/2018 03:58 PM, Mauricio Vasquez B wrote:
>> The example is made by two parts, a eBPF program that consumes elements
>> from a FIFO queue and prints them in the screen and a user space
>> application that inserts new elements into the queue each time this is
>> executed.
>>
>> Signed-off-by: Mauricio Vasquez B <mauricio.vasquez@polito.it>
>> ---
>> samples/bpf/.gitignore | 1 +
>> samples/bpf/Makefile | 3 ++
>> samples/bpf/test_map_in_map_user.c | 9 +-----
>> samples/bpf/test_queuemap.sh | 37 +++++++++++++++++++++++++
>> samples/bpf/test_queuemap_kern.c | 51 +++++++++++++++++++++++++++++++++++
>> samples/bpf/test_queuemap_user.c | 53 ++++++++++++++++++++++++++++++++++++
>> 6 files changed, 147 insertions(+), 7 deletions(-)
>> create mode 100755 samples/bpf/test_queuemap.sh
>> create mode 100644 samples/bpf/test_queuemap_kern.c
>> create mode 100644 samples/bpf/test_queuemap_user.c
>>
>> diff --git a/samples/bpf/.gitignore b/samples/bpf/.gitignore
>> index 8ae4940025f8..d7e518c1b3ed 100644
>> --- a/samples/bpf/.gitignore
>> +++ b/samples/bpf/.gitignore
>> @@ -26,6 +26,7 @@ test_lru_dist
>> test_map_in_map
>> test_overhead
>> test_probe_write_user
>> +test_queuemap
>> trace_event
>> trace_output
>> tracex1
>> diff --git a/samples/bpf/Makefile b/samples/bpf/Makefile
>> index f88d5683d6ee..624f4f4b81db 100644
>> --- a/samples/bpf/Makefile
>> +++ b/samples/bpf/Makefile
>> @@ -53,6 +53,7 @@ hostprogs-y += xdpsock
>> hostprogs-y += xdp_fwd
>> hostprogs-y += task_fd_query
>> hostprogs-y += xdp_sample_pkts
>> +hostprogs-y += test_queuemap
>>
>> # Libbpf dependencies
>> LIBBPF = $(TOOLS_PATH)/lib/bpf/libbpf.a
>> @@ -109,6 +110,7 @@ xdpsock-objs := xdpsock_user.o
>> xdp_fwd-objs := xdp_fwd_user.o
>> task_fd_query-objs := bpf_load.o task_fd_query_user.o $(TRACE_HELPERS)
>> xdp_sample_pkts-objs := xdp_sample_pkts_user.o $(TRACE_HELPERS)
>> +test_queuemap-objs := bpf_load.o test_queuemap_user.o
>>
>> # Tell kbuild to always build the programs
>> always := $(hostprogs-y)
>> @@ -166,6 +168,7 @@ always += xdpsock_kern.o
>> always += xdp_fwd_kern.o
>> always += task_fd_query_kern.o
>> always += xdp_sample_pkts_kern.o
>> +always += test_queuemap_kern.o
>>
>> HOSTCFLAGS += -I$(objtree)/usr/include
>> HOSTCFLAGS += -I$(srctree)/tools/lib/
>> diff --git a/samples/bpf/test_map_in_map_user.c b/samples/bpf/test_map_in_map_user.c
>> index e308858f7bcf..28edac94234e 100644
>> --- a/samples/bpf/test_map_in_map_user.c
>> +++ b/samples/bpf/test_map_in_map_user.c
>> @@ -1,10 +1,5 @@
>> -/*
>> - * Copyright (c) 2017 Facebook
>> - *
>> - * This program is free software; you can redistribute it and/or
>> - * modify it under the terms of version 2 of the GNU General Public
>> - * License as published by the Free Software Foundation.
>> - */
>> +// SPDX-License-Identifier: GPL-2.0
>> +/* Copyright (c) 2018 Politecnico di Torino */
> I don't think you can remove above Copyright and replace it with a different one. ;)
Copy-paste on the wrong file. :/
>
>> #include <sys/resource.h>
>> #include <sys/socket.h>
>> #include <arpa/inet.h>
>> diff --git a/samples/bpf/test_queuemap.sh b/samples/bpf/test_queuemap.sh
>> new file mode 100755
>> index 000000000000..ed08c1fa8c2c
>> --- /dev/null
>> +++ b/samples/bpf/test_queuemap.sh
>> @@ -0,0 +1,37 @@
>> +#!/bin/bash
>> +# SPDX-License-Identifier: GPL-2.0
>> +
>> +[[ -z $TC ]] && TC='tc'
>> +[[ -z $IP ]] && IP='ip'
>> +
>> +TEST_QUEUE_USER='./test_queuemap'
>> +TEST_QUEUE_BPF='./test_queuemap_kern.o'
>> +
>> +function config {
>> + $IP netns add ns1
>> + $IP link add ve1 type veth peer name vens1
>> + $IP link set dev ve1 up
>> + $IP link set dev ve1 mtu 1500
>> + $IP link set dev vens1 netns ns1
>> +
>> + $IP -n ns1 link set dev lo up
>> + $IP -n ns1 link set dev vens1 up
>> + $IP -n ns1 addr add 10.1.1.101/24 dev vens1
>> +
>> + $IP addr add 10.1.1.1/24 dev ve1
>> + $TC qdisc add dev ve1 clsact
>> + $TC filter add dev ve1 ingress bpf da obj $TEST_QUEUE_BPF sec test_queue
>> +}
>> +
>> +function cleanup {
>> + set +e
>> + [[ -z $DEBUG ]] || set +x
>> + $IP netns delete ns1 >& /dev/null
>> + $IP link del ve1 >& /dev/null
>> + rm -f /sys/fs/bpf/tc/globals/queue
>> + [[ -z $DEBUG ]] || set -x
>> + set -e
>> +}
>> +
>> +cleanup
>> +config
>> diff --git a/samples/bpf/test_queuemap_kern.c b/samples/bpf/test_queuemap_kern.c
>> new file mode 100644
>> index 000000000000..2b496dafaffd
>> --- /dev/null
>> +++ b/samples/bpf/test_queuemap_kern.c
>> @@ -0,0 +1,51 @@
>> +// SPDX-License-Identifier: GPL-2.0
>> +/* Copyright (c) 2018 Politecnico di Torino */
>> +#define KBUILD_MODNAME "foo"
>> +#include <linux/ptrace.h>
>> +#include <linux/version.h>
>> +#include <uapi/linux/bpf.h>
>> +#include <uapi/linux/in6.h>
>> +#include <uapi/linux/pkt_cls.h>
>> +#include "bpf_helpers.h"
>> +
>> +#define PIN_GLOBAL_NS 2
>> +
>> +struct bpf_elf_map {
>> + __u32 type;
>> + __u32 key_size;
>> + __u32 value_size;
>> + __u32 max_entries;
>> + __u32 flags;
>> + __u32 id;
>> + __u32 pinning;
>> +};
>> +
>> +/* map #0 */
>> +struct bpf_elf_map SEC("maps") queue = {
>> + .type = BPF_MAP_TYPE_QUEUE,
>> + .key_size = 0,
>> + .value_size = sizeof(u32),
>> + .flags = BPF_F_QUEUE_FIFO,
>> + .max_entries = 1024,
>> + .pinning = PIN_GLOBAL_NS,
>> +};
>> +
>> +SEC("test_queue")
>> +int _test_queue(struct __sk_buff *skb)
>> +{
>> + char msg[] = "element is %u\n";
>> + char msg_no[] = "there are not elements\n";
>> +
>> + u32 *val = bpf_map_lookup_elem(&queue, NULL);
>> +
>> + if (!val) {
>> + bpf_trace_printk(msg_no, sizeof(msg_no));
>> + return TC_ACT_OK;
>> + }
>> +
>> + bpf_trace_printk(msg, sizeof(msg), *val);
> Could we add a more elaborate example? In cover letter and patch 1 which
> implements the map, you mention SNAT, perhaps it would be useful to make
> it a minimal sample app.
I'll try to create a minimal sample of a SNAT.
>
>> + return TC_ACT_OK;
>> +}
>> +
>> +char _license[] SEC("license") = "GPL";
>> +u32 _version SEC("version") = LINUX_VERSION_CODE;
>> diff --git a/samples/bpf/test_queuemap_user.c b/samples/bpf/test_queuemap_user.c
>> new file mode 100644
>> index 000000000000..68f9a5d54596
>> --- /dev/null
>> +++ b/samples/bpf/test_queuemap_user.c
>> @@ -0,0 +1,53 @@
>> +/*
>> + * Copyright (c) 2018 Politecnico di Torino
>> + *
>> + * This program is free software; you can redistribute it and/or
>> + * modify it under the terms of version 2 of the GNU General Public
>> + * License as published by the Free Software Foundation.
>> + */
>> +#include <sys/resource.h>
>> +#include <sys/socket.h>
>> +#include <arpa/inet.h>
>> +#include <stdint.h>
>> +#include <assert.h>
>> +#include <errno.h>
>> +#include <stdlib.h>
>> +#include <stdio.h>
>> +#include <bpf/bpf.h>
>> +#include "bpf_load.h"
>> +
>> +int queue_map;
>> +
>> +static void test_queue_map(void)
>> +{
>> + int ret;
>> + uint32_t i;
>> +
>> + queue_map = bpf_obj_get("/sys/fs/bpf/tc/globals/queue");
>> + if (queue_map < 0) {
>> + fprintf(stderr, "error getting map");
>> + return;
>> + }
>> +
>> + for (i = 0; i < 256; i++) {
>> + uint32_t v = 1000 - i*3;
>> +
>> + ret = bpf_map_update_elem(queue_map, NULL, &v, 0);
>> + if (ret)
>> + fprintf(stderr, "ret is %d\n", ret);
>> + }
>> +}
>> +
>> +int main(int argc, char **argv)
>> +{
>> + struct rlimit r = {RLIM_INFINITY, RLIM_INFINITY};
>> + char filename[256];
>> +
>> + assert(!setrlimit(RLIMIT_MEMLOCK, &r));
>> +
>> + snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]);
>> +
>> + test_queue_map();
>> +
>> + return 0;
>> +}
>>
>
^ permalink raw reply
* Re: [PATCH bpf-next 1/3] bpf: add bpf queue map
From: Mauricio Vasquez @ 2018-08-09 2:50 UTC (permalink / raw)
To: Daniel Borkmann, Alexei Starovoitov; +Cc: netdev
In-Reply-To: <bb6fcefb-3dbd-d6cd-2e86-a8921d9be8bd@iogearbox.net>
On 08/07/2018 08:40 AM, Daniel Borkmann wrote:
> On 08/06/2018 03:58 PM, Mauricio Vasquez B wrote:
>> Bpf queue implements a LIFO/FIFO data containers for ebpf programs.
>>
>> It allows to push an element to the queue by using the update operation
>> and to pop an element from the queue by using the lookup operation.
>>
>> A use case for this is to keep track of a pool of elements, like
>> network ports in a SNAT.
>>
>> Signed-off-by: Mauricio Vasquez B <mauricio.vasquez@polito.it>
>> ---
>> include/linux/bpf_types.h | 1
>> include/uapi/linux/bpf.h | 5 +
>> kernel/bpf/Makefile | 2
>> kernel/bpf/queuemap.c | 287 +++++++++++++++++++++++++++++++++++++++++++++
>> kernel/bpf/syscall.c | 61 +++++++---
>> kernel/bpf/verifier.c | 16 ++-
>> 6 files changed, 353 insertions(+), 19 deletions(-)
>> create mode 100644 kernel/bpf/queuemap.c
>>
>> diff --git a/include/linux/bpf_types.h b/include/linux/bpf_types.h
>> index c5700c2d5549..6c7a62f3fe43 100644
>> --- a/include/linux/bpf_types.h
>> +++ b/include/linux/bpf_types.h
>> @@ -58,3 +58,4 @@ BPF_MAP_TYPE(BPF_MAP_TYPE_CPUMAP, cpu_map_ops)
>> BPF_MAP_TYPE(BPF_MAP_TYPE_XSKMAP, xsk_map_ops)
>> #endif
>> #endif
>> +BPF_MAP_TYPE(BPF_MAP_TYPE_QUEUE, queue_map_ops)
>> diff --git a/include/uapi/linux/bpf.h b/include/uapi/linux/bpf.h
>> index 0ebaaf7f3568..2c171c40eb45 100644
>> --- a/include/uapi/linux/bpf.h
>> +++ b/include/uapi/linux/bpf.h
>> @@ -120,6 +120,7 @@ enum bpf_map_type {
>> BPF_MAP_TYPE_CPUMAP,
>> BPF_MAP_TYPE_XSKMAP,
>> BPF_MAP_TYPE_SOCKHASH,
>> + BPF_MAP_TYPE_QUEUE,
>> };
>>
>> enum bpf_prog_type {
>> @@ -255,6 +256,10 @@ enum bpf_attach_type {
>> /* Flag for stack_map, store build_id+offset instead of pointer */
>> #define BPF_F_STACK_BUILD_ID (1U << 5)
>>
>> +/* Flags for queue_map, type of queue */
>> +#define BPF_F_QUEUE_FIFO (1U << 16)
>> +#define BPF_F_QUEUE_LIFO (2U << 16)
>> +
>> enum bpf_stack_build_id_status {
>> /* user space need an empty entry to identify end of a trace */
>> BPF_STACK_BUILD_ID_EMPTY = 0,
>> diff --git a/kernel/bpf/Makefile b/kernel/bpf/Makefile
>> index f27f5496d6fe..30f02ef66635 100644
>> --- a/kernel/bpf/Makefile
>> +++ b/kernel/bpf/Makefile
>> @@ -2,7 +2,7 @@
>> obj-y := core.o
>>
>> obj-$(CONFIG_BPF_SYSCALL) += syscall.o verifier.o inode.o helpers.o tnum.o
>> -obj-$(CONFIG_BPF_SYSCALL) += hashtab.o arraymap.o percpu_freelist.o bpf_lru_list.o lpm_trie.o map_in_map.o
>> +obj-$(CONFIG_BPF_SYSCALL) += hashtab.o arraymap.o percpu_freelist.o bpf_lru_list.o lpm_trie.o map_in_map.o queuemap.o
>> obj-$(CONFIG_BPF_SYSCALL) += disasm.o
>> obj-$(CONFIG_BPF_SYSCALL) += btf.o
>> ifeq ($(CONFIG_NET),y)
>> diff --git a/kernel/bpf/queuemap.c b/kernel/bpf/queuemap.c
>> new file mode 100644
>> index 000000000000..ab30af43b4cc
>> --- /dev/null
>> +++ b/kernel/bpf/queuemap.c
>> @@ -0,0 +1,287 @@
>> +// SPDX-License-Identifier: GPL-2.0
>> +/*
>> + * queuemap.c: BPF queue map
>> + *
>> + * Copyright (c) 2018 Politecnico di Torino
>> + */
>> +#include <linux/bpf.h>
>> +#include <linux/rculist.h>
>> +#include <linux/slab.h>
>> +#include "percpu_freelist.h"
>> +
>> +#define QUEUE_CREATE_FLAG_MASK \
>> + (BPF_F_NO_PREALLOC | BPF_F_NUMA_NODE | BPF_F_RDONLY | BPF_F_WRONLY | \
>> + BPF_F_QUEUE_FIFO | BPF_F_QUEUE_LIFO)
>> +
>> +enum queue_type {
>> + QUEUE_FIFO = (BPF_F_QUEUE_FIFO >> 16),
>> + QUEUE_LIFO = (BPF_F_QUEUE_LIFO >> 16),
>> +};
>> +
>> +struct bpf_queue {
>> + struct bpf_map map;
>> + struct list_head head;
>> + struct pcpu_freelist freelist;
>> + void *nodes;
>> + enum queue_type type;
>> + raw_spinlock_t lock;
>> + atomic_t count;
>> + u32 node_size;
>> +};
>> +
>> +struct queue_node {
>> + struct pcpu_freelist_node fnode;
>> + struct bpf_queue *queue;
>> + struct list_head list;
>> + struct rcu_head rcu;
>> + char element[0] __aligned(8);
>> +};
>> +
>> +static bool queue_map_is_prealloc(struct bpf_queue *queue)
>> +{
>> + return !(queue->map.map_flags & BPF_F_NO_PREALLOC);
>> +}
>> +
>> +/* Called from syscall */
>> +static int queue_map_alloc_check(union bpf_attr *attr)
>> +{
>> + /* check sanity of attributes */
>> + if (attr->max_entries == 0 || attr->key_size != 0 ||
>> + attr->value_size == 0 || attr->map_flags & ~QUEUE_CREATE_FLAG_MASK)
>> + return -EINVAL;
>> +
>> + if ((attr->map_flags >> 16) != QUEUE_FIFO &&
>> + (attr->map_flags >> 16) != QUEUE_LIFO) {
>> + 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.
>> + */
>> + return -E2BIG;
>> +
>> + return 0;
>> +}
>> +
>> +static int prealloc_init(struct bpf_queue *queue)
>> +{
>> + u32 node_size = sizeof(struct queue_node) +
>> + round_up(queue->map.value_size, 8);
>> + u32 num_entries = queue->map.max_entries;
>> + int err;
>> +
>> + queue->nodes = bpf_map_area_alloc(node_size * num_entries,
>> + queue->map.numa_node);
>> + if (!queue->nodes)
>> + return -ENOMEM;
>> +
>> + err = pcpu_freelist_init(&queue->freelist);
>> + if (err)
>> + goto free_nodes;
>> +
>> + pcpu_freelist_populate(&queue->freelist,
>> + queue->nodes +
>> + offsetof(struct queue_node, fnode),
>> + node_size, num_entries);
>> +
>> + return 0;
>> +
>> +free_nodes:
>> + bpf_map_area_free(queue->nodes);
>> + return err;
>> +}
>> +
>> +static void prealloc_destroy(struct bpf_queue *queue)
>> +{
>> + bpf_map_area_free(queue->nodes);
>> + pcpu_freelist_destroy(&queue->freelist);
>> +}
>> +
>> +static struct bpf_map *queue_map_alloc(union bpf_attr *attr)
>> +{
>> + struct bpf_queue *queue;
>> + u64 cost = sizeof(*queue);
>> + int ret;
>> +
>> + queue = kzalloc(sizeof(*queue), GFP_USER);
>> + if (!queue)
>> + return ERR_PTR(-ENOMEM);
>> +
>> + bpf_map_init_from_attr(&queue->map, attr);
>> +
>> + queue->node_size = sizeof(struct queue_node) +
>> + round_up(attr->value_size, 8);
>> + cost += (u64) attr->max_entries * queue->node_size;
>> + if (cost >= U32_MAX - PAGE_SIZE) {
>> + ret = -E2BIG;
>> + goto free_queue;
>> + }
>> +
>> + queue->map.pages = round_up(cost, PAGE_SIZE) >> PAGE_SHIFT;
>> +
>> + ret = bpf_map_precharge_memlock(queue->map.pages);
>> + if (ret)
>> + goto free_queue;
>> +
>> + INIT_LIST_HEAD(&queue->head);
>> +
>> + raw_spin_lock_init(&queue->lock);
>> +
>> + queue->type = attr->map_flags >> 16;
>> +
>> + if (queue_map_is_prealloc(queue))
>> + ret = prealloc_init(queue);
>> + if (ret)
>> + goto free_queue;
>> +
>> + return &queue->map;
>> +
>> +free_queue:
>> + kfree(queue);
>> + return ERR_PTR(ret);
>> +}
>> +
>> +/* Called when map->refcnt goes to zero, either from workqueue or from syscall */
>> +static void queue_map_free(struct bpf_map *map)
>> +{
>> + struct bpf_queue *queue = container_of(map, struct bpf_queue, map);
>> + struct queue_node *l;
>> +
>> + /* at this point bpf_prog->aux->refcnt == 0 and this map->refcnt == 0,
>> + * so the programs (can be more than one that used this map) were
>> + * disconnected from events. Wait for outstanding critical sections in
>> + * these programs to complete
>> + */
>> + synchronize_rcu();
>> +
>> + /* some of queue_elem_free_rcu() callbacks for elements of this map may
>> + * not have executed. Wait for them.
>> + */
>> + rcu_barrier();
>> + if (!queue_map_is_prealloc(queue))
>> + list_for_each_entry_rcu(l, &queue->head, list) {
>> + list_del_rcu(&l->list);
>> + kfree(l);
>> + }
>> + else
>> + prealloc_destroy(queue);
>> + kfree(queue);
>> +}
>> +
>> +static void queue_elem_free_rcu(struct rcu_head *head)
>> +{
>> + struct queue_node *l = container_of(head, struct queue_node, rcu);
>> + struct bpf_queue *queue = l->queue;
>> +
>> + /* must increment bpf_prog_active to avoid kprobe+bpf triggering while
>> + * we're calling kfree, otherwise deadlock is possible if kprobes
>> + * are placed somewhere inside of slub
>> + */
>> + preempt_disable();
>> + __this_cpu_inc(bpf_prog_active);
>> + if (queue_map_is_prealloc(queue))
>> + pcpu_freelist_push(&queue->freelist, &l->fnode);
>> + else
>> + kfree(l);
>> + __this_cpu_dec(bpf_prog_active);
>> + preempt_enable();
>> +}
>> +
>> +/* Called from syscall or from eBPF program */
>> +static void *queue_map_lookup_elem(struct bpf_map *map, void *key)
>> +{
>> + struct bpf_queue *queue = container_of(map, struct bpf_queue, map);
>> + unsigned long flags;
>> + struct queue_node *node;
>> +
>> + raw_spin_lock_irqsave(&queue->lock, flags);
> I think a per-cpu flavor would be very useful here as well in this map
> type such that we wouldn't need a lock here but only guarantee that
> preemption is disabled, and it could be used as temp store for the program
> for example.
I agree. I'd focus for now in the basic implementation, once it is ready
we could go ahead with a per-cpu version.
>
>> + node = list_first_or_null_rcu(&queue->head, struct queue_node, list);
>> + if (!node) {
>> + raw_spin_unlock_irqrestore(&queue->lock, flags);
>> + return NULL;
>> + }
>> +
>> + if (!queue_map_is_prealloc(queue))
>> + atomic_dec(&queue->count);
>> +
>> + list_del_rcu(&node->list);
>> + call_rcu(&node->rcu, queue_elem_free_rcu);
>> +
>> + raw_spin_unlock_irqrestore(&queue->lock, flags);
>> +
>> + return &node->element;
>> +}
>> +
>> +/* Called from syscall or from eBPF program */
>> +static int queue_map_update_elem(struct bpf_map *map, void *key, void *value,
>> + u64 map_flags)
>> +{
>> + struct bpf_queue *queue = container_of(map, struct bpf_queue, map);
>> + unsigned long flags;
>> + struct queue_node *new;
> Should reject invalid map update flags here.
Will do in new push helper proposed by Alexei.
>
>> + if (!queue_map_is_prealloc(queue)) {
>> + if (atomic_inc_return(&queue->count) > queue->map.max_entries) {
>> + atomic_dec(&queue->count);
>> + return -E2BIG;
>> + }
>> +
>> + new = kmalloc_node(queue->node_size, GFP_ATOMIC | __GFP_NOWARN,
>> + queue->map.numa_node);
>> + if (!new) {
>> + atomic_dec(&queue->count);
>> + return -ENOMEM;
>> + }
>> + } else {
>> + struct pcpu_freelist_node *l;
>> +
>> + l = pcpu_freelist_pop(&queue->freelist);
>> + if (!l)
>> + return -E2BIG;
>> + new = container_of(l, struct queue_node, fnode);
>> + }
>> +
>> + memcpy(new->element, value, queue->map.value_size);
>> + new->queue = queue;
>> +
>> + raw_spin_lock_irqsave(&queue->lock, flags);
>> + switch (queue->type) {
>> + case QUEUE_FIFO:
>> + list_add_tail_rcu(&new->list, &queue->head);
>> + break;
>> +
>> + case QUEUE_LIFO:
>> + list_add_rcu(&new->list, &queue->head);
>> + break;
>> + }
>> +
>> + raw_spin_unlock_irqrestore(&queue->lock, flags);
>> +
>> + return 0;
>> +}
>> +
>> +/* Called from syscall or from eBPF program */
>> +static int queue_map_delete_elem(struct bpf_map *map, void *key)
>> +{
>> + return -EINVAL;
>> +}
>> +
>> +/* Called from syscall */
>> +static int queue_map_get_next_key(struct bpf_map *map, void *key,
>> + void *next_key)
>> +{
>> + return -EINVAL;
>> +}
>> +
>> +const struct bpf_map_ops queue_map_ops = {
>> + .map_alloc_check = queue_map_alloc_check,
>> + .map_alloc = queue_map_alloc,
>> + .map_free = queue_map_free,
>> + .map_lookup_elem = queue_map_lookup_elem,
>> + .map_update_elem = queue_map_update_elem,
>> + .map_delete_elem = queue_map_delete_elem,
>> + .map_get_next_key = queue_map_get_next_key,
>> +};
>> +
>> diff --git a/kernel/bpf/syscall.c b/kernel/bpf/syscall.c
>> index a31a1ba0f8ea..7e9a11d69eef 100644
>> --- a/kernel/bpf/syscall.c
>> +++ b/kernel/bpf/syscall.c
>> @@ -622,11 +622,19 @@ static int map_lookup_elem(union bpf_attr *attr)
>> err = -EPERM;
>> goto err_put;
>> }
>> + if (map->map_type != BPF_MAP_TYPE_QUEUE) {
>> + key = memdup_user(ukey, map->key_size);
>> + if (IS_ERR(key)) {
>> + err = PTR_ERR(key);
>> + goto err_put;
>> + }
>> + } else {
>> + if (ukey) {
>> + err = -EINVAL;
>> + goto err_put;
>> + }
> Given you have a fixed key_size of 0, this could probably be made more
> generic and refactored a bit into a helper to check for 0 map->key_size
> instead of map type.
With a new set of helpers following changes are not needed anymore.
>
>> - key = memdup_user(ukey, map->key_size);
>> - if (IS_ERR(key)) {
>> - err = PTR_ERR(key);
>> - goto err_put;
>> + key = NULL;
>> }
>>
>> if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
>> @@ -709,10 +717,19 @@ static int map_update_elem(union bpf_attr *attr)
>> goto err_put;
>> }
>>
>> - key = memdup_user(ukey, map->key_size);
>> - if (IS_ERR(key)) {
>> - err = PTR_ERR(key);
>> - goto err_put;
>> + if (map->map_type != BPF_MAP_TYPE_QUEUE) {
>> + key = memdup_user(ukey, map->key_size);
>> + if (IS_ERR(key)) {
>> + err = PTR_ERR(key);
>> + goto err_put;
>> + }
>> + } else {
>> + if (ukey) {
>> + err = -EINVAL;
>> + goto err_put;
>> + }
>> +
>> + key = NULL;
>> }
> Ditto, and also further below as well for the other syscall cases.
>
>>
>> if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
>> @@ -803,10 +820,19 @@ static int map_delete_elem(union bpf_attr *attr)
>> goto err_put;
>> }
>>
>> - key = memdup_user(ukey, map->key_size);
>> - if (IS_ERR(key)) {
>> - err = PTR_ERR(key);
>> - goto err_put;
>> + if (map->map_type != BPF_MAP_TYPE_QUEUE) {
>> + key = memdup_user(ukey, map->key_size);
>> + if (IS_ERR(key)) {
>> + err = PTR_ERR(key);
>> + goto err_put;
>> + }
>> + } else {
>> + if (ukey) {
>> + err = -EINVAL;
>> + goto err_put;
>> + }
>> +
>> + key = NULL;
>> }
>>
>> if (bpf_map_is_dev_bound(map)) {
>> @@ -855,9 +881,14 @@ static int map_get_next_key(union bpf_attr *attr)
>> }
>>
>> if (ukey) {
>> - key = memdup_user(ukey, map->key_size);
>> - if (IS_ERR(key)) {
>> - err = PTR_ERR(key);
>> + if (map->map_type != BPF_MAP_TYPE_QUEUE) {
>> + key = memdup_user(ukey, map->key_size);
>> + if (IS_ERR(key)) {
>> + err = PTR_ERR(key);
>> + goto err_put;
>> + }
>> + } else {
>> + err = -EINVAL;
>> goto err_put;
>> }
>> } else {
>> diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c
>> index 25e47c195874..83099a9a21d9 100644
>> --- a/kernel/bpf/verifier.c
>> +++ b/kernel/bpf/verifier.c
>> @@ -1976,8 +1976,12 @@ static int check_func_arg(struct bpf_verifier_env *env, u32 regno,
>> return -EACCES;
>> }
>>
>> - if (arg_type == ARG_PTR_TO_MAP_KEY ||
>> - arg_type == ARG_PTR_TO_MAP_VALUE) {
>> + if (arg_type == ARG_PTR_TO_MAP_KEY) {
>> + expected_type = PTR_TO_STACK;
>> + if (!type_is_pkt_pointer(type) && type != PTR_TO_MAP_VALUE &&
>> + type != expected_type && type != SCALAR_VALUE)
>> + goto err_type;
>> + } else if (arg_type == ARG_PTR_TO_MAP_VALUE) {
>> expected_type = PTR_TO_STACK;
>> if (!type_is_pkt_pointer(type) && type != PTR_TO_MAP_VALUE &&
>> type != expected_type)
>> @@ -2021,6 +2025,7 @@ static int check_func_arg(struct bpf_verifier_env *env, u32 regno,
>> /* bpf_map_xxx(map_ptr) call: remember that map_ptr */
>> meta->map_ptr = reg->map_ptr;
>> } else if (arg_type == ARG_PTR_TO_MAP_KEY) {
>> + bool zero_size_allowed = false;
>> /* bpf_map_xxx(..., map_ptr, ..., key) call:
>> * check that [key, key + map->key_size) are within
>> * stack limits and initialized
>> @@ -2034,8 +2039,13 @@ static int check_func_arg(struct bpf_verifier_env *env, u32 regno,
>> verbose(env, "invalid map_ptr to access map->key\n");
>> return -EACCES;
>> }
>> +
>> + if (meta->map_ptr->map_type == BPF_MAP_TYPE_QUEUE)
> Here as well.
>
>> + zero_size_allowed = true;
> Also, verifier should rather enforce a const NULL key here than allowing one to
> be set (but not as the only 'valid' input option), meaning, I don't think it would
> be good to allow a 'zero' sized pointer to stack in this case.
>
>> err = check_helper_mem_access(env, regno,
>> - meta->map_ptr->key_size, false,
>> + meta->map_ptr->key_size,
>> + zero_size_allowed,
>> NULL);
>> } else if (arg_type == ARG_PTR_TO_MAP_VALUE) {
>> /* bpf_map_xxx(..., map_ptr, ..., value) call:
>>
>
^ permalink raw reply
* [GIT] Networking
From: David Miller @ 2018-08-09 5:06 UTC (permalink / raw)
To: torvalds; +Cc: akpm, netdev, linux-kernel
1) The real fix for the ipv6 route metric leak Sabrina was seeing,
from Cong Wang.
2) Fix syzbot triggers AF_PACKET v3 ring buffer insufficient room
conditions, from Willem de Bruijn.
3) vsock can reinitialize active work struct, fix from Cong Wang.
4) RXRPC keepalive generator can wedge a cpu, fix from David Howells.
5) Fix locking in AF_SMC ioctl, from Ursula Braun.
Please pull, thanks a lot!
The following changes since commit 1ffaddd029c867d134a1dde39f540dcc8c52e274:
Linux 4.18-rc8 (2018-08-05 12:37:41 -0700)
are available in the Git repository at:
gitolite@ra.kernel.org:/pub/scm/linux/kernel/git/davem/net.git
for you to fetch changes up to 1be52e97ed3e524f82e25d6e53f48df3c6e85282:
dsa: slave: eee: Allow ports to use phylink (2018-08-08 19:19:03 -0700)
----------------------------------------------------------------
Al Viro (1):
cxgb4: mk_act_open_req() buggers ->{local, peer}_ip on big-endian hosts
Alexey Kodanev (1):
dccp: fix undefined behavior with 'cwnd' shift in ccid2_cwnd_restart()
Andrew Lunn (1):
dsa: slave: eee: Allow ports to use phylink
Colin Ian King (1):
net: thunderx: check for failed allocation lmac->dmacs
Cong Wang (3):
ipv6: fix double refcount of fib6_metrics
vsock: split dwork to avoid reinitializations
llc: use refcount_inc_not_zero() for llc_sap_find()
David Howells (1):
rxrpc: Fix the keepalive generator [ver #2]
David S. Miller (2):
Merge branch 'mlx5-fixes'
Merge branch 'smc-fixes'
Dmitry Bogdanov (1):
net: aquantia: Fix IFF_ALLMULTI flag functionality
Huy Nguyen (1):
net/mlx5e: Cleanup of dcbnl related fields
Jason Wang (1):
vhost: reset metadata cache when initializing new IOTLB
Or Gerlitz (1):
net/mlx5e: Properly check if hairpin is possible between two functions
Ursula Braun (3):
net/smc: no shutdown in state SMC_LISTEN
net/smc: allow sysctl rmem and wmem defaults for servers
net/smc: move sock lock in smc_ioctl()
Willem de Bruijn (1):
packet: refine ring v3 block size test to hold one frame
Xin Long (1):
ip6_tunnel: use the right value for ipv4 min mtu check in ip6_tnl_xmit
Ying Xue (1):
tipc: fix an interrupt unsafe locking scenario
drivers/net/ethernet/aquantia/atlantic/hw_atl/hw_atl_b0.c | 2 +-
drivers/net/ethernet/cavium/thunder/thunder_bgx.c | 2 +
drivers/net/ethernet/chelsio/cxgb4/cxgb4_filter.c | 6 +--
drivers/net/ethernet/mellanox/mlx5/core/en.h | 2 -
drivers/net/ethernet/mellanox/mlx5/core/en_dcbnl.c | 30 +++++---------
drivers/net/ethernet/mellanox/mlx5/core/en_tc.c | 8 ++--
drivers/vhost/vhost.c | 9 ++--
include/net/af_vsock.h | 4 +-
include/net/llc.h | 5 +++
net/dccp/ccids/ccid2.c | 6 ++-
net/dsa/slave.c | 4 +-
net/ipv6/ip6_tunnel.c | 8 +---
net/ipv6/route.c | 4 --
net/llc/llc_core.c | 4 +-
net/packet/af_packet.c | 10 +++--
net/rxrpc/ar-internal.h | 8 ++--
net/rxrpc/conn_event.c | 4 +-
net/rxrpc/net_ns.c | 6 +--
net/rxrpc/output.c | 12 +++---
net/rxrpc/peer_event.c | 156 +++++++++++++++++++++++++++++++++++++++-------------------------------
net/rxrpc/peer_object.c | 8 ++--
net/rxrpc/rxkad.c | 4 +-
net/smc/af_smc.c | 15 ++++---
net/tipc/net.c | 4 +-
net/vmw_vsock/af_vsock.c | 15 +++----
net/vmw_vsock/vmci_transport.c | 3 +-
26 files changed, 178 insertions(+), 161 deletions(-)
^ permalink raw reply
* Re: [PATCH mlx5-next 0/6] Mellanox, mlx5 next updates 2018-08-09
From: David Miller @ 2018-08-09 2:37 UTC (permalink / raw)
To: saeedm; +Cc: netdev, linux-rdma, leonro, jgg, dledford
In-Reply-To: <20180808232353.15514-1-saeedm@mellanox.com>
From: Saeed Mahameed <saeedm@mellanox.com>
Date: Wed, 8 Aug 2018 16:23:47 -0700
> This series includes mlx5 core driver updates and mostly simple
> cleanups.
>
> From Denis: Use max #EQs reported by firmware to request MSIX vectors.
>
> From Eli: Trivial cleanups, unused arguments/functions and reduce
> command polling interval when command interface is in polling mode.
>
> From Eran: Rename vport state enums, to better reflect their actual
> usage.
Series applied to net-next, thank you.
^ permalink raw reply
* Re: [PATCH bpf] bpf: fix bpffs non-array map seq_show issue
From: Alexei Starovoitov @ 2018-08-09 2:25 UTC (permalink / raw)
To: Yonghong Song; +Cc: ast, daniel, netdev, kernel-team
In-Reply-To: <20180809012519.3534824-1-yhs@fb.com>
On Wed, Aug 08, 2018 at 06:25:19PM -0700, Yonghong Song wrote:
> In function map_seq_next() of kernel/bpf/inode.c,
> the first key will be the "0" regardless of the map type.
> This works for array. But for hash type, if it happens
> key "0" is in the map, the bpffs map show will miss
> some items if the key "0" is not the first element of
> the first bucket.
>
> This patch fixed the issue by guaranteeing to get
> the first element, if the seq_show is just started,
> by passing NULL pointer key to map_get_next_key() callback.
> This way, no missing elements will occur for
> bpffs hash table show even if key "0" is in the map.
>
> Fixes: a26ca7c982cb5 ("bpf: btf: Add pretty print support to the basic arraymap")
> Signed-off-by: Yonghong Song <yhs@fb.com>
Acked-by: Alexei Starovoitov <ast@kernel.org>
^ permalink raw reply
* Re: [PATCH] dsa: slave: eee: Allow ports to use phylink
From: David Miller @ 2018-08-09 2:19 UTC (permalink / raw)
To: andrew; +Cc: netdev, f.fainelli
In-Reply-To: <1533754600-1753-1-git-send-email-andrew@lunn.ch>
From: Andrew Lunn <andrew@lunn.ch>
Date: Wed, 8 Aug 2018 20:56:40 +0200
> For a port to be able to use EEE, both the MAC and the PHY must
> support EEE. A phy can be provided by both a phydev or phylink. Verify
> at least one of these exist, not just phydev.
>
> Fixes: aab9c4067d23 ("net: dsa: Plug in PHYLINK support")
> Signed-off-by: Andrew Lunn <andrew@lunn.ch>
Applied, thanks Andrew.
^ permalink raw reply
* Re: [PATCH net] net: aquantia: Fix IFF_ALLMULTI flag functionality
From: David Miller @ 2018-08-09 2:12 UTC (permalink / raw)
To: igor.russkikh; +Cc: netdev, darcari, dmitry.bogdanov
In-Reply-To: <30bef74fcd937e168589cfdf9a148c2674742410.1533306383.git.dmitry.bogdanov@aquantia.com>
From: Igor Russkikh <igor.russkikh@aquantia.com>
Date: Wed, 8 Aug 2018 14:06:32 +0300
> From: Dmitry Bogdanov <dmitry.bogdanov@aquantia.com>
>
> It was noticed that NIC always pass all multicast traffic to the host
> regardless of IFF_ALLMULTI flag on the interface.
> The rule in MC Filter Table in NIC, that is configured to accept any
> multicast packets, is turning on if IFF_MULTICAST flag is set on the
> interface. It leads to passing all multicast traffic to the host.
> This fix changes the condition to turn on that rule by checking
> IFF_ALLMULTI flag as it should.
>
> Fixes: b21f502 ("net:ethernet:aquantia: Fix for multicast filter handling.")
Please use 12 digits of significance for the SHA1_ID next time.
> Signed-off-by: Dmitry Bogdanov <dmitry.bogdanov@aquantia.com>
Applied and queued up for -stable.
^ permalink raw reply
* Re: [PATCH net 0/2] Mellanox, mlx5e fixes 2018-08-07
From: David Miller @ 2018-08-09 2:10 UTC (permalink / raw)
To: saeedm; +Cc: netdev
In-Reply-To: <20180808224808.12600-1-saeedm@mellanox.com>
From: Saeed Mahameed <saeedm@mellanox.com>
Date: Wed, 8 Aug 2018 15:48:06 -0700
> I know it is late into 4.18 release, and this is why I am submitting
> only two mlx5e ethernet fixes.
>
> The first one from Or, is needed for -stable and it fixes hairpin
> for "same device" check.
>
> The second fix is a non risk fix from Huy which cleans up and improves
> error return value reporting for dcbnl_ieee_setapp.
Series applied.
> For -stable v4.16
> - net/mlx5e: Properly check if hairpin is possible between two functions
Queued up.
^ permalink raw reply
* Re: [PATCH net-next] ieee802154: hwsim: fix rcu handling
From: David Miller @ 2018-08-09 1:37 UTC (permalink / raw)
To: stefan; +Cc: aring, netdev, linux-wpan, kernel
In-Reply-To: <0a1c05ae-b329-1053-d394-111e78348180@datenfreihafen.org>
From: Stefan Schmidt <stefan@datenfreihafen.org>
Date: Wed, 8 Aug 2018 22:05:54 +0200
> I would, once again, ask you to apply these three directly to
> net-next.
Ok, done.
^ permalink raw reply
* Re: [PATCH] net: phy: sftp: print debug message with text, not numbers
From: Andrew Lunn @ 2018-08-09 1:31 UTC (permalink / raw)
To: Russell King - ARM Linux; +Cc: David Miller, netdev, Florian Fainelli
In-Reply-To: <20180808221657.GO30658@n2100.armlinux.org.uk>
On Wed, Aug 08, 2018 at 11:16:57PM +0100, Russell King - ARM Linux wrote:
> You might want to fix the subject line.
Ah, yes, s/sftp/sfp/
Thanks
Andrew
^ permalink raw reply
page: next (older) | prev (newer) | latest
- recent:[subjects (threaded)|topics (new)|topics (active)]
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox