Commit a46b5367 authored by Juergen Gross's avatar Juergen Gross Committed by Konrad Rzeszutek Wilk

xen/blkfront: cleanup stale persistent grants

Add a periodic cleanup function to remove old persistent grants which
are no longer in use on the backend side. This avoids starvation in
case there are lots of persistent grants for a device which no longer
is involved in I/O business.
Signed-off-by: default avatarJuergen Gross <jgross@suse.com>
Reviewed-by: default avatarRoger Pau Monné <roger.pau@citrix.com>
Signed-off-by: default avatarKonrad Rzeszutek Wilk <konrad.wilk@oracle.com>
parent 973e5405
...@@ -46,6 +46,7 @@ ...@@ -46,6 +46,7 @@
#include <linux/scatterlist.h> #include <linux/scatterlist.h>
#include <linux/bitmap.h> #include <linux/bitmap.h>
#include <linux/list.h> #include <linux/list.h>
#include <linux/workqueue.h>
#include <xen/xen.h> #include <xen/xen.h>
#include <xen/xenbus.h> #include <xen/xenbus.h>
...@@ -121,6 +122,8 @@ static inline struct blkif_req *blkif_req(struct request *rq) ...@@ -121,6 +122,8 @@ static inline struct blkif_req *blkif_req(struct request *rq)
static DEFINE_MUTEX(blkfront_mutex); static DEFINE_MUTEX(blkfront_mutex);
static const struct block_device_operations xlvbd_block_fops; static const struct block_device_operations xlvbd_block_fops;
static struct delayed_work blkfront_work;
static LIST_HEAD(info_list);
/* /*
* Maximum number of segments in indirect requests, the actual value used by * Maximum number of segments in indirect requests, the actual value used by
...@@ -216,6 +219,7 @@ struct blkfront_info ...@@ -216,6 +219,7 @@ struct blkfront_info
/* Save uncomplete reqs and bios for migration. */ /* Save uncomplete reqs and bios for migration. */
struct list_head requests; struct list_head requests;
struct bio_list bio_list; struct bio_list bio_list;
struct list_head info_list;
}; };
static unsigned int nr_minors; static unsigned int nr_minors;
...@@ -1759,6 +1763,12 @@ static int write_per_ring_nodes(struct xenbus_transaction xbt, ...@@ -1759,6 +1763,12 @@ static int write_per_ring_nodes(struct xenbus_transaction xbt,
return err; return err;
} }
static void free_info(struct blkfront_info *info)
{
list_del(&info->info_list);
kfree(info);
}
/* Common code used when first setting up, and when resuming. */ /* Common code used when first setting up, and when resuming. */
static int talk_to_blkback(struct xenbus_device *dev, static int talk_to_blkback(struct xenbus_device *dev,
struct blkfront_info *info) struct blkfront_info *info)
...@@ -1880,7 +1890,10 @@ static int talk_to_blkback(struct xenbus_device *dev, ...@@ -1880,7 +1890,10 @@ static int talk_to_blkback(struct xenbus_device *dev,
destroy_blkring: destroy_blkring:
blkif_free(info, 0); blkif_free(info, 0);
kfree(info); mutex_lock(&blkfront_mutex);
free_info(info);
mutex_unlock(&blkfront_mutex);
dev_set_drvdata(&dev->dev, NULL); dev_set_drvdata(&dev->dev, NULL);
return err; return err;
...@@ -1991,6 +2004,10 @@ static int blkfront_probe(struct xenbus_device *dev, ...@@ -1991,6 +2004,10 @@ static int blkfront_probe(struct xenbus_device *dev,
info->handle = simple_strtoul(strrchr(dev->nodename, '/')+1, NULL, 0); info->handle = simple_strtoul(strrchr(dev->nodename, '/')+1, NULL, 0);
dev_set_drvdata(&dev->dev, info); dev_set_drvdata(&dev->dev, info);
mutex_lock(&blkfront_mutex);
list_add(&info->info_list, &info_list);
mutex_unlock(&blkfront_mutex);
return 0; return 0;
} }
...@@ -2301,6 +2318,12 @@ static void blkfront_gather_backend_features(struct blkfront_info *info) ...@@ -2301,6 +2318,12 @@ static void blkfront_gather_backend_features(struct blkfront_info *info)
if (indirect_segments <= BLKIF_MAX_SEGMENTS_PER_REQUEST) if (indirect_segments <= BLKIF_MAX_SEGMENTS_PER_REQUEST)
indirect_segments = 0; indirect_segments = 0;
info->max_indirect_segments = indirect_segments; info->max_indirect_segments = indirect_segments;
if (info->feature_persistent) {
mutex_lock(&blkfront_mutex);
schedule_delayed_work(&blkfront_work, HZ * 10);
mutex_unlock(&blkfront_mutex);
}
} }
/* /*
...@@ -2482,7 +2505,9 @@ static int blkfront_remove(struct xenbus_device *xbdev) ...@@ -2482,7 +2505,9 @@ static int blkfront_remove(struct xenbus_device *xbdev)
mutex_unlock(&info->mutex); mutex_unlock(&info->mutex);
if (!bdev) { if (!bdev) {
kfree(info); mutex_lock(&blkfront_mutex);
free_info(info);
mutex_unlock(&blkfront_mutex);
return 0; return 0;
} }
...@@ -2502,7 +2527,9 @@ static int blkfront_remove(struct xenbus_device *xbdev) ...@@ -2502,7 +2527,9 @@ static int blkfront_remove(struct xenbus_device *xbdev)
if (info && !bdev->bd_openers) { if (info && !bdev->bd_openers) {
xlvbd_release_gendisk(info); xlvbd_release_gendisk(info);
disk->private_data = NULL; disk->private_data = NULL;
kfree(info); mutex_lock(&blkfront_mutex);
free_info(info);
mutex_unlock(&blkfront_mutex);
} }
mutex_unlock(&bdev->bd_mutex); mutex_unlock(&bdev->bd_mutex);
...@@ -2585,7 +2612,7 @@ static void blkif_release(struct gendisk *disk, fmode_t mode) ...@@ -2585,7 +2612,7 @@ static void blkif_release(struct gendisk *disk, fmode_t mode)
dev_info(disk_to_dev(bdev->bd_disk), "releasing disk\n"); dev_info(disk_to_dev(bdev->bd_disk), "releasing disk\n");
xlvbd_release_gendisk(info); xlvbd_release_gendisk(info);
disk->private_data = NULL; disk->private_data = NULL;
kfree(info); free_info(info);
} }
out: out:
...@@ -2618,6 +2645,61 @@ static struct xenbus_driver blkfront_driver = { ...@@ -2618,6 +2645,61 @@ static struct xenbus_driver blkfront_driver = {
.is_ready = blkfront_is_ready, .is_ready = blkfront_is_ready,
}; };
static void purge_persistent_grants(struct blkfront_info *info)
{
unsigned int i;
unsigned long flags;
for (i = 0; i < info->nr_rings; i++) {
struct blkfront_ring_info *rinfo = &info->rinfo[i];
struct grant *gnt_list_entry, *tmp;
spin_lock_irqsave(&rinfo->ring_lock, flags);
if (rinfo->persistent_gnts_c == 0) {
spin_unlock_irqrestore(&rinfo->ring_lock, flags);
continue;
}
list_for_each_entry_safe(gnt_list_entry, tmp, &rinfo->grants,
node) {
if (gnt_list_entry->gref == GRANT_INVALID_REF ||
gnttab_query_foreign_access(gnt_list_entry->gref))
continue;
list_del(&gnt_list_entry->node);
gnttab_end_foreign_access(gnt_list_entry->gref, 0, 0UL);
rinfo->persistent_gnts_c--;
__free_page(gnt_list_entry->page);
kfree(gnt_list_entry);
}
spin_unlock_irqrestore(&rinfo->ring_lock, flags);
}
}
static void blkfront_delay_work(struct work_struct *work)
{
struct blkfront_info *info;
bool need_schedule_work = false;
mutex_lock(&blkfront_mutex);
list_for_each_entry(info, &info_list, info_list) {
if (info->feature_persistent) {
need_schedule_work = true;
mutex_lock(&info->mutex);
purge_persistent_grants(info);
mutex_unlock(&info->mutex);
}
}
if (need_schedule_work)
schedule_delayed_work(&blkfront_work, HZ * 10);
mutex_unlock(&blkfront_mutex);
}
static int __init xlblk_init(void) static int __init xlblk_init(void)
{ {
int ret; int ret;
...@@ -2650,6 +2732,8 @@ static int __init xlblk_init(void) ...@@ -2650,6 +2732,8 @@ static int __init xlblk_init(void)
return -ENODEV; return -ENODEV;
} }
INIT_DELAYED_WORK(&blkfront_work, blkfront_delay_work);
ret = xenbus_register_frontend(&blkfront_driver); ret = xenbus_register_frontend(&blkfront_driver);
if (ret) { if (ret) {
unregister_blkdev(XENVBD_MAJOR, DEV_NAME); unregister_blkdev(XENVBD_MAJOR, DEV_NAME);
...@@ -2663,6 +2747,8 @@ module_init(xlblk_init); ...@@ -2663,6 +2747,8 @@ module_init(xlblk_init);
static void __exit xlblk_exit(void) static void __exit xlblk_exit(void)
{ {
cancel_delayed_work_sync(&blkfront_work);
xenbus_unregister_driver(&blkfront_driver); xenbus_unregister_driver(&blkfront_driver);
unregister_blkdev(XENVBD_MAJOR, DEV_NAME); unregister_blkdev(XENVBD_MAJOR, DEV_NAME);
kfree(minors); kfree(minors);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment