Skip to content

Commit e4ff70a

Browse files
djbwdavejiang
authored andcommitted
cxl/acpi: Cleanup __cxl_parse_cfmws()
As a follow on to the recent rework of __cxl_parse_cfmws() to always return errors [1], use cleanup.h helpers to remove goto and other cleanups now that logging is moved to the cxl_parse_cfmws() wrapper. This ends up adding more code than it deletes, but __cxl_parse_cfmws() itself does get smaller. The takeaway from the cond_no_free_ptr() discussion [2] was to not add new macros to handle the cases where no_free_ptr() is awkward, instead rework the code to have helpers and clearer delineation of responsibility. Now one might say that __free(del_cxl_resource) is excessive given it is immediately registered with add_or_reset_cxl_resource(). The rationale for keeping it is that it forces use of "no_free_ptr()" on the argument passed to add_or_reset_cxl_resource(). That in turn makes it clear that @res is NULL for the rest of the function which is part of the point of the cleanup helpers, to turn subtle use after free errors [3] into loud NULL pointer de-references. Link: http://lore.kernel.org/r/170820177238.631006.1012639681618409284.stgit@dwillia2-xfh.jf.intel.com [1] Link: http://lore.kernel.org/r/CAHk-=whBVhnh=KSeBBRet=E7qJAwnPR_aj5em187Q3FiD+LXnA@mail.gmail.com [2] Link: http://lore.kernel.org/r/[email protected] [3] Reported-by: Jonathan Cameron <[email protected]> Closes: http://lore.kernel.org/r/[email protected] Reviewed-by: Alison Schofield <[email protected]> Reviewed-by: Vishal Verma <[email protected]> Signed-off-by: Dan Williams <[email protected]> Reviewed-by: Jonathan Cameron <[email protected]> Link: https://lore.kernel.org/r/171235474028.2718248.14109646123143505522.stgit@dwillia2-xfh.jf.intel.com Signed-off-by: Dave Jiang <[email protected]>
1 parent 1c987cf commit e4ff70a

File tree

2 files changed

+56
-42
lines changed

2 files changed

+56
-42
lines changed

drivers/cxl/acpi.c

Lines changed: 51 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -316,28 +316,59 @@ static const struct cxl_root_ops acpi_root_ops = {
316316
.qos_class = cxl_acpi_qos_class,
317317
};
318318

319+
static void del_cxl_resource(struct resource *res)
320+
{
321+
if (!res)
322+
return;
323+
kfree(res->name);
324+
kfree(res);
325+
}
326+
327+
static struct resource *alloc_cxl_resource(resource_size_t base,
328+
resource_size_t n, int id)
329+
{
330+
struct resource *res __free(kfree) = kzalloc(sizeof(*res), GFP_KERNEL);
331+
332+
if (!res)
333+
return NULL;
334+
335+
res->start = base;
336+
res->end = base + n - 1;
337+
res->flags = IORESOURCE_MEM;
338+
res->name = kasprintf(GFP_KERNEL, "CXL Window %d", id);
339+
if (!res->name)
340+
return NULL;
341+
342+
return no_free_ptr(res);
343+
}
344+
345+
static int add_or_reset_cxl_resource(struct resource *parent, struct resource *res)
346+
{
347+
int rc = insert_resource(parent, res);
348+
349+
if (rc)
350+
del_cxl_resource(res);
351+
return rc;
352+
}
353+
354+
DEFINE_FREE(put_cxlrd, struct cxl_root_decoder *,
355+
if (!IS_ERR_OR_NULL(_T)) put_device(&_T->cxlsd.cxld.dev))
356+
DEFINE_FREE(del_cxl_resource, struct resource *, if (_T) del_cxl_resource(_T))
319357
static int __cxl_parse_cfmws(struct acpi_cedt_cfmws *cfmws,
320358
struct cxl_cfmws_context *ctx)
321359
{
322360
int target_map[CXL_DECODER_MAX_INTERLEAVE];
323361
struct cxl_port *root_port = ctx->root_port;
324-
struct resource *cxl_res = ctx->cxl_res;
325362
struct cxl_cxims_context cxims_ctx;
326-
struct cxl_root_decoder *cxlrd;
327363
struct device *dev = ctx->dev;
328364
cxl_calc_hb_fn cxl_calc_hb;
329365
struct cxl_decoder *cxld;
330366
unsigned int ways, i, ig;
331-
struct resource *res;
332367
int rc;
333368

334369
rc = cxl_acpi_cfmws_verify(dev, cfmws);
335-
if (rc) {
336-
dev_err(dev, "CFMWS range %#llx-%#llx not registered\n",
337-
cfmws->base_hpa,
338-
cfmws->base_hpa + cfmws->window_size - 1);
370+
if (rc)
339371
return rc;
340-
}
341372

342373
rc = eiw_to_ways(cfmws->interleave_ways, &ways);
343374
if (rc)
@@ -348,38 +379,32 @@ static int __cxl_parse_cfmws(struct acpi_cedt_cfmws *cfmws,
348379
for (i = 0; i < ways; i++)
349380
target_map[i] = cfmws->interleave_targets[i];
350381

351-
res = kzalloc(sizeof(*res), GFP_KERNEL);
382+
struct resource *res __free(del_cxl_resource) = alloc_cxl_resource(
383+
cfmws->base_hpa, cfmws->window_size, ctx->id++);
352384
if (!res)
353385
return -ENOMEM;
354386

355-
res->name = kasprintf(GFP_KERNEL, "CXL Window %d", ctx->id++);
356-
if (!res->name)
357-
goto err_name;
358-
359-
res->start = cfmws->base_hpa;
360-
res->end = cfmws->base_hpa + cfmws->window_size - 1;
361-
res->flags = IORESOURCE_MEM;
362-
363387
/* add to the local resource tracking to establish a sort order */
364-
rc = insert_resource(cxl_res, res);
388+
rc = add_or_reset_cxl_resource(ctx->cxl_res, no_free_ptr(res));
365389
if (rc)
366-
goto err_insert;
390+
return rc;
367391

368392
if (cfmws->interleave_arithmetic == ACPI_CEDT_CFMWS_ARITHMETIC_MODULO)
369393
cxl_calc_hb = cxl_hb_modulo;
370394
else
371395
cxl_calc_hb = cxl_hb_xor;
372396

373-
cxlrd = cxl_root_decoder_alloc(root_port, ways, cxl_calc_hb);
397+
struct cxl_root_decoder *cxlrd __free(put_cxlrd) =
398+
cxl_root_decoder_alloc(root_port, ways, cxl_calc_hb);
374399
if (IS_ERR(cxlrd))
375400
return PTR_ERR(cxlrd);
376401

377402
cxld = &cxlrd->cxlsd.cxld;
378403
cxld->flags = cfmws_to_decoder_flags(cfmws->restrictions);
379404
cxld->target_type = CXL_DECODER_HOSTONLYMEM;
380405
cxld->hpa_range = (struct range) {
381-
.start = res->start,
382-
.end = res->end,
406+
.start = cfmws->base_hpa,
407+
.end = cfmws->base_hpa + cfmws->window_size - 1,
383408
};
384409
cxld->interleave_ways = ways;
385410
/*
@@ -399,30 +424,20 @@ static int __cxl_parse_cfmws(struct acpi_cedt_cfmws *cfmws,
399424
rc = acpi_table_parse_cedt(ACPI_CEDT_TYPE_CXIMS,
400425
cxl_parse_cxims, &cxims_ctx);
401426
if (rc < 0)
402-
goto err_xormap;
427+
return rc;
403428
if (!cxlrd->platform_data) {
404429
dev_err(dev, "No CXIMS for HBIG %u\n", ig);
405-
rc = -EINVAL;
406-
goto err_xormap;
430+
return -EINVAL;
407431
}
408432
}
409433
}
410434

411435
cxlrd->qos_class = cfmws->qtg_id;
412436

413437
rc = cxl_decoder_add(cxld, target_map);
414-
err_xormap:
415438
if (rc)
416-
put_device(&cxld->dev);
417-
else
418-
rc = cxl_decoder_autoremove(dev, cxld);
419-
return rc;
420-
421-
err_insert:
422-
kfree(res->name);
423-
err_name:
424-
kfree(res);
425-
return -ENOMEM;
439+
return rc;
440+
return cxl_root_decoder_autoremove(dev, no_free_ptr(cxlrd));
426441
}
427442

428443
static int cxl_parse_cfmws(union acpi_subtable_headers *header, void *arg,
@@ -683,12 +698,6 @@ static void cxl_acpi_lock_reset_class(void *dev)
683698
device_lock_reset_class(dev);
684699
}
685700

686-
static void del_cxl_resource(struct resource *res)
687-
{
688-
kfree(res->name);
689-
kfree(res);
690-
}
691-
692701
static void cxl_set_public_resource(struct resource *priv, struct resource *pub)
693702
{
694703
priv->desc = (unsigned long) pub;

drivers/cxl/cxl.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -781,6 +781,11 @@ int cxl_decoder_add(struct cxl_decoder *cxld, int *target_map);
781781
struct cxl_endpoint_decoder *cxl_endpoint_decoder_alloc(struct cxl_port *port);
782782
int cxl_decoder_add_locked(struct cxl_decoder *cxld, int *target_map);
783783
int cxl_decoder_autoremove(struct device *host, struct cxl_decoder *cxld);
784+
static inline int cxl_root_decoder_autoremove(struct device *host,
785+
struct cxl_root_decoder *cxlrd)
786+
{
787+
return cxl_decoder_autoremove(host, &cxlrd->cxlsd.cxld);
788+
}
784789
int cxl_endpoint_autoremove(struct cxl_memdev *cxlmd, struct cxl_port *endpoint);
785790

786791
/**

0 commit comments

Comments
 (0)