From: Sarah Newman <hidden> Date: 2018-03-05 04:29:32
Take an additional reference to a page whenever it is placed
into the rx ring and put the page again after running
dma_unmap_page.
When swiotlb is in use, calling dma_unmap_page means that
the original page mapped with dma_map_page must still be valid,
as swiotlb will copy data from its internal cache back to the
originally requested DMA location.
When GRO is enabled, before this patch all references to the
original frag may be put and the page freed before dma_unmap_page
in mlx4_en_free_frag is called.
It is possible there is a path where the use-after-free occurs
even with GRO disabled, but this has not been observed so far.
The bug can be trivially detected by doing the following:
* Compile the kernel with DEBUG_PAGEALLOC
* Run the kernel as a Xen Dom0
* Leave GRO enabled on the interface
* Run a 10 second or more test with iperf over the interface.
This bug was likely introduced in
commit 4cce66cdd14a ("mlx4_en: map entire pages to increase throughput"),
first part of u3.6.
It was incidentally fixed in
commit 34db548bfb95 ("mlx4: add page recycling in receive path"),
first part of v4.12.
This version applies to the v4.9 series.
Signed-off-by: Sarah Newman <redacted>
Tested-by: Sarah Newman <redacted>
---
drivers/net/ethernet/mellanox/mlx4/en_rx.c | 39 +++++++++++++++++++++-------
drivers/net/ethernet/mellanox/mlx4/en_tx.c | 3 ++-
drivers/net/ethernet/mellanox/mlx4/mlx4_en.h | 1 +
3 files changed, 32 insertions(+), 11 deletions(-)
@@ -80,10 +80,14 @@ static int mlx4_alloc_pages(struct mlx4_en_priv *priv,page_alloc->page=page;page_alloc->dma=dma;page_alloc->page_offset=0;+page_alloc->page_owner=true;/* Not doing get_page() for each frag is a big win*onasymetricworkloads.Notewecannotuseatomic_set().*/-page_ref_add(page,page_alloc->page_size/frag_info->frag_stride-1);+/* Since the page must be valid until after dma_unmap_page is called,+*takeanadditionalreferencewewouldnothaveotherwise.+*/+page_ref_add(page,page_alloc->page_size/frag_info->frag_stride);return0;}
@@ -105,9 +109,13 @@ static int mlx4_en_alloc_frags(struct mlx4_en_priv *priv,page_alloc[i].page_offset+=frag_info->frag_stride;if(page_alloc[i].page_offset+frag_info->frag_stride<=-ring_alloc[i].page_size)-continue;-+ring_alloc[i].page_size){+WARN_ON(!page_alloc[i].page);+WARN_ON(!page_alloc[i].page_owner);+if(likely(page_alloc[i].page&&+page_alloc[i].page_owner))+continue;+}if(unlikely(mlx4_alloc_pages(priv,&page_alloc[i],frag_info,gfp)))gotoout;
@@ -131,7 +139,7 @@ static int mlx4_en_alloc_frags(struct mlx4_en_priv *priv,page=page_alloc[i].page;/* Revert changes done by mlx4_alloc_pages */page_ref_sub(page,page_alloc[i].page_size/-priv->frag_info[i].frag_stride-1);+priv->frag_info[i].frag_stride);put_page(page);}}
@@ -595,7 +614,7 @@ static int mlx4_en_complete_rx_desc(struct mlx4_en_priv *priv,frag_info=&priv->frag_info[nr];if(length<=frag_info->frag_prefix_size)break;-if(unlikely(!frags[nr].page))+if(unlikely(!frags[nr].page_owner))gotofail;dma=be64_to_cpu(rx_desc->data[nr].addr);
@@ -607,7 +626,7 @@ static int mlx4_en_complete_rx_desc(struct mlx4_en_priv *priv,skb_frag_size_set(&skb_frags_rx[nr],frag_info->frag_size);skb_frags_rx[nr].page_offset=frags[nr].page_offset;skb->truesize+=frag_info->frag_stride;-frags[nr].page=NULL;+frags[nr].page_owner=false;}/* Adjust size of last fragment to match actual length */if(nr>0)
Take an additional reference to a page whenever it is placed
into the rx ring and put the page again after running
dma_unmap_page.
When swiotlb is in use, calling dma_unmap_page means that
the original page mapped with dma_map_page must still be valid,
as swiotlb will copy data from its internal cache back to the
originally requested DMA location.
When GRO is enabled, before this patch all references to the
original frag may be put and the page freed before dma_unmap_page
in mlx4_en_free_frag is called.
It is possible there is a path where the use-after-free occurs
even with GRO disabled, but this has not been observed so far.
The bug can be trivially detected by doing the following:
* Compile the kernel with DEBUG_PAGEALLOC
* Run the kernel as a Xen Dom0
* Leave GRO enabled on the interface
* Run a 10 second or more test with iperf over the interface.
Hi Sarah, thanks for your patch!
quoted hunk
This bug was likely introduced in
commit 4cce66cdd14a ("mlx4_en: map entire pages to increase throughput"),
first part of u3.6.
It was incidentally fixed in
commit 34db548bfb95 ("mlx4: add page recycling in receive path"),
first part of v4.12.
This version applies to the v4.9 series.
Signed-off-by: Sarah Newman <redacted>
Tested-by: Sarah Newman <redacted>
---
drivers/net/ethernet/mellanox/mlx4/en_rx.c | 39 +++++++++++++++++++++-------
drivers/net/ethernet/mellanox/mlx4/en_tx.c | 3 ++-
drivers/net/ethernet/mellanox/mlx4/mlx4_en.h | 1 +
3 files changed, 32 insertions(+), 11 deletions(-)
@@ -80,10 +80,14 @@ static int mlx4_alloc_pages(struct mlx4_en_priv *priv,page_alloc->page=page;page_alloc->dma=dma;page_alloc->page_offset=0;+page_alloc->page_owner=true;
Do we really need this boolean? I believe the issue can be fixed without it. We need to make sure we hold the correct refcnt at every stage, and maintain symmetry between a flow and its inverse.
Upon alloc, refcnt is 1. This alloc refcnt should be inverted by a call to put_page. We might want to introduce a page free API (symmetric to mlx4_alloc_pages), that does: dma unmap the page, call put_page, nullify pointer.
Once alloced, page refcnt is bumped up by the amount of possible frags populating it, which is (page_size / frag_stride), as you do here.
quoted hunk
/* Not doing get_page() for each frag is a big win * on asymetric workloads. Note we can not use atomic_set(). */- page_ref_add(page, page_alloc->page_size / frag_info->frag_stride - 1);+ /* Since the page must be valid until after dma_unmap_page is called,+ * take an additional reference we would not have otherwise.+ */+ page_ref_add(page, page_alloc->page_size / frag_info->frag_stride); return 0; }
@@ -595,7 +614,7 @@ static int mlx4_en_complete_rx_desc(struct mlx4_en_priv *priv, frag_info = &priv->frag_info[nr]; if (length <= frag_info->frag_prefix_size) break;- if (unlikely(!frags[nr].page))+ if (unlikely(!frags[nr].page_owner)) goto fail; dma = be64_to_cpu(rx_desc->data[nr].addr);
@@ -607,7 +626,7 @@ static int mlx4_en_complete_rx_desc(struct mlx4_en_priv *priv, skb_frag_size_set(&skb_frags_rx[nr], frag_info->frag_size); skb_frags_rx[nr].page_offset = frags[nr].page_offset; skb->truesize += frag_info->frag_stride;- frags[nr].page = NULL;+ frags[nr].page_owner = false; } /* Adjust size of last fragment to match actual length */ if (nr > 0)
From: Sarah Newman <hidden> Date: 2018-03-05 21:10:37
On 03/05/2018 02:09 AM, Tariq Toukan wrote:
On 05/03/2018 6:20 AM, Sarah Newman wrote:
quoted
Take an additional reference to a page whenever it is placed
into the rx ring and put the page again after running
dma_unmap_page.
When swiotlb is in use, calling dma_unmap_page means that
the original page mapped with dma_map_page must still be valid,
as swiotlb will copy data from its internal cache back to the
originally requested DMA location.
When GRO is enabled, before this patch all references to the
original frag may be put and the page freed before dma_unmap_page
in mlx4_en_free_frag is called.
It is possible there is a path where the use-after-free occurs
even with GRO disabled, but this has not been observed so far.
The bug can be trivially detected by doing the following:
* Compile the kernel with DEBUG_PAGEALLOC
* Run the kernel as a Xen Dom0
* Leave GRO enabled on the interface
* Run a 10 second or more test with iperf over the interface.
Hi Sarah, thanks for your patch!
quoted
This bug was likely introduced in
commit 4cce66cdd14a ("mlx4_en: map entire pages to increase throughput"),
first part of u3.6.
It was incidentally fixed in
commit 34db548bfb95 ("mlx4: add page recycling in receive path"),
first part of v4.12.
This version applies to the v4.9 series.
Signed-off-by: Sarah Newman <redacted>
Tested-by: Sarah Newman <redacted>
---
drivers/net/ethernet/mellanox/mlx4/en_rx.c | 39 +++++++++++++++++++++-------
drivers/net/ethernet/mellanox/mlx4/en_tx.c | 3 ++-
drivers/net/ethernet/mellanox/mlx4/mlx4_en.h | 1 +
3 files changed, 32 insertions(+), 11 deletions(-)
Do we really need this boolean? I believe the issue can be fixed without it. We need to make sure we hold the correct refcnt at every stage, and
maintain symmetry between a flow and its inverse.
The reason this was added is because the page address needs to stay around until after dma unmap_page is called, and right now setting page to NULL is
used to indicate that put_page should not be called when frags are freed in mlx4_en_free_frag. So either the code needs to be rearranged so that
dma_unmap_page while page is still set, or some variable needed to be used to indicate whether put_page should be called when the frags are freed.
If dma_unmap_page was called before page was set to NULL, then this variable doesn't need to be added, yes. Then the call to dma_unmap_page in
mlx4_en_free_frag would also be contingent on frags[i].page being set.
There are two places where page is set to NULL without calling dma_unmap_page first, mlx4_en_complete_rx_desc and mlx4_en_xmit_frame.
Is mlx4_en_complete_rx_desc the only place where a call to dma_unmap_page would need to be added? The other place page is set to NULL without a call
to dma_unmap_page first is in mlx4_en_xmit_frame, and I believe there is no call to mlx4_en_free_frag if mlx4_en_xmit_frame executes.
Upon alloc, refcnt is 1. This alloc refcnt should be inverted by a call to put_page. We might want to introduce a page free API (symmetric to
mlx4_alloc_pages), that does: dma unmap the page, call put_page, nullify pointer.
That seems reasonable.
Once alloced, page refcnt is bumped up by the amount of possible frags populating it, which is (page_size / frag_stride), as you do here.
quoted
/* Not doing get_page() for each frag is a big win
* on asymetric workloads. Note we can not use atomic_set().
*/
- page_ref_add(page, page_alloc->page_size / frag_info->frag_stride - 1);
+ /* Since the page must be valid until after dma_unmap_page is called,
+ * take an additional reference we would not have otherwise.
+ */
+ page_ref_add(page, page_alloc->page_size / frag_info->frag_stride);
return 0;
}
@@ -105,9 +109,13 @@ static int mlx4_en_alloc_frags(struct mlx4_en_priv *priv,
page_alloc[i].page_offset += frag_info->frag_stride;
if (page_alloc[i].page_offset + frag_info->frag_stride <=
- ring_alloc[i].page_size)
- continue;
-
+ ring_alloc[i].page_size) {
+ WARN_ON(!page_alloc[i].page);
+ WARN_ON(!page_alloc[i].page_owner);
Why WARN before the likely() check?
Move after the check, for a better performance.
No particular reason.
quoted
+ if (likely(page_alloc[i].page &&
+ page_alloc[i].page_owner))
+ continue;
+ }
if (unlikely(mlx4_alloc_pages(priv, &page_alloc[i],
frag_info, gfp)))
goto out;
@@ -131,7 +139,7 @@ static int mlx4_en_alloc_frags(struct mlx4_en_priv *priv,
Why? If I'm not mistaken, the page is cached with refcnt == 2. No?
In mlx4_en_deactivate_rx_ring, pages assigned to frames in the page_cache are only put once. If refcnt == 2 when it's inserted, isn't that a memory
leak? I can confirm one way or another if you haven't already.
@@ -595,7 +614,7 @@ static int mlx4_en_complete_rx_desc(struct mlx4_en_priv *priv,
frag_info = &priv->frag_info[nr];
if (length <= frag_info->frag_prefix_size)
break;
- if (unlikely(!frags[nr].page))
+ if (unlikely(!frags[nr].page_owner))
goto fail;
dma = be64_to_cpu(rx_desc->data[nr].addr);
@@ -607,7 +626,7 @@ static int mlx4_en_complete_rx_desc(struct mlx4_en_priv *priv,
skb_frag_size_set(&skb_frags_rx[nr], frag_info->frag_size);
skb_frags_rx[nr].page_offset = frags[nr].page_offset;
skb->truesize += frag_info->frag_stride;
- frags[nr].page = NULL;
+ frags[nr].page_owner = false;
}
/* Adjust size of last fragment to match actual length */
if (nr > 0)
Take an additional reference to a page whenever it is placed
into the rx ring and put the page again after running
dma_unmap_page.
When swiotlb is in use, calling dma_unmap_page means that
the original page mapped with dma_map_page must still be valid,
as swiotlb will copy data from its internal cache back to the
originally requested DMA location.
When GRO is enabled, before this patch all references to the
original frag may be put and the page freed before dma_unmap_page
in mlx4_en_free_frag is called.
It is possible there is a path where the use-after-free occurs
even with GRO disabled, but this has not been observed so far.
The bug can be trivially detected by doing the following:
* Compile the kernel with DEBUG_PAGEALLOC
* Run the kernel as a Xen Dom0
* Leave GRO enabled on the interface
* Run a 10 second or more test with iperf over the interface.
Hi Sarah, thanks for your patch!
quoted
This bug was likely introduced in
commit 4cce66cdd14a ("mlx4_en: map entire pages to increase throughput"),
first part of u3.6.
It was incidentally fixed in
commit 34db548bfb95 ("mlx4: add page recycling in receive path"),
first part of v4.12.
This version applies to the v4.9 series.
Signed-off-by: Sarah Newman <redacted>
Tested-by: Sarah Newman <redacted>
---
drivers/net/ethernet/mellanox/mlx4/en_rx.c | 39 +++++++++++++++++++++-------
drivers/net/ethernet/mellanox/mlx4/en_tx.c | 3 ++-
drivers/net/ethernet/mellanox/mlx4/mlx4_en.h | 1 +
3 files changed, 32 insertions(+), 11 deletions(-)
@@ -80,10 +80,14 @@ static int mlx4_alloc_pages(struct mlx4_en_priv *priv,page_alloc->page=page;page_alloc->dma=dma;page_alloc->page_offset=0;+page_alloc->page_owner=true;
Do we really need this boolean? I believe the issue can be fixed without it. We need to make sure we hold the correct refcnt at every stage, and
maintain symmetry between a flow and its inverse.
The reason this was added is because the page address needs to stay around until after dma unmap_page is called, and right now setting page to NULL is
used to indicate that put_page should not be called when frags are freed in mlx4_en_free_frag. So either the code needs to be rearranged so that
dma_unmap_page while page is still set, or some variable needed to be used to indicate whether put_page should be called when the frags are freed.
rearranging sounds better.
If dma_unmap_page was called before page was set to NULL, then this variable doesn't need to be added, yes. Then the call to dma_unmap_page in
mlx4_en_free_frag would also be contingent on frags[i].page being set.
There are two places where page is set to NULL without calling dma_unmap_page first, mlx4_en_complete_rx_desc and mlx4_en_xmit_frame.
In mlx4_en_xmit_frame, should not unmap, it should be done only upon completion, this is done in mlx4_en_recycle_tx_desc.
In mlx4_en_complete_rx_desc I think it was just a bug.
Is mlx4_en_complete_rx_desc the only place where a call to dma_unmap_page would need to be added? The other place page is set to NULL without a call
to dma_unmap_page first is in mlx4_en_xmit_frame, and I believe there is no call to mlx4_en_free_frag if mlx4_en_xmit_frame executes.
Yes, only in mlx4_en_complete_rx_desc, see above.
quoted
Upon alloc, refcnt is 1. This alloc refcnt should be inverted by a call to put_page. We might want to introduce a page free API (symmetric to
mlx4_alloc_pages), that does: dma unmap the page, call put_page, nullify pointer.
That seems reasonable.
Yes, let's use it in mlx4_en_free_frag.
quoted
Once alloced, page refcnt is bumped up by the amount of possible frags populating it, which is (page_size / frag_stride), as you do here.
quoted
/* Not doing get_page() for each frag is a big win * on asymetric workloads. Note we can not use atomic_set(). */- page_ref_add(page, page_alloc->page_size / frag_info->frag_stride - 1);+ /* Since the page must be valid until after dma_unmap_page is called,+ * take an additional reference we would not have otherwise.+ */+ page_ref_add(page, page_alloc->page_size / frag_info->frag_stride); return 0; } @@ -105,9 +109,13 @@ static int mlx4_en_alloc_frags(struct mlx4_en_priv *priv, page_alloc[i].page_offset += frag_info->frag_stride; if (page_alloc[i].page_offset + frag_info->frag_stride <=- ring_alloc[i].page_size)- continue;-+ ring_alloc[i].page_size) {+ WARN_ON(!page_alloc[i].page);+ WARN_ON(!page_alloc[i].page_owner);
Why WARN before the likely() check?
Move after the check, for a better performance.
No particular reason.
please move them.
quoted
quoted
+ if (likely(page_alloc[i].page &&+ page_alloc[i].page_owner))+ continue;+ } if (unlikely(mlx4_alloc_pages(priv, &page_alloc[i], frag_info, gfp))) goto out;
Why? If I'm not mistaken, the page is cached with refcnt == 2. No?
In mlx4_en_deactivate_rx_ring, pages assigned to frames in the page_cache are only put once. If refcnt == 2 when it's inserted, isn't that a memory
leak? I can confirm one way or another if you haven't already.
I think you're right. But I didn't check this yet.
@@ -595,7 +614,7 @@ static int mlx4_en_complete_rx_desc(struct mlx4_en_priv *priv, frag_info = &priv->frag_info[nr]; if (length <= frag_info->frag_prefix_size) break;- if (unlikely(!frags[nr].page))+ if (unlikely(!frags[nr].page_owner)) goto fail; dma = be64_to_cpu(rx_desc->data[nr].addr);
@@ -607,7 +626,7 @@ static int mlx4_en_complete_rx_desc(struct mlx4_en_priv *priv, skb_frag_size_set(&skb_frags_rx[nr], frag_info->frag_size); skb_frags_rx[nr].page_offset = frags[nr].page_offset; skb->truesize += frag_info->frag_stride;- frags[nr].page = NULL;+ frags[nr].page_owner = false; } /* Adjust size of last fragment to match actual length */ if (nr > 0)
I have a general question about the process.
I don't totally get what branch this patch is targeted to.
It touches critical areas in datapath and should go through regression tests before it is accepted to any branch.
Thanks,
Tariq
From: Sarah Newman <hidden> Date: 2018-03-06 20:16:54
On 03/06/2018 08:13 AM, Tariq Toukan wrote:
I have a general question about the process.
I don't totally get what branch this patch is targeted to.
It touches critical areas in datapath and should go through regression tests before it is accepted to any branch.
This one is against 4.9 -
https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux-stable.git/log/?h=linux-4.9.y
I assume you'd be interested in applying a fix to all currently maintained stable branches between 3.6 to 4.11, per
https://www.kernel.org/category/releases.html .
I know of at least one other person whose workaround has been to disable GRO, but I don't understand the networking code well enough to guarantee that
disabling GRO means the problem will never occur under all possible error conditions. If it's guaranteed that disabling GRO will definitely mitigate
the bug, probably it's better to stop supporting GRO for these versions of the driver instead of trying to implement some other fix.
--Sarah
I have a general question about the process.
I don't totally get what branch this patch is targeted to.
It touches critical areas in datapath and should go through regression tests before it is accepted to any branch.
I know of at least one other person whose workaround has been to disable GRO, but I don't understand the networking code well enough to guarantee that
disabling GRO means the problem will never occur under all possible error conditions. If it's guaranteed that disabling GRO will definitely mitigate
the bug, probably it's better to stop supporting GRO for these versions of the driver instead of trying to implement some other fix.
I don't think we have a guarantee, it just happens to work.
Also, disabling GRO will cause dramatic performance degradation.
We should fix this, not WA.
From: Sarah Newman <hidden> Date: 2018-04-26 04:08:35
When swiotlb is in use, calling dma_unmap_page means that
the original page mapped with dma_map_page must still be valid
as swiotlb will copy data from its internal cache back to the
originally requested DMA location. When GRO is enabled,
all references to the original frag may be put before
mlx4_en_free_frag is called, meaning the page has been freed
before the call to dma_unmap_page in mlx4_en_free_frag.
To fix, unmap the page as soon as possible.
This can be trivially detected by doing the following:
Compile the kernel with DEBUG_PAGEALLOC
Run the kernel as a Xen Dom0
Leave GRO enabled on the interface
Run a 10 second or more test with iperf over the interface.
Signed-off-by: Sarah Newman <redacted>
---
drivers/net/ethernet/mellanox/mlx4/en_rx.c | 32 +++++++++++++++++++-----------
1 file changed, 20 insertions(+), 12 deletions(-)
@@ -586,21 +587,28 @@ static int mlx4_en_complete_rx_desc(struct mlx4_en_priv *priv,intlength){structskb_frag_struct*skb_frags_rx=skb_shinfo(skb)->frags;-structmlx4_en_frag_info*frag_info;intnr;dma_addr_tdma;/* Collect used fragments while replacing them in the HW descriptors */for(nr=0;nr<priv->num_frags;nr++){-frag_info=&priv->frag_info[nr];+structmlx4_en_frag_info*frag_info=&priv->frag_info[nr];+u32next_frag_end=frags[nr].page_offset++2*frag_info->frag_stride;+if(length<=frag_info->frag_prefix_size)break;if(unlikely(!frags[nr].page))gotofail;dma=be64_to_cpu(rx_desc->data[nr].addr);-dma_sync_single_for_cpu(priv->ddev,dma,frag_info->frag_size,-DMA_FROM_DEVICE);+if(next_frag_end>frags[nr].page_size)+dma_unmap_page(priv->ddev,frags[nr].dma,+frags[nr].page_size,frag_info->dma_dir);+else+dma_sync_single_for_cpu(priv->ddev,dma,+frag_info->frag_size,+DMA_FROM_DEVICE);/* Save page reference in skb */__skb_frag_set_page(&skb_frags_rx[nr],frags[nr].page);
When swiotlb is in use, calling dma_unmap_page means that
the original page mapped with dma_map_page must still be valid
as swiotlb will copy data from its internal cache back to the
originally requested DMA location. When GRO is enabled,
all references to the original frag may be put before
mlx4_en_free_frag is called, meaning the page has been freed
before the call to dma_unmap_page in mlx4_en_free_frag.
To fix, unmap the page as soon as possible.
This can be trivially detected by doing the following:
Compile the kernel with DEBUG_PAGEALLOC
Run the kernel as a Xen Dom0
Leave GRO enabled on the interface
Run a 10 second or more test with iperf over the interface.
Signed-off-by: Sarah Newman <redacted>
Tariq, I assume I will get this from you in the next set of
changes you submit to me.
Thanks.
When swiotlb is in use, calling dma_unmap_page means that
the original page mapped with dma_map_page must still be valid
as swiotlb will copy data from its internal cache back to the
originally requested DMA location. When GRO is enabled,
all references to the original frag may be put before
mlx4_en_free_frag is called, meaning the page has been freed
before the call to dma_unmap_page in mlx4_en_free_frag.
To fix, unmap the page as soon as possible.
This can be trivially detected by doing the following:
Compile the kernel with DEBUG_PAGEALLOC
Run the kernel as a Xen Dom0
Leave GRO enabled on the interface
Run a 10 second or more test with iperf over the interface.
Signed-off-by: Sarah Newman <redacted>
Tariq, I assume I will get this from you in the next set of
changes you submit to me.
Thanks.
This patch fixes an issue existing in old kernels. It is not relevant per latest code.
So I'm not sure about the process. After I review it, do I just submit it again for -stable?
Thanks.
This patch fixes an issue existing in old kernels. It is not relevant
per latest code.
So I'm not sure about the process. After I review it, do I just submit
it again for -stable?
If that's the case, yes that is what you do, just submit it for -stable
and add a note to the top of the commit message body that says something
like:
[ Not relevant upstream, therefore no upstream commit. ]
As -stable submissions have to say what their upstream commit ID is
otherwise.
CC: me on the submission, thank you.
Thanks.