* Re: [RFC] slab: introduce auto_kfree macro [not found] ` <Z-0SU8cYkTTbprSh@smile.fi.intel.com> @ 2025-04-02 12:19 ` Peter Zijlstra 2025-04-02 12:22 ` Peter Zijlstra 2025-04-04 3:05 ` Herbert Xu 0 siblings, 2 replies; 4+ messages in thread From: Peter Zijlstra @ 2025-04-02 12:19 UTC (permalink / raw) To: Andy Shevchenko Cc: Przemek Kitszel, linux-kernel, linux-mm, vbabka, torvalds, intel-wired-lan, netdev, linux-toolchains On Wed, Apr 02, 2025 at 01:32:51PM +0300, Andy Shevchenko wrote: > On Tue, Apr 01, 2025 at 03:44:08PM +0200, Przemek Kitszel wrote: > > Add auto_kfree macro that acts as a higher level wrapper for manual > > __free(kfree) invocation, and sets the pointer to NULL - to have both > > well defined behavior also for the case code would lack other assignement. > > > > Consider the following code: > > int my_foo(int arg) > > { > > struct my_dev_foo *foo __free(kfree); /* no assignement */ > > > > foo = kzalloc(sizeof(*foo), GFP_KERNEL); > > /* ... */ > > } > > > > So far it is fine and even optimal in terms of not assigning when > > not needed. But it is typical to don't touch (and sadly to don't > > think about) code that is not related to the change, so let's consider > > an extension to the above, namely an "early return" style to check > > arg prior to allocation: > > int my_foo(int arg) > > { > > struct my_dev_foo *foo __free(kfree); /* no assignement */ > > + > > + if (!arg) > > + return -EINVAL; > > foo = kzalloc(sizeof(*foo), GFP_KERNEL); > > /* ... */ > > } > > Now we have uninitialized foo passed to kfree, what likely will crash. > > One could argue that `= NULL` should be added to this patch, but it is > > easy to forgot, especially when the foo declaration is outside of the > > default git context. The compiler *should* complain. But neither GCC nor clang actually appear to warn in this case. I don't think we should be making dodgy macros like you propose to work around this compiler deficiency. Instead I would argue we ought to get both compilers fixed asap, and then none of this will be needed. ^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [RFC] slab: introduce auto_kfree macro 2025-04-02 12:19 ` [RFC] slab: introduce auto_kfree macro Peter Zijlstra @ 2025-04-02 12:22 ` Peter Zijlstra 2025-04-02 12:57 ` Andy Shevchenko 2025-04-04 3:05 ` Herbert Xu 1 sibling, 1 reply; 4+ messages in thread From: Peter Zijlstra @ 2025-04-02 12:22 UTC (permalink / raw) To: Andy Shevchenko Cc: Przemek Kitszel, linux-kernel, linux-mm, vbabka, torvalds, intel-wired-lan, netdev, linux-toolchains On Wed, Apr 02, 2025 at 02:19:35PM +0200, Peter Zijlstra wrote: > On Wed, Apr 02, 2025 at 01:32:51PM +0300, Andy Shevchenko wrote: > > On Tue, Apr 01, 2025 at 03:44:08PM +0200, Przemek Kitszel wrote: > > > Add auto_kfree macro that acts as a higher level wrapper for manual > > > __free(kfree) invocation, and sets the pointer to NULL - to have both > > > well defined behavior also for the case code would lack other assignement. > > > > > > Consider the following code: > > > int my_foo(int arg) > > > { > > > struct my_dev_foo *foo __free(kfree); /* no assignement */ > > > > > > foo = kzalloc(sizeof(*foo), GFP_KERNEL); > > > /* ... */ > > > } > > > > > > So far it is fine and even optimal in terms of not assigning when > > > not needed. But it is typical to don't touch (and sadly to don't > > > think about) code that is not related to the change, so let's consider > > > an extension to the above, namely an "early return" style to check > > > arg prior to allocation: > > > int my_foo(int arg) > > > { > > > struct my_dev_foo *foo __free(kfree); /* no assignement */ > > > + > > > + if (!arg) > > > + return -EINVAL; > > > foo = kzalloc(sizeof(*foo), GFP_KERNEL); > > > /* ... */ > > > } > > > Now we have uninitialized foo passed to kfree, what likely will crash. > > > One could argue that `= NULL` should be added to this patch, but it is > > > easy to forgot, especially when the foo declaration is outside of the > > > default git context. > > The compiler *should* complain. But neither GCC nor clang actually > appear to warn in this case. > > I don't think we should be making dodgy macros like you propose to work > around this compiler deficiency. Instead I would argue we ought to get > both compilers fixed asap, and then none of this will be needed. Ah, I think the problem is that the cleanup function takes a pointer to the object, and pointers to uninitialized values are generally considered okay. The compilers would have to explicitly disallow this for the cleanup functions. ^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [RFC] slab: introduce auto_kfree macro 2025-04-02 12:22 ` Peter Zijlstra @ 2025-04-02 12:57 ` Andy Shevchenko 0 siblings, 0 replies; 4+ messages in thread From: Andy Shevchenko @ 2025-04-02 12:57 UTC (permalink / raw) To: Peter Zijlstra Cc: Przemek Kitszel, linux-kernel, linux-mm, vbabka, torvalds, intel-wired-lan, netdev, linux-toolchains On Wed, Apr 02, 2025 at 02:22:24PM +0200, Peter Zijlstra wrote: > On Wed, Apr 02, 2025 at 02:19:35PM +0200, Peter Zijlstra wrote: > > On Wed, Apr 02, 2025 at 01:32:51PM +0300, Andy Shevchenko wrote: > > > On Tue, Apr 01, 2025 at 03:44:08PM +0200, Przemek Kitszel wrote: > > > > Add auto_kfree macro that acts as a higher level wrapper for manual > > > > __free(kfree) invocation, and sets the pointer to NULL - to have both > > > > well defined behavior also for the case code would lack other assignement. > > > > > > > > Consider the following code: > > > > int my_foo(int arg) > > > > { > > > > struct my_dev_foo *foo __free(kfree); /* no assignement */ > > > > > > > > foo = kzalloc(sizeof(*foo), GFP_KERNEL); > > > > /* ... */ > > > > } > > > > > > > > So far it is fine and even optimal in terms of not assigning when > > > > not needed. But it is typical to don't touch (and sadly to don't > > > > think about) code that is not related to the change, so let's consider > > > > an extension to the above, namely an "early return" style to check > > > > arg prior to allocation: > > > > int my_foo(int arg) > > > > { > > > > struct my_dev_foo *foo __free(kfree); /* no assignement */ > > > > + > > > > + if (!arg) > > > > + return -EINVAL; > > > > foo = kzalloc(sizeof(*foo), GFP_KERNEL); > > > > /* ... */ > > > > } > > > > Now we have uninitialized foo passed to kfree, what likely will crash. > > > > One could argue that `= NULL` should be added to this patch, but it is > > > > easy to forgot, especially when the foo declaration is outside of the > > > > default git context. > > > > The compiler *should* complain. But neither GCC nor clang actually > > appear to warn in this case. > > > > I don't think we should be making dodgy macros like you propose to work > > around this compiler deficiency. Instead I would argue we ought to get > > both compilers fixed asap, and then none of this will be needed. > > Ah, I think the problem is that the cleanup function takes a pointer to > the object, and pointers to uninitialized values are generally > considered okay. > > The compilers would have to explicitly disallow this for the cleanup > functions. Hmm... What I have heard is that the cleanup is basically a port of C++ destructor code to C, and it might be related to the virtual functions that are may be absent for the basic classes. But not an expert here, just speculating based on my poor knowledge of C++. -- With Best Regards, Andy Shevchenko ^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [RFC] slab: introduce auto_kfree macro 2025-04-02 12:19 ` [RFC] slab: introduce auto_kfree macro Peter Zijlstra 2025-04-02 12:22 ` Peter Zijlstra @ 2025-04-04 3:05 ` Herbert Xu 1 sibling, 0 replies; 4+ messages in thread From: Herbert Xu @ 2025-04-04 3:05 UTC (permalink / raw) To: Peter Zijlstra Cc: andriy.shevchenko, przemyslaw.kitszel, linux-kernel, linux-mm, vbabka, torvalds, intel-wired-lan, netdev, linux-toolchains Peter Zijlstra <peterz@infradead.org> wrote: > > The compiler *should* complain. But neither GCC nor clang actually > appear to warn in this case. Linus turned that warning off in 2020: commit 78a5255ffb6a1af189a83e493d916ba1c54d8c75 Author: Linus Torvalds <torvalds@linux-foundation.org> Date: Sat May 9 13:57:10 2020 -0700 Stop the ad-hoc games with -Wno-maybe-initialized You need to enable it by hand to see the warning: make KBUILD_CFLAGS_KERNEL=-Wmaybe-uninitialized CFLAGS_MODULE=-Wmaybe-uninitialized W=2 enables it too but it also enables lots of other crap so it's useless. Cheers, -- Email: Herbert Xu <herbert@gondor.apana.org.au> Home Page: http://gondor.apana.org.au/~herbert/ PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt ^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2025-04-04 3:06 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
[not found] <20250401134408.37312-1-przemyslaw.kitszel@intel.com>
[not found] ` <Z-0SU8cYkTTbprSh@smile.fi.intel.com>
2025-04-02 12:19 ` [RFC] slab: introduce auto_kfree macro Peter Zijlstra
2025-04-02 12:22 ` Peter Zijlstra
2025-04-02 12:57 ` Andy Shevchenko
2025-04-04 3:05 ` Herbert Xu
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox