Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

selftest: rtc: Add to check rtc alarm status for alarm related test

In alarm_wkalm_set and alarm_wkalm_set_minute test, they use different
ioctl (RTC_ALM_SET/RTC_WKALM_SET) for alarm feature detection. They will
skip testing if RTC_ALM_SET/RTC_WKALM_SET ioctl returns an EINVAL error
code. This design may miss detecting real problems when the
efi.set_wakeup_time() return errors and then RTC_ALM_SET/RTC_WKALM_SET
ioctl returns an EINVAL error code with RTC_FEATURE_ALARM enabled.

In order to make rtctest more explicit and robust, we propose to use
RTC_PARAM_GET ioctl interface to check rtc alarm feature state before
running alarm related tests. If the kernel does not support RTC_PARAM_GET
ioctl interface, we will fallback to check the error number of
(RTC_ALM_SET/RTC_WKALM_SET) ioctl call for alarm feature detection.

Requires commit 101ca8d05913b ("rtc: efi: Enable SET/GET WAKEUP services
as optional")

Reviewed-by: Koba Ko <kobak@nvidia.com>
Reviewed-by: Matthew R. Ochs <mochs@nvidia.com>
Signed-off-by: Joseph Jang <jjang@nvidia.com>
Acked-by: Alexandre Belloni <alexandre.belloni@bootlin.com>
Signed-off-by: Shuah Khan <skhan@linuxfoundation.org>

authored by

Joseph Jang and committed by
Shuah Khan
2a027d6b ecfe6870

+65 -1
+1 -1
tools/testing/selftests/rtc/Makefile
··· 1 1 # SPDX-License-Identifier: GPL-2.0 2 - CFLAGS += -O3 -Wl,-no-as-needed -Wall 2 + CFLAGS += -O3 -Wl,-no-as-needed -Wall -I$(top_srcdir)/usr/include 3 3 LDLIBS += -lrt -lpthread -lm 4 4 5 5 TEST_GEN_PROGS = rtctest
+64
tools/testing/selftests/rtc/rtctest.c
··· 25 25 26 26 static char *rtc_file = "/dev/rtc0"; 27 27 28 + enum rtc_alarm_state { 29 + RTC_ALARM_UNKNOWN, 30 + RTC_ALARM_ENABLED, 31 + RTC_ALARM_DISABLED, 32 + }; 33 + 28 34 FIXTURE(rtc) { 29 35 int fd; 30 36 }; ··· 86 80 req.tv_sec = rem.tv_sec; 87 81 req.tv_nsec = rem.tv_nsec; 88 82 } 83 + } 84 + 85 + static enum rtc_alarm_state get_rtc_alarm_state(int fd) 86 + { 87 + struct rtc_param param = { 0 }; 88 + int rc; 89 + 90 + /* Validate kernel reflects unsupported RTC alarm state */ 91 + param.param = RTC_PARAM_FEATURES; 92 + param.index = 0; 93 + rc = ioctl(fd, RTC_PARAM_GET, &param); 94 + if (rc < 0) 95 + return RTC_ALARM_UNKNOWN; 96 + 97 + if ((param.uvalue & _BITUL(RTC_FEATURE_ALARM)) == 0) 98 + return RTC_ALARM_DISABLED; 99 + 100 + return RTC_ALARM_ENABLED; 89 101 } 90 102 91 103 TEST_F_TIMEOUT(rtc, date_read_loop, READ_LOOP_DURATION_SEC + 2) { ··· 221 197 fd_set readfds; 222 198 time_t secs, new; 223 199 int rc; 200 + enum rtc_alarm_state alarm_state = RTC_ALARM_UNKNOWN; 224 201 225 202 if (self->fd == -1 && errno == ENOENT) 226 203 SKIP(return, "Skipping test since %s does not exist", rtc_file); 227 204 ASSERT_NE(-1, self->fd); 205 + 206 + alarm_state = get_rtc_alarm_state(self->fd); 207 + if (alarm_state == RTC_ALARM_DISABLED) 208 + SKIP(return, "Skipping test since alarms are not supported."); 228 209 229 210 rc = ioctl(self->fd, RTC_RD_TIME, &tm); 230 211 ASSERT_NE(-1, rc); ··· 239 210 240 211 rc = ioctl(self->fd, RTC_ALM_SET, &tm); 241 212 if (rc == -1) { 213 + /* 214 + * Report error if rtc alarm was enabled. Fallback to check ioctl 215 + * error number if rtc alarm state is unknown. 216 + */ 217 + ASSERT_EQ(RTC_ALARM_UNKNOWN, alarm_state); 242 218 ASSERT_EQ(EINVAL, errno); 243 219 TH_LOG("skip alarms are not supported."); 244 220 return; ··· 289 255 fd_set readfds; 290 256 time_t secs, new; 291 257 int rc; 258 + enum rtc_alarm_state alarm_state = RTC_ALARM_UNKNOWN; 292 259 293 260 if (self->fd == -1 && errno == ENOENT) 294 261 SKIP(return, "Skipping test since %s does not exist", rtc_file); 295 262 ASSERT_NE(-1, self->fd); 263 + 264 + alarm_state = get_rtc_alarm_state(self->fd); 265 + if (alarm_state == RTC_ALARM_DISABLED) 266 + SKIP(return, "Skipping test since alarms are not supported."); 296 267 297 268 rc = ioctl(self->fd, RTC_RD_TIME, &alarm.time); 298 269 ASSERT_NE(-1, rc); ··· 309 270 310 271 rc = ioctl(self->fd, RTC_WKALM_SET, &alarm); 311 272 if (rc == -1) { 273 + /* 274 + * Report error if rtc alarm was enabled. Fallback to check ioctl 275 + * error number if rtc alarm state is unknown. 276 + */ 277 + ASSERT_EQ(RTC_ALARM_UNKNOWN, alarm_state); 312 278 ASSERT_EQ(EINVAL, errno); 313 279 TH_LOG("skip alarms are not supported."); 314 280 return; ··· 351 307 fd_set readfds; 352 308 time_t secs, new; 353 309 int rc; 310 + enum rtc_alarm_state alarm_state = RTC_ALARM_UNKNOWN; 354 311 355 312 if (self->fd == -1 && errno == ENOENT) 356 313 SKIP(return, "Skipping test since %s does not exist", rtc_file); 357 314 ASSERT_NE(-1, self->fd); 315 + 316 + alarm_state = get_rtc_alarm_state(self->fd); 317 + if (alarm_state == RTC_ALARM_DISABLED) 318 + SKIP(return, "Skipping test since alarms are not supported."); 358 319 359 320 rc = ioctl(self->fd, RTC_RD_TIME, &tm); 360 321 ASSERT_NE(-1, rc); ··· 369 320 370 321 rc = ioctl(self->fd, RTC_ALM_SET, &tm); 371 322 if (rc == -1) { 323 + /* 324 + * Report error if rtc alarm was enabled. Fallback to check ioctl 325 + * error number if rtc alarm state is unknown. 326 + */ 327 + ASSERT_EQ(RTC_ALARM_UNKNOWN, alarm_state); 372 328 ASSERT_EQ(EINVAL, errno); 373 329 TH_LOG("skip alarms are not supported."); 374 330 return; ··· 419 365 fd_set readfds; 420 366 time_t secs, new; 421 367 int rc; 368 + enum rtc_alarm_state alarm_state = RTC_ALARM_UNKNOWN; 422 369 423 370 if (self->fd == -1 && errno == ENOENT) 424 371 SKIP(return, "Skipping test since %s does not exist", rtc_file); 425 372 ASSERT_NE(-1, self->fd); 373 + 374 + alarm_state = get_rtc_alarm_state(self->fd); 375 + if (alarm_state == RTC_ALARM_DISABLED) 376 + SKIP(return, "Skipping test since alarms are not supported."); 426 377 427 378 rc = ioctl(self->fd, RTC_RD_TIME, &alarm.time); 428 379 ASSERT_NE(-1, rc); ··· 439 380 440 381 rc = ioctl(self->fd, RTC_WKALM_SET, &alarm); 441 382 if (rc == -1) { 383 + /* 384 + * Report error if rtc alarm was enabled. Fallback to check ioctl 385 + * error number if rtc alarm state is unknown. 386 + */ 387 + ASSERT_EQ(RTC_ALARM_UNKNOWN, alarm_state); 442 388 ASSERT_EQ(EINVAL, errno); 443 389 TH_LOG("skip alarms are not supported."); 444 390 return;