LKML Archive on lore.kernel.org
help / color / mirror / Atom feed
* [PATCH] mm: memory_hotplug: use put_device() if device_register fail
@ 2018-04-26 15:42 Arvind Yadav
2018-04-27 14:56 ` Michal Hocko
0 siblings, 1 reply; 4+ messages in thread
From: Arvind Yadav @ 2018-04-26 15:42 UTC (permalink / raw)
To: gregkh; +Cc: linux-kernel
if device_register() returned an error. Always use put_device()
to give up the initialized reference and release allocated memory.
Signed-off-by: Arvind Yadav <arvind.yadav.cs@gmail.com>
---
drivers/base/memory.c | 8 +++++++-
1 file changed, 7 insertions(+), 1 deletion(-)
diff --git a/drivers/base/memory.c b/drivers/base/memory.c
index bffe861..f5e5601 100644
--- a/drivers/base/memory.c
+++ b/drivers/base/memory.c
@@ -649,13 +649,19 @@ static const struct attribute_group *memory_memblk_attr_groups[] = {
static
int register_memory(struct memory_block *memory)
{
+ int ret;
+
memory->dev.bus = &memory_subsys;
memory->dev.id = memory->start_section_nr / sections_per_block;
memory->dev.release = memory_block_release;
memory->dev.groups = memory_memblk_attr_groups;
memory->dev.offline = memory->state == MEM_OFFLINE;
- return device_register(&memory->dev);
+ ret = device_register(&memory->dev);
+ if (ret)
+ put_device(&memory->dev);
+
+ return ret;
}
static int init_memory_block(struct memory_block **memory,
--
2.7.4
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH] mm: memory_hotplug: use put_device() if device_register fail
2018-04-26 15:42 [PATCH] mm: memory_hotplug: use put_device() if device_register fail Arvind Yadav
@ 2018-04-27 14:56 ` Michal Hocko
2018-04-28 5:35 ` arvindY
0 siblings, 1 reply; 4+ messages in thread
From: Michal Hocko @ 2018-04-27 14:56 UTC (permalink / raw)
To: Arvind Yadav; +Cc: gregkh, linux-kernel
On Thu 26-04-18 21:12:09, Arvind Yadav wrote:
> if device_register() returned an error. Always use put_device()
> to give up the initialized reference and release allocated memory.
Is this patch correct? The docummentation says
* NOTE: _Never_ directly free @dev after calling this function, even
* if it returned an error! Always use put_device() to give up your
* reference instead.
but we do not have _our_ reference in this path AFAICS. Maybe this is
just a documentation issue? How have you tested this change btw.?
> Signed-off-by: Arvind Yadav <arvind.yadav.cs@gmail.com>
> ---
> drivers/base/memory.c | 8 +++++++-
> 1 file changed, 7 insertions(+), 1 deletion(-)
>
> diff --git a/drivers/base/memory.c b/drivers/base/memory.c
> index bffe861..f5e5601 100644
> --- a/drivers/base/memory.c
> +++ b/drivers/base/memory.c
> @@ -649,13 +649,19 @@ static const struct attribute_group *memory_memblk_attr_groups[] = {
> static
> int register_memory(struct memory_block *memory)
> {
> + int ret;
> +
> memory->dev.bus = &memory_subsys;
> memory->dev.id = memory->start_section_nr / sections_per_block;
> memory->dev.release = memory_block_release;
> memory->dev.groups = memory_memblk_attr_groups;
> memory->dev.offline = memory->state == MEM_OFFLINE;
>
> - return device_register(&memory->dev);
> + ret = device_register(&memory->dev);
> + if (ret)
> + put_device(&memory->dev);
> +
> + return ret;
> }
>
> static int init_memory_block(struct memory_block **memory,
> --
> 2.7.4
--
Michal Hocko
SUSE Labs
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH] mm: memory_hotplug: use put_device() if device_register fail
2018-04-27 14:56 ` Michal Hocko
@ 2018-04-28 5:35 ` arvindY
2018-05-02 14:27 ` Michal Hocko
0 siblings, 1 reply; 4+ messages in thread
From: arvindY @ 2018-04-28 5:35 UTC (permalink / raw)
To: Michal Hocko; +Cc: gregkh, linux-kernel
On Friday 27 April 2018 08:26 PM, Michal Hocko wrote:
> On Thu 26-04-18 21:12:09, Arvind Yadav wrote:
>> if device_register() returned an error. Always use put_device()
>> to give up the initialized reference and release allocated memory.
> Is this patch correct? The docummentation says
> * NOTE: _Never_ directly free @dev after calling this function, even
> * if it returned an error! Always use put_device() to give up your
> * reference instead.
>
> but we do not have _our_ reference in this path AFAICS. Maybe this is
> just a documentation issue? How have you tested this change btw.?
The document is correct. Here device_register() will initialize object by
making reference count as 1 and also increment reference count for device.
device_register() {
device_initialize()->kobject_init()->kref_init() - initialize
object( reference count = 1).
device_add()->get_device() - increment reference count for device.
}
If device_register() will fail then we have to release the object by making
reference count 0. So we need to call put_object() which will release the
object and other resources like memory etc. so long as the reference
count is nonzero, the object continue to exist in memory.
I have not tested this peace of code. But I have tested other code which
is using Kboject.
~arvind
>
>> Signed-off-by: Arvind Yadav <arvind.yadav.cs@gmail.com>
>> ---
>> drivers/base/memory.c | 8 +++++++-
>> 1 file changed, 7 insertions(+), 1 deletion(-)
>>
>> diff --git a/drivers/base/memory.c b/drivers/base/memory.c
>> index bffe861..f5e5601 100644
>> --- a/drivers/base/memory.c
>> +++ b/drivers/base/memory.c
>> @@ -649,13 +649,19 @@ static const struct attribute_group *memory_memblk_attr_groups[] = {
>> static
>> int register_memory(struct memory_block *memory)
>> {
>> + int ret;
>> +
>> memory->dev.bus = &memory_subsys;
>> memory->dev.id = memory->start_section_nr / sections_per_block;
>> memory->dev.release = memory_block_release;
>> memory->dev.groups = memory_memblk_attr_groups;
>> memory->dev.offline = memory->state == MEM_OFFLINE;
>>
>> - return device_register(&memory->dev);
>> + ret = device_register(&memory->dev);
>> + if (ret)
>> + put_device(&memory->dev);
>> +
>> + return ret;
>> }
>>
>> static int init_memory_block(struct memory_block **memory,
>> --
>> 2.7.4
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH] mm: memory_hotplug: use put_device() if device_register fail
2018-04-28 5:35 ` arvindY
@ 2018-05-02 14:27 ` Michal Hocko
0 siblings, 0 replies; 4+ messages in thread
From: Michal Hocko @ 2018-05-02 14:27 UTC (permalink / raw)
To: arvindY; +Cc: gregkh, linux-kernel
On Sat 28-04-18 11:05:44, arvindY wrote:
>
>
> On Friday 27 April 2018 08:26 PM, Michal Hocko wrote:
> > On Thu 26-04-18 21:12:09, Arvind Yadav wrote:
> > > if device_register() returned an error. Always use put_device()
> > > to give up the initialized reference and release allocated memory.
> > Is this patch correct? The docummentation says
> > * NOTE: _Never_ directly free @dev after calling this function, even
> > * if it returned an error! Always use put_device() to give up your
> > * reference instead.
> >
> > but we do not have _our_ reference in this path AFAICS. Maybe this is
> > just a documentation issue? How have you tested this change btw.?
> The document is correct. Here device_register() will initialize object by
> making reference count as 1 and also increment reference count for device.
>
> device_register() {
> device_initialize()->kobject_init()->kref_init() - initialize object(
> reference count = 1).
> device_add()->get_device() - increment reference count for device.
> }
>
> If device_register() will fail then we have to release the object by making
> reference count 0.
I am confused. If device_register intializes the reference to 1 then why
it doesn't decrement it on a failure path and rather expects the caller
to do that? The doc as I read it means that we should only drop a
reference if we have our own one before calling device_register.
Or what do I miss here?
--
Michal Hocko
SUSE Labs
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2018-05-02 14:27 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2018-04-26 15:42 [PATCH] mm: memory_hotplug: use put_device() if device_register fail Arvind Yadav
2018-04-27 14:56 ` Michal Hocko
2018-04-28 5:35 ` arvindY
2018-05-02 14:27 ` Michal Hocko
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).