All the mail mirrored from lore.kernel.org
 help / color / mirror / Atom feed
* [LTP] [PATCH] futex: regression test for take hugepages into account when generating futex_key
@ 2015-06-16  8:53 Li Wang
  2015-06-16 13:21 ` Cyril Hrubis
  0 siblings, 1 reply; 3+ messages in thread
From: Li Wang @ 2015-06-16  8:53 UTC (permalink / raw)
  To: ltp-list

This issue fixed by:
	commit 13d60f4b6ab5b702dc8d2ee20999f98a93728aec
	Author: Zhang Yi <wetpzy@gmail.com>
	Date:   Tue Jun 25 21:19:31 2013 +0800
	    futex: Take hugepages into account when generating futex_key

Signed-off-by: Li Wang <liwang@redhat.com>
---
 runtest/syscalls                               |   1 +
 testcases/kernel/syscalls/futex/Makefile       |   1 +
 testcases/kernel/syscalls/futex/futex_wake04.c | 182 +++++++++++++++++++++++++
 3 files changed, 184 insertions(+)
 create mode 100644 testcases/kernel/syscalls/futex/futex_wake04.c

diff --git a/runtest/syscalls b/runtest/syscalls
index 07bfc1e..1308536 100644
--- a/runtest/syscalls
+++ b/runtest/syscalls
@@ -1382,5 +1382,6 @@ futex_wait05 futex_wait05
 futex_wake01 futex_wake01
 futex_wake02 futex_wake02
 futex_wake03 futex_wake03
+futex_wake04 futex_wake04
 futex_wait_bitset01 futex_wait_bitset01
 futex_wait_bitset02 futex_wait_bitset02
diff --git a/testcases/kernel/syscalls/futex/Makefile b/testcases/kernel/syscalls/futex/Makefile
index d888734..6e72daf 100644
--- a/testcases/kernel/syscalls/futex/Makefile
+++ b/testcases/kernel/syscalls/futex/Makefile
@@ -22,6 +22,7 @@ futex_wait02: LDLIBS+=-lrt
 futex_wake03: LDLIBS+=-lrt
 futex_wait03: CFLAGS+=-pthread
 futex_wake02: CFLAGS+=-pthread
+futex_wake04: CFLAGS+=-pthread
 futex_wait05: LDLIBS+=-lrt
 futex_wait_bitset01: LDLIBS+=-lrt
 futex_wait_bitset02: LDLIBS+=-lrt
diff --git a/testcases/kernel/syscalls/futex/futex_wake04.c b/testcases/kernel/syscalls/futex/futex_wake04.c
new file mode 100644
index 0000000..b5da4a8
--- /dev/null
+++ b/testcases/kernel/syscalls/futex/futex_wake04.c
@@ -0,0 +1,182 @@
+/*
+ * Copyright (C) 2015  Yi Zhang <wetpzy@gmail.com>
+ *                     Li Wang <liwang@redhat.com>
+ *
+ * Licensed under the GNU GPLv2 or later.
+ * This program is free software;  you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY;  without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
+ * the GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program;  if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+ /* DESCRIPTION:
+ *
+ *   It is a regression test for commit:
+ *   http://git.kernel.org/cgit/linux/kernel/git/torvalds/linux.git/
+ *   commit/?id=13d60f4
+ *
+ *   The implementation of futex doesn't produce unique keys for futexes
+ *   in shared huge pages, so threads waiting on different futexes may
+ *   end up on the same wait list. This results in incorrect threads being
+ *   woken by FUTEX_WAKE.
+ *
+ *   Needs to be run as root unless there are already enough huge pages available.
+ *   In the fail case, which happens in the CentOS-6.6 kernel (2.6.32-504.8.1),
+ *   the tests hangs until it times out after a 30-second wait.
+ *
+ */
+
+#include <stdio.h>
+#include <sys/mman.h>
+#include <fcntl.h>
+#include <pthread.h>
+#include <errno.h>
+#include <sys/time.h>
+#include <string.h>
+
+#include "test.h"
+#include "safe_macros.h"
+#include "futextest.h"
+
+#if __i386__ || __x86_64__
+#define HUGE_SIZE (2 * 1024 * 1024)
+
+#elif __powerpc__ || __powerpc64__
+#define HUGE_SIZE (16 * 1024 * 1024)
+
+#elif __s390__ || __s390x__
+#define HUGE_SIZE (1 * 1024 * 1024)
+
+#else
+#define HUGE_SIZE (2 * 1024 * 1024)
+#endif
+
+#define PATH_NR_HUGEPAGES "/proc/sys/vm/nr_hugepages"
+
+const char *TCID = "futex_wake04";
+const int TST_TOTAL = 1;
+
+static futex_t *futex1, *futex2;
+
+static long th2_wait_time;
+static int th2_wait_done;
+
+static long orig_hugepages;
+
+static void setup(void)
+{
+	tst_require_root(NULL);
+	tst_tmpdir();
+
+	SAFE_FILE_SCANF(NULL, PATH_NR_HUGEPAGES, "%ld", &orig_hugepages);
+	SAFE_FILE_PRINTF(NULL, PATH_NR_HUGEPAGES, "%d", 1);
+
+	TEST_PAUSE;
+}
+
+static void cleanup(void)
+{
+	SAFE_FILE_PRINTF(NULL, PATH_NR_HUGEPAGES, "%ld", orig_hugepages);
+
+	tst_rmdir();
+}
+
+static void *wait_thread1(void *arg LTP_ATTRIBUTE_UNUSED)
+{
+	futex_wait(futex1, *futex1, NULL, 0);
+
+	return NULL;
+}
+
+static void *wait_thread2(void *arg LTP_ATTRIBUTE_UNUSED)
+{
+	struct timeval tv;
+
+	gettimeofday(&tv, NULL);
+	th2_wait_time = tv.tv_sec;
+	futex_wait(futex2, *futex2, NULL, 0);
+
+	th2_wait_done = 1;
+	tst_resm(TPASS, "Hi hydra, thread2 awake!");
+
+	return NULL;
+}
+
+static void wakeup_thread2(void)
+{
+	void *addr;
+	int pgsz, wait_max_time = 30;
+	pthread_t th1, th2;
+	struct timeval tv;
+
+	/*allocate some shared memory*/
+	addr = mmap(NULL,
+			HUGE_SIZE,
+			PROT_READ | PROT_WRITE,
+			MAP_SHARED | MAP_ANONYMOUS | MAP_HUGETLB,
+			-1,
+			0);
+	if (addr == MAP_FAILED) {
+		printf("errno=%d\n", errno);
+		perror("mmap");
+		if (errno == ENOMEM) {
+			tst_brkm(TBROK | TERRNO, NULL,
+				"Probably system didn't actually create any huge pages.");
+		}
+	}
+
+	pgsz = getpagesize();
+
+	/*apply the first subpage to futex1*/
+	futex1 = addr;
+	*futex1 = 0;
+	/*apply the second subpage to futex2*/
+	futex2 = (futex_t *)((char *)addr + pgsz);
+	*futex2 = 0;
+
+	/*thread1 block on futex1 first,then thread2 block on futex2*/
+	pthread_create(&th1, NULL, wait_thread1, NULL);
+	sleep(2);
+	pthread_create(&th2, NULL, wait_thread2, NULL);
+	sleep(2);
+
+	/*try to wake up thread2*/
+	futex_wake(futex2, 1, 0);
+
+	/*see if thread2 can be woke up*/
+	while (!th2_wait_done) {
+		gettimeofday(&tv, NULL);
+		/*thread2 block over 30 secs, test fail*/
+		if (tv.tv_sec > (th2_wait_time + wait_max_time)) {
+			tst_resm(TFAIL,
+				"Bug: wait_thread2 did not wake after %ld secs.",
+				tv.tv_sec - th2_wait_time);
+			break;
+		}
+	}
+
+	munmap(addr, HUGE_SIZE);
+}
+
+int main(int argc, char *argv[])
+{
+	int lc;
+
+	tst_parse_opts(argc, argv, NULL, NULL);
+
+	setup();
+
+	for (lc = 0; TEST_LOOPING(lc); lc++)
+		wakeup_thread2();
+
+	cleanup();
+	tst_exit();
+}
-- 
1.8.3.1


------------------------------------------------------------------------------
_______________________________________________
Ltp-list mailing list
Ltp-list@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/ltp-list

^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [LTP] [PATCH] futex: regression test for take hugepages into account when generating futex_key
  2015-06-16  8:53 [LTP] [PATCH] futex: regression test for take hugepages into account when generating futex_key Li Wang
@ 2015-06-16 13:21 ` Cyril Hrubis
       [not found]   ` <266085602.15945003.1434547937242.JavaMail.zimbra@redhat.com>
  0 siblings, 1 reply; 3+ messages in thread
From: Cyril Hrubis @ 2015-06-16 13:21 UTC (permalink / raw)
  To: Li Wang; +Cc: ltp-list

Hi!
> +#if __i386__ || __x86_64__
> +#define HUGE_SIZE (2 * 1024 * 1024)
> +
> +#elif __powerpc__ || __powerpc64__
> +#define HUGE_SIZE (16 * 1024 * 1024)
> +
> +#elif __s390__ || __s390x__
> +#define HUGE_SIZE (1 * 1024 * 1024)
> +
> +#else
> +#define HUGE_SIZE (2 * 1024 * 1024)
> +#endif

Can we use Hugepagesize from /proc/meminfo instead?

> +#define PATH_NR_HUGEPAGES "/proc/sys/vm/nr_hugepages"
> +
> +const char *TCID = "futex_wake04";
> +const int TST_TOTAL = 1;
> +
> +static futex_t *futex1, *futex2;
> +
> +static long th2_wait_time;
> +static int th2_wait_done;
> +
> +static long orig_hugepages;
> +
> +static void setup(void)
> +{
> +	tst_require_root(NULL);
> +	tst_tmpdir();
> +
> +	SAFE_FILE_SCANF(NULL, PATH_NR_HUGEPAGES, "%ld", &orig_hugepages);
> +	SAFE_FILE_PRINTF(NULL, PATH_NR_HUGEPAGES, "%d", 1);
> +
> +	TEST_PAUSE;
> +}
> +
> +static void cleanup(void)
> +{
> +	SAFE_FILE_PRINTF(NULL, PATH_NR_HUGEPAGES, "%ld", orig_hugepages);
> +
> +	tst_rmdir();
> +}
> +
> +static void *wait_thread1(void *arg LTP_ATTRIBUTE_UNUSED)
> +{
> +	futex_wait(futex1, *futex1, NULL, 0);
> +
> +	return NULL;
> +}
> +
> +static void *wait_thread2(void *arg LTP_ATTRIBUTE_UNUSED)
> +{
> +	struct timeval tv;
> +
> +	gettimeofday(&tv, NULL);
> +	th2_wait_time = tv.tv_sec;
> +	futex_wait(futex2, *futex2, NULL, 0);

Eh, futex_wait can have timeout parameter. Why don't you set the timeout
here and fail the test if the futex timeouted? That would be the
simplest solution.

> +	th2_wait_done = 1;
> +	tst_resm(TPASS, "Hi hydra, thread2 awake!");
> +
> +	return NULL;
> +}
> +
> +static void wakeup_thread2(void)
> +{
> +	void *addr;
> +	int pgsz, wait_max_time = 30;
> +	pthread_t th1, th2;
> +	struct timeval tv;
> +
> +	/*allocate some shared memory*/
> +	addr = mmap(NULL,
> +			HUGE_SIZE,
> +			PROT_READ | PROT_WRITE,
> +			MAP_SHARED | MAP_ANONYMOUS | MAP_HUGETLB,
> +			-1,
> +			0);
> +	if (addr == MAP_FAILED) {
> +		printf("errno=%d\n", errno);
> +		perror("mmap");
> +		if (errno == ENOMEM) {
> +			tst_brkm(TBROK | TERRNO, NULL,
> +				"Probably system didn't actually create any huge pages.");
> +		}
> +	}
> +
> +	pgsz = getpagesize();
> +
> +	/*apply the first subpage to futex1*/
> +	futex1 = addr;
> +	*futex1 = 0;
> +	/*apply the second subpage to futex2*/
> +	futex2 = (futex_t *)((char *)addr + pgsz);
> +	*futex2 = 0;
> +
> +	/*thread1 block on futex1 first,then thread2 block on futex2*/
> +	pthread_create(&th1, NULL, wait_thread1, NULL);
> +	sleep(2);
> +	pthread_create(&th2, NULL, wait_thread2, NULL);
> +	sleep(2);

No sleeps in testcases, you have to use proper synchronization
primitives.

If you need to run the test in several threads have a look at
futex_wake03.c how to assert that threads are sleeping on futex.

If the bug can be reproduced using child processes as well, you can use
TST_PROCESS_STATE_WAIT() to ensure children are sleeping in kernel and
tst_record_childstatus() to propagate the test result from child
process.

> +	/*try to wake up thread2*/
> +	futex_wake(futex2, 1, 0);
> +
> +	/*see if thread2 can be woke up*/
> +	while (!th2_wait_done) {
> +		gettimeofday(&tv, NULL);
> +		/*thread2 block over 30 secs, test fail*/
> +		if (tv.tv_sec > (th2_wait_time + wait_max_time)) {
> +			tst_resm(TFAIL,
> +				"Bug: wait_thread2 did not wake after %ld secs.",
> +				tv.tv_sec - th2_wait_time);
> +			break;
> +		}
> +	}
> +
> +	munmap(addr, HUGE_SIZE);

SAFE_MUNMAP()


You should also wake the second thread here and join both of them,
otherwise with test looping the program will create more and more
threads until it fails.

> +}
> +
> +int main(int argc, char *argv[])
> +{
> +	int lc;
> +
> +	tst_parse_opts(argc, argv, NULL, NULL);
> +
> +	setup();
> +
> +	for (lc = 0; TEST_LOOPING(lc); lc++)
> +		wakeup_thread2();
> +
> +	cleanup();
> +	tst_exit();
> +}
> -- 
> 1.8.3.1
> 
> 
> ------------------------------------------------------------------------------
> _______________________________________________
> Ltp-list mailing list
> Ltp-list@lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/ltp-list

-- 
Cyril Hrubis
chrubis@suse.cz

------------------------------------------------------------------------------
_______________________________________________
Ltp-list mailing list
Ltp-list@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/ltp-list

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [LTP] [PATCH] futex: regression test for take hugepages into account when generating futex_key
       [not found]   ` <266085602.15945003.1434547937242.JavaMail.zimbra@redhat.com>
@ 2015-06-17 13:41     ` Cyril Hrubis
  0 siblings, 0 replies; 3+ messages in thread
From: Cyril Hrubis @ 2015-06-17 13:41 UTC (permalink / raw)
  To: Li Wang; +Cc: ltp-list

Hi!
> proper synchronization primitives ??
> I haven't used that. Could you show me a simple demo? 

That just means that one thread/process waits for some
message/file_change/sleeps in syscall instead of sleeping predefined
time and hoping that the other thread/process has done something
meanwhile.

Which is faster than sleeping blindly and also more error prone.

-- 
Cyril Hrubis
chrubis@suse.cz

------------------------------------------------------------------------------
_______________________________________________
Ltp-list mailing list
Ltp-list@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/ltp-list

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2015-06-17 13:42 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-06-16  8:53 [LTP] [PATCH] futex: regression test for take hugepages into account when generating futex_key Li Wang
2015-06-16 13:21 ` Cyril Hrubis
     [not found]   ` <266085602.15945003.1434547937242.JavaMail.zimbra@redhat.com>
2015-06-17 13:41     ` Cyril Hrubis

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.