netdev.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [net-next v2 1/1] tipc: avoid unnecessary copying of bundled messages
@ 2018-02-15 13:14 Jon Maloy
  2018-02-16 20:32 ` David Miller
  0 siblings, 1 reply; 5+ messages in thread
From: Jon Maloy @ 2018-02-15 13:14 UTC (permalink / raw)
  To: davem, netdev
  Cc: mohan.krishna.ghanta.krishnamurthy, tung.q.nguyen, hoang.h.le,
	jon.maloy, canh.d.luu, ying.xue, tipc-discussion

A received sk buffer may contain dozens of smaller 'bundled' messages
which after extraction go each in their own direction.

Unfortunately, when we extract those messages using skb_clone() each
of the extracted buffers inherit the truesize value of the original
buffer. Apart from causing massive overaccounting of the base buffer's
memory, this often causes tipc_msg_validate() to come to the false
conclusion that the ratio truesize/datasize > 4, and perform an
unnecessary copying of the extracted buffer.

We now fix this problem by explicitly correcting the truesize value of
the buffer clones to be the truesize of the clone itself plus a
calculated fraction of the base buffer's overhead. This change
eliminates the overaccounting and at least mitigates the occurrence
of unnecessary buffer copying.

Reported-by: Hoang Le <hoang.h.le@dektek.com.au>
Acked-by: Ying Xue <ying.xue@windriver.com>
Signed-off-by: Jon Maloy <jon.maloy@ericsson.com>
---
 net/tipc/msg.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)

diff --git a/net/tipc/msg.c b/net/tipc/msg.c
index 4e1c6f6..ce0bfc4 100644
--- a/net/tipc/msg.c
+++ b/net/tipc/msg.c
@@ -416,8 +416,8 @@ bool tipc_msg_bundle(struct sk_buff *skb, struct tipc_msg *msg, u32 mtu)
  */
 bool tipc_msg_extract(struct sk_buff *skb, struct sk_buff **iskb, int *pos)
 {
+	int imsz, offset, clone_cnt, skb_overhead;
 	struct tipc_msg *msg;
-	int imsz, offset;
 
 	*iskb = NULL;
 	if (unlikely(skb_linearize(skb)))
@@ -434,6 +434,11 @@ bool tipc_msg_extract(struct sk_buff *skb, struct sk_buff **iskb, int *pos)
 	skb_pull(*iskb, offset);
 	imsz = msg_size(buf_msg(*iskb));
 	skb_trim(*iskb, imsz);
+
+	/* Scale extracted buffer's truesize to avoid double accounting */
+	clone_cnt = max_t(u32, 1, msg_msgcnt(msg));
+	skb_overhead = skb->truesize - skb->len;
+	(*iskb)->truesize = SKB_TRUESIZE(imsz) + skb_overhead / clone_cnt;
 	if (unlikely(!tipc_msg_validate(iskb)))
 		goto none;
 	*pos += align(imsz);
-- 
2.1.4

^ permalink raw reply related	[flat|nested] 5+ messages in thread

* Re: [net-next v2 1/1] tipc: avoid unnecessary copying of bundled messages
  2018-02-15 13:14 [net-next v2 1/1] tipc: avoid unnecessary copying of bundled messages Jon Maloy
@ 2018-02-16 20:32 ` David Miller
  2018-02-17 20:13   ` Jon Maloy
  2018-02-19 13:29   ` David Laight
  0 siblings, 2 replies; 5+ messages in thread
From: David Miller @ 2018-02-16 20:32 UTC (permalink / raw)
  To: jon.maloy
  Cc: netdev, mohan.krishna.ghanta.krishnamurthy, tung.q.nguyen,
	hoang.h.le, canh.d.luu, ying.xue, tipc-discussion

From: Jon Maloy <jon.maloy@ericsson.com>
Date: Thu, 15 Feb 2018 14:14:37 +0100

> A received sk buffer may contain dozens of smaller 'bundled' messages
> which after extraction go each in their own direction.
> 
> Unfortunately, when we extract those messages using skb_clone() each
> of the extracted buffers inherit the truesize value of the original
> buffer. Apart from causing massive overaccounting of the base buffer's
> memory, this often causes tipc_msg_validate() to come to the false
> conclusion that the ratio truesize/datasize > 4, and perform an
> unnecessary copying of the extracted buffer.
> 
> We now fix this problem by explicitly correcting the truesize value of
> the buffer clones to be the truesize of the clone itself plus a
> calculated fraction of the base buffer's overhead. This change
> eliminates the overaccounting and at least mitigates the occurrence
> of unnecessary buffer copying.
> 
> Reported-by: Hoang Le <hoang.h.le@dektek.com.au>
> Acked-by: Ying Xue <ying.xue@windriver.com>
> Signed-off-by: Jon Maloy <jon.maloy@ericsson.com>

As I explained in my previous two emails, I don't think this method of
accounting is appropriate.

All of your clones must use the same skb->truesize as the original SKB
because each and every one of them keeps the full buffer from being
liberated until they are released.

^ permalink raw reply	[flat|nested] 5+ messages in thread

* RE: [net-next v2 1/1] tipc: avoid unnecessary copying of bundled messages
  2018-02-16 20:32 ` David Miller
@ 2018-02-17 20:13   ` Jon Maloy
  2018-02-19 13:29   ` David Laight
  1 sibling, 0 replies; 5+ messages in thread
From: Jon Maloy @ 2018-02-17 20:13 UTC (permalink / raw)
  To: David Miller
  Cc: netdev@vger.kernel.org, Mohan Krishna Ghanta Krishnamurthy,
	Tung Quang Nguyen, Hoang Huu Le, Canh Duc Luu, Ying Xue,
	tipc-discussion@lists.sourceforge.net



> -----Original Message-----
> From: David Miller [mailto:davem@davemloft.net]
> Sent: Friday, February 16, 2018 21:33
> To: Jon Maloy <jon.maloy@ericsson.com>
> Cc: netdev@vger.kernel.org; Mohan Krishna Ghanta Krishnamurthy
> <mohan.krishna.ghanta.krishnamurthy@ericsson.com>; Tung Quang Nguyen
> <tung.q.nguyen@dektech.com.au>; Hoang Huu Le
> <hoang.h.le@dektech.com.au>; Canh Duc Luu
> <canh.d.luu@dektech.com.au>; Ying Xue <ying.xue@windriver.com>; tipc-
> discussion@lists.sourceforge.net
> Subject: Re: [net-next v2 1/1] tipc: avoid unnecessary copying of bundled
> messages
> 
> From: Jon Maloy <jon.maloy@ericsson.com>
> Date: Thu, 15 Feb 2018 14:14:37 +0100
> 
> > A received sk buffer may contain dozens of smaller 'bundled' messages
> > which after extraction go each in their own direction.
> >
> > Unfortunately, when we extract those messages using skb_clone() each
> > of the extracted buffers inherit the truesize value of the original
> > buffer. Apart from causing massive overaccounting of the base buffer's
> > memory, this often causes tipc_msg_validate() to come to the false
> > conclusion that the ratio truesize/datasize > 4, and perform an
> > unnecessary copying of the extracted buffer.
> >
> > We now fix this problem by explicitly correcting the truesize value of
> > the buffer clones to be the truesize of the clone itself plus a
> > calculated fraction of the base buffer's overhead. This change
> > eliminates the overaccounting and at least mitigates the occurrence of
> > unnecessary buffer copying.
> >
> > Reported-by: Hoang Le <hoang.h.le@dektek.com.au>
> > Acked-by: Ying Xue <ying.xue@windriver.com>
> > Signed-off-by: Jon Maloy <jon.maloy@ericsson.com>
> 
> As I explained in my previous two emails, I don't think this method of
> accounting is appropriate.
> 
> All of your clones must use the same skb->truesize as the original SKB
> because each and every one of them keeps the full buffer from being
> liberated until they are released.

I understand what you are saying, although I am not happy with its consequences in this case. I guess I will just leave it the way it is until I can come up with something  smarter.
///jon

^ permalink raw reply	[flat|nested] 5+ messages in thread

* RE: [net-next v2 1/1] tipc: avoid unnecessary copying of bundled messages
  2018-02-16 20:32 ` David Miller
  2018-02-17 20:13   ` Jon Maloy
@ 2018-02-19 13:29   ` David Laight
  2018-02-19 17:19     ` Jon Maloy
  1 sibling, 1 reply; 5+ messages in thread
From: David Laight @ 2018-02-19 13:29 UTC (permalink / raw)
  To: jon.maloy@ericsson.com
  Cc: netdev@vger.kernel.org,
	mohan.krishna.ghanta.krishnamurthy@ericsson.com,
	tung.q.nguyen@dektech.com.au, hoang.h.le@dektech.com.au,
	canh.d.luu@dektech.com.au, ying.xue@windriver.com,
	tipc-discussion@lists.sourceforge.net

From: Jon Maloy <jon.maloy@ericsson.com>
Date: Thu, 15 Feb 2018 14:14:37 +0100

> A received sk buffer may contain dozens of smaller 'bundled' messages
> which after extraction go each in their own direction.
>
> Unfortunately, when we extract those messages using skb_clone() each
> of the extracted buffers inherit the truesize value of the original
> buffer. Apart from causing massive overaccounting of the base buffer's
> memory, this often causes tipc_msg_validate() to come to the false
> conclusion that the ratio truesize/datasize > 4, and perform an
> unnecessary copying of the extracted buffer.
>
> We now fix this problem by explicitly correcting the truesize value of
> the buffer clones to be the truesize of the clone itself plus a
> calculated fraction of the base buffer's overhead. This change
> eliminates the overaccounting and at least mitigates the occurrence
> of unnecessary buffer copying.

Have you actually checked that copying the data when you extract the
messages isn't faster than cloning and trying to avoid the copy?
Copying at the point is probably cheaper because it leads to
a simpler message structure.

	David

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [net-next v2 1/1] tipc: avoid unnecessary copying of bundled messages
  2018-02-19 13:29   ` David Laight
@ 2018-02-19 17:19     ` Jon Maloy
  0 siblings, 0 replies; 5+ messages in thread
From: Jon Maloy @ 2018-02-19 17:19 UTC (permalink / raw)
  To: David Laight
  Cc: netdev@vger.kernel.org, tipc-discussion@lists.sourceforge.net,
	Hoang Huu Le, Mohan Krishna Ghanta Krishnamurthy



> -----Original Message-----
> From: David Laight [mailto:David.Laight@ACULAB.COM]
> Sent: Monday, February 19, 2018 14:30
> To: Jon Maloy <jon.maloy@ericsson.com>
> Cc: netdev@vger.kernel.org; Mohan Krishna Ghanta Krishnamurthy
> <mohan.krishna.ghanta.krishnamurthy@ericsson.com>; Tung Quang Nguyen
> <tung.q.nguyen@dektech.com.au>; Hoang Huu Le
> <hoang.h.le@dektech.com.au>; Canh Duc Luu
> <canh.d.luu@dektech.com.au>; Ying Xue <ying.xue@windriver.com>; tipc-
> discussion@lists.sourceforge.net
> Subject: RE: [net-next v2 1/1] tipc: avoid unnecessary copying of bundled
> messages
> 
> From: Jon Maloy <jon.maloy@ericsson.com>
> Date: Thu, 15 Feb 2018 14:14:37 +0100
> 
> > A received sk buffer may contain dozens of smaller 'bundled' messages
> > which after extraction go each in their own direction.
> >
> > Unfortunately, when we extract those messages using skb_clone() each
> > of the extracted buffers inherit the truesize value of the original
> > buffer. Apart from causing massive overaccounting of the base buffer's
> > memory, this often causes tipc_msg_validate() to come to the false
> > conclusion that the ratio truesize/datasize > 4, and perform an
> > unnecessary copying of the extracted buffer.
> >
> > We now fix this problem by explicitly correcting the truesize value of
> > the buffer clones to be the truesize of the clone itself plus a
> > calculated fraction of the base buffer's overhead. This change
> > eliminates the overaccounting and at least mitigates the occurrence of
> > unnecessary buffer copying.
> 
> Have you actually checked that copying the data when you extract the
> messages isn't faster than cloning and trying to avoid the copy?
> Copying at the point is probably cheaper because it leads to a simpler
> message structure.

Yes, that is probably what I'll end up doing, if copying is unavoidable anyway.

///jon

> 
> 	David


------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2018-02-19 17:19 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2018-02-15 13:14 [net-next v2 1/1] tipc: avoid unnecessary copying of bundled messages Jon Maloy
2018-02-16 20:32 ` David Miller
2018-02-17 20:13   ` Jon Maloy
2018-02-19 13:29   ` David Laight
2018-02-19 17:19     ` Jon Maloy

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).