net: microchip: vcap: fix races on the shared Super VCAP block
Subsystems (4)
PATCH 1/1 net: microchip: vcap: fix races on the shared Super VCAP block
The VCAP instances on a chip are not independent, yet they are locked independently. On sparx5 and lan969x the IS0 and IS2 instances are backed by the same Super VCAP hardware block and share its cache and command registers: every access drives the shared VCAP_SUPER_CTRL register and moves data through the shared cache registers.
Accessing one instance therefore races with accessing another. The per-instance admin->lock cannot prevent this, as each instance takes a different lock.
The locking issue is mostly disguised by the fact that the core usage of the vcap api runs under rtnl. However, the full rule dump in debugfs decodes rules straight from hardware (a READ command followed by a cache read) and runs outside rtnl, so it races a concurrent tc-flower rule write to another Super VCAP instance.
Besides corrupting the dump, the read repopulates the shared cache between the writers cache fill and its write command, so the writer commits the wrong data and corrupts the hardware entry.
Introduce vcap_lock() and vcap_unlock() helpers and route every rule lock site in the VCAP API and its debugfs code through them. Replace the per-instance admin->lock with a single mutex in struct vcap_control that serializes access to all instances. The helpers reach it through a new admin->vctrl back-pointer, and the clients initialise and destroy the control lock instead of a per-instance one.
No path holds more than one instance lock, so collapsing them onto a single mutex cannot self-deadlock.
---
This was discovered by sashiko on a net-next series adding L3 unicast routing to sparx5 and lan969x. That work introduces a new vcap instance which is driven outside of rtnl. However, since the debugfs dump already runs outside of rtnl the bug is reachable in mainline.
I have added this as a single patch to make it easier to cherry-pick. I can split it into a patch refactoring all locking sites to use new vcap_lock/vcap_unlock functions, and one which then swaps the admin->lock to a global vcap lock, if that is preferred.
--- .../ethernet/microchip/lan966x/lan966x_vcap_impl.c | 5 +- .../ethernet/microchip/sparx5/sparx5_vcap_impl.c | 5 +- drivers/net/ethernet/microchip/vcap/vcap_api.c | 72 ++++++++++++---------- drivers/net/ethernet/microchip/vcap/vcap_api.h | 3 +- .../net/ethernet/microchip/vcap/vcap_api_debugfs.c | 8 +-- .../microchip/vcap/vcap_api_debugfs_kunit.c | 3 +- .../net/ethernet/microchip/vcap/vcap_api_kunit.c | 3 +- .../net/ethernet/microchip/vcap/vcap_api_private.h | 3 + 8 files changed, 60 insertions(+), 42 deletions(-)
drivers/net/ethernet/microchip/lan966x/lan966x_vcap_impl.c +3 -2
@@ -601,7 +601,6 @@ static void lan966x_vcap_admin_free(struct vcap_admin *admin)kfree (admin->cache.keystream);kfree (admin->cache.maskstream);kfree (admin->cache.actionstream); -mutex_destroy (&admin->lock);kfree (admin); }
@@ -615,7 +614,7 @@ lan966x_vcap_admin_alloc(struct lan966x *lan966x, struct vcap_control *ctrl,if (!admin)return ERR_PTR (-ENOMEM ); -mutex_init (&admin->lock); + admin->vctrl = ctrl;INIT_LIST_HEAD (&admin->list);INIT_LIST_HEAD (&admin->rules);INIT_LIST_HEAD (&admin->enabled);
@@ -721,6 +720,7 @@ int lan966x_vcap_init(struct lan966x *lan966x) ctrl->ops = &lan966x_vcap_ops;INIT_LIST_HEAD (&ctrl->list); +mutex_init (&ctrl->lock);for (int i =0 ; i <ARRAY_SIZE (lan966x_vcap_inst_cfg); ++i) { cfg = &lan966x_vcap_inst_cfg[i];
@@ -780,5 +780,6 @@ void lan966x_vcap_deinit(struct lan966x *lan966x)lan966x_vcap_admin_free (admin); } +mutex_destroy (&ctrl->lock);kfree (ctrl); }
drivers/net/ethernet/microchip/sparx5/sparx5_vcap_impl.c +3 -2
@@ -1930,7 +1930,6 @@ static void sparx5_vcap_admin_free(struct vcap_admin *admin) {if (!admin)return ; -mutex_destroy (&admin->lock);kfree (admin->cache.keystream);kfree (admin->cache.maskstream);kfree (admin->cache.actionstream);
@@ -1950,7 +1949,7 @@ sparx5_vcap_admin_alloc(struct sparx5 *sparx5, struct vcap_control *ctrl,INIT_LIST_HEAD (&admin->list);INIT_LIST_HEAD (&admin->rules);INIT_LIST_HEAD (&admin->enabled); -mutex_init (&admin->lock); + admin->vctrl = ctrl; admin->vtype = cfg->vtype; admin->vinst = cfg->vinst; admin->ingress = cfg->ingress;
@@ -2059,6 +2058,7 @@ int sparx5_vcap_init(struct sparx5 *sparx5) ctrl->ops = &sparx5_vcap_ops;INIT_LIST_HEAD (&ctrl->list); +mutex_init (&ctrl->lock);for (idx =0 ; idx <ARRAY_SIZE (sparx5_vcap_inst_cfg); ++idx) { cfg = &consts->vcaps_cfg[idx]; admin =sparx5_vcap_admin_alloc (sparx5, ctrl, cfg);
@@ -2097,5 +2097,6 @@ void sparx5_vcap_deinit(struct sparx5 *sparx5)list_del (&admin->list);sparx5_vcap_admin_free (admin); } +mutex_destroy (&ctrl->lock);kfree (ctrl); }
drivers/net/ethernet/microchip/vcap/vcap_api.c +41 -31
235 lines not on this page · raw message
drivers/net/ethernet/microchip/vcap/vcap_api.h +2 -1
@@ -164,7 +164,7 @@ struct vcap_admin {struct list_head list;/* for insertion in vcap_control */ struct list_head rules;/* list of rules */ struct list_head enabled;/* list of enabled ports */ -struct mutex lock;/* control access to rules */ +struct vcap_control *vctrl;/* the control instance owning this vcap */ enum vcap_type vtype;/* type of vcap */ int vinst;/* instance number within the same type */ int first_cid;/* first chain id in this vcap */
@@ -275,6 +275,7 @@ struct vcap_control {const struct vcap_info *vcaps;/* client supplied vcap models */ const struct vcap_statistics *stats;/* client supplied vcap stats */ struct list_head list;/* list of vcap instances */ + struct mutex lock; /* serialize access to all vcap instances */ };#endif /* __VCAP_API__ */
drivers/net/ethernet/microchip/vcap/vcap_api_debugfs.c +4 -4
@@ -410,9 +410,9 @@ static int vcap_debugfs_show(struct seq_file *m, void *unused) };int ret; -mutex_lock (&info->admin->lock); +vcap_lock (info->admin); ret =vcap_show_admin (info->vctrl, info->admin, &out); -mutex_unlock (&info->admin->lock); +vcap_unlock (info->admin);return ret; }DEFINE_SHOW_ATTRIBUTE (vcap_debugfs);
@@ -427,9 +427,9 @@ static int vcap_raw_debugfs_show(struct seq_file *m, void *unused) };int ret; -mutex_lock (&info->admin->lock); +vcap_lock (info->admin); ret =vcap_show_admin_raw (info->vctrl, info->admin, &out); -mutex_unlock (&info->admin->lock); +vcap_unlock (info->admin);return ret; }DEFINE_SHOW_ATTRIBUTE (vcap_raw_debugfs);
drivers/net/ethernet/microchip/vcap/vcap_api_debugfs_kunit.c +2 -1
@@ -243,10 +243,11 @@ static void vcap_test_api_init(struct vcap_admin *admin) {/* Initialize the shared objects */ INIT_LIST_HEAD (&test_vctrl.list); +mutex_init (&test_vctrl.lock);INIT_LIST_HEAD (&admin->list);INIT_LIST_HEAD (&admin->rules);INIT_LIST_HEAD (&admin->enabled); -mutex_init (&admin->lock); + admin->vctrl = &test_vctrl;list_add_tail (&admin->list, &test_vctrl.list);memset (test_updateaddr,0 ,sizeof (test_updateaddr)); test_updateaddridx =0 ;
drivers/net/ethernet/microchip/vcap/vcap_api_kunit.c +2 -1
@@ -233,10 +233,11 @@ static void vcap_test_api_init(struct vcap_admin *admin) {/* Initialize the shared objects */ INIT_LIST_HEAD (&test_vctrl.list); +mutex_init (&test_vctrl.lock);INIT_LIST_HEAD (&admin->list);INIT_LIST_HEAD (&admin->rules);INIT_LIST_HEAD (&admin->enabled); -mutex_init (&admin->lock); + admin->vctrl = &test_vctrl;list_add_tail (&admin->list, &test_vctrl.list);memset (test_updateaddr,0 ,sizeof (test_updateaddr)); test_updateaddridx =0 ;
drivers/net/ethernet/microchip/vcap/vcap_api_private.h +3 -0
@@ -50,6 +50,9 @@ struct vcap_stream_iter {/* Check that the control has a valid set of callbacks */ int vcap_api_check (struct vcap_control *ctrl); +/* Serialize access to the vcap instances of a control */ +void vcap_lock (struct vcap_admin *admin); +void vcap_unlock (struct vcap_admin *admin);/* Erase the VCAP cache area used or encoding and decoding */ void vcap_erase_cache (struct vcap_rule_internal *ri);
---
base-commit: d87363b0edfc7504ff2b144fe4cdd8154f90f42e
change-id: 20260624-microchip_fix_vcap_locking-70c057531c16
Best regards,
Signature · 1 line
Jens Emil Schulz Østergaard <[email protected]>
This patch was applied to netdev/net.git (main) by Paolo Abeni <[email protected]>:
Hello:
This patch was applied to netdev/net.git (main)
by Paolo Abeni <[email protected]>:
Quoted 11 lines · On Tue, 30 Jun 2026 14:20:13 +0200 you wrote:
The VCAP instances on a chip are not independent, yet they are locked independently. On sparx5 and lan969x the IS0 and IS2 instances are backed by the same Super VCAP hardware block and share its cache and command registers: every access drives the shared
VCAP_SUPER_CTRLregister and moves data through the shared cache registers.Accessing one instance therefore races with accessing another. The per-instance
admin->lockcannot prevent this, as each instance takes a different lock.[...]
Here is the summary with links:
- [net] net: microchip: vcap: fix races on the shared Super VCAP block
https://git.kernel.org/netdev/net/c/d7a8d500d7e4
You are awesome, thank you!
Signature · 2 lines
Deet-doot-dot, I am a bot. https://korg.docs.kernel.org/patchwork/pwbot.html