ia64/xen-unstable

changeset 10029:019411cc3ae5

Fix blkif and netif backend teardown -- do not remove devices from
sysfs (and hence trigger hotplug callbacks) until the devices really
are dead. This fixes a bug where the deferred code to free a blk
device was running concurrently with a hotplug-remove callback which
would try to reclaim the underlying storage. In some cases the race
would be lost and the hotplug script would fail.

Thanks to the Zhu Han at Intel for finding the root cause of this
long-term and annoying bug!

Signed-off-by: Keir Fraser <keir@xensource.com>
author kaf24@firebug.cl.cam.ac.uk
date Wed May 10 13:27:17 2006 +0100 (2006-05-10)
parents 1e3977e029fd
children d8954fc3b76b
files linux-2.6-xen-sparse/drivers/xen/blkback/common.h linux-2.6-xen-sparse/drivers/xen/blkback/interface.c linux-2.6-xen-sparse/drivers/xen/blkback/xenbus.c linux-2.6-xen-sparse/drivers/xen/netback/common.h linux-2.6-xen-sparse/drivers/xen/netback/interface.c linux-2.6-xen-sparse/drivers/xen/netback/xenbus.c
line diff
     1.1 --- a/linux-2.6-xen-sparse/drivers/xen/blkback/common.h	Mon May 08 19:21:41 2006 +0100
     1.2 +++ b/linux-2.6-xen-sparse/drivers/xen/blkback/common.h	Wed May 10 13:27:17 2006 +0100
     1.3 @@ -34,6 +34,7 @@
     1.4  #include <linux/slab.h>
     1.5  #include <linux/blkdev.h>
     1.6  #include <linux/vmalloc.h>
     1.7 +#include <linux/wait.h>
     1.8  #include <asm/io.h>
     1.9  #include <asm/setup.h>
    1.10  #include <asm/pgalloc.h>
    1.11 @@ -90,21 +91,21 @@ typedef struct blkif_st {
    1.12  	int                 st_wr_req;
    1.13  	int                 st_oo_req;
    1.14  
    1.15 -	struct work_struct free_work;
    1.16 +	wait_queue_head_t waiting_to_free;
    1.17  
    1.18  	grant_handle_t shmem_handle;
    1.19  	grant_ref_t    shmem_ref;
    1.20  } blkif_t;
    1.21  
    1.22 -blkif_t *alloc_blkif(domid_t domid);
    1.23 -void free_blkif_callback(blkif_t *blkif);
    1.24 +blkif_t *blkif_alloc(domid_t domid);
    1.25 +void blkif_free(blkif_t *blkif);
    1.26  int blkif_map(blkif_t *blkif, unsigned long shared_page, unsigned int evtchn);
    1.27  
    1.28  #define blkif_get(_b) (atomic_inc(&(_b)->refcnt))
    1.29  #define blkif_put(_b)					\
    1.30  	do {						\
    1.31  		if (atomic_dec_and_test(&(_b)->refcnt))	\
    1.32 -			free_blkif_callback(_b);	\
    1.33 +			wake_up(&(_b)->waiting_to_free);\
    1.34  	} while (0)
    1.35  
    1.36  /* Create a vbd. */
     2.1 --- a/linux-2.6-xen-sparse/drivers/xen/blkback/interface.c	Mon May 08 19:21:41 2006 +0100
     2.2 +++ b/linux-2.6-xen-sparse/drivers/xen/blkback/interface.c	Wed May 10 13:27:17 2006 +0100
     2.3 @@ -35,7 +35,7 @@
     2.4  
     2.5  static kmem_cache_t *blkif_cachep;
     2.6  
     2.7 -blkif_t *alloc_blkif(domid_t domid)
     2.8 +blkif_t *blkif_alloc(domid_t domid)
     2.9  {
    2.10  	blkif_t *blkif;
    2.11  
    2.12 @@ -49,6 +49,7 @@ blkif_t *alloc_blkif(domid_t domid)
    2.13  	atomic_set(&blkif->refcnt, 1);
    2.14  	init_waitqueue_head(&blkif->wq);
    2.15  	blkif->st_print = jiffies;
    2.16 +	init_waitqueue_head(&blkif->waiting_to_free);
    2.17  
    2.18  	return blkif;
    2.19  }
    2.20 @@ -138,33 +139,25 @@ int blkif_map(blkif_t *blkif, unsigned l
    2.21  	return 0;
    2.22  }
    2.23  
    2.24 -static void free_blkif(void *arg)
    2.25 +void blkif_free(blkif_t *blkif)
    2.26  {
    2.27 -	blkif_t *blkif = (blkif_t *)arg;
    2.28 +	atomic_dec(&blkif->refcnt);
    2.29 +	wait_event(blkif->waiting_to_free, atomic_read(&blkif->refcnt) == 0);
    2.30  
    2.31  	/* Already disconnected? */
    2.32 -	if (blkif->irq) {
    2.33 +	if (blkif->irq)
    2.34  		unbind_from_irqhandler(blkif->irq, blkif);
    2.35 -		blkif->irq = 0;
    2.36 -	}
    2.37  
    2.38  	vbd_free(&blkif->vbd);
    2.39  
    2.40  	if (blkif->blk_ring.sring) {
    2.41  		unmap_frontend_page(blkif);
    2.42  		free_vm_area(blkif->blk_ring_area);
    2.43 -		blkif->blk_ring.sring = NULL;
    2.44  	}
    2.45  
    2.46  	kmem_cache_free(blkif_cachep, blkif);
    2.47  }
    2.48  
    2.49 -void free_blkif_callback(blkif_t *blkif)
    2.50 -{
    2.51 -	INIT_WORK(&blkif->free_work, free_blkif, (void *)blkif);
    2.52 -	schedule_work(&blkif->free_work);
    2.53 -}
    2.54 -
    2.55  void __init blkif_interface_init(void)
    2.56  {
    2.57  	blkif_cachep = kmem_cache_create("blkif_cache", sizeof(blkif_t), 
     3.1 --- a/linux-2.6-xen-sparse/drivers/xen/blkback/xenbus.c	Mon May 08 19:21:41 2006 +0100
     3.2 +++ b/linux-2.6-xen-sparse/drivers/xen/blkback/xenbus.c	Wed May 10 13:27:17 2006 +0100
     3.3 @@ -108,7 +108,7 @@ static int blkback_remove(struct xenbus_
     3.4  	if (be->blkif) {
     3.5  		if (be->blkif->xenblkd)
     3.6  			kthread_stop(be->blkif->xenblkd);
     3.7 -		blkif_put(be->blkif);
     3.8 +		blkif_free(be->blkif);
     3.9  		be->blkif = NULL;
    3.10  	}
    3.11  
    3.12 @@ -140,7 +140,7 @@ static int blkback_probe(struct xenbus_d
    3.13  	be->dev = dev;
    3.14  	dev->data = be;
    3.15  
    3.16 -	be->blkif = alloc_blkif(dev->otherend_id);
    3.17 +	be->blkif = blkif_alloc(dev->otherend_id);
    3.18  	if (IS_ERR(be->blkif)) {
    3.19  		err = PTR_ERR(be->blkif);
    3.20  		be->blkif = NULL;
     4.1 --- a/linux-2.6-xen-sparse/drivers/xen/netback/common.h	Mon May 08 19:21:41 2006 +0100
     4.2 +++ b/linux-2.6-xen-sparse/drivers/xen/netback/common.h	Wed May 10 13:27:17 2006 +0100
     4.3 @@ -38,6 +38,7 @@
     4.4  #include <linux/in.h>
     4.5  #include <linux/netdevice.h>
     4.6  #include <linux/etherdevice.h>
     4.7 +#include <linux/wait.h>
     4.8  #include <xen/evtchn.h>
     4.9  #include <xen/interface/io/netif.h>
    4.10  #include <asm/io.h>
    4.11 @@ -91,7 +92,7 @@ typedef struct netif_st {
    4.12  	struct net_device *dev;
    4.13  	struct net_device_stats stats;
    4.14  
    4.15 -	struct work_struct free_work;
    4.16 +	wait_queue_head_t waiting_to_free;
    4.17  } netif_t;
    4.18  
    4.19  #define NET_TX_RING_SIZE __RING_SIZE((netif_tx_sring_t *)0, PAGE_SIZE)
    4.20 @@ -99,8 +100,7 @@ typedef struct netif_st {
    4.21  
    4.22  void netif_disconnect(netif_t *netif);
    4.23  
    4.24 -netif_t *alloc_netif(domid_t domid, unsigned int handle, u8 be_mac[ETH_ALEN]);
    4.25 -void free_netif(netif_t *netif);
    4.26 +netif_t *netif_alloc(domid_t domid, unsigned int handle, u8 be_mac[ETH_ALEN]);
    4.27  int netif_map(netif_t *netif, unsigned long tx_ring_ref,
    4.28  	      unsigned long rx_ring_ref, unsigned int evtchn);
    4.29  
    4.30 @@ -108,7 +108,7 @@ int netif_map(netif_t *netif, unsigned l
    4.31  #define netif_put(_b)						\
    4.32  	do {							\
    4.33  		if ( atomic_dec_and_test(&(_b)->refcnt) )	\
    4.34 -			free_netif(_b);				\
    4.35 +			wake_up(&(_b)->waiting_to_free);	\
    4.36  	} while (0)
    4.37  
    4.38  void netif_xenbus_init(void);
     5.1 --- a/linux-2.6-xen-sparse/drivers/xen/netback/interface.c	Mon May 08 19:21:41 2006 +0100
     5.2 +++ b/linux-2.6-xen-sparse/drivers/xen/netback/interface.c	Wed May 10 13:27:17 2006 +0100
     5.3 @@ -78,7 +78,7 @@ static struct ethtool_ops network_ethtoo
     5.4  	.set_tx_csum = ethtool_op_set_tx_csum,
     5.5  };
     5.6  
     5.7 -netif_t *alloc_netif(domid_t domid, unsigned int handle, u8 be_mac[ETH_ALEN])
     5.8 +netif_t *netif_alloc(domid_t domid, unsigned int handle, u8 be_mac[ETH_ALEN])
     5.9  {
    5.10  	int err = 0, i;
    5.11  	struct net_device *dev;
    5.12 @@ -97,7 +97,8 @@ netif_t *alloc_netif(domid_t domid, unsi
    5.13  	netif->domid  = domid;
    5.14  	netif->handle = handle;
    5.15  	netif->status = DISCONNECTED;
    5.16 -	atomic_set(&netif->refcnt, 0);
    5.17 +	atomic_set(&netif->refcnt, 1);
    5.18 +	init_waitqueue_head(&netif->waiting_to_free);
    5.19  	netif->dev = dev;
    5.20  
    5.21  	netif->credit_bytes = netif->remaining_credit = ~0UL;
    5.22 @@ -273,9 +274,10 @@ err_rx:
    5.23  	return err;
    5.24  }
    5.25  
    5.26 -static void free_netif_callback(void *arg)
    5.27 +static void netif_free(netif_t *netif)
    5.28  {
    5.29 -	netif_t *netif = (netif_t *)arg;
    5.30 +	atomic_dec(&netif->refcnt);
    5.31 +	wait_event(netif->waiting_to_free, atomic_read(&netif->refcnt) == 0);
    5.32  
    5.33  	if (netif->irq)
    5.34  		unbind_from_irqhandler(netif->irq, netif);
    5.35 @@ -291,12 +293,6 @@ static void free_netif_callback(void *ar
    5.36  	free_netdev(netif->dev);
    5.37  }
    5.38  
    5.39 -void free_netif(netif_t *netif)
    5.40 -{
    5.41 -	INIT_WORK(&netif->free_work, free_netif_callback, (void *)netif);
    5.42 -	schedule_work(&netif->free_work);
    5.43 -}
    5.44 -
    5.45  void netif_disconnect(netif_t *netif)
    5.46  {
    5.47  	switch (netif->status) {
    5.48 @@ -308,10 +304,9 @@ void netif_disconnect(netif_t *netif)
    5.49  			__netif_down(netif);
    5.50  		rtnl_unlock();
    5.51  		netif_put(netif);
    5.52 -		break;
    5.53 +		/* fall through */
    5.54  	case DISCONNECTED:
    5.55 -		BUG_ON(atomic_read(&netif->refcnt) != 0);
    5.56 -		free_netif(netif);
    5.57 +		netif_free(netif);
    5.58  		break;
    5.59  	default:
    5.60  		BUG();
     6.1 --- a/linux-2.6-xen-sparse/drivers/xen/netback/xenbus.c	Mon May 08 19:21:41 2006 +0100
     6.2 +++ b/linux-2.6-xen-sparse/drivers/xen/netback/xenbus.c	Wed May 10 13:27:17 2006 +0100
     6.3 @@ -172,7 +172,7 @@ static void backend_changed(struct xenbu
     6.4  	if (be->netif == NULL) {
     6.5  		u8 be_mac[ETH_ALEN] = { 0, 0, 0, 0, 0, 0 };
     6.6  
     6.7 -		be->netif = alloc_netif(dev->otherend_id, handle, be_mac);
     6.8 +		be->netif = netif_alloc(dev->otherend_id, handle, be_mac);
     6.9  		if (IS_ERR(be->netif)) {
    6.10  			err = PTR_ERR(be->netif);
    6.11  			be->netif = NULL;