Skip to contentSkip to conversation

devm_spi_register_master

DISCUSSION Julia Lawall 4 participants 11 messages Nov 11, 2013 – Nov 22, 2013 5 messages not in the archive SPI

lore
Share & tools
lore
#0 lore raw

Hello,

I was looking into the use of devm_spi_register_master. This function seems to take care of registering and unregistering, but not allocation and freeing. I have the impression that that is done with spi_alloc_master and and spi_master_put, which are not managed functions. But then I wonder how this can work? It seems that the spi_master_put function should remain in the probe or remove function, and then the object could be freed before it is unregistered?

The patch 2fe7e4add3e53df7c1b97e32076f8390dd81c6b3 introduces a call to devm_spi_register_master and also removes a call to spi_master_put. By my reasoning, this would cause a memory leak.

Am I missing something?

thanks,
julia
--
To unsubscribe from this list: send the line "unsubscribe linux-spi" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html

#1 lore raw
Quoted 11 lines · Julia Lawall

Hello,

I was looking into the use of devm_spi_register_master. This function seems to take care of registering and unregistering, but not allocation and freeing. I have the impression that that is done with spi_alloc_master and and spi_master_put, which are not managed functions. But then I wonder how this can work? It seems that the spi_master_put function should remain in the probe or remove function, and then the object could be freed before it is unregistered?

Quoted 3 lines

The patch 2fe7e4add3e53df7c1b97e32076f8390dd81c6b3 introduces a call to devm_spi_register_master and also removes a call to spi_master_put. By my reasoning, this would cause a memory leak.

Hi Julia Lawall,

I already checked that the patch 2fe7e4a "spi: txx9: use devm_spi_register_master()" is right. The call to spi_master_put() was duplicated. So, it should be removed.

When removing the driver, the following functions are called. : devm_spi_unregister()

       -> spi_unregister_master()
           -> device_unregister()
               -> put_device()

Also, spi_master_put() calls the following functions. : spi_master_put()

       -> put_device()

So, put_device() is duplicated, when devm_spi_register_master() is used. In this case, spi_master_put() can be removed.

Mark,
If I am wrong, please let me know kindly. :-)

Best regards,
Jingoo Han

--
To unsubscribe from this list: send the line "unsubscribe linux-spi" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html

A message that is not in the archive (sent off-list or not yet received): 002701cedf3b$19f5b280$4de11780$%[email protected]

#3 lore raw
Quoted 28 lines · Jingoo Han
Quoted 11 lines · Julia Lawall

Hello,

I was looking into the use of devm_spi_register_master. This function seems to take care of registering and unregistering, but not allocation and freeing. I have the impression that that is done with spi_alloc_master and and spi_master_put, which are not managed functions. But then I wonder how this can work? It seems that the spi_master_put function should remain in the probe or remove function, and then the object could be freed before it is unregistered?

Quoted 3 lines

The patch 2fe7e4add3e53df7c1b97e32076f8390dd81c6b3 introduces a call to devm_spi_register_master and also removes a call to spi_master_put. By my reasoning, this would cause a memory leak.

Hi Julia Lawall,

I already checked that the patch 2fe7e4a "spi: txx9: use devm_spi_register_master()" is right. The call to spi_master_put() was duplicated. So, it should be removed.

When removing the driver, the following functions are called. : devm_spi_unregister()

       -> spi_unregister_master()
           -> device_unregister()
               -> put_device()
Quoted 6 lines

Also, spi_master_put() calls the following functions. : spi_master_put()

       -> put_device()

So, put_device() is duplicated, when devm_spi_register_master() is used. In this case, spi_master_put() can be removed.

spi_register_master calls device_add, which seems to increment the reference count. So it may be that both calls to put_device are needed. On the other hand, in that case it would be safe to invert their order.

julia

Quoted 7 lines

Mark,
If I am wrong, please let me know kindly. :-)

Best regards,
Jingoo Han

--
To unsubscribe from this list: send the line "unsubscribe linux-spi" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html

#4 lore raw
Quoted 31 lines · Julia Lawall
Quoted 30 lines · Jingoo Han
Quoted 9 lines · Julia Lawall

I was looking into the use of devm_spi_register_master. This function seems to take care of registering and unregistering, but not allocation and freeing. I have the impression that that is done with spi_alloc_master and and spi_master_put, which are not managed functions. But then I wonder how this can work? It seems that the spi_master_put function should remain in the probe or remove function, and then the object could be freed before it is unregistered?

Quoted 3 lines

The patch 2fe7e4add3e53df7c1b97e32076f8390dd81c6b3 introduces a call to devm_spi_register_master and also removes a call to spi_master_put. By my reasoning, this would cause a memory leak.

Hi Julia Lawall,

I already checked that the patch 2fe7e4a "spi: txx9: use devm_spi_register_master()" is right. The call to spi_master_put() was duplicated. So, it should be removed.

When removing the driver, the following functions are called. : devm_spi_unregister()

       -> spi_unregister_master()
           -> device_unregister()
               -> put_device()

Also, spi_master_put() calls the following functions. : spi_master_put()

       -> put_device()
Quoted 7 lines
Quoted 2 lines

So, put_device() is duplicated, when devm_spi_register_master() is used. In this case, spi_master_put() can be removed.

spi_register_master calls device_add, which seems to increment the reference count. So it may be that both calls to put_device are needed. On the other hand, in that case it would be safe to invert their order.

Hi Mark,

By the way, now I am testing s3c64xx by repeating modprobe & rmmod. However, after s3c64xx_spi_remove() by rmmod, devm_spi_unregister() is not called. So, at the next calling modprobe, it makes kernel panic. On the other hand, When spi_unregister_master()& spi_master_put() are added, the kernel panic does not happen.

Best regards,
Jingoo Han

--
To unsubscribe from this list: send the line "unsubscribe linux-spi" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html

A message that is not in the archive (sent off-list or not yet received): 000301cedf73$2ae7a3e0$80b6eba0$%[email protected]

#6 lore raw
Quoted 7 lines · Jingoo Han

Hi Mark,

By the way, now I am testing s3c64xx by repeating modprobe & rmmod. However, after s3c64xx_spi_remove() by rmmod, devm_spi_unregister() is not called. So, at the next calling modprobe, it makes kernel panic. On the other hand, When spi_unregister_master()& spi_master_put() are added, the kernel panic does not happen.

Hi Stephen Warren,

I tested your patch "spi: core: invert success test in devm_spi_register_master" with spi-s3c64xx.ko. I checked that the kernel panic is resolved. Thank you for your patch. :-)

Best regards,
Jingoo Han

--
To unsubscribe from this list: send the line "unsubscribe linux-spi" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html

#7 lore raw
In reply to Julia Lawall
Quoted 43 lines

-----Original Message-----
From: Julia Lawall [mailto:[email protected]]
Sent: Tuesday, November 12, 2013 12:19 PM
To: Jingoo Han
Cc: 'Mark Brown'; [email protected]
Subject: Re: devm_spi_register_master

Quoted 32 lines · Jingoo Han
Quoted 11 lines · Julia Lawall

Hello,

I was looking into the use of devm_spi_register_master. This function seems to take care of registering and unregistering, but not allocation and freeing. I have the impression that that is done with spi_alloc_master and and spi_master_put, which are not managed functions. But then I wonder how this can work? It seems that the spi_master_put function should remain in the probe or remove function, and then the object could be freed before it is unregistered?

Quoted 3 lines

The patch 2fe7e4add3e53df7c1b97e32076f8390dd81c6b3 introduces a call to devm_spi_register_master and also removes a call to spi_master_put. By my reasoning, this would cause a memory leak.

Hi Julia Lawall,

I already checked that the patch 2fe7e4a "spi: txx9: use devm_spi_register_master()" is right. The call to spi_master_put() was duplicated. So, it should be removed.

When removing the driver, the following functions are called. : devm_spi_unregister()

       -> spi_unregister_master()
           -> device_unregister()
               -> put_device()

Also, spi_master_put() calls the following functions. : spi_master_put()

       -> put_device()
Quoted 7 lines
Quoted 2 lines

So, put_device() is duplicated, when devm_spi_register_master() is used. In this case, spi_master_put() can be removed.

spi_register_master calls device_add, which seems to increment the reference count. So it may be that both calls to put_device are needed. On the other hand, in that case it would be safe to invert their order.

Then, how about the following patch submitted by Wei Yongjun?
http://www.spinics.net/lists/arm-kernel/msg286576.html

Best regards,
Jingoo Han

--
To unsubscribe from this list: send the line "unsubscribe linux-spi" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html

A message that is not in the archive (sent off-list or not yet received): 000e01cee1dc$12a4fba0$37eef2e0$%[email protected]

#9 lore raw
Quoted 40 lines · Jingoo Han
Quoted 40 lines

-----Original Message-----
From: Julia Lawall [mailto:[email protected]]
Sent: Tuesday, November 12, 2013 12:19 PM
To: Jingoo Han
Cc: 'Mark Brown'; [email protected]
Subject: Re: devm_spi_register_master

Quoted 30 lines · Jingoo Han
Quoted 10 lines · Julia Lawall

Hello,

I was looking into the use of devm_spi_register_master. This function seems to take care of registering and unregistering, but not allocation and freeing. I have the impression that that is done with spi_alloc_master and and spi_master_put, which are not managed functions. But then I wonder how this can work? It seems that the spi_master_put function should remain in the probe or remove function, and then the object could be freed before it is unregistered?

Quoted 3 lines

The patch 2fe7e4add3e53df7c1b97e32076f8390dd81c6b3 introduces a call to devm_spi_register_master and also removes a call to spi_master_put. By my reasoning, this would cause a memory leak.

Hi Julia Lawall,

I already checked that the patch 2fe7e4a "spi: txx9: use devm_spi_register_master()" is right. The call to spi_master_put() was duplicated. So, it should be removed.

When removing the driver, the following functions are called. : devm_spi_unregister()

       -> spi_unregister_master()
           -> device_unregister()
               -> put_device()

Also, spi_master_put() calls the following functions. : spi_master_put()

       -> put_device()
Quoted 8 lines
Quoted 6 lines
Quoted 2 lines

So, put_device() is duplicated, when devm_spi_register_master() is used. In this case, spi_master_put() can be removed.

spi_register_master calls device_add, which seems to increment the reference count. So it may be that both calls to put_device are needed. On the other hand, in that case it would be safe to invert their order.

Then, how about the following patch submitted by Wei Yongjun?
http://www.spinics.net/lists/arm-kernel/msg286576.html

spi_master_get() should be moved.

probe:

	spi_alloc_master        	-> Init reference count to 1	=> 1
	devm_spi_register_master	-> Not increment reference count => 1
        (It seems device_add() does not hold a reference)
remove:
	spi_master_get			-> increment reference count => 2
	devm_spi_unregister		-> decrement reference count => 1

So after remove, the reference count still 1.

If devm_spi_register_master() hold a reference, we will need a extra spi_master_put() in remove to let master be freed.

--
To unsubscribe from this list: send the line "unsubscribe linux-spi" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html

A message that is not in the archive (sent off-list or not yet received): CAPgLHd9hFNuke_cfs9QrQ0E0E_QmRfN3=fESUtBH6PPGgx=Wbw-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org

#11 lore raw
Quoted 4 lines
Quoted 2 lines

Then, how about the following patch submitted by Wei Yongjun?
http://www.spinics.net/lists/arm-kernel/msg286576.html

spi_master_get() should be moved.

OK, I see now that that is the issue.

Quoted 4 lines

probe:

	spi_alloc_master        	-> Init reference count to 1	=> 1
	devm_spi_register_master	-> Not increment reference count => 1
        (It seems device_add() does not hold a reference)

Indeed. It acquires a reference, but it releases it at the end.

Quoted 8 lines

remove:

	spi_master_get			-> increment reference count => 2
	devm_spi_unregister		-> decrement reference count => 1

So after remove, the reference count still 1.

If devm_spi_register_master() hold a reference, we will need a extra spi_master_put() in remove to let master be freed.

OK, I think that everything is OK with just getting rid of the get and the put in the remove function. Thanks for the explanations.

julia
--
To unsubscribe from this list: send the line "unsubscribe linux-spi" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html

#12 lore raw
In reply to Julia Lawall
Quoted 3 lines · Julia Lawall

I was looking into the use of devm_spi_register_master. This function seems to take care of registering and unregistering, but not allocation and freeing. I have the impression that that is done with

Please CC maintainers on things using the addresses from MAINTAINERS - I didn't read these until now because you sent the mail to my work address which mostly gets duplicates of mails to the account I normally use for upstream so I tend not to pay so much attention to upstream mail sent there. There's also other Mark Brown's working on the kernel so you might've got the wrong one!

I think this has been addressed by the patches that Wei sent or are there still outstanding problems?

A message that is not in the archive (sent off-list or not yet received): [email protected]

#14 lore raw
Quoted 6 lines · Mark Brown
Quoted 3 lines · Julia Lawall

I was looking into the use of devm_spi_register_master. This function seems to take care of registering and unregistering, but not allocation and freeing. I have the impression that that is done with

Quoted 6 lines

Please CC maintainers on things using the addresses from MAINTAINERS - I didn't read these until now because you sent the mail to my work address which mostly gets duplicates of mails to the account I normally use for upstream so I tend not to pay so much attention to upstream mail sent there. There's also other Mark Brown's working on the kernel so you might've got the wrong one!

Sorry about that. I think I took the address from a patch, which was obviously not the right thing to do.

Quoted 2 lines

I think this has been addressed by the patches that Wei sent or are there still outstanding problems?

I didn't have a chance to check yet. I will take a look now.

thanks,
julia
--
To unsubscribe from this list: send the line "unsubscribe linux-spi" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html

#15 lore raw
Quoted 2 lines · Julia Lawall

Sorry about that. I think I took the address from a patch, which was obviously not the right thing to do.

No, that's fine and reasonable - just add in the maintainer addresses for the subsystem as well so maintainers see it and that'll deal with the issue.

Quoted 4 lines
Quoted 2 lines

I think this has been addressed by the patches that Wei sent or are there still outstanding problems?

I didn't have a chance to check yet. I will take a look now.

There's one of the patches Wei sent that I didn't apply yet since I'm waiting for review from the maintainer.