lustre: mdc: fix possible deadlock in chlg_open()
PATCH 1/1 lustre: mdc: fix possible deadlock in chlg_open()
Lockdep reports a possible deadlock between chlg_open() and mdc_changelog_cdev_init()
mdc_changelog_cdev_init() takes chlg_registered_dev_lock and then calls misc_register() which takes misc_mtx.chlg_open() is called while misc_mtx is held, and tries to take chlg_registered_dev_lock.
If these two functions race, a deadlock can occur as each thread will hold one of the locks while trying to take the other.
chlg_open() does not need to take a lock. It only uses the lock to stablize a list while looking for the matching chlg_registered_dev, and this can be found directly by examining file->private_data.
So remove chlg_obd_get(), and use file->private_Data to find the obd_device.
---
Note that this bug exists in upstream (OpenSFS) lustre as well.
drivers/staging/lustre/lustre/mdc/mdc_changelog.c | 35 +++++------------------ 1 file changed, 7 insertions(+), 28 deletions(-)
drivers/staging/lustre/lustre/mdc/mdc_changelog.c +7 -28
@@ -444,31 +444,6 @@ static ssize_t chlg_write(struct file *file, const char __user *buff,return rc <0 ? rc : count; } -/** -* Find the OBD device associated to a changelog character device. -* @param[in] cdev character device instance descriptor -* @return corresponding OBD device or NULL if none was found. -*/ -static struct obd_device *chlg_obd_get (dev_t cdev) -{ -int minor =MINOR (cdev); -struct obd_device *obd =NULL ; -struct chlg_registered_dev *curr; - -mutex_lock (&chlg_registered_dev_lock); -list_for_each_entry (curr, &chlg_registered_devices, ced_link) { -if (curr->ced_misc.minor == minor) { -/* take the first available OBD device attached */ - obd =list_first_entry (&curr->ced_obds, - struct obd_device, - u.cli.cl_chg_dev_linkage); -break ; - } - } -mutex_unlock (&chlg_registered_dev_lock); -return obd; -} -/** * Open handler, initialize internal CRS state and spawn prefetch thread if * needed.
@@ -479,12 +454,16 @@ static struct obd_device *chlg_obd_get(dev_t cdev)static int chlg_open (struct inode *inode,struct file *file) {struct chlg_reader_state *crs; -struct obd_device *obd =chlg_obd_get (inode->i_rdev); +struct miscdevice *misc = file->private_data; +struct chlg_registered_dev *dev; +struct obd_device *obd;struct task_struct *task;int rc; -if (!obd) -return -ENODEV ; + dev = container_of(misc, struct chlg_registered_dev, ced_misc); + obd =list_first_entry (&dev->ced_obds, + struct obd_device, + u.cli.cl_chg_dev_linkage); crs =kzalloc (sizeof (*crs),GFP_KERNEL );if (!crs)
Signature · 9 lines
2.14.0.rc0.dirty -------------- next part -------------- A non-text attachment was scrubbed... Name: signature.asc Type: application/pgp-signature Size: 832 bytes Desc: not available URL: <http://lists.lustre.org/pipermail/lustre-devel-lustre.org/attachments/20181031/c037145f/attachment.sig>
PATCH 1/1 lustre: mdc: fix possible deadlock in chlg_open()
Le 31/10/2018 ? 02:29, NeilBrown a ?crit?:
Quoted 15 lines
Lockdep reports a possible deadlock between
chlg_open()andmdc_changelog_cdev_init()
mdc_changelog_cdev_init()takeschlg_registered_dev_lockand then callsmisc_register()which takesmisc_mtx.chlg_open()is called whilemisc_mtxis held, and tries to takechlg_registered_dev_lock.
If these two functions race, a deadlock can occur as each thread will hold one of the locks while trying to take the other.
chlg_open()does not need to take a lock. It only uses the lock to stablize a list while looking for the matchingchlg_registered_dev, and this can be found directly by examiningfile->private_data.
Quoted 7 lines
So remove
chlg_obd_get(), and usefile->private_Datato find theobd_device.Signed-off-by NeilBrown <[email protected]>---
Note that this bug exists in upstream (OpenSFS) lustre as well.
I think you need to modify mdc_changelog_cdev_init() as well so that misc_register() comes after the two list_add_tail() statements.
If someone opens the device a bit a too fast after it becomes visible, they might find an empty list at `dev->ced_obds'.
Previously, holding `chlg_registered_dev_loc' implicitely prevented that.
Is it possible that maybe the kernel shouldn't hold `misc_mtx' while calling chlg_open()?
Quentin Bouget
Quoted 62 lines
drivers/staging/lustre/lustre/mdc/mdc_changelog.c | 35 +++++------------------ 1 file changed, 7 insertions(+), 28 deletions(-)drivers/staging/lustre/lustre/mdc/mdc_changelog.c +7
-28@@ -444,31 +444,6 @@ static ssize_t chlg_write(struct file *file, const char __user *buff,return rc <0 ? rc : count; } -/** -* Find the OBD device associated to a changelog character device. -* @param[in] cdev character device instance descriptor -* @return corresponding OBD device or NULL if none was found. -*/ -static struct obd_device *chlg_obd_get (dev_t cdev) -{ -int minor =MINOR (cdev); -struct obd_device *obd =NULL ; -struct chlg_registered_dev *curr; - -mutex_lock (&chlg_registered_dev_lock); -list_for_each_entry (curr, &chlg_registered_devices, ced_link) { -if (curr->ced_misc.minor == minor) { -/* take the first available OBD device attached */ - obd =list_first_entry (&curr->ced_obds, - struct obd_device, - u.cli.cl_chg_dev_linkage); -break ; - } - } -mutex_unlock (&chlg_registered_dev_lock); -return obd; -} -/** * Open handler, initialize internal CRS state and spawn prefetch thread if * needed. @@ -479,12 +454,16 @@ static struct obd_device *chlg_obd_get(dev_t cdev)static int chlg_open (struct inode *inode,struct file *file) {struct chlg_reader_state *crs; -struct obd_device *obd =chlg_obd_get (inode->i_rdev); +struct miscdevice *misc = file->private_data; +struct chlg_registered_dev *dev; +struct obd_device *obd;struct task_struct *task;int rc; -if (!obd) -return -ENODEV ; + dev = container_of(misc, struct chlg_registered_dev, ced_misc); + obd =list_first_entry (&dev->ced_obds, + struct obd_device, + u.cli.cl_chg_dev_linkage); crs =kzalloc (sizeof (*crs),GFP_KERNEL );if (!crs)
Quoted 4 lines
_______________________________________________
lustre-devel mailing list
lustre-devel at lists.lustre.org
http://lists.lustre.org/listinfo.cgi/lustre-devel-lustre.org
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.lustre.org/pipermail/lustre-devel-lustre.org/attachments/20181031/fb1187bf/attachment.html>
PATCH 1/1 lustre: mdc: fix possible deadlock in chlg_open()
Quoted 19 lines · quentin.bouget at cea.fr
Le 31/10/2018 ? 02:29, NeilBrown a ?crit?:
Quoted 18 lines
Lockdep reports a possible deadlock between
chlg_open()andmdc_changelog_cdev_init()
mdc_changelog_cdev_init()takeschlg_registered_dev_lockand then callsmisc_register()which takesmisc_mtx.chlg_open()is called whilemisc_mtxis held, and tries to takechlg_registered_dev_lock.
If these two functions race, a deadlock can occur as each thread will hold one of the locks while trying to take the other.
chlg_open()does not need to take a lock. It only uses the lock to stablize a list while looking for the matchingchlg_registered_dev, and this can be found directly by examiningfile->private_data.So remove
chlg_obd_get(), and usefile->private_Datato find theobd_device.
Quoted 6 lines
Quoted 4 lines
Signed-off-by NeilBrown <[email protected]>---
Note that this bug exists in upstream (OpenSFS) lustre as well.
I think you need to modify
mdc_changelog_cdev_init()as well so thatmisc_register()comes after the twolist_add_tail()statements.
Yes, definitely. Thanks for that. I'll change the patch.
Quoted 7 lines
If someone opens the device a bit a too fast after it becomes visible, they might find an empty list at `dev->ced_obds'.
Previously, holding `chlg_registered_dev_loc' implicitely prevented that.Is it possible that maybe the kernel shouldn't hold `misc_mtx' while calling
chlg_open()?
No, it really should. It protects against races between chlg_open and misc_unregister().
Thanks,
NeilBrown
Quoted 60 lines
Quentin Bouget
Quoted 58 lines
drivers/staging/lustre/lustre/mdc/mdc_changelog.c | 35 +++++------------------ 1 file changed, 7 insertions(+), 28 deletions(-)drivers/staging/lustre/lustre/mdc/mdc_changelog.c +7
-28@@ -444,31 +444,6 @@ static ssize_t chlg_write(struct file *file, const char __user *buff,return rc <0 ? rc : count; } -/** -* Find the OBD device associated to a changelog character device. -* @param[in] cdev character device instance descriptor -* @return corresponding OBD device or NULL if none was found. -*/ -static struct obd_device *chlg_obd_get (dev_t cdev) -{ -int minor =MINOR (cdev); -struct obd_device *obd =NULL ; -struct chlg_registered_dev *curr; - -mutex_lock (&chlg_registered_dev_lock); -list_for_each_entry (curr, &chlg_registered_devices, ced_link) { -if (curr->ced_misc.minor == minor) { -/* take the first available OBD device attached */ - obd =list_first_entry (&curr->ced_obds, - struct obd_device, - u.cli.cl_chg_dev_linkage); -break ; - } - } -mutex_unlock (&chlg_registered_dev_lock); -return obd; -} -/** * Open handler, initialize internal CRS state and spawn prefetch thread if * needed. @@ -479,12 +454,16 @@ static struct obd_device *chlg_obd_get(dev_t cdev)static int chlg_open (struct inode *inode,struct file *file) {struct chlg_reader_state *crs; -struct obd_device *obd =chlg_obd_get (inode->i_rdev); +struct miscdevice *misc = file->private_data; +struct chlg_registered_dev *dev; +struct obd_device *obd;struct task_struct *task;int rc; -if (!obd) -return -ENODEV ; + dev = container_of(misc, struct chlg_registered_dev, ced_misc); + obd =list_first_entry (&dev->ced_obds, + struct obd_device, + u.cli.cl_chg_dev_linkage);
Quoted 8 lines
Quoted 8 lines
drivers/staging/lustre/lustre/mdc/mdc_changelog.c +0
-0crs =kzalloc (sizeof (*crs),GFP_KERNEL );if (!crs)_______________________________________________
lustre-devel mailing list
lustre-devel at lists.lustre.org
http://lists.lustre.org/listinfo.cgi/lustre-devel-lustre.org
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 832 bytes
Desc: not available
URL: <http://lists.lustre.org/pipermail/lustre-devel-lustre.org/attachments/20181101/ad861667/attachment.sig>
PATCH v2 1/1 lustre: mdc: fix possible deadlock in chlg_open()
In reply to quentin.bouget at cea.frLockdep reports a possible deadlock between chlg_open() and mdc_changelog_cdev_init()
mdc_changelog_cdev_init() takes chlg_registered_dev_lock and then calls misc_register() which takes misc_mtx.chlg_open() is called while misc_mtx is held, and tries to take chlg_registered_dev_lock.
If these two functions race, a deadlock can occur as each thread will hold one of the locks while trying to take the other.
chlg_open() does not need to take a lock. It only uses the lock to stablize a list while looking for the matching chlg_registered_dev, and this can be found directly by examining file->private_data.
So remove chlg_obd_get(), and use file->private_data to find the obd_device.
Also ensure the device is fully initialized before calling misc_register(). This means setting up some list linkage before the call, and tearing it down if there is an error.
---
This is the revised version with the problem identified by Quentin fixed.
drivers/staging/lustre/lustre/mdc/mdc_changelog.c | 46 +++++++---------------- 1 file changed, 14 insertions(+), 32 deletions(-)
drivers/staging/lustre/lustre/mdc/mdc_changelog.c +14 -32
@@ -444,31 +444,6 @@ static ssize_t chlg_write(struct file *file, const char __user *buff,return rc <0 ? rc : count; } -/** -* Find the OBD device associated to a changelog character device. -* @param[in] cdev character device instance descriptor -* @return corresponding OBD device or NULL if none was found. -*/ -static struct obd_device *chlg_obd_get (dev_t cdev) -{ -int minor =MINOR (cdev); -struct obd_device *obd =NULL ; -struct chlg_registered_dev *curr; - -mutex_lock (&chlg_registered_dev_lock); -list_for_each_entry (curr, &chlg_registered_devices, ced_link) { -if (curr->ced_misc.minor == minor) { -/* take the first available OBD device attached */ - obd =list_first_entry (&curr->ced_obds, - struct obd_device, - u.cli.cl_chg_dev_linkage); -break ; - } - } -mutex_unlock (&chlg_registered_dev_lock); -return obd; -} -/** * Open handler, initialize internal CRS state and spawn prefetch thread if * needed.
@@ -479,12 +454,16 @@ static struct obd_device *chlg_obd_get(dev_t cdev)static int chlg_open (struct inode *inode,struct file *file) {struct chlg_reader_state *crs; -struct obd_device *obd =chlg_obd_get (inode->i_rdev); +struct miscdevice *misc = file->private_data; +struct chlg_registered_dev *dev; +struct obd_device *obd;struct task_struct *task;int rc; -if (!obd) -return -ENODEV ; + dev = container_of(misc, struct chlg_registered_dev, ced_misc); + obd =list_first_entry (&dev->ced_obds, + struct obd_device, + u.cli.cl_chg_dev_linkage); crs =kzalloc (sizeof (*crs),GFP_KERNEL );if (!crs)
@@ -669,13 +648,16 @@ int mdc_changelog_cdev_init(struct obd_device *obd) goto out_unlock; } +list_add_tail (&obd->u.cli.cl_chg_dev_linkage, &entry->ced_obds); +list_add_tail (&entry->ced_link, &chlg_registered_devices); +/* Register new character device */ rc =misc_register (&entry->ced_misc); -if (rc !=0 ) +if (rc !=0 ) { +list_del_init (&obd->u.cli.cl_chg_dev_linkage); +list_del (&entry->ced_link); goto out_unlock; - -list_add_tail (&obd->u.cli.cl_chg_dev_linkage, &entry->ced_obds); -list_add_tail (&entry->ced_link, &chlg_registered_devices); + } entry =NULL ;/* prevent it from being freed below */
Signature · 9 lines
2.14.0.rc0.dirty -------------- next part -------------- A non-text attachment was scrubbed... Name: signature.asc Type: application/pgp-signature Size: 832 bytes Desc: not available URL: <http://lists.lustre.org/pipermail/lustre-devel-lustre.org/attachments/20181101/1d8587cb/attachment.sig>
PATCH v2 1/1 lustre: mdc: fix possible deadlock in chlg_open()
Quoted 16 lines
Lockdep reports a possible deadlock between
chlg_open()andmdc_changelog_cdev_init()
mdc_changelog_cdev_init()takeschlg_registered_dev_lockand then callsmisc_register()which takesmisc_mtx.chlg_open()is called whilemisc_mtxis held, and tries to takechlg_registered_dev_lock.
If these two functions race, a deadlock can occur as each thread will hold one of the locks while trying to take the other.
chlg_open()does not need to take a lock. It only uses the lock to stablize a list while looking for the matchingchlg_registered_dev, and this can be found directly by examiningfile->private_data.
Quoted 5 lines
So remove
chlg_obd_get(), and usefile->private_datato find theobd_device.
Also ensure the device is fully initialized before callingmisc_register(). This means setting up some list linkage before the call, and tearing it down if there is an error.
I have been testing this but I'm no HSM expert. I pushed this patch to OpenSFS branch as well.
https://jira.whamcloud.com/browse/LU-11617
https://review.whamcloud.com/#/c/33572/
Quoted 90 lines
Signed-off-by NeilBrown <[email protected]>---
This is the revised version with the problem identified by Quentin fixed.
drivers/staging/lustre/lustre/mdc/mdc_changelog.c | 46 +++++++---------------- 1 file changed, 14 insertions(+), 32 deletions(-)drivers/staging/lustre/lustre/mdc/mdc_changelog.c +14
-32@@ -444,31 +444,6 @@ static ssize_t chlg_write(struct file *file, const char __user *buff,return rc <0 ? rc : count; } -/** -* Find the OBD device associated to a changelog character device. -* @param[in] cdev character device instance descriptor -* @return corresponding OBD device or NULL if none was found. -*/ -static struct obd_device *chlg_obd_get (dev_t cdev) -{ -int minor =MINOR (cdev); -struct obd_device *obd =NULL ; -struct chlg_registered_dev *curr; - -mutex_lock (&chlg_registered_dev_lock); -list_for_each_entry (curr, &chlg_registered_devices, ced_link) { -if (curr->ced_misc.minor == minor) { -/* take the first available OBD device attached */ - obd =list_first_entry (&curr->ced_obds, - struct obd_device, - u.cli.cl_chg_dev_linkage); -break ; - } - } -mutex_unlock (&chlg_registered_dev_lock); -return obd; -} -/** * Open handler, initialize internal CRS state and spawn prefetch thread if * needed. @@ -479,12 +454,16 @@ static struct obd_device *chlg_obd_get(dev_t cdev)static int chlg_open (struct inode *inode,struct file *file) {struct chlg_reader_state *crs; -struct obd_device *obd =chlg_obd_get (inode->i_rdev); +struct miscdevice *misc = file->private_data; +struct chlg_registered_dev *dev; +struct obd_device *obd;struct task_struct *task;int rc; -if (!obd) -return -ENODEV ; + dev = container_of(misc, struct chlg_registered_dev, ced_misc); + obd =list_first_entry (&dev->ced_obds, + struct obd_device, + u.cli.cl_chg_dev_linkage); crs =kzalloc (sizeof (*crs),GFP_KERNEL );if (!crs)@@ -669,13 +648,16 @@ int mdc_changelog_cdev_init(struct obd_device *obd) goto out_unlock; } +list_add_tail (&obd->u.cli.cl_chg_dev_linkage, &entry->ced_obds); +list_add_tail (&entry->ced_link, &chlg_registered_devices); +/* Register new character device */ rc =misc_register (&entry->ced_misc); -if (rc !=0 ) +if (rc !=0 ) { +list_del_init (&obd->u.cli.cl_chg_dev_linkage); +list_del (&entry->ced_link); goto out_unlock; - -list_add_tail (&obd->u.cli.cl_chg_dev_linkage, &entry->ced_obds); -list_add_tail (&entry->ced_link, &chlg_registered_devices); + } entry =NULL ;/* prevent it from being freed below */ Signature · 1 line
2.14.0.rc0.dirty
PATCH v2 1/1 lustre: mdc: fix possible deadlock in chlg_open()
Le 04/11/2018 ? 22:34, James Simmons a ?crit?:
Quoted 20 lines
Quoted 15 lines
Lockdep reports a possible deadlock between
chlg_open()andmdc_changelog_cdev_init()
mdc_changelog_cdev_init()takeschlg_registered_dev_lockand then callsmisc_register()which takesmisc_mtx.chlg_open()is called whilemisc_mtxis held, and tries to takechlg_registered_dev_lock.
If these two functions race, a deadlock can occur as each thread will hold one of the locks while trying to take the other.
chlg_open()does not need to take a lock. It only uses the lock to stablize a list while looking for the matchingchlg_registered_dev, and this can be found directly by examiningfile->private_data.Quoted 5 lines
So remove
chlg_obd_get(), and usefile->private_datato find theobd_device.
Also ensure the device is fully initialized before callingmisc_register(). This means setting up some list linkage before the call, and tearing it down if there is an error.
Quoted 7 lines
I have been testing this but I'm no HSM expert. I pushed this patch to OpenSFS branch as well.
https://jira.whamcloud.com/browse/LU-11617
https://review.whamcloud.com/#/c/33572/Reviewed-by James Simmons <[email protected]>
Quoted 91 lines
Quoted 91 lines
Signed-off-by NeilBrown <[email protected]>---
This is the revised version with the problem identified by Quentin fixed.
drivers/staging/lustre/lustre/mdc/mdc_changelog.c | 46 +++++++---------------- 1 file changed, 14 insertions(+), 32 deletions(-)drivers/staging/lustre/lustre/mdc/mdc_changelog.c +14
-32@@ -444,31 +444,6 @@ static ssize_t chlg_write(struct file *file, const char __user *buff,return rc <0 ? rc : count; } -/** -* Find the OBD device associated to a changelog character device. -* @param[in] cdev character device instance descriptor -* @return corresponding OBD device or NULL if none was found. -*/ -static struct obd_device *chlg_obd_get (dev_t cdev) -{ -int minor =MINOR (cdev); -struct obd_device *obd =NULL ; -struct chlg_registered_dev *curr; - -mutex_lock (&chlg_registered_dev_lock); -list_for_each_entry (curr, &chlg_registered_devices, ced_link) { -if (curr->ced_misc.minor == minor) { -/* take the first available OBD device attached */ - obd =list_first_entry (&curr->ced_obds, - struct obd_device, - u.cli.cl_chg_dev_linkage); -break ; - } - } -mutex_unlock (&chlg_registered_dev_lock); -return obd; -} -/** * Open handler, initialize internal CRS state and spawn prefetch thread if * needed. @@ -479,12 +454,16 @@ static struct obd_device *chlg_obd_get(dev_t cdev)static int chlg_open (struct inode *inode,struct file *file) {struct chlg_reader_state *crs; -struct obd_device *obd =chlg_obd_get (inode->i_rdev); +struct miscdevice *misc = file->private_data; +struct chlg_registered_dev *dev; +struct obd_device *obd;struct task_struct *task;int rc; -if (!obd) -return -ENODEV ; + dev = container_of(misc, struct chlg_registered_dev, ced_misc); + obd =list_first_entry (&dev->ced_obds, + struct obd_device, + u.cli.cl_chg_dev_linkage); crs =kzalloc (sizeof (*crs),GFP_KERNEL );if (!crs)@@ -669,13 +648,16 @@ int mdc_changelog_cdev_init(struct obd_device *obd) goto out_unlock; } +list_add_tail (&obd->u.cli.cl_chg_dev_linkage, &entry->ced_obds); +list_add_tail (&entry->ced_link, &chlg_registered_devices); +/* Register new character device */ rc =misc_register (&entry->ced_misc); -if (rc !=0 ) +if (rc !=0 ) { +list_del_init (&obd->u.cli.cl_chg_dev_linkage); +list_del (&entry->ced_link); goto out_unlock; - -list_add_tail (&obd->u.cli.cl_chg_dev_linkage, &entry->ced_obds); -list_add_tail (&entry->ced_link, &chlg_registered_devices); + } entry =NULL ;/* prevent it from being freed below */ Signature · 1 line
2.14.0.rc0.dirty
PATCH v2 1/1 lustre: mdc: fix possible deadlock in chlg_open()
Quoted 24 lines · quentin.bouget at cea.fr
Le 04/11/2018 ? 22:34, James Simmons a ?crit?:
Quoted 23 lines
Quoted 15 lines
Lockdep reports a possible deadlock between
chlg_open()andmdc_changelog_cdev_init()
mdc_changelog_cdev_init()takeschlg_registered_dev_lockand then callsmisc_register()which takesmisc_mtx.chlg_open()is called whilemisc_mtxis held, and tries to takechlg_registered_dev_lock.
If these two functions race, a deadlock can occur as each thread will hold one of the locks while trying to take the other.
chlg_open()does not need to take a lock. It only uses the lock to stablize a list while looking for the matchingchlg_registered_dev, and this can be found directly by examiningfile->private_data.Quoted 5 lines
So remove
chlg_obd_get(), and usefile->private_datato find theobd_device.
Also ensure the device is fully initialized before callingmisc_register(). This means setting up some list linkage before the call, and tearing it down if there is an error.I have been testing this but I'm no HSM expert. I pushed this patch to OpenSFS branch as well.
Quoted 7 lines
Quoted 4 lines
https://jira.whamcloud.com/browse/LU-11617
https://review.whamcloud.com/#/c/33572/Reviewed-by James Simmons <[email protected]>Reviewed-by Quentin Bouget <[email protected]>
Thanks to you both for the review.
NeilBrown
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 832 bytes
Desc: not available
URL: <http://lists.lustre.org/pipermail/lustre-devel-lustre.org/attachments/20181106/3a987263/attachment.sig>
PATCH v2 1/1 lustre: mdc: fix possible deadlock in chlg_open()
Le 06/11/2018 ? 04:11, NeilBrown a ?crit?:
Quoted 29 lines · quentin.bouget at cea.fr
Quoted 28 lines
Le 04/11/2018 ? 22:34, James Simmons a ?crit?:
Quoted 26 lines
Quoted 15 lines
Lockdep reports a possible deadlock between
chlg_open()andmdc_changelog_cdev_init()
mdc_changelog_cdev_init()takeschlg_registered_dev_lockand then callsmisc_register()which takesmisc_mtx.chlg_open()is called whilemisc_mtxis held, and tries to takechlg_registered_dev_lock.
If these two functions race, a deadlock can occur as each thread will hold one of the locks while trying to take the other.
chlg_open()does not need to take a lock. It only uses the lock to stablize a list while looking for the matchingchlg_registered_dev, and this can be found directly by examiningfile->private_data.Quoted 5 lines
So remove
chlg_obd_get(), and usefile->private_datato find theobd_device.
Also ensure the device is fully initialized before callingmisc_register(). This means setting up some list linkage before the call, and tearing it down if there is an error.I have been testing this but I'm no HSM expert. I pushed this patch to OpenSFS branch as well.
https://jira.whamcloud.com/browse/LU-11617
https://review.whamcloud.com/#/c/33572/
Quoted 7 lines
Quoted 3 lines
Quoted 1 line
Reviewed-by James Simmons <[email protected]>Reviewed-by Quentin Bouget <[email protected]>Thanks to you both for the review.
NeilBrown
Wait! I just realised there might be another issue! I think there is now a race between chlg_open() and mdc_changelog_cdev_finish().
Wait! I just realised there might be another bigger issue!
The whole "take the first obd you can find" is broken! I opened a ticket
<https://jira.whamcloud.com/browse/LU-11626> on whamcloud's JIRA about it.
Quentin Bouget
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.lustre.org/pipermail/lustre-devel-lustre.org/attachments/20181106/bad9cdb2/attachment-0001.html>
PATCH v2 1/1 lustre: mdc: fix possible deadlock in chlg_open()
Quoted 33 lines · quentin.bouget at cea.fr
Le 06/11/2018 ? 04:11, NeilBrown a ?crit?:
Quoted 32 lines · quentin.bouget at cea.fr
Quoted 31 lines
Le 04/11/2018 ? 22:34, James Simmons a ?crit?:
Quoted 20 lines
Quoted 15 lines
Lockdep reports a possible deadlock between
chlg_open()andmdc_changelog_cdev_init()
mdc_changelog_cdev_init()takeschlg_registered_dev_lockand then callsmisc_register()which takesmisc_mtx.chlg_open()is called whilemisc_mtxis held, and tries to takechlg_registered_dev_lock.
If these two functions race, a deadlock can occur as each thread will hold one of the locks while trying to take the other.
chlg_open()does not need to take a lock. It only uses the lock to stablize a list while looking for the matchingchlg_registered_dev, and this can be found directly by examiningfile->private_data.Quoted 5 lines
So remove
chlg_obd_get(), and usefile->private_datato find theobd_device.
Also ensure the device is fully initialized before callingmisc_register(). This means setting up some list linkage before the call, and tearing it down if there is an error.Quoted 7 lines
I have been testing this but I'm no HSM expert. I pushed this patch to OpenSFS branch as well.
https://jira.whamcloud.com/browse/LU-11617
https://review.whamcloud.com/#/c/33572/Reviewed-by James Simmons <[email protected]>Reviewed-by Quentin Bouget <[email protected]>
Quoted 7 lines
Quoted 4 lines
Thanks to you both for the review.
NeilBrown
Wait! I just realised there might be another issue! I think there is now a race between
chlg_open()andmdc_changelog_cdev_finish().
Hmmm.. yes. Also another deadlock possibility as the locks can be taken in the wrong order here too.
Quoted 4 lines
Wait! I just realised there might be another bigger issue!
The whole "take the first obd you can find" is broken! I opened a ticket
<https://jira.whamcloud.com/browse/LU-11626> on whamcloud's JIRA about it.
Yep...
My guess is that chlg_load(), chlg_clear() and (possibly) chlg_read_cat_process_cb() should take the mutex, choose an obd, used it, and release the mutex.
I might post an RFC patch.
NeilBrown
Quoted 2 lines
Quentin Bouget
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 832 bytes
Desc: not available
URL: <http://lists.lustre.org/pipermail/lustre-devel-lustre.org/attachments/20181107/df58b631/attachment.sig>
PATCH v2 1/1 lustre: mdc: fix possible deadlock in chlg_open()
Quoted 37 lines · NeilBrown
Quoted 35 lines · quentin.bouget at cea.fr
Le 06/11/2018 ? 04:11, NeilBrown a ?crit?:
Quoted 34 lines · quentin.bouget at cea.fr
Quoted 22 lines
Le 04/11/2018 ? 22:34, James Simmons a ?crit?:
Quoted 20 lines
Quoted 20 lines
Lockdep reports a possible deadlock between
chlg_open()andmdc_changelog_cdev_init()
mdc_changelog_cdev_init()takeschlg_registered_dev_lockand then callsmisc_register()which takesmisc_mtx.chlg_open()is called whilemisc_mtxis held, and tries to takechlg_registered_dev_lock.
If these two functions race, a deadlock can occur as each thread will hold one of the locks while trying to take the other.
chlg_open()does not need to take a lock. It only uses the lock to stablize a list while looking for the matchingchlg_registered_dev, and this can be found directly by examiningfile->private_data.So remove
chlg_obd_get(), and usefile->private_datato find theobd_device.
Also ensure the device is fully initialized before callingmisc_register(). This means setting up some list linkage before the call, and tearing it down if there is an error.Quoted 9 lines
Quoted 7 lines
I have been testing this but I'm no HSM expert. I pushed this patch to OpenSFS branch as well.
https://jira.whamcloud.com/browse/LU-11617
https://review.whamcloud.com/#/c/33572/Reviewed-by James Simmons <[email protected]>Reviewed-by Quentin Bouget <[email protected]>Thanks to you both for the review.
Quoted 8 lines
Quoted 5 lines
Quoted 2 lines
NeilBrown
Wait! I just realised there might be another issue! I think there is now a race between
chlg_open()andmdc_changelog_cdev_finish().Hmmm.. yes. Also another deadlock possibility as the locks can be taken in the wrong order here too.
Sorry, I got that wrong - there is no other deadlock. mdc_changelog_cdev_finish() can call misc_deregister() while holding chlg_registered_dev_lock, and misc_deregister() will take misc_mtx, but that is the right way around, not deadlock-causing.
NeilBrown
Quoted 6 lines
Quoted 5 lines
Wait! I just realised there might be another bigger issue!
The whole "take the first obd you can find" is broken! I opened a ticket
<https://jira.whamcloud.com/browse/LU-11626> on whamcloud's JIRA about it.
Quoted 12 lines
Yep...
My guess is thatchlg_load(),chlg_clear()and (possibly)chlg_read_cat_process_cb()should take the mutex, choose an obd, used it, and release the mutex.I might post an RFC patch.
NeilBrown
Quoted 2 lines
Quentin Bouget
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 832 bytes
Desc: not available
URL: <http://lists.lustre.org/pipermail/lustre-devel-lustre.org/attachments/20181107/fe44f28c/attachment.sig>
[lustre-devel] [PATCH/RFC] lustre: changelog_cdev need to find obd on each access.
In reply to NeilBrownA changelog cdev can be held open indefinitely, and obd structures can come and go while it is open. So it isn't safe to choose and obd at open time, and continue to use it.
Instead, we need to choose and obd on each access that needs it, and hold the chlg_registered_dev_lock mutex while using the obd. This prevents the obd from being freed.
To help with this, we store a link to the chlg_registered_dev in the chlg_reader_state, and use the name of the dev (instead of the name of the obd) in some error messages.
---
This is and RFC - I have only compile-tested it.
It seems sensible to me, but I know little about what is happening here so I could easily be missing something important.
See also https://jira.whamcloud.com/browse/LU-11626
Thanks,
NeilBrown
drivers/staging/lustre/lustre/mdc/mdc_changelog.c | 50 ++++++++++++++++------- 1 file changed, 36 insertions(+), 14 deletions(-)
drivers/staging/lustre/lustre/mdc/mdc_changelog.c +36 -14
@@ -66,8 +66,8 @@ struct chlg_registered_dev { };struct chlg_reader_state { -/* Shortcut to the corresponding OBD device */ -struct obd_device *crs_obd; +/* Device this state is associated with */ +struct chlg_registered_dev *crs_dev;/* Producer thread (if any) */ struct task_struct *crs_prod_task;/* An error occurred that prevents from reading further */
@@ -132,7 +132,7 @@ static int chlg_read_cat_process_cb(const struct lu_env *env,if (rec->cr_hdr.lrh_type !=CHANGELOG_REC ) { rc = -EINVAL ;CERROR ("%s: not a changelog rec %x/%d in llog : rc = %d\n" , - crs->crs_obd->obd_name, rec->cr_hdr.lrh_type, + crs->crs_dev->ced_name, rec->cr_hdr.lrh_type, rec->cr.cr_type, rc);return rc; }
@@ -183,6 +183,17 @@ static void enq_record_delete(struct chlg_rec_entry *rec)kfree (rec); } +/* +* Find any OBD device associated with this reader +* chlg_registered_dev_lock is held. +*/ +static inline struct obd_device *chlg_obd_get (struct chlg_registered_dev *dev) +{ +return list_first_entry_or_null (&dev->ced_obds, + struct obd_device, + u.cli.cl_chg_dev_linkage); +} +/** * Record prefetch thread entry point. Opens the changelog catalog and starts * reading records.
@@ -193,11 +204,17 @@ static void enq_record_delete(struct chlg_rec_entry *rec)static int chlg_load (void *args) {struct chlg_reader_state *crs = args; -struct obd_device *obd = crs->crs_obd; +struct obd_device *obd;struct llog_ctxt *ctx =NULL ;struct llog_handle *llh =NULL ;int rc; +mutex_lock (&chlg_registered_dev_lock); + obd =chlg_obd_get (crs->crs_dev); +if (!obd) { + rc = -ENOENT ; + goto err_out; + } ctx =llog_get_context (obd,LLOG_CHANGELOG_REPL_CTXT );if (!ctx) { rc = -ENOENT ;
@@ -230,6 +247,7 @@ static int chlg_load(void *args) crs->crs_eof = true; err_out: +mutex_unlock (&chlg_registered_dev_lock);if (rc <0 ) crs->crs_err = true;
@@ -387,15 +405,23 @@ static loff_t chlg_llseek(struct file *file, loff_t off, int whence)*/ static int chlg_clear (struct chlg_reader_state *crs,u32 reader,u64 record) { -struct obd_device *obd = crs->crs_obd; +struct obd_device *obd;struct changelog_setinfo cs = { .cs_recno = record, .cs_id = reader }; +int ret; -return obd_set_info_async (NULL , obd->obd_self_export, -strlen (KEY_CHANGELOG_CLEAR ), -KEY_CHANGELOG_CLEAR ,sizeof (cs), &cs,NULL ); +mutex_lock (&chlg_registered_dev_lock); + obd =chlg_obd_get (crs->crs_dev); +if (!obd) + ret = -ENOENT ; +else + ret =obd_set_info_async (NULL , obd->obd_self_export, +strlen (KEY_CHANGELOG_CLEAR ), +KEY_CHANGELOG_CLEAR ,sizeof (cs), &cs,NULL ); +mutex_unlock (&chlg_registered_dev_lock); +return ret; }/** Maximum changelog control command size */
@@ -456,20 +482,16 @@ static int chlg_open(struct inode *inode, struct file *file)struct chlg_reader_state *crs;struct miscdevice *misc = file->private_data;struct chlg_registered_dev *dev; -struct obd_device *obd;struct task_struct *task;int rc; dev = container_of(misc, struct chlg_registered_dev, ced_misc); - obd =list_first_entry (&dev->ced_obds, - struct obd_device, - u.cli.cl_chg_dev_linkage); crs =kzalloc (sizeof (*crs),GFP_KERNEL );if (!crs)return -ENOMEM ; - crs->crs_obd = obd; + crs->crs_dev = dev; crs->crs_err = false; crs->crs_eof = false;
@@ -483,7 +505,7 @@ static int chlg_open(struct inode *inode, struct file *file)if (IS_ERR (task)) { rc =PTR_ERR (task);CERROR ("%s: cannot start changelog thread: rc = %d\n" , - obd->obd_name, rc); + dev->ced_name, rc); goto err_crs; } crs->crs_prod_task = task;
Signature · 9 lines
2.14.0.rc0.dirty -------------- next part -------------- A non-text attachment was scrubbed... Name: signature.asc Type: application/pgp-signature Size: 832 bytes Desc: not available URL: <http://lists.lustre.org/pipermail/lustre-devel-lustre.org/attachments/20181107/c50ffabf/attachment.sig>
[lustre-devel] [PATCH/RFC] lustre: changelog_cdev need to find obd on each access.
Le 07/11/2018 ? 02:09, NeilBrown a ?crit?:
Quoted 7 lines
A changelog cdev can be held open indefinitely, and obd structures can come and go while it is open. So it isn't safe to choose and obd at open time, and continue to use it.
Instead, we need to choose and obd on each access that needs it, and hold the
chlg_registered_dev_lockmutex while using the obd. This prevents the obd from being freed.
struct llog_ctxt' has a reference on that obd (and struct llog_handle' has a reference on a `struct llog_ctxt').
I don't think letting the obd go away is safe.
Quoted 7 lines
drivers/staging/lustre/lustre/mdc/mdc_changelog.c +1
-1@@ -193,11 +204,17 @@ static void enq_record_delete(struct chlg_rec_entry *rec)static int chlg_load (void *args) {struct chlg_reader_state *crs = args; -struct obd_device *obd = crs->crs_obd; +struct obd_device *obd;struct llog_ctxt *ctx =NULL ;
drivers/staging/lustre/lustre/mdc/mdc_changelog.c static void enq_record_delete(struct chlg_rec_entry *rec) · line 218
Quoted 10 lines
drivers/staging/lustre/lustre/mdc/mdc_changelog.c +6
-0struct llog_handle *llh =NULL ;int rc; +mutex_lock (&chlg_registered_dev_lock); + obd =chlg_obd_get (crs->crs_dev); +if (!obd) { + rc = -ENOENT ; + goto err_out; + } ctx =llog_get_context (obd,LLOG_CHANGELOG_REPL_CTXT );
From there the obd is irrevocably referenced. I suspect it is used for
more than just its name.
Maybe trying to unmount the (wrong) mountpoint will fail after that, but I am not confident.
Quentin