[PATCH v2 blktests] block/034: Test memory is released by null-blk driver with memory_backed=1

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



This tests memory leak, by loading/unloading nullblk driver.
Steps:
	1. Load nullblk driver with memory_backed=1
	2. "dd" of 50M
	3. Unload null-blk driver
We do it for 5 iterations to avoid any noise.

Commit 8cfb98196cceec35416041c6b91212d2b99392e4 fixes issue in kernel

Signed-off-by: Nitesh Shetty <nj.shetty@xxxxxxxxxxx>
---
 tests/block/034     | 60 +++++++++++++++++++++++++++++++++++++++++++++
 tests/block/034.out |  2 ++
 2 files changed, 62 insertions(+)
 create mode 100644 tests/block/034
 create mode 100644 tests/block/034.out

diff --git a/tests/block/034 b/tests/block/034
new file mode 100644
index 0000000..9a3bec4
--- /dev/null
+++ b/tests/block/034
@@ -0,0 +1,60 @@
+#!/bin/bash
+# SPDX-License-Identifier: GPL-3.0+
+# Copyright (C) 2023 Nitesh Shetty
+#
+# Check memory leak when null_blk driver is loaded with memory_backed=1
+
+. tests/block/rc
+. common/null_blk
+
+DESCRIPTION="load/unload null_blk memory_backed=1 to check memleak"
+QUICK=1
+
+requires() {
+	_have_module null_blk
+	_have_module_param null_blk memory_backed
+	_have_program dd
+}
+
+run_nullblk_dd() {
+	if ! _init_null_blk memory_backed=1; then
+		echo "Loading null_blk failed"
+		return 1
+	fi
+	dd if=/dev/urandom of=/dev/nullb0 oflag=direct bs=1M count="$1" > \
+		/dev/null 2>&1
+	_exit_null_blk
+}
+
+free_memory() {
+	sed -n -e 's/^MemFree:[[:blank:]]*\([0-9]*\)[[:blank:]]*kB$/\1/p' \
+		/proc/meminfo
+}
+
+test() {
+	echo "Running ${TEST_NAME}"
+
+	local mem_leak=0 size=50
+	local i mem_start mem_end mem_used
+
+	for ((i = 0; i < 5; i++)); do
+		mem_start=$(free_memory)
+		run_nullblk_dd $size
+		mem_end=$(free_memory)
+
+		mem_used=$(((mem_start - mem_end) / 1024))
+		# -10MB to account for some randomness in freeing by some
+		# simultaneous process
+		if ((mem_used >= size - 10)); then
+			mem_leak=$((mem_leak + 1))
+		fi
+	done
+
+	# There might be possibilty of some random process freeing up memory at
+	# same time nullblk is unloaded.
+	# we consider 3/5 times to be positive.
+	if ((mem_leak > 3)); then
+		echo "Memleak: Memory is not freed by null-blk driver"
+	fi
+	echo "Test complete"
+}
diff --git a/tests/block/034.out b/tests/block/034.out
new file mode 100644
index 0000000..a916dd8
--- /dev/null
+++ b/tests/block/034.out
@@ -0,0 +1,2 @@
+Running block/034
+Test complete
-- 
2.25.1




[Index of Archives]     [Linux RAID]     [Linux SCSI]     [Linux ATA RAID]     [IDE]     [Linux Wireless]     [Linux Kernel]     [ATH6KL]     [Linux Bluetooth]     [Linux Netdev]     [Kernel Newbies]     [Security]     [Git]     [Netfilter]     [Bugtraq]     [Yosemite News]     [MIPS Linux]     [ARM Linux]     [Linux Security]     [Device Mapper]

  Powered by Linux