From mboxrd@z Thu Jan 1 00:00:00 1970 From: Matt Wilson Subject: [RFC] [PATCH] xen/blkback: add locking in statistics sysfs show functions Date: Fri, 20 Apr 2012 19:21:13 +0000 Message-ID: <1334949673-25632-2-git-send-email-msw@amazon.com> References: <1334949673-25632-1-git-send-email-msw@amazon.com> Mime-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Return-path: In-Reply-To: <1334949673-25632-1-git-send-email-msw@amazon.com> List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Sender: xen-devel-bounces@lists.xen.org Errors-To: xen-devel-bounces@lists.xen.org To: Konrad Rzeszutek Wilk Cc: Joe Jin , Matt Wilson , xen-devel@lists.xen.org List-Id: xen-devel@lists.xenproject.org This is a port of a patch in the XenoLinux 2.6.18 tree: http://xenbits.xen.org/hg/linux-2.6.18-xen.hg/rev/f47c07325a56 Fix blkback sysfs race Read blkback statistics info after remove vbd device(s) kernel will crash. Signed-off-by: Joe Jin Acked-by: Jan Beulich [Ported to Linux 3.x] Signed-off-by: Matt Wilson --- drivers/block/xen-blkback/xenbus.c | 20 +++++++++++++++++--- 1 files changed, 17 insertions(+), 3 deletions(-) diff --git a/drivers/block/xen-blkback/xenbus.c b/drivers/block/xen-blkback/xenbus.c index da19506..74d4810 100644 --- a/drivers/block/xen-blkback/xenbus.c +++ b/drivers/block/xen-blkback/xenbus.c @@ -21,6 +21,8 @@ #include #include "common.h" +static DEFINE_RWLOCK(sysfs_read_lock); + struct backend_info { struct xenbus_device *dev; struct xen_blkif *blkif; @@ -225,10 +227,20 @@ int __init xen_blkif_interface_init(void) struct device_attribute *attr, \ char *buf) \ { \ - struct xenbus_device *dev = to_xenbus_device(_dev); \ - struct backend_info *be = dev_get_drvdata(&dev->dev); \ + ssize_t ret = -ENODEV; \ + struct xenbus_device *dev; \ + struct backend_info *be; \ \ - return sprintf(buf, format, ##args); \ + if (!get_device(_dev)) \ + return ret; \ + dev = to_xenbus_device(_dev); \ + read_lock(&sysfs_read_lock); \ + be = (struct backend_info *) dev_get_drvdata(&dev->dev);\ + if (be != NULL) \ + ret = sprintf(buf, format, ##args); \ + read_unlock(&sysfs_read_lock); \ + put_device(_dev); \ + return ret; \ } \ static DEVICE_ATTR(name, S_IRUGO, show_##name, NULL) @@ -353,6 +365,7 @@ static int xen_blkbk_remove(struct xenbus_device *dev) DPRINTK(""); + write_lock(&sysfs_read_lock); if (be->major || be->minor) xenvbd_sysfs_delif(dev); @@ -371,6 +384,7 @@ static int xen_blkbk_remove(struct xenbus_device *dev) kfree(be); dev_set_drvdata(&dev->dev, NULL); + write_unlock(&sysfs_read_lock); return 0; } -- 1.7.2.5