From patchwork Sat Jan 25 10:18:46 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: FUJITA Tomonori X-Patchwork-Id: 13950300 Received: from mail-pl1-f180.google.com (mail-pl1-f180.google.com [209.85.214.180]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 2EBFD13C9B3; Sat, 25 Jan 2025 10:20:06 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.180 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1737800408; cv=none; b=Jup+5HGuKrqQxmLXV6va/gyK1Gmakeu0pX628hEYozmw9v0pmryPNV3CG/T8q4dXrJZjuux4cAnMIoIHXh9uw7E8ooUxuoXULW71ETVXntwfZ/gCreYovxFCC6eZ0l2KU5MG8ztG3koVfqKsOpE+umpC6Ya6D/HVmL1styiTNoQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1737800408; c=relaxed/simple; bh=5oz9sIjw/o0ZjqC8hTqxUM3NwZQ1hLqZIqcLUVdkDLc=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=JxwZAevWbARuJwInW1vAwi3GdCKMQGpQmLn9TPNesu8EtCKdaYgbX9mg7ng2Ob5hdmhPZD02/MMbNV+xpjIY6Iavo81aXBWr6rdE/aCMxGdX2iqsopiELVkR3FGXk/nwz/4Jbsah5MFBPC0xDZ5henr8CKlnfBYCKMgPw1A21G0= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=gIyHRDff; arc=none smtp.client-ip=209.85.214.180 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="gIyHRDff" Received: by mail-pl1-f180.google.com with SMTP id d9443c01a7336-216281bc30fso69226655ad.0; Sat, 25 Jan 2025 02:20:06 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1737800406; x=1738405206; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=Ug7H6Tg9hQrupRnAyu5Tv5PLt5bLM7m9t9K7+m86s68=; b=gIyHRDffddh5zD5IxXzA7S9O83fHTODOrZIyhOVCz1q7eKde0OvlBkoVrt8eREWY/t kSw491UpSAR0VREeg562lzkzGVPAv5FouY7tIK/FSOQYpMsLmNh7tVo2rhvdon0Zspg7 Ia53X4Ij8EB8C42LBYo8HQsOoidCy17Da6fNHN3khgbRNCBFkiTeGqFPFeHz6osHNV/E 2QDFp6f+Fd/W6/274JrV3xGU+iRWyfYqYnvi8sfZ0Q17cxPQWeoFyzrp/QbU2og5hcNe QvDrUfXljd9iPlkA16R1ZK3TTwZlKM8+d95sx2dmDVqUbN8KdNHUmQEuauGsXJP0CZvS 187g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1737800406; x=1738405206; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=Ug7H6Tg9hQrupRnAyu5Tv5PLt5bLM7m9t9K7+m86s68=; b=sbGvSr/rnFrhzlC8Rl33Mf+tJGgQUrNaOwZf0NCgPIBp5l3daI4frSbADh7bNvqPmq w52Ys+eadkdIMON+i/BE6N+BOkqsGCRwwE7ARd6dLLaLz2k/Y5oFEHsFyUNS1bpRMzwx B9bnGsekhJ2Onw3sv4XS6Pum92pxvvO7bhuVv6UOzo5nEn4u0P6YZl4O9K/7JzqPMDgV FnrteaiQsphZkCaNEXK7u4VHrpLfqHwyCpy97TjQmA31LsCLn33rgpOIo1a9jkzVPriH wvFNyxZo69fje2ebG7TJ4B+jpfFsgusMucTLuPlMSUdg4SZJ1Sv1leZZhBX/Ye5/UrMk AAcw== X-Forwarded-Encrypted: i=1; AJvYcCXF2MwKIeHZof6KHKw8A3kGCM5MHkTiraGWsPuVNLKuKnlL1uF4PepOg7TqsIGXzfZDxUHWuO0griYpfyd1ByM=@vger.kernel.org, AJvYcCXLvoV7tpaOOhosTcdhP2TEL8IMcP6mTmUO+hbl8c0QL25EoyQrsh28YWy/SZyTcgenVLeaA8c=@vger.kernel.org X-Gm-Message-State: AOJu0YwfuUz/ki9C8vfMUMSw2Uqc0INwmgu9Fd9MhZNjhz66rf81PBdY caGo6sfoU4XqRQFWxe3ps42sbPAUYDbwVoVIBeihf0j4tAvzKM4yZbGlDWLU X-Gm-Gg: ASbGncvQ1xWtplfkQnmI3aPsco+IYbK2NSYPWuawn+wwmqzi6UU0KiV8h5hSUFLF06f sJY+JFj0P8Xe1NIyqhyLnFS15Ix2hDt0o5rBcAVwMV7CJui+WEsoM9Prj/DdmMeBF4PTrD7ev14 MIEcsTjNnwhMsIAvyPgza3QqxRoFmkJHjzIC+j8Kwi2dg0SVef7aBQsB15eo6ts8yBdPpEP/aSU hKqV25rdSens5FHRmk3YmHjougVClC69Bk17miokfltDlyHH/uYRviNRgsseJnvE0hTXC+fcrrO 9yMYJReRPRnyycnfYnbnqTM5eVs9XrOi4XI+vNVYk3wAaY6THEZzQO3r X-Google-Smtp-Source: AGHT+IHCCIlprRg4YscGaB/p+BlLhJxnEKb2Nxo4CInGgZ2U7G89kCOHyV2cTRQSOW/Ag71I2TrLOA== X-Received: by 2002:a17:902:ccc2:b0:216:3e86:1cb9 with SMTP id d9443c01a7336-21c357a608dmr481655415ad.50.1737800406177; Sat, 25 Jan 2025 02:20:06 -0800 (PST) Received: from mew.. (p3882177-ipxg22501hodogaya.kanagawa.ocn.ne.jp. [180.15.148.177]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-21da424f344sm29461155ad.232.2025.01.25.02.19.58 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 25 Jan 2025 02:20:05 -0800 (PST) From: FUJITA Tomonori To: linux-kernel@vger.kernel.org Cc: Boqun Feng , rust-for-linux@vger.kernel.org, netdev@vger.kernel.org, andrew@lunn.ch, hkallweit1@gmail.com, tmgross@umich.edu, ojeda@kernel.org, alex.gaynor@gmail.com, gary@garyguo.net, bjorn3_gh@protonmail.com, benno.lossin@proton.me, a.hindborg@samsung.com, aliceryhl@google.com, anna-maria@linutronix.de, frederic@kernel.org, tglx@linutronix.de, arnd@arndb.de, jstultz@google.com, sboyd@kernel.org, mingo@redhat.com, peterz@infradead.org, juri.lelli@redhat.com, vincent.guittot@linaro.org, dietmar.eggemann@arm.com, rostedt@goodmis.org, bsegall@google.com, mgorman@suse.de, vschneid@redhat.com, tgunders@redhat.com Subject: [PATCH v9 1/8] sched/core: Add __might_sleep_precision() Date: Sat, 25 Jan 2025 19:18:46 +0900 Message-ID: <20250125101854.112261-2-fujita.tomonori@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20250125101854.112261-1-fujita.tomonori@gmail.com> References: <20250125101854.112261-1-fujita.tomonori@gmail.com> Precedence: bulk X-Mailing-List: netdev@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Add __might_sleep_precision(), Rust friendly version of __might_sleep(), which takes a pointer to a string with the length instead of a null-terminated string. Rust's core::panic::Location::file(), which gives the file name of a caller, doesn't provide a null-terminated string. __might_sleep_precision() uses a precision specifier in the printk format, which specifies the length of a string; a string doesn't need to be a null-terminated. Modify __might_sleep() to call __might_sleep_precision() but the impact should be negligible. strlen() isn't called in a normal case; it's called only when printing the error (sleeping function called from invalid context). Note that Location::file() providing a null-terminated string for better C interoperability is under discussion [1]. [1]: https://github.com/rust-lang/libs-team/issues/466 Co-developed-by: Boqun Feng Signed-off-by: Boqun Feng Signed-off-by: FUJITA Tomonori Reviewed-by: Alice Ryhl --- include/linux/kernel.h | 2 ++ kernel/sched/core.c | 55 ++++++++++++++++++++++++++---------------- 2 files changed, 36 insertions(+), 21 deletions(-) diff --git a/include/linux/kernel.h b/include/linux/kernel.h index be2e8c0a187e..086ee1dc447e 100644 --- a/include/linux/kernel.h +++ b/include/linux/kernel.h @@ -87,6 +87,7 @@ extern int dynamic_might_resched(void); #ifdef CONFIG_DEBUG_ATOMIC_SLEEP extern void __might_resched(const char *file, int line, unsigned int offsets); extern void __might_sleep(const char *file, int line); +extern void __might_sleep_precision(const char *file, int len, int line); extern void __cant_sleep(const char *file, int line, int preempt_offset); extern void __cant_migrate(const char *file, int line); @@ -145,6 +146,7 @@ extern void __cant_migrate(const char *file, int line); static inline void __might_resched(const char *file, int line, unsigned int offsets) { } static inline void __might_sleep(const char *file, int line) { } +static inline void __might_sleep_precision(const char *file, int len, int line) { } # define might_sleep() do { might_resched(); } while (0) # define cant_sleep() do { } while (0) # define cant_migrate() do { } while (0) diff --git a/kernel/sched/core.c b/kernel/sched/core.c index 88a9a515b2ba..69d77c14ad33 100644 --- a/kernel/sched/core.c +++ b/kernel/sched/core.c @@ -8666,24 +8666,6 @@ void __init sched_init(void) #ifdef CONFIG_DEBUG_ATOMIC_SLEEP -void __might_sleep(const char *file, int line) -{ - unsigned int state = get_current_state(); - /* - * Blocking primitives will set (and therefore destroy) current->state, - * since we will exit with TASK_RUNNING make sure we enter with it, - * otherwise we will destroy state. - */ - WARN_ONCE(state != TASK_RUNNING && current->task_state_change, - "do not call blocking ops when !TASK_RUNNING; " - "state=%x set at [<%p>] %pS\n", state, - (void *)current->task_state_change, - (void *)current->task_state_change); - - __might_resched(file, line, 0); -} -EXPORT_SYMBOL(__might_sleep); - static void print_preempt_disable_ip(int preempt_offset, unsigned long ip) { if (!IS_ENABLED(CONFIG_DEBUG_PREEMPT)) @@ -8705,7 +8687,8 @@ static inline bool resched_offsets_ok(unsigned int offsets) return nested == offsets; } -void __might_resched(const char *file, int line, unsigned int offsets) +static void __might_resched_precision(const char *file, int len, int line, + unsigned int offsets) { /* Ratelimiting timestamp: */ static unsigned long prev_jiffy; @@ -8728,8 +8711,10 @@ void __might_resched(const char *file, int line, unsigned int offsets) /* Save this before calling printk(), since that will clobber it: */ preempt_disable_ip = get_preempt_disable_ip(current); - pr_err("BUG: sleeping function called from invalid context at %s:%d\n", - file, line); + if (len < 0) + len = strlen(file); + pr_err("BUG: sleeping function called from invalid context at %.*s:%d\n", + len, file, line); pr_err("in_atomic(): %d, irqs_disabled(): %d, non_block: %d, pid: %d, name: %s\n", in_atomic(), irqs_disabled(), current->non_block_count, current->pid, current->comm); @@ -8754,8 +8739,36 @@ void __might_resched(const char *file, int line, unsigned int offsets) dump_stack(); add_taint(TAINT_WARN, LOCKDEP_STILL_OK); } + +void __might_resched(const char *file, int line, unsigned int offsets) +{ + __might_resched_precision(file, -1, line, offsets); +} EXPORT_SYMBOL(__might_resched); +void __might_sleep_precision(const char *file, int len, int line) +{ + unsigned int state = get_current_state(); + /* + * Blocking primitives will set (and therefore destroy) current->state, + * since we will exit with TASK_RUNNING make sure we enter with it, + * otherwise we will destroy state. + */ + WARN_ONCE(state != TASK_RUNNING && current->task_state_change, + "do not call blocking ops when !TASK_RUNNING; " + "state=%x set at [<%p>] %pS\n", state, + (void *)current->task_state_change, + (void *)current->task_state_change); + + __might_resched_precision(file, len, line, 0); +} + +void __might_sleep(const char *file, int line) +{ + __might_sleep_precision(file, -1, line); +} +EXPORT_SYMBOL(__might_sleep); + void __cant_sleep(const char *file, int line, int preempt_offset) { static unsigned long prev_jiffy; From patchwork Sat Jan 25 10:18:47 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: FUJITA Tomonori X-Patchwork-Id: 13950301 Received: from mail-pl1-f170.google.com (mail-pl1-f170.google.com [209.85.214.170]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id B8CD11E22FA; Sat, 25 Jan 2025 10:20:14 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.170 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1737800416; cv=none; b=Js5Y6q+HhDw3DucLOoONR1B7qQzJSqLPN49rK62TXV8S1PTN0BBjnidaqx3vhT0099l09vBLyojWsTICd3pGRmfHb7ozFjcYCC5SXO+zKJAAxt3tKWP9abRbQ0RqROmJiFSjkXgMgAf22VUuNlxYY9MYRuI4qpZmR6v07i7+vPs= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1737800416; c=relaxed/simple; bh=p1cX+47M/YK8hisgffkIVlD9QZQbKffZqY8fbEVUxCE=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=lcnDSnEL1OgASm8cmj6INIkpOSHxRdaj77rfdI3kwONdZM36PelGACKkBIKyP4jhsPo01nA8o66pxVKiSQYNvQFP7dVZ/VEPL+jV3UpDoakVzCzUJbVsLZnqGBvTVzAo1OimPoiyqCjNKfgz9KPZoJtewHBqo2tZZeL0XT+Nwz8= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=dDKGy+au; arc=none smtp.client-ip=209.85.214.170 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="dDKGy+au" Received: by mail-pl1-f170.google.com with SMTP id d9443c01a7336-2164b662090so57357905ad.1; Sat, 25 Jan 2025 02:20:14 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1737800414; x=1738405214; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=/nY2pY2qTfJ1hmO2xyCvTiE0MYO/8Hj3wARJQO2vqCU=; b=dDKGy+auIVotSYb3CMRL2w2WLPgjMaRiOljA+CWTGNpeZvQDLqfTNgwhJQ67mOP7Jh YOBaamJ08e4CzpnC6H8jV7opQsGBqOWiwA0oyJoQ80JfjMGx0O8C9Nf+af7iSBWbG+Zh V/lkN1EllYiIW8IS7IoB+ioYkFsXPuV4kDPPvmjDSQ7WOF2X7n1CHxmlcCXsgaNdFOdn huWsl/WZgkvBG/at9XY5uFwXi5JA9Sl8Tr2Lobz5gb2ppc/9m5Wl3RpBKsF8cpjUbj/H YbuiSvkA6w0J0snmvHp3cEwDt6imfshRitQhvSinwDkwyoi9T2H1Aqaa30phbK/Sdv4e QZKA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1737800414; x=1738405214; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=/nY2pY2qTfJ1hmO2xyCvTiE0MYO/8Hj3wARJQO2vqCU=; b=wAnNit47eZ6adxpZEGvef73USnQ80bXVjEmxp7qmTIKmthx9pfaAkNkpu5M5dyV+Du XbM+nUKdmR41T5sOieYzFXzTkSx+fk9FGGjRHxJcuuv2b6jFiGyvXW1qqR9s3xZBhO3+ IbKhlCH2Pe13x1uaFLhOo6Gz7YDajFgBXv4NlJ5qaoBiXlxqTbx0qkjbCne759hdQoqD lOnGEScpzpMEgBCuQNhrFeIHpi3iJmbLi7eJCdKb0eI+duzmtuGrAYFiD2eoGu6Zh4NY xWxtmaCyJy6A9V3mYndn/NJgieRIptyo1kTQrp6XTAaLbHgmizpLmEIY1DIbNrwvJOCn I+uw== X-Forwarded-Encrypted: i=1; AJvYcCUZWVeIg8hKGwZmlblkvdPqAsKy/bG+EfUtdgKyR+YVUxBpC1nsvfzckgPTM2Q+6Epj0usiOnhftcCazcTKmPI=@vger.kernel.org, AJvYcCW1jGhUTZw7QIgwFfO5U+gLR3FunX+ltB6EGpY+AhXnSJ2oCiLz+yO3jh8jO4VpvssEWXEBzr8=@vger.kernel.org X-Gm-Message-State: AOJu0Yw5G3TbMHtEEBLKVhpvFWTLsJMJQHIDpzJw5YQL8naHUbrG22sp luCcWXhItaiDgooRPuhg4EiuAOI8HbUFiRVqvz9K/Up1j9xNgkZ4rpI0H7V+ X-Gm-Gg: ASbGncshlMTTeatx7pqm7yQlQH80GqtzcVY0K47ZQoCfvWWK1eD20SdkVGIrMVvvWOQ MsGIehqenqz2L8DDRwJCGaalT3UJhtSuuDMENy/O117CJl7RN8GYafM1k197v8mck20Glq7rdw6 dYpafqC18rODGjuqre9Pv8K/vZ/CXPPgDOuFY5bXPfJj2KlMEYbeNhL9kzAuBN5Y4sBoCKWbwOb o66Ntu/eJoOk5h92ZPzCzyyMKOm700vvJsskuMzws35uwIGEwJqId2IFaaa/rUXd6ibmtcZWuR6 1HvcmZkMihIrCONOUQxsJRoWbz2gw8b/mcdBozP85+rMLMvlgpSzPnKo X-Google-Smtp-Source: AGHT+IHQVhGkX6ttjDPwZ8GRNul36L1073PwK3wZnL5SgGDM+14JUptIWnnviS6p3upTwTZ7bV9Cgw== X-Received: by 2002:a17:903:2bcb:b0:215:8847:435c with SMTP id d9443c01a7336-21c353e663cmr515798275ad.12.1737800413726; Sat, 25 Jan 2025 02:20:13 -0800 (PST) Received: from mew.. (p3882177-ipxg22501hodogaya.kanagawa.ocn.ne.jp. [180.15.148.177]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-21da424f344sm29461155ad.232.2025.01.25.02.20.06 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 25 Jan 2025 02:20:13 -0800 (PST) From: FUJITA Tomonori To: linux-kernel@vger.kernel.org Cc: Trevor Gross , Alice Ryhl , Gary Guo , rust-for-linux@vger.kernel.org, netdev@vger.kernel.org, andrew@lunn.ch, hkallweit1@gmail.com, ojeda@kernel.org, alex.gaynor@gmail.com, bjorn3_gh@protonmail.com, benno.lossin@proton.me, a.hindborg@samsung.com, anna-maria@linutronix.de, frederic@kernel.org, tglx@linutronix.de, arnd@arndb.de, jstultz@google.com, sboyd@kernel.org, mingo@redhat.com, peterz@infradead.org, juri.lelli@redhat.com, vincent.guittot@linaro.org, dietmar.eggemann@arm.com, rostedt@goodmis.org, bsegall@google.com, mgorman@suse.de, vschneid@redhat.com, tgunders@redhat.com Subject: [PATCH v9 2/8] rust: time: Add PartialEq/Eq/PartialOrd/Ord trait to Ktime Date: Sat, 25 Jan 2025 19:18:47 +0900 Message-ID: <20250125101854.112261-3-fujita.tomonori@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20250125101854.112261-1-fujita.tomonori@gmail.com> References: <20250125101854.112261-1-fujita.tomonori@gmail.com> Precedence: bulk X-Mailing-List: netdev@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Add PartialEq/Eq/PartialOrd/Ord trait to Ktime so two Ktime instances can be compared to determine whether a timeout is met or not. Use the derive implements; we directly touch C's ktime_t rather than using the C's accessors because it is more efficient and we already do in the existing code (Ktime::sub). Reviewed-by: Trevor Gross Reviewed-by: Alice Ryhl Reviewed-by: Gary Guo Signed-off-by: FUJITA Tomonori --- rust/kernel/time.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rust/kernel/time.rs b/rust/kernel/time.rs index 379c0f5772e5..48b71e6641ce 100644 --- a/rust/kernel/time.rs +++ b/rust/kernel/time.rs @@ -27,7 +27,7 @@ pub fn msecs_to_jiffies(msecs: Msecs) -> Jiffies { /// A Rust wrapper around a `ktime_t`. #[repr(transparent)] -#[derive(Copy, Clone)] +#[derive(Copy, Clone, PartialEq, PartialOrd, Eq, Ord)] pub struct Ktime { inner: bindings::ktime_t, } From patchwork Sat Jan 25 10:18:48 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: FUJITA Tomonori X-Patchwork-Id: 13950302 Received: from mail-pl1-f177.google.com (mail-pl1-f177.google.com [209.85.214.177]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 556ED207DFB; Sat, 25 Jan 2025 10:20:22 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.177 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1737800423; cv=none; b=IAVHxinC+OGRCV6eBkw5JqMQH3c6O97CB/NuhmQiU8FxqYBK49BRVTaB0lg5I8fdMOR5Vebo2KL9qXZYQc+I9FvOx3eM9qomsbjxPc6emnYDhNyeivwgHxoMGPghUVTdCuFhTqWjhKKCHBlxpRKU7iYrjQyE59jenej4aPxHlpI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1737800423; c=relaxed/simple; bh=npkLO44nwRn09hWn3AQ/PzjMZSoIiZHcvuD7B+YVOC8=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=MT9ZFFNyVVS6dD9/xCEVXekwb5NgMlZAij3itfiKMvbALm0OKa3fQ/fq/bd3H7NhWV+dd6j6wKh6XDYMiPJjB5aL2zM4J+I43lTGk93BH+Hxo8uWDzwfqMiWxbMksfY6z3se1UvtRMQZRNINSt1k42EGthPel2dATEIzls7ZeAg= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=Pbnr978/; arc=none smtp.client-ip=209.85.214.177 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="Pbnr978/" Received: by mail-pl1-f177.google.com with SMTP id d9443c01a7336-21a7ed0155cso49813755ad.3; Sat, 25 Jan 2025 02:20:22 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1737800421; x=1738405221; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=Z1QG+0Fo21HAylZ+jKq45RlAJ6ob2hW+fSi9sEos4xU=; b=Pbnr978/6NqzS3wmOmDow2cHlDEsfDowVLzjl6Ei7ixypGgI0mHEEgBC5VlvNpLBpz jFhk862UOGbqMkExahWXwbDZibldIr4CNSftcgIcwzp45YIec0v6NVvO57dUkgot6nL2 ie1nRVzr9sGcaq/b1x8p0iu//tQufXzTzq2RMmZSGRshMkw0MtURG7f7QUJohO+To022 ghQuaNm5/gDR6DZFJEaLjWgdzB+Nl6vG/okFMYpN6YKLzy8jmwBSLtfuJE3SVww8IZTq /S9g3MUjEEw+q24VNQVJhGnCP0tJnEXPOsGOkQ3ZFqMwJTUf3OJ125U9hpapryhSZpbf rwNA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1737800421; x=1738405221; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=Z1QG+0Fo21HAylZ+jKq45RlAJ6ob2hW+fSi9sEos4xU=; b=kBP+qWiy0U2RuXjDmc3LfJ6PVbc4HNWDuwUwaLcPDtWYDjjORJnEePAOEuMJZq5BHr ndaght0oY8roeQy+M9Oe9XgtZc3V9rFX92Rtf4Yh8zlzw51eCTSioQgLpz+w3rNBN28b neGdgfHvX2X4KrheEPXLjRP3n5ru9Fg+OEFlBfYASUgCnQlK2viz6MnWsF4xgfIc3L3i iPRM622RZ23JKhP9x5O6TiYE+gPEsLcruJBJf3x02cQhpTZ5bkkWFrLTMxJJdWd/6BoD as5hRsd7Un2tIWDICgC7+fekQibueiQUM00K5/CGqmN5tzR33grQLkbXVv+A8AEFn05T Q+3w== X-Forwarded-Encrypted: i=1; AJvYcCVFGq0cgBoOX5i9E1pnkwrqbF9w1wRX3xLsK4wxzzcjG0GTWbse0dAaBn+gDBmw7o4JEfJ7wQ+1QHfY+LlDE1g=@vger.kernel.org, AJvYcCVMb8IEiFJETJTioNDpeVjxtfYo4WUnL1iZz2r+VrzIAJHA4ztIAH01NbyTQXdP7nIkQGv1OxU=@vger.kernel.org X-Gm-Message-State: AOJu0Yw8ItAunYjrCdI/5ySnzyUL1kYvCrNprqWOs1hcdvAqFWZLouh9 TMX6docIGMgmVL2K+S1PJmOGBs+wqUEGFpug0XOhCZRqOrLvoWCIcmNgcANo X-Gm-Gg: ASbGncsnqDyAg3MfwqHwbj9zlEx/OKivy9Fo4qjriKARAf/5MFitd/+TmR9ixTkyvbA i4K71AT9q+MBK4KCpZv83PGo7eoA7AdWDrJXEy+ScCKn1a0SHbaEsseLqt+mQz32z6Xkq07WElt v7srbOOY62qY5y26iNo5vSKINforksl+ykrOJNUebXR0Pta+ZtgxOOygYQGBpYv18APc1lkyWLd r9HSgEl5KFNVNI7bYjYjUhnvYu3Xc7GMzsJW1yBYWjQIkcKFY2REPMw16Zvvsn+wLYZres0pQJA Or/KDT3J6lvfO4m7vP7ZTiXPi0vU2bS+o6yFkpqJON/oKSvXzXyaDG3A X-Google-Smtp-Source: AGHT+IGv468mmgGS62YxtkiQFRdIB4WPhA26i87pIVtcrJQloApknPyQDlTxlAkWqdOMuQGlH5+m/w== X-Received: by 2002:a17:902:d48a:b0:216:5cbd:5449 with SMTP id d9443c01a7336-21c35407dcfmr514223625ad.13.1737800421376; Sat, 25 Jan 2025 02:20:21 -0800 (PST) Received: from mew.. (p3882177-ipxg22501hodogaya.kanagawa.ocn.ne.jp. [180.15.148.177]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-21da424f344sm29461155ad.232.2025.01.25.02.20.14 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 25 Jan 2025 02:20:20 -0800 (PST) From: FUJITA Tomonori To: linux-kernel@vger.kernel.org Cc: Andrew Lunn , Alice Ryhl , rust-for-linux@vger.kernel.org, netdev@vger.kernel.org, hkallweit1@gmail.com, tmgross@umich.edu, ojeda@kernel.org, alex.gaynor@gmail.com, gary@garyguo.net, bjorn3_gh@protonmail.com, benno.lossin@proton.me, a.hindborg@samsung.com, anna-maria@linutronix.de, frederic@kernel.org, tglx@linutronix.de, arnd@arndb.de, jstultz@google.com, sboyd@kernel.org, mingo@redhat.com, peterz@infradead.org, juri.lelli@redhat.com, vincent.guittot@linaro.org, dietmar.eggemann@arm.com, rostedt@goodmis.org, bsegall@google.com, mgorman@suse.de, vschneid@redhat.com, tgunders@redhat.com Subject: [PATCH v9 3/8] rust: time: Introduce Delta type Date: Sat, 25 Jan 2025 19:18:48 +0900 Message-ID: <20250125101854.112261-4-fujita.tomonori@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20250125101854.112261-1-fujita.tomonori@gmail.com> References: <20250125101854.112261-1-fujita.tomonori@gmail.com> Precedence: bulk X-Mailing-List: netdev@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Introduce a type representing a span of time. Define our own type because `core::time::Duration` is large and could panic during creation. time::Ktime could be also used for time duration but timestamp and timedelta are different so better to use a new type. i64 is used instead of u64 to represent a span of time; some C drivers uses negative Deltas and i64 is more compatible with Ktime using i64 too (e.g., ktime_[us|ms]_delta() APIs return i64 so we create Delta object without type conversion. i64 is used instead of bindings::ktime_t because when the ktime_t type is used as timestamp, it represents values from 0 to KTIME_MAX, which is different from Delta. as_millis() method isn't used in this patchset. It's planned to be used in Binder driver. Reviewed-by: Andrew Lunn Reviewed-by: Alice Ryhl Signed-off-by: FUJITA Tomonori Reviewed-by: Gary Guo --- rust/kernel/time.rs | 88 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 88 insertions(+) diff --git a/rust/kernel/time.rs b/rust/kernel/time.rs index 48b71e6641ce..622cd01e24d7 100644 --- a/rust/kernel/time.rs +++ b/rust/kernel/time.rs @@ -8,9 +8,15 @@ //! C header: [`include/linux/jiffies.h`](srctree/include/linux/jiffies.h). //! C header: [`include/linux/ktime.h`](srctree/include/linux/ktime.h). +/// The number of nanoseconds per microsecond. +pub const NSEC_PER_USEC: i64 = bindings::NSEC_PER_USEC as i64; + /// The number of nanoseconds per millisecond. pub const NSEC_PER_MSEC: i64 = bindings::NSEC_PER_MSEC as i64; +/// The number of nanoseconds per second. +pub const NSEC_PER_SEC: i64 = bindings::NSEC_PER_SEC as i64; + /// The time unit of Linux kernel. One jiffy equals (1/HZ) second. pub type Jiffies = crate::ffi::c_ulong; @@ -81,3 +87,85 @@ fn sub(self, other: Ktime) -> Ktime { } } } + +/// A span of time. +/// +/// This struct represents a span of time, with its value stored as nanoseconds. +/// The value can represent any valid i64 value, including negative, zero, and +/// positive numbers. +#[derive(Copy, Clone, PartialEq, PartialOrd, Eq, Ord, Debug)] +pub struct Delta { + nanos: i64, +} + +impl Delta { + /// A span of time equal to zero. + pub const ZERO: Self = Self { nanos: 0 }; + + /// Create a new [`Delta`] from a number of microseconds. + /// + /// The `micros` can range from -9_223_372_036_854_775 to 9_223_372_036_854_775. + /// If `micros` is outside this range, `i64::MIN` is used for negative values, + /// and `i64::MAX` is used for positive values due to saturation. + #[inline] + pub const fn from_micros(micros: i64) -> Self { + Self { + nanos: micros.saturating_mul(NSEC_PER_USEC), + } + } + + /// Create a new [`Delta`] from a number of milliseconds. + /// + /// The `millis` can range from -9_223_372_036_854 to 9_223_372_036_854. + /// If `millis` is outside this range, `i64::MIN` is used for negative values, + /// and `i64::MAX` is used for positive values due to saturation. + #[inline] + pub const fn from_millis(millis: i64) -> Self { + Self { + nanos: millis.saturating_mul(NSEC_PER_MSEC), + } + } + + /// Create a new [`Delta`] from a number of seconds. + /// + /// The `secs` can range from -9_223_372_036 to 9_223_372_036. + /// If `secs` is outside this range, `i64::MIN` is used for negative values, + /// and `i64::MAX` is used for positive values due to saturation. + #[inline] + pub const fn from_secs(secs: i64) -> Self { + Self { + nanos: secs.saturating_mul(NSEC_PER_SEC), + } + } + + /// Return `true` if the [`Delta`] spans no time. + #[inline] + pub fn is_zero(self) -> bool { + self.as_nanos() == 0 + } + + /// Return `true` if the [`Delta`] spans a negative amount of time. + #[inline] + pub fn is_negative(self) -> bool { + self.as_nanos() < 0 + } + + /// Return the number of nanoseconds in the [`Delta`]. + #[inline] + pub const fn as_nanos(self) -> i64 { + self.nanos + } + + /// Return the smallest number of microseconds greater than or equal + /// to the value in the [`Delta`]. + #[inline] + pub const fn as_micros_ceil(self) -> i64 { + self.as_nanos().saturating_add(NSEC_PER_USEC - 1) / NSEC_PER_USEC + } + + /// Return the number of milliseconds in the [`Delta`]. + #[inline] + pub const fn as_millis(self) -> i64 { + self.as_nanos() / NSEC_PER_MSEC + } +} From patchwork Sat Jan 25 10:18:49 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: FUJITA Tomonori X-Patchwork-Id: 13950303 Received: from mail-pl1-f170.google.com (mail-pl1-f170.google.com [209.85.214.170]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 072ED207E10; Sat, 25 Jan 2025 10:20:29 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.170 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1737800431; cv=none; b=F8QXECDBiiahiXPBQFoZ18vA+trScf7tN5hRnINrgegWTwpALJwRfupJnQktOrHHwMPyHWl8yTJITgj8REIvmXUI0tIylewaCG835Rpwjzi5LDCG0HmuYCqN1SY9MRFeuTFy9vK+kC74iRBvyLxU+A90cDbA6Zeme/z23akuaeM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1737800431; c=relaxed/simple; bh=HeEtOL3k54zb89/FWTsjNFuw4q/j8Gt5GdjLU3lTzP0=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=D+4sMEhUS5985TELYAVDhijNIw1SzkBwiW+dLLMU202PP7j289OjF949VLYxKN6Qh+JtcYYdN4cber1JXwLGjPWAaBJo1SO8HPHyZR9opODK4VT61AOL5gojsZw2pD8JAAdGfIcVPITgAHOPMTpGEzIqmGwtpyVq+rjXy1VVwbA= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=Okeb/j+v; arc=none smtp.client-ip=209.85.214.170 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="Okeb/j+v" Received: by mail-pl1-f170.google.com with SMTP id d9443c01a7336-2165cb60719so51509375ad.0; Sat, 25 Jan 2025 02:20:29 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1737800429; x=1738405229; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=HYF/+WEcHUxYz6LUGWyjprMe9I73XNctlPRWKUzX670=; b=Okeb/j+vHe+Pnb13jex9hSqHlxbU0yrEo+RVGU98ikqhRMVY7t5jFaHWcVCb+YfbS0 ENyt/I5O4lHreqrgwNlQklBglF0DX4PJxSD/5z5uuTpEH3um4BvJ/tCSXjFeMjO0D1cx 2uNHC6u3zrz6XGaNbaqiutS0W6Pi59HH/UlbBOlGn0ZazZINOaQ8hUDZ61m3zsuCHHAd W/bPtA5VkVmZRB9tz8FUG6w+Qd2Y0YdBIxlBSSKgW3ws4kN/vBKm0gdVuDiLjJloMcm2 mNFLvOXgPysfeoN05A7/RPmOI8+oYyjac2qC8kFqM1uCuKIMeHV0+Jtd3giId6FXo8Q+ MKSg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1737800429; x=1738405229; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=HYF/+WEcHUxYz6LUGWyjprMe9I73XNctlPRWKUzX670=; b=VQxncmdtBLX1ueu5C5WZWLtxfCS4CUP0/xZCvIGTIQgefeLaBojiGo9ie2S1teAx5c +67ktGvqJAaUpjB3UJUGyQ+AcA0EI37BjIALVQQ929+y9FXmBFTT58CCHpHrZrPwMClI KNBe8MMVqGWTXxOg9wC2VjE+J2s1bYcVU090jysXy3/oor9uCLobdK+4to3qaCB8h4+P ndd04y+IASX4OyAuNo72p7VbxQcz8+9AAOi4hoIoOlY3VyWUCrsVsFTYbmMJWwcJDGqY Dks12AlIfG5gDmWU/sm8kkLk4DhaoS/kXkZTkhsaBthVFuN9TbjudLC3qj1E53Xzx/m8 wZsA== X-Forwarded-Encrypted: i=1; AJvYcCW/cEC0j5gIRnozSvyxzavrukdE8GzQxKnC+VPIVd+SQg5v19vShjTqLbOGc79sE/ZoaCkeyDQ=@vger.kernel.org, AJvYcCXfDQMbNxqgnkDFqPCTXAMc88+A6MsxbJ+m+WjU+7OBHtmVjoSHUMAlfTG5HrHTAFCG9iXGdbcabbxzwgoVNDo=@vger.kernel.org X-Gm-Message-State: AOJu0YwB/jDC1LGnKPcyRziSy9GLRsSi2Fw6EudHZcRpFpFSYoe+ayjE KN0TDn1FF68WQSsKzThNEk/nMmtzgQJKypgbYGMs1vH+KwJQymVBZxbBK02b X-Gm-Gg: ASbGncuZc5l4VNpSivUCAMBWgnC/pkh3SDL926OqztxjLrKgqc/kZ7OcuAjZvkAT29w MnXQ55BFsny7FahDwzsRKJr/MldojPrGz+GnkGI69/Kj7DK9s6Z/B352o7iTErfkuL0cAyKiFxs a2/MzYhv/zi/ymzHl2GlbiT6DdELzkwTbZsiXUYoTVPTxT0gKXwSHTyNR3iTCldZFXedU2vo5e5 EOFXIGFmF9kf1LV797m7ypEt0wlqzC7+fP/38qJ4HLjjZHV1bQxJj1TuenLJwS2yiS0e/2f1IKP C38iBvYxWYOPj+tutOq0D9Uyza2y2QEsbwbPNsJHXSCkDExXXi6N+Afz X-Google-Smtp-Source: AGHT+IGzBwoEOlcBY+G0bOmX/uS2sidJTNzJK39POICAA2N1Fqhiu3ASMl2yF0YdgtDQ2XrN91MAmg== X-Received: by 2002:a17:902:db0e:b0:215:522d:72d6 with SMTP id d9443c01a7336-21c355bae02mr538611245ad.38.1737800428799; Sat, 25 Jan 2025 02:20:28 -0800 (PST) Received: from mew.. (p3882177-ipxg22501hodogaya.kanagawa.ocn.ne.jp. [180.15.148.177]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-21da424f344sm29461155ad.232.2025.01.25.02.20.21 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 25 Jan 2025 02:20:28 -0800 (PST) From: FUJITA Tomonori To: linux-kernel@vger.kernel.org Cc: Boqun Feng , rust-for-linux@vger.kernel.org, netdev@vger.kernel.org, andrew@lunn.ch, hkallweit1@gmail.com, tmgross@umich.edu, ojeda@kernel.org, alex.gaynor@gmail.com, gary@garyguo.net, bjorn3_gh@protonmail.com, benno.lossin@proton.me, a.hindborg@samsung.com, aliceryhl@google.com, anna-maria@linutronix.de, frederic@kernel.org, tglx@linutronix.de, arnd@arndb.de, jstultz@google.com, sboyd@kernel.org, mingo@redhat.com, peterz@infradead.org, juri.lelli@redhat.com, vincent.guittot@linaro.org, dietmar.eggemann@arm.com, rostedt@goodmis.org, bsegall@google.com, mgorman@suse.de, vschneid@redhat.com, tgunders@redhat.com Subject: [PATCH v9 4/8] rust: time: Introduce Instant type Date: Sat, 25 Jan 2025 19:18:49 +0900 Message-ID: <20250125101854.112261-5-fujita.tomonori@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20250125101854.112261-1-fujita.tomonori@gmail.com> References: <20250125101854.112261-1-fujita.tomonori@gmail.com> Precedence: bulk X-Mailing-List: netdev@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Introduce a type representing a specific point in time. We could use the Ktime type but C's ktime_t is used for both timestamp and timedelta. To avoid confusion, introduce a new Instant type for timestamp. Rename Ktime to Instant and modify their methods for timestamp. Implement the subtraction operator for Instant: Delta = Instant A - Instant B Reviewed-by: Boqun Feng Signed-off-by: FUJITA Tomonori Reviewed-by: Gary Guo --- rust/kernel/time.rs | 77 +++++++++++++++++++++++---------------------- 1 file changed, 39 insertions(+), 38 deletions(-) diff --git a/rust/kernel/time.rs b/rust/kernel/time.rs index 622cd01e24d7..d64a05a4f4d1 100644 --- a/rust/kernel/time.rs +++ b/rust/kernel/time.rs @@ -5,6 +5,22 @@ //! This module contains the kernel APIs related to time and timers that //! have been ported or wrapped for usage by Rust code in the kernel. //! +//! There are two types in this module: +//! +//! - The [`Instant`] type represents a specific point in time. +//! - The [`Delta`] type represents a span of time. +//! +//! Note that the C side uses `ktime_t` type to represent both. However, timestamp +//! and timedelta are different. To avoid confusion, we use two different types. +//! +//! A [`Instant`] object can be created by calling the [`Instant::now()`] function. +//! It represents a point in time at which the object was created. +//! By calling the [`Instant::elapsed()`] method, a [`Delta`] object representing +//! the elapsed time can be created. The [`Delta`] object can also be created +//! by subtracting two [`Instant`] objects. +//! +//! A [`Delta`] type supports methods to retrieve the duration in various units. +//! //! C header: [`include/linux/jiffies.h`](srctree/include/linux/jiffies.h). //! C header: [`include/linux/ktime.h`](srctree/include/linux/ktime.h). @@ -31,59 +47,44 @@ pub fn msecs_to_jiffies(msecs: Msecs) -> Jiffies { unsafe { bindings::__msecs_to_jiffies(msecs) } } -/// A Rust wrapper around a `ktime_t`. +/// A specific point in time. +/// +/// # Invariants +/// +/// The `inner` value is in the range from 0 to `KTIME_MAX`. #[repr(transparent)] #[derive(Copy, Clone, PartialEq, PartialOrd, Eq, Ord)] -pub struct Ktime { +pub struct Instant { inner: bindings::ktime_t, } -impl Ktime { - /// Create a `Ktime` from a raw `ktime_t`. - #[inline] - pub fn from_raw(inner: bindings::ktime_t) -> Self { - Self { inner } - } - +impl Instant { /// Get the current time using `CLOCK_MONOTONIC`. #[inline] - pub fn ktime_get() -> Self { - // SAFETY: It is always safe to call `ktime_get` outside of NMI context. - Self::from_raw(unsafe { bindings::ktime_get() }) - } - - /// Divide the number of nanoseconds by a compile-time constant. - #[inline] - fn divns_constant(self) -> i64 { - self.to_ns() / DIV - } - - /// Returns the number of nanoseconds. - #[inline] - pub fn to_ns(self) -> i64 { - self.inner + pub fn now() -> Self { + // INVARIANT: The `ktime_get()` function returns a value in the range + // from 0 to `KTIME_MAX`. + Self { + // SAFETY: It is always safe to call `ktime_get()` outside of NMI context. + inner: unsafe { bindings::ktime_get() }, + } } - /// Returns the number of milliseconds. + /// Return the amount of time elapsed since the [`Instant`]. #[inline] - pub fn to_ms(self) -> i64 { - self.divns_constant::() + pub fn elapsed(&self) -> Delta { + Self::now() - *self } } -/// Returns the number of milliseconds between two ktimes. -#[inline] -pub fn ktime_ms_delta(later: Ktime, earlier: Ktime) -> i64 { - (later - earlier).to_ms() -} - -impl core::ops::Sub for Ktime { - type Output = Ktime; +impl core::ops::Sub for Instant { + type Output = Delta; + // By the type invariant, it never overflows. #[inline] - fn sub(self, other: Ktime) -> Ktime { - Self { - inner: self.inner - other.inner, + fn sub(self, other: Instant) -> Delta { + Delta { + nanos: self.inner - other.inner, } } } From patchwork Sat Jan 25 10:18:50 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: FUJITA Tomonori X-Patchwork-Id: 13950304 Received: from mail-pl1-f180.google.com (mail-pl1-f180.google.com [209.85.214.180]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 2752B20767A; Sat, 25 Jan 2025 10:20:36 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.180 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1737800438; cv=none; b=kbX0VjPCrv+3D0Dt1+urA+zvDb1mEkUelwkNO2W+B5rm6ChU4SY1twLHNlJj5/TA1HNWtr5Zz3DIvd6FGnJ+Joxnd+yO/NIBkT4yDi/QGmNJh1WUTenQ/GmGRSrzvrWHpfii9SvzdHkGKnEhzQipsOabw/x6ZwZG6ubY28PKKQY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1737800438; c=relaxed/simple; bh=ReVL26aH6p26mZ26kw6xQ9IoO9iV2Bv4A9ApO1HmIo4=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=FMQMbkYqk7CsqTVurOUncsyr7V4Z+UK7BgwZFvckwEYbP3o/iBpbaN64wMFT9gblwzIYPLECTJY0V6IAdM5phExombDANqWaGlIpvXYQn+2c72pG2MtEGiypoBmyy4SX8BpCkTmLMWh/WQAUx3m2gFJDStJMKqez60zbIGFy5E4= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=VR19NQsc; arc=none smtp.client-ip=209.85.214.180 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="VR19NQsc" Received: by mail-pl1-f180.google.com with SMTP id d9443c01a7336-21c2f1b610dso66426005ad.0; Sat, 25 Jan 2025 02:20:36 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1737800436; x=1738405236; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=zkbKHzExAW0fQ/U6FarCKCDLHrJfi9LS3u9zqTKNql4=; b=VR19NQscUgDSySJLDcxXhxKZ+ud4uHhd1efcQCOY4lBCpD6ZgZXFBcpVWyibPeCKn+ zPBYbJZYXZrv4jrIHLq+UbSIfSeevRgPyIMChKLUbxpQdm//cjhgjqn6HMM9SITYm7ga P1AMpt5W95HndaLH9uYsVPblCW1Tfj5f2YLkYgG4iPHIjdO67jJpXua6WQIJd3HAf5zT Y/5zj1FdSnaDr/+9b+t7L0P3Na58hWBHZNogpt1n46BDkn8Sx/tbp3oPECMtv4I5xtTI TtCtVq+sDhxDSKhUnzbZnSI2bBptuUHaAI0TI4AKtQDLsP4w/zA/Xp3uUew7tjs5EBki PA0A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1737800436; x=1738405236; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=zkbKHzExAW0fQ/U6FarCKCDLHrJfi9LS3u9zqTKNql4=; b=W+N2+tzsJeuY3j0J8DgmkYLpSgB6OCzV8RhBlvZ45j0+fPU0QoYU68V9sdLM7GssJC v0M87kjh/kKeL7frwBdvNpxOkxO2eqsFrW+U9YxG61nrFXmO5xc2d72T62DOTwzluRpa VtCbUK3x7h2GCRkQtXvc5Mc2IyC8WRjAn7tT239yQXL3VAP1oyAeK8Wj9JkVwiZSoSad 0kThvcFAPXYAkCmY9mQiy/EY7Pe68YVZRRB5OrRCIfl9vzn8UMI/p3wO56CZtSgA1rEF qAR3UAaX5QfEs8VtwcBxRS3sPo8DO1GKLDDHI3Qd2vpssJBRghujZH1Zn600hqUcFcQT vgHw== X-Forwarded-Encrypted: i=1; AJvYcCX5Ltfxho6wUwHVZPJdtuJRn9E43bMCWP9BABa6fXguIblfG/liVJ+mbS4wmZrX26B7LmQycwQ=@vger.kernel.org X-Gm-Message-State: AOJu0Yy9rOa/32VdxqhfBHzThLc650JrXCaFjotPdkoP+G2DNjpFIQI+ BqSYeHklgC3E6/9d/b7M1oqJOY/57hR09CGWikW6UxVPzTW0UN8qVSp8ChUg X-Gm-Gg: ASbGncvGL8V1uGUVCx2cAqyre6daH+/KWIVcIwf8ZwFB8FBbO/AvO1wdh3a3IUh8Ult JbnBvfOD5CBdT59R437aISArV29ACCdocRjnsXYmSkdpnMm99UJa/YqMsJU02wUDOkLuPPge3/0 eMJ6l25h8vg2tOWtmzv/K2o5oNyhDmVVbmLC54IG9mSeCQYBy6W3rhP4JvLu6GWc1rKPESK8MXH 5wwhLzsQTjqHkiQTgx0Q7LZUJpT8jMjP5ycAadwRd7lXJrrtHntZ+sc5bZx5Pc2DIOYddjmEBSZ MQVntprOxWRwxd9nByynccXnAwaHnG4XbEcgHIlgoa3F0IP7jrvDphmw5bS6GVew9MQ= X-Google-Smtp-Source: AGHT+IEL+mIPaebwamtGa27uOdF/FkrLTiTyKWZitH97Ao290Y78qu2TxCJGyzVQKtvEHrrkvpoxmg== X-Received: by 2002:a17:902:ea10:b0:216:4e9f:4ec9 with SMTP id d9443c01a7336-21c355c2906mr536597425ad.38.1737800436067; Sat, 25 Jan 2025 02:20:36 -0800 (PST) Received: from mew.. (p3882177-ipxg22501hodogaya.kanagawa.ocn.ne.jp. [180.15.148.177]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-21da424f344sm29461155ad.232.2025.01.25.02.20.29 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 25 Jan 2025 02:20:35 -0800 (PST) From: FUJITA Tomonori To: linux-kernel@vger.kernel.org Cc: rust-for-linux@vger.kernel.org, netdev@vger.kernel.org, andrew@lunn.ch, hkallweit1@gmail.com, tmgross@umich.edu, ojeda@kernel.org, alex.gaynor@gmail.com, gary@garyguo.net, bjorn3_gh@protonmail.com, benno.lossin@proton.me, a.hindborg@samsung.com, aliceryhl@google.com, anna-maria@linutronix.de, frederic@kernel.org, tglx@linutronix.de, arnd@arndb.de, jstultz@google.com, sboyd@kernel.org, mingo@redhat.com, peterz@infradead.org, juri.lelli@redhat.com, vincent.guittot@linaro.org, dietmar.eggemann@arm.com, rostedt@goodmis.org, bsegall@google.com, mgorman@suse.de, vschneid@redhat.com, tgunders@redhat.com Subject: [PATCH v9 5/8] rust: time: Add wrapper for fsleep() function Date: Sat, 25 Jan 2025 19:18:50 +0900 Message-ID: <20250125101854.112261-6-fujita.tomonori@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20250125101854.112261-1-fujita.tomonori@gmail.com> References: <20250125101854.112261-1-fujita.tomonori@gmail.com> Precedence: bulk X-Mailing-List: netdev@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Add a wrapper for fsleep(), flexible sleep functions in include/linux/delay.h which typically deals with hardware delays. The kernel supports several sleep functions to handle various lengths of delay. This adds fsleep(), automatically chooses the best sleep method based on a duration. sleep functions including fsleep() belongs to TIMERS, not TIMEKEEPING. They are maintained separately. rust/kernel/time.rs is an abstraction for TIMEKEEPING. To make Rust abstractions match the C side, add rust/kernel/time/delay.rs for this wrapper. fsleep() can only be used in a nonatomic context. This requirement is not checked by these abstractions, but it is intended that klint [1] or a similar tool will be used to check it in the future. Link: https://rust-for-linux.com/klint [1] Signed-off-by: FUJITA Tomonori Reviewed-by: Gary Guo Reviewed-by: Alice Ryhl --- rust/helpers/helpers.c | 1 + rust/helpers/time.c | 8 +++++++ rust/kernel/time.rs | 2 ++ rust/kernel/time/delay.rs | 49 +++++++++++++++++++++++++++++++++++++++ 4 files changed, 60 insertions(+) create mode 100644 rust/helpers/time.c create mode 100644 rust/kernel/time/delay.rs diff --git a/rust/helpers/helpers.c b/rust/helpers/helpers.c index dcf827a61b52..d16aeda7a558 100644 --- a/rust/helpers/helpers.c +++ b/rust/helpers/helpers.c @@ -26,6 +26,7 @@ #include "slab.c" #include "spinlock.c" #include "task.c" +#include "time.c" #include "uaccess.c" #include "vmalloc.c" #include "wait.c" diff --git a/rust/helpers/time.c b/rust/helpers/time.c new file mode 100644 index 000000000000..7ae64ad8141d --- /dev/null +++ b/rust/helpers/time.c @@ -0,0 +1,8 @@ +// SPDX-License-Identifier: GPL-2.0 + +#include + +void rust_helper_fsleep(unsigned long usecs) +{ + fsleep(usecs); +} diff --git a/rust/kernel/time.rs b/rust/kernel/time.rs index d64a05a4f4d1..eeb0f6a7e5d4 100644 --- a/rust/kernel/time.rs +++ b/rust/kernel/time.rs @@ -24,6 +24,8 @@ //! C header: [`include/linux/jiffies.h`](srctree/include/linux/jiffies.h). //! C header: [`include/linux/ktime.h`](srctree/include/linux/ktime.h). +pub mod delay; + /// The number of nanoseconds per microsecond. pub const NSEC_PER_USEC: i64 = bindings::NSEC_PER_USEC as i64; diff --git a/rust/kernel/time/delay.rs b/rust/kernel/time/delay.rs new file mode 100644 index 000000000000..02b8731433c7 --- /dev/null +++ b/rust/kernel/time/delay.rs @@ -0,0 +1,49 @@ +// SPDX-License-Identifier: GPL-2.0 + +//! Delay and sleep primitives. +//! +//! This module contains the kernel APIs related to delay and sleep that +//! have been ported or wrapped for usage by Rust code in the kernel. +//! +//! C header: [`include/linux/delay.h`](srctree/include/linux/delay.h). + +use super::Delta; +use crate::ffi::c_ulong; + +/// Sleeps for a given duration at least. +/// +/// Equivalent to the C side [`fsleep()`], flexible sleep function, +/// which automatically chooses the best sleep method based on a duration. +/// +/// `delta` must be within `[0, i32::MAX]` microseconds; +/// otherwise, it is erroneous behavior. That is, it is considered a bug +/// to call this function with an out-of-range value, in which case the function +/// will sleep for at least the maximum value in the range and may warn +/// in the future. +/// +/// The behavior above differs from the C side [`fsleep()`] for which out-of-range +/// values mean "infinite timeout" instead. +/// +/// This function can only be used in a nonatomic context. +/// +/// [`fsleep`]: https://docs.kernel.org/timers/delay_sleep_functions.html#c.fsleep +pub fn fsleep(delta: Delta) { + // The maximum value is set to `i32::MAX` microseconds to prevent integer + // overflow inside fsleep, which could lead to unintentional infinite sleep. + const MAX_DELTA: Delta = Delta::from_micros(i32::MAX as i64); + + let delta = if (Delta::ZERO..=MAX_DELTA).contains(&delta) { + delta + } else { + // TODO: Add WARN_ONCE() when it's supported. + MAX_DELTA + }; + + // SAFETY: It is always safe to call `fsleep()` with any duration. + unsafe { + // Convert the duration to microseconds and round up to preserve + // the guarantee; `fsleep()` sleeps for at least the provided duration, + // but that it may sleep for longer under some circumstances. + bindings::fsleep(delta.as_micros_ceil() as c_ulong) + } +} From patchwork Sat Jan 25 10:18:51 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: FUJITA Tomonori X-Patchwork-Id: 13950305 Received: from mail-pl1-f171.google.com (mail-pl1-f171.google.com [209.85.214.171]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 430DD20767A; Sat, 25 Jan 2025 10:20:44 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.171 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1737800445; cv=none; b=HkEUk0vKJcGASr4ppkQj1UqJFzvvFEoY+XvDbTP074sC32zBImMJEHKqxI6W7u4wanPgIi5xyJCaB+mIISZSBUY5r/NCKub4DX/8w2lByyJrvhfgGuvjU/xW4C8+MhpLZb7YBjKkcpSlRTldP8CUCRWl++CUTR84oER4orl7wnw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1737800445; c=relaxed/simple; bh=k+obMTJKWj1D4d5SWKV3Hm0ZQWUwCvZYP7hKNH2kr2A=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=uv5DvaZKzsJ2145i+ChNHztIAyMETI7eMd3cJoFrsJZN8E7hhCK7KCatrgocq2Mk7Llo1pqgghWJpk1BWWedmIBgGuZzAbIMX2MsHD1J6T1WPwxJEaelaeBy4YHoJSr2/2jSG8lOGhsyiPe+Ni9dVf7/um4Mb0D0qpYhNe9FRjc= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=RYAlhzo5; arc=none smtp.client-ip=209.85.214.171 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="RYAlhzo5" Received: by mail-pl1-f171.google.com with SMTP id d9443c01a7336-2166022c5caso46590405ad.2; Sat, 25 Jan 2025 02:20:44 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1737800443; x=1738405243; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=kQVkyYAu0pwAzQ7e0HkaEcvOZiijzMM+hzluoVt+E5A=; b=RYAlhzo5SxnxWJ63bQfg52VBXLMPWx1733Nuh2eUUas0K/P3CdArcWV5gnprbDPoVU s7z6k0L9znyeNR1rCnb7K4JIVEESSZ3/6pSW1qci6a/RzKGzdgljMQQ6ohnsb8KKKP72 wsfN8nINXm8IAHDHjWfUjtH7zeg0cbKMFhocSnUNX10WXq7XdilyfL+Fzhp0fcUsSXSl LZIPu/fcbJ+zgP0v4qcuNa0w6/xiAsCTrLTbqh1sy3PtvWQSFiVJhTPugNYJwt15s5Cf 7HyyU8mSvVxasoY+clxgT0IoRUMMrsTksQFhlEU1U3XY9JtwO9CyjpKUVsuZn+LBNjDc jZ6w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1737800443; x=1738405243; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=kQVkyYAu0pwAzQ7e0HkaEcvOZiijzMM+hzluoVt+E5A=; b=wVcswSN0PpUZDvPuldq5yByy4LLvlwYPbw6vMOiuyX/Ulg2TgFNlJZTwYKZ0fSGYLC 3RmeHEp8bCZhP/LjZ0Lx5IRbMb6KibtoRDd1EjBd5g6DrNdQ/cpECBl4SUGVovzKpscE SVUwsc8qGAcGx/a1+rzQAxil5SRI0ZDpX8roql653Lw41Th3LgK7jLXqA9bDpqwcFYw3 RjiMdWKDGk4slHVSlFoiUqGXHBA2CJJv+n8B/I5cyGOKaGwyp+5V3o2rXUCx6mEn3YvR X7PHMpjCfGMfunrXa/8FXXDkmscq0UEISXbn/rot3mKRywVdHFi95KMwOFGgeHDwtngp RZeA== X-Forwarded-Encrypted: i=1; AJvYcCU3F2DB8apE5tQmAewdSIJKatlDN7c8F/2KUFrWNYco7x+ycU/n5Uls7Sx7HklFkpfGXoaDeI0=@vger.kernel.org X-Gm-Message-State: AOJu0YxSF0CoPuTqMNpZQBDYo0jh5ieu/iY4dzlO/nEtGBbKWfne6QBt MKg6t1JyydQdp/flWtDhz8oYovbqpzFooOiHk1e8La6R+a8eFSNmVABttR0t X-Gm-Gg: ASbGncsrkDTKnUtDy5XQHdCmE9DvF3dl98KXLgKsB/SiyWhlYYmeI1RXN7JZa0OVPrN DnvmcNXSpC3KV0ZcuXvy44iicMMXsIshyEuauPVKdQ9xbiqos2RP393JAuxHzZDno1Vi7nv9+/L m0YaoPxtPskfJcWgvt+NWuB0s9z5J/z4LU50tnvY4FcfNBD6/J2EzeiGn/f6Um++WEveHK84ph4 wKSdUhG89tYjrlxjbTXbfxDAVm58krnUBtIMmORXf6xJMSxQsYkPAMcIZZArCEU/SYwo/9nqlv5 /PtAQzxX9u/hk5sK8LiQNvji4zovctDKGlhcZ1vwmETeIAGUdTW9c9Fj X-Google-Smtp-Source: AGHT+IEi4aWAoxQaDbZWMe/Aw+49i/+LZnLUNflot7Bqnw+ULcXmCPJc+z7CHV3N/pRdsKlX6OunEA== X-Received: by 2002:a17:902:f689:b0:215:acb3:3786 with SMTP id d9443c01a7336-21c35504c1cmr509533115ad.19.1737800443339; Sat, 25 Jan 2025 02:20:43 -0800 (PST) Received: from mew.. (p3882177-ipxg22501hodogaya.kanagawa.ocn.ne.jp. [180.15.148.177]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-21da424f344sm29461155ad.232.2025.01.25.02.20.36 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 25 Jan 2025 02:20:43 -0800 (PST) From: FUJITA Tomonori To: linux-kernel@vger.kernel.org Cc: rust-for-linux@vger.kernel.org, netdev@vger.kernel.org, andrew@lunn.ch, hkallweit1@gmail.com, tmgross@umich.edu, ojeda@kernel.org, alex.gaynor@gmail.com, gary@garyguo.net, bjorn3_gh@protonmail.com, benno.lossin@proton.me, a.hindborg@samsung.com, aliceryhl@google.com, anna-maria@linutronix.de, frederic@kernel.org, tglx@linutronix.de, arnd@arndb.de, jstultz@google.com, sboyd@kernel.org, mingo@redhat.com, peterz@infradead.org, juri.lelli@redhat.com, vincent.guittot@linaro.org, dietmar.eggemann@arm.com, rostedt@goodmis.org, bsegall@google.com, mgorman@suse.de, vschneid@redhat.com, tgunders@redhat.com Subject: [PATCH v9 6/8] MAINTAINERS: rust: Add TIMEKEEPING and TIMER abstractions Date: Sat, 25 Jan 2025 19:18:51 +0900 Message-ID: <20250125101854.112261-7-fujita.tomonori@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20250125101854.112261-1-fujita.tomonori@gmail.com> References: <20250125101854.112261-1-fujita.tomonori@gmail.com> Precedence: bulk X-Mailing-List: netdev@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Add Rust TIMEKEEPING and TIMER abstractions to the maintainers entry respectively. Signed-off-by: FUJITA Tomonori --- MAINTAINERS | 2 ++ 1 file changed, 2 insertions(+) diff --git a/MAINTAINERS b/MAINTAINERS index 8e047e20fbd8..faa4081e9e76 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -10307,6 +10307,7 @@ F: kernel/time/sleep_timeout.c F: kernel/time/timer.c F: kernel/time/timer_list.c F: kernel/time/timer_migration.* +F: rust/kernel/time/delay.rs F: tools/testing/selftests/timers/ HIGH-SPEED SCC DRIVER FOR AX.25 @@ -23741,6 +23742,7 @@ F: kernel/time/timeconv.c F: kernel/time/timecounter.c F: kernel/time/timekeeping* F: kernel/time/time_test.c +F: rust/kernel/time.rs F: tools/testing/selftests/timers/ TIPC NETWORK LAYER From patchwork Sat Jan 25 10:18:52 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: FUJITA Tomonori X-Patchwork-Id: 13950306 Received: from mail-pl1-f177.google.com (mail-pl1-f177.google.com [209.85.214.177]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 93E4920A5C2; Sat, 25 Jan 2025 10:20:51 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.177 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1737800453; cv=none; b=MOni8Rw8LK4WCq77P/kqSOh0ebhLVM2dnT+dcUHLHYG7IwasiyGlg7TZUHzG+z+sNL49g01XRz2+6GMQRSJPTplJgDiFn0bkes1LjZ6ZF1KqBA9rFn+vwiEtbQDSjBVxvuDHxTkYEcbAMl9N7iwcFAcrFYGAyw8MOB8JLFmFMkE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1737800453; c=relaxed/simple; bh=I0HU++TiIi/bjwfayKjc8D6SR4E6d+5vz6vuwMPJ3I8=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=aUFRgYZHgrdWC21QR2YogZkkFkyP3wlEB0D9tLRWr3gEMUChFAeiNAl5WOZvNfE4EbkeMEoiURbIDVIi/yEFhWRDTgcJ8ov9fdi9OOrS8i59o5EC5/5/L8GP39jtCir1KDAxRVthU+ImByOby6Bifdj526GmTq3S1aXeSJDSZnA= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=FCwdKmO3; arc=none smtp.client-ip=209.85.214.177 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="FCwdKmO3" Received: by mail-pl1-f177.google.com with SMTP id d9443c01a7336-219f8263ae0so54155465ad.0; Sat, 25 Jan 2025 02:20:51 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1737800451; x=1738405251; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=M7PYttLtBbsdK/gZ6kIbMnSJ3NKWcgkxmnzIQ8mz2TQ=; b=FCwdKmO37oSDg5XkuBlQLwuMbhHk9ONoEVPtshYzPoqoc+fzaQYaPo0woFjYvBRD2Y itx7H1KQTRgV9/m6L8V4RMoG+rbCaGIkVjvly9zqRkgXv1zeQAZgITse+j5Js6hPW2J2 mRDAGds9Ywn64wlQoK36SyaJdM+LKIrfqvuc9jm61/JrM6pfwd2jDTvzH07aTThtroSY C+WusOHfNJ4yQMy77EL91LGvkVaz+9r4fWH+kXDj1OXgtloZ/nQrQjX6cbhz9Urhyhl+ QVeWblniPvIms+iSukbkzUx0cSqfw1CH23+pkBY54UIxmBXMQBwL5wHeqhRuuw4tqR1y 1X9g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1737800451; x=1738405251; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=M7PYttLtBbsdK/gZ6kIbMnSJ3NKWcgkxmnzIQ8mz2TQ=; b=UEeTh49XkFumE+OZ6UiWDMwQCyYv4SzT5x4juGJgHPSGIpbMAdV2A0uA0NfQt3nkG8 tjPDPTlE2DWi+Lp/yPJ+mN3vwHwAUSRS20FyZaTaL0z6xQzS6Bap8sqDKJGo2ijTpU6m YWvNsmSdsPQ/eSEoL5X0jo4I6U3epwd+XnrCEFSfq5zOgTUSQhrgTmU38z58gkPcW3Yj qlcGC7OBbKdtHnCDQ0OO/ku04C7vF89UMapp7NxPxJmkwdYnSi0S+aJurEtmzDhkzY3I T4svA0rik0NCgGZjueYcWJSMl50tySseW3833SG29opY1UPHWYjfe9SG2/Td6eGBwed/ Akmw== X-Forwarded-Encrypted: i=1; AJvYcCVH58bueHD550H71tiV7Zk3ZWdeYW1iVRxabLGTrj2lLi+t9bfEhtGZ6u5GbnnqREt6pwrILRQ=@vger.kernel.org X-Gm-Message-State: AOJu0YzpNfE1Y05a5pLfp/a6mIpAwdHxYO+qbvhUcsq4AhIU7hN6bWWX AqCZgrKMV/F4asl0lLnMwmXqv7EKXKn8XdDZQABKeu/C0+FSX2/xJHyUQMSF X-Gm-Gg: ASbGncsorIc/PAKM3lRRrVwGRzY0Z70wujYqyXSDncImWfl9EWg/WrnZ9ZYaKnBUeUo eWzEkPqUzQRGjG5dqP/y2B9iyS8XVLjD5eUs2necDK+RdIOjKvk1b2QT5uxyUkUbMBCHeJWEmoJ 0YyQFFBlG6IQpyvxHv9MFqRkTOPggfMgFLfUjFTsVN207nyLzXH44r1uu0ocipqzr5uFIxgPa/b 6/tvDqFYnmcjN9LP/Lou49RVo3oAidGeA1D7R+2ivMQnRNlDV/dy+2c43atRHkGa2lBALMuwTBQ 3AbtyghZYardTbRyVZirEmHeZgphFa0+XZvWi+AvGBBOk3CxVlCDwKw4 X-Google-Smtp-Source: AGHT+IEew27tHyn3byUsCXwKbwCRMB7Xk82wvoYBvjshYg++NuADnORNnU736Vpc+7NZKPHoEMWCUg== X-Received: by 2002:a17:902:f644:b0:211:8404:a957 with SMTP id d9443c01a7336-21c355f6aa9mr579549235ad.41.1737800450601; Sat, 25 Jan 2025 02:20:50 -0800 (PST) Received: from mew.. (p3882177-ipxg22501hodogaya.kanagawa.ocn.ne.jp. [180.15.148.177]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-21da424f344sm29461155ad.232.2025.01.25.02.20.43 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 25 Jan 2025 02:20:50 -0800 (PST) From: FUJITA Tomonori To: linux-kernel@vger.kernel.org Cc: rust-for-linux@vger.kernel.org, netdev@vger.kernel.org, andrew@lunn.ch, hkallweit1@gmail.com, tmgross@umich.edu, ojeda@kernel.org, alex.gaynor@gmail.com, gary@garyguo.net, bjorn3_gh@protonmail.com, benno.lossin@proton.me, a.hindborg@samsung.com, aliceryhl@google.com, anna-maria@linutronix.de, frederic@kernel.org, tglx@linutronix.de, arnd@arndb.de, jstultz@google.com, sboyd@kernel.org, mingo@redhat.com, peterz@infradead.org, juri.lelli@redhat.com, vincent.guittot@linaro.org, dietmar.eggemann@arm.com, rostedt@goodmis.org, bsegall@google.com, mgorman@suse.de, vschneid@redhat.com, tgunders@redhat.com Subject: [PATCH v9 7/8] rust: Add read_poll_timeout functions Date: Sat, 25 Jan 2025 19:18:52 +0900 Message-ID: <20250125101854.112261-8-fujita.tomonori@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20250125101854.112261-1-fujita.tomonori@gmail.com> References: <20250125101854.112261-1-fujita.tomonori@gmail.com> Precedence: bulk X-Mailing-List: netdev@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Add read_poll_timeout functions which poll periodically until a condition is met or a timeout is reached. The C's read_poll_timeout (include/linux/iopoll.h) is a complicated macro and a simple wrapper for Rust doesn't work. So this implements the same functionality in Rust. The C version uses usleep_range() while the Rust version uses fsleep(), which uses the best sleep method so it works with spans that usleep_range() doesn't work nicely with. Unlike the C version, __might_sleep() is used instead of might_sleep() to show proper debug info; the file name and line number. might_resched() could be added to match what the C version does but this function works without it. The sleep_before_read argument isn't supported since there is no user for now. It's rarely used in the C version. read_poll_timeout() can only be used in a nonatomic context. This requirement is not checked by these abstractions, but it is intended that klint [1] or a similar tool will be used to check it in the future. Link: https://rust-for-linux.com/klint [1] Signed-off-by: FUJITA Tomonori --- rust/helpers/helpers.c | 1 + rust/helpers/kernel.c | 13 +++++++ rust/kernel/cpu.rs | 13 +++++++ rust/kernel/error.rs | 1 + rust/kernel/io.rs | 5 +++ rust/kernel/io/poll.rs | 79 ++++++++++++++++++++++++++++++++++++++++++ rust/kernel/lib.rs | 2 ++ 7 files changed, 114 insertions(+) create mode 100644 rust/helpers/kernel.c create mode 100644 rust/kernel/cpu.rs create mode 100644 rust/kernel/io.rs create mode 100644 rust/kernel/io/poll.rs diff --git a/rust/helpers/helpers.c b/rust/helpers/helpers.c index d16aeda7a558..7ab71a6d4603 100644 --- a/rust/helpers/helpers.c +++ b/rust/helpers/helpers.c @@ -13,6 +13,7 @@ #include "build_bug.c" #include "cred.c" #include "err.c" +#include "kernel.c" #include "fs.c" #include "jump_label.c" #include "kunit.c" diff --git a/rust/helpers/kernel.c b/rust/helpers/kernel.c new file mode 100644 index 000000000000..9dff28f4618e --- /dev/null +++ b/rust/helpers/kernel.c @@ -0,0 +1,13 @@ +// SPDX-License-Identifier: GPL-2.0 + +#include + +void rust_helper_cpu_relax(void) +{ + cpu_relax(); +} + +void rust_helper___might_sleep_precision(const char *file, int len, int line) +{ + __might_sleep_precision(file, len, line); +} diff --git a/rust/kernel/cpu.rs b/rust/kernel/cpu.rs new file mode 100644 index 000000000000..eeeff4be84fa --- /dev/null +++ b/rust/kernel/cpu.rs @@ -0,0 +1,13 @@ +// SPDX-License-Identifier: GPL-2.0 + +//! Processor related primitives. +//! +//! C header: [`include/linux/processor.h`](srctree/include/linux/processor.h). + +/// Lower CPU power consumption or yield to a hyperthreaded twin processor. +/// +/// It also happens to serve as a compiler barrier. +pub fn cpu_relax() { + // SAFETY: FFI call. + unsafe { bindings::cpu_relax() } +} diff --git a/rust/kernel/error.rs b/rust/kernel/error.rs index f6ecf09cb65f..8858eb13b3df 100644 --- a/rust/kernel/error.rs +++ b/rust/kernel/error.rs @@ -64,6 +64,7 @@ macro_rules! declare_err { declare_err!(EPIPE, "Broken pipe."); declare_err!(EDOM, "Math argument out of domain of func."); declare_err!(ERANGE, "Math result not representable."); + declare_err!(ETIMEDOUT, "Connection timed out."); declare_err!(ERESTARTSYS, "Restart the system call."); declare_err!(ERESTARTNOINTR, "System call was interrupted by a signal and will be restarted."); declare_err!(ERESTARTNOHAND, "Restart if no handler."); diff --git a/rust/kernel/io.rs b/rust/kernel/io.rs new file mode 100644 index 000000000000..033f3c4e4adf --- /dev/null +++ b/rust/kernel/io.rs @@ -0,0 +1,5 @@ +// SPDX-License-Identifier: GPL-2.0 + +//! Input and Output. + +pub mod poll; diff --git a/rust/kernel/io/poll.rs b/rust/kernel/io/poll.rs new file mode 100644 index 000000000000..7a503cf643a1 --- /dev/null +++ b/rust/kernel/io/poll.rs @@ -0,0 +1,79 @@ +// SPDX-License-Identifier: GPL-2.0 + +//! IO polling. +//! +//! C header: [`include/linux/iopoll.h`](srctree/include/linux/iopoll.h). + +use crate::{ + cpu::cpu_relax, + error::{code::*, Result}, + time::{delay::fsleep, Delta, Instant}, +}; + +use core::panic::Location; + +/// Polls periodically until a condition is met or a timeout is reached. +/// +/// Public but hidden since it should only be used from public macros. +/// +/// ```rust +/// use kernel::io::poll::read_poll_timeout; +/// use kernel::time::Delta; +/// use kernel::sync::{SpinLock, new_spinlock}; +/// +/// let lock = KBox::pin_init(new_spinlock!(()), kernel::alloc::flags::GFP_KERNEL)?; +/// let g = lock.lock(); +/// read_poll_timeout(|| Ok(()), |()| true, Delta::from_micros(42), Delta::from_micros(42)); +/// drop(g); +/// +/// # Ok::<(), Error>(()) +/// ``` +#[track_caller] +pub fn read_poll_timeout( + mut op: Op, + mut cond: Cond, + sleep_delta: Delta, + timeout_delta: Delta, +) -> Result +where + Op: FnMut() -> Result, + Cond: FnMut(&T) -> bool, +{ + let start = Instant::now(); + let sleep = !sleep_delta.is_zero(); + let timeout = !timeout_delta.is_zero(); + + if sleep { + might_sleep(Location::caller()); + } + + loop { + let val = op()?; + if cond(&val) { + // Unlike the C version, we immediately return. + // We know the condition is met so we don't need to check again. + return Ok(val); + } + if timeout && start.elapsed() > timeout_delta { + // Unlike the C version, we immediately return. + // We have just called `op()` so we don't need to call it again. + return Err(ETIMEDOUT); + } + if sleep { + fsleep(sleep_delta); + } + // fsleep() could be busy-wait loop so we always call cpu_relax(). + cpu_relax(); + } +} + +fn might_sleep(loc: &Location<'_>) { + // SAFETY: FFI call. + unsafe { + crate::bindings::__might_sleep_precision( + loc.file().as_ptr().cast(), + loc.file().len() as i32, + loc.line() as i32, + ) + } +} diff --git a/rust/kernel/lib.rs b/rust/kernel/lib.rs index 545d1170ee63..c477701b2efa 100644 --- a/rust/kernel/lib.rs +++ b/rust/kernel/lib.rs @@ -35,6 +35,7 @@ pub mod block; #[doc(hidden)] pub mod build_assert; +pub mod cpu; pub mod cred; pub mod device; pub mod error; @@ -42,6 +43,7 @@ pub mod firmware; pub mod fs; pub mod init; +pub mod io; pub mod ioctl; pub mod jump_label; #[cfg(CONFIG_KUNIT)] From patchwork Sat Jan 25 10:18:53 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: FUJITA Tomonori X-Patchwork-Id: 13950307 X-Patchwork-Delegate: kuba@kernel.org Received: from mail-pl1-f180.google.com (mail-pl1-f180.google.com [209.85.214.180]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id B9217207DFD; Sat, 25 Jan 2025 10:20:58 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.180 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1737800460; cv=none; b=SmB5x3VY2tPzmFznOzSlj/FZrPJsyNW4PyVuS9uvpQgyrqECozVqcpJnf1TsOz3t2EMC8YcXKG8t64ejqgK/0AbdI0STGVdPL2OzAF9PdGuTx6DwnjgactOHXNqZ2uxe70Bx3BJRAFU06OOgvjEKfZkx3KNWfyG+2Wil78EMtyE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1737800460; c=relaxed/simple; bh=dtsPSe/89K1qHsAPwEqFVf1AQJ3aUTY/1OyPXFve/GU=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=o5+UlwrWTlLSl7bgc5Jccxx4YkkjcZX6XFPdDXBCUtgf4HiAlYqBTwWYzw+BlYU7lQ8/p36gikAGkzMr672xegpHk3Sb5ntp4J2biBGww8pteWqoE2xdSLwjn3Tbk008oOYcvEl5tXDsUDgjk6Wm86h6bE0cTpRVjr5DF2G35CY= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=dyHzqdyx; arc=none smtp.client-ip=209.85.214.180 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="dyHzqdyx" Received: by mail-pl1-f180.google.com with SMTP id d9443c01a7336-21bc1512a63so59034455ad.1; Sat, 25 Jan 2025 02:20:58 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1737800458; x=1738405258; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=DdEH3sWOd96J7GmaCg3rI9cjGuBtz1cihBXbvMrlTZ8=; b=dyHzqdyxX8fzTmOegdLma4+X80IV9IkeObLZL/tfryqzMgXfCfCkqKfnO1TMRLhnjR s5uis2T2VZcYGQHjReoVGAIvmbYe3jBFUPCHuJfBUn8ZbzrrXQs2oiYqNtNj8PSsSMfr /LB9iILr9K1kG70uM0g0CHShYY2mZ44nXzeLl4kCI2WXrlEiiGS8VDkQccVctj4b6B+7 4RvSAXW6JgWYoWOSDZiCot4pWcbq5Elj+YnJJSeh2kcHMvU3EWDdmrQF11VcPHTPmJ40 LFgxTKsWmPBUj49cfsvUa8gYl4kig/cDchOFhkdTzQX6Hy6mBKHi5/Dbat9V39WumpL5 JDpg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1737800458; x=1738405258; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=DdEH3sWOd96J7GmaCg3rI9cjGuBtz1cihBXbvMrlTZ8=; b=je++zdp79gv08k0OeGvtebfq3UrQAuBR3tWzJwPQ5Xru0ZMVJkAFJSkeDsNrtP/h9g hdIyEfEFLTd4FhgFkLYX30ph1xGAlcUGCPMf6jaZL/zyPVRuOpZioTwC7NhBl997pRXM yruMbvEpl0PU9gy8Ly72sSSrQivpAyt5/SUKmURbGJyTFg8vD83UkqmwEqv+6B8IJb+w uWaENqKlVkeK1OrG12KKKSCSAOljOXu70LqIG+iR2n6d3Tl5s5ggH+Iz20iPGVDR1sWh GelWKeuu0w0DNTsX8qMW8u4VpQJ/ALnVS8LqwcNP8I0xWIP+nfd7A5QVoaVg5OUfnzD8 II9g== X-Forwarded-Encrypted: i=1; AJvYcCWPNdWfW0JoasNGSzH6CYrFpgpF0ExDLcO8d1YxFO39RD+PGmAWBhbqlWMaqY2RzEay8m+rVYg=@vger.kernel.org, AJvYcCXt5P50yIHHj4mwhGDQnd8rPu9xdGtBOEKZ2WSOF9LG6lBE4R6RvfJ3B9SmW/NIIs4h8HwHOq5/8Wtcl9AEmaI=@vger.kernel.org X-Gm-Message-State: AOJu0YwPxW7h35cHjYNomu0iOFl6R/G4LUBpgQGFS105R0JiK6F0/x2/ HRX3Ihxs++lf/6E29zjzpYrBlUauSdEmyfD+o2ZELJtJndbDYL6DxINeRluM X-Gm-Gg: ASbGnculVzXf+oU7wNjJrsVKhfwBifBhNxEOIH24OwjRyFbE9zzOG8oyU1MGIb5tJ69 CPrtPO6DItfV96mUrpMPij89Carx1V7Yy1gHwFSfNZvjV0hmwff0SCLEal7WhCwtRlFVG9PwwjV oWlXi9NA+e2DdUkDRDgZHmgpO3Z4iGnk2SgI+VJLnqOyFDGEHJII7gFDqP7HVUOEYet6xxKnz6b ffv9Jb0SsYv59cr0EYJBB3v6NYkzhChjjCem4WrJEcpIypLQzDgcW8zDEhuLE0gAiDSGa9Zcd5i iUoFdoOBZUIbN1lJQAmGyWHHgPxwTQQAR/6ICQUgMyh5xlwAeN1OtEut X-Google-Smtp-Source: AGHT+IG3ByhHT2uvNChQG8MEVjIDl1+asqjbY9Lbh9elMS6FdR1fy14OY5OjIprl1M3G87M1OfTS4g== X-Received: by 2002:a17:902:ec90:b0:216:3dc0:c8ab with SMTP id d9443c01a7336-21c3553b527mr458147165ad.9.1737800457820; Sat, 25 Jan 2025 02:20:57 -0800 (PST) Received: from mew.. (p3882177-ipxg22501hodogaya.kanagawa.ocn.ne.jp. [180.15.148.177]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-21da424f344sm29461155ad.232.2025.01.25.02.20.51 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 25 Jan 2025 02:20:57 -0800 (PST) From: FUJITA Tomonori To: linux-kernel@vger.kernel.org Cc: Andrew Lunn , Alice Ryhl , Gary Guo , rust-for-linux@vger.kernel.org, netdev@vger.kernel.org, hkallweit1@gmail.com, tmgross@umich.edu, ojeda@kernel.org, alex.gaynor@gmail.com, bjorn3_gh@protonmail.com, benno.lossin@proton.me, a.hindborg@samsung.com, anna-maria@linutronix.de, frederic@kernel.org, tglx@linutronix.de, arnd@arndb.de, jstultz@google.com, sboyd@kernel.org, mingo@redhat.com, peterz@infradead.org, juri.lelli@redhat.com, vincent.guittot@linaro.org, dietmar.eggemann@arm.com, rostedt@goodmis.org, bsegall@google.com, mgorman@suse.de, vschneid@redhat.com, tgunders@redhat.com Subject: [PATCH v9 8/8] net: phy: qt2025: Wait until PHY becomes ready Date: Sat, 25 Jan 2025 19:18:53 +0900 Message-ID: <20250125101854.112261-9-fujita.tomonori@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20250125101854.112261-1-fujita.tomonori@gmail.com> References: <20250125101854.112261-1-fujita.tomonori@gmail.com> Precedence: bulk X-Mailing-List: netdev@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Patchwork-Delegate: kuba@kernel.org Wait until a PHY becomes ready in the probe callback by using read_poll_timeout function. Reviewed-by: Andrew Lunn Reviewed-by: Alice Ryhl Reviewed-by: Gary Guo Signed-off-by: FUJITA Tomonori --- drivers/net/phy/qt2025.rs | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/drivers/net/phy/qt2025.rs b/drivers/net/phy/qt2025.rs index 1ab065798175..b6a63eaa6ef5 100644 --- a/drivers/net/phy/qt2025.rs +++ b/drivers/net/phy/qt2025.rs @@ -12,6 +12,7 @@ use kernel::c_str; use kernel::error::code; use kernel::firmware::Firmware; +use kernel::io::poll::read_poll_timeout; use kernel::net::phy::{ self, reg::{Mmd, C45}, @@ -19,6 +20,7 @@ }; use kernel::prelude::*; use kernel::sizes::{SZ_16K, SZ_8K}; +use kernel::time::Delta; kernel::module_phy_driver! { drivers: [PhyQT2025], @@ -93,7 +95,13 @@ fn probe(dev: &mut phy::Device) -> Result<()> { // The micro-controller will start running from SRAM. dev.write(C45::new(Mmd::PCS, 0xe854), 0x0040)?; - // TODO: sleep here until the hw becomes ready. + read_poll_timeout( + || dev.read(C45::new(Mmd::PCS, 0xd7fd)), + |val| *val != 0x00 && *val != 0x10, + Delta::from_millis(50), + Delta::from_secs(3), + )?; + Ok(()) }