Skip to content

Commit db53f99

Browse files
spandruvadaSasha Levin
authored and
Sasha Levin
committed
platform/x86: ISST: Fix possible circular locking dependency detected
[ Upstream commit 17da2d5 ] As reported: [ 256.104522] ====================================================== [ 256.113783] WARNING: possible circular locking dependency detected [ 256.120093] 5.16.0-rc6-yocto-standard+ torvalds#99 Not tainted [ 256.125362] ------------------------------------------------------ [ 256.131673] intel-speed-sel/844 is trying to acquire lock: [ 256.137290] ffffffffc036f0d0 (punit_misc_dev_lock){+.+.}-{3:3}, at: isst_if_open+0x18/0x90 [isst_if_common] [ 256.147171] [ 256.147171] but task is already holding lock: [ 256.153135] ffffffff8ee7cb50 (misc_mtx){+.+.}-{3:3}, at: misc_open+0x2a/0x170 [ 256.160407] [ 256.160407] which lock already depends on the new lock. [ 256.160407] [ 256.168712] [ 256.168712] the existing dependency chain (in reverse order) is: [ 256.176327] [ 256.176327] -> #1 (misc_mtx){+.+.}-{3:3}: [ 256.181946] lock_acquire+0x1e6/0x330 [ 256.186265] __mutex_lock+0x9b/0x9b0 [ 256.190497] mutex_lock_nested+0x1b/0x20 [ 256.195075] misc_register+0x32/0x1a0 [ 256.199390] isst_if_cdev_register+0x65/0x180 [isst_if_common] [ 256.205878] isst_if_probe+0x144/0x16e [isst_if_mmio] ... [ 256.241976] [ 256.241976] -> #0 (punit_misc_dev_lock){+.+.}-{3:3}: [ 256.248552] validate_chain+0xbc6/0x1750 [ 256.253131] __lock_acquire+0x88c/0xc10 [ 256.257618] lock_acquire+0x1e6/0x330 [ 256.261933] __mutex_lock+0x9b/0x9b0 [ 256.266165] mutex_lock_nested+0x1b/0x20 [ 256.270739] isst_if_open+0x18/0x90 [isst_if_common] [ 256.276356] misc_open+0x100/0x170 [ 256.280409] chrdev_open+0xa5/0x1e0 ... The call sequence suggested that misc_device /dev file can be opened before misc device is yet to be registered, which is done only once. Here punit_misc_dev_lock was used as common lock, to protect the registration by multiple ISST HW drivers, one time setup, prevent duplicate registry of misc device and prevent load/unload when device is open. We can split into locks: - One which just prevent duplicate call to misc_register() and one time setup. Also never call again if the misc_register() failed or required one time setup is failed. This lock is not shared with any misc device callbacks. - The other lock protects registry, load and unload of HW drivers. Sequence in isst_if_cdev_register() - Register callbacks under punit_misc_dev_open_lock - Call isst_misc_reg() which registers misc_device on the first registry which is under punit_misc_dev_reg_lock, which is not shared with callbacks. Sequence in isst_if_cdev_unregister Just opposite of isst_if_cdev_register Reported-and-tested-by: Liwei Song <[email protected]> Signed-off-by: Srinivas Pandruvada <[email protected]> Link: https://lore.kernel.org/r/[email protected] Reviewed-by: Hans de Goede <[email protected]> Signed-off-by: Hans de Goede <[email protected]> Signed-off-by: Sasha Levin <[email protected]>
1 parent c1b3d47 commit db53f99

File tree

1 file changed

+63
-34
lines changed

1 file changed

+63
-34
lines changed

drivers/platform/x86/intel_speed_select_if/isst_if_common.c

Lines changed: 63 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -529,7 +529,10 @@ static long isst_if_def_ioctl(struct file *file, unsigned int cmd,
529529
return ret;
530530
}
531531

532-
static DEFINE_MUTEX(punit_misc_dev_lock);
532+
/* Lock to prevent module registration when already opened by user space */
533+
static DEFINE_MUTEX(punit_misc_dev_open_lock);
534+
/* Lock to allow one share misc device for all ISST interace */
535+
static DEFINE_MUTEX(punit_misc_dev_reg_lock);
533536
static int misc_usage_count;
534537
static int misc_device_ret;
535538
static int misc_device_open;
@@ -539,7 +542,7 @@ static int isst_if_open(struct inode *inode, struct file *file)
539542
int i, ret = 0;
540543

541544
/* Fail open, if a module is going away */
542-
mutex_lock(&punit_misc_dev_lock);
545+
mutex_lock(&punit_misc_dev_open_lock);
543546
for (i = 0; i < ISST_IF_DEV_MAX; ++i) {
544547
struct isst_if_cmd_cb *cb = &punit_callbacks[i];
545548

@@ -561,7 +564,7 @@ static int isst_if_open(struct inode *inode, struct file *file)
561564
} else {
562565
misc_device_open++;
563566
}
564-
mutex_unlock(&punit_misc_dev_lock);
567+
mutex_unlock(&punit_misc_dev_open_lock);
565568

566569
return ret;
567570
}
@@ -570,15 +573,15 @@ static int isst_if_relase(struct inode *inode, struct file *f)
570573
{
571574
int i;
572575

573-
mutex_lock(&punit_misc_dev_lock);
576+
mutex_lock(&punit_misc_dev_open_lock);
574577
misc_device_open--;
575578
for (i = 0; i < ISST_IF_DEV_MAX; ++i) {
576579
struct isst_if_cmd_cb *cb = &punit_callbacks[i];
577580

578581
if (cb->registered)
579582
module_put(cb->owner);
580583
}
581-
mutex_unlock(&punit_misc_dev_lock);
584+
mutex_unlock(&punit_misc_dev_open_lock);
582585

583586
return 0;
584587
}
@@ -595,6 +598,43 @@ static struct miscdevice isst_if_char_driver = {
595598
.fops = &isst_if_char_driver_ops,
596599
};
597600

601+
static int isst_misc_reg(void)
602+
{
603+
mutex_lock(&punit_misc_dev_reg_lock);
604+
if (misc_device_ret)
605+
goto unlock_exit;
606+
607+
if (!misc_usage_count) {
608+
misc_device_ret = isst_if_cpu_info_init();
609+
if (misc_device_ret)
610+
goto unlock_exit;
611+
612+
misc_device_ret = misc_register(&isst_if_char_driver);
613+
if (misc_device_ret) {
614+
isst_if_cpu_info_exit();
615+
goto unlock_exit;
616+
}
617+
}
618+
misc_usage_count++;
619+
620+
unlock_exit:
621+
mutex_unlock(&punit_misc_dev_reg_lock);
622+
623+
return misc_device_ret;
624+
}
625+
626+
static void isst_misc_unreg(void)
627+
{
628+
mutex_lock(&punit_misc_dev_reg_lock);
629+
if (misc_usage_count)
630+
misc_usage_count--;
631+
if (!misc_usage_count && !misc_device_ret) {
632+
misc_deregister(&isst_if_char_driver);
633+
isst_if_cpu_info_exit();
634+
}
635+
mutex_unlock(&punit_misc_dev_reg_lock);
636+
}
637+
598638
/**
599639
* isst_if_cdev_register() - Register callback for IOCTL
600640
* @device_type: The device type this callback handling.
@@ -612,38 +652,31 @@ static struct miscdevice isst_if_char_driver = {
612652
*/
613653
int isst_if_cdev_register(int device_type, struct isst_if_cmd_cb *cb)
614654
{
615-
if (misc_device_ret)
616-
return misc_device_ret;
655+
int ret;
617656

618657
if (device_type >= ISST_IF_DEV_MAX)
619658
return -EINVAL;
620659

621-
mutex_lock(&punit_misc_dev_lock);
660+
mutex_lock(&punit_misc_dev_open_lock);
661+
/* Device is already open, we don't want to add new callbacks */
622662
if (misc_device_open) {
623-
mutex_unlock(&punit_misc_dev_lock);
663+
mutex_unlock(&punit_misc_dev_open_lock);
624664
return -EAGAIN;
625665
}
626-
if (!misc_usage_count) {
627-
int ret;
628-
629-
misc_device_ret = misc_register(&isst_if_char_driver);
630-
if (misc_device_ret)
631-
goto unlock_exit;
632-
633-
ret = isst_if_cpu_info_init();
634-
if (ret) {
635-
misc_deregister(&isst_if_char_driver);
636-
misc_device_ret = ret;
637-
goto unlock_exit;
638-
}
639-
}
640666
memcpy(&punit_callbacks[device_type], cb, sizeof(*cb));
641667
punit_callbacks[device_type].registered = 1;
642-
misc_usage_count++;
643-
unlock_exit:
644-
mutex_unlock(&punit_misc_dev_lock);
668+
mutex_unlock(&punit_misc_dev_open_lock);
645669

646-
return misc_device_ret;
670+
ret = isst_misc_reg();
671+
if (ret) {
672+
/*
673+
* No need of mutex as the misc device register failed
674+
* as no one can open device yet. Hence no contention.
675+
*/
676+
punit_callbacks[device_type].registered = 0;
677+
return ret;
678+
}
679+
return 0;
647680
}
648681
EXPORT_SYMBOL_GPL(isst_if_cdev_register);
649682

@@ -658,16 +691,12 @@ EXPORT_SYMBOL_GPL(isst_if_cdev_register);
658691
*/
659692
void isst_if_cdev_unregister(int device_type)
660693
{
661-
mutex_lock(&punit_misc_dev_lock);
662-
misc_usage_count--;
694+
isst_misc_unreg();
695+
mutex_lock(&punit_misc_dev_open_lock);
663696
punit_callbacks[device_type].registered = 0;
664697
if (device_type == ISST_IF_DEV_MBOX)
665698
isst_delete_hash();
666-
if (!misc_usage_count && !misc_device_ret) {
667-
misc_deregister(&isst_if_char_driver);
668-
isst_if_cpu_info_exit();
669-
}
670-
mutex_unlock(&punit_misc_dev_lock);
699+
mutex_unlock(&punit_misc_dev_open_lock);
671700
}
672701
EXPORT_SYMBOL_GPL(isst_if_cdev_unregister);
673702

0 commit comments

Comments
 (0)