Skip to contentSkip to conversation

spi: Avoid calling spi_slave_abort() with kfreed spidev

PATCH In review Lukasz Majewski 3 participants 6 messages Oct 1, 2019 SPI

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 spi: Avoid calling spi_slave_abort() with kfreed spidev

Call spi_slave_abort() only when the spidev->spi is !NULL and the structure hasn't already been kfreed.

---
This fix applies on:
repo: https://kernel.googlesource.com/pub/scm/linux/kernel/git/broonie/spi.git
branch: for-5.4
SHA1: 6b04e47b73f2a0d2c330cecca99f8e2cb8f85b34

---
 drivers/spi/spidev.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
drivers/spi/spidev.c +4 -2
index 3ea9d8a3e6e8..2c6d4dbeebac 100644
@@ -600,15 +600,16 @@ static int spidev_open(struct inode *inode, struct file *filp)
 static int spidev_release(struct inode *inode, struct file *filp)
 {
 	struct spidev_data	*spidev;
+	int dofree;
 
 	mutex_lock(&device_list_lock);
 	spidev = filp->private_data;
 	filp->private_data = NULL;
+	dofree = 0;
 
 	/* last close? */
 	spidev->users--;
 	if (!spidev->users) {
-		int		dofree;
 
 		kfree(spidev->tx_buffer);
 		spidev->tx_buffer = NULL;
@@ -628,7 +629,8 @@ static int spidev_release(struct inode *inode, struct file *filp)
 			kfree(spidev);
 	}
 #ifdef CONFIG_SPI_SLAVE
-	spi_slave_abort(spidev->spi);
+	if (!dofree)
+		spi_slave_abort(spidev->spi);
 #endif
 	mutex_unlock(&device_list_lock);
 
Signature · 1 line
2.20.1
#1 lore raw

Hi Lukasz,

Quoted 7 lines · Lukasz Majewski

Call spi_slave_abort() only when the spidev->spi is !NULL and the structure hasn't already been kfreed.

Thanks for your patch!

drivers/spi/spidev.c static int spidev_open(struct inode *inode, struct file *filp) · line 603

Quoted 7 lines
drivers/spi/spidev.c +1 -0
@@ -600,15 +600,16 @@ static int spidev_open(struct inode *inode, struct file *filp)
 static int spidev_release(struct inode *inode, struct file *filp)
 {
        struct spidev_data      *spidev;
+       int dofree;

bool?

drivers/spi/spidev.c static int spidev_open(struct inode *inode, struct file *filp) · line 608

Quoted 5 lines
drivers/spi/spidev.c +1 -0
        mutex_lock(&device_list_lock);
        spidev = filp->private_data;
        filp->private_data = NULL;
+       dofree = 0;

Why not initialize it at declaration time?

Quoted 9 lines
drivers/spi/spidev.c +0 -1
        /* last close? */
        spidev->users--;
        if (!spidev->users) {
-               int             dofree;
 
                kfree(spidev->tx_buffer);
                spidev->tx_buffer = NULL;

drivers/spi/spidev.c static int spidev_release(struct inode *inode, struct file *filp) · line 633

Quoted 6 lines
drivers/spi/spidev.c +2 -1
@@ -628,7 +629,8 @@ static int spidev_release(struct inode *inode, struct file *filp)
                        kfree(spidev);
        }
 #ifdef CONFIG_SPI_SLAVE
-       spi_slave_abort(spidev->spi);
+       if (!dofree)
+               spi_slave_abort(spidev->spi);

Can spidev->spi be NULL, if spidev->users != 0?

drivers/spi/spidev.c static int spidev_release(struct inode *inode, struct file *filp) · line 635

Quoted 2 lines
drivers/spi/spidev.c +0 -0
 #endif
        mutex_unlock(&device_list_lock);

Gr{oetje,eeting}s,

Geert

Signature · 5 lines
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- [email protected]

In personal conversations with technical people, I call myself a hacker. But
when I'm talking to journalists I just say "programmer" or something like that.
                                -- Linus Torvalds
#2 lore raw

Hi Geert,

Thank you for a very prompt response.

Quoted 13 lines

Hi Lukasz,

Quoted 7 lines · Lukasz Majewski

Call spi_slave_abort() only when the spidev->spi is !NULL and the structure hasn't already been kfreed.

Thanks for your patch!

Quoted 9 lines

drivers/spi/spidev.c static int spidev_open(struct inode *inode, · line 603

Quoted 7 lines
drivers/spi/spidev.c +1 -0
@@ -600,15 +600,16 @@ static int spidev_open(struct inode *inode,
 struct file *filp) static int spidev_release(struct inode *inode,
 struct file *filp) {
        struct spidev_data      *spidev;
+       int dofree;  

bool?

It may be bool, yes - I took this "int" from the original code (further down in the patch), as I've moved it a bit up.

Quoted 8 lines

drivers/spi/spidev.c static int spidev_open(struct inode *inode, struct file *filp) · line 608

Quoted 6 lines
drivers/spi/spidev.c +1 -0
        mutex_lock(&device_list_lock);
        spidev = filp->private_data;
        filp->private_data = NULL;
+       dofree = 0;  

Why not initialize it at declaration time?

I wanted to have it protected by mutex_lock() above. However, this also shall work with the initialization at declaration time.

Quoted 7 lines
Quoted 7 lines
drivers/spi/spidev.c +0 -1
        /* last close? */
        spidev->users--;
        if (!spidev->users) {
-               int             dofree;
 
Quoted 11 lines

drivers/spi/spidev.c static int spidev_release(struct inode *inode, · line 633

Quoted 9 lines
drivers/spi/spidev.c +2 -1
                kfree(spidev->tx_buffer);
                spidev->tx_buffer = NULL;
@@ -628,7 +629,8 @@ static int spidev_release(struct inode *inode,
 struct file *filp) kfree(spidev);
        }
 #ifdef CONFIG_SPI_SLAVE
-       spi_slave_abort(spidev->spi);
+       if (!dofree)
+               spi_slave_abort(spidev->spi);  

Can spidev->spi be NULL, if spidev->users != 0?

No, it shouldn't be.

The "dofree" is only set to true (the spidev->spi == NULL condition is checked) if there are no references (spidev->users == 0).

The if (!dofree) prevents from calling spi_slave_abort() when spidev->spi == NULL and spidev is kfree'd.

Quoted 8 lines

drivers/spi/spidev.c static int spidev_release(struct inode *inode, struct file *filp) · line 635

Quoted 3 lines
drivers/spi/spidev.c +0 -0
 #endif
        mutex_unlock(&device_list_lock);  

Gr{oetje,eeting}s,

Geert

Best regards,

Lukasz Majewski

--

DENX Software Engineering GmbH, Managing Director: Wolfgang Denk
HRB 165235 Munich, Office: Kirchenstr.5, D-82194 Groebenzell, Germany
Phone: (+49)-8142-66989-59 Fax: (+49)-8142-66989-80 Email: [email protected]

#3 lore raw

Hi Lukasz,

Quoted 39 lines · Lukasz Majewski
Quoted 27 lines · Lukasz Majewski
Quoted 26 lines

Call spi_slave_abort() only when the spidev->spi is !NULL and the structure hasn't already been kfreed.

drivers/spi/spidev.c +2 -1
@@ -600,15 +600,16 @@ static int spidev_open(struct inode *inode,
 struct file *filp) static int spidev_release(struct inode *inode,
 struct file *filp) {
        struct spidev_data      *spidev;
+       int dofree;
 
        mutex_lock(&device_list_lock);
        spidev = filp->private_data;
        filp->private_data = NULL;
+       dofree = 0;
 
        /* last close? */
        spidev->users--;
        if (!spidev->users) {
-               int             dofree;
 
Quoted 11 lines

drivers/spi/spidev.c static int spidev_release(struct inode *inode, · line 633

Quoted 9 lines
drivers/spi/spidev.c +2 -1
                kfree(spidev->tx_buffer);
                spidev->tx_buffer = NULL;
@@ -628,7 +629,8 @@ static int spidev_release(struct inode *inode,
 struct file *filp) kfree(spidev);
        }
 #ifdef CONFIG_SPI_SLAVE
-       spi_slave_abort(spidev->spi);
+       if (!dofree)
+               spi_slave_abort(spidev->spi);

Can spidev->spi be NULL, if spidev->users != 0?

Quoted 7 lines

No, it shouldn't be.

The "dofree" is only set to true (the spidev->spi == NULL condition is checked) if there are no references (spidev->users == 0).

The if (!dofree) prevents from calling spi_slave_abort() when spidev->spi == NULL and spidev is kfree'd.

If spidev->users != 0, the block checking spidev->spi == NULL is never executed, and spi_slave_abort() will be called.

I'm wondering if spidev->spi can be NULL if spidev->users is still positive.

Gr{oetje,eeting}s,

Geert

Signature · 5 lines
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- [email protected]

In personal conversations with technical people, I call myself a hacker. But
when I'm talking to journalists I just say "programmer" or something like that.
                                -- Linus Torvalds
#4 lore raw

Hi Geert,

Quoted 45 lines

Hi Lukasz,

Quoted 42 lines · Lukasz Majewski
Quoted 28 lines
Quoted 26 lines · Lukasz Majewski

Call spi_slave_abort() only when the spidev->spi is !NULL and the structure hasn't already been kfreed.

drivers/spi/spidev.c +2 -1
@@ -600,15 +600,16 @@ static int spidev_open(struct inode
 *inode, struct file *filp) static int spidev_release(struct
 inode *inode, struct file *filp) {
        struct spidev_data      *spidev;
+       int dofree;
 
        mutex_lock(&device_list_lock);
        spidev = filp->private_data;
        filp->private_data = NULL;
+       dofree = 0;
 
        /* last close? */
        spidev->users--;
        if (!spidev->users) {
-               int             dofree;
 
Quoted 11 lines

drivers/spi/spidev.c static int spidev_release(struct inode · line 633

Quoted 9 lines
drivers/spi/spidev.c +2 -1
                kfree(spidev->tx_buffer);
                spidev->tx_buffer = NULL;
@@ -628,7 +629,8 @@ static int spidev_release(struct inode
 *inode, struct file *filp) kfree(spidev);
        }
 #ifdef CONFIG_SPI_SLAVE
-       spi_slave_abort(spidev->spi);
+       if (!dofree)
+               spi_slave_abort(spidev->spi);  

Can spidev->spi be NULL, if spidev->users != 0?

No, it shouldn't be.

Quoted 8 lines
Quoted 5 lines

The "dofree" is only set to true (the spidev->spi == NULL condition is checked) if there are no references (spidev->users == 0).

The if (!dofree) prevents from calling spi_slave_abort() when spidev->spi == NULL and spidev is kfree'd.

If spidev->users != 0, the block checking spidev->spi == NULL is never executed, and spi_slave_abort() will be called.

Yes, this is correct. My other patch [1] clears the FIFOs in SPI IP block and ends (if there are any stalled) DMA transactions.

Quoted 3 lines

I'm wondering if spidev->spi can be NULL if spidev->users is still positive.

I think that it cannot.

From my tests [2] - when I do enter spi_slave_abort() function the state of
spidev->users: 0 dofree: 0 spidev->spi: 0x51337072

So it is possible to call the spidev_release without previously setting spidev->spi to NULL (which is done in spidev_remove() function).

IMHO the above behavior also seems to be correct, as during distortion the slave losts synchronization from master.

The spidev_remove() callback is part of spi_device struct and is called when the device is removed (rmmod spi_fsl_dspi).

From my tests the spidev_release() is NOT called after spidev_remove(), so the code in former seems to be a dead one.

Or maybe there is an use case which causes calling spidev_release() after spidev_remove()?

Quoted 5 lines

Gr{oetje,eeting}s,

Geert

Note:

[1] - https://lkml.org/lkml/2019/9/24/245
[2] -
https://github.com/lmajewski/tests-spi/blob/master/tests/spi/spi_tests.sh

HW setup: HW loopback with two /dev/spidevX.Y devices used

Best regards,

Lukasz Majewski

--

DENX Software Engineering GmbH, Managing Director: Wolfgang Denk
HRB 165235 Munich, Office: Kirchenstr.5, D-82194 Groebenzell, Germany
Phone: (+49)-8142-66989-59 Fax: (+49)-8142-66989-80 Email: [email protected]

maintainer
#5 lore raw
In reply to Geert Uytterhoeven
Quoted 10 lines · Geert Uytterhoeven
Quoted 4 lines · Lukasz Majewski
Quoted 1 line · Lukasz Majewski

The if (!dofree) prevents from calling spi_slave_abort() when spidev->spi == NULL and spidev is kfree'd.

If spidev->users != 0, the block checking spidev->spi == NULL is never executed, and spi_slave_abort() will be called.

I'm wondering if spidev->spi can be NULL if spidev->users is still positive.

It *shouldn't* be. I think we have other problems if it is.