Re: [PATCH] cleanup: Add usage and style documentation

From: Markus Elfring
Date: Fri Mar 22 2024 - 09:01:57 EST



> +++ b/include/linux/cleanup.h
> @@ -4,6 +4,118 @@
>
> #include <linux/compiler.h>
>
> +/**
> + * DOC: scope-based cleanup helpers
> + *
> + * The "goto error" pattern is notorious for introducing …

Will any other label become more helpful for this description approach?


> + * this tedium and can aid in maintaining FILO (first in last out)

Would an other word be more appropriate here?



> + * contraindicates a pattern like the following:

I would prefer an other wording approach.


> + * struct pci_dev *dev __free(pci_dev_put) = NULL;

Programmers got used to null pointer initialisations.


> + * In this case @dev is declared in x-mas tree style in a preamble
> + * declaration block. That is problematic because it destroys the
> + * compiler's ability to infer proper unwind order.

Can capabilities be clarified better for the applied compilers?


> If other cleanup
> + * helpers appeared in such a function that depended on @dev being live
> + * to complete their unwind then using the "struct obj_type *obj
> + * __free(...) = NULL" style is an anti-pattern that potentially causes
> + * a use-after-free bug.

I suggest to reconsider such a development concern in more detail.


> + * struct pci_dev *dev __free(pci_dev_put) =
> + * pci_get_slot(parent, PCI_DEVFN(0, 0));
> + *
> + * ...which implies that declaring variables in mid-function scope is
> + * not only allowed, but expected.

* Is there a need to separate the ellipsis from the subsequent word
by a space character?

* You propose a variable definition without specifying extra curly brackets
(for another compound statement / code block).
This can work only if an appropriate pointer is returned by the called function.

* The involved identifiers can occasionally get longer.
Further code layout challenges would need corresponding clarifications.
How will the handling of line length concerns evolve?

* I suggest to take another look also at the transformation pattern
“Reduce Scope of Variable”.
https://refactoring.com/catalog/reduceScopeOfVariable.html


> + * Conversions of existing code to use cleanup helpers should convert
> + * all resources so that no "goto" unwind statements remain. If not all
> + * resources are amenable to cleanup then additional refactoring is
> + * needed to build helper functions, or the function is simply not a
> + * good candidate for conversion.

* How do you think about to specify any more resource cleanup functions
for growing usage of “smart pointers”?

* Would you like to extend the specification of function pairs for
improved applications of guard variants?


Regards,
Markus