]> www.infradead.org Git - users/dwmw2/linux.git/commitdiff
of: unittest: Add a case to test if API of_irq_parse_one() leaks refcount
authorZijun Hu <quic_zijuhu@quicinc.com>
Sun, 9 Feb 2025 12:58:54 +0000 (20:58 +0800)
committerRob Herring (Arm) <robh@kernel.org>
Mon, 24 Feb 2025 21:34:45 +0000 (15:34 -0600)
To test if of_irq_parse_one(@int_gen_dev, i, ...) will leak refcount of
@i_th_phandle.

int_gen_dev {
...
interrupts-extended = ..., <&i_th_phandle ...>, ...;
...
};

Signed-off-by: Zijun Hu <quic_zijuhu@quicinc.com>
Link: https://lore.kernel.org/r/20250209-of_irq_fix-v2-1-93e3a2659aa7@quicinc.com
Signed-off-by: Rob Herring (Arm) <robh@kernel.org>
drivers/of/unittest.c

index f88ddb1cf5d7f75ac90eeff1f944d563df56f2d3..48aec4695fff647226697fefcae696adaa307480 100644 (file)
@@ -1654,6 +1654,50 @@ static void __init of_unittest_parse_interrupts_extended(void)
        of_node_put(np);
 }
 
+#if IS_ENABLED(CONFIG_OF_DYNAMIC)
+static void __init of_unittest_irq_refcount(void)
+{
+       struct of_phandle_args args;
+       struct device_node *intc0, *int_ext0;
+       unsigned int ref_c0, ref_c1, ref_c2;
+       int rc;
+       bool passed;
+
+       if (of_irq_workarounds & OF_IMAP_OLDWORLD_MAC)
+               return;
+
+       intc0 = of_find_node_by_path("/testcase-data/interrupts/intc0");
+       int_ext0 = of_find_node_by_path("/testcase-data/interrupts/interrupts-extended0");
+       if (!intc0 || !int_ext0) {
+               pr_err("missing testcase data\n");
+               goto out;
+       }
+
+       /* Test refcount for API of_irq_parse_one() */
+       passed = true;
+       ref_c0 = OF_KREF_READ(intc0);
+       ref_c1 = ref_c0 + 1;
+       memset(&args, 0, sizeof(args));
+       rc = of_irq_parse_one(int_ext0, 0, &args);
+       ref_c2 = OF_KREF_READ(intc0);
+       of_node_put(args.np);
+
+       passed &= !rc;
+       passed &= (args.np == intc0);
+       passed &= (args.args_count == 1);
+       passed &= (args.args[0] == 1);
+       passed &= (ref_c1 == ref_c2);
+       unittest(passed, "IRQ refcount case #1 failed, original(%u) expected(%u) got(%u)\n",
+                ref_c0, ref_c1, ref_c2);
+
+out:
+       of_node_put(int_ext0);
+       of_node_put(intc0);
+}
+#else
+static inline void __init of_unittest_irq_refcount(void) { }
+#endif
+
 static const struct of_device_id match_node_table[] = {
        { .data = "A", .name = "name0", }, /* Name alone is lowest priority */
        { .data = "B", .type = "type1", }, /* followed by type alone */
@@ -4324,6 +4368,7 @@ static int __init of_unittest(void)
        of_unittest_changeset_prop();
        of_unittest_parse_interrupts();
        of_unittest_parse_interrupts_extended();
+       of_unittest_irq_refcount();
        of_unittest_dma_get_max_cpu_address();
        of_unittest_parse_dma_ranges();
        of_unittest_pci_dma_ranges();