[2/2] igt/gem_fd_exhaustion: Modify fs.nr_open for duration of the test

Submitted by Karol Krol on Feb. 27, 2018, 2:30 p.m.

Details

Message ID 20180227143044.10497-2-karol.krol@intel.com
State Accepted
Commit 5d71d7782a830843c7231fbd72ab3edae19b48d7
Headers show
Series "Series without cover letter" ( rev: 1 ) in IGT (deprecated)

Not browsing as part of any series.

Commit Message

Karol Krol Feb. 27, 2018, 2:30 p.m.
We also need to adjust fs.nr_open to allow setting rlimit equal to
fs.file-max. Otherwise we're getting EPREM on setrlimit().

Signed-off-by: Karol Krol <karol.krol@intel.com>
---
 tests/gem_fd_exhaustion.c | 49 ++++++++++++++++++++++++++++++++++++++++-------
 1 file changed, 42 insertions(+), 7 deletions(-)

Patch hide | download patch | download mbox

diff --git a/tests/gem_fd_exhaustion.c b/tests/gem_fd_exhaustion.c
index 6750444e..aa3fe8b3 100644
--- a/tests/gem_fd_exhaustion.c
+++ b/tests/gem_fd_exhaustion.c
@@ -33,17 +33,44 @@ 
 #include <fcntl.h>
 #include <limits.h>
 
+unsigned int original_nr_open;
+
+static int read_sysctl(const char *path)
+{
+	unsigned int val;
+	FILE *f = fopen(path, "r");
+
+	if (f) {
+		igt_assert(fscanf(f, "%u", &val) == 1);
+		fclose(f);
+		return val;
+	}
+	return -errno;
+}
+
+static int write_sysctl(const char *path, unsigned int val)
+{
+	FILE *f = fopen(path, "w");
+
+	if (f) {
+		igt_assert(fprintf(f, "%u", val));
+		fclose(f);
+		return 0;
+	}
+	return -errno;
+}
+
 static bool allow_unlimited_files(void)
 {
+	unsigned int nofile_rlim = 1024*1024;
 	struct rlimit rlim;
-	unsigned nofile_rlim = 1024*1024;
+	unsigned int buf;
 
-	FILE *file = fopen("/proc/sys/fs/file-max", "r");
-	if (file) {
-		igt_assert(fscanf(file, "%u", &nofile_rlim) == 1);
-		igt_info("System limit for open files is %u\n", nofile_rlim);
-		fclose(file);
-	}
+	buf = read_sysctl("/proc/sys/fs/file-max");
+	if (buf > 0)
+		nofile_rlim = buf;
+	original_nr_open = read_sysctl("/proc/sys/fs/nr_open");
+	igt_assert(write_sysctl("/proc/sys/fs/nr_open", nofile_rlim) == 0);
 
 	if (getrlimit(RLIMIT_NOFILE, &rlim))
 		return false;
@@ -53,6 +80,12 @@  static bool allow_unlimited_files(void)
 	return setrlimit(RLIMIT_NOFILE, &rlim) == 0;
 }
 
+static void restore_original_sysctl(void)
+{
+	if (original_nr_open > 0)
+		write_sysctl("/proc/sys/fs/nr_open", original_nr_open);
+}
+
 igt_simple_main
 {
 	int fd;
@@ -61,6 +94,8 @@  igt_simple_main
 
 	fd = drm_open_driver(DRIVER_INTEL);
 
+	igt_install_exit_handler(restore_original_sysctl);
+
 	igt_fork(n, 1) {
 		igt_drop_root();