* Re: [syzbot] [usb?] KASAN: invalid-free in dev_free
2024-08-16 20:25 [syzbot] [usb?] KASAN: invalid-free in dev_free syzbot
@ 2024-09-16 1:24 ` syzbot
2024-11-01 23:26 ` Andrey Konovalov
2024-10-30 6:39 ` [syzbot] [PATCH] usb: raw_gadget: Fix a KMSAN double free bug in raw_release syzbot
2024-11-02 22:00 ` [syzbot] [PATCH] usb: raw_gadget: Fix a KASAN double-free " syzbot
2 siblings, 1 reply; 7+ messages in thread
From: syzbot @ 2024-09-16 1:24 UTC (permalink / raw)
To: andreyknvl, gregkh, linux-kernel, linux-usb, syzkaller-bugs
syzbot has found a reproducer for the following issue on:
HEAD commit: 68d4209158f4 sub: cdns3: Use predefined PCI vendor ID cons..
git tree: https://git.kernel.org/pub/scm/linux/kernel/git/gregkh/usb.git usb-testing
console output: https://syzkaller.appspot.com/x/log.txt?x=10a96200580000
kernel config: https://syzkaller.appspot.com/x/.config?x=cb61872d4d8c5df9
dashboard link: https://syzkaller.appspot.com/bug?extid=3e563d99e70973c0755c
compiler: gcc (Debian 12.2.0-14) 12.2.0, GNU ld (GNU Binutils for Debian) 2.40
syz repro: https://syzkaller.appspot.com/x/repro.syz?x=1297cc07980000
C reproducer: https://syzkaller.appspot.com/x/repro.c?x=1217c8a9980000
Downloadable assets:
disk image: https://storage.googleapis.com/syzbot-assets/c69290425359/disk-68d42091.raw.xz
vmlinux: https://storage.googleapis.com/syzbot-assets/caf4f26a3e85/vmlinux-68d42091.xz
kernel image: https://storage.googleapis.com/syzbot-assets/3acdec4b62e6/bzImage-68d42091.xz
IMPORTANT: if you fix the issue, please add the following tag to the commit:
Reported-by: syzbot+3e563d99e70973c0755c@syzkaller.appspotmail.com
==================================================================
BUG: KASAN: double-free in dev_free+0x446/0x700 drivers/usb/gadget/legacy/raw_gadget.c:225
Free of addr ffff8881066f9240 by task syz-executor254/3953
CPU: 1 UID: 0 PID: 3953 Comm: syz-executor254 Not tainted 6.11.0-rc7-syzkaller-00152-g68d4209158f4 #0
Hardware name: Google Google Compute Engine/Google Compute Engine, BIOS Google 08/06/2024
Call Trace:
<TASK>
__dump_stack lib/dump_stack.c:93 [inline]
dump_stack_lvl+0x116/0x1f0 lib/dump_stack.c:119
print_address_description mm/kasan/report.c:377 [inline]
print_report+0xc3/0x620 mm/kasan/report.c:488
kasan_report_invalid_free+0xaa/0xd0 mm/kasan/report.c:563
poison_slab_object+0x135/0x160 mm/kasan/common.c:232
__kasan_slab_free+0x14/0x30 mm/kasan/common.c:256
kasan_slab_free include/linux/kasan.h:184 [inline]
slab_free_hook mm/slub.c:2256 [inline]
slab_free mm/slub.c:4477 [inline]
kfree+0x10b/0x380 mm/slub.c:4598
dev_free+0x446/0x700 drivers/usb/gadget/legacy/raw_gadget.c:225
kref_put include/linux/kref.h:65 [inline]
raw_release+0x16e/0x2c0 drivers/usb/gadget/legacy/raw_gadget.c:473
__fput+0x408/0xbb0 fs/file_table.c:422
task_work_run+0x14e/0x250 kernel/task_work.c:228
exit_task_work include/linux/task_work.h:40 [inline]
do_exit+0xaa3/0x2b30 kernel/exit.c:882
do_group_exit+0xd3/0x2a0 kernel/exit.c:1031
__do_sys_exit_group kernel/exit.c:1042 [inline]
__se_sys_exit_group kernel/exit.c:1040 [inline]
__x64_sys_exit_group+0x3e/0x50 kernel/exit.c:1040
x64_sys_call+0x14a9/0x16a0 arch/x86/include/generated/asm/syscalls_64.h:232
do_syscall_x64 arch/x86/entry/common.c:52 [inline]
do_syscall_64+0xcd/0x250 arch/x86/entry/common.c:83
entry_SYSCALL_64_after_hwframe+0x77/0x7f
RIP: 0033:0x7ffa254cedf9
Code: Unable to access opcode bytes at 0x7ffa254cedcf.
RSP: 002b:00007fffc9f7d918 EFLAGS: 00000246 ORIG_RAX: 00000000000000e7
RAX: ffffffffffffffda RBX: 0000000000000000 RCX: 00007ffa254cedf9
RDX: 000000000000003c RSI: 00000000000000e7 RDI: 0000000000000000
RBP: 00007ffa255583b0 R08: ffffffffffffffb0 R09: 00007fffc9f7d9a0
R10: 00007fffc9f7d9a0 R11: 0000000000000246 R12: 00007ffa255583b0
R13: 0000000000000000 R14: 00007ffa2555c1e0 R15: 00007ffa25499680
</TASK>
Allocated by task 3955:
kasan_save_stack+0x33/0x60 mm/kasan/common.c:47
kasan_save_track+0x14/0x30 mm/kasan/common.c:68
poison_kmalloc_redzone mm/kasan/common.c:370 [inline]
__kasan_kmalloc+0x8f/0xa0 mm/kasan/common.c:387
kasan_kmalloc include/linux/kasan.h:211 [inline]
__do_kmalloc_node mm/slub.c:4162 [inline]
__kmalloc_node_track_caller_noprof+0x1ff/0x3e0 mm/slub.c:4181
memdup_user+0x2a/0xd0 mm/util.c:226
raw_ioctl_ep_enable drivers/usb/gadget/legacy/raw_gadget.c:847 [inline]
raw_ioctl+0xbca/0x2b90 drivers/usb/gadget/legacy/raw_gadget.c:1318
vfs_ioctl fs/ioctl.c:51 [inline]
__do_sys_ioctl fs/ioctl.c:907 [inline]
__se_sys_ioctl fs/ioctl.c:893 [inline]
__x64_sys_ioctl+0x193/0x220 fs/ioctl.c:893
do_syscall_x64 arch/x86/entry/common.c:52 [inline]
do_syscall_64+0xcd/0x250 arch/x86/entry/common.c:83
entry_SYSCALL_64_after_hwframe+0x77/0x7f
Freed by task 3954:
kasan_save_stack+0x33/0x60 mm/kasan/common.c:47
kasan_save_track+0x14/0x30 mm/kasan/common.c:68
kasan_save_free_info+0x3b/0x60 mm/kasan/generic.c:579
poison_slab_object+0xf7/0x160 mm/kasan/common.c:240
__kasan_slab_free+0x14/0x30 mm/kasan/common.c:256
kasan_slab_free include/linux/kasan.h:184 [inline]
slab_free_hook mm/slub.c:2256 [inline]
slab_free mm/slub.c:4477 [inline]
kfree+0x10b/0x380 mm/slub.c:4598
dev_free+0x446/0x700 drivers/usb/gadget/legacy/raw_gadget.c:225
kref_put include/linux/kref.h:65 [inline]
raw_release+0x16e/0x2c0 drivers/usb/gadget/legacy/raw_gadget.c:473
__fput+0x408/0xbb0 fs/file_table.c:422
__fput_sync+0x47/0x50 fs/file_table.c:507
__do_sys_close fs/open.c:1566 [inline]
__se_sys_close fs/open.c:1551 [inline]
__x64_sys_close+0x86/0x100 fs/open.c:1551
do_syscall_x64 arch/x86/entry/common.c:52 [inline]
do_syscall_64+0xcd/0x250 arch/x86/entry/common.c:83
entry_SYSCALL_64_after_hwframe+0x77/0x7f
The buggy address belongs to the object at ffff8881066f9240
which belongs to the cache kmalloc-16 of size 16
The buggy address is located 0 bytes inside of
16-byte region [ffff8881066f9240, ffff8881066f9250)
The buggy address belongs to the physical page:
page: refcount:1 mapcount:0 mapping:0000000000000000 index:0x0 pfn:0x1066f9
anon flags: 0x200000000000000(node=0|zone=2)
page_type: 0xfdffffff(slab)
raw: 0200000000000000 ffff888100041640 0000000000000000 dead000000000001
raw: 0000000000000000 0000000000800080 00000001fdffffff 0000000000000000
page dumped because: kasan: bad access detected
page_owner tracks the page as allocated
page last allocated via order 0, migratetype Unmovable, gfp_mask 0xd2cc0(GFP_KERNEL|__GFP_NOWARN|__GFP_NORETRY|__GFP_COMP|__GFP_NOMEMALLOC), pid 1, tgid 1 (swapper/0), ts 4772382410, free_ts 0
create_dummy_stack mm/page_owner.c:94 [inline]
register_dummy_stack+0x8a/0xd0 mm/page_owner.c:100
init_page_owner+0x48/0xbe0 mm/page_owner.c:118
invoke_init_callbacks mm/page_ext.c:148 [inline]
page_ext_init+0x725/0xbf0 mm/page_ext.c:497
mm_core_init+0x202/0x240 mm/mm_init.c:2673
page_owner free stack trace missing
Memory state around the buggy address:
ffff8881066f9100: 00 05 fc fc 00 00 fc fc 00 00 fc fc 00 00 fc fc
ffff8881066f9180: 00 00 fc fc 00 00 fc fc 00 00 fc fc fa fb fc fc
>ffff8881066f9200: 00 00 fc fc fa fb fc fc fa fb fc fc 00 00 fc fc
^
ffff8881066f9280: 00 00 fc fc fa fb fc fc fa fb fc fc 00 00 fc fc
ffff8881066f9300: 00 00 fc fc 00 05 fc fc fa fb fc fc 00 00 fc fc
==================================================================
---
If you want syzbot to run the reproducer, reply with:
#syz test: git://repo/address.git branch-or-commit-hash
If you attach or paste a git patch, syzbot will apply it before testing.
^ permalink raw reply [flat|nested] 7+ messages in thread* Re: [syzbot] [usb?] KASAN: invalid-free in dev_free
2024-09-16 1:24 ` syzbot
@ 2024-11-01 23:26 ` Andrey Konovalov
2024-11-01 23:44 ` Chang Yu
0 siblings, 1 reply; 7+ messages in thread
From: Andrey Konovalov @ 2024-11-01 23:26 UTC (permalink / raw)
To: Chang Yu; +Cc: gregkh, linux-kernel, linux-usb, syzkaller-bugs, syzbot
On Mon, Sep 16, 2024 at 3:24 AM syzbot
<syzbot+3e563d99e70973c0755c@syzkaller.appspotmail.com> wrote:
>
> syzbot has found a reproducer for the following issue on:
>
> HEAD commit: 68d4209158f4 sub: cdns3: Use predefined PCI vendor ID cons..
> git tree: https://git.kernel.org/pub/scm/linux/kernel/git/gregkh/usb.git usb-testing
> console output: https://syzkaller.appspot.com/x/log.txt?x=10a96200580000
> kernel config: https://syzkaller.appspot.com/x/.config?x=cb61872d4d8c5df9
> dashboard link: https://syzkaller.appspot.com/bug?extid=3e563d99e70973c0755c
> compiler: gcc (Debian 12.2.0-14) 12.2.0, GNU ld (GNU Binutils for Debian) 2.40
> syz repro: https://syzkaller.appspot.com/x/repro.syz?x=1297cc07980000
> C reproducer: https://syzkaller.appspot.com/x/repro.c?x=1217c8a9980000
Hi Chang,
I saw your patch testing request for this bug [1] (thank you for
looking into it!), but your patch doesn't appear correct to me, even
though it did satisfy syzbot. One issue that I see with the patch is
calling spin_unlock_irqrestore(&dev->lock) after
kref_put(&dev->count): kref_put() might free the device struct and
spin_unlock_irqrestore() will then do a UAF.
I'm not sure what the correct patch would be though, as I don't
understand what the issue is. It seems that dev_free() indeed gets
called twice, but since it's guarded by kref_put(), this shouldn't
happen AFAIU. Or at least we should get a bad refcount report.
Thanks!
[1] https://lore.kernel.org/all/6721d497.050a0220.35b515.0001.GAE@google.com/T/#mca35ec73d63ff30d7219b6739ed0801688f15f35
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [syzbot] [usb?] KASAN: invalid-free in dev_free
2024-11-01 23:26 ` Andrey Konovalov
@ 2024-11-01 23:44 ` Chang Yu
2024-11-24 20:29 ` Andrey Konovalov
0 siblings, 1 reply; 7+ messages in thread
From: Chang Yu @ 2024-11-01 23:44 UTC (permalink / raw)
To: Andrey Konovalov
Cc: Chang Yu, gregkh, linux-kernel, linux-usb, syzkaller-bugs, syzbot
On Sat, Nov 02, 2024 at 12:26:30AM +0100, Andrey Konovalov wrote:
> On Mon, Sep 16, 2024 at 3:24 AM syzbot
> <syzbot+3e563d99e70973c0755c@syzkaller.appspotmail.com> wrote:
> >
> > syzbot has found a reproducer for the following issue on:
> >
> > HEAD commit: 68d4209158f4 sub: cdns3: Use predefined PCI vendor ID cons..
> > git tree: https://git.kernel.org/pub/scm/linux/kernel/git/gregkh/usb.git usb-testing
> > console output: https://syzkaller.appspot.com/x/log.txt?x=10a96200580000
> > kernel config: https://syzkaller.appspot.com/x/.config?x=cb61872d4d8c5df9
> > dashboard link: https://syzkaller.appspot.com/bug?extid=3e563d99e70973c0755c
> > compiler: gcc (Debian 12.2.0-14) 12.2.0, GNU ld (GNU Binutils for Debian) 2.40
> > syz repro: https://syzkaller.appspot.com/x/repro.syz?x=1297cc07980000
> > C reproducer: https://syzkaller.appspot.com/x/repro.c?x=1217c8a9980000
>
> Hi Chang,
>
> I saw your patch testing request for this bug [1] (thank you for
> looking into it!), but your patch doesn't appear correct to me, even
> though it did satisfy syzbot. One issue that I see with the patch is
> calling spin_unlock_irqrestore(&dev->lock) after
> kref_put(&dev->count): kref_put() might free the device struct and
> spin_unlock_irqrestore() will then do a UAF.
>
> I'm not sure what the correct patch would be though, as I don't
> understand what the issue is. It seems that dev_free() indeed gets
> called twice, but since it's guarded by kref_put(), this shouldn't
> happen AFAIU. Or at least we should get a bad refcount report.
>
> Thanks!
>
> [1] https://lore.kernel.org/all/6721d497.050a0220.35b515.0001.GAE@google.com/T/#mca35ec73d63ff30d7219b6739ed0801688f15f35
Hi Andrey,
Thank you for the acknowledgement! I realized that patch was incorrect
too which is why I haven't sent it out yet. I'll continue to look into
it. I'll keep you posted if I find a solution.
Thanks!
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [syzbot] [usb?] KASAN: invalid-free in dev_free
2024-11-01 23:44 ` Chang Yu
@ 2024-11-24 20:29 ` Andrey Konovalov
0 siblings, 0 replies; 7+ messages in thread
From: Andrey Konovalov @ 2024-11-24 20:29 UTC (permalink / raw)
To: Andrey Konovalov
Cc: Chang Yu, gregkh, linux-kernel, linux-usb, syzkaller-bugs, syzbot
On Sat, Nov 2, 2024 at 12:44 AM Chang Yu <marcus.yu.56@gmail.com> wrote:
>
> On Sat, Nov 02, 2024 at 12:26:30AM +0100, Andrey Konovalov wrote:
> > On Mon, Sep 16, 2024 at 3:24 AM syzbot
> > <syzbot+3e563d99e70973c0755c@syzkaller.appspotmail.com> wrote:
> > >
> > > syzbot has found a reproducer for the following issue on:
> > >
> > > HEAD commit: 68d4209158f4 sub: cdns3: Use predefined PCI vendor ID cons..
> > > git tree: https://git.kernel.org/pub/scm/linux/kernel/git/gregkh/usb.git usb-testing
> > > console output: https://syzkaller.appspot.com/x/log.txt?x=10a96200580000
> > > kernel config: https://syzkaller.appspot.com/x/.config?x=cb61872d4d8c5df9
> > > dashboard link: https://syzkaller.appspot.com/bug?extid=3e563d99e70973c0755c
> > > compiler: gcc (Debian 12.2.0-14) 12.2.0, GNU ld (GNU Binutils for Debian) 2.40
> > > syz repro: https://syzkaller.appspot.com/x/repro.syz?x=1297cc07980000
> > > C reproducer: https://syzkaller.appspot.com/x/repro.c?x=1217c8a9980000
> >
> > I'm not sure what the correct patch would be though, as I don't
> > understand what the issue is. It seems that dev_free() indeed gets
> > called twice, but since it's guarded by kref_put(), this shouldn't
> > happen AFAIU. Or at least we should get a bad refcount report.
Interestingly, crashes stopped happening 20 days ago. It could be that
there was some kind of bug in the refcount or the generic USB code,
and that got fixed (at least I don't see a problem in the Raw Gadget
code). Let's keep this bug open for now and monitor, and late I'll
close it if there are no more crashes.
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [syzbot] [PATCH] usb: raw_gadget: Fix a KMSAN double free bug in raw_release
2024-08-16 20:25 [syzbot] [usb?] KASAN: invalid-free in dev_free syzbot
2024-09-16 1:24 ` syzbot
@ 2024-10-30 6:39 ` syzbot
2024-11-02 22:00 ` [syzbot] [PATCH] usb: raw_gadget: Fix a KASAN double-free " syzbot
2 siblings, 0 replies; 7+ messages in thread
From: syzbot @ 2024-10-30 6:39 UTC (permalink / raw)
To: linux-kernel
For archival purposes, forwarding an incoming command email to
linux-kernel@vger.kernel.org.
***
Subject: [PATCH] usb: raw_gadget: Fix a KMSAN double free bug in raw_release
Author: marcus.yu.56@gmail.com
#syz test
syzkaller reported a double free bug
(https://syzkaller.appspot.com/bug?extid=3e563d99e70973c0755c) in
raw_release. Specifically, if there are concurrent calls to raw_release
and dev->gadget_registered is false, there will be racing kref_put
calls causing dev_free to be invoked twice.
The fix is to check ref count and put under lock so that kref_put is
called at most once.
The "unregister" path is safe because checking dev->gadget_registered
and then flipping it to false under lock guarantees that this branch is
taken at most once.
Signed-off-by: Chang Yu <marcus.yu.56@gmail.com>
Reported-by: syzbot+3e563d99e70973c0755c@syzkaller.appspotmail.com
Closes: https://syzkaller.appspot.com/bug?extid=3e563d99e70973c0755c
Fixes: f2c2e717642c ("usb: gadget: add raw-gadget interface")
---
drivers/usb/gadget/legacy/raw_gadget.c | 27 ++++++++++++++------------
1 file changed, 15 insertions(+), 12 deletions(-)
diff --git a/drivers/usb/gadget/legacy/raw_gadget.c b/drivers/usb/gadget/legacy/raw_gadget.c
index 112fd18d8c99..3da022aae830 100644
--- a/drivers/usb/gadget/legacy/raw_gadget.c
+++ b/drivers/usb/gadget/legacy/raw_gadget.c
@@ -445,7 +445,6 @@ static int raw_release(struct inode *inode, struct file *fd)
int ret = 0;
struct raw_dev *dev = fd->private_data;
unsigned long flags;
- bool unregister = false;
spin_lock_irqsave(&dev->lock, flags);
dev->state = STATE_DEV_CLOSED;
@@ -453,20 +452,24 @@ static int raw_release(struct inode *inode, struct file *fd)
spin_unlock_irqrestore(&dev->lock, flags);
goto out_put;
}
- if (dev->gadget_registered)
- unregister = true;
+ if (!dev->gadget_registered) {
+ if (kref_read(&dev->count)) {
+ /* Matches dev_new() in raw_open(). */
+ kref_put(&dev->count, dev_free);
+ }
+ spin_unlock_irqrestore(&dev->lock, flags);
+ return ret;
+ }
dev->gadget_registered = false;
spin_unlock_irqrestore(&dev->lock, flags);
- if (unregister) {
- ret = usb_gadget_unregister_driver(&dev->driver);
- if (ret != 0)
- dev_err(dev->dev,
- "usb_gadget_unregister_driver() failed with %d\n",
- ret);
- /* Matches kref_get() in raw_ioctl_run(). */
- kref_put(&dev->count, dev_free);
- }
+ ret = usb_gadget_unregister_driver(&dev->driver);
+ if (ret != 0)
+ dev_err(dev->dev,
+ "usb_gadget_unregister_driver() failed with %d\n",
+ ret);
+ /* Matches kref_get() in raw_ioctl_run(). */
+ kref_put(&dev->count, dev_free);
out_put:
/* Matches dev_new() in raw_open(). */
--
2.47.0
^ permalink raw reply [flat|nested] 7+ messages in thread* Re: [syzbot] [PATCH] usb: raw_gadget: Fix a KASAN double-free bug in raw_release
2024-08-16 20:25 [syzbot] [usb?] KASAN: invalid-free in dev_free syzbot
2024-09-16 1:24 ` syzbot
2024-10-30 6:39 ` [syzbot] [PATCH] usb: raw_gadget: Fix a KMSAN double free bug in raw_release syzbot
@ 2024-11-02 22:00 ` syzbot
2 siblings, 0 replies; 7+ messages in thread
From: syzbot @ 2024-11-02 22:00 UTC (permalink / raw)
To: linux-kernel
For archival purposes, forwarding an incoming command email to
linux-kernel@vger.kernel.org.
***
Subject: [PATCH] usb: raw_gadget: Fix a KASAN double-free bug in raw_release
Author: marcus.yu.56@gmail.com
#syz test
syzkaller reported a double free bug
(https://syzkaller.appspot.com/bug?extid=3e563d99e70973c0755c) in
raw_release.
I suspect this is because a race between raw_release and
raw_ioctl_run. While kref_get in raw_ioctl_run is protected by
the spin lock, all kref_put in raw_release are not under the
lock. This makes it possible that a kref_put might occur during
kref_get, which is specifically prohibited by the kref
documentation[1].
The fix is to ensure that all kref_put calls are made under lock
and that we only call kfree(dev) after releasing the lock.
[1] https://docs.kernel.org/core-api/kref.html
Signed-off-by: Chang Yu <marcus.yu.56@gmail.com>
Reported-by: syzbot+3e563d99e70973c0755c@syzkaller.appspotmail.com
Closes: https://syzkaller.appspot.com/bug?extid=3e563d99e70973c0755c
Fixes: f2c2e717642c ("usb: gadget: add raw-gadget interface")
---
drivers/usb/gadget/legacy/raw_gadget.c | 44 ++++++++++++++------------
1 file changed, 24 insertions(+), 20 deletions(-)
diff --git a/drivers/usb/gadget/legacy/raw_gadget.c b/drivers/usb/gadget/legacy/raw_gadget.c
index 112fd18d8c99..0c01d491d489 100644
--- a/drivers/usb/gadget/legacy/raw_gadget.c
+++ b/drivers/usb/gadget/legacy/raw_gadget.c
@@ -225,7 +225,6 @@ static void dev_free(struct kref *kref)
kfree(dev->eps[i].ep->desc);
dev->eps[i].state = STATE_EP_DISABLED;
}
- kfree(dev);
}
/*----------------------------------------------------------------------*/
@@ -330,7 +329,8 @@ static void gadget_unbind(struct usb_gadget *gadget)
set_gadget_data(gadget, NULL);
/* Matches kref_get() in gadget_bind(). */
- kref_put(&dev->count, dev_free);
+ if (kref_put(&dev->count, dev_free))
+ kfree(dev);
}
static int gadget_setup(struct usb_gadget *gadget,
@@ -443,34 +443,38 @@ static int raw_open(struct inode *inode, struct file *fd)
static int raw_release(struct inode *inode, struct file *fd)
{
int ret = 0;
+ int freed = 0;
struct raw_dev *dev = fd->private_data;
unsigned long flags;
- bool unregister = false;
spin_lock_irqsave(&dev->lock, flags);
dev->state = STATE_DEV_CLOSED;
- if (!dev->gadget) {
- spin_unlock_irqrestore(&dev->lock, flags);
- goto out_put;
- }
- if (dev->gadget_registered)
- unregister = true;
+ if (!dev->gadget)
+ goto out_put_locked;
+ if (!dev->gadget_registered)
+ goto out_put_locked;
dev->gadget_registered = false;
spin_unlock_irqrestore(&dev->lock, flags);
- if (unregister) {
- ret = usb_gadget_unregister_driver(&dev->driver);
- if (ret != 0)
- dev_err(dev->dev,
- "usb_gadget_unregister_driver() failed with %d\n",
- ret);
- /* Matches kref_get() in raw_ioctl_run(). */
- kref_put(&dev->count, dev_free);
- }
+ ret = usb_gadget_unregister_driver(&dev->driver);
+ if (ret != 0)
+ dev_err(dev->dev,
+ "usb_gadget_unregister_driver() failed with %d\n",
+ ret);
+
+ spin_lock_irqsave(&dev->lock, flags);
+ /* Matches kref_get() in raw_ioctl_run(). */
+ freed = kref_put(&dev->count, dev_free);
+ if (freed)
+ goto out_free_dev;
-out_put:
+out_put_locked:
/* Matches dev_new() in raw_open(). */
- kref_put(&dev->count, dev_free);
+ freed = kref_put(&dev->count, dev_free);
+out_free_dev:
+ spin_unlock_irqrestore(&dev->lock, flags);
+ if (freed)
+ kfree(dev);
return ret;
}
--
2.47.0
^ permalink raw reply [flat|nested] 7+ messages in thread