From: "Jan Beulich" Subject: Re: [PATCH] Fix blk{back,tap} sysfs race Date: Thu, 21 Jan 2010 08:23:19 +0000 Message-ID: <4B581D07020000780002B2F3@vpn.id2.novell.com> References: <20100121004658.GA28792@joejin-pc.cn.oracle.com> Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: quoted-printable Return-path: In-Reply-To: <20100121004658.GA28792@joejin-pc.cn.oracle.com> Content-Disposition: inline List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Sender: xen-devel-bounces@lists.xensource.com Errors-To: xen-devel-bounces@lists.xensource.com To: Keir Fraser , Joe Jin Cc: xen-devel@lists.xensource.com, deepak.patel@oracle.com, greg.marsden@oracle.com List-Id: xen-devel@lists.xenproject.org While I would have wanted it to use DEFINE_RWLOCK(), it's probably okay without. Or maybe you could adjust this as you check it in, Keir? Acked-by: Jan Beulich >>> "Joe Jin" 21.01.10 01:46 >>> Hi,=20 Read blk{back,tap} statistics info after remove vbd device(s) kernel will = crash. Below patch will fix the race, please review and apply. Signed-off-by: Joe Jin Cc: Keir Fraser Cc: Jan Beulich --- blkback/xenbus.c | 19 ++++++++++++++++--- blktap/xenbus.c | 18 +++++++++++++++--- 2 files changed, 31 insertions(+), 6 deletions(-) diff -r 0bec29c94ce9 drivers/xen/blkback/xenbus.c --- a/drivers/xen/blkback/xenbus.c Mon Jan 18 14:50:43 2010 +0000 +++ b/drivers/xen/blkback/xenbus.c Thu Jan 21 08:38:58 2010 +0800 @@ -26,6 +26,8 @@ #define DPRINTK(fmt, args...) \ pr_debug("blkback/xenbus (%s:%d) " fmt ".\n", \ __FUNCTION__, __LINE__, ##args) + +static rwlock_t sysfs_read_lock =3D RW_LOCK_UNLOCKED; =20 struct backend_info { @@ -104,10 +106,19 @@ struct device_attribute *attr, \ char *buf) \ { \ - struct xenbus_device *dev =3D to_xenbus_device(_dev); \ - struct backend_info *be =3D dev->dev.driver_data; = \ + ssize_t ret =3D -ENODEV; = \ + struct xenbus_device *dev; \ + struct backend_info *be; \ \ - return sprintf(buf, format, ##args); \ + if (!get_device(_dev)) \ + return ret; \ + dev =3D to_xenbus_device(_dev); \ + read_lock(&sysfs_read_lock); \ + if ((be =3D dev->dev.driver_data) !=3D NULL) \ + ret =3D sprintf(buf, format, ##args); \ + read_unlock(&sysfs_read_lock); \ + put_device(_dev); \ + return ret; \ } \ static DEVICE_ATTR(name, S_IRUGO, show_##name, NULL) =20 @@ -173,6 +184,7 @@ =20 DPRINTK(""); =20 + write_lock(&sysfs_read_lock); if (be->major || be->minor) xenvbd_sysfs_delif(dev); =20 @@ -191,6 +203,7 @@ =20 kfree(be); dev->dev.driver_data =3D NULL; + write_unlock(&sysfs_read_lock); return 0; } =20 diff -r 0bec29c94ce9 drivers/xen/blktap/xenbus.c --- a/drivers/xen/blktap/xenbus.c Mon Jan 18 14:50:43 2010 +0000 +++ b/drivers/xen/blktap/xenbus.c Thu Jan 21 08:38:58 2010 +0800 @@ -50,6 +50,7 @@ int group_added; }; =20 +static rwlock_t sysfs_read_lock =3D RW_LOCK_UNLOCKED; =20 static void connect(struct backend_info *); static int connect_ring(struct backend_info *); @@ -122,10 +123,19 @@ struct device_attribute *attr, \ char *buf) \ { \ - struct xenbus_device *dev =3D to_xenbus_device(_dev); \ - struct backend_info *be =3D dev->dev.driver_data; = \ + ssize_t ret =3D -ENODEV; = \ + struct xenbus_device *dev; \ + struct backend_info *be; \ \ - return sprintf(buf, format, ##args); \ + if (!get_device(_dev)) \ + return ret; \ + dev =3D to_xenbus_device(_dev); \ + read_lock(&sysfs_read_lock); \ + if ((be =3D dev->dev.driver_data) !=3D NULL) \ + ret =3D sprintf(buf, format, ##args); \ + read_unlock(&sysfs_read_lock); \ + put_device(_dev); \ + return ret; \ } \ static DEVICE_ATTR(name, S_IRUGO, show_##name, NULL) =20 @@ -170,6 +180,7 @@ { struct backend_info *be =3D dev->dev.driver_data; =20 + write_lock(&sysfs_read_lock); if (be->group_added) xentap_sysfs_delif(be->dev); if (be->backend_watch.node) { @@ -187,6 +198,7 @@ } kfree(be); dev->dev.driver_data =3D NULL; + write_unlock(&sysfs_read_lock); return 0; } =20