Skip to contentSkip to conversation

Moving fwnode_handle_put() calls to a single place with SmPL?

cocci Markus Elfring 1 participant 4 messages Feb 20, 2024 – Feb 21, 2024

lore
Share & tools
lore
#0 lore raw

Hello,

I would like to achieve another known source code transformation also together with the software combination “Coccinelle 1.1.1-00739-g68c580f1” for the implementation of a function like “pmic_glink_altmode_probe”.

Unfortunately, I stumble on the following test result for the attached two files.

Markus_Elfring@Sonne:…/Projekte/Coccinelle/Probe> time spatch --timeout 123 --show-trying pmic_glink_altmode-excerpt2-20240220.c ../janitor/adjust_exception_handling_in_pmic_glink_altmode-20240220.cocci …
SPECIAL NAMES: adding device_for_each_child_node as a iterator
…

     trying function: pmic_glink_altmode_probe: pmic_glink_altmode-excerpt2-20240220.c:10

timeout (we abort)
Fatal error: exception Coccinelle_modules.Common.Timeout

real    2m3,829s
user    2m3,014s
sys     0m0,420s

Will there any further software improvements become possible?

Regards,
Markus

// See also:
// https://git.kernel.org/pub/scm/linux/kernel/git/next/linux-next.git/log/drivers/soc/qcom/pmic_glink_altmode.c
//
// [PATCH 3/6] soc: qcom: pmic_glink_altmode: fix drm bridge use-after-free
// https://lore.kernel.org/lkml/[email protected]/
// https://lkml.org/lkml/2024/2/17/279

@replacement@
identifier rc;
constant c;
@@
<+...
if (...)

 {
    ... when != rc

- fwnode_handle_put(fwnode);
(
+ rc =
- return
( dev_err_probe(...)
| - c
) ;
|
- return rc;
)
+ goto put_fwnode;
}
...+>
return PTR_ERR_OR_ZERO(altmode->client);
+
+put_fwnode:
+fwnode_handle_put(fwnode);
+return rc;

// SPDX-License-Identifier: GPL-2.0-only
/*
* Copyright (c) 2019-2020, The Linux Foundation. All rights reserved.
* Copyright (c) 2022, Linaro Ltd
*/
// See also:
// https://git.kernel.org/pub/scm/linux/kernel/git/next/linux-next.git/log/drivers/soc/qcom/pmic_glink_altmode.c

// deleted part
static int pmic_glink_altmode_probe(struct auxiliary_device *adev,
const struct auxiliary_device_id *id)

{
	struct pmic_glink_altmode_port *alt_port;
	struct pmic_glink_altmode *altmode;
	const struct of_device_id *match;
	struct fwnode_handle *fwnode;
	struct device *dev = &adev->dev;
	u32 port;
	int ret;
// deleted part
	device_for_each_child_node(dev, fwnode) {
		ret = fwnode_property_read_u32(fwnode, "reg", &port);
		if (ret < 0) {
			dev_err(dev, "missing reg property of %pOFn\n", fwnode);
			fwnode_handle_put(fwnode);
			return ret;
		}

		if (port >= ARRAY_SIZE(altmode->ports)) {
			dev_warn(dev, "invalid connector number, ignoring\n");
			continue;
		}

		if (altmode->ports[port].altmode) {
			dev_err(dev, "multiple connector definition for port %u\n", port);
			fwnode_handle_put(fwnode);
			return -EINVAL;
		}

		alt_port = &altmode->ports[port];
		alt_port->altmode = altmode;
		alt_port->index = port;
		INIT_WORK(&alt_port->work, pmic_glink_altmode_worker);

		alt_port->bridge = drm_dp_hpd_bridge_register(dev, to_of_node(fwnode));
		if (IS_ERR(alt_port->bridge)) {
			fwnode_handle_put(fwnode);
			return PTR_ERR(alt_port->bridge);
		}

		alt_port->dp_alt.svid = USB_TYPEC_DP_SID;
		alt_port->dp_alt.mode = USB_TYPEC_DP_MODE;
		alt_port->dp_alt.active = 1;

		alt_port->typec_mux = fwnode_typec_mux_get(fwnode);
		if (IS_ERR(alt_port->typec_mux)) {
			fwnode_handle_put(fwnode);
			return dev_err_probe(dev, PTR_ERR(alt_port->typec_mux),
					     "failed to acquire mode-switch for port: %d\n",
					     port);
		}

		ret = devm_add_action_or_reset(dev, pmic_glink_altmode_put_mux,
					       alt_port->typec_mux);
		if (ret) {
			fwnode_handle_put(fwnode);
			return ret;
		}

		alt_port->typec_retimer = fwnode_typec_retimer_get(fwnode);
		if (IS_ERR(alt_port->typec_retimer)) {
			fwnode_handle_put(fwnode);
			return dev_err_probe(dev, PTR_ERR(alt_port->typec_retimer),
					     "failed to acquire retimer-switch for port: %d\n",
					     port);
		}

		ret = devm_add_action_or_reset(dev, pmic_glink_altmode_put_retimer,
					       alt_port->typec_retimer);
		if (ret) {
			fwnode_handle_put(fwnode);
			return ret;
		}

		alt_port->typec_switch = fwnode_typec_switch_get(fwnode);
		if (IS_ERR(alt_port->typec_switch)) {
			fwnode_handle_put(fwnode);
			return dev_err_probe(dev, PTR_ERR(alt_port->typec_switch),
					     "failed to acquire orientation-switch for port: %d\n",
					     port);
		}

		ret = devm_add_action_or_reset(dev, pmic_glink_altmode_put_switch,
					       alt_port->typec_switch);
		if (ret) {
			fwnode_handle_put(fwnode);
			return ret;
		}
	}
// deleted part
	return PTR_ERR_OR_ZERO(altmode->client);
}

// deleted part

#1 lore raw
Quoted 3 lines

I would like to achieve another known source code transformation also together with the software combination “Coccinelle 1.1.1-00739-g68c580f1” for the implementation of a function like “pmic_glink_altmode_probe”.

It seems that the following script variant for the semantic patch language can work as expected.

@replacement@
constant c;
@@
<+...
if (...)

 {
    ... when != ret

- fwnode_handle_put(fwnode);
(
+ ret =
- return
( dev_err_probe(...)
| - c
) ;
|
- return ret;
)
+ goto put_fwnode;
}
...+>
return PTR_ERR_OR_ZERO(altmode->client);
+
+put_fwnode:
+fwnode_handle_put(fwnode);
+return ret;

Markus_Elfring@Sonne:…/Projekte/Linux/next-analyses> git checkout next-20240220 && time spatch drivers/soc/qcom/pmic_glink_altmode.c …/Projekte/Coccinelle/janitor/adjust_exception_handling_in_pmic_glink_altmode-2-20240220.cocci …

real    0m18,382s
user    0m17,132s
sys     0m0,230s

Regards,
Markus

#2 lore raw
In reply to Markus Elfring

…

Quoted 3 lines

Markus_Elfring@Sonne:…/Projekte/Coccinelle/Probe> time spatch --timeout 123 --show-trying pmic_glink_altmode-excerpt2-20240220.c ../janitor/adjust_exception_handling_in_pmic_glink_altmode-20240220.cocci …
Fatal error: exception Coccinelle_modules.Common.Timeout

…

I would like to add that I could not reproduce such a questionable software behaviour with the following example files.

int my_test_for_refactoring(void)

{
	struct my_handle * h;
	int rc = my_configuration(h);
	if (rc) {
		dev_err(dev, "Handle configuration failed\n");
		put_handle(h);
		return rc;
	}

	if (5 > 1) {
		dev_err(dev, "Reconsider comparison\n");
		put_handle(h);
		return EINVAL;
	}

	return MY_RESULT_AT_THE_END;
}

@adjustment@
identifier rc;
constant c;
@@
<+...
if (...)

 {
    ... when != rc

- put_handle(h);
(
+ rc =
- return

           c;

|
- return rc;
)
+ goto free_handle;
}
...+>
return MY_RESULT_AT_THE_END;
+
+free_handle:
+put_handle(h);
+return rc;

Markus_Elfring@Sonne:…/Projekte/Coccinelle/Probe> time spatch adjust_exception_handling_in_test_function-20240221.cocci test-put_handle-20240221.c …

real    0m0,061s
user    0m0,043s
sys     0m0,015s

Diff output was produced in the way I would usually expect it here.

Regards,
Markus

#3 lore raw

Re: [cocci] Fixing software for the movement of fwnode_handle_put() calls with SmPL

In reply to Markus Elfring

…

Quoted 3 lines

Markus_Elfring@Sonne:…/Projekte/Coccinelle/Probe> time spatch --timeout 123 --show-trying pmic_glink_altmode-excerpt2-20240220.c ../janitor/adjust_exception_handling_in_pmic_glink_altmode-20240220.cocci …
Fatal error: exception Coccinelle_modules.Common.Timeout

…

The amount of source code within a function implementation (like for this test example) obviously influences the software run time also for the shown transformation attempt. Thus the involved source code can be reduced for further software tests. Finally, I could reproduce questionable software behaviour after a single if statement (which contains a call of the function “dev_err_probe”) was left over behind three other if statements.

Markus_Elfring@Sonne:…/Projekte/Coccinelle/Probe> time spatch --timeout 5 pmic_glink_altmode-excerpt3-20240220.c ../janitor/adjust_exception_handling_in_pmic_glink_altmode-20240220.cocci …
Fatal error: exception Coccinelle_modules.Common.Timeout
…

Will observations of “exploding” data processing run times trigger any improvements for the discussed development tool?

Regards,
Markus