cxl/region: Fix region reference target accounting
authorDan Williams <dan.j.williams@intel.com>
Mon, 1 Aug 2022 19:55:30 +0000 (12:55 -0700)
committerDan Williams <dan.j.williams@intel.com>
Fri, 5 Aug 2022 15:41:02 +0000 (08:41 -0700)
commite29a8995d63f6f861b2cc446c58cef430885f469
tree984e4105a2d6429ea051ec551e20f86b38ac5192
parent69c9961387f244077101de3ce4e272717617dc87
cxl/region: Fix region reference target accounting

Dan reports:

    The error handling in cxl_port_attach_region() looks like it might
    have a similar bug.  The cxl_rr->nr_targets++; might want a --.

    That function is more complicated.

Indeed cxl_rr->nr_targets leaks when cxl_rr_ep_add() fails, but that
flow is not clear. Fix the bug and the clarity by separating the 'new'
region-reference case from the 'extend' region-reference case. This also
moves the host-physical-address (HPA) validation, that the HPA of a new
region being accounted to the port is greater than the HPA of all other
regions associated with the port, to alloc_region_ref().

Introduce @nr_targets_inc to track when the error exit path needs to
clean up cxl_rr->nr_targets.

Fixes: 384e624bb211 ("cxl/region: Attach endpoint decoders")
Reported-by: Dan Carpenter <dan.carpenter@oracle.com>
Reviewed-by: Ira Weiny <ira.weiny@intel.com>
Link: http://lore.kernel.org/r/165939482134.252363.1915691883146696327.stgit@dwillia2-xfh.jf.intel.com
Signed-off-by: Dan Williams <dan.j.williams@intel.com>
drivers/cxl/core/region.c