octeontx2-af: npc: Enhancements.
Subsystems (6)
Ratheesh Kannoth
maintainerv18 cover octeontx2-af: npc: Enhancements.
This series extends Marvell octeontx2-af support for CN20K NPC (MCAM debuggability, allocation policy, default-rule lifetime, optional KPU profiles from firmware files, X2/X4 MCAM keyword handling in flows and defaults, and dynamic CN20K NPC private state), adds a devlink mechanism for multi-value parameters, and moves devlink_nl_param_fill() temporaries to the heap so stack usage stays reasonable once union devlink_param_value grows (patch 3).
Patch 1 improves CN20K MCAM visibility in debugfs: mcam_layout marks enabled entries, dstats reports per-entry hit deltas (baseline updated in software after each read; hardware counters are not cleared), and mismatch lists enabled entries without a PF mapping.
Patch 2 allocates the per-configuration-mode union devlink_param_value buffers and struct devlink_param_gset_ctx used by devlink_nl_param_fill() with kcalloc()/kzalloc_obj() and funnels failures through a single cleanup path so the netlink reply path stays safe as the union grows.
Patch 3 (Saeed) introduces DEVLINK_PARAM_TYPE_U64_ARRAY and nested DEVLINK_ATTR_PARAM_VALUE_DATA attributes so drivers and user space can exchange bounded u64 arrays; YAML, uapi, and netlink validation are updated.
Patch 4 adds a runtime devlink parameter srch_order to reorder CN20K subbank search during MCAM allocation (the param uses the u64 array type from patch 3).
Patch 5 ties default MCAM entries to NIX LF alloc/free on CN20K, adds NIX_LF_DONT_FREE_DFT_IDXS for PF teardown paths that must not drop default NPC indexes while the driver still owns state, and tightens nix_lf_alloc error propagation.
Patch 6 allows loading a custom KPU profile from /lib/firmware/kpu via module parameter kpu_profile, with cam2 / ptype_mask wiring and helpers that share firmware-sourced vs filesystem-sourced profile layouts.
Patch 7 makes default-rule allocation, AF flow install, and PF-side RSS, defaults, and ethtool flows respect the active CN20K MCAM keyword width (X2 vs X4), including X4 reference-index masking and -EOPNOTSUPP when a flow needs X4 keys on an X2-only profile.
Patch 8 replaces file-scope npc_priv and static dstats with allocation sized from discovered bank/subbank geometry, threads npc_priv_get() through CN20K NPC paths, and allocates dstats via devm_kzalloc for the debugfs helper.
Heap-backed devlink_nl_param_fill() sits immediately before the U64 array param work so incremental builds stay stack-safe as the union grows; the CN20K patches keep srch_order ahead of NIX LF coordination, optional KPU profile load from firmware files, X2/X4 handling, and the npc_priv refactor that touches the same files heavily.
Ratheesh Kannoth (7):
octeontx2-af: npc: cn20k: debugfs enhancements
devlink: heap-allocate param fill buffers in devlink_nl_param_fill
octeontx2-af: npc: cn20k: add subbank search order control
octeontx2: cn20k: Coordinate default rules with NIX LF lifecycle
octeontx2-af: npc: Support for custom KPU profile from filesystem
octeontx2: cn20k: Respect NPC MCAM X2/X4 profile in flows and DFT
alloc
octeontx2-af: npc: cn20k: Allocate npc_priv and dstats dynamically.
Saeed Mahameed (1):
devlink: Implement devlink param multi attribute nested data values
Documentation/netlink/specs/devlink.yaml | 4 + .../marvell/octeontx2/af/cn20k/debugfs.c | 163 ++++- .../ethernet/marvell/octeontx2/af/cn20k/npc.c | 633 ++++++++++++------ .../ethernet/marvell/octeontx2/af/cn20k/npc.h | 17 +- .../net/ethernet/marvell/octeontx2/af/mbox.h | 1 + .../net/ethernet/marvell/octeontx2/af/npc.h | 17 + .../net/ethernet/marvell/octeontx2/af/rvu.h | 12 +- .../marvell/octeontx2/af/rvu_devlink.c | 92 ++- .../ethernet/marvell/octeontx2/af/rvu_nix.c | 69 +- .../ethernet/marvell/octeontx2/af/rvu_npc.c | 486 +++++++++++--- .../ethernet/marvell/octeontx2/af/rvu_npc.h | 17 + .../marvell/octeontx2/af/rvu_npc_fs.c | 12 +- .../ethernet/marvell/octeontx2/af/rvu_reg.h | 1 + .../marvell/octeontx2/nic/otx2_flows.c | 48 +- .../ethernet/marvell/octeontx2/nic/otx2_pf.c | 6 +- include/net/devlink.h | 8 + include/uapi/linux/devlink.h | 1 + net/devlink/netlink_gen.c | 2 + net/devlink/param.c | 95 ++- 19 files changed, 1280 insertions(+), 404 deletions(-)
--
v17 -> v18: Addressed sashiko comments.
https://lore.kernel.org/netdev/[email protected]/
v16 -> v17: Addressed Jakub comments.
https://lore.kernel.org/netdev/[email protected]/
v15 -> v16: Addressed Sashiko comments
https://lore.kernel.org/netdev/[email protected]/
v14 -> v15: Addressed Paolo comments
https://lore.kernel.org/netdev/[email protected]/
v13 -> v14: Addressed sashiko comments.
I had to revert Jiri comment in v11 as sashiko was complaining about leaking kernel memory to userspace.
https://lore.kernel.org/netdev/[email protected]/
v12 -> v13: Addressed David Laight comments
https://lore.kernel.org/netdev/[email protected]/
v11 -> v12: Addressed Paolo,Jiri comments.
https://lore.kernel.org/netdev/[email protected]/
Added one patch which was rejected by simon in net (as it was kind of enhancement rather than a bug) Added one more patch- which allocates two variables from heap.
v10 -> v11: Addressed Paolo comments.
https://lore.kernel.org/netdev/[email protected]/
v9 -> v10: Addressed Paolo comments
https://lore.kernel.org/netdev/ [email protected]/
v8 -> v9: Addressed Simon comments
https://lore.kernel.org/netdev/ [email protected]/
v7 -> v8: Addressed Simon comments
https://lore.kernel.org/netdev/ [email protected]/T/#t
v6 -> v7: Addressed Simon comments
https://lore.kernel.org/netdev/[email protected]/
v5 -> v6: Addressed Jakub,Jiri comments
https://lore.kernel.org/netdev/ [email protected]/
v4 -> v5: Addressed Jakub comments
https://lore.kernel.org/netdev/ [email protected]/
v3 -> v4: Addressed Simon comments
https://lore.kernel.org/netdev/abDeXLpMMxp7G1v3@rkannoth-OptiPlex-7090/#t
v2 -> v3: Addressed Simon comments.
https://lore.kernel.org/netdev/ [email protected]/
v1 -> v2: Addressed Jakub comments.
https://lore.kernel.org/netdev/ [email protected]/#t
2.43.0
Ratheesh Kannoth
maintainerPATCH v18 1/8 octeontx2-af: npc: cn20k: debugfs enhancements
Improve MCAM visibility and field debugging for CN20K NPC.
- Extend "mcam_layout" to show enabled (+) or disabled state per entry so status can be verified without parsing the full "mcam_entry" dump.
- Add "dstats" debugfs entry: for enabled MCAM indices, print hit deltas since the prior read by comparing hardware counters to a per-entry software baseline and advancing that baseline after each read (hardware counters are not cleared).
- Add "mismatch" debugfs entry: lists MCAM entries that are enabled but not explicitly allocated, helping diagnose allocation/field issues.
--- .../marvell/octeontx2/af/cn20k/debugfs.c | 158 +++++++++++++++++- .../ethernet/marvell/octeontx2/af/cn20k/npc.c | 37 +++- .../ethernet/marvell/octeontx2/af/cn20k/npc.h | 11 ++ 3 files changed, 191 insertions(+), 15 deletions(-)
drivers/net/ethernet/marvell/octeontx2/af/cn20k/debugfs.c +151 -7
@@ -13,6 +13,7 @@#include "struct.h" #include "rvu.h" #include "debugfs.h" +#include "cn20k/reg.h" #include "cn20k/npc.h" static int npc_mcam_layout_show (struct seq_file *s,void *unused)
@@ -58,7 +59,8 @@ static int npc_mcam_layout_show(struct seq_file *s, void *unused) "v:%u", vidx0); } -seq_printf (s,"\t%u(%#x) %s\n" , idx0, pf1, +seq_printf (s,"\t%u(%#x)%c %s\n" , idx0, pf1, +test_bit (idx0, npc_priv->en_map) ?'+' :' ' , map ? buf0 :" " ); } goto next;
@@ -101,9 +103,13 @@ static int npc_mcam_layout_show(struct seq_file *s, void *unused) vidx1); } -seq_printf (s,"%05u(%#x) %s\t\t%05u(%#x) %s\n" , - idx1, pf2, v1 ? buf1 :" " , - idx0, pf1, v0 ? buf0 :" " ); +seq_printf (s,"%05u(%#x)%c %s\t\t%05u(%#x)%c %s\n" , + idx1, pf2, +test_bit (idx1, npc_priv->en_map) ?'+' :' ' , + v1 ? buf1 :" " , + idx0, pf1, +test_bit (idx0, npc_priv->en_map) ?'+' :' ' , + v0 ? buf0 :" " );continue ; }
@@ -120,8 +126,9 @@ static int npc_mcam_layout_show(struct seq_file *s, void *unused) vidx0); } -seq_printf (s,"\t\t \t\t%05u(%#x) %s\n" , idx0, - pf1, map ? buf0 :" " ); +seq_printf (s,"\t\t \t\t%05u(%#x)%c %s\n" , idx0, pf1, +test_bit (idx0, npc_priv->en_map) ?'+' :' ' , + map ? buf0 :" " );continue ; }
@@ -134,7 +141,8 @@ static int npc_mcam_layout_show(struct seq_file *s, void *unused)snprintf (buf1,sizeof (buf1),"v:%05u" , vidx1); } -seq_printf (s,"%05u(%#x) %s\n" , idx1, pf1, +seq_printf (s,"%05u(%#x)%c %s\n" , idx1, pf1, +test_bit (idx1, npc_priv->en_map) ?'+' :' ' , map ? buf1 :" " ); } next:
@@ -145,6 +153,136 @@ static int npc_mcam_layout_show(struct seq_file *s, void *unused)DEFINE_SHOW_ATTRIBUTE (npc_mcam_layout); +#define __OCTEONTX2_DEBUGFS_ATTRIBUTE_FOPS (__name) \ +static const struct file_operations __name ## _fops = { \ + .owner = THIS_MODULE, \ + .open = __name ## _open, \ + .read = seq_read, \ + .llseek = seq_lseek, \ + .release = single_release, \ +} + +#define DEFINE_OCTEONTX2_DEBUGFS_ATTRIBUTE_WITH_SIZE (__name, __size) \ +static int __name ## _open(struct inode *inode, struct file *file) \ +{ \ + return single_open_size(file, __name ## _show, inode->i_private, \ + __size); \ +} \ +__OCTEONTX2_DEBUGFS_ATTRIBUTE_FOPS(__name) + +static DEFINE_MUTEX (stats_lock); + +/* MAX_NUM_BANKS, MAX_SUBBANK_DEPTH and MAX_NUM_SUB_BANKS represent +* hard limit on all silicon variants, preventing any possibility of +* out-of-bounds access. +*/ +static u64 dstats[MAX_NUM_BANKS ][MAX_SUBBANK_DEPTH *MAX_NUM_SUB_BANKS ] = {}; +static int npc_mcam_dstats_show (struct seq_file *s,void *unused) +{ +struct npc_priv_t *npc_priv; +int blkaddr, pf, mcam_idx; +u64 stats, delta; +struct rvu *rvu; +char buff[64 ]; +u8 key_type; +void *map; + + npc_priv =npc_priv_get (); + rvu = s->private; + blkaddr =rvu_get_blkaddr (rvu,BLKTYPE_NPC ,0 ); +if (blkaddr <0 ) +return 0 ; + +mutex_lock (&stats_lock); +seq_puts (s,"idx\tpfunc\tstats\n" ); +for (int bank = npc_priv->num_banks -1 ; bank >=0 ; bank--) { +for (int idx = npc_priv->bank_depth -1 ; idx >=0 ; idx--) { + mcam_idx = bank * npc_priv->bank_depth + idx; + +if (npc_mcam_idx_2_key_type (rvu, mcam_idx, &key_type)) +continue ; + +if (key_type ==NPC_MCAM_KEY_X4 && bank !=0 ) +continue ; + +if (!test_bit (mcam_idx, npc_priv->en_map)) +continue ; + + stats =rvu_read64 (rvu, blkaddr, +NPC_AF_CN20K_MCAMEX_BANKX_STAT_EXT (idx, bank)); +if (!stats) +continue ; +if (stats == dstats[bank][idx]) +continue ; + +if (stats < dstats[bank][idx]) + dstats[bank][idx] =0 ; + + pf =0xFFFF ; + map =xa_load (&npc_priv->xa_idx2pf_map, mcam_idx); +if (map) + pf =xa_to_value (map); + + delta = stats - dstats[bank][idx]; + +snprintf (buff,sizeof (buff),"%u\t%#04x\t%llu\n" , + mcam_idx, pf, delta); +seq_puts (s, buff); + + dstats[bank][idx] = stats; + } + } + +mutex_unlock (&stats_lock); +return 0 ; +} + +/* "%u\t%#04x\t%llu\n" needs less than 64 characters to print */ +#define TOTAL_SZ (MAX_NUM_BANKS * MAX_NUM_SUB_BANKS * MAX_SUBBANK_DEPTH * 64) +DEFINE_OCTEONTX2_DEBUGFS_ATTRIBUTE_WITH_SIZE (npc_mcam_dstats,TOTAL_SZ ); + +static int npc_mcam_mismatch_show (struct seq_file *s,void *unused) +{ +struct npc_priv_t *npc_priv; +struct npc_subbank *sb; +int mcam_idx, sb_off; +struct rvu *rvu; +char buff[64 ]; +void *map; +int rc; + + npc_priv =npc_priv_get (); + rvu = s->private; + +seq_puts (s,"index\tsb idx\tkw type\n" ); +for (int bank = npc_priv->num_banks -1 ; bank >=0 ; bank--) { +for (int idx = npc_priv->bank_depth -1 ; idx >=0 ; idx--) { + mcam_idx = bank * npc_priv->bank_depth + idx; + +if (!test_bit (mcam_idx, npc_priv->en_map)) +continue ; + + map =xa_load (&npc_priv->xa_idx2pf_map, mcam_idx); +if (map) +continue ; + + rc =npc_mcam_idx_2_subbank_idx (rvu, mcam_idx, + &sb, &sb_off); +if (rc) +continue ; + +snprintf (buff,sizeof (buff),"%u\t%d\t%u\n" , + mcam_idx, sb->idx, sb->key_type); + +seq_puts (s, buff); + } + } +return 0 ; +} + +/* "%u\t%d\t%u\n" needs less than 64 characters to print. */ +DEFINE_OCTEONTX2_DEBUGFS_ATTRIBUTE_WITH_SIZE (npc_mcam_mismatch,TOTAL_SZ ); +static int npc_mcam_default_show (struct seq_file *s,void *unused) {struct npc_priv_t *npc_priv;
@@ -259,6 +397,12 @@ int npc_cn20k_debugfs_init(struct rvu *rvu)debugfs_create_file ("vidx2idx" ,0444 , rvu->rvu_dbg.npc, npc_priv, &npc_vidx2idx_map_fops); +debugfs_create_file ("dstats" ,0444 , rvu->rvu_dbg.npc, rvu, + &npc_mcam_dstats_fops); + +debugfs_create_file ("mismatch" ,0444 , rvu->rvu_dbg.npc, rvu, + &npc_mcam_mismatch_fops); +debugfs_create_file ("idx2vidx" ,0444 , rvu->rvu_dbg.npc, npc_priv, &npc_idx2vidx_map_fops);
drivers/net/ethernet/marvell/octeontx2/af/cn20k/npc.c +29 -8
@@ -824,7 +824,7 @@ npc_cn20k_enable_mcam_entry(struct rvu *rvu, int blkaddr,rvu_write64 (rvu, blkaddr,NPC_AF_CN20K_MCAMEX_BANKX_CFG_EXT (mcam_idx, bank), cfg); -return 0 ; + goto update_en_map; }/* For NPC_CN20K_MCAM_KEY_X4 keys, both the banks
@@ -842,6 +842,12 @@ npc_cn20k_enable_mcam_entry(struct rvu *rvu, int blkaddr, cfg); } +update_en_map: +if (enable) +set_bit (index, npc_priv.en_map); +else +clear_bit (index, npc_priv.en_map); +return 0 ; }
@@ -1789,9 +1795,9 @@ static int npc_subbank_idx_2_mcam_idx(struct rvu *rvu, struct npc_subbank *sb,return 0 ; } -static int npc_mcam_idx_2_subbank_idx (struct rvu *rvu,u16 mcam_idx, -struct npc_subbank **sb, -int *sb_off) +int npc_mcam_idx_2_subbank_idx (struct rvu *rvu,u16 mcam_idx, +struct npc_subbank **sb, +int *sb_off) {int bank_off, sb_id;
@@ -4498,11 +4504,19 @@ static int npc_priv_init(struct rvu *rvu) npc_const2 =rvu_read64 (rvu, blkaddr,NPC_AF_CONST2 ); num_banks = mcam->banks; +if (num_banks >MAX_NUM_BANKS ) { +dev_err (rvu->dev, +"Number of banks(%u) is invalid\n" , num_banks); +return -EINVAL ; + } + bank_depth = mcam->banksize; num_subbanks =FIELD_GET (GENMASK_ULL (39 ,32 ), npc_const2); -if (!num_subbanks) { -dev_err (rvu->dev,"Number of subbanks is zero\n" ); +if (!num_subbanks || num_subbanks >MAX_NUM_SUB_BANKS ) { +dev_err (rvu->dev, +"Number of subbanks is invalid %u\n" , + num_subbanks);return -EFAULT ; }
@@ -4513,10 +4527,15 @@ static int npc_priv_init(struct rvu *rvu)return -EINVAL ; } - npc_priv.num_subbanks = num_subbanks; - subbank_depth = bank_depth / num_subbanks; +if (subbank_depth >MAX_SUBBANK_DEPTH ) { +dev_err (rvu->dev, +"Invalid subbank depth %u\n" , + subbank_depth); +return -EINVAL ; + } + npc_priv.num_subbanks = num_subbanks; npc_priv.bank_depth = bank_depth; npc_priv.subbank_depth = subbank_depth;
@@ -4605,6 +4624,8 @@ void npc_cn20k_deinit(struct rvu *rvu)*/ kfree (npc_priv.sb);kfree (subbank_srch_order); +bitmap_clear (npc_priv.en_map,0 ,MAX_NUM_BANKS *MAX_NUM_SUB_BANKS * +MAX_SUBBANK_DEPTH ); }static int npc_setup_mcam_section (struct rvu *rvu,int key_type)
drivers/net/ethernet/marvell/octeontx2/af/cn20k/npc.h +11 -0
@@ -10,6 +10,10 @@#define MKEX_CN20K_SIGN 0x19bbfdbd160 +/* MAX_NUM_BANKS, MAX_SUBBANK_DEPTH and MAX_NUM_SUB_BANKS represent +* hard limit on all silicon variants, preventing any possibility of +* out-of-bounds access on matrix defined using these values. +*/ #define MAX_NUM_BANKS 2#define MAX_NUM_SUB_BANKS 32#define MAX_SUBBANK_DEPTH 256
@@ -170,6 +174,7 @@ struct npc_defrag_show_node {* @num_banks: Number of banks. * @num_subbanks: Number of subbanks. * @subbank_depth: Depth of subbank. +* @en_map: Enable/disable status. * @kw: Kex configured key type. * @sb: Subbank array. * @xa_sb_used: Array of used subbanks.
@@ -193,6 +198,9 @@ struct npc_priv_t {const int num_banks;int num_subbanks;int subbank_depth; +DECLARE_BITMAP (en_map,MAX_NUM_BANKS * +MAX_NUM_SUB_BANKS * +MAX_SUBBANK_DEPTH );u8 kw;struct npc_subbank *sb;struct xarray xa_sb_used;
@@ -336,5 +344,8 @@ u16 npc_cn20k_vidx2idx(u16 index);u16 npc_cn20k_idx2vidx (u16 idx);int npc_cn20k_defrag (struct rvu *rvu);bool npc_is_cgx_or_lbk (struct rvu *rvu,u16 pcifunc); +int npc_mcam_idx_2_subbank_idx (struct rvu *rvu,u16 mcam_idx, +struct npc_subbank **sb, +int *sb_off);#endif /* NPC_CN20K_H */
Signature · 1 line
2.43.0
Ratheesh Kannoth
maintainerRe: [PATCH v18 net-next 1/8] octeontx2-af: npc: cn20k: debugfs enhancements
Quoted 11 lines · Ratheesh Kannoth
Improve MCAM visibility and field debugging for CN20K NPC.
- Extend "
mcam_layout" to show enabled (+) or disabled state per entry so status can be verified without parsing the full "mcam_entry" dump.
- Add "dstats" debugfs entry: for enabled MCAM indices, print hit deltas since the prior read by comparing hardware counters to a per-entry software baseline and advancing that baseline after each read (hardware counters are not cleared).
- Add "mismatch" debugfs entry: lists MCAM entries that are enabled but not explicitly allocated, helping diagnose allocation/field issues.
Quoted 1 line
Signed-off-by Ratheesh Kannoth <[email protected]>
https://sashiko.dev/#/patchset/20260602060359.1894952-1-rkannoth%40marvell.com
sashiko says
Quoted 5 lines
Quoted 5 lines
drivers/net/ethernet/marvell/octeontx2/af/cn20k/debugfs.c +5
-0+ pf =0xFFFF ; + map =xa_load (&npc_priv->xa_idx2pf_map, mcam_idx); +if (map) + pf =xa_to_value (map); +
Quoted 8 lines
drivers/net/ethernet/marvell/octeontx2/af/cn20k/debugfs.c static int npc_mcam_layout_show(struct seq_file *s, void *unused) · line 232
Quoted 7 lines
drivers/net/ethernet/marvell/octeontx2/af/cn20k/debugfs.c +7
-0+ delta = stats - dstats[bank][idx]; + +snprintf (buff,sizeof (buff),"%u\t%#04x\t%llu\n" , + mcam_idx, pf, delta); +seq_puts (s, buff); + + dstats[bank][idx] = stats;Does this code allow an out-of-bounds array access?
No.
Quoted 4 lines
In
npc_priv_init()(shown below), integer division truncation allows a hardware-suppliedbank_depthto exceed statically allocated array dimensions. Because remainders are truncated, abank_depthvalue such as 8223 (withnum_subbanks= 32) yieldssubbank_depth= 256.
npc_priv_init() do check against max limit for MAX_BANKDEPTH and bail out on error.
Quoted 3 lines
I noticed this was fixed later in the series in commit "octeontx2-af: npc:
cn20k: Allocatenpc_privand dstats dynamically.", but this patch introduces a concrete buffer overflow.
These checks are in the current patch itself.
Quoted 7 lines
+update_en_map:
drivers/net/ethernet/marvell/octeontx2/af/cn20k/npc.c npc_cn20k_enable_mcam_entry(struct rvu *rvu, int blkaddr, · line 847
Quoted 2 lines
drivers/net/ethernet/marvell/octeontx2/af/cn20k/npc.c +2
-0+if (enable) +set_bit (index, npc_priv.en_map);Can index exceed the bitmap size here?
Due to the same integer division truncation issue mentioned above, index can exceed the expected bounds, causingset_bit()to write beyond the statically allocated 16384 bits of npc_priv.en_map.
No. "en_map" bitmap is defined with MAXimum size possible. Check against these max limits are done during probe (in npc_priv_init()) and returns error.
Quoted 5 lines
subbank_depth=bank_depth/num_subbanks;drivers/net/ethernet/marvell/octeontx2/af/cn20k/npc.c static int npc_priv_init(struct rvu *rvu) · line 4531
Quoted 1 line
drivers/net/ethernet/marvell/octeontx2/af/cn20k/npc.c +1
-0+if (subbank_depth >MAX_SUBBANK_DEPTH ) {This is a pre-existing issue, but can this integer division evaluate to 0? If hardware reports a banksize (which becomes
bank_depth) less thannum_subbanks(e.g., 16 vs 32), the integer division evaluates to 0.
Pre-existing issue, so not addressing here. We need to check agaist zero, wll post as a seperate patch to "net" tree, - or may be a hardening series.
Ratheesh Kannoth
maintainerRe: [PATCH v18 net-next 1/8] octeontx2-af: npc: cn20k: debugfs enhancements
In reply to Ratheesh KannothQuoted 11 lines · Ratheesh Kannoth
Improve MCAM visibility and field debugging for CN20K NPC.
- Extend "
mcam_layout" to show enabled (+) or disabled state per entry so status can be verified without parsing the full "mcam_entry" dump.
- Add "dstats" debugfs entry: for enabled MCAM indices, print hit deltas since the prior read by comparing hardware counters to a per-entry software baseline and advancing that baseline after each read (hardware counters are not cleared).
- Add "mismatch" debugfs entry: lists MCAM entries that are enabled but not explicitly allocated, helping diagnose allocation/field issues.
Quoted 1 line
Signed-off-by Ratheesh Kannoth <[email protected]>
https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20260602060359.1894952-1-rkannoth%40marvell.com says
Quoted 23 lines
drivers/net/ethernet/marvell/octeontx2/af/cn20k/debugfs.c static int npc_mcam_layout_show(struct seq_file *s, void *unused) · line 179
Quoted 7 lines
drivers/net/ethernet/marvell/octeontx2/af/cn20k/debugfs.c +7
-0+static DEFINE_MUTEX (stats_lock); + +/* MAX_NUM_BANKS, MAX_SUBBANK_DEPTH and MAX_NUM_SUB_BANKS represent +* hard limit on all silicon variants, preventing any possibility of +* out-of-bounds access. +*/ +static u64 dstats[MAX_NUM_BANKS ][MAX_SUBBANK_DEPTH *MAX_NUM_SUB_BANKS ] = {};The dstats array is a file-scope static and is never reset on driver teardown.
npc_cn20k_deinit()now clears npc_priv.en_map but leaves dstats alone:
voidnpc_cn20k_deinit(struct rvu *rvu){ ... bitmap_clear(npc_priv.en_map, 0, MAX_NUM_BANKS * MAX_NUM_SUB_BANKS * MAX_SUBBANK_DEPTH);}
On a probe -> remove -> probe sequence (or after an NPC reset) within the same module load, do the stale baselines from the previous device incarnation remain in dstats[][]?
The wrap-around guard only self-corrects when the new hardware counter is smaller than the stored baseline:
if (stats < dstats[bank][idx])dstats[bank][idx] = 0;
Quoted 8 lines
If the post-reinit counter is greater than the stale value, would the first read of dstats report stats -
stale_baselinerather than the true post-init delta? The commit message states baselines are advanced "after each read", which seems to imply per-incarnation correctness.
Note: this appears to be addressed later in the same series by the patch "octeontx2-af: npc: cn20k: Allocatenpc_privand dstats dynamically" (4767c16e8909), which moves dstats to adevm_kzalloc()allocation that is freshly zeroed at each probe.
There is only one AF device per system. So this case wont happen.
Quoted 44 lines
Quoted 7 lines
drivers/net/ethernet/marvell/octeontx2/af/cn20k/debugfs.c +7
-0+mutex_lock (&stats_lock); +seq_puts (s,"idx\tpfunc\tstats\n" ); +for (int bank = npc_priv->num_banks -1 ; bank >=0 ; bank--) { +for (int idx = npc_priv->bank_depth -1 ; idx >=0 ; idx--) { + mcam_idx = bank * npc_priv->bank_depth + idx; + +if (npc_mcam_idx_2_key_type (rvu, mcam_idx, &key_type))drivers/net/ethernet/marvell/octeontx2/af/cn20k/debugfs.c static int npc_mcam_layout_show(struct seq_file *s, void *unused) · line 212
Quoted 10 lines
drivers/net/ethernet/marvell/octeontx2/af/cn20k/debugfs.c +10
-0+continue ; + +if (key_type ==NPC_MCAM_KEY_X4 && bank !=0 ) +continue ; + +if (!test_bit (mcam_idx, npc_priv->en_map)) +continue ; + + stats =rvu_read64 (rvu, blkaddr, +NPC_AF_CN20K_MCAMEX_BANKX_STAT_EXT (idx, bank));Can idx exceed the second dimension of dstats[][] here? The inner loop runs idx from
npc_priv->bank_depth- 1 down to 0, but dstats is sizedMAX_SUBBANK_DEPTH*MAX_NUM_SUB_BANKS(8192).
Innpc_priv_init()the new validation only constrains the quotient:
subbank_depth=bank_depth/num_subbanks;if (subbank_depth > MAX_SUBBANK_DEPTH) { ... return -EINVAL;}
Because of integer truncation,bank_depthvalues in the range (MAX_NUM_SUB_BANKS*MAX_SUBBANK_DEPTH,MAX_NUM_SUB_BANKS*MAX_SUBBANK_DEPTH+num_subbanks- 1] (for example 8193..8223 with num_subbanks=32) pass this check while exceeding 8192.mcam->banksizeis sourced from a 16-bit hardware-reported field, so the value is structurally allowed even if current silicon programs a power-of-2 banksize. With such a banksize, would the newset_bit(index, npc_priv.en_map)innpc_cn20k_enable_mcam_entry()(which only validates index <mcam->total_entries) and the dstats[bank][idx] indexing here write past the bitmap and array?
The new comment incn20k/npc.hstates:
/*MAX_NUM_BANKS,MAX_SUBBANK_DEPTHandMAX_NUM_SUB_BANKSrepresent
* hard limit on all silicon variants, preventing any possibility of
* out-of-bounds access on matrix defined using these values. */
Would an explicit check such asbank_depth>MAX_NUM_SUB_BANKS*MAX_SUBBANK_DEPTH(ormcam->total_entriesagainst the bitmap size) make the comment match the code?
Quoted 8 lines
drivers/net/ethernet/marvell/octeontx2/af/cn20k/debugfs.c static int npc_mcam_layout_show(struct seq_file *s, void *unused) · line 220
Quoted 8 lines
drivers/net/ethernet/marvell/octeontx2/af/cn20k/debugfs.c +8
-0+if (!stats) +continue ; +if (stats == dstats[bank][idx]) +continue ; + +if (stats < dstats[bank][idx]) + dstats[bank][idx] =0 ; +
npc_priv_init() during probe() do check against limits MAX_SUBBANK_DEPTH, MAX_NUM_SUB_BANKS. So the condition wont happen.
Jakub Kicinski
maintainerRe: [PATCH v18 net-next 1/8] octeontx2-af: npc: cn20k: debugfs enhancements
Quoted 1 line · On Thu, 4 Jun 2026 07:49:43 +0530 Ratheesh Kannoth wrote:
There is only one AF device per system. So this case wont happen.
Can you explain why? I thought this was a PCIe card, not an SoC driver.
Ratheesh Kannoth
maintainerRe: [PATCH v18 net-next 1/8] octeontx2-af: npc: cn20k: debugfs enhancements
Quoted 4 lines · Jakub Kicinski
Quoted 1 line · On Thu, 4 Jun 2026 07:49:43 +0530 Ratheesh Kannoth wrote:
There is only one AF device per system. So this case wont happen.
Can you explain why? I thought this was a PCIe card, not an SoC driver.
This is PCIe. Firmware creates only one AF device. I will add a patch to reject any additional AF probe with -EBUSY so the driver model matches hardware and automated reviewers can rely on a single bound instance.
Ratheesh Kannoth
maintainerPATCH v18 2/8 devlink: heap-allocate param fill buffers in devlink_nl_param_fill
In reply to Ratheesh Kannothdevlink_nl_param_fill() kept two per-configuration-mode copies of union devlink_param_value plus a struct devlink_param_gset_ctx on the stack while building the Netlink reply. Allocate those with kcalloc() and kzalloc_obj() instead, and route failures through a single cleanup path so temporary buffers are always freed.
--- net/devlink/param.c | 62 +++++++++++++++++++++++++++++++++------------ 1 file changed, 46 insertions(+), 16 deletions(-)
net/devlink/param.c +46 -16
@@ -304,56 +304,79 @@ static int devlink_nl_param_fill(struct sk_buff *msg, struct devlink *devlink, u32 portid, u32 seq, int flags, struct netlink_ext_ack *extack) { - union devlink_param_value default_value[DEVLINK_PARAM_CMODE_MAX + 1]; -union devlink_param_value param_value[DEVLINK_PARAM_CMODE_MAX +1 ]; bool default_value_set[DEVLINK_PARAM_CMODE_MAX + 1] = {};bool param_value_set[DEVLINK_PARAM_CMODE_MAX +1 ] = {};const struct devlink_param *param = param_item->param; -struct devlink_param_gset_ctx ctx; +union devlink_param_value *default_value; +union devlink_param_value *param_value; +struct devlink_param_gset_ctx *ctx;struct nlattr *param_values_list;struct nlattr *param_attr;void *hdr;int err;int i; + default_value =kcalloc (DEVLINK_PARAM_CMODE_MAX +1 , +sizeof (*default_value),GFP_KERNEL ); +if (!default_value) +return -ENOMEM ; + + param_value =kcalloc (DEVLINK_PARAM_CMODE_MAX +1 , +sizeof (*param_value),GFP_KERNEL ); +if (!param_value) { +kfree (default_value); +return -ENOMEM ; + } + + ctx =kzalloc_obj (*ctx); +if (!ctx) { +kfree (param_value); +kfree (default_value); +return -ENOMEM ; + } +/* Get value from driver part to driverinit configuration mode */ for (i =0 ; i <=DEVLINK_PARAM_CMODE_MAX ; i++) {if (!devlink_param_cmode_is_supported (param, i))continue ;if (i ==DEVLINK_PARAM_CMODE_DRIVERINIT ) { -if (param_item->driverinit_value_new_valid) +if (param_item->driverinit_value_new_valid) { param_value[i] = param_item->driverinit_value_new; -else if (param_item->driverinit_value_valid) + }else if (param_item->driverinit_value_valid) { param_value[i] = param_item->driverinit_value; -else -return -EOPNOTSUPP ; + }else { + err = -EOPNOTSUPP ; + goto get_put_fail; + }if (param_item->driverinit_value_valid) { default_value[i] = param_item->driverinit_default; default_value_set[i] = true; } }else { - ctx.cmode = i; - err =devlink_param_get (devlink, param, &ctx, extack); + ctx->cmode = i; + err =devlink_param_get (devlink, param, ctx, extack);if (err) -return err; - param_value[i] = ctx.val; + goto get_put_fail; - err =devlink_param_get_default (devlink, param, &ctx, + param_value[i] = ctx->val; + + err =devlink_param_get_default (devlink, param, ctx, extack);if (!err) { - default_value[i] = ctx.val; + default_value[i] = ctx->val; default_value_set[i] = true; }else if (err != -EOPNOTSUPP ) { -return err; + goto get_put_fail; } } param_value_set[i] = true; } + err = -EMSGSIZE ; hdr =genlmsg_put (msg, portid, seq, &devlink_nl_family, flags, cmd);if (!hdr) -return -EMSGSIZE ; + goto get_put_fail;if (devlink_nl_put_handle (msg, devlink)) goto genlmsg_cancel;
@@ -393,6 +416,9 @@ static int devlink_nl_param_fill(struct sk_buff *msg, struct devlink *devlink,nla_nest_end (msg, param_values_list);nla_nest_end (msg, param_attr);genlmsg_end (msg, hdr); +kfree (default_value); +kfree (param_value); +kfree (ctx);return 0 ; values_list_nest_cancel:
@@ -401,7 +427,11 @@ static int devlink_nl_param_fill(struct sk_buff *msg, struct devlink *devlink,nla_nest_cancel (msg, param_attr); genlmsg_cancel:genlmsg_cancel (msg, hdr); -return -EMSGSIZE ; +get_put_fail: +kfree (default_value); +kfree (param_value); +kfree (ctx); +return err; } static void devlink_param_notify(struct devlink *devlink,
Signature · 1 line
2.43.0
Ratheesh Kannoth
maintainerPATCH v18 3/8 devlink: Implement devlink param multi attribute nested data values
In reply to Ratheesh KannothFrom: Saeed Mahameed <[email protected]>
Devlink param value attribute is not defined since devlink is handling the value validating and parsing internally, this allows us to implement multi attribute values without breaking any policies.
Devlink param multi-attribute values are considered to be dynamically sized arrays of u64 values, by introducing a new devlink param type DEVLINK_PARAM_TYPE_U64_ARRAY, driver and user space can set a variable count of u64 values into the DEVLINK_ATTR_PARAM_VALUE_DATA attribute.
Implement get/set parsing and add to the internal value structure passed to drivers.
This is useful for devices that need to configure a list of values for a specific configuration.
example:
$ devlink dev param show pci/... name multi-value-param name multi-value-param type driver-specific values: cmode permanent value: 0,1,2,3,4,5,6,7
$ devlink dev param set pci/... name multi-value-param \ value 4,5,6,7,0,1,2,3 cmode permanent
--- Documentation/netlink/specs/devlink.yaml | 4 +++ include/net/devlink.h | 8 ++++++ include/uapi/linux/devlink.h | 1 + net/devlink/netlink_gen.c | 2 ++ net/devlink/param.c | 33 +++++++++++++++++++++++- 5 files changed, 47 insertions(+), 1 deletion(-)
Documentation/netlink/specs/devlink.yaml +4 -0
@@ -234,6 +234,10 @@ definitions: value: 10 - name: binary + - + name: u64-array + value: 129 + - name: rate-tc-index-max type: const
include/net/devlink.h +8 -0
@@ -433,6 +433,13 @@ enum devlink_param_type {DEVLINK_PARAM_TYPE_U64 =DEVLINK_VAR_ATTR_TYPE_U64 ,DEVLINK_PARAM_TYPE_STRING =DEVLINK_VAR_ATTR_TYPE_STRING ,DEVLINK_PARAM_TYPE_BOOL =DEVLINK_VAR_ATTR_TYPE_FLAG , + DEVLINK_PARAM_TYPE_U64_ARRAY = DEVLINK_VAR_ATTR_TYPE_U64_ARRAY, +}; + +#define __DEVLINK_PARAM_MAX_ARRAY_SIZE 32 +struct devlink_param_u64_array { +u64 size; +u64 val[__DEVLINK_PARAM_MAX_ARRAY_SIZE]; }; union devlink_param_value {
@@ -442,6 +449,7 @@ union devlink_param_value {u64 vu64;char vstr[__DEVLINK_PARAM_MAX_STRING_VALUE];bool vbool; +struct devlink_param_u64_array u64arr; };struct devlink_param_gset_ctx {
include/uapi/linux/devlink.h +1 -0
@@ -406,6 +406,7 @@ enum devlink_var_attr_type {DEVLINK_VAR_ATTR_TYPE_BINARY , __DEVLINK_VAR_ATTR_TYPE_CUSTOM_BASE =0x80 ,/* Any possible custom types, unrelated to NLA_* values go below */ + DEVLINK_VAR_ATTR_TYPE_U64_ARRAY, }; enum devlink_attr {
net/devlink/netlink_gen.c +2 -0
@@ -37,6 +37,8 @@ devlink_attr_param_type_validate(const struct nlattr *attr,case DEVLINK_VAR_ATTR_TYPE_NUL_STRING : fallthrough;case DEVLINK_VAR_ATTR_TYPE_BINARY : + fallthrough; +case DEVLINK_VAR_ATTR_TYPE_U64_ARRAY :return 0 ; }NL_SET_ERR_MSG_ATTR (extack, attr,"invalid enum value" );
net/devlink/param.c +32 -1
@@ -252,6 +252,15 @@ devlink_nl_param_value_put(struct sk_buff *msg, enum devlink_param_type type,return -EMSGSIZE ; }break ; +case DEVLINK_PARAM_TYPE_U64_ARRAY : +if (val->u64arr.size > __DEVLINK_PARAM_MAX_ARRAY_SIZE) +return -EMSGSIZE ; + +for (int i =0 ; i < val->u64arr.size; i++) { +if (nla_put_uint (msg, nla_type, val->u64arr.val[i])) +return -EMSGSIZE ; + } +break ; }return 0 ; }
@@ -537,7 +546,7 @@ devlink_param_value_get_from_info(const struct devlink_param *param, union devlink_param_value *value) {struct nlattr *param_data; -int len; +int len, cnt, rem; param_data = info->attrs[DEVLINK_ATTR_PARAM_VALUE_DATA ];
@@ -577,6 +586,28 @@ devlink_param_value_get_from_info(const struct devlink_param *param,return -EINVAL ; value->vbool =nla_get_flag (param_data);break ; + +case DEVLINK_PARAM_TYPE_U64_ARRAY : + cnt =0 ; +nla_for_each_attr_type (param_data, +DEVLINK_ATTR_PARAM_VALUE_DATA , +genlmsg_data (info->genlhdr), +genlmsg_len (info->genlhdr), rem) { +if (cnt >= __DEVLINK_PARAM_MAX_ARRAY_SIZE) +return -EMSGSIZE ; + +if ((nla_len (param_data) !=sizeof (u64)) && + (nla_len (param_data) !=sizeof (u32))) { +NL_SET_BAD_ATTR (info->extack, param_data); +return -EINVAL ; + } + + value->u64arr.val[cnt] =nla_get_uint (param_data); + cnt++; + } + + value->u64arr.size = cnt; +break ; }return 0 ; }
Signature · 1 line
2.43.0
Ratheesh Kannoth
maintainerPATCH v18 4/8 octeontx2-af: npc: cn20k: add subbank search order control
In reply to Ratheesh KannothCN20K NPC MCAM is split into 32 subbanks that are searched in a predefined order during allocation. Lower-numbered subbanks have higher priority than higher-numbered ones.
Add a runtime "srch_order" to control the order in which subbanks are searched during MCAM allocation.
--- .../ethernet/marvell/octeontx2/af/cn20k/npc.c | 120 +++++++++++++++++- .../ethernet/marvell/octeontx2/af/cn20k/npc.h | 3 + .../marvell/octeontx2/af/rvu_devlink.c | 92 ++++++++++++-- 3 files changed, 203 insertions(+), 12 deletions(-)
drivers/net/ethernet/marvell/octeontx2/af/cn20k/npc.c +118 -2
@@ -3376,7 +3376,7 @@ rvu_mbox_handler_npc_cn20k_get_kex_cfg(struct rvu *rvu,return 0 ; } -static int *subbank_srch_order; +static u32 *subbank_srch_order;static void npc_populate_restricted_idxs (int num_subbanks) {
@@ -3388,7 +3388,7 @@ static int npc_create_srch_order(int cnt) {int val =0 ; - subbank_srch_order =kcalloc (cnt,sizeof (int ), + subbank_srch_order =kcalloc (cnt,sizeof (u32),GFP_KERNEL );if (!subbank_srch_order)return -ENOMEM ;
@@ -3906,6 +3906,122 @@ static void npc_unlock_all_subbank(void)mutex_unlock (&npc_priv.sb[i].lock); } +int npc_cn20k_search_order_set (struct rvu *rvu, +u64 narr[MAX_NUM_SUB_BANKS ],int cnt) +{ +struct npc_mcam *mcam = &rvu->hw->mcam; +int rsrc[2 ][MAX_NUM_SUB_BANKS ] = { }; +u8 save[MAX_NUM_SUB_BANKS ] = { }; +struct npc_subbank *sb; +struct xarray *xa; +int prio, rc, err; +int sb_idx; +enum { +FREE =0 , +USED =1 , + }; + +if (cnt != npc_priv.num_subbanks) { +dev_err (rvu->dev,"Number of entries(%u) != %u\n" , + cnt, npc_priv.num_subbanks); +return -EINVAL ; + } + +mutex_lock (&mcam->lock); +npc_lock_all_subbank (); + +for (sb_idx =0 ; sb_idx < cnt; sb_idx++) { + sb = &npc_priv.sb[sb_idx]; + save[sb->idx] = sb->arr_idx; + } + +for (prio =0 ; prio < cnt; prio++) { + sb_idx = narr[prio]; + sb = &npc_priv.sb[sb_idx]; + +if (sb->flags &NPC_SUBBANK_FLAG_USED ) + xa = &npc_priv.xa_sb_used; +else + xa = &npc_priv.xa_sb_free; + + rc =xa_err (xa_store (xa, prio, +xa_mk_value (sb_idx),GFP_KERNEL )); +if (rc) { +dev_err (rvu->dev, +"Setting arr_idx=%d for sb=%d failed\n" , + sb->arr_idx, sb_idx); + goto fail; + } + +if (sb->flags &NPC_SUBBANK_FLAG_USED ) { + rsrc[USED ][sb->arr_idx] -=1 ; + rsrc[USED ][prio] +=1 ; + }else { + rsrc[FREE ][sb->arr_idx] -=1 ; + rsrc[FREE ][prio] +=1 ; + } + + sb->arr_idx = prio; + } + +for (prio =0 ; prio < cnt; prio++) { +if (rsrc[FREE ][prio] ==-1 ) +xa_erase (&npc_priv.xa_sb_free, prio); + +if (rsrc[USED ][prio] ==-1 ) +xa_erase (&npc_priv.xa_sb_used, prio); + } + +for (int i =0 ; i < cnt; i++) + subbank_srch_order[i] = (u32 )narr[i]; + + restrict_valid = false; + +npc_unlock_all_subbank (); +mutex_unlock (&mcam->lock); + +return 0 ; + +fail: +for (prio =0 ; prio < cnt; prio++) { +if (rsrc[FREE ][prio] ==1 ) +xa_erase (&npc_priv.xa_sb_free, prio); + +if (rsrc[USED ][prio] ==1 ) +xa_erase (&npc_priv.xa_sb_used, prio); + } + +for (sb_idx =0 ; sb_idx < cnt; sb_idx++) { + sb = &npc_priv.sb[sb_idx]; + sb->arr_idx = save[sb_idx]; + +if (sb->flags &NPC_SUBBANK_FLAG_USED ) + xa = &npc_priv.xa_sb_used; +else + xa = &npc_priv.xa_sb_free; + +/* Since the entry already exists, xa_store() replaces +* the value without a kmalloc(), making failure highly unlikely. +*/ + err =xa_err (xa_store (xa, sb->arr_idx, +xa_mk_value (sb->idx),GFP_KERNEL )); +WARN (!!err,"Failed to rollback sb=%u idx=%u\n" , + sb->idx, sb->arr_idx); + } + +npc_unlock_all_subbank (); +mutex_unlock (&mcam->lock); + +return rc; +} + +const u32 *npc_cn20k_search_order_get (bool *restricted_order,u32 *sz) +{ + *restricted_order = restrict_valid; + *sz = npc_priv.num_subbanks; +return subbank_srch_order; +} +/* Only non-ref non-contigous mcam indexes * are picked for defrag process */
drivers/net/ethernet/marvell/octeontx2/af/cn20k/npc.h +3 -0
@@ -347,5 +347,8 @@ bool npc_is_cgx_or_lbk(struct rvu *rvu, u16 pcifunc);int npc_mcam_idx_2_subbank_idx (struct rvu *rvu,u16 mcam_idx,struct npc_subbank **sb,int *sb_off); +const u32 *npc_cn20k_search_order_get (bool *restricted_order,u32 *sz); +int npc_cn20k_search_order_set (struct rvu *rvu,u64 narr[MAX_NUM_SUB_BANKS ], +int cnt);#endif /* NPC_CN20K_H */
drivers/net/ethernet/marvell/octeontx2/af/rvu_devlink.c +82 -10
131 lines not on this page · raw message
Signature · 1 line
2.43.0
Ratheesh Kannoth
maintainerRe: [PATCH v18 net-next 4/8] octeontx2-af: npc: cn20k: add subbank search order control
Quoted 8 lines · Ratheesh Kannoth
CN20K NPC MCAM is split into 32 subbanks that are searched in a predefined order during allocation. Lower-numbered subbanks have higher priority than higher-numbered ones.
Add a runtime "
srch_order" to control the order in which subbanks are searched during MCAM allocation.Signed-off-by Ratheesh Kannoth <[email protected]>
https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20260602060359.1894952-1-rkannoth%40marvell.com says
Quoted 15 lines
(quoted diff) +0
-0@@ -3906,6 +3906,122 @@ static void npc_unlock_all_subbank(void) > mutex_unlock(&npc_priv.sb[i].lock); > }drivers/net/ethernet/marvell/octeontx2/af/cn20k/npc.c static void npc_unlock_all_subbank(void) · line 3918
Quoted 11 lines
drivers/net/ethernet/marvell/octeontx2/af/cn20k/npc.c +10
-0+int npc_cn20k_search_order_set (struct rvu *rvu, +u64 narr[MAX_NUM_SUB_BANKS ],int cnt) +{ +struct npc_mcam *mcam = &rvu->hw->mcam; +int rsrc[2 ][MAX_NUM_SUB_BANKS ] = { }; +u8 save[MAX_NUM_SUB_BANKS ] = { }; +struct npc_subbank *sb; +struct xarray *xa; +int prio, rc, err; +int sb_idx;The rvu pointer is taken as an argument, but the state being mutated
Quoted 8 lines
(
subbank_srch_order,restrict_valid,npc_priv) lives in file-scope statics incn20k/npc.c. The rvu parameter is used only fordev_err()and to grabmcam->lock. Devlink parameters are per-device, so what happens on systems with more than one CN20K AF instance? It looks like settingnpc_srch_orderon one instance would silently apply to all of them, and a get on any instance would return whatever the last writer set globally. Was this intended, or should the state be moved under struct rvu (or its hw/npc container)?
There is only one AF device per system.
Quoted 6 lines
The save[] array stores
sb->arr_idx, butarr_idxis u16 in structnpc_subbankwhile save[] is declared u8. WithMAX_NUM_SUB_BANKS== 32 today the values fit, but is aBUILD_BUG_ONor a u16 declaration appropriate so that a future bump ofMAX_NUM_SUB_BANKSdoes not silently truncate restored values during rollback? The neighbouring rsrc[][] array is correctly typed int.
Maximum subbank available on highend SoC is 32.
Quoted 9 lines
drivers/net/ethernet/marvell/octeontx2/af/cn20k/npc.c static void npc_unlock_all_subbank(void) · line 3978
Quoted 4 lines
drivers/net/ethernet/marvell/octeontx2/af/cn20k/npc.c +4
-0+for (int i =0 ; i < cnt; i++) + subbank_srch_order[i] = (u32 )narr[i]; + + restrict_valid = false;
restrict_validis unconditionally set to false here on every successful set, and there is no path elsewhere that sets it back to true. The flag gates several other behaviours incn20k/npc.c:
-npc_subbank_restrict_usage()returns false oncerestrict_validis false (disabling the minimization of allocations from restricted
Quoted 8 lines
subbanks).
- The "Allocate from restricted subbanks" fallback loop is skipped.
-npc_defrag_skip_restricted_sb()always returns false.
Is it intended that the first userspace set ofnpc_srch_ordersilently disables the restricted-subbank usage-minimization feature and the restricted-subbank allocation fallback for the remaining lifetime of the driver? If so, could that be documented and exposed through the same uAPI so userspace can observe and revert it?
This is intentional. Once user change srch orer, "restrict_valid" is invalid.
Quoted 26 lines
Quoted 16 lines
drivers/net/ethernet/marvell/octeontx2/af/cn20k/npc.c +16
-0+fail: +for (prio =0 ; prio < cnt; prio++) { +if (rsrc[FREE ][prio] ==1 ) +xa_erase (&npc_priv.xa_sb_free, prio); + +if (rsrc[USED ][prio] ==1 ) +xa_erase (&npc_priv.xa_sb_used, prio); + } + +for (sb_idx =0 ; sb_idx < cnt; sb_idx++) { + sb = &npc_priv.sb[sb_idx]; + sb->arr_idx = save[sb_idx]; + +if (sb->flags &NPC_SUBBANK_FLAG_USED ) + xa = &npc_priv.xa_sb_used; +else drivers/net/ethernet/marvell/octeontx2/af/cn20k/npc.c static void npc_unlock_all_subbank(void) · line 4010
Quoted 10 lines
drivers/net/ethernet/marvell/octeontx2/af/cn20k/npc.c +10
-0+ xa = &npc_priv.xa_sb_free; + +/* Since the entry already exists, xa_store() replaces +* the value without a kmalloc(), making failure highly unlikely. +*/ + err =xa_err (xa_store (xa, sb->arr_idx, +xa_mk_value (sb->idx),GFP_KERNEL )); +WARN (!!err,"Failed to rollback sb=%u idx=%u\n" , + sb->idx, sb->arr_idx); + }
Quoted 8 lines
Is the comment above accurate on the failure path?
The first loop in fail: erases keys where rsrc[FLAG][prio] == 1, which are exactly the prio slots written by completed iterations. When the rollback loop then writes the saved subbanks back to those keys viaxa_store(xa, sb->arr_idx, ...), those keys are no longer present, so thexa_storehas to allocate a new node and is not a simple replace. Under memory pressure that allocation can return-ENOMEM, and the only response here isWARN().
rsrc[FLAG][prio] == 1 indicate entries which are newly allocated. So rolling back will delete those. Other slots do exist, so xa_store() wont fail due to memory pressure.
Quoted 13 lines
+static int rvu_af_dl_npc_srch_order_get(struct devlink *devlink, u32 id,
Quoted 12 lines
drivers/net/ethernet/marvell/octeontx2/af/rvu_devlink.c +12
-0+struct devlink_param_gset_ctx *ctx, + struct netlink_ext_ack *extack) +{ + bool restricted_order; +const u32 *order; +u32 sz; + + order =npc_cn20k_search_order_get (&restricted_order, &sz); + ctx->val.u64arr.size = sz; +for (int i =0 ; i < sz; i++) + ctx->val.u64arr.val[i] = order[i]; +
Quoted 8 lines
Review
Quoted 2 lines
(quoted diff) +2
-0+ return 0; +}The
restricted_orderout parameter is written here but never read. The same is true inrvu_af_dl_npc_srch_order_validate()below. Is the intention to expose whether the default (restricted) order or a user-provided order is currently active to userspace, perhaps via a separate read-only param? As written, the value never leaves the local stack, so userspace has no way to learn it.
if default srch_order is modified, restrict_order will be false. No need to read this value from userspace.
Ratheesh Kannoth
maintainerPATCH v18 5/8 octeontx2: cn20k: Coordinate default rules with NIX LF lifecycle
In reply to Ratheesh KannothAdd NIX_LF_DONT_FREE_DFT_IDXS so the PF can send NIX LF free during hw reinit or teardown without the AF freeing CN20K default NPC rule indexes while the driver still owns that state (otx2_init_hw_resources and otx2_free_hw_resources).
On CN20K, allocate default NPC rules from NIX LF alloc before nix_interface_init, roll back with npc_cn20k_dft_rules_free on failure, and free from NIX LF free when the new flag is not set. Tighten rvu_mbox_handler_nix_lf_alloc error handling: use a single rc, propagate qmem_alloc and other errors, and set -ENOMEM only when kcalloc fails (remove the blanket -ENOMEM at the free_mem path).
--- .../net/ethernet/marvell/octeontx2/af/mbox.h | 1 + .../ethernet/marvell/octeontx2/af/rvu_nix.c | 69 ++++++++++++------- .../ethernet/marvell/octeontx2/af/rvu_npc.c | 20 ++++-- .../ethernet/marvell/octeontx2/nic/otx2_pf.c | 6 +- 4 files changed, 61 insertions(+), 35 deletions(-)
drivers/net/ethernet/marvell/octeontx2/af/mbox.h +1 -0
@@ -1009,6 +1009,7 @@ struct nix_lf_free_req {struct mbox_msghdr hdr;#define NIX_LF_DISABLE_FLOWS BIT_ULL(0)#define NIX_LF_DONT_FREE_TX_VTAG BIT_ULL(1) +#define NIX_LF_DONT_FREE_DFT_IDXS BIT_ULL(2)u64 flags; };
drivers/net/ethernet/marvell/octeontx2/af/rvu_nix.c +44 -25
155 lines not on this page · raw message
drivers/net/ethernet/marvell/octeontx2/af/rvu_npc.c +14 -6
@@ -1285,11 +1285,18 @@ void npc_enadis_default_mce_entry(struct rvu *rvu, u16 pcifunc,struct nix_mce_list *mce_list;int index, blkaddr, mce_idx;struct rvu_pfvf *pfvf; +u16 ptr[4 ];/* multicast pkt replication is not enabled for AF's VFs & SDP links */ if (is_lbk_vf (rvu, pcifunc) ||is_sdp_pfvf (rvu, pcifunc))return ; +/* In cn20k, only CGX mapped devices have default MCAST entry */ +if (is_cn20k (rvu->pdev) && +npc_cn20k_dft_rules_idx_get (rvu, pcifunc, &ptr[0 ], &ptr[1 ], + &ptr[2 ], &ptr[3 ])) +return ; + blkaddr =rvu_get_blkaddr (rvu,BLKTYPE_NPC ,0 );if (blkaddr <0 )return ;
@@ -1329,9 +1336,12 @@ static void npc_enadis_default_entries(struct rvu *rvu, u16 pcifunc,struct rvu_pfvf *pfvf =rvu_get_pfvf (rvu, pcifunc);struct npc_mcam *mcam = &rvu->hw->mcam;int index, blkaddr; +u16 ptr[4 ];/* only CGX or LBK interfaces have default entries */ -if (is_cn20k (rvu->pdev) && !npc_is_cgx_or_lbk (rvu, pcifunc)) +if (is_cn20k (rvu->pdev) && +npc_cn20k_dft_rules_idx_get (rvu, pcifunc, &ptr[0 ], &ptr[1 ], + &ptr[2 ], &ptr[3 ]))return ; blkaddr =rvu_get_blkaddr (rvu,BLKTYPE_NPC ,0 );
@@ -4085,12 +4095,10 @@ void rvu_npc_clear_ucast_entry(struct rvu *rvu, int pcifunc, int nixlf) ucast_idx =npc_get_nixlf_mcam_index (mcam, pcifunc, nixlf,NIXLF_UCAST_ENTRY ); -if (ucast_idx <0 ) { -dev_err (rvu->dev, -"%s: Error to get ucast entry for pcifunc=%#x\n" , - __func__, pcifunc); + +/* In cn20k, default rules are freed before detach rsrc */ +if (ucast_idx <0 )return ; - }npc_enable_mcam_entry (rvu, mcam, blkaddr, ucast_idx, false);
drivers/net/ethernet/marvell/octeontx2/nic/otx2_pf.c +2 -4
@@ -1053,7 +1053,6 @@ irqreturn_t otx2_pfaf_mbox_intr_handler(int irq, void *pf_irq)/* Clear the IRQ */ otx2_write64 (pf,RVU_PF_INT ,BIT_ULL (0 )); - mbox_data =otx2_read64 (pf,RVU_PF_PFAF_MBOX0 );if (mbox_data &MBOX_UP_MSG ) {
@@ -1729,7 +1728,7 @@ int otx2_init_hw_resources(struct otx2_nic *pf)mutex_lock (&mbox->lock); free_req =otx2_mbox_alloc_msg_nix_lf_free (mbox);if (free_req) { - free_req->flags =NIX_LF_DISABLE_FLOWS ; + free_req->flags =NIX_LF_DISABLE_FLOWS |NIX_LF_DONT_FREE_DFT_IDXS ;if (otx2_sync_mbox_msg (mbox))dev_err (pf->dev,"%s failed to free nixlf\n" , __func__); }
@@ -1803,7 +1802,7 @@ void otx2_free_hw_resources(struct otx2_nic *pf)/* Reset NIX LF */ free_req =otx2_mbox_alloc_msg_nix_lf_free (mbox);if (free_req) { - free_req->flags =NIX_LF_DISABLE_FLOWS ; + free_req->flags =NIX_LF_DISABLE_FLOWS |NIX_LF_DONT_FREE_DFT_IDXS ;if (!(pf->flags &OTX2_FLAG_PF_SHUTDOWN )) free_req->flags |=NIX_LF_DONT_FREE_TX_VTAG ;if (otx2_sync_mbox_msg (mbox))
@@ -1926,7 +1925,6 @@ int otx2_alloc_queue_mem(struct otx2_nic *pf)struct otx2_qset *qset = &pf->qset;struct otx2_cq_poll *cq_poll; -/* RQ and SQs are mapped to different CQs, * so find out max CQ IRQs (i.e CINTs) needed. */
Signature · 1 line
2.43.0
Ratheesh Kannoth
maintainerRe: [PATCH v18 net-next 5/8] octeontx2: cn20k: Coordinate default rules with NIX LF lifecycle
Quoted 5 lines · Ratheesh Kannoth
Add
NIX_LF_DONT_FREE_DFT_IDXSso the PF can send NIX LF free during hw reinit or teardown without the AF freeing CN20K default NPC rule indexes while the driver still owns that state (otx2_init_hw_resourcesandotx2_free_hw_resources).
Quoted 8 lines
On CN20K, allocate default NPC rules from NIX LF alloc before
nix_interface_init, roll back withnpc_cn20k_dft_rules_freeon failure, and free from NIX LF free when the new flag is not set. Tightenrvu_mbox_handler_nix_lf_allocerror handling: use a single rc, propagateqmem_allocand other errors, and set-ENOMEMonly when kcalloc fails (remove the blanket-ENOMEMat thefree_mempath).Signed-off-by Ratheesh Kannoth <[email protected]>
https://sashiko.dev/#/patchset/20260602060359.1894952-1-rkannoth%40marvell.com says
Quoted 8 lines
Quoted 8 lines
drivers/net/ethernet/marvell/octeontx2/af/rvu_nix.c +2
-2/* Alloc NIX RQ HW context memory and config the base */ hwctx_size =1UL << ((ctx_cfg >>4 ) &0xF ); - err =qmem_alloc (rvu->dev, &pfvf->rq_ctx, req->rq_cnt, hwctx_size); -if (err) + rc =qmem_alloc (rvu->dev, &pfvf->rq_ctx, req->rq_cnt, hwctx_size); +if (rc) goto free_mem;
Quoted 13 lines
drivers/net/ethernet/marvell/octeontx2/af/rvu_nix.c int rvu_mbox_handler_nix_lf_alloc(struct rvu *rvu, · line 1578
Quoted 6 lines
drivers/net/ethernet/marvell/octeontx2/af/rvu_nix.c +3
-1pfvf->rq_bmap =kcalloc (req->rq_cnt,sizeof (long ),GFP_KERNEL ); -if (!pfvf->rq_bmap) +if (!pfvf->rq_bmap) { + rc = -ENOMEM ; goto free_mem; + }This is a pre-existing issue, but can repeated mailbox messages cause a memory leak here?
If an untrusted VF continuously sendsMBOX_MSG_NIX_LF_ALLOCmessages without sending a correspondingNIX_LF_FREE, it appearsqmem_alloc()andkcalloc()will overwrite the pointers in the pfvf structure (likepfvf->rq_ctxandpfvf->rq_bmap) without freeing the previously allocated memory. Could this be used by a guest to cause host DMA or heap memory exhaustion?
Prexisting issue, not addressing as part of this. May be a hardening series post this series, or patch to introduce some limit can be planned.
Quoted 28 lines
Quoted 8 lines
drivers/net/ethernet/marvell/octeontx2/af/rvu_nix.c +4
-0+ rc =nix_interface_init (rvu, pcifunc, intf, nixlf, rsp, + !!(req->flags &NIX_LF_LBK_BLK_SEL )); +if (rc) + goto free_dft;/* Disable NPC entries as NIXLF's contexts are not initialized yet */ rvu_npc_disable_default_entries (rvu, pcifunc, nixlf);drivers/net/ethernet/marvell/octeontx2/af/rvu_nix.c int rvu_mbox_handler_nix_lf_alloc(struct rvu *rvu, · line 1717
Quoted 6 lines
drivers/net/ethernet/marvell/octeontx2/af/rvu_nix.c +3
-0@@ -1699,9 +1712,12 @@ int rvu_mbox_handler_nix_lf_alloc(struct rvu *rvu, goto exit; +free_dft: +if (is_cn20k (rvu->pdev)) +npc_cn20k_dft_rules_free (rvu, pcifunc);Does this rollback path accidentally destroy pre-existing default rules? If the PF preserved the rules using
NIX_LF_DONT_FREE_DFT_IDXSduring teardown,npc_cn20k_dft_rules_alloc()returns 0 without allocating anything when they are already installed:npc_cn20k_dft_rules_alloc(struct rvu *rvu, u16 pcifunc){ ... ret = npc_cn20k_dft_rules_idx_get(rvu, pcifunc, &b, &m, &p, &u); if (!ret) { dev_dbg(rvu->dev, "default rules are already installed..."); return 0; } ... }
Quoted 2 lines
If
nix_interface_init()subsequently fails, the error handler jumps tofree_dftand unconditionally frees the rules.
Even if it is freed, next successful nix lf alloc, will setup these default rules.
We can modify npc_cn20k_dft_rules_alloc() to add one more paremeter (bool &) to indicate whether newly/already allocated status. Based on the flag, invoke npc_cn20k_dft_rules_free(). Will take it up as hardening series after the series is merged.
Ratheesh Kannoth
maintainerRe: [PATCH v18 net-next 5/8] octeontx2: cn20k: Coordinate default rules with NIX LF lifecycle
In reply to Ratheesh KannothQuoted 5 lines · Ratheesh Kannoth
Add
NIX_LF_DONT_FREE_DFT_IDXSso the PF can send NIX LF free during hw reinit or teardown without the AF freeing CN20K default NPC rule indexes while the driver still owns that state (otx2_init_hw_resourcesandotx2_free_hw_resources).
Quoted 8 lines
On CN20K, allocate default NPC rules from NIX LF alloc before
nix_interface_init, roll back withnpc_cn20k_dft_rules_freeon failure, and free from NIX LF free when the new flag is not set. Tightenrvu_mbox_handler_nix_lf_allocerror handling: use a single rc, propagateqmem_allocand other errors, and set-ENOMEMonly when kcalloc fails (remove the blanket-ENOMEMat thefree_mempath).Signed-off-by Ratheesh Kannoth <[email protected]>
https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20260602060359.1894952-1-rkannoth%40marvell.com says:
Quoted 4 lines
4.
nix_interface_init()then callsnix_update_mce_rule(NIXLF_BCAST_ENTRY, true), which can fail (for example-EINVALfromnpc_get_nixlf_mcam_indexin the cn20k path, or kzalloc failure innix_update_mce_list).
Quoted 8 lines
5. The new error path goto
free_dft->npc_cn20k_dft_rules_free()walksmcam->mcam_rulesand kfree()s every rule whose entry matches one of the freed dft indexes, including the rule installed in step 3.
6.pfvf->def_ucast_ruleis not cleared, so it now points at freed memory.
Subsequent mbox traffic for the same pcifunc dereferences that dangling pointer in places like:
If nix lf alloc fails, subsequent mbox traffic wont come for that Pcifunc. Next nix_lf alloc, would alloc default mcam rules. Practically this is not possible. I will add an hardening patchseries to remove the idempotency attribute of npc_cn20k_dft_rules_alloc() after this series is merged.
Ratheesh Kannoth
maintainerPATCH v18 6/8 octeontx2-af: npc: Support for custom KPU profile from filesystem
In reply to Ratheesh KannothFlashing updated firmware on deployed devices is cumbersome. Provide a mechanism to load a custom KPU (Key Parse Unit) profile directly from the filesystem at module load time.
When the rvu_af module is loaded with the kpu_profile parameter, the specified profile is read from /lib/firmware/kpu and programmed into the KPU registers. Add npc_kpu_profile_cam2 for the extended cam format used by filesystem-loaded profiles and support ptype/ptype_mask in npc_config_kpucam when profile->from_fs is set.
Usage:
1. Copy the KPU profile file to /lib/firmware/kpu.
2. Build OCTEONTX2_AF as a module.
3. Load: insmod rvu_af.ko kpu_profile=<profile_name>
--- .../ethernet/marvell/octeontx2/af/cn20k/npc.c | 57 ++- .../net/ethernet/marvell/octeontx2/af/npc.h | 17 + .../net/ethernet/marvell/octeontx2/af/rvu.h | 12 +- .../ethernet/marvell/octeontx2/af/rvu_npc.c | 466 ++++++++++++++---- .../ethernet/marvell/octeontx2/af/rvu_npc.h | 17 + .../ethernet/marvell/octeontx2/af/rvu_reg.h | 1 + 6 files changed, 459 insertions(+), 111 deletions(-)
drivers/net/ethernet/marvell/octeontx2/af/cn20k/npc.c +40 -17
@@ -521,13 +521,17 @@ npc_program_single_kpm_profile(struct rvu *rvu, int blkaddr, int kpm, int start_entry, const struct npc_kpu_profile *profile) { + int num_cam_entries, num_action_entries;int entry, num_entries, max_entries;u64 idx; -if (profile->cam_entries != profile->action_entries) { + num_cam_entries =npc_get_num_kpu_cam_entries (rvu, profile); + num_action_entries =npc_get_num_kpu_action_entries (rvu, profile); + +if (num_cam_entries != num_action_entries) {dev_err (rvu->dev,"kpm%d: CAM and action entries [%d != %d] not equal\n" , - kpm, profile->cam_entries, profile->action_entries); + kpm, num_cam_entries, num_action_entries);WARN (1 ,"Fatal error\n" );return ;
@@ -536,16 +540,18 @@ npc_program_single_kpm_profile(struct rvu *rvu, int blkaddr, max_entries = rvu->hw->npc_kpu_entries /2 ; entry = start_entry;/* Program CAM match entries for previous kpm extracted data */ - num_entries =min_t (int, profile->cam_entries, max_entries); + num_entries =min_t (int, num_cam_entries, max_entries);for (idx =0 ; entry < num_entries + start_entry; entry++, idx++) -npc_config_kpmcam (rvu, blkaddr, &profile->cam[idx], +npc_config_kpmcam (rvu, blkaddr, +npc_get_kpu_cam_nth_entry (rvu, profile, idx), kpm, entry); entry = start_entry;/* Program this kpm's actions */ - num_entries =min_t (int, profile->action_entries, max_entries); + num_entries =min_t (int, num_action_entries, max_entries);for (idx =0 ; entry < num_entries + start_entry; entry++, idx++) -npc_config_kpmaction (rvu, blkaddr, &profile->action[idx], +npc_config_kpmaction (rvu, blkaddr, +npc_get_kpu_action_nth_entry (rvu, profile, idx), kpm, entry, false); }
@@ -611,20 +617,23 @@ npc_enable_kpm_entry(struct rvu *rvu, int blkaddr, int kpm, int num_entries)static void npc_program_kpm_profile (struct rvu *rvu,int blkaddr,int num_kpms) {const struct npc_kpu_profile *profile1, *profile2; +int pfl1_num_cam_entries, pfl2_num_cam_entries;int idx, total_cam_entries;for (idx =0 ; idx < num_kpms; idx++) { profile1 = &rvu->kpu.kpu[idx]; + pfl1_num_cam_entries =npc_get_num_kpu_cam_entries (rvu, profile1);npc_program_single_kpm_profile (rvu, blkaddr, idx,0 , profile1); profile2 = &rvu->kpu.kpu[idx +KPU_OFFSET ]; + pfl2_num_cam_entries =npc_get_num_kpu_cam_entries (rvu, profile2); +npc_program_single_kpm_profile (rvu, blkaddr, idx, - profile1->cam_entries, + pfl1_num_cam_entries, profile2); - total_cam_entries = profile1->cam_entries + - profile2->cam_entries; + total_cam_entries = pfl1_num_cam_entries + pfl2_num_cam_entries;npc_enable_kpm_entry (rvu, blkaddr, idx, total_cam_entries);rvu_write64 (rvu, blkaddr,NPC_AF_KPMX_PASS2_OFFSET (idx), - profile1->cam_entries); + pfl1_num_cam_entries);/* Enable the KPUs associated with this KPM */ rvu_write64 (rvu, blkaddr,NPC_AF_KPUX_CFG (idx),0x01 ); rvu_write64(rvu, blkaddr, NPC_AF_KPUX_CFG(idx + KPU_OFFSET),
@@ -634,6 +643,7 @@ static void npc_program_kpm_profile(struct rvu *rvu, int blkaddr, int num_kpms)void npc_cn20k_parser_profile_init (struct rvu *rvu,int blkaddr) { +struct npc_kpu_profile_action *act;struct rvu_hwinfo *hw = rvu->hw;int num_pkinds, idx;
@@ -665,9 +675,15 @@ void npc_cn20k_parser_profile_init(struct rvu *rvu, int blkaddr) num_pkinds = rvu->kpu.pkinds; num_pkinds =min_t (int, hw->npc_pkinds, num_pkinds); -for (idx =0 ; idx < num_pkinds; idx++) -npc_config_kpmaction (rvu, blkaddr, &rvu->kpu.ikpu[idx], +/* Cn20k does not support Custom profile from filesystem */ +for (idx =0 ; idx < num_pkinds; idx++) { + act =npc_get_ikpu_nth_entry (rvu, idx); +if (!act) +continue ; + +npc_config_kpmaction (rvu, blkaddr, act,0 , idx, true); + }/* Program KPM CAM and Action profiles */ npc_program_kpm_profile (rvu, blkaddr, hw->npc_kpms);
@@ -679,7 +695,7 @@ struct npc_priv_t *npc_priv_get(void) }static void npc_program_mkex_rx (struct rvu *rvu,int blkaddr, -struct npc_mcam_kex_extr *mkex_extr, +const struct npc_mcam_kex_extr *mkex_extr,u8 intf) {u8 num_extr = rvu->hw->npc_kex_extr;
@@ -708,7 +724,7 @@ static void npc_program_mkex_rx(struct rvu *rvu, int blkaddr, }static void npc_program_mkex_tx (struct rvu *rvu,int blkaddr, -struct npc_mcam_kex_extr *mkex_extr, +const struct npc_mcam_kex_extr *mkex_extr,u8 intf) {u8 num_extr = rvu->hw->npc_kex_extr;
@@ -737,7 +753,7 @@ static void npc_program_mkex_tx(struct rvu *rvu, int blkaddr, }static void npc_program_mkex_profile (struct rvu *rvu,int blkaddr, -struct npc_mcam_kex_extr *mkex_extr) +const struct npc_mcam_kex_extr *mkex_extr) {struct rvu_hwinfo *hw = rvu->hw;u8 intf;
@@ -1630,8 +1646,8 @@ npc_cn20k_update_action_entries_n_flags(struct rvu *rvu,int npc_cn20k_apply_custom_kpu (struct rvu *rvu,struct npc_kpu_profile_adapter *profile) { +const struct npc_cn20k_kpu_profile_fwdata *fw = rvu->kpu_fwdata;size_t hdr_sz =sizeof (struct npc_cn20k_kpu_profile_fwdata ); -struct npc_cn20k_kpu_profile_fwdata *fw = rvu->kpu_fwdata;struct npc_kpu_profile_action *action;struct npc_kpu_profile_cam *cam;struct npc_kpu_fwdata *fw_kpu;
@@ -1676,8 +1692,15 @@ int npc_cn20k_apply_custom_kpu(struct rvu *rvu, }/* Verify if profile fits the HW */ +if (fw->kpus > rvu->hw->npc_kpus) { +dev_warn (rvu->dev,"Not enough KPUs: %d > %d\n" , fw->kpus, + rvu->hw->npc_kpus); +return -EINVAL ; + } + +/* Check if there is enough memory */ if (fw->kpus > profile->kpus) { -dev_warn (rvu->dev,"Not enough KPUs: %d > %ld\n" , fw->kpus, +dev_warn (rvu->dev,"Not enough KPUs: %d > %zu\n" , fw->kpus, profile->kpus);return -EINVAL ; }
drivers/net/ethernet/marvell/octeontx2/af/npc.h +17 -0
@@ -265,6 +265,19 @@ struct npc_kpu_profile_cam {u16 dp2_mask; } __packed; +struct npc_kpu_profile_cam2 { +u8 state; +u8 state_mask; +u16 dp0; +u16 dp0_mask; +u16 dp1; +u16 dp1_mask; +u16 dp2; +u16 dp2_mask; +u8 ptype; +u8 ptype_mask; +} __packed; +struct npc_kpu_profile_action {u8 errlev;u8 errcode;
@@ -290,6 +303,10 @@ struct npc_kpu_profile {int action_entries;struct npc_kpu_profile_cam *cam;struct npc_kpu_profile_action *action; +int cam_entries2; +int action_entries2; +struct npc_kpu_profile_action *action2; +struct npc_kpu_profile_cam2 *cam2; };/* NPC KPU register formats */
drivers/net/ethernet/marvell/octeontx2/af/rvu.h +7 -5
@@ -553,17 +553,19 @@ struct npc_kpu_profile_adapter {const char *name;u64 version;const struct npc_lt_def_cfg *lt_def; -const struct npc_kpu_profile_action *ikpu;/* array[pkinds] */ -const struct npc_kpu_profile *kpu;/* array[kpus] */ +struct npc_kpu_profile_action *ikpu;/* array[pkinds] */ +struct npc_kpu_profile_action *ikpu2;/* array[pkinds] */ +struct npc_kpu_profile *kpu;/* array[kpus] */ union npc_mcam_key_prfl { -struct npc_mcam_kex *mkex; +const struct npc_mcam_kex *mkex;/* used for cn9k and cn10k */ -struct npc_mcam_kex_extr *mkex_extr;/* used for cn20k */ +const struct npc_mcam_kex_extr *mkex_extr;/* used for cn20k */ } mcam_kex_prfl;struct npc_mcam_kex_hash *mkex_hash;bool custom;size_t pkinds;size_t kpus; +bool from_fs; };#define RVU_SWITCH_LBK_CHAN 63
@@ -634,7 +636,7 @@ struct rvu {/* Firmware data */ struct rvu_fwdata *fwdata; -void *kpu_fwdata; +const void *kpu_fwdata;size_t kpu_fwdata_sz;void __iomem *kpu_prfl_addr;
drivers/net/ethernet/marvell/octeontx2/af/rvu_npc.c +377 -89
622 lines not on this page · raw message
drivers/net/ethernet/marvell/octeontx2/af/rvu_npc.h +17 -0
@@ -18,4 +18,21 @@ int npc_fwdb_prfl_img_map(struct rvu *rvu, void __iomem **prfl_img_addr,void npc_mcam_clear_bit (struct npc_mcam *mcam,u16 index);void npc_mcam_set_bit (struct npc_mcam *mcam,u16 index); + +struct npc_kpu_profile_action * +npc_get_ikpu_nth_entry (struct rvu *rvu,int n); + +int +npc_get_num_kpu_cam_entries (struct rvu *rvu, +const struct npc_kpu_profile *kpu_pfl); +struct npc_kpu_profile_cam * +npc_get_kpu_cam_nth_entry (struct rvu *rvu, +const struct npc_kpu_profile *kpu_pfl,int n); + +int +npc_get_num_kpu_action_entries (struct rvu *rvu, +const struct npc_kpu_profile *kpu_pfl); +struct npc_kpu_profile_action * +npc_get_kpu_action_nth_entry (struct rvu *rvu, +const struct npc_kpu_profile *kpu_pfl,int n);#endif /* RVU_NPC_H */
drivers/net/ethernet/marvell/octeontx2/af/rvu_reg.h +1 -0
@@ -596,6 +596,7 @@#define NPC_AF_INTFX_KEX_CFG (a) (0x01010 | (a) << 8)#define NPC_AF_PKINDX_ACTION0 (a) (0x80000ull | (a) << 6)#define NPC_AF_PKINDX_ACTION1 (a) (0x80008ull | (a) << 6) +#define NPC_AF_PKINDX_TYPE (a) (0x80010ull | (a) << 6)#define NPC_AF_PKINDX_CPI_DEFX (a, b) (0x80020ull | (a) << 6 | (b) << 3)#define NPC_AF_KPUX_ENTRYX_CAMX (a, b, c) \ (0x100000 | (a) << 14 | (b) << 6 | (c) << 3)
Signature · 1 line
2.43.0
Ratheesh Kannoth
maintainerRe: [PATCH v18 net-next 6/8] octeontx2-af: npc: Support for custom KPU profile from filesystem
Quoted 10 lines · Ratheesh Kannoth
Flashing updated firmware on deployed devices is cumbersome. Provide a mechanism to load a custom KPU (Key Parse Unit) profile directly from the filesystem at module load time.
When the
rvu_afmodule is loaded with thekpu_profileparameter, the specified profile is read from/lib/firmware/kpuand programmed into the KPU registers. Addnpc_kpu_profile_cam2for the extended cam format used by filesystem-loaded profiles and support ptype/ptype_mask innpc_config_kpucamwhenprofile->from_fsis set.
Quoted 6 lines
Usage:
1. Copy the KPU profile file to/lib/firmware/kpu.
2. BuildOCTEONTX2_AFas a module.
3. Load: insmod rvu_af.ko kpu_profile=<profile_name>Signed-off-by Ratheesh Kannoth <[email protected]>
https://sashiko.dev/#/patchset/20260602060359.1894952-1-rkannoth%40marvell.com says
Quoted 11 lines
+static int npc_alloc_kpu_cam2_n_action2(struct rvu *rvu, int
kpu_num,drivers/net/ethernet/marvell/octeontx2/af/rvu_npc.c static void npc_prepare_default_kpu(struct rvu *rvu, · line 1884
Quoted 9 lines
drivers/net/ethernet/marvell/octeontx2/af/rvu_npc.c +9
-0+ int num_entries) +{ +struct npc_kpu_profile_adapter *adapter = &rvu->kpu; +struct npc_kpu_profile *kpu; + + kpu = &adapter->kpu[kpu_num]; + + kpu->cam2 =devm_kcalloc (rvu->dev, num_entries, +sizeof (*kpu->cam2),GFP_KERNEL );Does this code introduce a use-after-free risk?
No.
Quoted 7 lines
The adapter's kpu pointer (
profile->kpu) is set to the global static arraynpc_kpu_profilesinnpc_prepare_default_kpu(). When this function is called, it allocates device-managed memory (devm_kcalloc) and stores the pointer into that globally shared array.
If the device unbinds, the devres subsystem will automatically free this memory, leaving dangling pointers in the global array. A subsequent probe or concurrent access by another device could result in a use-after-free.
There is only one AF device per system.
When device binds, the structure which holds these pointers (global array) are also freed.
Quoted 11 lines
drivers/net/ethernet/marvell/octeontx2/af/rvu_npc.c +1
-0+struct npc_kpu_fwdata *fw_kpu;drivers/net/ethernet/marvell/octeontx2/af/rvu_npc.c static void npc_prepare_default_kpu(struct rvu *rvu, · line 1909
Quoted 8 lines
drivers/net/ethernet/marvell/octeontx2/af/rvu_npc.c +5
-2+int entries, entry, kpu; -if (is_cn20k (rvu->pdev)) -return npc_cn20k_apply_custom_kpu (rvu, profile); + fw = rvu->kpu_fwdata; + +for (kpu =0 ; kpu < fw->kpus; kpu++) { +if (rvu->kpu_fwdata_sz < hdr_sz + offset) {This is a pre-existing issue, but does this boundary check allow an out-of-bounds read?
Pre-existing issue, will post a patch to net tree, once this series is merged.
Quoted 7 lines
(quoted diff) +1
-0+ return -EINVAL;drivers/net/ethernet/marvell/octeontx2/af/rvu_npc.c static void npc_prepare_default_kpu(struct rvu *rvu, · line 1976
Quoted 6 lines
drivers/net/ethernet/marvell/octeontx2/af/rvu_npc.c +6
-0+ +/* The firmware layout does dependent on the internal size of +* ikpu_action_entries. +*/ +memcpy ((void *)profile->ikpu2, action,sizeof (ikpu_action_entries)); + offset +=sizeof (ikpu_action_entries);
Quoted 8 lines
Will this logic break backward compatibility with existing firmware binaries? The firmware parsing offset is incremented by
sizeof(ikpu_action_entries). Becauseikpu_action_entriesis a static kernel array whose size is determined by the number of supported port kinds (PKINDs), any future addition of a PKIND to the kernel will increase the array's size.
This would misalign the parser's offset calculation for all existing firmware binaries on disk. Could this be made independent of the internal kernel structure size?
No, Since there is no field in this structure to indicate number of ikpu_action_entries, we will keep same number of entries always.
Quoted 5 lines
Quoted 5 lines
drivers/net/ethernet/marvell/octeontx2/af/rvu_npc.c +1
-0+const struct npc_mcam_kex *mkex;u64 nibble_ena, rx_kex, tx_kex;u64 *keyx_cfg, reg;u8 intf;
Quoted 15 lines
drivers/net/ethernet/marvell/octeontx2/af/rvu_npc.c static void rvu_npc_hw_init(struct rvu *rvu, int blkaddr) · line 2597
Quoted 6 lines
drivers/net/ethernet/marvell/octeontx2/af/rvu_npc.c +4
-1+ mkex_extr = rvu->kpu.mcam_kex_prfl.mkex_extr; + mkex = rvu->kpu.mcam_kex_prfl.mkex; +if (is_cn20k (rvu->pdev)) { - keyx_cfg = mkex_extr->keyx_cfg; + keyx_cfg = (u64 *)mkex_extr->keyx_cfg;This is a pre-existing issue, but does this explicit cast mask a potential kernel panic?
By casting the const pointermkex_extr->keyx_cfgto a non-const u64 *, the code allows mutating the underlying data later in this function (e.g., keyx_cfg[NIX_INTF_TX] =tx_kex).
If the profile was loaded via the firmware database (fwdb), this memory is an__iomemregion. A direct assignment to__iomemon ARM64 without using the appropriate I/O accessors (like writeq) can cause a synchronous external abort.
This is firmware loaded in memory (request_firmware_direct()), not iomem.
Ratheesh Kannoth
maintainerRe: [PATCH v18 net-next 6/8] octeontx2-af: npc: Support for custom KPU profile from filesystem
In reply to Ratheesh KannothQuoted 10 lines · Ratheesh Kannoth
Flashing updated firmware on deployed devices is cumbersome. Provide a mechanism to load a custom KPU (Key Parse Unit) profile directly from the filesystem at module load time.
When the
rvu_afmodule is loaded with thekpu_profileparameter, the specified profile is read from/lib/firmware/kpuand programmed into the KPU registers. Addnpc_kpu_profile_cam2for the extended cam format used by filesystem-loaded profiles and support ptype/ptype_mask innpc_config_kpucamwhenprofile->from_fsis set.
Quoted 6 lines
Usage:
1. Copy the KPU profile file to/lib/firmware/kpu.
2. BuildOCTEONTX2_AFas a module.
3. Load: insmod rvu_af.ko kpu_profile=<profile_name>Signed-off-by Ratheesh Kannoth <[email protected]>
https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20260602060359.1894952-1-rkannoth%40marvell.com says
Quoted 10 lines
Quoted 10 lines
drivers/net/ethernet/marvell/octeontx2/af/cn20k/npc.c +7
-2-for (idx =0 ; idx < num_pkinds; idx++) -npc_config_kpmaction (rvu, blkaddr, &rvu->kpu.ikpu[idx], +/* Cn20k does not support Custom profile from filesystem */ +for (idx =0 ; idx < num_pkinds; idx++) { + act =npc_get_ikpu_nth_entry (rvu, idx); +if (!act) +continue ; + +npc_config_kpmaction (rvu, blkaddr, act,0 , idx, true);
Quoted 8 lines
Review
Quoted 1 line
(quoted diff) +1
-0+ }Can act ever be NULL on this path?
npc_get_ikpu_nth_entry()returns either &profile->ikpu2[n] (whenfrom_fsis true) or &profile->ikpu[n], neither of which can be NULL. And as the comment above notes, cn20k does not take thefrom_fspath at all (it is gated by !is_cn20k() innpc_load_kpu_profile()), so ikpu always points at the static ikpu_action_entries[].
If that is the case, the if (!act) continue check is dead.
I agree, but there is a plan to add it in future, so would like to keep this check
Quoted 20 lines
drivers/net/ethernet/marvell/octeontx2/af/rvu_npc.c static void npc_config_kpucam(struct rvu *rvu, int blkaddr, · line 1721
Quoted 7 lines
drivers/net/ethernet/marvell/octeontx2/af/rvu_npc.c +7
-0+if (profile->from_fs) { +u8 ptype = kpucam2->ptype; +u8 pmask = kpucam2->ptype_mask; + + *val |=FIELD_PREP (GENMASK_ULL (57 ,56 ), ptype & pmask); + *mask |=FIELD_PREP (GENMASK_ULL (57 ,56 ), ~ptype & pmask); + }The cast const struct
npc_kpu_profile_cam2*kpucam2 = (void *)kpucam is unconditional, but the function's parameter is typed as
structnpc_kpu_profile_cam*. The two structures differ in size
(12 vs 14 bytes packed).
The reads ofkpucam2->ptypeandkpucam2->ptype_maskare gated byprofile->from_fs, but the gate relies on a cross-function invariant:
thefrom_fsflag must imply thatnpc_get_kpu_cam_nth_entry()was used and returned a cam2-typed element. There is no static assertion or runtime check tying these two together.
Could aBUILD_BUG_ONensuringoffsetof(struct npc_kpu_profile_cam2, ptype)==sizeof(struct npc_kpu_profile_cam)be added so that a future layout change is caught at compile time? The bits that ptype/ptype_mask are written into are bits 57:56 of cam0
Quoted 8 lines
and cam1, but struct
npc_kpu_caminnpc.hstill declares those bits as part ofrsvd_63_56: 8 in both endianness branches. Should the cam struct be updated with an explicit ptype : 2 /rsvd_63_58: 6 split (or similar) so the contract that those two reserved bits are now live is visible at the struct definition?
Is there also a way to assert that the silicon variants on which the filesystem profile path runs (cn9k, cn10k) actually decode bits 57:56 as ptype-match?
The rsvd_63_56 field in struct npc_kpu_cam remains intentionally generic because this shared structure header is consumed across multiple legacy silicon generations where those bits are strictly reserved. Modifying the base bitfield definition would break backward compatibility metrics for older targets. The localized use of FIELD_PREP(GENMASK_ULL(57, 56), ...) inside the from_fs block serves as the explicit.
Quoted 11 lines
drivers/net/ethernet/marvell/octeontx2/af/rvu_npc.c static void npc_prepare_default_kpu(struct rvu *rvu, · line 1894
Quoted 9 lines
drivers/net/ethernet/marvell/octeontx2/af/rvu_npc.c +9
-0+return -ENOMEM ; + + kpu->action2 =devm_kcalloc (rvu->dev, num_entries, +sizeof (*kpu->action2),GFP_KERNEL ); +if (!kpu->action2) +return -ENOMEM ; + +return 0 ; +}Could this function be storing per-device devm allocations in module-global static state?
Quoted 8 lines
adapter->kpuis set bynpc_prepare_default_kpu()tonpc_kpu_profilesfromnpc_profile.h, which is a static module-global array.kpu->cam2andkpu->action2aredevm_kcalloc(rvu->dev, ...)so the underlying memory is freed at device release, but the pointers (and the cam_entries2/action_entries2 counters set innpc_apply_custom_kpu_from_fs()) remain in the static array. After unbind, would a subsequent rebind that uses the default profile or thefrom_fwpath leave dangling pointers and non-zero
There is only one AF device per system.
Quoted 24 lines
+static int npc_apply_custom_kpu_from_fw(struct rvu *rvu,
Quoted 23 lines
drivers/net/ethernet/marvell/octeontx2/af/rvu_npc.c +12
-5+ struct npc_kpu_profile_adapter *profile) {size_t hdr_sz =sizeof (struct npc_kpu_profile_fwdata ), offset =0 ; +const struct npc_kpu_profile_fwdata *fw;struct npc_kpu_profile_action *action; -struct npc_kpu_profile_fwdata *fw;struct npc_kpu_profile_cam *cam;struct npc_kpu_fwdata *fw_kpu; -int entries; -u16 kpu, entry; +int entries, entry, kpu; -if (is_cn20k (rvu->pdev)) -return npc_cn20k_apply_custom_kpu (rvu, profile); + fw = rvu->kpu_fwdata; + +for (kpu =0 ; kpu < fw->kpus; kpu++) { +if (rvu->kpu_fwdata_sz < hdr_sz + offset) { +dev_warn (rvu->dev, +"Profile size mismatch on KPU%i parsing\n" , + kpu +1 ); +return -EINVAL ; + }
Quoted 8 lines
drivers/net/ethernet/marvell/octeontx2/af/rvu_npc.c static void npc_prepare_default_kpu(struct rvu *rvu, · line 1917
Quoted 3 lines
drivers/net/ethernet/marvell/octeontx2/af/rvu_npc.c +3
-0+ + fw_kpu = (struct npc_kpu_fwdata *)(fw->data + offset); +if (fw_kpu->entries <0 ) {Is the size check above missing
sizeof(*fw_kpu)? The check guards that offset is reachable, but the next statement readsfw_kpu->entrieswhich is atfw->data+ offset + 0..3. Whenkpu_fwdata_szequalshdr_sz+ offset exactly, the read offw_kpu->entriesis past the end of the firmware buffer.
This is a prexisintg issue in the code, will fix it as part of patch to "net" tree once this series is merged.
Quoted 12 lines
Quoted 12 lines
drivers/net/ethernet/marvell/octeontx2/af/rvu_npc.c +9
-0fw = rvu->kpu_fwdata; +/* Binary blob contains ikpu actions entries at start of data[0] */ + profile->ikpu2 =devm_kcalloc (rvu->dev,1 , +sizeof (ikpu_action_entries), +GFP_KERNEL ); +if (!profile->ikpu2) +return -ENOMEM ; + + action = (struct npc_kpu_profile_action *)(fw->data + offset); +
Quoted 8 lines
Review
Quoted 2 lines
(quoted diff) +2
-0+ if (rvu->kpu_fwdata_sz < hdr_sz + sizeof(ikpu_action_entries)) + return -EINVAL;Should the size validation happen before the devm allocation? When
kpu_fwdata_szis too small the function returns-EINVALwithprofile->ikpu2already allocated and stored. Devm reclaims at device release, but on every malformed-firmware probe a small buffer is left orphaned and reachable throughprofile->ikpu2(withfrom_fsreset to false on the fallback path, so it is not currently dereferenced).
There is only one AF device per system and this loading of profiles is done during driver init() time.
Quoted 6 lines
drivers/net/ethernet/marvell/octeontx2/af/rvu_npc.c static void npc_prepare_default_kpu(struct rvu *rvu, · line 1976
Quoted 6 lines
drivers/net/ethernet/marvell/octeontx2/af/rvu_npc.c +6
-0+ +/* The firmware layout does dependent on the internal size of +* ikpu_action_entries. +*/ +memcpy ((void *)profile->ikpu2, action,sizeof (ikpu_action_entries)); + offset +=sizeof (ikpu_action_entries);
Quoted 8 lines
Is the firmware ABI here tied to a kernel-internal sizeof? ikpu_action_entries[] is a static array in
npc_profile.h. The filesystem blob has no field declaring how many ikpu entries it contains, so the offset advance depends on the kernel build. If a future kernel adds a pkind toikpu_action_entries, every existing filesystem blob will be silently misparsed (offset advances by the wrong amount, then cam entries are read from the wrong location) even though the major version check still passes.
We have support only for little endian. And ikpu_action_entries structure does not have a fieled to hold "size" currently. we cant add it now as it would break backward compatability.
Quoted 20 lines
drivers/net/ethernet/marvell/octeontx2/af/rvu_npc.c static void npc_prepare_default_kpu(struct rvu *rvu, · line 2046
Quoted 11 lines
drivers/net/ethernet/marvell/octeontx2/af/rvu_npc.c +2
-0return npc_cn20k_apply_custom_kpu (rvu, profile);if (rvu->kpu_fwdata_sz < hdr_sz) {dev_warn (rvu->dev,"Invalid KPU profile size\n" );return -EINVAL ; } + + fw = rvu->kpu_fwdata;if (le64_to_cpu (fw->signature) !=KPU_SIGN ) {dev_warn (rvu->dev,"Invalid KPU profile signature %llx\n" , fw->signature);The new filesystem-loaded blob and the existing fwdb blob share the same
KPU_SIGNsignature and the same structnpc_kpu_profile_fwdataheader, but data[] has a different binary layout: the filesystem format prepends asizeof(ikpu_action_entries)block of ikpu actions and usesnpc_kpu_profile_cam2(16 bytes packed) instead ofnpc_kpu_profile_cam(14 bytes packed).
Dispatch is decided entirely by the loader path:return from_fs ? npc_apply_custom_kpu_from_fs(rvu, profile) : npc_apply_custom_kpu_from_fw(rvu, profile);
Quoted 7 lines
with nothing in the file itself indicating which layout applies. If a fwdb-formatted blob is dropped into /lib/firmware/kpu/<name>, signature and version checks pass, then the parser silently misinterprets the bytes, programming arbitrary values plus enabled ptype-match bits into the KPU CAM and PKIND ACTION CSRs. Should the on-disk header carry a format/cam_format_version field, or should the cam2 layout use a distinct signature?
This is intentional as per customer requirement.
Quoted 6 lines
Quoted 6 lines
drivers/net/ethernet/marvell/octeontx2/af/rvu_npc.c +2
-2+ profile->custom =1 ; - profile->name = fw->name; + profile->name = sfw->name; profile->version =le64_to_cpu (fw->version); - profile->mcam_kex_prfl.mkex = &fw->mkex;
Quoted 8 lines
drivers/net/ethernet/marvell/octeontx2/af/rvu_npc.c static int npc_apply_custom_kpu(struct rvu *rvu, · line 1848
Quoted 1 line
drivers/net/ethernet/marvell/octeontx2/af/rvu_npc.c +0
-1- profile->lt_def = &fw->lt_def;If
npc_load_kpu_profile_from_fs()runs first and fails, thennpc_load_kpu_profile_from_fw()retries vianpc_apply_custom_kpu()again, a second sfw is allocated and overwritesprofile->name,profile->mcam_kex_prfl.mkexandprofile->lt_def. The first sfw is still tracked by devm but no longer referenced.
Is one orphaned sfw per probe acceptable, or should the helper reuse the existing sfw on retry?
There is only one AF device per system
Quoted 4 lines
Quoted 4 lines
drivers/net/ethernet/marvell/octeontx2/af/rvu_npc.c +4
-0+release_firmware (fw); + rvu->kpu_fwdata =NULL ; + +if (ret) {
Quoted 8 lines
drivers/net/ethernet/marvell/octeontx2/af/rvu_npc.c void npc_load_kpu_profile(struct rvu *rvu) · line 2267
Quoted 5 lines
drivers/net/ethernet/marvell/octeontx2/af/rvu_npc.c +5
-0+ rvu->kpu_fwdata_sz =0 ; +dev_err (rvu->dev, +"Loading KPU profile from filesystem failed\n" ); +return ret; + }On the success path,
kpu_fwdatais cleared butkpu_fwdata_szis left non-zero. On the failure path both are cleared. Is the asymmetry intentional?
Yes.
Quoted 14 lines
drivers/net/ethernet/marvell/octeontx2/af/rvu_npc.c static void npc_parser_profile_init(struct rvu *rvu, int blkaddr) · line 2356
Quoted 9 lines
drivers/net/ethernet/marvell/octeontx2/af/rvu_npc.c +5
-0for (idx =0 ; idx < num_kpus; idx++)npc_program_kpu_profile (rvu, blkaddr, idx, &rvu->kpu.kpu[idx]); + +if (profile->from_fs) { +rvu_write64 (rvu, blkaddr,NPC_AF_PKINDX_TYPE (54 ),0x03 ); +rvu_write64 (rvu, blkaddr,NPC_AF_PKINDX_TYPE (58 ),0x03 ); + } }A few questions about these unconditional writes:
PKIND indices 54 and 58 are hardcoded. Other sites in the file clamp viamin_t(int, hw->npc_pkinds, num_pkinds)before programming PKIND CSRs. Is there a guarantee thathw->npc_pkindsis greater than 58 on all silicon variants that take this path?
Quoted 8 lines
The value
0x03has no comment explaining what it programs. Could a brief comment or named constant be added?
These writes are profile-specific (they encode knowledge of two particular PKINDs the customer's profile uses) but apply to any filesystem-loaded blob. Should these settings be expressed in the firmware blob itself rather than hardcoded in the kernel? The commit message describes the change as programming the profile read from the file and does not mention these register writes.
This very much hw specific and intentional.
Ratheesh Kannoth
maintainerPATCH v18 7/8 octeontx2: cn20k: Respect NPC MCAM X2/X4 profile in flows and DFT alloc
In reply to Ratheesh KannothDefault CN20K NPC rule allocation now keys off the active MCAM keyword
width: use X4 with a bank-masked reference index when the silicon uses X4 keys, and X2 with the raw index otherwise (replacing the previous always-X2 / eidx + 1 behaviour).
In the AF flow-install path, flows that need more than 256 key bits query the NPC profile; if the platform is fixed to X2 entries, fail with -EOPNOTSUPP instead of requesting X4. Otherwise select X4 for the MCAM alloc.
On the PF, cache and pass the profile kw_type from npc_get_pfl_info through otx2_mcam_pfl_info_get(), and use it when allocating MCAM entries for RSS/defaults and when installing ethtool flows on CN20K, including masking the reference index for X4 slot layout.
--- .../ethernet/marvell/octeontx2/af/cn20k/npc.c | 21 ++++++-- .../marvell/octeontx2/af/rvu_npc_fs.c | 12 ++++- .../marvell/octeontx2/nic/otx2_flows.c | 48 +++++++++++++------ 3 files changed, 61 insertions(+), 20 deletions(-)
drivers/net/ethernet/marvell/octeontx2/af/cn20k/npc.c +17 -4
@@ -4500,10 +4500,16 @@ int npc_cn20k_dft_rules_alloc(struct rvu *rvu, u16 pcifunc) pfvf =rvu_get_pfvf (rvu, pcifunc); pfvf->hw_prio =NPC_DFT_RULE_PRIO ; +if (npc_priv.kw ==NPC_MCAM_KEY_X4 ) { + req.kw_type =NPC_MCAM_KEY_X4 ; + req.ref_entry = eidx & (npc_priv.bank_depth -1 ); + }else { + req.kw_type =NPC_MCAM_KEY_X2 ; + req.ref_entry = eidx; + } + req.contig = false; req.ref_prio =NPC_MCAM_HIGHER_PRIO ; - req.ref_entry = eidx; - req.kw_type =NPC_MCAM_KEY_X2 ; req.count = cnt; req.hdr.pcifunc = pcifunc;
@@ -4533,11 +4539,18 @@ int npc_cn20k_dft_rules_alloc(struct rvu *rvu, u16 pcifunc)* as NPC_DFT_RULE_PRIO - 1 (higher hw priority) */ req.contig = false; - req.kw_type =NPC_MCAM_KEY_X2 ; req.count = cnt; req.hdr.pcifunc = pcifunc; req.ref_prio =NPC_MCAM_LOWER_PRIO ; - req.ref_entry = eidx +1 ; + +if (npc_priv.kw ==NPC_MCAM_KEY_X4 ) { + req.kw_type =NPC_MCAM_KEY_X4 ; + req.ref_entry = eidx & (npc_priv.bank_depth -1 ); + }else { + req.kw_type =NPC_MCAM_KEY_X2 ; + req.ref_entry = eidx; + } + ret =rvu_mbox_handler_npc_mcam_alloc_entry (rvu, &req, &rsp);if (ret) { dev_err(rvu->dev,
drivers/net/ethernet/marvell/octeontx2/af/rvu_npc_fs.c +11 -1
@@ -1671,9 +1671,11 @@ rvu_npc_alloc_entry_for_flow_install(struct rvu *rvu, {struct npc_mcam_alloc_entry_req entry_req;struct npc_mcam_alloc_entry_rsp entry_rsp; +struct npc_get_pfl_info_rsp rsp = {0 };struct npc_get_num_kws_req kws_req;struct npc_get_num_kws_rsp kws_rsp;int off, kw_bits, rc; +struct msg_req req;u8 *src, *dst;if (!is_cn20k (rvu->pdev)) {
@@ -1697,8 +1699,16 @@ rvu_npc_alloc_entry_for_flow_install(struct rvu *rvu, kw_bits = kws_rsp.kws *64 ; *kw_type =NPC_MCAM_KEY_X2 ; -if (kw_bits >256 ) +if (kw_bits >256 ) { +rvu_mbox_handler_npc_get_pfl_info (rvu, &req, &rsp); +if (rsp.kw_type ==NPC_MCAM_KEY_X2 ) { +dev_err (rvu->dev, +"Only X2 entries are supported in X2 profile\n" ); +return -EOPNOTSUPP ; + } + *kw_type =NPC_MCAM_KEY_X4 ; + }memset (&entry_req,0 ,sizeof (entry_req));memset (&entry_rsp,0 ,sizeof (entry_rsp));
drivers/net/ethernet/marvell/octeontx2/nic/otx2_flows.c +33 -15
128 lines not on this page · raw message
Signature · 1 line
2.43.0
Ratheesh Kannoth
maintainerRe: [PATCH v18 net-next 7/8] octeontx2: cn20k: Respect NPC MCAM X2/X4 profile in flows and DFT alloc
Quoted 10 lines · Ratheesh Kannoth
Default CN20K NPC rule allocation now keys off the active MCAM keyword
width: use X4 with a bank-masked reference index when the silicon uses X4 keys, and X2 with the raw index otherwise (replacing the previous always-X2 / eidx + 1 behaviour).In the AF flow-install path, flows that need more than 256 key bits query the NPC profile; if the platform is fixed to X2 entries, fail with
-EOPNOTSUPPinstead of requesting X4. Otherwise select X4 for the MCAM alloc.
Quoted 6 lines
On the PF, cache and pass the profile
kw_typefromnpc_get_pfl_infothroughotx2_mcam_pfl_info_get(), and use it when allocating MCAM entries for RSS/defaults and when installing ethtool flows on CN20K, including masking the reference index for X4 slot layout.Signed-off-by Ratheesh Kannoth <[email protected]>
https://sashiko.dev/#/patchset/20260602060359.1894952-1-rkannoth%40marvell.com says
Quoted 14 lines
drivers/net/ethernet/marvell/octeontx2/nic/otx2_flows.c static void otx2_clear_ntuple_flow_info(struct otx2_nic *pfvf, struct otx2_flow_ · line 48
Quoted 12 lines
drivers/net/ethernet/marvell/octeontx2/nic/otx2_flows.c +2
-2- u16 *x4_slots) +static int otx2_mcam_pfl_info_get (struct otx2_nic *pfvf,u16 *x4_slots,u8 *kw_type) {struct npc_get_pfl_info_rsp *rsp;struct msg_req *req;static struct {bool is_set; -bool is_x2; +u8 kw_type;u16 x4_slots; } pfl_info;This is a pre-existing issue, but does using a static structure to cache the hardware profile break multi-device support?
These are value of AF device (singleton device per system). AF process mbox messages serially, so if any PF updates this first, that value will be same and can be used for all other PFs.
Quoted 3 lines
Additionally, the cache is protected by a per-instance lock, which provides no mutual exclusion when multiple distinct devices access it concurrently. Could this lead to data races?
No races as value is same and is updated by first PF who request it.
Quoted 8 lines
Quoted 8 lines
drivers/net/ethernet/marvell/octeontx2/nic/otx2_flows.c +2
-0@@ -289,6 +291,8 @@ int otx2_mcam_entry_init(struct otx2_nic *pfvf)struct npc_mcam_alloc_entry_rsp *rsp;int vf_vlan_max_flows, count;int rc, ref, prio, ent; +u8 kw_type =0 ; +u16 x4_slots;u16 dft_idx;
Quoted 6 lines
drivers/net/ethernet/marvell/octeontx2/nic/otx2_flows.c int otx2_mcam_entry_init(struct otx2_nic *pfvf) · line 320
Quoted 4 lines
drivers/net/ethernet/marvell/octeontx2/nic/otx2_flows.c +0
-0ref =0 ;@@ -315,6 +319,16 @@ int otx2_mcam_entry_init(struct otx2_nic *pfvf)if (!flow_cfg->def_ent)return -ENOMEM ;This is a pre-existing issue, but does this function leak devres-managed memory when triggered by devlink parameters?
Preexting issue, so will be addressed to net tree once this series is merged.
Quoted 15 lines
drivers/net/ethernet/marvell/octeontx2/nic/otx2_flows.c static int otx2_add_flow_msg(struct otx2_nic *pfvf, struct otx2_flow *flow) · line 1182
Quoted 4 lines
drivers/net/ethernet/marvell/octeontx2/nic/otx2_flows.c +0
-1u8 kw_type =0 ;u8 *src, *dst;u16 x4_slots; -bool is_x2;This is a pre-existing issue, but does the VF validation error path in
otx2_add_flow_msg()leak a mailbox message buffer?
A mailbox message is dynamically allocated early in the function:req = otx2_mbox_alloc_msg_npc_install_flow(&pfvf->mbox); During the flow specification validation, if the VF is invalid, the function unlocks the mailbox and returns an error: vf = ethtool_get_flow_spec_ring_vf(ring_cookie); if (vf > pci_num_vf(pfvf->pdev)) { mutex_unlock(&pfvf->mbox.lock); return -EINVAL; }
Quoted 4 lines
However, it fails to call
otx2_mbox_reset(), which means the message slot remains active or consumed in the mailbox queue.
Could a user repeatedly submitting invalid flows exhaust the mailbox queue and break communications with the Admin Function?
same as above.
Ratheesh Kannoth
maintainerRe: [PATCH v18 net-next 7/8] octeontx2: cn20k: Respect NPC MCAM X2/X4 profile in flows and DFT alloc
In reply to Ratheesh KannothQuoted 10 lines · Ratheesh Kannoth
Default CN20K NPC rule allocation now keys off the active MCAM keyword
width: use X4 with a bank-masked reference index when the silicon uses X4 keys, and X2 with the raw index otherwise (replacing the previous always-X2 / eidx + 1 behaviour).In the AF flow-install path, flows that need more than 256 key bits query the NPC profile; if the platform is fixed to X2 entries, fail with
-EOPNOTSUPPinstead of requesting X4. Otherwise select X4 for the MCAM alloc.
Quoted 6 lines
On the PF, cache and pass the profile
kw_typefromnpc_get_pfl_infothroughotx2_mcam_pfl_info_get(), and use it when allocating MCAM entries for RSS/defaults and when installing ethtool flows on CN20K, including masking the reference index for X4 slot layout.Signed-off-by Ratheesh Kannoth <[email protected]>
https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20260602060359.1894952-1-rkannoth%40marvell.com says:
Quoted 19 lines
Quoted 19 lines
drivers/net/ethernet/marvell/octeontx2/af/cn20k/npc.c +8
-2@@ -4533,11 +4539,18 @@ int npc_cn20k_dft_rules_alloc(struct rvu *rvu, u16 pcifunc)* as NPC_DFT_RULE_PRIO - 1 (higher hw priority) */ req.contig = false; - req.kw_type =NPC_MCAM_KEY_X2 ; req.count = cnt; req.hdr.pcifunc = pcifunc; req.ref_prio =NPC_MCAM_LOWER_PRIO ; - req.ref_entry = eidx +1 ; + +if (npc_priv.kw ==NPC_MCAM_KEY_X4 ) { + req.kw_type =NPC_MCAM_KEY_X4 ; + req.ref_entry = eidx & (npc_priv.bank_depth -1 ); + }else { + req.kw_type =NPC_MCAM_KEY_X2 ; + req.ref_entry = eidx; + }
Quoted 8 lines
drivers/net/ethernet/marvell/octeontx2/af/cn20k/npc.c int npc_cn20k_dft_rules_alloc(struct rvu *rvu, u16 pcifunc) · line 4553
Quoted 1 line
drivers/net/ethernet/marvell/octeontx2/af/cn20k/npc.c +1
-0+The first allocation block in
npc_cn20k_dft_rules_alloc()previously used req.ref_entry = eidx, so adding only the X4 path to it is purely additive. This second (LOWER_PRIOfallback) block is different: it used to use req.ref_entry = eidx + 1, and the new X2 branch silently drops the +1 and uses eidx instead.
Was the previous eidx + 1 (that is, 2 * npc_priv.bank_depth) in the fallback path intentional?
This is intentional to fix a bug as eidx + 1 may cross the boundary of the resource.
Quoted 19 lines
drivers/net/ethernet/marvell/octeontx2/af/cn20k/npc.c int npc_cn20k_dft_rules_alloc(struct rvu *rvu, u16 pcifunc)
Quoted 11 lines
drivers/net/ethernet/marvell/octeontx2/af/cn20k/npc.c +9
-1-if (kw_bits >256 ) +if (kw_bits >256 ) { +rvu_mbox_handler_npc_get_pfl_info (rvu, &req, &rsp); +if (rsp.kw_type ==NPC_MCAM_KEY_X2 ) { +dev_err (rvu->dev, +"Only X2 entries are supported in X2 profile\n" ); +return -EOPNOTSUPP ; + } + *kw_type =NPC_MCAM_KEY_X4 ; + }A couple of concerns here.
The structmsg_reqreq; is left uninitialized on the stack and then passed torvu_mbox_handler_npc_get_pfl_info(). The handler does not currently read req, but it is a registered mbox dispatch entry whose contract has its hdr filled in by the mbox layer, so should req be zero-initialized at the declaration to match that contract, similar to the on-the-same-line structnpc_get_pfl_info_rsprsp = { 0 } above it?
The int return ofrvu_mbox_handler_npc_get_pfl_info()is also discarded,
Quoted 8 lines
and rsp.kw_type is then tested unconditionally. Since rsp starts at zero and
NPC_MCAM_KEY_X1is 0, a hypothetical handler failure would skip the X2-profile guard and selectNPC_MCAM_KEY_X4. The handler's only failure return today is the !is_cn20k() check, which the function has already short-circuited above, so there is no live failure path; would it still make sense to capture the return and bail out, matching the rc-checking pattern used forrvu_mbox_handler_npc_get_num_kws()just a few lines earlier?
rvu_mbox_handler_npc_get_pfl_info() behaves purely as a local accessor function in this execution path. It retrieves a statically cached profile configuration without processing any mailboxes. However, to maintain strict consistency with the mailbox contract and prevent
any future static analysis tools from flagging uninitialized stack usage, we will add a error handler as per of hardening series once this patch series is merged.
Ratheesh Kannoth
maintainerPATCH v18 8/8 octeontx2-af: npc: cn20k: Allocate npc_priv and dstats dynamically.
In reply to Ratheesh KannothReplace the file-scope static npc_priv with a kcalloc'd struct filled from hardware bank/subbank geometry at init (num_banks is no longer a const compile-time constant; drop init_done and use a non-NULL npc_priv pointer for liveness). Thread npc_priv_get() / pointer access through the CN20K NPC code paths, extend teardown to kfree the root struct on failure and in npc_cn20k_deinit, and adjust MCAM section setup to use the discovered subbank count.
Allocate MCAM debugfs dstats via devm_kzalloc instead of a static matrix, and use the allocated backing store consistently when computing deltas (including the counter rollover compare).
--- .../marvell/octeontx2/af/cn20k/debugfs.c | 17 +- .../ethernet/marvell/octeontx2/af/cn20k/npc.c | 442 +++++++++--------- .../ethernet/marvell/octeontx2/af/cn20k/npc.h | 3 +- 3 files changed, 240 insertions(+), 222 deletions(-)
drivers/net/ethernet/marvell/octeontx2/af/cn20k/debugfs.c +11 -6
@@ -176,7 +176,8 @@ static DEFINE_MUTEX(stats_lock);* hard limit on all silicon variants, preventing any possibility of * out-of-bounds access. */ -static u64 dstats[MAX_NUM_BANKS ][MAX_SUBBANK_DEPTH *MAX_NUM_SUB_BANKS ] = {}; +static u64 (*dstats)[MAX_NUM_BANKS ][MAX_SUBBANK_DEPTH *MAX_NUM_SUB_BANKS ]; +static int npc_mcam_dstats_show (struct seq_file *s,void *unused) {struct npc_priv_t *npc_priv;
@@ -212,24 +213,24 @@ static int npc_mcam_dstats_show(struct seq_file *s, void *unused) NPC_AF_CN20K_MCAMEX_BANKX_STAT_EXT(idx, bank));if (!stats)continue ; -if (stats == dstats[bank][idx]) +if (stats == dstats[0 ][bank][idx])continue ; -if (stats < dstats[bank][idx]) - dstats[bank][idx] =0 ; +if (stats < dstats[0 ][bank][idx]) + dstats[0 ][bank][idx] =0 ; pf =0xFFFF ; map =xa_load (&npc_priv->xa_idx2pf_map, mcam_idx);if (map) pf =xa_to_value (map); - delta = stats - dstats[bank][idx]; + delta = stats - dstats[0 ][bank][idx];snprintf (buff,sizeof (buff),"%u\t%#04x\t%llu\n" , mcam_idx, pf, delta);seq_puts (s, buff); - dstats[bank][idx] = stats; + dstats[0 ][bank][idx] = stats; } }
@@ -397,6 +398,10 @@ int npc_cn20k_debugfs_init(struct rvu *rvu)debugfs_create_file ("vidx2idx" ,0444 , rvu->rvu_dbg.npc, npc_priv, &npc_vidx2idx_map_fops); + dstats =devm_kzalloc (rvu->dev,sizeof (*dstats),GFP_KERNEL ); +if (!dstats) +return -ENOMEM ; +debugfs_create_file ("dstats" ,0444 , rvu->rvu_dbg.npc, rvu, &npc_mcam_dstats_fops);
drivers/net/ethernet/marvell/octeontx2/af/cn20k/npc.c +228 -214
1,179 lines not on this page · raw message
drivers/net/ethernet/marvell/octeontx2/af/cn20k/npc.h +1 -2
@@ -195,7 +195,7 @@ struct npc_defrag_show_node {*/ struct npc_priv_t {int bank_depth; -const int num_banks; +int num_banks;int num_subbanks;int subbank_depth; DECLARE_BITMAP(en_map, MAX_NUM_BANKS *
@@ -214,7 +214,6 @@ struct npc_priv_t {struct list_head defrag_lh;struct mutex lock;/* protect defrag nodes */ int pf_cnt; -bool init_done; };struct npc_kpm_action0 {
Signature · 1 line
2.43.0
Ratheesh Kannoth
maintainerRe: [PATCH v18 net-next 8/8] octeontx2-af: npc: cn20k: Allocate npc_priv and dstats dynamically.
Quoted 8 lines · Ratheesh Kannoth
Replace the file-scope static
npc_privwith a kcalloc'd struct filled from hardware bank/subbank geometry at init (num_banksis no longer a const compile-time constant; dropinit_doneand use a non-NULLnpc_privpointer for liveness). Threadnpc_priv_get()/ pointer access through the CN20K NPC code paths, extend teardown to kfree the root struct on failure and innpc_cn20k_deinit, and adjust MCAM section setup to use the discovered subbank count.
Quoted 5 lines
Allocate MCAM debugfs dstats via
devm_kzallocinstead of a static matrix, and use the allocated backing store consistently when computing deltas (including the counter rollover compare).Signed-off-by Ratheesh Kannoth <[email protected]>
https://sashiko.dev/#/patchset/20260602060359.1894952-1-rkannoth%40marvell.com says
Quoted 9 lines
drivers/net/ethernet/marvell/octeontx2/af/cn20k/debugfs.c int npc_cn20k_debugfs_init(struct rvu *rvu) · line 404
Quoted 5 lines
drivers/net/ethernet/marvell/octeontx2/af/cn20k/debugfs.c +4
-0+ dstats =devm_kzalloc (rvu->dev,sizeof (*dstats),GFP_KERNEL ); +if (!dstats) +return -ENOMEM ; +Since dstats remains a global static pointer, won't multiple RVU devices probing cause this to be unconditionally overwritten, leaking the previously allocated memory?
There is only one admin-function PCI device per system. Will post a patch to net-next (after this series is merged)to reject any additional AF probe with -EBUSY so the driver model matches hardware and automated reviewers can rely on a single bound instance.
Quoted 4 lines
Additionally, because this uses
devm_kzalloc, if a device is unbound, won't devres free the memory while leaving the global pointer dangling? Could this lead to a use-after-free for other active devices that attempt to read from the debugfs file?
There is only one AF device per system, there is no support unbind AF device, so it is already attached.
Quoted 9 lines
drivers/net/ethernet/marvell/octeontx2/af/cn20k/npc.c static int __npc_subbank_alloc(struct rvu *rvu, struct npc_subbank *sb, · line 2290
Quoted 7 lines
drivers/net/ethernet/marvell/octeontx2/af/cn20k/npc.c +2
-2- bw =bitmap_weight (bmap, npc_priv.subbank_depth); - bfree = npc_priv.subbank_depth - bw; + bw =bitmap_weight (bmap, npc_priv->subbank_depth); + bfree = npc_priv->subbank_depth - bw;if (!bfree) {dev_dbg (rvu->dev,"%s: subbank is full\n" , __func__);This is a pre-existing issue, but does
__npc_subbank_alloc()properly verify the bounds of themcam_idxbuffer before writing to it?
Pre-existing issue, will add bound check as part of hardening series, after the patch series merged.
Quoted 10 lines
drivers/net/ethernet/marvell/octeontx2/af/cn20k/npc.c +0
-1- npc_priv.subbank_depth = subbank_depth;drivers/net/ethernet/marvell/octeontx2/af/cn20k/npc.c static int npc_priv_init(struct rvu *rvu) · line 4691
Quoted 4 lines
drivers/net/ethernet/marvell/octeontx2/af/cn20k/npc.c +4
-0+ npc_priv =kcalloc (1 ,sizeof (*npc_priv),GFP_KERNEL ); +if (!npc_priv) +return -ENOMEM ; +Since
npc_privremains a file-scope global pointer, if multiple PCI physical functions probe, won't the second device initialization overwrite this pointer and permanently leak the memory allocated by the first device? Should this state be encapsulated within the per-device private data structure (such as struct rvu) instead?
THere is only one AF device, so only one npc_priv_t instance.
Quoted 5 lines
(quoted diff) +1
-0+ kfree(npc_priv->sb);Quoted 4 lines
drivers/net/ethernet/marvell/octeontx2/af/cn20k/npc.c +1
-1kfree (subbank_srch_order); -bitmap_clear (npc_priv.en_map,0 ,MAX_NUM_BANKS *MAX_NUM_SUB_BANKS * +bitmap_clear (npc_priv->en_map,0 ,MAX_NUM_BANKS *MAX_NUM_SUB_BANKS *MAX_SUBBANK_DEPTH );
Quoted 9 lines
drivers/net/ethernet/marvell/octeontx2/af/cn20k/npc.c void npc_cn20k_deinit(struct rvu *rvu) · line 4793
Quoted 4 lines
drivers/net/ethernet/marvell/octeontx2/af/cn20k/npc.c +3
-0+npc_defrag_list_clear (); +kfree (npc_priv); + npc_priv =NULL ; }If one device is torn down and calls
npc_cn20k_deinit(), it explicitly frees the globalnpc_privpointer and sets it to NULL. Won't this immediately break any other active RVU devices that are concurrently using this shared global pointer, leading to use-after-free or NULL pointer crashes?
THere is only one AF device, so only one npc_priv_t instance.
Ratheesh Kannoth
maintainerRe: [PATCH v18 net-next 8/8] octeontx2-af: npc: cn20k: Allocate npc_priv and dstats dynamically.
In reply to Ratheesh KannothQuoted 8 lines · Ratheesh Kannoth
Replace the file-scope static
npc_privwith a kcalloc'd struct filled from hardware bank/subbank geometry at init (num_banksis no longer a const compile-time constant; dropinit_doneand use a non-NULLnpc_privpointer for liveness). Threadnpc_priv_get()/ pointer access through the CN20K NPC code paths, extend teardown to kfree the root struct on failure and innpc_cn20k_deinit, and adjust MCAM section setup to use the discovered subbank count.
Quoted 5 lines
Allocate MCAM debugfs dstats via
devm_kzallocinstead of a static matrix, and use the allocated backing store consistently when computing deltas (including the counter rollover compare).Signed-off-by Ratheesh Kannoth <[email protected]>
https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20260602060359.1894952-1-rkannoth%40marvell.com says
Quoted 17 lines
Quoted 1 line
+++
b/drivers/net/ethernet/marvell/octeontx2/af/cn20k/debugfs.c[ ... ]
drivers/net/ethernet/marvell/octeontx2/af/cn20k/debugfs.c int npc_cn20k_debugfs_init(struct rvu *rvu) · line 406
Quoted 10 lines
drivers/net/ethernet/marvell/octeontx2/af/cn20k/debugfs.c +4
-0@@ -397,6 +398,10 @@ int npc_cn20k_debugfs_init(struct rvu *rvu)debugfs_create_file ("vidx2idx" ,0444 , rvu->rvu_dbg.npc, npc_priv, &npc_vidx2idx_map_fops); + dstats =devm_kzalloc (rvu->dev,sizeof (*dstats),GFP_KERNEL ); +if (!dstats) +return -ENOMEM ; +debugfs_create_file ("dstats" ,0444 , rvu->rvu_dbg.npc, rvu, &npc_mcam_dstats_fops);Should the dstats allocation move to the top of
npc_cn20k_debugfs_init()? By the time the kzalloc fails, three debugfs files (mcam_layout,mcam_default, vidx2idx) have already been created, and on-ENOMEMthe function returns without unwinding them. The sole caller inrvu_dbg_npc_init()is:
Quoted 7 lines
if (is_cn20k(rvu->pdev)) npc_cn20k_debugfs_init(rvu);
rvu_dbg_npc_init()is static void and discards the return value, so under memory pressure during probe the device completes probe with a partially populated debugfs hierarchy and no error is logged. Pre-patch the function unconditionally returned 0, so this is a new failure mode introduced by this change.
I dont see any problem with this.
Quoted 5 lines
Quoted 1 line
(quoted diff) +0
-1- xa_destroy(&npc_priv.xa_sb_used);[ ... ]
Quoted 2 lines
drivers/net/ethernet/marvell/octeontx2/af/cn20k/npc.c +2
-0+if (!npc_priv) +return ;[ ... ]
Quoted 12 lines
drivers/net/ethernet/marvell/octeontx2/af/cn20k/npc.c void npc_cn20k_deinit(struct rvu *rvu) · line 4793
Quoted 9 lines
drivers/net/ethernet/marvell/octeontx2/af/cn20k/npc.c +5
-1+kfree (npc_priv->sb);kfree (subbank_srch_order); -bitmap_clear (npc_priv.en_map,0 ,MAX_NUM_BANKS *MAX_NUM_SUB_BANKS * +bitmap_clear (npc_priv->en_map,0 ,MAX_NUM_BANKS *MAX_NUM_SUB_BANKS *MAX_SUBBANK_DEPTH ); +npc_defrag_list_clear (); +kfree (npc_priv); + npc_priv =NULL ; }Should
npc_cn20k_deinit()also NULL outsubbank_srch_orderafter kfree()ing it, to match the cleanup symmetry of the fail3 path innpc_priv_init()?
I dont see a need as there is only one AF device per system
Quoted 10 lines
Quoted 10 lines
drivers/net/ethernet/marvell/octeontx2/af/cn20k/npc.h +1
-2-const int num_banks; +int num_banks;int num_subbanks;int subbank_depth; DECLARE_BITMAP(en_map, MAX_NUM_BANKS *@@ -214,7 +214,6 @@ struct npc_priv_t {struct list_head defrag_lh;struct mutex lock;/* protect defrag nodes */ int pf_cnt; -bool init_done;
Quoted 8 lines
Review struct npc_priv_t { · line 217
Quoted 1 line
(quoted diff) +0
-0};The
init_donefield is removed from structnpc_priv_t, but the kernel-doc block above the struct still carries the corresponding description:
* @init_done: Indicates MCAM initialization is done.
Should this @init_done line in the kernel-doc be removed alongside the field?scripts/kernel-docand make htmldocs will warn with "Excess struct memberinit_donedescription innpc_priv_t".
ACK. Should i post v19 to address this ?