Skip to contentSkip to conversation

net: fix a data race when get vlan device

PATCH zhudi (J) 2 participants 2 messages Apr 16, 2021

Subsystems (2)
lore
Share & tools
lore

zhudi (J)

author
#0 lore raw
Quoted 25 lines
Quoted 7 lines · zhudi

From: Di Zhu <[email protected]>

We encountered a crash: in the packet receiving process, we got an illegal VLAN device address, but the VLAN device address saved in vmcore is correct. After checking the code, we found a possible data competition:
CPU 0: CPU 1:

Quoted 8 lines
    (RCU read lock)                  (RTNL lock)
    vlan_do_receive()		       register_vlan_dev()
      vlan_find_dev()

        ->__vlan_group_get_device()	 ->vlan_group_prealloc_vid()

In vlan_group_prealloc_vid(), We need to make sure that kzalloc is executed before assigning a value to vlan devices array, otherwise we

As my understanding, there is a dependency between calling kzalloc() and assigning the address(returned from kzalloc()) to vg->vlan_devices_arrays, CPU and compiler can see the dependency, why can't it handling the dependency before adding the smp_wmb()?

See CONTROL DEPENDENCIES section in Documentation/memory- barriers.txt:

Quoted 8 lines

However, stores are not speculated. This means that ordering -is- provided for load-store control dependencies, as in the following example:

        q = READ_ONCE(a);
        if (q) {
                WRITE_ONCE(b, 1);
        }

Maybe I didn't make it clear. This memory isolation is to ensure the order of memset(object, 0, size) in kzalloc() operations and the subsequent array assignment statements.

kzalloc()

    ->memset(object, 0, size)

smp_wmb()

vg->vlan_devices_arrays[pidx][vidx] = array;

Because __vlan_group_get_device() function depends on this order

Quoted 44 lines

net/8021q/vlan.c static int vlan_group_prealloc_vid(struct vlan_group

Quoted 18 lines

may get a wrong address from the hardware cache on another cpu.

So fix it by adding memory barrier instruction to ensure the order of memory operations.

---
 net/8021q/vlan.c | 2 ++
 net/8021q/vlan.h | 3 +++
 2 files changed, 5 insertions(+)
net/8021q/vlan.c index renamed from net/8021q/vlan.c +0 -0

8b644113715e..4f541e05cd3f 100644

net/8021q/vlan.c +0 -0
@@ -71,6 +71,8 @@ static int vlan_group_prealloc_vid(struct vlan_group

*vg,

Quoted 13 lines
net/8021q/vlan.c +2 -0
 	if (array == NULL)
 		return -ENOBUFS;
 
+	smp_wmb();
+
 	vg->vlan_devices_arrays[pidx][vidx] = array;
 	return 0;
 }
net/8021q/vlan.h index renamed from net/8021q/vlan.h +0 -0

953405362795..7408fda084d3 100644

net/8021q/vlan.h static inline struct net_device · line 60

Quoted 4 lines
net/8021q/vlan.h +0 -0
@@ -57,6 +57,9 @@ static inline struct net_device
 *__vlan_group_get_device(struct vlan_group *vg,
 
 	array = vg->vlan_devices_arrays[pidx]
 				       [vlan_id /

VLAN_GROUP_ARRAY_PART_LEN];

net/8021q/vlan.h static inline struct net_device · line 64

Quoted 4 lines
net/8021q/vlan.h +3 -0
+
+	smp_rmb();
+
 	return array ? array[vlan_id % VLAN_GROUP_ARRAY_PART_LEN] :

NULL; }

Quoted 2 lines
#1 lore raw
Quoted 37 lines · zhudi (J)
Quoted 25 lines
Quoted 7 lines · zhudi

From: Di Zhu <[email protected]>

We encountered a crash: in the packet receiving process, we got an illegal VLAN device address, but the VLAN device address saved in vmcore is correct. After checking the code, we found a possible data competition:
CPU 0: CPU 1:

Quoted 8 lines
    (RCU read lock)                  (RTNL lock)
    vlan_do_receive()		       register_vlan_dev()
      vlan_find_dev()

        ->__vlan_group_get_device()	 ->vlan_group_prealloc_vid()

In vlan_group_prealloc_vid(), We need to make sure that kzalloc is executed before assigning a value to vlan devices array, otherwise we

As my understanding, there is a dependency between calling kzalloc() and assigning the address(returned from kzalloc()) to vg->vlan_devices_arrays, CPU and compiler can see the dependency, why can't it handling the dependency before adding the smp_wmb()?

See CONTROL DEPENDENCIES section in Documentation/memory- barriers.txt:

Quoted 8 lines

However, stores are not speculated. This means that ordering -is- provided for load-store control dependencies, as in the following example:

        q = READ_ONCE(a);
        if (q) {
                WRITE_ONCE(b, 1);
        }

Maybe I didn't make it clear. This memory isolation is to ensure the order of memset(object, 0, size) in kzalloc() operations and the subsequent array assignment statements.

Quoted 8 lines

kzalloc()

    ->memset(object, 0, size)

smp_wmb()

vg->vlan_devices_arrays[pidx][vidx] = array;

Because __vlan_group_get_device() function depends on this order

Thanks for clarify, it would be good to mention this in the commit log too.

Also, __vlan_group_get_device() is used in the data path, it would be to avoid the barrier op too. Maybe using rcu to avoid the barrier if the __vlan_group_get_device() is already protected by rcu_lock.

Quoted 46 lines
Quoted 45 lines

net/8021q/vlan.c static int vlan_group_prealloc_vid(struct vlan_group

Quoted 19 lines

may get a wrong address from the hardware cache on another cpu.

So fix it by adding memory barrier instruction to ensure the order of memory operations.

---
 net/8021q/vlan.c | 2 ++
 net/8021q/vlan.h | 3 +++
 2 files changed, 5 insertions(+)
net/8021q/vlan.c index renamed from net/8021q/vlan.c +0 -0

8b644113715e..4f541e05cd3f 100644

net/8021q/vlan.c +0 -0
@@ -71,6 +71,8 @@ static int vlan_group_prealloc_vid(struct vlan_group

*vg,

Quoted 13 lines
net/8021q/vlan.c +2 -0
 	if (array == NULL)
 		return -ENOBUFS;
 
+	smp_wmb();
+
 	vg->vlan_devices_arrays[pidx][vidx] = array;
 	return 0;
 }
net/8021q/vlan.h index renamed from net/8021q/vlan.h +0 -0

953405362795..7408fda084d3 100644

net/8021q/vlan.h static inline struct net_device · line 60

Quoted 4 lines
net/8021q/vlan.h +0 -0
@@ -57,6 +57,9 @@ static inline struct net_device
 *__vlan_group_get_device(struct vlan_group *vg,
 
 	array = vg->vlan_devices_arrays[pidx]
 				       [vlan_id /

VLAN_GROUP_ARRAY_PART_LEN];

net/8021q/vlan.h static inline struct net_device · line 64

Quoted 4 lines
net/8021q/vlan.h +3 -0
+
+	smp_rmb();
+
 	return array ? array[vlan_id % VLAN_GROUP_ARRAY_PART_LEN] :

NULL; }

Quoted 2 lines