From: Christophe JAILLET <christophe.jaillet@wanadoo.fr>
To: Michal Hocko <mhocko@suse.com>, David Hildenbrand <david@redhat.com>
Cc: Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
"Rafael J. Wysocki" <rafael@kernel.org>,
Scott Cheloha <cheloha@linux.vnet.ibm.com>,
Andrew Morton <akpm@linux-foundation.org>,
Nathan Lynch <nathanl@linux.ibm.com>,
linux-kernel@vger.kernel.org, kernel-janitors@vger.kernel.org
Subject: Re: [PATCH] drivers/base/memory: Fix a reference counting issue in __add_memory_block()
Date: Fri, 22 Apr 2022 10:01:48 +0200 [thread overview]
Message-ID: <ef8e4a32-bc05-2632-bc09-00a20faf49dd@wanadoo.fr> (raw)
In-Reply-To: <YmJaoSam6g95yToH@dhcp22.suse.cz>
Le 22/04/2022 à 09:34, Michal Hocko a écrit :
> On Fri 22-04-22 09:15:21, Christophe JAILLET wrote:
>> There is no point in doing put_device()/device_unregister() on a
>> device that has just been registered a few lines above. This will lead to
>> a double reference decrement.
>
> This is a bit confusing. I would rephrase.
> "
> __add_memory_block calls both put_device and device_unregister when
> storing the memory block into the xarray. This is incorrect because
> xarray doesn't take an additional reference and device_unregister
> already calls put_device.
> "
>
> Btw. I do not think this failure path can be triggered, or is there a
> way to hit it?
>
>> I guess that this put_device()/device_unregister() is a cut'n'paste from
>> remove_memory_block() (i.e. unregister_memory() at the time being) which
>> does need it.
>>
>> Fixes: 4fb6eabf1037 ("drivers/base/memory.c: cache memory blocks in xarray to accelerate lookup")
>> Signed-off-by: Christophe JAILLET <christophe.jaillet@wanadoo.fr>
>
> Other than that looks good to me. With the changelog clarified,
> especially the part that evaluates whether this is a real or a
> theoretical problem, feel free to add
> Acked-by: Michal Hocko <mhocko@suse.com>
I'll send a v2 with the updated changelog.
I do agree that this is certainly a theoretical issue.
Moreover, should it be triggered, I think that it would only print a
warning message about an erroneous decrement on a ref counted resource
that is already 0.
Well, it also saves 2 LoC and will look more logical to other readers
(and static checkers :) )
Thanks for the quick review for both of you.
CJ
>
> Thanks!
>
>> ---
>> drivers/base/memory.c | 5 ++---
>> 1 file changed, 2 insertions(+), 3 deletions(-)
>>
>> diff --git a/drivers/base/memory.c b/drivers/base/memory.c
>> index 7222ff9b5e05..084d67fd55cc 100644
>> --- a/drivers/base/memory.c
>> +++ b/drivers/base/memory.c
>> @@ -636,10 +636,9 @@ static int __add_memory_block(struct memory_block *memory)
>> }
>> ret = xa_err(xa_store(&memory_blocks, memory->dev.id, memory,
>> GFP_KERNEL));
>> - if (ret) {
>> - put_device(&memory->dev);
>> + if (ret)
>> device_unregister(&memory->dev);
>> - }
>> +
>> return ret;
>> }
>>
>> --
>> 2.32.0
>
next prev parent reply other threads:[~2022-04-22 8:01 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-04-22 7:15 [PATCH] drivers/base/memory: Fix a reference counting issue in __add_memory_block() Christophe JAILLET
2022-04-22 7:34 ` Michal Hocko
2022-04-22 8:01 ` Christophe JAILLET [this message]
2022-04-22 7:39 ` David Hildenbrand
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=ef8e4a32-bc05-2632-bc09-00a20faf49dd@wanadoo.fr \
--to=christophe.jaillet@wanadoo.fr \
--cc=akpm@linux-foundation.org \
--cc=cheloha@linux.vnet.ibm.com \
--cc=david@redhat.com \
--cc=gregkh@linuxfoundation.org \
--cc=kernel-janitors@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=mhocko@suse.com \
--cc=nathanl@linux.ibm.com \
--cc=rafael@kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox