diff mbox series

[net-next,v6,1/8] octeontx2-pf: map skb data as device writeable

Message ID 20240819122348.490445-2-bbhushan2@marvell.com (mailing list archive)
State Superseded
Delegated to: Netdev Maintainers
Headers show
Series cn10k-ipsec: Add outbound inline ipsec support | expand

Checks

Context Check Description
netdev/series_format success Posting correctly formatted
netdev/tree_selection success Clearly marked for net-next, async
netdev/ynl success Generated files up to date; no warnings/errors; no diff in generated;
netdev/fixes_present success Fixes tag not required for -next series
netdev/header_inline success No static functions without inline keyword in header files
netdev/build_32bit success Errors and warnings before: 7 this patch: 7
netdev/build_tools success No tools touched, skip
netdev/cc_maintainers success CCed 8 of 8 maintainers
netdev/build_clang success Errors and warnings before: 16 this patch: 16
netdev/verify_signedoff success Signed-off-by tag matches author and committer
netdev/deprecated_api success None detected
netdev/check_selftest success No net selftest shell script
netdev/verify_fixes success No Fixes tag
netdev/build_allmodconfig_warn success Errors and warnings before: 19 this patch: 19
netdev/checkpatch success total: 0 errors, 0 warnings, 0 checks, 16 lines checked
netdev/build_clang_rust success No Rust files in patch. Skipping build
netdev/kdoc success Errors and warnings before: 0 this patch: 0
netdev/source_inline success Was 0 now: 0
netdev/contest success net-next-2024-08-20--09-00 (tests: 712)

Commit Message

Bharat Bhushan Aug. 19, 2024, 12:23 p.m. UTC
Crypto hardware need write permission for in-place encrypt
or decrypt operation on skb-data to support IPsec crypto
offload. So map this memory as device read-write.

Signed-off-by: Bharat Bhushan <bbhushan2@marvell.com>
---
 drivers/net/ethernet/marvell/octeontx2/nic/otx2_txrx.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

Comments

Jakub Kicinski Aug. 20, 2024, 10:35 p.m. UTC | #1
On Mon, 19 Aug 2024 17:53:41 +0530 Bharat Bhushan wrote:
> Crypto hardware need write permission for in-place encrypt
> or decrypt operation on skb-data to support IPsec crypto
> offload. So map this memory as device read-write.

How do you know the fragments are not read only?

(Whatever the answer is it should be part of the commit msg)
Bharat Bhushan Aug. 22, 2024, 3:45 a.m. UTC | #2
On Wed, Aug 21, 2024 at 4:06 AM Jakub Kicinski <kuba@kernel.org> wrote:
>
> On Mon, 19 Aug 2024 17:53:41 +0530 Bharat Bhushan wrote:
> > Crypto hardware need write permission for in-place encrypt
> > or decrypt operation on skb-data to support IPsec crypto
> > offload. So map this memory as device read-write.
>
> How do you know the fragments are not read only?

IOMMU permission faults will be reported if the DMA_TO_DEVICE direction flag
is used in dma_map_page_attrs(). This is because iommu creates read only mapping
if the DMA_TO_DEVICE direction flag is used.  If the direction flag used in
dma_map_pages() is DMA_BIDIRECTIONAL then iommu creates mapping with
both read and write permission.

>
> (Whatever the answer is it should be part of the commit msg)

Will update commit message to something like:

Crypto hardware needs both read and write permission for in-place
encryption/decryption operation. Iommu creates read only mapping
if the DMA_TO_DEVICE direction flag is used in dma_map_page_attrs().
Use DMA_BIDIRECTIONAL direction flag in dma_map_page_attrs(), so
that iommu mapping is created with both read and write permission.

Is that okay?

Thanks
-Bharat
Jakub Kicinski Aug. 22, 2024, 2:48 p.m. UTC | #3
On Thu, 22 Aug 2024 09:15:43 +0530 Bharat Bhushan wrote:
> On Wed, Aug 21, 2024 at 4:06 AM Jakub Kicinski <kuba@kernel.org> wrote:
> > On Mon, 19 Aug 2024 17:53:41 +0530 Bharat Bhushan wrote:  
> > > Crypto hardware need write permission for in-place encrypt
> > > or decrypt operation on skb-data to support IPsec crypto
> > > offload. So map this memory as device read-write.  
> >
> > How do you know the fragments are not read only?  
> 
> IOMMU permission faults will be reported if the DMA_TO_DEVICE direction flag
> is used in dma_map_page_attrs(). This is because iommu creates read only mapping
> if the DMA_TO_DEVICE direction flag is used.  If the direction flag used in
> dma_map_pages() is DMA_BIDIRECTIONAL then iommu creates mapping with
> both read and write permission.

The other way around, I understand that your code makes the pages
writable for the device. What I'm concerned about is that if this
code path is fed Tx skbs you will corrupt them. Are these not Tx
skbs that you're mapping? Have you fully CoW'd them to make sure
they are writable?
Bharat Bhushan Aug. 23, 2024, 11:25 a.m. UTC | #4
On Thu, Aug 22, 2024 at 8:18 PM Jakub Kicinski <kuba@kernel.org> wrote:
>
> On Thu, 22 Aug 2024 09:15:43 +0530 Bharat Bhushan wrote:
> > On Wed, Aug 21, 2024 at 4:06 AM Jakub Kicinski <kuba@kernel.org> wrote:
> > > On Mon, 19 Aug 2024 17:53:41 +0530 Bharat Bhushan wrote:
> > > > Crypto hardware need write permission for in-place encrypt
> > > > or decrypt operation on skb-data to support IPsec crypto
> > > > offload. So map this memory as device read-write.
> > >
> > > How do you know the fragments are not read only?
> >
> > IOMMU permission faults will be reported if the DMA_TO_DEVICE direction flag
> > is used in dma_map_page_attrs(). This is because iommu creates read only mapping
> > if the DMA_TO_DEVICE direction flag is used.  If the direction flag used in
> > dma_map_pages() is DMA_BIDIRECTIONAL then iommu creates mapping with
> > both read and write permission.
>
> The other way around, I understand that your code makes the pages
> writable for the device. What I'm concerned about is that if this
> code path is fed Tx skbs you will corrupt them. Are these not Tx
> skbs that you're mapping? Have you fully CoW'd them to make sure
> they are writable?

This code is mapping skb data for hardware to over-write plain-text with
cypher-text and update authentication data (in-place encap/auth).
This patch series doesn't take care of CoWing for skb data. Actually I was
not aware of that before your comment.

To understand your comment better, If the device writes to shared skb data
without CoWing then we have an issue. Is that correct?

I do not see any other driver supporting IPsec crypto offload ensuring
skb data CoWing,
but there is a possibility that those devices are not doing in-place
encap and auth (encap
and auth data written to separate buffer). I do not have clarity about
this, This skb is set for
IPSEC crypto offload, Is this the driver which has to ensure that the
skb is writeable or the
network stack (xfrm layer) will ensure the same. If it is former then
add code to call skb_unshare().
Please suggest.

Thanks
-Bharat
Bharat Bhushan Aug. 24, 2024, 1:59 p.m. UTC | #5
On Fri, Aug 23, 2024 at 4:55 PM Bharat Bhushan <bharatb.linux@gmail.com> wrote:
>
> On Thu, Aug 22, 2024 at 8:18 PM Jakub Kicinski <kuba@kernel.org> wrote:
> >
> > On Thu, 22 Aug 2024 09:15:43 +0530 Bharat Bhushan wrote:
> > > On Wed, Aug 21, 2024 at 4:06 AM Jakub Kicinski <kuba@kernel.org> wrote:
> > > > On Mon, 19 Aug 2024 17:53:41 +0530 Bharat Bhushan wrote:
> > > > > Crypto hardware need write permission for in-place encrypt
> > > > > or decrypt operation on skb-data to support IPsec crypto
> > > > > offload. So map this memory as device read-write.
> > > >
> > > > How do you know the fragments are not read only?
> > >
> > > IOMMU permission faults will be reported if the DMA_TO_DEVICE direction flag
> > > is used in dma_map_page_attrs(). This is because iommu creates read only mapping
> > > if the DMA_TO_DEVICE direction flag is used.  If the direction flag used in
> > > dma_map_pages() is DMA_BIDIRECTIONAL then iommu creates mapping with
> > > both read and write permission.
> >
> > The other way around, I understand that your code makes the pages
> > writable for the device. What I'm concerned about is that if this
> > code path is fed Tx skbs you will corrupt them. Are these not Tx
> > skbs that you're mapping? Have you fully CoW'd them to make sure
> > they are writable?
>
> This code is mapping skb data for hardware to over-write plain-text with
> cypher-text and update authentication data (in-place encap/auth).
> This patch series doesn't take care of CoWing for skb data. Actually I was
> not aware of that before your comment.
>
> To understand your comment better, If the device writes to shared skb data
> without CoWing then we have an issue. Is that correct?
>
> I do not see any other driver supporting IPsec crypto offload ensuring
> skb data CoWing,
> but there is a possibility that those devices are not doing in-place
> encap and auth (encap
> and auth data written to separate buffer). I do not have clarity about
> this, This skb is set for
> IPSEC crypto offload, Is this the driver which has to ensure that the
> skb is writeable or the
> network stack (xfrm layer) will ensure the same. If it is former then
> add code to call skb_unshare().
> Please suggest.

My understanding after further looking into the code is that it is the
driver responsibility to ensure skb is not in a shared state.

Thanks for pointing this out. Will change code in the next version.

Regards
-Bharat


>
> Thanks
> -Bharat
diff mbox series

Patch

diff --git a/drivers/net/ethernet/marvell/octeontx2/nic/otx2_txrx.c b/drivers/net/ethernet/marvell/octeontx2/nic/otx2_txrx.c
index 3eb85949677a..ce1a7db745c5 100644
--- a/drivers/net/ethernet/marvell/octeontx2/nic/otx2_txrx.c
+++ b/drivers/net/ethernet/marvell/octeontx2/nic/otx2_txrx.c
@@ -98,7 +98,7 @@  static dma_addr_t otx2_dma_map_skb_frag(struct otx2_nic *pfvf,
 		offset = skb_frag_off(frag);
 		*len = skb_frag_size(frag);
 	}
-	return otx2_dma_map_page(pfvf, page, offset, *len, DMA_TO_DEVICE);
+	return otx2_dma_map_page(pfvf, page, offset, *len, DMA_BIDIRECTIONAL);
 }
 
 static void otx2_dma_unmap_skb_frags(struct otx2_nic *pfvf, struct sg_list *sg)
@@ -107,7 +107,7 @@  static void otx2_dma_unmap_skb_frags(struct otx2_nic *pfvf, struct sg_list *sg)
 
 	for (seg = 0; seg < sg->num_segs; seg++) {
 		otx2_dma_unmap_page(pfvf, sg->dma_addr[seg],
-				    sg->size[seg], DMA_TO_DEVICE);
+				    sg->size[seg], DMA_BIDIRECTIONAL);
 	}
 	sg->num_segs = 0;
 }