Skip to contentSkip to conversation

xdp: add NULL pointer check in __xdp_return()

PATCH Superseded Taehee Yoo 6 participants 14 messages Jul 20, 2018 – Aug 2, 2018

Subsystems (4)

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Taehee Yoo

author
#0 lore raw

PATCH 1/1 xdp: add NULL pointer check in __xdp_return()

rhashtable_lookup() can return NULL. so that NULL pointer check routine should be added.

Fixes 02b55e5657c3 ("xdp: add MEM_TYPE_ZERO_COPY")
---
 net/core/xdp.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
net/core/xdp.c +2 -1
index 9d1f220..1c12bc7 100644
@@ -345,7 +345,8 @@ static void __xdp_return(void *data, struct xdp_mem_info *mem, bool napi_direct,
 		rcu_read_lock();
 		/* mem->id is valid, checked in xdp_rxq_info_reg_mem_model() */
 		xa = rhashtable_lookup(mem_id_ht, &mem->id, mem_id_rht_params);
-		xa->zc_alloc->free(xa->zc_alloc, handle);
+		if (xa)
+			xa->zc_alloc->free(xa->zc_alloc, handle);
 		rcu_read_unlock();
 	default:
 		/* Not possible, checked in xdp_rxq_info_reg_mem_model() */
Signature · 1 line
2.9.3
#1 lore raw
Quoted 14 lines · Taehee Yoo

rhashtable_lookup() can return NULL. so that NULL pointer check routine should be added.

Fixes 02b55e5657c3 ("xdp: add MEM_TYPE_ZERO_COPY")
---
 net/core/xdp.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
net/core/xdp.c +0 -0
index 9d1f220..1c12bc7 100644

net/core/xdp.c static void __xdp_return(void *data, struct xdp_mem_info *mem, bool napi_direct, · line 349

Quoted 6 lines
net/core/xdp.c +2 -1
@@ -345,7 +345,8 @@ static void __xdp_return(void *data, struct xdp_mem_info *mem, bool napi_direct,
 		rcu_read_lock();
 		/* mem->id is valid, checked in xdp_rxq_info_reg_mem_model() */
 		xa = rhashtable_lookup(mem_id_ht, &mem->id, mem_id_rht_params);
-		xa->zc_alloc->free(xa->zc_alloc, handle);
+		if (xa)
+			xa->zc_alloc->free(xa->zc_alloc, handle);

hmm...It is not clear to me the "!xa" case don't have to be handled?

Quoted 6 lines
net/core/xdp.c +0 -0
 		rcu_read_unlock();
 	default:
 		/* Not possible, checked in xdp_rxq_info_reg_mem_model() */
Signature · 1 line
2.9.3
#2 lore raw
Quoted 10 lines · Martin KaFai Lau
Quoted 9 lines · Taehee Yoo

rhashtable_lookup() can return NULL. so that NULL pointer check routine should be added.

Fixes 02b55e5657c3 ("xdp: add MEM_TYPE_ZERO_COPY")
---
 net/core/xdp.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
Quoted 12 lines

net/core/xdp.c static void __xdp_return(void *data, struct xdp_mem_info *mem, bool napi_direct, · line 349

Quoted 11 lines
net/core/xdp.c +2 -1
index 9d1f220..1c12bc7 100644
@@ -345,7 +345,8 @@ static void __xdp_return(void *data, struct xdp_mem_info *mem, bool napi_direct,
 		rcu_read_lock();
 		/* mem->id is valid, checked in xdp_rxq_info_reg_mem_model() */
 		xa = rhashtable_lookup(mem_id_ht, &mem->id, mem_id_rht_params);
-		xa->zc_alloc->free(xa->zc_alloc, handle);
+		if (xa)
+			xa->zc_alloc->free(xa->zc_alloc, handle);  

hmm...It is not clear to me the "!xa" case don't have to be handled?

Actually I have a more fundamental question about this interface I've been meaning to ask.

IIUC free() can happen on any CPU at any time, when whatever device, socket or CPU this got redirected to completed the TX. IOW there may be multiple producers. Drivers would need to create spin lock a'la the a9744f7ca200 ("xsk: fix potential race in SKB TX completion code") fix?

We need some form of internal kernel circulation which would be MPSC. I'm currently hacking up the XSK code to tell me whether the frame was consumed by the correct XSK, and always clone the frame otherwise (claiming to be the "traditional" MEM_TYPE_PAGE_ORDER0).

I feel like I'm missing something about the code. Is redirect of ZC/UMEM frame outside the xsk not possible and the only returns we will see are from net/xdp/xsk.c? That would work, but I don't see such a check. Help would be appreciated.

Also the fact that XSK bufs can't be freed, only completed, adds to the pain of implementing AF_XDP, we'd certainly need some form of "give back the frame, but I may need it later" SPSC mechanism, otherwise driver writers will have tough time. Unless, again, I'm missing something about the code :)

Quoted 3 lines
Quoted 3 lines
net/core/xdp.c +0 -0
 		rcu_read_unlock();
 	default:
 		/* Not possible, checked in xdp_rxq_info_reg_mem_model() */
author
#7 lore raw
In reply to Martin KaFai Lau

2018-07-21 2:18 GMT+09:00 Martin KaFai Lau <[email protected]>:

Quoted 10 lines · Taehee Yoo
Quoted 9 lines

rhashtable_lookup() can return NULL. so that NULL pointer check routine should be added.

Fixes 02b55e5657c3 ("xdp: add MEM_TYPE_ZERO_COPY")
---
 net/core/xdp.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
Quoted 12 lines

net/core/xdp.c static void __xdp_return(void *data, struct xdp_mem_info *mem, bool napi_direct, · line 349

Quoted 11 lines
net/core/xdp.c +2 -1
index 9d1f220..1c12bc7 100644
@@ -345,7 +345,8 @@ static void __xdp_return(void *data, struct xdp_mem_info *mem, bool napi_direct,
              rcu_read_lock();
              /* mem->id is valid, checked in xdp_rxq_info_reg_mem_model() */
              xa = rhashtable_lookup(mem_id_ht, &mem->id, mem_id_rht_params);
-             xa->zc_alloc->free(xa->zc_alloc, handle);
+             if (xa)
+                     xa->zc_alloc->free(xa->zc_alloc, handle);

hmm...It is not clear to me the "!xa" case don't have to be handled?

Thank you for reviewing!

Returning NULL pointer is bug case such as calling after use xdp_rxq_info_unreg().
so that, I think it can't handle at that moment. we can make __xdp_return to add WARN_ON_ONCE() or add return error code to driver.
But I'm not sure if these is useful information.

I might have misunderstood scenario of MEM_TYPE_ZERO_COPY because there is no use case of MEM_TYPE_ZERO_COPY yet.

Thanks!

Quoted 7 lines
Quoted 7 lines
net/core/xdp.c +0 -1
              rcu_read_unlock();
      default:
              /* Not possible, checked in xdp_rxq_info_reg_mem_model() */
--

2.9.3

#3 lore raw
In reply to Jakub Kicinski

Den fre 20 juli 2018 kl 22:08 skrev Jakub Kicinski <[email protected]>:

Quoted 25 lines · Martin KaFai Lau
Quoted 10 lines · Taehee Yoo
Quoted 9 lines

rhashtable_lookup() can return NULL. so that NULL pointer check routine should be added.

Fixes 02b55e5657c3 ("xdp: add MEM_TYPE_ZERO_COPY")
---
 net/core/xdp.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
Quoted 12 lines

net/core/xdp.c static void __xdp_return(void *data, struct xdp_mem_info *mem, bool napi_direct, · line 349

Quoted 11 lines
net/core/xdp.c +2 -1
index 9d1f220..1c12bc7 100644
@@ -345,7 +345,8 @@ static void __xdp_return(void *data, struct xdp_mem_info *mem, bool napi_direct,
            rcu_read_lock();
            /* mem->id is valid, checked in xdp_rxq_info_reg_mem_model() */
            xa = rhashtable_lookup(mem_id_ht, &mem->id, mem_id_rht_params);
-           xa->zc_alloc->free(xa->zc_alloc, handle);
+           if (xa)
+                   xa->zc_alloc->free(xa->zc_alloc, handle);

hmm...It is not clear to me the "!xa" case don't have to be handled?

Quoted 8 lines

Actually I have a more fundamental question about this interface I've been meaning to ask.

IIUC free() can happen on any CPU at any time, when whatever device, socket or CPU this got redirected to completed the TX. IOW there may be multiple producers. Drivers would need to create spin lock a'la the a9744f7ca200 ("xsk: fix potential race in SKB TX completion code") fix?

Jakub, apologies for the slow response. I'm still in "holiday/hammock&beer mode", but will be back in a week. :-P

The idea with the xdp_return_* functions are that an xdp_buff and xdp_frame can have custom allocations schemes. The difference beween struct xdp_buff and struct xdp_frame is lifetime. The xdp_buff lifetime is within the napi context, whereas xdp_frame can have a lifetime longer/outside the napi context. E.g. for a XDP_REDIRECT scenario an xdp_buff is converted to a xdp_frame. The conversion is done in include/net/xdp.h:convert_to_xdp_frame.

Currently, the zero-copy MEM_TYPE_ZERO_COPY memtype can *only* be used for xdp_buff, meaning that the lifetime is constrained to a napi context. Further, given an xdp_buff with memtype MEM_TYPE_ZERO_COPY, doing XDP_REDIRECT to a target that is *not* an AF_XDP socket would mean converting the xdp_buff to an xdp_frame. The xdp_frame can then be free'd on any CPU.

Note that the xsk_rcv* functions is always called from an napi context, and therefore is using the xdp_return_buff calls.

To answer your question -- no, this fix is *not* needed, because the xdp_buff napi constrained, and the xdp_buff will only be free'd on one CPU.

Quoted 10 lines

We need some form of internal kernel circulation which would be MPSC. I'm currently hacking up the XSK code to tell me whether the frame was consumed by the correct XSK, and always clone the frame otherwise (claiming to be the "traditional" MEM_TYPE_PAGE_ORDER0).

I feel like I'm missing something about the code. Is redirect of ZC/UMEM frame outside the xsk not possible and the only returns we will see are from net/xdp/xsk.c? That would work, but I don't see such a check. Help would be appreciated.

Right now, this is the case (refer to the TODO in convert_to_xdp_frame), i.e. you cannot redirect an ZC/UMEM allocated xdp_buff to a target that is not an xsk. This must, obviously, change so that an xdp_buff (of MEM_TYPE_ZERO_COPY) can be converted to an xdp_frame. The xdp_frame must be able to be free'd from multiple CPUs, so here the a more sophisticated allocation scheme is required.

Quoted 6 lines

Also the fact that XSK bufs can't be freed, only completed, adds to the pain of implementing AF_XDP, we'd certainly need some form of "give back the frame, but I may need it later" SPSC mechanism, otherwise driver writers will have tough time. Unless, again, I'm missing something about the code :)

Yup, moving the recycling scheme from driver to "generic" is a good idea! I need to finish up those i40e zerocopy patches first though...

(...and I'm very excited that you're doing nfp support for AF_XDP!!!)

Björn

Quoted 4 lines
Quoted 3 lines
Quoted 3 lines
net/core/xdp.c +0 -0
            rcu_read_unlock();
    default:
            /* Not possible, checked in xdp_rxq_info_reg_mem_model() */
#8 lore raw
In reply to Taehee Yoo

Den lör 21 juli 2018 kl 14:58 skrev Taehee Yoo <[email protected]>:

Quoted 34 lines

2018-07-21 2:18 GMT+09:00 Martin KaFai Lau <[email protected]>:

Quoted 10 lines · Taehee Yoo
Quoted 9 lines

rhashtable_lookup() can return NULL. so that NULL pointer check routine should be added.

Fixes 02b55e5657c3 ("xdp: add MEM_TYPE_ZERO_COPY")
---
 net/core/xdp.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
Quoted 12 lines

net/core/xdp.c static void __xdp_return(void *data, struct xdp_mem_info *mem, bool napi_direct, · line 349

Quoted 11 lines
net/core/xdp.c +2 -1
index 9d1f220..1c12bc7 100644
@@ -345,7 +345,8 @@ static void __xdp_return(void *data, struct xdp_mem_info *mem, bool napi_direct,
              rcu_read_lock();
              /* mem->id is valid, checked in xdp_rxq_info_reg_mem_model() */
              xa = rhashtable_lookup(mem_id_ht, &mem->id, mem_id_rht_params);
-             xa->zc_alloc->free(xa->zc_alloc, handle);
+             if (xa)
+                     xa->zc_alloc->free(xa->zc_alloc, handle);

hmm...It is not clear to me the "!xa" case don't have to be handled?

Thank you for reviewing!

Returning NULL pointer is bug case such as calling after use xdp_rxq_info_unreg().
so that, I think it can't handle at that moment. we can make __xdp_return to add WARN_ON_ONCE() or add return error code to driver.
But I'm not sure if these is useful information.

Quoted 3 lines

I might have misunderstood scenario of MEM_TYPE_ZERO_COPY because there is no use case of MEM_TYPE_ZERO_COPY yet.

Taehee, again, sorry for the slow response and thanks for patch!

If xa is NULL, the driver has a buggy/broken implementation. What would be a proper way of dealing with this? BUG?

Björn

Quoted 9 lines

Thanks!

Quoted 7 lines
Quoted 7 lines
net/core/xdp.c +0 -1
              rcu_read_unlock();
      default:
              /* Not possible, checked in xdp_rxq_info_reg_mem_model() */
--

2.9.3

author
#9 lore raw

2018-07-23 18:41 GMT+09:00 Björn Töpel <[email protected]>:

Quoted 35 lines

Den lör 21 juli 2018 kl 14:58 skrev Taehee Yoo <[email protected]>:

Quoted 34 lines

2018-07-21 2:18 GMT+09:00 Martin KaFai Lau <[email protected]>:

Quoted 10 lines · Taehee Yoo
Quoted 9 lines

rhashtable_lookup() can return NULL. so that NULL pointer check routine should be added.

Fixes 02b55e5657c3 ("xdp: add MEM_TYPE_ZERO_COPY")
---
 net/core/xdp.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
Quoted 12 lines

net/core/xdp.c static void __xdp_return(void *data, struct xdp_mem_info *mem, bool napi_direct, · line 349

Quoted 11 lines
net/core/xdp.c +2 -1
index 9d1f220..1c12bc7 100644
@@ -345,7 +345,8 @@ static void __xdp_return(void *data, struct xdp_mem_info *mem, bool napi_direct,
              rcu_read_lock();
              /* mem->id is valid, checked in xdp_rxq_info_reg_mem_model() */
              xa = rhashtable_lookup(mem_id_ht, &mem->id, mem_id_rht_params);
-             xa->zc_alloc->free(xa->zc_alloc, handle);
+             if (xa)
+                     xa->zc_alloc->free(xa->zc_alloc, handle);

hmm...It is not clear to me the "!xa" case don't have to be handled?

Thank you for reviewing!

Returning NULL pointer is bug case such as calling after use xdp_rxq_info_unreg().
so that, I think it can't handle at that moment. we can make __xdp_return to add WARN_ON_ONCE() or add return error code to driver.
But I'm not sure if these is useful information.

Quoted 9 lines
Quoted 3 lines

I might have misunderstood scenario of MEM_TYPE_ZERO_COPY because there is no use case of MEM_TYPE_ZERO_COPY yet.

Taehee, again, sorry for the slow response and thanks for patch!

If xa is NULL, the driver has a buggy/broken implementation. What would be a proper way of dealing with this? BUG?

Thank you for reviewing!

I would like to add WARN_ON_ONCE. because code writers can get opportunity for debugging this in runtime. also I think this bug doesn't make critical side effect.

Thanks!

Quoted 12 lines

Björn

Quoted 9 lines

Thanks!

Quoted 7 lines
Quoted 7 lines
net/core/xdp.c +0 -1
              rcu_read_unlock();
      default:
              /* Not possible, checked in xdp_rxq_info_reg_mem_model() */
--

2.9.3

#4 lore raw
In reply to Björn Töpel
Quoted 28 lines · Björn Töpel

Den fre 20 juli 2018 kl 22:08 skrev Jakub Kicinski:

Quoted 27 lines · Martin KaFai Lau
Quoted 10 lines · Taehee Yoo
Quoted 9 lines

rhashtable_lookup() can return NULL. so that NULL pointer check routine should be added.

Fixes 02b55e5657c3 ("xdp: add MEM_TYPE_ZERO_COPY")
---
 net/core/xdp.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
Quoted 12 lines

net/core/xdp.c static void __xdp_return(void *data, struct xdp_mem_info *mem, bool napi_direct, · line 349

Quoted 11 lines
net/core/xdp.c +2 -1
index 9d1f220..1c12bc7 100644
@@ -345,7 +345,8 @@ static void __xdp_return(void *data, struct xdp_mem_info *mem, bool napi_direct,
            rcu_read_lock();
            /* mem->id is valid, checked in xdp_rxq_info_reg_mem_model() */
            xa = rhashtable_lookup(mem_id_ht, &mem->id, mem_id_rht_params);
-           xa->zc_alloc->free(xa->zc_alloc, handle);
+           if (xa)
+                   xa->zc_alloc->free(xa->zc_alloc, handle);  

hmm...It is not clear to me the "!xa" case don't have to be handled?

Actually I have a more fundamental question about this interface I've been meaning to ask.

Quoted 8 lines
Quoted 5 lines

IIUC free() can happen on any CPU at any time, when whatever device, socket or CPU this got redirected to completed the TX. IOW there may be multiple producers. Drivers would need to create spin lock a'la the a9744f7ca200 ("xsk: fix potential race in SKB TX completion code") fix?

Jakub, apologies for the slow response. I'm still in "holiday/hammock&beer mode", but will be back in a week. :-P

Ah, sorry to interrupt! :)

Quoted 15 lines

The idea with the xdp_return_* functions are that an xdp_buff and xdp_frame can have custom allocations schemes. The difference beween struct xdp_buff and struct xdp_frame is lifetime. The xdp_buff lifetime is within the napi context, whereas xdp_frame can have a lifetime longer/outside the napi context. E.g. for a XDP_REDIRECT scenario an xdp_buff is converted to a xdp_frame. The conversion is done in include/net/xdp.h:convert_to_xdp_frame.

Currently, the zero-copy MEM_TYPE_ZERO_COPY memtype can *only* be used for xdp_buff, meaning that the lifetime is constrained to a napi context. Further, given an xdp_buff with memtype MEM_TYPE_ZERO_COPY, doing XDP_REDIRECT to a target that is *not* an AF_XDP socket would mean converting the xdp_buff to an xdp_frame. The xdp_frame can then be free'd on any CPU.

Quoted 6 lines

Note that the xsk_rcv* functions is always called from an napi context, and therefore is using the xdp_return_buff calls.

To answer your question -- no, this fix is *not* needed, because the xdp_buff napi constrained, and the xdp_buff will only be free'd on one CPU.

Oh, thanks, I missed the check in convert_to_xdp_frame(), so the only frames which can come back via the free path are out of the error path in __xsk_rcv_zc()?

That path looks a little surprising too, isn't the expectation that if xdp_do_redirect() returns an error the driver retains the ownership of the buffer?

static int __xsk_rcv_zc(struct xdp_sock *xs, struct xdp_buff *xdp, u32 len)

{
	int err = xskq_produce_batch_desc(xs->rx, (u64)xdp->handle, len);

	if (err) {
		xdp_return_buff(xdp);
		xs->rx_dropped++;
	}

	return err;
}

This seems to call xdp_return_buff() *and* return an error.

Quoted 25 lines
Quoted 10 lines

We need some form of internal kernel circulation which would be MPSC. I'm currently hacking up the XSK code to tell me whether the frame was consumed by the correct XSK, and always clone the frame otherwise (claiming to be the "traditional" MEM_TYPE_PAGE_ORDER0).

I feel like I'm missing something about the code. Is redirect of ZC/UMEM frame outside the xsk not possible and the only returns we will see are from net/xdp/xsk.c? That would work, but I don't see such a check. Help would be appreciated.

Right now, this is the case (refer to the TODO in convert_to_xdp_frame), i.e. you cannot redirect an ZC/UMEM allocated xdp_buff to a target that is not an xsk. This must, obviously, change so that an xdp_buff (of MEM_TYPE_ZERO_COPY) can be converted to an xdp_frame. The xdp_frame must be able to be free'd from multiple CPUs, so here the a more sophisticated allocation scheme is required.

Quoted 6 lines

Also the fact that XSK bufs can't be freed, only completed, adds to the pain of implementing AF_XDP, we'd certainly need some form of "give back the frame, but I may need it later" SPSC mechanism, otherwise driver writers will have tough time. Unless, again, I'm missing something about the code :)

Quoted 2 lines

Yup, moving the recycling scheme from driver to "generic" is a good idea! I need to finish up those i40e zerocopy patches first though...

Interesting, FWIW I wasn't necessarily thinking about full recycling, although that would be the holy grail. Just a generic way of giving up buffers for example when user changes ring sizes or brings the device down.

Quoted 1 line

(...and I'm very excited that you're doing nfp support for AF_XDP!!!)

Thanks, I'm still way out in the weeds but it's interesting work :)

#5 lore raw

Den mån 23 juli 2018 kl 21:58 skrev Jakub Kicinski <[email protected]>:

Quoted 36 lines · Björn Töpel
Quoted 34 lines

Den fre 20 juli 2018 kl 22:08 skrev Jakub Kicinski:

Quoted 32 lines · Martin KaFai Lau
Quoted 10 lines · Taehee Yoo
Quoted 9 lines

rhashtable_lookup() can return NULL. so that NULL pointer check routine should be added.

Fixes 02b55e5657c3 ("xdp: add MEM_TYPE_ZERO_COPY")
---
 net/core/xdp.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
Quoted 12 lines

net/core/xdp.c static void __xdp_return(void *data, struct xdp_mem_info *mem, bool napi_direct, · line 349

Quoted 11 lines
net/core/xdp.c +2 -1
index 9d1f220..1c12bc7 100644
@@ -345,7 +345,8 @@ static void __xdp_return(void *data, struct xdp_mem_info *mem, bool napi_direct,
            rcu_read_lock();
            /* mem->id is valid, checked in xdp_rxq_info_reg_mem_model() */
            xa = rhashtable_lookup(mem_id_ht, &mem->id, mem_id_rht_params);
-           xa->zc_alloc->free(xa->zc_alloc, handle);
+           if (xa)
+                   xa->zc_alloc->free(xa->zc_alloc, handle);

hmm...It is not clear to me the "!xa" case don't have to be handled?

Actually I have a more fundamental question about this interface I've been meaning to ask.

IIUC free() can happen on any CPU at any time, when whatever device, socket or CPU this got redirected to completed the TX. IOW there may be multiple producers. Drivers would need to create spin lock a'la the a9744f7ca200 ("xsk: fix potential race in SKB TX completion code") fix?

Quoted 5 lines
Quoted 2 lines

Jakub, apologies for the slow response. I'm still in "holiday/hammock&beer mode", but will be back in a week. :-P

Ah, sorry to interrupt! :)

Don't make it a habit! ;-P

Quoted 42 lines
Quoted 15 lines

The idea with the xdp_return_* functions are that an xdp_buff and xdp_frame can have custom allocations schemes. The difference beween struct xdp_buff and struct xdp_frame is lifetime. The xdp_buff lifetime is within the napi context, whereas xdp_frame can have a lifetime longer/outside the napi context. E.g. for a XDP_REDIRECT scenario an xdp_buff is converted to a xdp_frame. The conversion is done in include/net/xdp.h:convert_to_xdp_frame.

Currently, the zero-copy MEM_TYPE_ZERO_COPY memtype can *only* be used for xdp_buff, meaning that the lifetime is constrained to a napi context. Further, given an xdp_buff with memtype MEM_TYPE_ZERO_COPY, doing XDP_REDIRECT to a target that is *not* an AF_XDP socket would mean converting the xdp_buff to an xdp_frame. The xdp_frame can then be free'd on any CPU.

Quoted 6 lines

Note that the xsk_rcv* functions is always called from an napi context, and therefore is using the xdp_return_buff calls.

To answer your question -- no, this fix is *not* needed, because the xdp_buff napi constrained, and the xdp_buff will only be free'd on one CPU.

Oh, thanks, I missed the check in convert_to_xdp_frame(), so the only frames which can come back via the free path are out of the error path in __xsk_rcv_zc()?

That path looks a little surprising too, isn't the expectation that if xdp_do_redirect() returns an error the driver retains the ownership of the buffer?

static int __xsk_rcv_zc(struct xdp_sock *xs, struct xdp_buff *xdp, u32 len)

{
        int err = xskq_produce_batch_desc(xs->rx, (u64)xdp->handle, len);

        if (err) {
                xdp_return_buff(xdp);
                xs->rx_dropped++;
        }

        return err;
}
Quoted 2 lines

This seems to call xdp_return_buff() *and* return an error.

Ugh, this is indeed an error. The xdp_return buff should be removed. Thanks for spotting this!

So, yes, the way to get the buffer back (in ZC) to the driver is via the error path (recycling) or via the UMEM fill queue.

Quoted 36 lines
Quoted 25 lines
Quoted 10 lines

We need some form of internal kernel circulation which would be MPSC. I'm currently hacking up the XSK code to tell me whether the frame was consumed by the correct XSK, and always clone the frame otherwise (claiming to be the "traditional" MEM_TYPE_PAGE_ORDER0).

I feel like I'm missing something about the code. Is redirect of ZC/UMEM frame outside the xsk not possible and the only returns we will see are from net/xdp/xsk.c? That would work, but I don't see such a check. Help would be appreciated.

Right now, this is the case (refer to the TODO in convert_to_xdp_frame), i.e. you cannot redirect an ZC/UMEM allocated xdp_buff to a target that is not an xsk. This must, obviously, change so that an xdp_buff (of MEM_TYPE_ZERO_COPY) can be converted to an xdp_frame. The xdp_frame must be able to be free'd from multiple CPUs, so here the a more sophisticated allocation scheme is required.

Quoted 6 lines

Also the fact that XSK bufs can't be freed, only completed, adds to the pain of implementing AF_XDP, we'd certainly need some form of "give back the frame, but I may need it later" SPSC mechanism, otherwise driver writers will have tough time. Unless, again, I'm missing something about the code :)

Quoted 2 lines

Yup, moving the recycling scheme from driver to "generic" is a good idea! I need to finish up those i40e zerocopy patches first though...

Interesting, FWIW I wasn't necessarily thinking about full recycling, although that would be the holy grail. Just a generic way of giving up buffers for example when user changes ring sizes or brings the device down.

Quoted 1 line

(...and I'm very excited that you're doing nfp support for AF_XDP!!!)

Thanks, I'm still way out in the weeds but it's interesting work :)

#6 lore raw
Quoted 36 lines · Björn Töpel

Den mån 23 juli 2018 kl 21:58 skrev Jakub Kicinski:

Quoted 35 lines · Björn Töpel
Quoted 34 lines

Den fre 20 juli 2018 kl 22:08 skrev Jakub Kicinski:

Quoted 32 lines · Martin KaFai Lau
Quoted 10 lines · Taehee Yoo
Quoted 9 lines

rhashtable_lookup() can return NULL. so that NULL pointer check routine should be added.

Fixes 02b55e5657c3 ("xdp: add MEM_TYPE_ZERO_COPY")
---
 net/core/xdp.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
Quoted 12 lines

net/core/xdp.c static void __xdp_return(void *data, struct xdp_mem_info *mem, bool napi_direct, · line 349

Quoted 11 lines
net/core/xdp.c +2 -1
index 9d1f220..1c12bc7 100644
@@ -345,7 +345,8 @@ static void __xdp_return(void *data, struct xdp_mem_info *mem, bool napi_direct,
            rcu_read_lock();
            /* mem->id is valid, checked in xdp_rxq_info_reg_mem_model() */
            xa = rhashtable_lookup(mem_id_ht, &mem->id, mem_id_rht_params);
-           xa->zc_alloc->free(xa->zc_alloc, handle);
+           if (xa)
+                   xa->zc_alloc->free(xa->zc_alloc, handle);  

hmm...It is not clear to me the "!xa" case don't have to be handled?

Actually I have a more fundamental question about this interface I've been meaning to ask.

IIUC free() can happen on any CPU at any time, when whatever device, socket or CPU this got redirected to completed the TX. IOW there may be multiple producers. Drivers would need to create spin lock a'la the a9744f7ca200 ("xsk: fix potential race in SKB TX completion code") fix?

Quoted 7 lines
Quoted 5 lines
Quoted 2 lines

Jakub, apologies for the slow response. I'm still in "holiday/hammock&beer mode", but will be back in a week. :-P

Ah, sorry to interrupt! :)

Don't make it a habit! ;-P

:-D

Quoted 42 lines
Quoted 42 lines
Quoted 15 lines

The idea with the xdp_return_* functions are that an xdp_buff and xdp_frame can have custom allocations schemes. The difference beween struct xdp_buff and struct xdp_frame is lifetime. The xdp_buff lifetime is within the napi context, whereas xdp_frame can have a lifetime longer/outside the napi context. E.g. for a XDP_REDIRECT scenario an xdp_buff is converted to a xdp_frame. The conversion is done in include/net/xdp.h:convert_to_xdp_frame.

Currently, the zero-copy MEM_TYPE_ZERO_COPY memtype can *only* be used for xdp_buff, meaning that the lifetime is constrained to a napi context. Further, given an xdp_buff with memtype MEM_TYPE_ZERO_COPY, doing XDP_REDIRECT to a target that is *not* an AF_XDP socket would mean converting the xdp_buff to an xdp_frame. The xdp_frame can then be free'd on any CPU.

Quoted 6 lines

Note that the xsk_rcv* functions is always called from an napi context, and therefore is using the xdp_return_buff calls.

To answer your question -- no, this fix is *not* needed, because the xdp_buff napi constrained, and the xdp_buff will only be free'd on one CPU.

Oh, thanks, I missed the check in convert_to_xdp_frame(), so the only frames which can come back via the free path are out of the error path in __xsk_rcv_zc()?

That path looks a little surprising too, isn't the expectation that if xdp_do_redirect() returns an error the driver retains the ownership of the buffer?

static int __xsk_rcv_zc(struct xdp_sock *xs, struct xdp_buff *xdp, u32 len)

{
        int err = xskq_produce_batch_desc(xs->rx, (u64)xdp->handle, len);

        if (err) {
                xdp_return_buff(xdp);
                xs->rx_dropped++;
        }

        return err;
}
Quoted 8 lines
Quoted 2 lines

This seems to call xdp_return_buff() *and* return an error.

Ugh, this is indeed an error. The xdp_return buff should be removed. Thanks for spotting this!

So, yes, the way to get the buffer back (in ZC) to the driver is via the error path (recycling) or via the UMEM fill queue.

Okay, I'm gonna test and submit this later today for bpf tree:

net/xdp/xsk.c +1 -3
index 72335c2e8108..4e937cd7c17d 100644
@@ -84,10 +84,8 @@ static int __xsk_rcv_zc(struct xdp_sock *xs, struct xdp_buff *xdp, u32 len)
 {
        int err = xskq_produce_batch_desc(xs->rx, (u64)xdp->handle, len);
 
-       if (err) {
-               xdp_return_buff(xdp);
+       if (err)
                xs->rx_dropped++;
-       }
 
        return err;
 }

Now the frame return/ZC allocator ->free path is all dead code :S

#10 lore raw
In reply to Björn Töpel
Quoted 22 lines · On Mon, 23 Jul 2018 11:41:02 +0200 Björn Töpel <[email protected]> wrote:
Quoted 22 lines
Quoted 4 lines
Quoted 4 lines
net/core/xdp.c +0 -0
index 9d1f220..1c12bc7 100644
Quoted 8 lines

net/core/xdp.c static void __xdp_return(void *data, struct xdp_mem_info *mem, bool napi_direct, · line 349

Quoted 7 lines
net/core/xdp.c +2 -1
@@ -345,7 +345,8 @@ static void __xdp_return(void *data, struct xdp_mem_info *mem, bool napi_direct,
              rcu_read_lock();
              /* mem->id is valid, checked in xdp_rxq_info_reg_mem_model() */
              xa = rhashtable_lookup(mem_id_ht, &mem->id, mem_id_rht_params);
-             xa->zc_alloc->free(xa->zc_alloc, handle);
+             if (xa)
+                     xa->zc_alloc->free(xa->zc_alloc, handle);  

hmm...It is not clear to me the "!xa" case don't have to be handled?

Thank you for reviewing!

Returning NULL pointer is bug case such as calling after use xdp_rxq_info_unreg().
so that, I think it can't handle at that moment. we can make __xdp_return to add WARN_ON_ONCE() or add return error code to driver.
But I'm not sure if these is useful information.

Quoted 8 lines
Quoted 3 lines

I might have misunderstood scenario of MEM_TYPE_ZERO_COPY because there is no use case of MEM_TYPE_ZERO_COPY yet.

Taehee, again, sorry for the slow response and thanks for patch!

If xa is NULL, the driver has a buggy/broken implementation. What would be a proper way of dealing with this? BUG?

Hmm... I don't like these kind of changes to the hot-path code!

You might not realize this, but adding BUG() and WARN_ON() to the code affect performance in ways you might not realize! These macros gets compiled and uses an asm instruction called "ud2". Seeing the "ud2" instruction causes the CPUs instruction cache prefetcher to stop. Thus, if some code ends up below this instruction, this will cause more i-cache-misses.

I don't know if xa==NULL is even possible, but if it is, then I think this is a result of a driver mem_reg API usage bug. And the mem-reg API is full of WARN's and error messages, exactly to push these kind of checks out of the fast-path. There is no need for a BUG() call, as deref a NULL pointer will case an OOPS, that is easy to read and understand.

Signature · 4 lines
Best regards,
  Jesper Dangaard Brouer
  MSc.CS, Principal Kernel Engineer at Red Hat
  LinkedIn: http://www.linkedin.com/in/brouer
#11 lore raw

Den ons 1 aug. 2018 kl 16:14 skrev Jesper Dangaard Brouer <[email protected]>:

Quoted 44 lines
Quoted 22 lines · On Mon, 23 Jul 2018 11:41:02 +0200 Björn Töpel <[email protected]> wrote:
Quoted 22 lines
Quoted 4 lines
Quoted 4 lines
net/core/xdp.c +0 -0
index 9d1f220..1c12bc7 100644
Quoted 8 lines

net/core/xdp.c static void __xdp_return(void *data, struct xdp_mem_info *mem, bool napi_direct, · line 349

Quoted 7 lines
net/core/xdp.c +2 -1
@@ -345,7 +345,8 @@ static void __xdp_return(void *data, struct xdp_mem_info *mem, bool napi_direct,
              rcu_read_lock();
              /* mem->id is valid, checked in xdp_rxq_info_reg_mem_model() */
              xa = rhashtable_lookup(mem_id_ht, &mem->id, mem_id_rht_params);
-             xa->zc_alloc->free(xa->zc_alloc, handle);
+             if (xa)
+                     xa->zc_alloc->free(xa->zc_alloc, handle);

hmm...It is not clear to me the "!xa" case don't have to be handled?

Thank you for reviewing!

Returning NULL pointer is bug case such as calling after use xdp_rxq_info_unreg().
so that, I think it can't handle at that moment. we can make __xdp_return to add WARN_ON_ONCE() or add return error code to driver.
But I'm not sure if these is useful information.

Quoted 8 lines
Quoted 3 lines

I might have misunderstood scenario of MEM_TYPE_ZERO_COPY because there is no use case of MEM_TYPE_ZERO_COPY yet.

Taehee, again, sorry for the slow response and thanks for patch!

If xa is NULL, the driver has a buggy/broken implementation. What would be a proper way of dealing with this? BUG?

Hmm... I don't like these kind of changes to the hot-path code!

You might not realize this, but adding BUG() and WARN_ON() to the code affect performance in ways you might not realize! These macros gets compiled and uses an asm instruction called "ud2". Seeing the "ud2" instruction causes the CPUs instruction cache prefetcher to stop. Thus, if some code ends up below this instruction, this will cause more i-cache-misses.

Quoted 7 lines

I don't know if xa==NULL is even possible, but if it is, then I think this is a result of a driver mem_reg API usage bug. And the mem-reg API is full of WARN's and error messages, exactly to push these kind of checks out of the fast-path. There is no need for a BUG() call, as deref a NULL pointer will case an OOPS, that is easy to read and understand.

Jesper, thanks for having a look! So, you're right that if xa==NULL the driver is "broken/buggy" (as stated earlier!). I agree that OOPSing on a NULL pointer is as good as a BUG!

The applied patch adds a WARN_ON_ONCE, and I thought best practice was that a buggy driver shouldn't crash the kernel... What is considered best practices in these scenarios? *I'd* prefer an OOPS instead of WARN_ON_ONCE, to catch that buggy driver. Again, that's me. I thought that most people prefer not crashing, hence the patch. :-)

Björn

Quoted 5 lines

--
Best regards,
Jesper Dangaard Brouer
MSc.CS, Principal Kernel Engineer at Red Hat
LinkedIn: http://www.linkedin.com/in/brouer

maintainer
#12 lore raw
Quoted 54 lines · Björn Töpel

Den ons 1 aug. 2018 kl 16:14 skrev Jesper Dangaard Brouer <[email protected]>:

Quoted 42 lines
Quoted 22 lines · On Mon, 23 Jul 2018 11:41:02 +0200 Björn Töpel <[email protected]> wrote:
Quoted 22 lines
Quoted 4 lines
Quoted 4 lines
net/core/xdp.c +0 -0
index 9d1f220..1c12bc7 100644
Quoted 8 lines

net/core/xdp.c static void __xdp_return(void *data, struct xdp_mem_info *mem, bool napi_direct, · line 349

Quoted 7 lines
net/core/xdp.c +2 -1
@@ -345,7 +345,8 @@ static void __xdp_return(void *data, struct xdp_mem_info *mem, bool napi_direct,
              rcu_read_lock();
              /* mem->id is valid, checked in xdp_rxq_info_reg_mem_model() */
              xa = rhashtable_lookup(mem_id_ht, &mem->id, mem_id_rht_params);
-             xa->zc_alloc->free(xa->zc_alloc, handle);
+             if (xa)
+                     xa->zc_alloc->free(xa->zc_alloc, handle);

hmm...It is not clear to me the "!xa" case don't have to be handled?

Thank you for reviewing!

Returning NULL pointer is bug case such as calling after use xdp_rxq_info_unreg().
so that, I think it can't handle at that moment. we can make __xdp_return to add WARN_ON_ONCE() or add return error code to driver.
But I'm not sure if these is useful information.

Quoted 7 lines
Quoted 2 lines

I might have misunderstood scenario of MEM_TYPE_ZERO_COPY because there is no use case of MEM_TYPE_ZERO_COPY yet.

Taehee, again, sorry for the slow response and thanks for patch!

If xa is NULL, the driver has a buggy/broken implementation. What would be a proper way of dealing with this? BUG?

Hmm... I don't like these kind of changes to the hot-path code!

You might not realize this, but adding BUG() and WARN_ON() to the code affect performance in ways you might not realize! These macros gets compiled and uses an asm instruction called "ud2". Seeing the "ud2" instruction causes the CPUs instruction cache prefetcher to stop. Thus, if some code ends up below this instruction, this will cause more i-cache-misses.

Quoted 6 lines

I don't know if xa==NULL is even possible, but if it is, then I think this is a result of a driver mem_reg API usage bug. And the mem-reg API is full of WARN's and error messages, exactly to push these kind of checks out of the fast-path. There is no need for a BUG() call, as deref a NULL pointer will case an OOPS, that is easy to read and understand.

Jesper, thanks for having a look! So, you're right that if xa==NULL the driver is "broken/buggy" (as stated earlier!). I agree that OOPSing on a NULL pointer is as good as a BUG!

Quoted 5 lines

The applied patch adds a WARN_ON_ONCE, and I thought best practice was that a buggy driver shouldn't crash the kernel... What is considered best practices in these scenarios? *I'd* prefer an OOPS instead of WARN_ON_ONCE, to catch that buggy driver. Again, that's me. I thought that most people prefer not crashing, hence the patch. :-)

In that case, lets send a revert for the patch with a proper analysis of why it is safe to omit the NULL check which should be placed as a comment right near the rhashtable_lookup().

Thanks,
Daniel

#13 lore raw

Den ons 1 aug. 2018 kl 22:25 skrev Daniel Borkmann <[email protected]>:

Quoted 62 lines · Björn Töpel
Quoted 54 lines

Den ons 1 aug. 2018 kl 16:14 skrev Jesper Dangaard Brouer <[email protected]>:

Quoted 42 lines
Quoted 22 lines · On Mon, 23 Jul 2018 11:41:02 +0200 Björn Töpel <[email protected]> wrote:
Quoted 22 lines
Quoted 4 lines
Quoted 4 lines
net/core/xdp.c +0 -0
index 9d1f220..1c12bc7 100644
Quoted 8 lines

net/core/xdp.c static void __xdp_return(void *data, struct xdp_mem_info *mem, bool napi_direct, · line 349

Quoted 7 lines
net/core/xdp.c +2 -1
@@ -345,7 +345,8 @@ static void __xdp_return(void *data, struct xdp_mem_info *mem, bool napi_direct,
              rcu_read_lock();
              /* mem->id is valid, checked in xdp_rxq_info_reg_mem_model() */
              xa = rhashtable_lookup(mem_id_ht, &mem->id, mem_id_rht_params);
-             xa->zc_alloc->free(xa->zc_alloc, handle);
+             if (xa)
+                     xa->zc_alloc->free(xa->zc_alloc, handle);

hmm...It is not clear to me the "!xa" case don't have to be handled?

Thank you for reviewing!

Returning NULL pointer is bug case such as calling after use xdp_rxq_info_unreg().
so that, I think it can't handle at that moment. we can make __xdp_return to add WARN_ON_ONCE() or add return error code to driver.
But I'm not sure if these is useful information.

Quoted 7 lines
Quoted 2 lines

I might have misunderstood scenario of MEM_TYPE_ZERO_COPY because there is no use case of MEM_TYPE_ZERO_COPY yet.

Taehee, again, sorry for the slow response and thanks for patch!

If xa is NULL, the driver has a buggy/broken implementation. What would be a proper way of dealing with this? BUG?

Hmm... I don't like these kind of changes to the hot-path code!

You might not realize this, but adding BUG() and WARN_ON() to the code affect performance in ways you might not realize! These macros gets compiled and uses an asm instruction called "ud2". Seeing the "ud2" instruction causes the CPUs instruction cache prefetcher to stop. Thus, if some code ends up below this instruction, this will cause more i-cache-misses.

Quoted 6 lines

I don't know if xa==NULL is even possible, but if it is, then I think this is a result of a driver mem_reg API usage bug. And the mem-reg API is full of WARN's and error messages, exactly to push these kind of checks out of the fast-path. There is no need for a BUG() call, as deref a NULL pointer will case an OOPS, that is easy to read and understand.

Jesper, thanks for having a look! So, you're right that if xa==NULL the driver is "broken/buggy" (as stated earlier!). I agree that OOPSing on a NULL pointer is as good as a BUG!

Quoted 5 lines

The applied patch adds a WARN_ON_ONCE, and I thought best practice was that a buggy driver shouldn't crash the kernel... What is considered best practices in these scenarios? *I'd* prefer an OOPS instead of WARN_ON_ONCE, to catch that buggy driver. Again, that's me. I thought that most people prefer not crashing, hence the patch. :-)

Quoted 4 lines

In that case, lets send a revert for the patch with a proper analysis of why it is safe to omit the NULL check which should be placed as a comment right near the rhashtable_lookup().

I'll do that (as soon as I've double-checked so that I'm not lying)!

Björn

Quoted 2 lines

Thanks,
Daniel