Skip to content

Commit a5737b2

Browse files
committed
of: Use scope based of_node_put() cleanups
Use the relatively new scope based of_node_put() cleanup to simplify function exit handling. Doing so reduces the chances of forgetting an of_node_put() and simplifies error paths by avoiding the need for goto statements. Reviewed-by: Saravana Kannan <[email protected]> Reviewed-by: Jonathan Cameron <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Rob Herring <[email protected]>
1 parent 40b0f17 commit a5737b2

File tree

2 files changed

+48
-93
lines changed

2 files changed

+48
-93
lines changed

drivers/of/address.c

Lines changed: 39 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -486,34 +486,30 @@ static int of_translate_one(struct device_node *parent, struct of_bus *bus,
486486
* device that had registered logical PIO mapping, and the return code is
487487
* relative to that node.
488488
*/
489-
static u64 __of_translate_address(struct device_node *dev,
489+
static u64 __of_translate_address(struct device_node *node,
490490
struct device_node *(*get_parent)(const struct device_node *),
491491
const __be32 *in_addr, const char *rprop,
492492
struct device_node **host)
493493
{
494-
struct device_node *parent = NULL;
494+
struct device_node *dev __free(device_node) = of_node_get(node);
495+
struct device_node *parent __free(device_node) = get_parent(dev);
495496
struct of_bus *bus, *pbus;
496497
__be32 addr[OF_MAX_ADDR_CELLS];
497498
int na, ns, pna, pns;
498-
u64 result = OF_BAD_ADDR;
499499

500500
pr_debug("** translation for device %pOF **\n", dev);
501501

502-
/* Increase refcount at current level */
503-
of_node_get(dev);
504-
505502
*host = NULL;
506-
/* Get parent & match bus type */
507-
parent = get_parent(dev);
503+
508504
if (parent == NULL)
509-
goto bail;
505+
return OF_BAD_ADDR;
510506
bus = of_match_bus(parent);
511507

512508
/* Count address cells & copy address locally */
513509
bus->count_cells(dev, &na, &ns);
514510
if (!OF_CHECK_COUNTS(na, ns)) {
515511
pr_debug("Bad cell count for %pOF\n", dev);
516-
goto bail;
512+
return OF_BAD_ADDR;
517513
}
518514
memcpy(addr, in_addr, na * 4);
519515

@@ -533,8 +529,7 @@ static u64 __of_translate_address(struct device_node *dev,
533529
/* If root, we have finished */
534530
if (parent == NULL) {
535531
pr_debug("reached root node\n");
536-
result = of_read_number(addr, na);
537-
break;
532+
return of_read_number(addr, na);
538533
}
539534

540535
/*
@@ -543,27 +538,27 @@ static u64 __of_translate_address(struct device_node *dev,
543538
*/
544539
iorange = find_io_range_by_fwnode(&dev->fwnode);
545540
if (iorange && (iorange->flags != LOGIC_PIO_CPU_MMIO)) {
546-
result = of_read_number(addr + 1, na - 1);
541+
u64 result = of_read_number(addr + 1, na - 1);
547542
pr_debug("indirectIO matched(%pOF) 0x%llx\n",
548543
dev, result);
549-
*host = of_node_get(dev);
550-
break;
544+
*host = no_free_ptr(dev);
545+
return result;
551546
}
552547

553548
/* Get new parent bus and counts */
554549
pbus = of_match_bus(parent);
555550
pbus->count_cells(dev, &pna, &pns);
556551
if (!OF_CHECK_COUNTS(pna, pns)) {
557552
pr_err("Bad cell count for %pOF\n", dev);
558-
break;
553+
return OF_BAD_ADDR;
559554
}
560555

561556
pr_debug("parent bus is %s (na=%d, ns=%d) on %pOF\n",
562557
pbus->name, pna, pns, parent);
563558

564559
/* Apply bus translation */
565560
if (of_translate_one(dev, bus, pbus, addr, na, ns, pna, rprop))
566-
break;
561+
return OF_BAD_ADDR;
567562

568563
/* Complete the move up one level */
569564
na = pna;
@@ -572,11 +567,8 @@ static u64 __of_translate_address(struct device_node *dev,
572567

573568
of_dump_addr("one level translation:", addr, na);
574569
}
575-
bail:
576-
of_node_put(parent);
577-
of_node_put(dev);
578570

579-
return result;
571+
unreachable();
580572
}
581573

582574
u64 of_translate_address(struct device_node *dev, const __be32 *in_addr)
@@ -654,19 +646,16 @@ EXPORT_SYMBOL(of_translate_dma_address);
654646
const __be32 *of_translate_dma_region(struct device_node *dev, const __be32 *prop,
655647
phys_addr_t *start, size_t *length)
656648
{
657-
struct device_node *parent;
649+
struct device_node *parent __free(device_node) = __of_get_dma_parent(dev);
658650
u64 address, size;
659651
int na, ns;
660652

661-
parent = __of_get_dma_parent(dev);
662653
if (!parent)
663654
return NULL;
664655

665656
na = of_bus_n_addr_cells(parent);
666657
ns = of_bus_n_size_cells(parent);
667658

668-
of_node_put(parent);
669-
670659
address = of_translate_dma_address(dev, prop);
671660
if (address == OF_BAD_ADDR)
672661
return NULL;
@@ -688,21 +677,19 @@ const __be32 *__of_get_address(struct device_node *dev, int index, int bar_no,
688677
{
689678
const __be32 *prop;
690679
unsigned int psize;
691-
struct device_node *parent;
680+
struct device_node *parent __free(device_node) = of_get_parent(dev);
692681
struct of_bus *bus;
693682
int onesize, i, na, ns;
694683

695-
/* Get parent & match bus type */
696-
parent = of_get_parent(dev);
697684
if (parent == NULL)
698685
return NULL;
686+
687+
/* match the parent's bus type */
699688
bus = of_match_bus(parent);
700-
if (strcmp(bus->name, "pci") && (bar_no >= 0)) {
701-
of_node_put(parent);
689+
if (strcmp(bus->name, "pci") && (bar_no >= 0))
702690
return NULL;
703-
}
691+
704692
bus->count_cells(dev, &na, &ns);
705-
of_node_put(parent);
706693
if (!OF_CHECK_ADDR_COUNT(na))
707694
return NULL;
708695

@@ -888,14 +875,13 @@ static u64 of_translate_ioport(struct device_node *dev, const __be32 *in_addr,
888875
*/
889876
int of_dma_get_range(struct device_node *np, const struct bus_dma_region **map)
890877
{
891-
struct device_node *node = of_node_get(np);
878+
struct device_node *node __free(device_node) = of_node_get(np);
892879
const __be32 *ranges = NULL;
893880
bool found_dma_ranges = false;
894881
struct of_range_parser parser;
895882
struct of_range range;
896883
struct bus_dma_region *r;
897884
int len, num_ranges = 0;
898-
int ret = 0;
899885

900886
while (node) {
901887
ranges = of_get_property(node, "dma-ranges", &len);
@@ -905,21 +891,18 @@ int of_dma_get_range(struct device_node *np, const struct bus_dma_region **map)
905891
break;
906892

907893
/* Once we find 'dma-ranges', then a missing one is an error */
908-
if (found_dma_ranges && !ranges) {
909-
ret = -ENODEV;
910-
goto out;
911-
}
894+
if (found_dma_ranges && !ranges)
895+
return -ENODEV;
896+
912897
found_dma_ranges = true;
913898

914899
node = of_get_next_dma_parent(node);
915900
}
916901

917902
if (!node || !ranges) {
918903
pr_debug("no dma-ranges found for node(%pOF)\n", np);
919-
ret = -ENODEV;
920-
goto out;
904+
return -ENODEV;
921905
}
922-
923906
of_dma_range_parser_init(&parser, node);
924907
for_each_of_range(&parser, &range) {
925908
if (range.cpu_addr == OF_BAD_ADDR) {
@@ -930,16 +913,12 @@ int of_dma_get_range(struct device_node *np, const struct bus_dma_region **map)
930913
num_ranges++;
931914
}
932915

933-
if (!num_ranges) {
934-
ret = -EINVAL;
935-
goto out;
936-
}
916+
if (!num_ranges)
917+
return -EINVAL;
937918

938919
r = kcalloc(num_ranges + 1, sizeof(*r), GFP_KERNEL);
939-
if (!r) {
940-
ret = -ENOMEM;
941-
goto out;
942-
}
920+
if (!r)
921+
return -ENOMEM;
943922

944923
/*
945924
* Record all info in the generic DMA ranges array for struct device,
@@ -957,9 +936,7 @@ int of_dma_get_range(struct device_node *np, const struct bus_dma_region **map)
957936
r->size = range.size;
958937
r++;
959938
}
960-
out:
961-
of_node_put(node);
962-
return ret;
939+
return 0;
963940
}
964941
#endif /* CONFIG_HAS_DMA */
965942

@@ -1016,24 +993,18 @@ phys_addr_t __init of_dma_get_max_cpu_address(struct device_node *np)
1016993
*/
1017994
bool of_dma_is_coherent(struct device_node *np)
1018995
{
1019-
struct device_node *node;
1020-
bool is_coherent = dma_default_coherent;
1021-
1022-
node = of_node_get(np);
996+
struct device_node *node __free(device_node) = of_node_get(np);
1023997

1024998
while (node) {
1025-
if (of_property_read_bool(node, "dma-coherent")) {
1026-
is_coherent = true;
1027-
break;
1028-
}
1029-
if (of_property_read_bool(node, "dma-noncoherent")) {
1030-
is_coherent = false;
1031-
break;
1032-
}
999+
if (of_property_read_bool(node, "dma-coherent"))
1000+
return true;
1001+
1002+
if (of_property_read_bool(node, "dma-noncoherent"))
1003+
return false;
1004+
10331005
node = of_get_next_dma_parent(node);
10341006
}
1035-
of_node_put(node);
1036-
return is_coherent;
1007+
return dma_default_coherent;
10371008
}
10381009
EXPORT_SYMBOL_GPL(of_dma_is_coherent);
10391010

@@ -1049,20 +1020,14 @@ EXPORT_SYMBOL_GPL(of_dma_is_coherent);
10491020
*/
10501021
static bool of_mmio_is_nonposted(struct device_node *np)
10511022
{
1052-
struct device_node *parent;
1053-
bool nonposted;
1054-
10551023
if (!IS_ENABLED(CONFIG_ARCH_APPLE))
10561024
return false;
10571025

1058-
parent = of_get_parent(np);
1026+
struct device_node *parent __free(device_node) = of_get_parent(np);
10591027
if (!parent)
10601028
return false;
10611029

1062-
nonposted = of_property_read_bool(parent, "nonposted-mmio");
1063-
1064-
of_node_put(parent);
1065-
return nonposted;
1030+
return of_property_read_bool(parent, "nonposted-mmio");
10661031
}
10671032

10681033
static int __of_address_to_resource(struct device_node *dev, int index, int bar_no,

drivers/of/property.c

Lines changed: 9 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -40,15 +40,12 @@
4040
*/
4141
bool of_graph_is_present(const struct device_node *node)
4242
{
43-
struct device_node *ports, *port;
43+
struct device_node *ports __free(device_node) = of_get_child_by_name(node, "ports");
4444

45-
ports = of_get_child_by_name(node, "ports");
4645
if (ports)
4746
node = ports;
4847

49-
port = of_get_child_by_name(node, "port");
50-
of_node_put(ports);
51-
of_node_put(port);
48+
struct device_node *port __free(device_node) = of_get_child_by_name(node, "port");
5249

5350
return !!port;
5451
}
@@ -610,25 +607,22 @@ EXPORT_SYMBOL(of_graph_parse_endpoint);
610607
*/
611608
struct device_node *of_graph_get_port_by_id(struct device_node *parent, u32 id)
612609
{
613-
struct device_node *node, *port;
610+
struct device_node *node __free(device_node) = of_get_child_by_name(parent, "ports");
614611

615-
node = of_get_child_by_name(parent, "ports");
616612
if (node)
617613
parent = node;
618614

619-
for_each_child_of_node(parent, port) {
615+
for_each_child_of_node_scoped(parent, port) {
620616
u32 port_id = 0;
621617

622618
if (!of_node_name_eq(port, "port"))
623619
continue;
624620
of_property_read_u32(port, "reg", &port_id);
625621
if (id == port_id)
626-
break;
622+
return_ptr(port);
627623
}
628624

629-
of_node_put(node);
630-
631-
return port;
625+
return NULL;
632626
}
633627
EXPORT_SYMBOL(of_graph_get_port_by_id);
634628

@@ -655,15 +649,13 @@ struct device_node *of_graph_get_next_endpoint(const struct device_node *parent,
655649
* parent port node.
656650
*/
657651
if (!prev) {
658-
struct device_node *node;
652+
struct device_node *node __free(device_node) =
653+
of_get_child_by_name(parent, "ports");
659654

660-
node = of_get_child_by_name(parent, "ports");
661655
if (node)
662656
parent = node;
663657

664658
port = of_get_child_by_name(parent, "port");
665-
of_node_put(node);
666-
667659
if (!port) {
668660
pr_debug("graph: no port node found in %pOF\n", parent);
669661
return NULL;
@@ -1052,15 +1044,13 @@ static int of_fwnode_graph_parse_endpoint(const struct fwnode_handle *fwnode,
10521044
struct fwnode_endpoint *endpoint)
10531045
{
10541046
const struct device_node *node = to_of_node(fwnode);
1055-
struct device_node *port_node = of_get_parent(node);
1047+
struct device_node *port_node __free(device_node) = of_get_parent(node);
10561048

10571049
endpoint->local_fwnode = fwnode;
10581050

10591051
of_property_read_u32(port_node, "reg", &endpoint->port);
10601052
of_property_read_u32(node, "reg", &endpoint->id);
10611053

1062-
of_node_put(port_node);
1063-
10641054
return 0;
10651055
}
10661056

0 commit comments

Comments
 (0)