2020-05-29 01:03:26

by Yi Wang

[permalink] [raw]
Subject: [PATCH] powerpc/nvram: Replace kmalloc with kzalloc in the error message

From: Liao Pingfang <[email protected]>

Use kzalloc instead of kmalloc in the error message according to
the previous kzalloc() call.

Signed-off-by: Liao Pingfang <[email protected]>
---
arch/powerpc/kernel/nvram_64.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/arch/powerpc/kernel/nvram_64.c b/arch/powerpc/kernel/nvram_64.c
index fb4f610..c3a0c8d 100644
--- a/arch/powerpc/kernel/nvram_64.c
+++ b/arch/powerpc/kernel/nvram_64.c
@@ -892,7 +892,7 @@ loff_t __init nvram_create_partition(const char *name, int sig,
/* Create our OS partition */
new_part = kzalloc(sizeof(*new_part), GFP_KERNEL);
if (!new_part) {
- pr_err("%s: kmalloc failed\n", __func__);
+ pr_err("%s: kzalloc failed\n", __func__);
return -ENOMEM;
}

--
2.9.5


2020-05-29 04:05:49

by Michael Ellerman

[permalink] [raw]
Subject: Re: [PATCH] powerpc/nvram: Replace kmalloc with kzalloc in the error message

Yi Wang <[email protected]> writes:
> From: Liao Pingfang <[email protected]>
>
> Use kzalloc instead of kmalloc in the error message according to
> the previous kzalloc() call.

Please just remove the message instead, it's a tiny allocation that's
unlikely to ever fail, and the caller will print an error anyway.

cheers

> diff --git a/arch/powerpc/kernel/nvram_64.c b/arch/powerpc/kernel/nvram_64.c
> index fb4f610..c3a0c8d 100644
> --- a/arch/powerpc/kernel/nvram_64.c
> +++ b/arch/powerpc/kernel/nvram_64.c
> @@ -892,7 +892,7 @@ loff_t __init nvram_create_partition(const char *name, int sig,
> /* Create our OS partition */
> new_part = kzalloc(sizeof(*new_part), GFP_KERNEL);
> if (!new_part) {
> - pr_err("%s: kmalloc failed\n", __func__);
> + pr_err("%s: kzalloc failed\n", __func__);
> return -ENOMEM;
> }
>
> --
> 2.9.5

2020-05-29 18:52:51

by Markus Elfring

[permalink] [raw]
Subject: Re: [PATCH] powerpc/nvram: Replace kmalloc with kzalloc in the error message

> Please just remove the message instead, it's a tiny allocation that's
> unlikely to ever fail, and the caller will print an error anyway.

How do you think about to take another look at a previous update suggestion
like the following?

powerpc/nvram: Delete three error messages for a failed memory allocation
https://patchwork.ozlabs.org/project/linuxppc-dev/patch/[email protected]/
https://lore.kernel.org/linuxppc-dev/[email protected]/
https://lore.kernel.org/patchwork/patch/752720/
https://lkml.org/lkml/2017/1/19/537

Regards,
Markus

2020-06-02 03:01:03

by Michael Ellerman

[permalink] [raw]
Subject: Re: [PATCH] powerpc/nvram: Replace kmalloc with kzalloc in the error message

Markus Elfring <[email protected]> writes:
>> Please just remove the message instead, it's a tiny allocation that's
>> unlikely to ever fail, and the caller will print an error anyway.
>
> How do you think about to take another look at a previous update suggestion
> like the following?
>
> powerpc/nvram: Delete three error messages for a failed memory allocation
> https://patchwork.ozlabs.org/project/linuxppc-dev/patch/[email protected]/
> https://lore.kernel.org/linuxppc-dev/[email protected]/
> https://lore.kernel.org/patchwork/patch/752720/
> https://lkml.org/lkml/2017/1/19/537

That deleted the messages from nvram_scan_partitions(), but neither of
the callers of nvram_scan_paritions() check its return value or print
anything if it fails. So removing those messages would make those
failures silent which is not what we want.

cheers

2020-06-02 05:06:15

by Markus Elfring

[permalink] [raw]
Subject: Re: [PATCH] powerpc/nvram: Replace kmalloc with kzalloc in the error message

>>> Please just remove the message instead, it's a tiny allocation that's
>>> unlikely to ever fail, and the caller will print an error anyway.
>>
>> How do you think about to take another look at a previous update suggestion
>> like the following?
>>
>> powerpc/nvram: Delete three error messages for a failed memory allocation
>> https://patchwork.ozlabs.org/project/linuxppc-dev/patch/[email protected]/
>> https://lore.kernel.org/linuxppc-dev/[email protected]/
>> https://lore.kernel.org/patchwork/patch/752720/
>> https://lkml.org/lkml/2017/1/19/537
>
> That deleted the messages from nvram_scan_partitions(), but neither of
> the callers of nvram_scan_paritions() check its return value or print
> anything if it fails. So removing those messages would make those
> failures silent which is not what we want.

* How do you think about information like the following?
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/process/coding-style.rst?id=f359287765c04711ff54fbd11645271d8e5ff763#n883
“…
These generic allocation functions all emit a stack dump on failure when used
without __GFP_NOWARN so there is no use in emitting an additional failure
message when NULL is returned.
…”

* Would you like to clarify software development concerns around
the Linux allocation failure report any more?

Regards,
Markus

2020-06-02 11:25:59

by Michael Ellerman

[permalink] [raw]
Subject: Re: [PATCH] powerpc/nvram: Replace kmalloc with kzalloc in the error message

Markus Elfring <[email protected]> writes:
>>>> Please just remove the message instead, it's a tiny allocation that's
>>>> unlikely to ever fail, and the caller will print an error anyway.
>>>
>>> How do you think about to take another look at a previous update suggestion
>>> like the following?
>>>
>>> powerpc/nvram: Delete three error messages for a failed memory allocation
>>> https://patchwork.ozlabs.org/project/linuxppc-dev/patch/[email protected]/
>>> https://lore.kernel.org/linuxppc-dev/[email protected]/
>>> https://lore.kernel.org/patchwork/patch/752720/
>>> https://lkml.org/lkml/2017/1/19/537
>>
>> That deleted the messages from nvram_scan_partitions(), but neither of
>> the callers of nvram_scan_paritions() check its return value or print
>> anything if it fails. So removing those messages would make those
>> failures silent which is not what we want.
>
> * How do you think about information like the following?
> https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/process/coding-style.rst?id=f359287765c04711ff54fbd11645271d8e5ff763#n883
> “…
> These generic allocation functions all emit a stack dump on failure when used
> without __GFP_NOWARN so there is no use in emitting an additional failure
> message when NULL is returned.
> …”

Are you sure that's actually true?

A quick look around in slub.c leads me to:

slab_out_of_memory(struct kmem_cache *s, gfp_t gfpflags, int nid)
{
#ifdef CONFIG_SLUB_DEBUG
static DEFINE_RATELIMIT_STATE(slub_oom_rs, DEFAULT_RATELIMIT_INTERVAL,
DEFAULT_RATELIMIT_BURST);
int node;
struct kmem_cache_node *n;

if ((gfpflags & __GFP_NOWARN) || !__ratelimit(&slub_oom_rs))
return;

pr_warn("SLUB: Unable to allocate memory on node %d, gfp=%#x(%pGg)\n",
nid, gfpflags, &gfpflags);
pr_warn(" cache: %s, object size: %u, buffer size: %u, default order: %u, min order: %u\n",
s->name, s->object_size, s->size, oo_order(s->oo),
oo_order(s->min));

if (oo_order(s->min) > get_order(s->object_size))
pr_warn(" %s debugging increased min order, use slub_debug=O to disable.\n",
s->name);

for_each_kmem_cache_node(s, node, n) {
unsigned long nr_slabs;
unsigned long nr_objs;
unsigned long nr_free;

nr_free = count_partial(n, count_free);
nr_slabs = node_nr_slabs(n);
nr_objs = node_nr_objs(n);

pr_warn(" node %d: slabs: %ld, objs: %ld, free: %ld\n",
node, nr_slabs, nr_objs, nr_free);
}
#endif
}

Which looks a lot like it won't print anything when CONFIG_SLUB_DEBUG=n.

But maybe I'm looking in the wrong place?

cheers

2020-06-02 11:45:27

by Dan Carpenter

[permalink] [raw]
Subject: Re: [PATCH] powerpc/nvram: Replace kmalloc with kzalloc in the error message

On Tue, Jun 02, 2020 at 09:23:57PM +1000, Michael Ellerman wrote:
> Markus Elfring <[email protected]> writes:
> >>>> Please just remove the message instead, it's a tiny allocation that's
> >>>> unlikely to ever fail, and the caller will print an error anyway.
> >>>
> >>> How do you think about to take another look at a previous update suggestion
> >>> like the following?
> >>>
> >>> powerpc/nvram: Delete three error messages for a failed memory allocation
> >>> https://patchwork.ozlabs.org/project/linuxppc-dev/patch/[email protected]/
> >>> https://lore.kernel.org/linuxppc-dev/[email protected]/
> >>> https://lore.kernel.org/patchwork/patch/752720/
> >>> https://lkml.org/lkml/2017/1/19/537
> >>
> >> That deleted the messages from nvram_scan_partitions(), but neither of
> >> the callers of nvram_scan_paritions() check its return value or print
> >> anything if it fails. So removing those messages would make those
> >> failures silent which is not what we want.
> >
> > * How do you think about information like the following?
> > https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/process/coding-style.rst?id=f359287765c04711ff54fbd11645271d8e5ff763#n883
> > “…
> > These generic allocation functions all emit a stack dump on failure when used
> > without __GFP_NOWARN so there is no use in emitting an additional failure
> > message when NULL is returned.
> > …”
>
> Are you sure that's actually true?
>
> A quick look around in slub.c leads me to:
>
> slab_out_of_memory(struct kmem_cache *s, gfp_t gfpflags, int nid)
> {
> #ifdef CONFIG_SLUB_DEBUG

You first have to enable EXPERT mode before you can disable SLUB_DEBUG.
So that hopefully means you *really* want to save memory. It doesn't
make sense to add a bunch of memory wasting printks when the users want
to go to extra lengths to conserve memory.

regards,
dan carpenter

2020-06-03 11:39:16

by Michael Ellerman

[permalink] [raw]
Subject: Re: [PATCH] powerpc/nvram: Replace kmalloc with kzalloc in the error message

Dan Carpenter <[email protected]> writes:
> On Tue, Jun 02, 2020 at 09:23:57PM +1000, Michael Ellerman wrote:
>> Markus Elfring <[email protected]> writes:
>> >>>> Please just remove the message instead, it's a tiny allocation that's
>> >>>> unlikely to ever fail, and the caller will print an error anyway.
>> >>>
>> >>> How do you think about to take another look at a previous update suggestion
>> >>> like the following?
>> >>>
>> >>> powerpc/nvram: Delete three error messages for a failed memory allocation
>> >>> https://patchwork.ozlabs.org/project/linuxppc-dev/patch/[email protected]/
>> >>> https://lore.kernel.org/linuxppc-dev/[email protected]/
>> >>> https://lore.kernel.org/patchwork/patch/752720/
>> >>> https://lkml.org/lkml/2017/1/19/537
>> >>
>> >> That deleted the messages from nvram_scan_partitions(), but neither of
>> >> the callers of nvram_scan_paritions() check its return value or print
>> >> anything if it fails. So removing those messages would make those
>> >> failures silent which is not what we want.
>> >
>> > * How do you think about information like the following?
>> > https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/process/coding-style.rst?id=f359287765c04711ff54fbd11645271d8e5ff763#n883
>> > “…
>> > These generic allocation functions all emit a stack dump on failure when used
>> > without __GFP_NOWARN so there is no use in emitting an additional failure
>> > message when NULL is returned.
>> > …”
>>
>> Are you sure that's actually true?
>>
>> A quick look around in slub.c leads me to:
>>
>> slab_out_of_memory(struct kmem_cache *s, gfp_t gfpflags, int nid)
>> {
>> #ifdef CONFIG_SLUB_DEBUG
>
> You first have to enable EXPERT mode before you can disable SLUB_DEBUG.

I see ~175 defconfigs with CONFIG_EXPERT=y, so that's not really a high
bar unfortunately.

And there's 38 defconfigs with SLUB_DEBUG=n.

So for kernels built with those defconfigs that documentation is plain
wrong and misleading.

And then there's SLOB which doesn't dump stack anywhere AFAICS.

In fact slab_out_of_memory() doesn't emit a stack dump either, it just
prints a bunch of slab related info!

> So that hopefully means you *really* want to save memory. It doesn't
> make sense to add a bunch of memory wasting printks when the users want
> to go to extra lengths to conserve memory.

I agree that in many cases those printks are just a waste of space in
the source and the binary and should be removed.

But I dislike being told "these generic allocation functions all emit a
stack dump" only to find out that actually they don't, they print some
other debug info, and depending on config settings they actually don't
print _anything_.

cheers

2020-06-03 13:25:03

by Dan Carpenter

[permalink] [raw]
Subject: Re: [PATCH] powerpc/nvram: Replace kmalloc with kzalloc in the error message

On Wed, Jun 03, 2020 at 09:37:18PM +1000, Michael Ellerman wrote:
> Dan Carpenter <[email protected]> writes:
> > On Tue, Jun 02, 2020 at 09:23:57PM +1000, Michael Ellerman wrote:
> >> Markus Elfring <[email protected]> writes:
> >> >>>> Please just remove the message instead, it's a tiny allocation that's
> >> >>>> unlikely to ever fail, and the caller will print an error anyway.
> >> >>>
> >> >>> How do you think about to take another look at a previous update suggestion
> >> >>> like the following?
> >> >>>
> >> >>> powerpc/nvram: Delete three error messages for a failed memory allocation
> >> >>> https://patchwork.ozlabs.org/project/linuxppc-dev/patch/[email protected]/
> >> >>> https://lore.kernel.org/linuxppc-dev/[email protected]/
> >> >>> https://lore.kernel.org/patchwork/patch/752720/
> >> >>> https://lkml.org/lkml/2017/1/19/537
> >> >>
> >> >> That deleted the messages from nvram_scan_partitions(), but neither of
> >> >> the callers of nvram_scan_paritions() check its return value or print
> >> >> anything if it fails. So removing those messages would make those
> >> >> failures silent which is not what we want.
> >> >
> >> > * How do you think about information like the following?
> >> > https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/process/coding-style.rst?id=f359287765c04711ff54fbd11645271d8e5ff763#n883
> >> > “…
> >> > These generic allocation functions all emit a stack dump on failure when used
> >> > without __GFP_NOWARN so there is no use in emitting an additional failure
> >> > message when NULL is returned.
> >> > …”
> >>
> >> Are you sure that's actually true?
> >>
> >> A quick look around in slub.c leads me to:
> >>
> >> slab_out_of_memory(struct kmem_cache *s, gfp_t gfpflags, int nid)
> >> {
> >> #ifdef CONFIG_SLUB_DEBUG
> >
> > You first have to enable EXPERT mode before you can disable SLUB_DEBUG.
>
> I see ~175 defconfigs with CONFIG_EXPERT=y, so that's not really a high
> bar unfortunately.
>
> And there's 38 defconfigs with SLUB_DEBUG=n.
>
> So for kernels built with those defconfigs that documentation is plain
> wrong and misleading.
>
> And then there's SLOB which doesn't dump stack anywhere AFAICS.
>
> In fact slab_out_of_memory() doesn't emit a stack dump either, it just
> prints a bunch of slab related info!
>
> > So that hopefully means you *really* want to save memory. It doesn't
> > make sense to add a bunch of memory wasting printks when the users want
> > to go to extra lengths to conserve memory.
>
> I agree that in many cases those printks are just a waste of space in
> the source and the binary and should be removed.
>
> But I dislike being told "these generic allocation functions all emit a
> stack dump" only to find out that actually they don't, they print some
> other debug info, and depending on config settings they actually don't
> print _anything_.

Wait... It *does* print a stack trace. We must but looking at the
wrong function. Huh... The stack trace comes from warn_alloc(). What
happen is this:

mm/slub.c
2673
2674 freelist = new_slab_objects(s, gfpflags, node, &c);
2675
2676 if (unlikely(!freelist)) {
2677 slab_out_of_memory(s, gfpflags, node);
2678 return NULL;
2679 }
2680

The new_slab_objects() will call allocate_slab() which calls
__alloc_pages_slowpath() which calls warn_alloc() on failure.

There are some error paths from alloc_pages() which look like they
could return without the stack dump, but those are impossible paths from
kmalloc or error injection.

regards,
dan carpenter