cxl/region: Restore a target's position from its slot after sorting
Davidlohr Bueso
maintainerPATCH v2 1/1 cxl/region: Restore a target's position from its slot after sorting
Region auto-discovery records each arriving endpoint decoder in the first free @targets slot and sets cxled->pos to that slot. Once every member has arrived, cxl_region_sort_targets() calculates each one's interleave position and sorts the array by it. The original version of that function then restored cxled->pos from the member's slot in the sorted array, so that follow-on code could rely on p->targets[cxled->pos] naming the member, failed sort or not.
Commit 0cf36a85c140 ("cxl/region: Use cxl_calc_interleave_pos() for auto-discovery") dropped that restore. cxled->pos now keeps whatever cxl_calc_interleave_pos() returned, which on failure is a negative errno. A member that could not be placed is left in a region it cannot be detached from by position: deleting that region runs __cxl_decoder_detach(), which finds pos < 0 and triggers:
cxl region0: expected mem0:decoder3.0 at position -6
WARNING: drivers/cxl/core/region.c:2314 at __cxl_decoder_detach+0x483/0x910
A decoder committed by firmware behind a switch whose own decoders were not committed for the range is enough to get there
The same stale position is what commit cbda6a2c2bec ("cxl/region: Fix out-of-bounds access in cxl_cancel_auto_attach()") had to work around by walking the array instead of indexing it.
Restore cxled->pos from the member's slot after the sort.
---
Found while battle testing BI changes.
Changes in v2:
- fix the hunk header
drivers/cxl/core/region.c | 8 ++++++++ 1 file changed, 8 insertions(+)
drivers/cxl/core/region.c +8 -0
@@ -2045,6 +2045,14 @@ static int cxl_region_sort_targets(struct cxl_region *cxlr)sort (p->targets, p->nr_targets,sizeof (p->targets[0 ]), cmp_interleave_pos,NULL ); +/* +* Restore cxled->pos from its ->targets[] slot so that follow-on +* code paths can reliably do p->targets[cxled->pos] to +* self-reference their entry, whether or not the sort succeeded. +*/ +for (i =0 ; i < p->nr_targets; i++) + p->targets[i]->pos = i; +dev_dbg (&cxlr->dev,"region sort %s\n" , rc ?"failed" :"successful" );return rc; }
Signature · 1 line
2.39.5
Quoted 25 lines · Davidlohr Bueso
Region auto-discovery records each arriving endpoint decoder in the first free @targets slot and sets
cxled->posto that slot. Once every member has arrived,cxl_region_sort_targets()calculates each one's interleave position and sorts the array by it. The original version of that function then restoredcxled->posfrom the member's slot in the sorted array, so that follow-on code could rely on p->targets[cxled->pos] naming the member, failed sort or not.Commit
0cf36a85c140("cxl/region: Usecxl_calc_interleave_pos()for auto-discovery") dropped that restore.cxled->posnow keeps whatevercxl_calc_interleave_pos()returned, which on failure is a negative errno. A member that could not be placed is left in a region it cannot be detached from by position: deleting that region runs__cxl_decoder_detach(), which finds pos < 0 and triggers:cxl region0: expected mem0:decoder3.0 at position -6
WARNING: drivers/cxl/core/region.c:2314 at __cxl_decoder_detach+0x483/0x910A decoder committed by firmware behind a switch whose own decoders were not committed for the range is enough to get there
The same stale position is what commit
cbda6a2c2bec("cxl/region: Fix out-of-bounds access incxl_cancel_auto_attach()") had to work around by walking the array instead of indexing it.
Quoted 6 lines
Restore
cxled->posfrom the member's slot after the sort.Fixes 0cf36a85c140 ("cxl/region: Use cxl_calc_interleave_pos() for auto-discovery")Reviewed-by Alison Schofield <[email protected]>Signed-off-by Davidlohr Bueso <[email protected]>---
Thanks,
Ben
Quoted 25 lines · Davidlohr Bueso
Region auto-discovery records each arriving endpoint decoder in the first free @targets slot and sets
cxled->posto that slot. Once every member has arrived,cxl_region_sort_targets()calculates each one's interleave position and sorts the array by it. The original version of that function then restoredcxled->posfrom the member's slot in the sorted array, so that follow-on code could rely on p->targets[cxled->pos] naming the member, failed sort or not.Commit
0cf36a85c140("cxl/region: Usecxl_calc_interleave_pos()for auto-discovery") dropped that restore.cxled->posnow keeps whatevercxl_calc_interleave_pos()returned, which on failure is a negative errno. A member that could not be placed is left in a region it cannot be detached from by position: deleting that region runs__cxl_decoder_detach(), which finds pos < 0 and triggers:cxl region0: expected mem0:decoder3.0 at position -6
WARNING: drivers/cxl/core/region.c:2314 at __cxl_decoder_detach+0x483/0x910A decoder committed by firmware behind a switch whose own decoders were not committed for the range is enough to get there
The same stale position is what commit
cbda6a2c2bec("cxl/region: Fix out-of-bounds access incxl_cancel_auto_attach()") had to work around by walking the array instead of indexing it.
Quoted 5 lines
Restore
cxled->posfrom the member's slot after the sort.Fixes 0cf36a85c140 ("cxl/region: Use cxl_calc_interleave_pos() for auto-discovery")Reviewed-by Alison Schofield <[email protected]>Signed-off-by Davidlohr Bueso <[email protected]>
Quoted 28 lines
---
Found while battle testing BI changes.Changes in v2:
- fix the hunk headerdrivers/cxl/core/region.c | 8 ++++++++ 1 file changed, 8 insertions(+)drivers/cxl/core/region.c +8
-0@@ -2045,6 +2045,14 @@ static int cxl_region_sort_targets(struct cxl_region *cxlr)sort (p->targets, p->nr_targets,sizeof (p->targets[0 ]), cmp_interleave_pos,NULL ); +/* +* Restore cxled->pos from its ->targets[] slot so that follow-on +* code paths can reliably do p->targets[cxled->pos] to +* self-reference their entry, whether or not the sort succeeded. +*/ +for (i =0 ; i < p->nr_targets; i++) + p->targets[i]->pos = i; +dev_dbg (&cxlr->dev,"region sort %s\n" , rc ?"failed" :"successful" );return rc; }
Quoted 25 lines · Davidlohr Bueso
Region auto-discovery records each arriving endpoint decoder in the first free @targets slot and sets
cxled->posto that slot. Once every member has arrived,cxl_region_sort_targets()calculates each one's interleave position and sorts the array by it. The original version of that function then restoredcxled->posfrom the member's slot in the sorted array, so that follow-on code could rely on p->targets[cxled->pos] naming the member, failed sort or not.Commit
0cf36a85c140("cxl/region: Usecxl_calc_interleave_pos()for auto-discovery") dropped that restore.cxled->posnow keeps whatevercxl_calc_interleave_pos()returned, which on failure is a negative errno. A member that could not be placed is left in a region it cannot be detached from by position: deleting that region runs__cxl_decoder_detach(), which finds pos < 0 and triggers:cxl region0: expected mem0:decoder3.0 at position -6
WARNING: drivers/cxl/core/region.c:2314 at __cxl_decoder_detach+0x483/0x910A decoder committed by firmware behind a switch whose own decoders were not committed for the range is enough to get there
The same stale position is what commit
cbda6a2c2bec("cxl/region: Fix out-of-bounds access incxl_cancel_auto_attach()") had to work around by walking the array instead of indexing it.
Quoted 5 lines
Restore
cxled->posfrom the member's slot after the sort.Fixes 0cf36a85c140 ("cxl/region: Use cxl_calc_interleave_pos() for auto-discovery")Reviewed-by Alison Schofield <[email protected]>Signed-off-by Davidlohr Bueso <[email protected]>
Applied to cxl/fixes1731c729762b
Quoted 28 lines
---
Found while battle testing BI changes.Changes in v2:
- fix the hunk headerdrivers/cxl/core/region.c | 8 ++++++++ 1 file changed, 8 insertions(+)drivers/cxl/core/region.c +8
-0@@ -2045,6 +2045,14 @@ static int cxl_region_sort_targets(struct cxl_region *cxlr)sort (p->targets, p->nr_targets,sizeof (p->targets[0 ]), cmp_interleave_pos,NULL ); +/* +* Restore cxled->pos from its ->targets[] slot so that follow-on +* code paths can reliably do p->targets[cxled->pos] to +* self-reference their entry, whether or not the sort succeeded. +*/ +for (i =0 ; i < p->nr_targets; i++) + p->targets[i]->pos = i; +dev_dbg (&cxlr->dev,"region sort %s\n" , rc ?"failed" :"successful" );return rc; }