Files
linux/tools/testing/selftests/timers/nanosleep.c
Linus Torvalds 509d3f4584 Merge tag 'mm-nonmm-stable-2025-12-06-11-14' of git://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm
Pull non-MM updates from Andrew Morton:

 - "panic: sys_info: Refactor and fix a potential issue" (Andy Shevchenko)
   fixes a build issue and does some cleanup in ib/sys_info.c

 - "Implement mul_u64_u64_div_u64_roundup()" (David Laight)
   enhances the 64-bit math code on behalf of a PWM driver and beefs up
   the test module for these library functions

 - "scripts/gdb/symbols: make BPF debug info available to GDB" (Ilya Leoshkevich)
   makes BPF symbol names, sizes, and line numbers available to the GDB
   debugger

 - "Enable hung_task and lockup cases to dump system info on demand" (Feng Tang)
   adds a sysctl which can be used to cause additional info dumping when
   the hung-task and lockup detectors fire

 - "lib/base64: add generic encoder/decoder, migrate users" (Kuan-Wei Chiu)
   adds a general base64 encoder/decoder to lib/ and migrates several
   users away from their private implementations

 - "rbree: inline rb_first() and rb_last()" (Eric Dumazet)
   makes TCP a little faster

 - "liveupdate: Rework KHO for in-kernel users" (Pasha Tatashin)
   reworks the KEXEC Handover interfaces in preparation for Live Update
   Orchestrator (LUO), and possibly for other future clients

 - "kho: simplify state machine and enable dynamic updates" (Pasha Tatashin)
   increases the flexibility of KEXEC Handover. Also preparation for LUO

 - "Live Update Orchestrator" (Pasha Tatashin)
   is a major new feature targeted at cloud environments. Quoting the
   cover letter:

      This series introduces the Live Update Orchestrator, a kernel
      subsystem designed to facilitate live kernel updates using a
      kexec-based reboot. This capability is critical for cloud
      environments, allowing hypervisors to be updated with minimal
      downtime for running virtual machines. LUO achieves this by
      preserving the state of selected resources, such as memory,
      devices and their dependencies, across the kernel transition.

      As a key feature, this series includes support for preserving
      memfd file descriptors, which allows critical in-memory data, such
      as guest RAM or any other large memory region, to be maintained in
      RAM across the kexec reboot.

   Mike Rappaport merits a mention here, for his extensive review and
   testing work.

 - "kexec: reorganize kexec and kdump sysfs" (Sourabh Jain)
   moves the kexec and kdump sysfs entries from /sys/kernel/ to
   /sys/kernel/kexec/ and adds back-compatibility symlinks which can
   hopefully be removed one day

 - "kho: fixes for vmalloc restoration" (Mike Rapoport)
   fixes a BUG which was being hit during KHO restoration of vmalloc()
   regions

* tag 'mm-nonmm-stable-2025-12-06-11-14' of git://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm: (139 commits)
  calibrate: update header inclusion
  Reinstate "resource: avoid unnecessary lookups in find_next_iomem_res()"
  vmcoreinfo: track and log recoverable hardware errors
  kho: fix restoring of contiguous ranges of order-0 pages
  kho: kho_restore_vmalloc: fix initialization of pages array
  MAINTAINERS: TPM DEVICE DRIVER: update the W-tag
  init: replace simple_strtoul with kstrtoul to improve lpj_setup
  KHO: fix boot failure due to kmemleak access to non-PRESENT pages
  Documentation/ABI: new kexec and kdump sysfs interface
  Documentation/ABI: mark old kexec sysfs deprecated
  kexec: move sysfs entries to /sys/kernel/kexec
  test_kho: always print restore status
  kho: free chunks using free_page() instead of kfree()
  selftests/liveupdate: add kexec test for multiple and empty sessions
  selftests/liveupdate: add simple kexec-based selftest for LUO
  selftests/liveupdate: add userspace API selftests
  docs: add documentation for memfd preservation via LUO
  mm: memfd_luo: allow preserving memfd
  liveupdate: luo_file: add private argument to store runtime state
  mm: shmem: export some functions to internal.h
  ...
2025-12-06 14:01:20 -08:00

214 lines
4.9 KiB
C

/* Make sure timers don't return early
* by: john stultz (johnstul@us.ibm.com)
* John Stultz (john.stultz@linaro.org)
* (C) Copyright IBM 2012
* (C) Copyright Linaro 2013 2015
* Licensed under the GPLv2
*
* To build:
* $ gcc nanosleep.c -o nanosleep -lrt
*
* 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.
*/
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <sys/time.h>
#include <sys/timex.h>
#include <string.h>
#include <signal.h>
#include <include/vdso/time64.h>
#include "kselftest.h"
/* CLOCK_HWSPECIFIC == CLOCK_SGI_CYCLE (Deprecated) */
#define CLOCK_HWSPECIFIC 10
#define UNSUPPORTED 0xf00f
char *clockstring(int clockid)
{
switch (clockid) {
case CLOCK_REALTIME:
return "CLOCK_REALTIME";
case CLOCK_MONOTONIC:
return "CLOCK_MONOTONIC";
case CLOCK_PROCESS_CPUTIME_ID:
return "CLOCK_PROCESS_CPUTIME_ID";
case CLOCK_THREAD_CPUTIME_ID:
return "CLOCK_THREAD_CPUTIME_ID";
case CLOCK_MONOTONIC_RAW:
return "CLOCK_MONOTONIC_RAW";
case CLOCK_REALTIME_COARSE:
return "CLOCK_REALTIME_COARSE";
case CLOCK_MONOTONIC_COARSE:
return "CLOCK_MONOTONIC_COARSE";
case CLOCK_BOOTTIME:
return "CLOCK_BOOTTIME";
case CLOCK_REALTIME_ALARM:
return "CLOCK_REALTIME_ALARM";
case CLOCK_BOOTTIME_ALARM:
return "CLOCK_BOOTTIME_ALARM";
case CLOCK_TAI:
return "CLOCK_TAI";
};
return "UNKNOWN_CLOCKID";
}
/* returns 1 if a <= b, 0 otherwise */
static inline int in_order(struct timespec a, struct timespec b)
{
if (a.tv_sec < b.tv_sec)
return 1;
if (a.tv_sec > b.tv_sec)
return 0;
if (a.tv_nsec > b.tv_nsec)
return 0;
return 1;
}
struct timespec timespec_add(struct timespec ts, unsigned long long ns)
{
ts.tv_nsec += ns;
while (ts.tv_nsec >= NSEC_PER_SEC) {
ts.tv_nsec -= NSEC_PER_SEC;
ts.tv_sec++;
}
return ts;
}
int nanosleep_test(int clockid, long long ns)
{
struct timespec now, target, rel;
/* First check abs time */
if (clock_gettime(clockid, &now))
return UNSUPPORTED;
target = timespec_add(now, ns);
if (clock_nanosleep(clockid, TIMER_ABSTIME, &target, NULL))
return UNSUPPORTED;
clock_gettime(clockid, &now);
if (!in_order(target, now))
return -1;
/* Second check reltime */
clock_gettime(clockid, &now);
rel.tv_sec = 0;
rel.tv_nsec = 0;
rel = timespec_add(rel, ns);
target = timespec_add(now, ns);
clock_nanosleep(clockid, 0, &rel, NULL);
clock_gettime(clockid, &now);
if (!in_order(target, now))
return -1;
return 0;
}
static void dummy_event_handler(int val)
{
/* No action needed */
}
static int nanosleep_test_remaining(int clockid)
{
struct timespec rqtp = {}, rmtp = {};
struct itimerspec itimer = {};
struct sigaction sa = {};
timer_t timer;
int ret;
sa.sa_handler = dummy_event_handler;
ret = sigaction(SIGALRM, &sa, NULL);
if (ret)
return -1;
ret = timer_create(clockid, NULL, &timer);
if (ret)
return -1;
itimer.it_value.tv_nsec = NSEC_PER_SEC / 4;
ret = timer_settime(timer, 0, &itimer, NULL);
if (ret)
return -1;
rqtp.tv_nsec = NSEC_PER_SEC / 2;
ret = clock_nanosleep(clockid, 0, &rqtp, &rmtp);
if (ret != EINTR)
return -1;
ret = timer_delete(timer);
if (ret)
return -1;
sa.sa_handler = SIG_DFL;
ret = sigaction(SIGALRM, &sa, NULL);
if (ret)
return -1;
if (!in_order((struct timespec) {}, rmtp))
return -1;
if (!in_order(rmtp, rqtp))
return -1;
return 0;
}
int main(int argc, char **argv)
{
long long length;
int clockid, ret;
int max_clocks = CLOCK_TAI + 1;
ksft_print_header();
ksft_set_plan(max_clocks);
for (clockid = CLOCK_REALTIME; clockid < max_clocks; clockid++) {
/* Skip cputime clockids since nanosleep won't increment cputime */
if (clockid == CLOCK_PROCESS_CPUTIME_ID ||
clockid == CLOCK_THREAD_CPUTIME_ID ||
clockid == CLOCK_HWSPECIFIC) {
ksft_test_result_skip("%-31s\n", clockstring(clockid));
continue;
}
fflush(stdout);
length = 10;
while (length <= (NSEC_PER_SEC * 10)) {
ret = nanosleep_test(clockid, length);
if (ret == UNSUPPORTED) {
ksft_test_result_skip("%-31s\n", clockstring(clockid));
goto next;
}
if (ret < 0) {
ksft_test_result_fail("%-31s\n", clockstring(clockid));
ksft_exit_fail();
}
length *= 100;
}
ret = nanosleep_test_remaining(clockid);
if (ret < 0) {
ksft_test_result_fail("%-31s\n", clockstring(clockid));
ksft_exit_fail();
}
ksft_test_result_pass("%-31s\n", clockstring(clockid));
next:
ret = 0;
}
ksft_exit_pass();
}