From: Eric Sandeen <sandeen@sandeen.net>
To: Eric Sandeen <sandeen@redhat.com>, linux-xfs <linux-xfs@vger.kernel.org>
Subject: [PATCH 4/5 V2] libxfs: Catch non-empty zones on destroy
Date: Tue, 6 Mar 2018 17:37:06 -0600 [thread overview]
Message-ID: <7fe7fc2a-8ed6-80d7-0727-0093b19f3164@sandeen.net> (raw)
In-Reply-To: <c2dd15a8-a048-6373-2fad-3ffd28a606e0@sandeen.net>
Create and use a kmem_zone_destroy which warns if we are
releasing a non-empty zone when the LIBXFS_LEAK_CHECK
environment variable is set, wire this into libxfs_destroy(),
and call that when various tools exit.
The LIBXFS_LEAK_CHECK environment variable also causes
the program to exit with failure when a leak is detected,
useful for failing automated tests if leaks are encountered.
Signed-off-by: Eric Sandeen <sandeen@redhat.com>
---
v2 fprintf, stderr
include/kmem.h | 1 +
libxfs/init.c | 36 +++++++++++++++++++++++-------------
libxfs/kmem.c | 14 ++++++++++++++
3 files changed, 38 insertions(+), 13 deletions(-)
diff --git a/include/kmem.h b/include/kmem.h
index 65f0ade..572a4fa 100644
--- a/include/kmem.h
+++ b/include/kmem.h
@@ -33,6 +33,7 @@ typedef struct kmem_zone {
extern kmem_zone_t *kmem_zone_init(int, char *);
extern void *kmem_zone_alloc(kmem_zone_t *, int);
extern void *kmem_zone_zalloc(kmem_zone_t *, int);
+extern int kmem_zone_destroy(kmem_zone_t *);
static inline void
kmem_zone_free(kmem_zone_t *zone, void *ptr)
diff --git a/libxfs/init.c b/libxfs/init.c
index 3456cb5..a65c86c 100644
--- a/libxfs/init.c
+++ b/libxfs/init.c
@@ -43,7 +43,7 @@ int libxfs_bhash_size; /* #buckets in bcache */
int use_xfs_buf_lock; /* global flag: use xfs_buf_t locks for MT */
-static void manage_zones(int); /* setup global zones */
+static int manage_zones(int); /* setup/teardown global zones */
/*
* dev_map - map open devices to fd.
@@ -372,7 +372,7 @@ done:
/*
* Initialize/destroy all of the zone allocators we use.
*/
-static void
+static int
manage_zones(int release)
{
extern kmem_zone_t *xfs_buf_zone;
@@ -388,16 +388,20 @@ manage_zones(int release)
extern void xfs_dir_startup();
if (release) { /* free zone allocation */
- kmem_free(xfs_buf_zone);
- kmem_free(xfs_inode_zone);
- kmem_free(xfs_ifork_zone);
- kmem_free(xfs_buf_item_zone);
- kmem_free(xfs_da_state_zone);
- kmem_free(xfs_btree_cur_zone);
- kmem_free(xfs_bmap_free_item_zone);
- kmem_free(xfs_trans_zone);
- kmem_free(xfs_log_item_desc_zone);
- return;
+ int leaked = 0;
+
+ leaked += kmem_zone_destroy(xfs_buf_zone);
+ leaked += kmem_zone_destroy(xfs_ili_zone);
+ leaked += kmem_zone_destroy(xfs_inode_zone);
+ leaked += kmem_zone_destroy(xfs_ifork_zone);
+ leaked += kmem_zone_destroy(xfs_buf_item_zone);
+ leaked += kmem_zone_destroy(xfs_da_state_zone);
+ leaked += kmem_zone_destroy(xfs_btree_cur_zone);
+ leaked += kmem_zone_destroy(xfs_bmap_free_item_zone);
+ leaked += kmem_zone_destroy(xfs_trans_zone);
+ leaked += kmem_zone_destroy(xfs_log_item_desc_zone);
+
+ return leaked;
}
/* otherwise initialise zone allocation */
xfs_buf_zone = kmem_zone_init(sizeof(xfs_buf_t), "xfs_buffer");
@@ -419,6 +423,8 @@ manage_zones(int release)
xfs_log_item_desc_zone = kmem_zone_init(
sizeof(struct xfs_log_item_desc), "xfs_log_item_desc");
xfs_dir_startup();
+
+ return 0;
}
/*
@@ -887,11 +893,15 @@ libxfs_umount(xfs_mount_t *mp)
void
libxfs_destroy(void)
{
+ int leaked;
+
/* Free everything from the buffer cache before freeing buffer zone */
libxfs_bcache_purge();
libxfs_bcache_free();
cache_destroy(libxfs_bcache);
- manage_zones(1);
+ leaked = manage_zones(1);
+ if (getenv("LIBXFS_LEAK_CHECK") && leaked)
+ exit(1);
}
int
diff --git a/libxfs/kmem.c b/libxfs/kmem.c
index c8bcb50..256db94 100644
--- a/libxfs/kmem.c
+++ b/libxfs/kmem.c
@@ -23,6 +23,20 @@ kmem_zone_init(int size, char *name)
return ptr;
}
+int
+kmem_zone_destroy(kmem_zone_t *zone)
+{
+ int leaked = 0;
+
+ if (getenv("LIBXFS_LEAK_CHECK") && zone->allocated) {
+ leaked = 1;
+ fprintf(stderr, "zone %s freed with %d items allocated\n",
+ zone->zone_name, zone->allocated);
+ }
+ free(zone);
+ return leaked;
+}
+
void *
kmem_zone_alloc(kmem_zone_t *zone, int flags)
{
next prev parent reply other threads:[~2018-03-06 23:37 UTC|newest]
Thread overview: 23+ messages / expand[flat|nested] mbox.gz Atom feed top
2018-03-06 21:52 [PATCH 0/5] xfsprogs: my very own patchbomb Eric Sandeen
2018-03-06 21:54 ` [PATCH 1/5] libxfs: Replace XFS_BUF_SET_PTR with xfs_buf_associate_memory Eric Sandeen
2018-03-06 22:57 ` Darrick J. Wong
2018-03-08 8:09 ` Christoph Hellwig
2018-03-06 21:54 ` [PATCH 2/5] libxfs: add function to free all buffers in bcache Eric Sandeen
2018-03-06 23:11 ` Darrick J. Wong
2018-03-08 8:11 ` Christoph Hellwig
2018-03-06 21:55 ` [PATCH 3/5] libxfs: move xfs_inode_zone to rdwr.c Eric Sandeen
2018-03-06 23:01 ` Darrick J. Wong
2018-03-08 8:12 ` Christoph Hellwig
2018-03-06 21:56 ` [PATCH 4/5] libxfs: Catch non-empty zones on destroy Eric Sandeen
2018-03-06 23:06 ` Darrick J. Wong
2018-03-06 23:25 ` Eric Sandeen
2018-03-06 23:37 ` Eric Sandeen [this message]
2018-03-06 23:39 ` [PATCH 4/5 V2] " Darrick J. Wong
2018-03-08 8:13 ` Christoph Hellwig
2018-03-06 21:56 ` [PATCH 5/5] Call libxfs_destroy from other utilities Eric Sandeen
2018-03-06 23:06 ` Darrick J. Wong
2018-03-08 8:13 ` Christoph Hellwig
2018-03-06 23:24 ` [PATCH 6/5] libxfs-apply: add Signed-off-by: Eric Sandeen
2018-03-06 23:32 ` Darrick J. Wong
2018-03-06 23:36 ` Eric Sandeen
2018-03-06 23:44 ` Darrick J. Wong
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=7fe7fc2a-8ed6-80d7-0727-0093b19f3164@sandeen.net \
--to=sandeen@sandeen.net \
--cc=linux-xfs@vger.kernel.org \
--cc=sandeen@redhat.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.