Skip to contentSkip to conversation

selftest: timers: Avoid signal deadlock in leap-a-day

PATCH New John Stultz 2 participants 2 messages Jun 17, 2015 – Jun 18, 2015

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 selftest: timers: Avoid signal deadlock in leap-a-day

In 0c4a5fc95b1df (Add leap-second timer edge testing to leap-a-day.c), we added a timer to the test which checks to make sure timers near the leapsecond edge behave correctly.

However, the output generated from the timer uses ctime_r, which isn't async-signal safe, and should that signal land while the main test is using ctime_r to print its output, its possible for the test to deadlock on glibc internal locks.

Thus this patch reworks the output to avoid using ctime_r in the signal handler.

Cc Prarit Bhargava <[email protected]>
Cc Daniel Bristot de Oliveira <[email protected]>
Cc Richard Cochran <[email protected]>
Cc Jan Kara <[email protected]>
Cc Jiri Bohac <[email protected]>
Cc Ingo Molnar <[email protected]>
Cc Thomas Gleixner <[email protected]>
Cc Shuah Khan <[email protected]>

---
(This applies to tip/timers/core)

 tools/testing/selftests/timers/leap-a-day.c | 23 ++++++++++++-----------
 1 file changed, 12 insertions(+), 11 deletions(-)
tools/testing/selftests/timers/leap-a-day.c +12 -11
index 331c4f7..fb46ad6 100644
@@ -141,27 +141,28 @@ void handler(int unused)
 void sigalarm(int signo)
 {
 	struct timex tx;
-	char buf[26];
 	int ret;
 
 	tx.modes = 0;
 	ret = adjtimex(&tx);
 
-	ctime_r(&tx.time.tv_sec, buf);
-	buf[strlen(buf)-1] = 0; /*remove trailing\n */
-	printf("%s + %6ld us (%i)\t%s - TIMER FIRED\n",
-					buf,
+	if (tx.time.tv_sec < next_leap) {
+		printf("Error: Early timer expiration! (Should be %ld)\n", next_leap);
+		error_found = 1;
+		printf("adjtimex: %10ld sec + %6ld us (%i)\t%s\n",
+					tx.time.tv_sec,
 					tx.time.tv_usec,
 					tx.tai,
 					time_state_str(ret));
-
-	if (tx.time.tv_sec < next_leap) {
-		printf("Error: Early timer expiration!\n");
-		error_found = 1;
 	}
 	if (ret != TIME_WAIT) {
-		printf("Error: Incorrect NTP state?\n");
+		printf("Error: Timer seeing incorrect NTP state? (Should be TIME_WAIT)\n");
 		error_found = 1;
+		printf("adjtimex: %10ld sec + %6ld us (%i)\t%s\n",
+					tx.time.tv_sec,
+					tx.time.tv_usec,
+					tx.tai,
+					time_state_str(ret));
 	}
 }
 
@@ -297,7 +298,7 @@ int main(int argc, char **argv)
 		printf("Scheduling leap second for %s", ctime(&next_leap));
 
 		/* Set up timer */
-		printf("Setting timer for %s", ctime(&next_leap));
+		printf("Setting timer for %ld -  %s", next_leap, ctime(&next_leap));
 		memset(&se, 0, sizeof(se));
 		se.sigev_notify = SIGEV_SIGNAL;
 		se.sigev_signo = signum;
Signature · 1 line
1.9.1
#1 lore raw

[tip:timers/core] selftest: Timers: Avoid signal deadlock in leap-a-day

selftest: Timers: Avoid signal deadlock in leap-a-day
Commit-ID:  51a16c1e887a5975ada27a3ae935a4f2783005da
Gitweb:     http://git.kernel.org/tip/51a16c1e887a5975ada27a3ae935a4f2783005da
Author:     John Stultz <[email protected]>
AuthorDate: Wed, 17 Jun 2015 11:16:43 -0700
Committer:  Thomas Gleixner <[email protected]>
CommitDate: Thu, 18 Jun 2015 15:28:14 +0200

selftest: Timers: Avoid signal deadlock in leap-a-day

In 0c4a5fc95b1df (Add leap-second timer edge testing to leap-a-day.c), we added a timer to the test which checks to make sure timers near the leapsecond edge behave correctly.

However, the output generated from the timer uses ctime_r, which isn't async-signal safe, and should that signal land while the main test is using ctime_r to print its output, its possible for the test to deadlock on glibc internal locks.

Thus this patch reworks the output to avoid using ctime_r in the signal handler.

Cc Prarit Bhargava <[email protected]>
Cc Daniel Bristot de Oliveira <[email protected]>
Cc Richard Cochran <[email protected]>
Cc Jan Kara <[email protected]>
Cc Jiri Bohac <[email protected]>
Cc Shuah Khan <[email protected]>
Cc Ingo Molnar <[email protected]>
---
 tools/testing/selftests/timers/leap-a-day.c | 23 ++++++++++++-----------
 1 file changed, 12 insertions(+), 11 deletions(-)
tools/testing/selftests/timers/leap-a-day.c +12 -11
index 331c4f7..fb46ad6 100644
@@ -141,27 +141,28 @@ void handler(int unused)
 void sigalarm(int signo)
 {
 	struct timex tx;
-	char buf[26];
 	int ret;
 
 	tx.modes = 0;
 	ret = adjtimex(&tx);
 
-	ctime_r(&tx.time.tv_sec, buf);
-	buf[strlen(buf)-1] = 0; /*remove trailing\n */
-	printf("%s + %6ld us (%i)\t%s - TIMER FIRED\n",
-					buf,
+	if (tx.time.tv_sec < next_leap) {
+		printf("Error: Early timer expiration! (Should be %ld)\n", next_leap);
+		error_found = 1;
+		printf("adjtimex: %10ld sec + %6ld us (%i)\t%s\n",
+					tx.time.tv_sec,
 					tx.time.tv_usec,
 					tx.tai,
 					time_state_str(ret));
-
-	if (tx.time.tv_sec < next_leap) {
-		printf("Error: Early timer expiration!\n");
-		error_found = 1;
 	}
 	if (ret != TIME_WAIT) {
-		printf("Error: Incorrect NTP state?\n");
+		printf("Error: Timer seeing incorrect NTP state? (Should be TIME_WAIT)\n");
 		error_found = 1;
+		printf("adjtimex: %10ld sec + %6ld us (%i)\t%s\n",
+					tx.time.tv_sec,
+					tx.time.tv_usec,
+					tx.tai,
+					time_state_str(ret));
 	}
 }
 
@@ -297,7 +298,7 @@ int main(int argc, char **argv)
 		printf("Scheduling leap second for %s", ctime(&next_leap));
 
 		/* Set up timer */
-		printf("Setting timer for %s", ctime(&next_leap));
+		printf("Setting timer for %ld -  %s", next_leap, ctime(&next_leap));
 		memset(&se, 0, sizeof(se));
 		se.sigev_notify = SIGEV_SIGNAL;
 		se.sigev_signo = signum;