Skip to contentSkip to conversation

tun: remove possible false sharing in tun_flow_update()

PATCH Applied Eric Dumazet 3 participants 3 messages Oct 9, 2019 – Oct 10, 2019

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 tun: remove possible false sharing in tun_flow_update()

As mentioned in https://github.com/google/ktsan/wiki/READ_ONCE-and-WRITE_ONCE#it-may-improve-performance
a C compiler can legally transform

if (e->queue_index != queue_index)

	e->queue_index = queue_index;

to :

	e->queue_index = queue_index;

Note that the code using jiffies has no issue, since jiffies has volatile attribute.

if (e->updated != jiffies)

    e->updated = jiffies;
Fixes 83b1bc122cab ("tun: align write-heavy flow entry members to a cache line")
Cc Zhang Yu <[email protected]>
Cc Wang Li <[email protected]>
Cc Li RongQing <[email protected]>
Cc Jason Wang <[email protected]>
---
 drivers/net/tun.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/net/tun.c +2 -2
index 812dc3a65efbb9d1ee2724e73978dbc4803ec171..a8d3141582a53caf407dc9aff61c452998de068f 100644
@@ -526,8 +526,8 @@ static void tun_flow_update(struct tun_struct *tun, u32 rxhash,
 	e = tun_flow_find(head, rxhash);
 	if (likely(e)) {
 		/* TODO: keep queueing to old queue until it's empty? */
-		if (e->queue_index != queue_index)
-			e->queue_index = queue_index;
+		if (READ_ONCE(e->queue_index) != queue_index)
+			WRITE_ONCE(e->queue_index, queue_index);
 		if (e->updated != jiffies)
 			e->updated = jiffies;
 		sock_rps_record_flow_hash(e->rps_rxhash);
Signature · 1 line
2.23.0.581.g78d2f28ef7-goog
Acked-by on v1 1/1
#1 lore raw
Quoted 31 lines · Eric Dumazet

As mentioned in https://github.com/google/ktsan/wiki/READ_ONCE-and-WRITE_ONCE#it-may-improve-performance
a C compiler can legally transform

if (e->queue_index != queue_index)

	e->queue_index = queue_index;

to :

	e->queue_index = queue_index;

Note that the code using jiffies has no issue, since jiffies has volatile attribute.

if (e->updated != jiffies)

     e->updated = jiffies;
Fixes 83b1bc122cab ("tun: align write-heavy flow entry members to a cache line")
Cc Zhang Yu <[email protected]>
Cc Wang Li <[email protected]>
Cc Li RongQing <[email protected]>
Cc Jason Wang <[email protected]>
---
  drivers/net/tun.c | 4 ++--
  1 file changed, 2 insertions(+), 2 deletions(-)
drivers/net/tun.c +0 -0
index 812dc3a65efbb9d1ee2724e73978dbc4803ec171..a8d3141582a53caf407dc9aff61c452998de068f 100644

drivers/net/tun.c static void tun_flow_update(struct tun_struct *tun, u32 rxhash, · line 533

Quoted 10 lines
drivers/net/tun.c +2 -2
@@ -526,8 +526,8 @@ static void tun_flow_update(struct tun_struct *tun, u32 rxhash,
  	e = tun_flow_find(head, rxhash);
  	if (likely(e)) {
  		/* TODO: keep queueing to old queue until it's empty? */
-		if (e->queue_index != queue_index)
-			e->queue_index = queue_index;
+		if (READ_ONCE(e->queue_index) != queue_index)
+			WRITE_ONCE(e->queue_index, queue_index);
  		if (e->updated != jiffies)
  			e->updated = jiffies;
  		sock_rps_record_flow_hash(e->rps_rxhash);
#2 lore raw
In reply to Eric Dumazet
Quoted 14 lines · Eric Dumazet

As mentioned in https://github.com/google/ktsan/wiki/READ_ONCE-and-WRITE_ONCE#it-may-improve-performance
a C compiler can legally transform

if (e->queue_index != queue_index)

	e->queue_index = queue_index;

to :

	e->queue_index = queue_index;

Note that the code using jiffies has no issue, since jiffies has volatile attribute.

if (e->updated != jiffies)

Quoted 8 lines
    e->updated = jiffies;
Fixes 83b1bc122cab ("tun: align write-heavy flow entry members to a cache line")
Cc Zhang Yu <[email protected]>
Cc Wang Li <[email protected]>
Cc Li RongQing <[email protected]>
Cc Jason Wang <[email protected]>

Applied, same story with stable, thanks!