From: John Fastabend <john.fastabend@gmail.com> Date: 2020-01-12 02:37:16
Couple cleanup patches to recently posted series[0] from Bjorn to
cleanup and optimize the devmap usage. Patches have commit ids
the cleanup applies to.
Toshiaki, noted that virtio_net depends on rcu_lock being held
when calling xdp flush routines. This is specific to virtio_net
dereferencing xdp program to determine if xdp is enabled or not.
More typical pattern is to look at a flag set by the driver, at
least in the case of real hardware drivers. veth has a similar
requirement where it derferences the peer netdev priv structure
requiring the rcu_read_lock. I believe its better to put the
rcu_read_lock()/unlock() pair where its actually used in the
driver. FWIW in other xdp paths we expect driver writers to
place rcu_read_lock/unlock pairs where they are needed as well
so this keeps that expectation. Also it improves readability
making it obvious why the rcu_read_lock and unlock pair is
needed. In the virtio case we can probably do some further
cleanup and remove it altogether if we want. For more details
see patch 2/2.
[0] https://www.spinics.net/lists/netdev/msg620639.html
v2: Place rcu_read_{un}lock pair in virtio_net and veth drivers
so we don't break this requirement when removing rcu read
lock from devmap.
---
John Fastabend (2):
bpf: xdp, update devmap comments to reflect napi/rcu usage
bpf: xdp, remove no longer required rcu_read_{un}lock()
drivers/net/veth.c | 6 +++++-
drivers/net/virtio_net.c | 8 ++++++--
kernel/bpf/devmap.c | 26 ++++++++++++++------------
3 files changed, 25 insertions(+), 15 deletions(-)
--
Signature
From: John Fastabend <john.fastabend@gmail.com> Date: 2020-01-12 02:37:38
Now that we rely on synchronize_rcu and call_rcu waiting to
exit perempt-disable regions (NAPI) lets update the comments
to reflect this.
Fixes: 0536b85239b84 ("xdp: Simplify devmap cleanup")
Acked-by: Björn Töpel <redacted>
Acked-by: Song Liu <redacted>
Signed-off-by: John Fastabend <john.fastabend@gmail.com>
---
kernel/bpf/devmap.c | 21 +++++++++++----------
1 file changed, 11 insertions(+), 10 deletions(-)
@@ -193,10 +193,12 @@ static void dev_map_free(struct bpf_map *map)/* At this point bpf_prog->aux->refcnt == 0 and this map->refcnt == 0,*sotheprograms(canbemorethanonethatusedthismap)were-*disconnectedfromevents.Waitforoutstandingcriticalsectionsin-*theseprogramstocomplete.Thercucriticalsectiononlyguarantees-*nofurtherreadsagainstnetdev_map.Itdoes__not__ensurepending-*flushoperations(ifany)arecomplete.+*disconnectedfromevents.Thefollowingsynchronize_rcu()guarantees+*bothrcureadcriticalsectionscompleteandwaitsfor+*preempt-disableregions(NAPIbeingtherelaventcontexthere)sowe+*arecertaintherewillbenofurtherreadsagainstthenetdev_mapand+*allflushoperationsarecomplete.Flushoperationscanonlybedone+*fromNAPIcontextforthisreason.*/spin_lock(&dev_map_lock);
@@ -498,12 +500,11 @@ static int dev_map_delete_elem(struct bpf_map *map, void *key)return-EINVAL;/* Use call_rcu() here to ensure any rcu critical sections have-*completed,butthisdoesnotguaranteeaflushhashappened-*yet.Becausedriversidercu_read_lock/unlockonlyprotectsthe-*runningXDPprogram.However,forpendingflushoperationsthe-*devandctxarestoredinanotherpercpumap.Andadditionally,-*thedriverteardownensuresallsoftirqsarecompletebefore-*removingthenetdeviceinthecaseofdev_putequalszero.+*completedaswellasanyflushoperationsbecausecall_rcu+*willwaitforpreempt-disableregiontocomplete,NAPIinthis+*context.Andadditionally,thedriverteardownensuresall+*softirqsarecompletebeforeremovingthenetdeviceinthe+*caseofdev_putequalszero.*/old_dev=xchg(&dtab->netdev_map[k],NULL);if(old_dev)
From: John Fastabend <john.fastabend@gmail.com> Date: 2020-01-12 02:37:59
Now that we depend on rcu_call() and synchronize_rcu() to also wait
for preempt_disabled region to complete the rcu read critical section
in __dev_map_flush() is no longer required. Except in a few special
cases in drivers that need it for other reasons.
These originally ensured the map reference was safe while a map was
also being free'd. And additionally that bpf program updates via
ndo_bpf did not happen while flush updates were in flight. But flush
by new rules can only be called from preempt-disabled NAPI context.
The synchronize_rcu from the map free path and the rcu_call from the
delete path will ensure the reference there is safe. So lets remove
the rcu_read_lock and rcu_read_unlock pair to avoid any confusion
around how this is being protected.
If the rcu_read_lock was required it would mean errors in the above
logic and the original patch would also be wrong.
Now that we have done above we put the rcu_read_lock in the driver
code where it is needed in a driver dependent way. I think this
helps readability of the code so we know where and why we are
taking read locks. Most drivers will not need rcu_read_locks here
and further XDP drivers already have rcu_read_locks in their code
paths for reading xdp programs on RX side so this makes it symmetric
where we don't have half of rcu critical sections define in driver
and the other half in devmap.
Fixes: 0536b85239b84 ("xdp: Simplify devmap cleanup")
Signed-off-by: John Fastabend <john.fastabend@gmail.com>
---
drivers/net/veth.c | 6 +++++-
drivers/net/virtio_net.c | 8 ++++++--
kernel/bpf/devmap.c | 5 +++--
3 files changed, 14 insertions(+), 5 deletions(-)
@@ -498,12 +498,16 @@ static int virtnet_xdp_xmit(struct net_device *dev,void*ptr;inti;+rcu_read_lock();+/* Only allow ndo_xdp_xmit if XDP is loaded on dev, as this*indicateXDPresourceshavebeensuccessfullyallocated.*/xdp_prog=rcu_dereference(rq->xdp_prog);-if(!xdp_prog)+if(!xdp_prog){+rcu_read_unlock();return-ENXIO;+}sq=virtnet_xdp_sq(vi);
@@ -552,7 +556,7 @@ static int virtnet_xdp_xmit(struct net_device *dev,sq->stats.xdp_tx_drops+=drops;sq->stats.kicks+=kicks;u64_stats_update_end(&sq->stats.syncp);-+rcu_read_unlock();returnret;}
@@ -372,16 +372,17 @@ static int bq_xmit_all(struct xdp_bulk_queue *bq, u32 flags)*fromNET_RX_SOFTIRQ.Eitherwaythepollroutinemustcompletebeforethe*netdevicecanbetorndown.Ondevmapteardownweensuretheflushlist*isemptybeforecompletingtoensureallflushoperationshavecompleted.+*Whendriversupdatethebpfprogramtheymayneedtoensureanyflushops+*arealsocomplete.Usingsynchronize_rcuorcall_rcuwillsufficeforthis+*becausebothwaitfornapicontexttoexit.*/void__dev_map_flush(void){structlist_head*flush_list=this_cpu_ptr(&dev_map_flush_list);structxdp_bulk_queue*bq,*tmp;-rcu_read_lock();list_for_each_entry_safe(bq,tmp,flush_list,flush_node)bq_xmit_all(bq,XDP_XMIT_FLUSH);-rcu_read_unlock();}/* rcu_read_lock (from syscall and BPF contexts) ensures that if a delete and/or
Now that we rely on synchronize_rcu and call_rcu waiting to
exit perempt-disable regions (NAPI) lets update the comments
to reflect this.
Fixes: 0536b85239b84 ("xdp: Simplify devmap cleanup")
Acked-by: Björn Töpel <redacted>
Acked-by: Song Liu <redacted>
Signed-off-by: John Fastabend <john.fastabend@gmail.com>
Now that we depend on rcu_call() and synchronize_rcu() to also wait
for preempt_disabled region to complete the rcu read critical section
in __dev_map_flush() is no longer required. Except in a few special
cases in drivers that need it for other reasons.
These originally ensured the map reference was safe while a map was
also being free'd. And additionally that bpf program updates via
ndo_bpf did not happen while flush updates were in flight. But flush
by new rules can only be called from preempt-disabled NAPI context.
The synchronize_rcu from the map free path and the rcu_call from the
delete path will ensure the reference there is safe. So lets remove
the rcu_read_lock and rcu_read_unlock pair to avoid any confusion
around how this is being protected.
If the rcu_read_lock was required it would mean errors in the above
logic and the original patch would also be wrong.
Now that we have done above we put the rcu_read_lock in the driver
code where it is needed in a driver dependent way. I think this
helps readability of the code so we know where and why we are
taking read locks. Most drivers will not need rcu_read_locks here
and further XDP drivers already have rcu_read_locks in their code
paths for reading xdp programs on RX side so this makes it symmetric
where we don't have half of rcu critical sections define in driver
and the other half in devmap.
Fixes: 0536b85239b84 ("xdp: Simplify devmap cleanup")
Signed-off-by: John Fastabend <john.fastabend@gmail.com>
From: Maciej Fijalkowski <maciej.fijalkowski@intel.com> Date: 2020-01-12 13:57:05
On Sat, Jan 11, 2020 at 06:37:42PM -0800, John Fastabend wrote:
quoted hunk
Now that we depend on rcu_call() and synchronize_rcu() to also wait
for preempt_disabled region to complete the rcu read critical section
in __dev_map_flush() is no longer required. Except in a few special
cases in drivers that need it for other reasons.
These originally ensured the map reference was safe while a map was
also being free'd. And additionally that bpf program updates via
ndo_bpf did not happen while flush updates were in flight. But flush
by new rules can only be called from preempt-disabled NAPI context.
The synchronize_rcu from the map free path and the rcu_call from the
delete path will ensure the reference there is safe. So lets remove
the rcu_read_lock and rcu_read_unlock pair to avoid any confusion
around how this is being protected.
If the rcu_read_lock was required it would mean errors in the above
logic and the original patch would also be wrong.
Now that we have done above we put the rcu_read_lock in the driver
code where it is needed in a driver dependent way. I think this
helps readability of the code so we know where and why we are
taking read locks. Most drivers will not need rcu_read_locks here
and further XDP drivers already have rcu_read_locks in their code
paths for reading xdp programs on RX side so this makes it symmetric
where we don't have half of rcu critical sections define in driver
and the other half in devmap.
Fixes: 0536b85239b84 ("xdp: Simplify devmap cleanup")
Signed-off-by: John Fastabend <john.fastabend@gmail.com>
---
drivers/net/veth.c | 6 +++++-
drivers/net/virtio_net.c | 8 ++++++--
kernel/bpf/devmap.c | 5 +++--
3 files changed, 14 insertions(+), 5 deletions(-)
@@ -498,12 +498,16 @@ static int virtnet_xdp_xmit(struct net_device *dev,void*ptr;inti;+rcu_read_lock();+/* Only allow ndo_xdp_xmit if XDP is loaded on dev, as this*indicateXDPresourceshavebeensuccessfullyallocated.*/xdp_prog=rcu_dereference(rq->xdp_prog);
We could convert that rcu_dereference to rcu_access_pointer so that we
don't need the rcu critical section here at all. Actually this was
suggested some time ago by David Ahern during the initial discussion
around this code. Not sure why we didn't change it.
Veth is also checking the xdp prog presence and it is doing that via
rcu_access_pointer so such conversion would make it more common, no?
xdp_prog is only check against NULL, so quoting the part of comment from
rcu_access_pointer:
"This is useful when the value of this pointer is accessed, but the pointer
is not dereferenced, for example, when testing an RCU-protected pointer
against NULL."
quoted hunk
- if (!xdp_prog)
+ if (!xdp_prog) {
+ rcu_read_unlock();
return -ENXIO;
+ }
sq = virtnet_xdp_sq(vi);
@@ -372,16 +372,17 @@ static int bq_xmit_all(struct xdp_bulk_queue *bq, u32 flags)*fromNET_RX_SOFTIRQ.Eitherwaythepollroutinemustcompletebeforethe*netdevicecanbetorndown.Ondevmapteardownweensuretheflushlist*isemptybeforecompletingtoensureallflushoperationshavecompleted.+*Whendriversupdatethebpfprogramtheymayneedtoensureanyflushops+*arealsocomplete.Usingsynchronize_rcuorcall_rcuwillsufficeforthis+*becausebothwaitfornapicontexttoexit.*/void__dev_map_flush(void){structlist_head*flush_list=this_cpu_ptr(&dev_map_flush_list);structxdp_bulk_queue*bq,*tmp;-rcu_read_lock();list_for_each_entry_safe(bq,tmp,flush_list,flush_node)bq_xmit_all(bq,XDP_XMIT_FLUSH);-rcu_read_unlock();}/* rcu_read_lock (from syscall and BPF contexts) ensures that if a delete and/or
@@ -193,10 +193,12 @@ static void dev_map_free(struct bpf_map *map)/* At this point bpf_prog->aux->refcnt == 0 and this map->refcnt == 0,*sotheprograms(canbemorethanonethatusedthismap)were-*disconnectedfromevents.Waitforoutstandingcriticalsectionsin-*theseprogramstocomplete.Thercucriticalsectiononlyguarantees-*nofurtherreadsagainstnetdev_map.Itdoes__not__ensurepending-*flushoperations(ifany)arecomplete.+*disconnectedfromevents.Thefollowingsynchronize_rcu()guarantees+*bothrcureadcriticalsectionscompleteandwaitsfor+*preempt-disableregions(NAPIbeingtherelaventcontexthere)sowe
s/relavent/relevant
quoted hunk
+ * are certain there will be no further reads against the netdev_map and
+ * all flush operations are complete. Flush operations can only be done
+ * from NAPI context for this reason.
*/
spin_lock(&dev_map_lock);
@@ -498,12 +500,11 @@ static int dev_map_delete_elem(struct bpf_map *map, void *key) return -EINVAL; /* Use call_rcu() here to ensure any rcu critical sections have- * completed, but this does not guarantee a flush has happened- * yet. Because driver side rcu_read_lock/unlock only protects the- * running XDP program. However, for pending flush operations the- * dev and ctx are stored in another per cpu map. And additionally,- * the driver tear down ensures all soft irqs are complete before- * removing the net device in the case of dev_put equals zero.+ * completed as well as any flush operations because call_rcu+ * will wait for preempt-disable region to complete, NAPI in this+ * context. And additionally, the driver tear down ensures all+ * soft irqs are complete before removing the net device in the+ * case of dev_put equals zero. */ old_dev = xchg(&dtab->netdev_map[k], NULL); if (old_dev)
From: John Fastabend <john.fastabend@gmail.com> Date: 2020-01-14 03:26:01
Maciej Fijalkowski wrote:
On Sat, Jan 11, 2020 at 06:37:42PM -0800, John Fastabend wrote:
quoted
Now that we depend on rcu_call() and synchronize_rcu() to also wait
for preempt_disabled region to complete the rcu read critical section
in __dev_map_flush() is no longer required. Except in a few special
cases in drivers that need it for other reasons.
These originally ensured the map reference was safe while a map was
also being free'd. And additionally that bpf program updates via
ndo_bpf did not happen while flush updates were in flight. But flush
by new rules can only be called from preempt-disabled NAPI context.
The synchronize_rcu from the map free path and the rcu_call from the
delete path will ensure the reference there is safe. So lets remove
the rcu_read_lock and rcu_read_unlock pair to avoid any confusion
around how this is being protected.
If the rcu_read_lock was required it would mean errors in the above
logic and the original patch would also be wrong.
Now that we have done above we put the rcu_read_lock in the driver
code where it is needed in a driver dependent way. I think this
helps readability of the code so we know where and why we are
taking read locks. Most drivers will not need rcu_read_locks here
and further XDP drivers already have rcu_read_locks in their code
paths for reading xdp programs on RX side so this makes it symmetric
where we don't have half of rcu critical sections define in driver
and the other half in devmap.
Fixes: 0536b85239b84 ("xdp: Simplify devmap cleanup")
Signed-off-by: John Fastabend <john.fastabend@gmail.com>
---
@@ -498,12 +498,16 @@ static int virtnet_xdp_xmit(struct net_device *dev,void*ptr;inti;+rcu_read_lock();+/* Only allow ndo_xdp_xmit if XDP is loaded on dev, as this*indicateXDPresourceshavebeensuccessfullyallocated.*/xdp_prog=rcu_dereference(rq->xdp_prog);
We could convert that rcu_dereference to rcu_access_pointer so that we
don't need the rcu critical section here at all. Actually this was
suggested some time ago by David Ahern during the initial discussion
around this code. Not sure why we didn't change it.
Makes sense I'll send a v3 with a middle patch to do this and then drop
this segment.
Veth is also checking the xdp prog presence and it is doing that via
rcu_access_pointer so such conversion would make it more common, no?
veth derefernces rcv netdevice and this accesses it. The logic to
drop the rcu here is less obvious to me. At least I would have to
study it closely.
xdp_prog is only check against NULL, so quoting the part of comment from
rcu_access_pointer:
"This is useful when the value of this pointer is accessed, but the pointer
is not dereferenced, for example, when testing an RCU-protected pointer
against NULL."
From: Maciej Fijalkowski <maciej.fijalkowski@intel.com> Date: 2020-01-14 07:39:17
On Mon, Jan 13, 2020 at 07:25:51PM -0800, John Fastabend wrote:
Maciej Fijalkowski wrote:
quoted
On Sat, Jan 11, 2020 at 06:37:42PM -0800, John Fastabend wrote:
quoted
Now that we depend on rcu_call() and synchronize_rcu() to also wait
one last thing since you'll be sending a v3, s/rcu_call/call_rcu.
quoted
quoted
for preempt_disabled region to complete the rcu read critical section
in __dev_map_flush() is no longer required. Except in a few special
cases in drivers that need it for other reasons.
These originally ensured the map reference was safe while a map was
also being free'd. And additionally that bpf program updates via
ndo_bpf did not happen while flush updates were in flight. But flush
by new rules can only be called from preempt-disabled NAPI context.
The synchronize_rcu from the map free path and the rcu_call from the
delete path will ensure the reference there is safe. So lets remove
the rcu_read_lock and rcu_read_unlock pair to avoid any confusion
around how this is being protected.
If the rcu_read_lock was required it would mean errors in the above
logic and the original patch would also be wrong.
Now that we have done above we put the rcu_read_lock in the driver
code where it is needed in a driver dependent way. I think this
helps readability of the code so we know where and why we are
taking read locks. Most drivers will not need rcu_read_locks here
and further XDP drivers already have rcu_read_locks in their code
paths for reading xdp programs on RX side so this makes it symmetric
where we don't have half of rcu critical sections define in driver
and the other half in devmap.
Fixes: 0536b85239b84 ("xdp: Simplify devmap cleanup")
Signed-off-by: John Fastabend <john.fastabend@gmail.com>
---
@@ -498,12 +498,16 @@ static int virtnet_xdp_xmit(struct net_device *dev,void*ptr;inti;+rcu_read_lock();+/* Only allow ndo_xdp_xmit if XDP is loaded on dev, as this*indicateXDPresourceshavebeensuccessfullyallocated.*/xdp_prog=rcu_dereference(rq->xdp_prog);
We could convert that rcu_dereference to rcu_access_pointer so that we
don't need the rcu critical section here at all. Actually this was
suggested some time ago by David Ahern during the initial discussion
around this code. Not sure why we didn't change it.
Makes sense I'll send a v3 with a middle patch to do this and then drop
this segment.
Great :)
quoted
Veth is also checking the xdp prog presence and it is doing that via
rcu_access_pointer so such conversion would make it more common, no?
veth derefernces rcv netdevice and this accesses it. The logic to
drop the rcu here is less obvious to me. At least I would have to
study it closely.
Veth does two rcu derefs in the veth_xmit, one for netdev and one for
xdp_prog and I was referring to a xdp_prog deref which is done via
rcu_access_pointer. So yeah we need to keep the rcu section in there but I
was just making an argument for having the rcu_access_pointer on the
virtio_net side.
quoted
xdp_prog is only check against NULL, so quoting the part of comment from
rcu_access_pointer:
"This is useful when the value of this pointer is accessed, but the pointer
is not dereferenced, for example, when testing an RCU-protected pointer
against NULL."