[PATCH] block: shutdown blktrace in case of task exiting

From: Ming Lei
Date: Mon Mar 22 2021 - 22:32:23 EST


blktrace may allocate lots of memory, if the process is terminated
by user or OOM, we need to provide one chance to remove the trace
buffer, otherwise memory leak may be caused. Also new blktrace
instance can't be started too.

Fix the issue by shutdown blktrace in case of task exiting in
blkdev_close() when it is the last opener.

Signed-off-by: Ming Lei <ming.lei@xxxxxxxxxx>
---
fs/block_dev.c | 6 ++++++
1 file changed, 6 insertions(+)

diff --git a/fs/block_dev.c b/fs/block_dev.c
index 92ed7d5df677..8fa59cecce72 100644
--- a/fs/block_dev.c
+++ b/fs/block_dev.c
@@ -34,6 +34,7 @@
#include <linux/part_stat.h>
#include <linux/uaccess.h>
#include <linux/suspend.h>
+#include <linux/blktrace_api.h>
#include "internal.h"

struct bdev_inode {
@@ -1646,6 +1647,11 @@ EXPORT_SYMBOL(blkdev_put);
static int blkdev_close(struct inode * inode, struct file * filp)
{
struct block_device *bdev = I_BDEV(bdev_file_inode(filp));
+
+ /* shutdown blktrace in case of exiting which may be from OOM */
+ if ((current->flags & PF_EXITING) && (bdev->bd_openers == 1))
+ blk_trace_shutdown(bdev->bd_disk->queue);
+
blkdev_put(bdev, filp->f_mode);
return 0;
}
--
2.29.2


--
Ming