aboutsummaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
authorChunwei Chen <david.chen@nutanix.com>2019-10-28 09:49:44 -0700
committerBrian Behlendorf <behlendorf1@llnl.gov>2019-10-28 09:49:44 -0700
commit7125a109dcc55628336ff3f58e59e503f4d7694d (patch)
tree1952966258041a0c3f4fc1dead0db1ed32548e0f /lib
parente35704647e84c62c6a6017ead0b66b446010e4ff (diff)
Fix zpool history unbounded memory usage
In original implementation, zpool history will read the whole history before printing anything, causing memory usage goes unbounded. We fix this by breaking it into read-print iterations. Reviewed-by: Tom Caputi <tcaputi@datto.com> Reviewed-by: Matt Ahrens <matt@delphix.com> Reviewed-by: Igor Kozhukhov <igor@dilos.org> Reviewed-by: Brian Behlendorf <behlendorf1@llnl.gov> Signed-off-by: Chunwei Chen <david.chen@nutanix.com> Closes #9516
Diffstat (limited to 'lib')
-rw-r--r--lib/libzfs/libzfs_pool.c20
1 files changed, 11 insertions, 9 deletions
diff --git a/lib/libzfs/libzfs_pool.c b/lib/libzfs/libzfs_pool.c
index 75ce7c9ab..380229da5 100644
--- a/lib/libzfs/libzfs_pool.c
+++ b/lib/libzfs/libzfs_pool.c
@@ -4160,33 +4160,37 @@ get_history(zpool_handle_t *zhp, char *buf, uint64_t *off, uint64_t *len)
* Retrieve the command history of a pool.
*/
int
-zpool_get_history(zpool_handle_t *zhp, nvlist_t **nvhisp)
+zpool_get_history(zpool_handle_t *zhp, nvlist_t **nvhisp, uint64_t *off,
+ boolean_t *eof)
{
char *buf;
int buflen = 128 * 1024;
- uint64_t off = 0;
nvlist_t **records = NULL;
uint_t numrecords = 0;
int err, i;
+ uint64_t start = *off;
buf = malloc(buflen);
if (buf == NULL)
return (ENOMEM);
- do {
+ /* process about 1MB a time */
+ while (*off - start < 1024 * 1024) {
uint64_t bytes_read = buflen;
uint64_t leftover;
- if ((err = get_history(zhp, buf, &off, &bytes_read)) != 0)
+ if ((err = get_history(zhp, buf, off, &bytes_read)) != 0)
break;
/* if nothing else was read in, we're at EOF, just return */
- if (!bytes_read)
+ if (!bytes_read) {
+ *eof = B_TRUE;
break;
+ }
if ((err = zpool_history_unpack(buf, bytes_read,
&leftover, &records, &numrecords)) != 0)
break;
- off -= leftover;
+ *off -= leftover;
if (leftover == bytes_read) {
/*
* no progress made, because buffer is not big enough
@@ -4198,9 +4202,7 @@ zpool_get_history(zpool_handle_t *zhp, nvlist_t **nvhisp)
if (buf == NULL)
return (ENOMEM);
}
-
- /* CONSTCOND */
- } while (1);
+ }
free(buf);