2020-01-27 08:35:36

by SeongJae Park

[permalink] [raw]
Subject: [PATCH v14 0/5] xenbus/backend: Add memory pressure handler callback

From: SeongJae Park <[email protected]>

Granting pages consumes backend system memory. In systems configured
with insufficient spare memory for those pages, it can cause a memory
pressure situation. However, finding the optimal amount of the spare
memory is challenging for large systems having dynamic resource
utilization patterns. Also, such a static configuration might lack
flexibility.

To mitigate such problems, this patchset adds a memory reclaim callback
to 'xenbus_driver' (patch 1) and then introduce a lock for race
condition avoidance (patch 2). After that, patch 3 applies the callback
mechanism to mitigate the problem in 'xen-blkback'. The fourth and
fifth patches are trivial cleanups; those fix nits we found during the
development of this patchset.

Note that this patch has only trivial changes from v13. Please refer to
below patch history for the changes.


Base Version
------------

This patch is based on v5.5 Linux kernel. A complete tree is also
available at my public git repo:
https://github.com/sjp38/linux/tree/patches/blkback/buffer_squeeze/v14


Patch History
-------------

Changes from v13
(https://lore.kernel.org/xen-devel/[email protected]/)
- Rebase on v5.5
- Add 'Reviewed-by' tags
- Update applied version of the new blkback parameter (5.5 -> 5.6)

Changes from v12
(https://lore.kernel.org/xen-devel/[email protected]/)
- Do not unnecessarily disable interrupts (suggested by Juergen)
- Hold lock from xenbus side (suggested by Juergen)

Changes from v11
(https://lore.kernel.org/xen-devel/[email protected]/)
- Fix wrong trylock use (reported by Juergen)
- Merge patch 3 and 4 (suggested by Juergen)
- Update test result

Changes from v10
(https://lore.kernel.org/xen-devel/[email protected]/)
- Fix race condition (reported by SeongJae, suggested by Juergen)

Changes from v9
(https://lore.kernel.org/xen-devel/[email protected]/)
- Add 'Reviewed-by' and 'Acked-by' from Roger Pau Monné
- Update the commit message for overhead test of the 2nd path

Changes from v8
(https://lore.kernel.org/xen-devel/[email protected]/)
- Drop 'Reviewed-by: Juergen' from the second patch
(suggested by Roger Pau Monné)
- Update contact of the new module param to SeongJae Park
<[email protected]>
(suggested by Roger Pau Monné)
- Wordsmith the description of the parameter
(suggested by Roger Pau Monné)
- Fix dumb bugs
(suggested by Roger Pau Monné)
- Move module param definition to xenbus.c and reduce the number of
lines for this change
(suggested by Roger Pau Monné)
- Add a comment for the new callback, reclaim_memory, as other
callbacks also have
- Add another trivial cleanup of xenbus.c file (4th patch)

Changes from v7
(https://lore.kernel.org/xen-devel/[email protected]/)
- Update sysfs-driver-xen-blkback for new parameter
(suggested by Roger Pau Monné)
- Use per-xen_blkif buffer_squeeze_end instead of global variable
(suggested by Roger Pau Monné)

Changes from v6
(https://lore.kernel.org/linux-block/[email protected]/)
- Remove more unnecessary prefixes (suggested by Roger Pau Monné)
- Constify a variable (suggested by Roger Pau Monné)
- Rename 'reclaim' into 'reclaim_memory' (suggested by Roger Pau Monné)
- More wordsmith of the commit message (suggested by Roger Pau Monné)

Changes from v5
(https://lore.kernel.org/linux-block/[email protected]/)
- Wordsmith the commit messages (suggested by Roger Pau Monné)
- Change the reclaim callback return type (suggested by Roger Pau
Monné)
- Change the type of the blkback squeeze duration variable
(suggested by Roger Pau Monné)
- Add a patch for removal of unnecessary static variable name prefixes
(suggested by Roger Pau Monné)
- Fix checkpatch.pl warnings

Changes from v4
(https://lore.kernel.org/xen-devel/[email protected]/)
- Remove domain id parameter from the callback (suggested by Juergen
Gross)
- Rename xen-blkback module parameter (suggested by Stefan Nuernburger)

Changes from v3
(https://lore.kernel.org/xen-devel/[email protected]/)
- Add general callback in xen_driver and use it (suggested by Juergen
Gross)

Changes from v2
(https://lore.kernel.org/linux-block/[email protected])
- Rename the module parameter and variables for brevity
(aggressive shrinking -> squeezing)

Changes from v1
(https://lore.kernel.org/xen-devel/[email protected]/)
- Adjust the description to not use the term, `arbitrarily`
(suggested by Paul Durrant)
- Specify time unit of the duration in the parameter description,
(suggested by Maximilian Heyne)
- Change default aggressive shrinking duration from 1ms to 10ms
- Merge two patches into one single patch

SeongJae Park (5):
xenbus/backend: Add memory pressure handler callback
xenbus/backend: Protect xenbus callback with lock
xen/blkback: Squeeze page pools if a memory pressure is detected
xen/blkback: Remove unnecessary static variable name prefixes
xen/blkback: Consistently insert one empty line between functions

.../ABI/testing/sysfs-driver-xen-blkback | 10 +++++
drivers/block/xen-blkback/blkback.c | 42 +++++++++----------
drivers/block/xen-blkback/common.h | 1 +
drivers/block/xen-blkback/xenbus.c | 26 ++++++++++--
drivers/xen/xenbus/xenbus_probe.c | 8 +++-
drivers/xen/xenbus/xenbus_probe_backend.c | 38 +++++++++++++++++
include/xen/xenbus.h | 2 +
7 files changed, 101 insertions(+), 26 deletions(-)

--
2.17.1


2020-01-27 08:35:39

by SeongJae Park

[permalink] [raw]
Subject: [PATCH v14 2/5] xenbus/backend: Protect xenbus callback with lock

From: SeongJae Park <[email protected]>

A driver's 'reclaim_memory' callback can race with 'probe' or 'remove'
because it will be called whenever memory pressure is detected. To
avoid such race, this commit embeds a spinlock in each 'xenbus_device'
and make 'xenbus' to hold the lock while the corresponded callbacks are
running.

Reviewed-by: Juergen Gross <[email protected]>
Signed-off-by: SeongJae Park <[email protected]>
---
drivers/xen/xenbus/xenbus_probe.c | 8 +++++++-
drivers/xen/xenbus/xenbus_probe_backend.c | 10 ++++++++--
include/xen/xenbus.h | 1 +
3 files changed, 16 insertions(+), 3 deletions(-)

diff --git a/drivers/xen/xenbus/xenbus_probe.c b/drivers/xen/xenbus/xenbus_probe.c
index 378486b79f96..66975da4f3b6 100644
--- a/drivers/xen/xenbus/xenbus_probe.c
+++ b/drivers/xen/xenbus/xenbus_probe.c
@@ -239,7 +239,9 @@ int xenbus_dev_probe(struct device *_dev)
goto fail;
}

+ spin_lock(&dev->reclaim_lock);
err = drv->probe(dev, id);
+ spin_unlock(&dev->reclaim_lock);
if (err)
goto fail_put;

@@ -268,8 +270,11 @@ int xenbus_dev_remove(struct device *_dev)

free_otherend_watch(dev);

- if (drv->remove)
+ if (drv->remove) {
+ spin_lock(&dev->reclaim_lock);
drv->remove(dev);
+ spin_unlock(&dev->reclaim_lock);
+ }

module_put(drv->driver.owner);

@@ -468,6 +473,7 @@ int xenbus_probe_node(struct xen_bus_type *bus,
goto fail;

dev_set_name(&xendev->dev, "%s", devname);
+ spin_lock_init(&xendev->reclaim_lock);

/* Register with generic device framework. */
err = device_register(&xendev->dev);
diff --git a/drivers/xen/xenbus/xenbus_probe_backend.c b/drivers/xen/xenbus/xenbus_probe_backend.c
index 3b5cb7a5a7e4..791f6fe01e91 100644
--- a/drivers/xen/xenbus/xenbus_probe_backend.c
+++ b/drivers/xen/xenbus/xenbus_probe_backend.c
@@ -250,12 +250,18 @@ static int backend_probe_and_watch(struct notifier_block *notifier,
static int backend_reclaim_memory(struct device *dev, void *data)
{
const struct xenbus_driver *drv;
+ struct xenbus_device *xdev;

if (!dev->driver)
return 0;
drv = to_xenbus_driver(dev->driver);
- if (drv && drv->reclaim_memory)
- drv->reclaim_memory(to_xenbus_device(dev));
+ if (drv && drv->reclaim_memory) {
+ xdev = to_xenbus_device(dev);
+ if (!spin_trylock(&xdev->reclaim_lock))
+ return 0;
+ drv->reclaim_memory(xdev);
+ spin_unlock(&xdev->reclaim_lock);
+ }
return 0;
}

diff --git a/include/xen/xenbus.h b/include/xen/xenbus.h
index 980952ea452b..89a889585ba0 100644
--- a/include/xen/xenbus.h
+++ b/include/xen/xenbus.h
@@ -76,6 +76,7 @@ struct xenbus_device {
enum xenbus_state state;
struct completion down;
struct work_struct work;
+ spinlock_t reclaim_lock;
};

static inline struct xenbus_device *to_xenbus_device(struct device *dev)
--
2.17.1

2020-01-27 08:35:49

by SeongJae Park

[permalink] [raw]
Subject: [PATCH v14 3/5] xen/blkback: Squeeze page pools if a memory pressure is detected

From: SeongJae Park <[email protected]>

Each `blkif` has a free pages pool for the grant mapping. The size of
the pool starts from zero and is increased on demand while processing
the I/O requests. If current I/O requests handling is finished or 100
milliseconds has passed since last I/O requests handling, it checks and
shrinks the pool to not exceed the size limit, `max_buffer_pages`.

Therefore, host administrators can cause memory pressure in blkback by
attaching a large number of block devices and inducing I/O. Such
problematic situations can be avoided by limiting the maximum number of
devices that can be attached, but finding the optimal limit is not so
easy. Improper set of the limit can results in memory pressure or a
resource underutilization. This commit avoids such problematic
situations by squeezing the pools (returns every free page in the pool
to the system) for a while (users can set this duration via a module
parameter) if memory pressure is detected.

Discussions
===========

The `blkback`'s original shrinking mechanism returns only pages in the
pool which are not currently be used by `blkback` to the system. In
other words, the pages that are not mapped with granted pages. Because
this commit is changing only the shrink limit but still uses the same
freeing mechanism it does not touch pages which are currently mapping
grants.

Once memory pressure is detected, this commit keeps the squeezing limit
for a user-specified time duration. The duration should be neither too
long nor too short. If it is too long, the squeezing incurring overhead
can reduce the I/O performance. If it is too short, `blkback` will not
free enough pages to reduce the memory pressure. This commit sets the
value as `10 milliseconds` by default because it is a short time in
terms of I/O while it is a long time in terms of memory operations.
Also, as the original shrinking mechanism works for at least every 100
milliseconds, this could be a somewhat reasonable choice. I also tested
other durations (refer to the below section for more details) and
confirmed that 10 milliseconds is the one that works best with the test.
That said, the proper duration depends on actual configurations and
workloads. That's why this commit allows users to set the duration as a
module parameter.

Memory Pressure Test
====================

To show how this commit fixes the memory pressure situation well, I
configured a test environment on a xen-running virtualization system.
On the `blkfront` running guest instances, I attach a large number of
network-backed volume devices and induce I/O to those. Meanwhile, I
measure the number of pages that swapped in (pswpin) and out (pswpout)
on the `blkback` running guest. The test ran twice, once for the
`blkback` before this commit and once for that after this commit. As
shown below, this commit has dramatically reduced the memory pressure:

pswpin pswpout
before 76,672 185,799
after 867 3,967

Optimal Aggressive Shrinking Duration
-------------------------------------

To find a best squeezing duration, I repeated the test with three
different durations (1ms, 10ms, and 100ms). The results are as below:

duration pswpin pswpout
1 707 5,095
10 867 3,967
100 362 3,348

As expected, the memory pressure decreases as the duration increases,
but the reduction become slow from the `10ms`. Based on this results, I
chose the default duration as 10ms.

Performance Overhead Test
=========================

This commit could incur I/O performance degradation under severe memory
pressure because the squeezing will require more page allocations per
I/O. To show the overhead, I artificially made a worst-case squeezing
situation and measured the I/O performance of a `blkfront` running
guest.

For the artificial squeezing, I set the `blkback.max_buffer_pages` using
the `/sys/module/xen_blkback/parameters/max_buffer_pages` file. In this
test, I set the value to `1024` and `0`. The `1024` is the default
value. Setting the value as `0` is same to a situation doing the
squeezing always (worst-case).

If the underlying block device is slow enough, the squeezing overhead
could be hidden. For the reason, I use a fast block device, namely the
rbd[1]:

# xl block-attach guest phy:/dev/ram0 xvdb w

For the I/O performance measurement, I run a simple `dd` command 5 times
directly to the device as below and collect the 'MB/s' results.

$ for i in {1..5}; do dd if=/dev/zero of=/dev/xvdb \
bs=4k count=$((256*512)); sync; done

The results are as below. 'max_pgs' represents the value of the
`blkback.max_buffer_pages` parameter.

max_pgs Min Max Median Avg Stddev
0 417 423 420 419.4 2.5099801
1024 414 425 416 417.8 4.4384682
No difference proven at 95.0% confidence

In short, even worst case squeezing on ramdisk based fast block device
makes no visible performance degradation. Please note that this is just
a very simple and minimal test. On systems using super-fast block
devices and a special I/O workload, the results might be different. If
you have any doubt, test on your machine with your workload to find the
optimal squeezing duration for you.

[1] https://www.kernel.org/doc/html/latest/admin-guide/blockdev/ramdisk.html

Reviewed-by: Roger Pau Monné <[email protected]>
Signed-off-by: SeongJae Park <[email protected]>
---
.../ABI/testing/sysfs-driver-xen-blkback | 10 +++++++++
drivers/block/xen-blkback/blkback.c | 7 +++++--
drivers/block/xen-blkback/common.h | 1 +
drivers/block/xen-blkback/xenbus.c | 21 +++++++++++++++++++
4 files changed, 37 insertions(+), 2 deletions(-)

diff --git a/Documentation/ABI/testing/sysfs-driver-xen-blkback b/Documentation/ABI/testing/sysfs-driver-xen-blkback
index 4e7babb3ba1f..ecb7942ff146 100644
--- a/Documentation/ABI/testing/sysfs-driver-xen-blkback
+++ b/Documentation/ABI/testing/sysfs-driver-xen-blkback
@@ -25,3 +25,13 @@ Description:
allocated without being in use. The time is in
seconds, 0 means indefinitely long.
The default is 60 seconds.
+
+What: /sys/module/xen_blkback/parameters/buffer_squeeze_duration_ms
+Date: December 2019
+KernelVersion: 5.6
+Contact: SeongJae Park <[email protected]>
+Description:
+ When memory pressure is reported to blkback this option
+ controls the duration in milliseconds that blkback will not
+ cache any page not backed by a grant mapping.
+ The default is 10ms.
diff --git a/drivers/block/xen-blkback/blkback.c b/drivers/block/xen-blkback/blkback.c
index 716b99aa2307..5b9ee0494e15 100644
--- a/drivers/block/xen-blkback/blkback.c
+++ b/drivers/block/xen-blkback/blkback.c
@@ -656,8 +656,11 @@ int xen_blkif_schedule(void *arg)
ring->next_lru = jiffies + msecs_to_jiffies(LRU_INTERVAL);
}

- /* Shrink if we have more than xen_blkif_max_buffer_pages */
- shrink_free_pagepool(ring, xen_blkif_max_buffer_pages);
+ /* Shrink the free pages pool if it is too large. */
+ if (time_before(jiffies, blkif->buffer_squeeze_end))
+ shrink_free_pagepool(ring, 0);
+ else
+ shrink_free_pagepool(ring, xen_blkif_max_buffer_pages);

if (log_stats && time_after(jiffies, ring->st_print))
print_stats(ring);
diff --git a/drivers/block/xen-blkback/common.h b/drivers/block/xen-blkback/common.h
index 49132b0adbbe..a3eeccf3ac5f 100644
--- a/drivers/block/xen-blkback/common.h
+++ b/drivers/block/xen-blkback/common.h
@@ -319,6 +319,7 @@ struct xen_blkif {
/* All rings for this device. */
struct xen_blkif_ring *rings;
unsigned int nr_rings;
+ unsigned long buffer_squeeze_end;
};

struct seg_buf {
diff --git a/drivers/block/xen-blkback/xenbus.c b/drivers/block/xen-blkback/xenbus.c
index 4c5d99f87813..55960190b774 100644
--- a/drivers/block/xen-blkback/xenbus.c
+++ b/drivers/block/xen-blkback/xenbus.c
@@ -859,6 +859,26 @@ static void frontend_changed(struct xenbus_device *dev,
}


+/* Once a memory pressure is detected, squeeze free page pools for a while. */
+static unsigned int buffer_squeeze_duration_ms = 10;
+module_param_named(buffer_squeeze_duration_ms,
+ buffer_squeeze_duration_ms, int, 0644);
+MODULE_PARM_DESC(buffer_squeeze_duration_ms,
+"Duration in ms to squeeze pages buffer when a memory pressure is detected");
+
+/*
+ * Callback received when the memory pressure is detected.
+ */
+static void reclaim_memory(struct xenbus_device *dev)
+{
+ struct backend_info *be = dev_get_drvdata(&dev->dev);
+
+ if (!be)
+ return;
+ be->blkif->buffer_squeeze_end = jiffies +
+ msecs_to_jiffies(buffer_squeeze_duration_ms);
+}
+
/* ** Connection ** */


@@ -1152,6 +1172,7 @@ static struct xenbus_driver xen_blkbk_driver = {
.remove = xen_blkbk_remove,
.otherend_changed = frontend_changed,
.allow_rebind = true,
+ .reclaim_memory = reclaim_memory,
};

int xen_blkif_xenbus_init(void)
--
2.17.1

2020-01-30 13:39:25

by Boris Ostrovsky

[permalink] [raw]
Subject: Re: [Xen-devel] [PATCH v14 0/5] xenbus/backend: Add memory pressure handler callback



On 1/27/20 3:18 AM, [email protected] wrote:
> From: SeongJae Park <[email protected]>
>
> Granting pages consumes backend system memory. In systems configured
> with insufficient spare memory for those pages, it can cause a memory
> pressure situation. However, finding the optimal amount of the spare
> memory is challenging for large systems having dynamic resource
> utilization patterns. Also, such a static configuration might lack
> flexibility.
>
> To mitigate such problems, this patchset adds a memory reclaim callback
> to 'xenbus_driver' (patch 1) and then introduce a lock for race
> condition avoidance (patch 2). After that, patch 3 applies the callback
> mechanism to mitigate the problem in 'xen-blkback'. The fourth and
> fifth patches are trivial cleanups; those fix nits we found during the
> development of this patchset.
>
> Note that this patch has only trivial changes from v13. Please refer to
> below patch history for the changes.
>


Applied to for-linus-5.6.

-boris