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

xen/blkback: don't keep persistent grants too long

Persistent grants are allocated until a threshold per ring is being
reached. Those grants won't be freed until the ring is being destroyed
meaning there will be resources kept busy which might no longer be
used.

Instead of freeing only persistent grants until the threshold is
reached add a timestamp and remove all persistent grants not having
been in use for a minute.
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 b86d865c
...@@ -15,3 +15,13 @@ Description: ...@@ -15,3 +15,13 @@ Description:
blkback. If the frontend tries to use more than blkback. If the frontend tries to use more than
max_persistent_grants, the LRU kicks in and starts max_persistent_grants, the LRU kicks in and starts
removing 5% of max_persistent_grants every 100ms. removing 5% of max_persistent_grants every 100ms.
What: /sys/module/xen_blkback/parameters/persistent_grant_unused_seconds
Date: August 2018
KernelVersion: 4.19
Contact: Roger Pau Monné <roger.pau@citrix.com>
Description:
How long a persistent grant is allowed to remain
allocated without being in use. The time is in
seconds, 0 means indefinitely long.
The default is 60 seconds.
...@@ -83,6 +83,18 @@ module_param_named(max_persistent_grants, xen_blkif_max_pgrants, int, 0644); ...@@ -83,6 +83,18 @@ module_param_named(max_persistent_grants, xen_blkif_max_pgrants, int, 0644);
MODULE_PARM_DESC(max_persistent_grants, MODULE_PARM_DESC(max_persistent_grants,
"Maximum number of grants to map persistently"); "Maximum number of grants to map persistently");
/*
* How long a persistent grant is allowed to remain allocated without being in
* use. The time is in seconds, 0 means indefinitely long.
*/
static unsigned int xen_blkif_pgrant_timeout = 60;
module_param_named(persistent_grant_unused_seconds, xen_blkif_pgrant_timeout,
uint, 0644);
MODULE_PARM_DESC(persistent_grant_unused_seconds,
"Time in seconds an unused persistent grant is allowed to "
"remain allocated. Default is 60, 0 means unlimited.");
/* /*
* Maximum number of rings/queues blkback supports, allow as many queues as there * Maximum number of rings/queues blkback supports, allow as many queues as there
* are CPUs if user has not specified a value. * are CPUs if user has not specified a value.
...@@ -123,6 +135,13 @@ module_param(log_stats, int, 0644); ...@@ -123,6 +135,13 @@ module_param(log_stats, int, 0644);
/* Number of free pages to remove on each call to gnttab_free_pages */ /* Number of free pages to remove on each call to gnttab_free_pages */
#define NUM_BATCH_FREE_PAGES 10 #define NUM_BATCH_FREE_PAGES 10
static inline bool persistent_gnt_timeout(struct persistent_gnt *persistent_gnt)
{
return xen_blkif_pgrant_timeout &&
(jiffies - persistent_gnt->last_used >=
HZ * xen_blkif_pgrant_timeout);
}
static inline int get_free_page(struct xen_blkif_ring *ring, struct page **page) static inline int get_free_page(struct xen_blkif_ring *ring, struct page **page)
{ {
unsigned long flags; unsigned long flags;
...@@ -278,7 +297,7 @@ static void put_persistent_gnt(struct xen_blkif_ring *ring, ...@@ -278,7 +297,7 @@ static void put_persistent_gnt(struct xen_blkif_ring *ring,
{ {
if(!test_bit(PERSISTENT_GNT_ACTIVE, persistent_gnt->flags)) if(!test_bit(PERSISTENT_GNT_ACTIVE, persistent_gnt->flags))
pr_alert_ratelimited("freeing a grant already unused\n"); pr_alert_ratelimited("freeing a grant already unused\n");
set_bit(PERSISTENT_GNT_WAS_ACTIVE, persistent_gnt->flags); persistent_gnt->last_used = jiffies;
clear_bit(PERSISTENT_GNT_ACTIVE, persistent_gnt->flags); clear_bit(PERSISTENT_GNT_ACTIVE, persistent_gnt->flags);
atomic_dec(&ring->persistent_gnt_in_use); atomic_dec(&ring->persistent_gnt_in_use);
} }
...@@ -371,26 +390,26 @@ static void purge_persistent_gnt(struct xen_blkif_ring *ring) ...@@ -371,26 +390,26 @@ static void purge_persistent_gnt(struct xen_blkif_ring *ring)
struct persistent_gnt *persistent_gnt; struct persistent_gnt *persistent_gnt;
struct rb_node *n; struct rb_node *n;
unsigned int num_clean, total; unsigned int num_clean, total;
bool scan_used = false, clean_used = false; bool scan_used = false;
struct rb_root *root; struct rb_root *root;
if (ring->persistent_gnt_c < xen_blkif_max_pgrants ||
(ring->persistent_gnt_c == xen_blkif_max_pgrants &&
!ring->blkif->vbd.overflow_max_grants)) {
goto out;
}
if (work_busy(&ring->persistent_purge_work)) { if (work_busy(&ring->persistent_purge_work)) {
pr_alert_ratelimited("Scheduled work from previous purge is still busy, cannot purge list\n"); pr_alert_ratelimited("Scheduled work from previous purge is still busy, cannot purge list\n");
goto out; goto out;
} }
if (ring->persistent_gnt_c < xen_blkif_max_pgrants ||
(ring->persistent_gnt_c == xen_blkif_max_pgrants &&
!ring->blkif->vbd.overflow_max_grants)) {
num_clean = 0;
} else {
num_clean = (xen_blkif_max_pgrants / 100) * LRU_PERCENT_CLEAN; num_clean = (xen_blkif_max_pgrants / 100) * LRU_PERCENT_CLEAN;
num_clean = ring->persistent_gnt_c - xen_blkif_max_pgrants + num_clean; num_clean = ring->persistent_gnt_c - xen_blkif_max_pgrants +
num_clean;
num_clean = min(ring->persistent_gnt_c, num_clean); num_clean = min(ring->persistent_gnt_c, num_clean);
if ((num_clean == 0) || pr_debug("Going to purge at least %u persistent grants\n",
(num_clean > (ring->persistent_gnt_c - atomic_read(&ring->persistent_gnt_in_use)))) num_clean);
goto out; }
/* /*
* At this point, we can assure that there will be no calls * At this point, we can assure that there will be no calls
...@@ -401,9 +420,7 @@ static void purge_persistent_gnt(struct xen_blkif_ring *ring) ...@@ -401,9 +420,7 @@ static void purge_persistent_gnt(struct xen_blkif_ring *ring)
* number of grants. * number of grants.
*/ */
total = num_clean; total = 0;
pr_debug("Going to purge %u persistent grants\n", num_clean);
BUG_ON(!list_empty(&ring->persistent_purge_list)); BUG_ON(!list_empty(&ring->persistent_purge_list));
root = &ring->persistent_gnts; root = &ring->persistent_gnts;
...@@ -412,46 +429,37 @@ static void purge_persistent_gnt(struct xen_blkif_ring *ring) ...@@ -412,46 +429,37 @@ static void purge_persistent_gnt(struct xen_blkif_ring *ring)
BUG_ON(persistent_gnt->handle == BUG_ON(persistent_gnt->handle ==
BLKBACK_INVALID_HANDLE); BLKBACK_INVALID_HANDLE);
if (clean_used) {
clear_bit(PERSISTENT_GNT_WAS_ACTIVE, persistent_gnt->flags);
continue;
}
if (test_bit(PERSISTENT_GNT_ACTIVE, persistent_gnt->flags)) if (test_bit(PERSISTENT_GNT_ACTIVE, persistent_gnt->flags))
continue; continue;
if (!scan_used && if (!scan_used && !persistent_gnt_timeout(persistent_gnt))
(test_bit(PERSISTENT_GNT_WAS_ACTIVE, persistent_gnt->flags))) continue;
if (scan_used && total >= num_clean)
continue; continue;
rb_erase(&persistent_gnt->node, root); rb_erase(&persistent_gnt->node, root);
list_add(&persistent_gnt->remove_node, list_add(&persistent_gnt->remove_node,
&ring->persistent_purge_list); &ring->persistent_purge_list);
if (--num_clean == 0) total++;
goto finished;
} }
/* /*
* If we get here it means we also need to start cleaning * Check whether we also need to start cleaning
* grants that were used since last purge in order to cope * grants that were used since last purge in order to cope
* with the requested num * with the requested num
*/ */
if (!scan_used && !clean_used) { if (!scan_used && total < num_clean) {
pr_debug("Still missing %u purged frames\n", num_clean); pr_debug("Still missing %u purged frames\n", num_clean - total);
scan_used = true; scan_used = true;
goto purge_list; goto purge_list;
} }
finished:
if (!clean_used) {
pr_debug("Finished scanning for grants to clean, removing used flag\n");
clean_used = true;
goto purge_list;
}
ring->persistent_gnt_c -= (total - num_clean); if (total) {
ring->persistent_gnt_c -= total;
ring->blkif->vbd.overflow_max_grants = 0; ring->blkif->vbd.overflow_max_grants = 0;
/* We can defer this work */ /* We can defer this work */
schedule_work(&ring->persistent_purge_work); schedule_work(&ring->persistent_purge_work);
pr_debug("Purged %u/%u\n", (total - num_clean), total); pr_debug("Purged %u/%u\n", num_clean, total);
}
out: out:
return; return;
......
...@@ -234,14 +234,9 @@ struct xen_vbd { ...@@ -234,14 +234,9 @@ struct xen_vbd {
struct backend_info; struct backend_info;
/* Number of available flags */ /* Number of available flags */
#define PERSISTENT_GNT_FLAGS_SIZE 2 #define PERSISTENT_GNT_FLAGS_SIZE 1
/* This persistent grant is currently in use */ /* This persistent grant is currently in use */
#define PERSISTENT_GNT_ACTIVE 0 #define PERSISTENT_GNT_ACTIVE 0
/*
* This persistent grant has been used, this flag is set when we remove the
* PERSISTENT_GNT_ACTIVE, to know that this grant has been used recently.
*/
#define PERSISTENT_GNT_WAS_ACTIVE 1
/* Number of requests that we can fit in a ring */ /* Number of requests that we can fit in a ring */
#define XEN_BLKIF_REQS_PER_PAGE 32 #define XEN_BLKIF_REQS_PER_PAGE 32
...@@ -250,6 +245,7 @@ struct persistent_gnt { ...@@ -250,6 +245,7 @@ struct persistent_gnt {
struct page *page; struct page *page;
grant_ref_t gnt; grant_ref_t gnt;
grant_handle_t handle; grant_handle_t handle;
unsigned long last_used;
DECLARE_BITMAP(flags, PERSISTENT_GNT_FLAGS_SIZE); DECLARE_BITMAP(flags, PERSISTENT_GNT_FLAGS_SIZE);
struct rb_node node; struct rb_node node;
struct list_head remove_node; struct list_head remove_node;
......
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