From patchwork Thu Feb 20 07:06:03 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: FUJITA Tomonori X-Patchwork-Id: 13983399 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 EE3801E3DEF; Thu, 20 Feb 2025 07:10:20 +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=1740035422; cv=none; b=jbmckFMGxoLNshsLmF38fl4L14PCl6YM58LyJAhoCN/9Zj3eUJv95e118vQUwsaoL//TJRX0tSwByexyQCUR7VOg3q5VX5/0lhsRGrmT857PHpAkhuQSen8JEDx+ilW9RriPRGlDuIUd+2a/gkCUQvfNX7ePrPZftRyevleZR9w= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1740035422; c=relaxed/simple; bh=Rkh6ndSceSynog0AU/kr8BvXWRW8K2KzzGx5hds8B9w=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=q184d3gbyAYzzMeO6hwxEN7COn0COm+lAajkM8jYywiuweDSmX84zwdcVmzLpZdy38s8QmvJxOhINQHtuLQNPZJNNI43phaKuDYFQnMjhi7i0Eo9HLisZd+I5ihWrVZXTnruGvH4sGjGI81sSFvtC1NjnEVkASbPHhkf+PW9C88= 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=XLKtfbhl; 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="XLKtfbhl" Received: by mail-pl1-f177.google.com with SMTP id d9443c01a7336-220d39a5627so8451495ad.1; Wed, 19 Feb 2025 23:10:20 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1740035420; x=1740640220; 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=m3mkDtg1XWCj0JInAia08UGBi51YLWhiTTVLJLbItqg=; b=XLKtfbhlhH9ynLbRkjkOVmEk9mvec5TB0CNu2LolTClx5q6CPydFIvaRY/3rlRcR9T 2W6RADnyvHn/KHVA/bwCwHc/kz2t/10tZyvG6w1Tid+OixXZZ4OYyRzkXpzdzOfxCncT kV5GYnA2bU+Rfahloy2bRKoEdelCjOvJeqeBL8ln3gfdx+Y+TggoZgvwSjskT88zy0O6 GvA/6cYYH7v3D0cvcYyW3rRAqQFFuUQCN8GXfEVrnkH+AiTe2jg1V+2LKRoXQ3HyaFbw +/fGsAyxS2t8f1OBMRtmHpRhz807gHLf4M18btxPEX2elNCt6x9is+oCX9mB5iXi9eGQ lT/A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1740035420; x=1740640220; 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=m3mkDtg1XWCj0JInAia08UGBi51YLWhiTTVLJLbItqg=; b=WkmaWTkwhRLEamwvNsna/skmoiGdt1lT6vJxScg/UdUMPipaaFxUCA3kyVzJp4SGJF l/J5VtAdG0BaKd6VC//FbHueHPYi3xYdyFfPTW7V75euHV4S99Wygs7EbrZy9IMf3cTH QL+fKkkUCj0O9VU/CxCPUzRtiyFi/tzn8sI5sQZInu0Lc1qTA2MUEdy/24CssCnI2Tl/ KzpqlgbiclQAM/SQOAy4ZBb/rJLluur28ysaRgp/8q1YJU9HBxpMyA8l9DoZ9MDbPqke dYKolSCelDWuYVO8wxDDHPpz24otl7dyPFgNP2PT5HLJZZ/vlU1buaG78fsJc+NnGMDG BmSg== X-Forwarded-Encrypted: i=1; AJvYcCV0ssroOj3qI53cMm81ou0f7QgR2B55UdCULZ0NXm6m6ekPe3TXz2G5/W+NGdsfP9fX8YUKqjc=@vger.kernel.org, AJvYcCXZGusC+bSNIKZ6nDM3CT7sT0SOc3ABkXzgnUaD8J4mmYrMJWI2+2xpnBaxeRjHuX5m365mcdzrwZJ1DMyr17Q=@vger.kernel.org X-Gm-Message-State: AOJu0YxRIEbYTcfKkLrfSPArUbSR8VEBycd4EmrXWO8roqQRC/LGgaNX +onQuvf3+D0JBPktY+83PUpOFGTuQ+64K7EiSBh0IZ20IyWb2fADDW8j+H2e X-Gm-Gg: ASbGnctkVZ/VjYSAxLsGz6wiOiFDZjIJNMlL81DGDI0ksZknoYhKpcPOH0Klj+OmGAk 1yDiSwaUFInWexrbg0B7a7JCB/BVv0V1XAJsrinTGLITVcVa5uVjhS3R3eerADr5u5xdGkV8qyD zR5uQC4vHGKEUgbGCuyxkfg56baXSHvZDRGhSirBZ7IMD8fAsz1CJl7lGWI+LYYJ0Fqb41UEnrg ZvXzZzNRSnTxZgLFZKHWFXx85hycaswDuFzXbwsJ/jKzXQQTEqihTxyxZllXkoDPI9vr+x357QD LwX5rD+pnZV64e2vbsaNQ5+TysMrKrRJ0Mv0QW26zfAIrSCy5DPNSOXNYp30MR5Pi4w= X-Google-Smtp-Source: AGHT+IHYZsgaPYQx2FdMAsV+vsquysI7f3kod6flfQmC9QzDnSxmST3+dXSeVvKnFPe4OsQYvITBbQ== X-Received: by 2002:a05:6a20:b418:b0:1ee:abce:226f with SMTP id adf61e73a8af0-1eee5dabb6amr2402693637.42.1740035419816; Wed, 19 Feb 2025 23:10:19 -0800 (PST) Received: from mew.. (p3882177-ipxg22501hodogaya.kanagawa.ocn.ne.jp. [180.15.148.177]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-73242568af8sm13059672b3a.48.2025.02.19.23.10.11 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 19 Feb 2025 23:10:19 -0800 (PST) From: FUJITA Tomonori To: linux-kernel@vger.kernel.org Cc: Daniel Almeida , Alice Ryhl , 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, 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, me@kloenk.dev, david.laight.linux@gmail.com Subject: [PATCH v11 1/8] sched/core: Add __might_sleep_precision() Date: Thu, 20 Feb 2025 16:06:03 +0900 Message-ID: <20250220070611.214262-2-fujita.tomonori@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20250220070611.214262-1-fujita.tomonori@gmail.com> References: <20250220070611.214262-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. When printing the error (sleeping function called from invalid context), the precision string format is used instead of the simple string format; the precision specifies the the maximum length of the displayed string. 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 Tested-by: Daniel Almeida Reviewed-by: Alice Ryhl Co-developed-by: Boqun Feng Signed-off-by: Boqun Feng Signed-off-by: FUJITA Tomonori --- include/linux/kernel.h | 2 ++ kernel/sched/core.c | 61 +++++++++++++++++++++++++++--------------- 2 files changed, 42 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 165c90ba64ea..6643e03eafa4 100644 --- a/kernel/sched/core.c +++ b/kernel/sched/core.c @@ -8678,24 +8678,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)) @@ -8717,7 +8699,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 file_len, int line, + unsigned int offsets) { /* Ratelimiting timestamp: */ static unsigned long prev_jiffy; @@ -8740,8 +8723,8 @@ 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); + pr_err("BUG: sleeping function called from invalid context at %.*s:%d\n", + file_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); @@ -8766,8 +8749,44 @@ void __might_resched(const char *file, int line, unsigned int offsets) dump_stack(); add_taint(TAINT_WARN, LOCKDEP_STILL_OK); } + +/* + * The precision in vsnprintf() specifies the maximum length of the + * displayed string. The precision needs to be larger than the actual + * length of the string, so a sufficiently large value should be used + * for the filename length. + */ +#define MAX_FILENAME_LEN (1<<14) + +void __might_resched(const char *file, int line, unsigned int offsets) +{ + __might_resched_precision(file, MAX_FILENAME_LEN, 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, MAX_FILENAME_LEN, line); +} +EXPORT_SYMBOL(__might_sleep); + void __cant_sleep(const char *file, int line, int preempt_offset) { static unsigned long prev_jiffy; From patchwork Thu Feb 20 07:06:04 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: FUJITA Tomonori X-Patchwork-Id: 13983400 Received: from mail-pl1-f181.google.com (mail-pl1-f181.google.com [209.85.214.181]) (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 4CBD61E5B63; Thu, 20 Feb 2025 07:10:29 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.181 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1740035430; cv=none; b=NCrynUb9KJ6J6NQKSSYSZJ1qENM10ZquTKK3t5HK5JpdIgx6HDZ6H8oP2cpnSTmj5WmLEwRDfushjz+gLrBkEDvmnNrfl5/qY0WkcVGbzr0N8ehMrSk+EI3wwS1q1uW/3I+rsOhRS31tLGP66+cQ+edXp4eEepqUCz6BbMlnl+Y= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1740035430; c=relaxed/simple; bh=F4MQ13dRTvjdMRvvzGNbECYKc2DSsjjEfJe5kqp6TkA=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=WlKkQBbD+64CJydm+VflDO+O+RBGdu1MrJ1/PMxpW5AokQHvLkz5MLvbD3w04l8qxw8Q8QIe9kO3hyy7KTuOh01dsGMYWTU5PRcr1QzojanYdsAEz/TQ/lnukiHq9xcknGbIaQCdpSpNhsB4ShVxD/uNAxv5rHxLERh6F5xiDwc= 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=cpTQkaia; arc=none smtp.client-ip=209.85.214.181 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="cpTQkaia" Received: by mail-pl1-f181.google.com with SMTP id d9443c01a7336-221206dbd7eso9761625ad.2; Wed, 19 Feb 2025 23:10:29 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1740035428; x=1740640228; 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=e+v1QyRz41BbOlaQv2erYxdSScozwJORMKo7m3a8Kic=; b=cpTQkaia4PFwYeo0jBHTaxi54eksqyJuC6MY3RlqYB5hP5jmAgQg26CflQV9s9IsKs 8/Rp7AuXsSAykD4gPHyOmgPDG2RgvCuRQuG00xG2abrqlAAwC2BuuzxlMnXgXY83dqLv qce3+WV/HZdn+8kUb4U8kqR0/eJfrYcROHW+h2mmnMlj7a7+GWukRCOpBMxrABOqXOQD A1IN3rTDRWpmdzdEDJtNmrUi6I7yj7yfDqXpYD8a3lO35R7gRmPlkbqshRAXSk/GEmhL YEVyHAQGnG1UIy7ZGX48xWA/OqSTl8CNf4/KW7SKlKjEurVuNDjI2YljLZjuH3mUJF3j AsZw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1740035428; x=1740640228; 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=e+v1QyRz41BbOlaQv2erYxdSScozwJORMKo7m3a8Kic=; b=XBa4d1GzGPTWWQbuiZAVo2cFFRHQfd4juWgoiEdjznll/aIFNaejTZUbIAKR31UdzE PKu6KqQ6rq3yHUHMS4b0kH2wmImFRU9gIVXYswo6pW54Ro8+kitOX2FmnUbFwFfXxWod FykxcZ4h1BTnDHb3+p4UHrcJROb1Np4YEhKMeEbguMcybyDjIBzMYF1Wu9VzEbs80BwZ BJ+tOlYBE6P7hLHte2InxCYrY0VGVz/b+TswGL5eDkA8oGIztZKSl6NrKHtle7c9vxTG 3TH1BXwtrGB4g3YWmqXPe6U3rfuomzAYBsQw1/Ys3Po6RTMDlVn6ZwQhI6ZQfc8afiRC 8Gow== X-Forwarded-Encrypted: i=1; AJvYcCUF5j4FNxjCFWDrcguu87GI7SfhynJ78W9sE1whVa4jJrC49+hLm2GZ3XC+2whGzIYIuIuVasI=@vger.kernel.org, AJvYcCWaDtMhguJVkO+wOxMK0oYXtEDqBc47bW36vK87Lz8agwGlQB98vZddjXqBY8gARoXDYSNPiKufe/iz+fyxWv0=@vger.kernel.org X-Gm-Message-State: AOJu0Yxy0FwDJbFSqGzbxfJr5HUFpQV3S30i95hOO6WwQALCrWvVY0Zu ytFxSjGgcM4uYAcpnG4hNIdZX8T/uz+HFEc27OyLA8AWGqMInWNnacw8kaXk X-Gm-Gg: ASbGnct5v+7rd9KyKMzqJOSVFXRHO0kaqgFQgZXozHp8Nm+Lc9GJbAi2c9kap9/ncgI xI21TKGvlPuCsnPn+38hNYPApfhnT8f1OXQkVEbvCyVEQGR5TeQgs0A/+SFfh9jkML3PKDOKcxa QPQ6W4zLbe87KqbGhEzno79lql1z7AGmP8GI/JMUKS9A+Qs7n0nq+gbL2LB9U/ijqy4z5+yIal7 5LG0tYEA2lMv9V0HaPNRVW7r+2e6bZ485i+5WKJ/y9nEfBYOp27NI12VsDX9wJ7Qg+OyMpasB/5 MMYPV/DJBKir43TTMsWZKS60YF/YSycNTxaAi9HHxxTxBqzfmHohA9+FHAGzH7IjLMQ= X-Google-Smtp-Source: AGHT+IGn/7s0IeGlZEMDIjGUk6iLqy+keYxxiBJ9Djl3lxBK0ZN78N00biCShYeOIZ1LRgW96pnjLw== X-Received: by 2002:a05:6a00:3a11:b0:730:95a6:375f with SMTP id d2e1a72fcca58-7329de47f9amr10483915b3a.3.1740035428155; Wed, 19 Feb 2025 23:10:28 -0800 (PST) Received: from mew.. (p3882177-ipxg22501hodogaya.kanagawa.ocn.ne.jp. [180.15.148.177]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-73242568af8sm13059672b3a.48.2025.02.19.23.10.20 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 19 Feb 2025 23:10:27 -0800 (PST) From: FUJITA Tomonori To: linux-kernel@vger.kernel.org Cc: Daniel Almeida , Trevor Gross , Alice Ryhl , Gary Guo , Fiona Behrens , 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, david.laight.linux@gmail.com Subject: [PATCH v11 2/8] rust: time: Add PartialEq/Eq/PartialOrd/Ord trait to Ktime Date: Thu, 20 Feb 2025 16:06:04 +0900 Message-ID: <20250220070611.214262-3-fujita.tomonori@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20250220070611.214262-1-fujita.tomonori@gmail.com> References: <20250220070611.214262-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). Tested-by: Daniel Almeida Reviewed-by: Trevor Gross Reviewed-by: Alice Ryhl Reviewed-by: Gary Guo Reviewed-by: Fiona Behrens 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 Thu Feb 20 07:06:05 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: FUJITA Tomonori X-Patchwork-Id: 13983401 Received: from mail-pl1-f172.google.com (mail-pl1-f172.google.com [209.85.214.172]) (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 352131E5B87; Thu, 20 Feb 2025 07:10:36 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.172 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1740035438; cv=none; b=grhPjThQpdki7Z4K4TmyStANYWGeZ3GDkug3BJg3YkXud1ztrbw2R3yDW4AJTQfldg8XwXUvwXlW7P51pfh2suwiAbBExrh3Gn5FyLW2jBPp8txJxJznerGFVQ+0Na/L2YCGRGuzgWV9nX0PKzsXLf/8RDnkLQ+d+ZSuvNDIGZ8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1740035438; c=relaxed/simple; bh=pkSYO6NlMhW+D/DQ7y5lZOVnQKEZRbtDpqdcNVBlflY=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=WARA1YpbBbBvRiXAK6flwanHL+FuN77EgYZTXMmnx1/+yl1ZA6GFoFE9jnc5FfA/hFV+6s7CwXENLrMOTdU2velGwu3Rz/yiqrZyI/fqtTC4Xa72cGIk5l2lC/nm/qclIVVSelCEm3IS3d5wVWA21U7NstWo5Wm47w2/zm4nTb0= 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=mUkbjpJX; arc=none smtp.client-ip=209.85.214.172 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="mUkbjpJX" Received: by mail-pl1-f172.google.com with SMTP id d9443c01a7336-22113560c57so10608955ad.2; Wed, 19 Feb 2025 23:10:36 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1740035436; x=1740640236; 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=I4TJ+Zzt6WaZlUf68dO8eijca8HC/3tLlWTzp/tyTWE=; b=mUkbjpJX4sPXcW2dA0mIORtcLMQqP6m1EYx+pZyVBzwLxH71VepNO5eU1vV42p9eXd /sePPbAyugCUP2DdNqvCIil0IKTd8Q8t2m6qDfVEm5xeu6wuCKSVVYjM5+n0xLQwy7/q qcDEeC9UdygfYUk+RstBwBZOYHJLQMQ8+g9ZKmttxQ2W2FHEpG5LfVm1lhZE4UApJU4v 3chfFY3v7QECTLQXaPFLILbGm9n12kWZHjBiHKMi1qtrXwGaWbyvbGT7FFXgYg4+HESY W4SnQfI89wR/jNW7ABvMSEHtuNwS7522ovq7K038UHANaEi2fuGVpiKC0apbIoKw7eUH 7z1g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1740035436; x=1740640236; 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=I4TJ+Zzt6WaZlUf68dO8eijca8HC/3tLlWTzp/tyTWE=; b=MBW6R9Sx2yduTn0tJUrUH8uKtlDGCVY4+X5lPX+bXnHl9RCIkthrwOIayTBECsXBxE wBIPtV2oy7Q1X4vvC0CsnUsNKwj+SODodBl/I6ab3+9ViVgXaTPJib8fC0cAu2PoTWVP tldUFBQPhbO93tlIhUogfbOpUsfMwgNYunj0GC5ebkTXTfKbJAAtvmaJnFfsNuFF8OEF ScIe+ggiWieollkIvLdfSo3SLS4QPDs6/w2paVlxGaQtASQ8tOdzYBjhkQmOQRckWhc2 p7CAoyWvyd1AmmRIv9DBA7Gy9ErgEUNVw2ClkhjcYFX5MhoFWArzflf64cv4oMK+WkGA Uc8A== X-Forwarded-Encrypted: i=1; AJvYcCU6fXBgYpsmTuXOJ7TmSH6BJYfC02gPWc+GyfecKEvRKNPONRAOtMTI2lAkmT8Yaj+fzjqEKMg=@vger.kernel.org, AJvYcCXs+8E9oVMJTleOdJOo9jhhQKmtrWuQSxQTZn18OKQvb9lREMWC9Ty0SCulLVwx7MEk/KWudOiI9/GOWcyCk1g=@vger.kernel.org X-Gm-Message-State: AOJu0YwDAsgmWiKnSuwjEHkl3/EGTohscYzY1eLYI9/bVBGN6ntXxiAp hF7f8pGdo05SfNxlrJIPIgu5tI+5VowF5S8IieVuJUSUDMDU2kZazIN7jHnm X-Gm-Gg: ASbGncsdlkPofLLwoN+Fr/Dm8nh2BRD0UYayNblfG+eliKsooUT99//kGI6lhwHWapa /5VQTNTsqxU2P4svFTZBK3xpbXwFmH7hV4I6/jW9APO4zS5saHdxl+TR4ioJlRANMXHHviBFovu Nh9ejXo98VaOsOxJoJ1UQLoXxUZoG8ryZc7UyQgD9ZcVAeN2j/30KVLwK46cHardSP6kK5xkBbE Bv/jgRSgNW1ObRIRry/RsL+A6JpLoRZJFMtYyyFualo+VhwklbwjZvLnWWCVNXyvkDyTBm09ffD NEZkiIN0uDtZ+VtPlpbFLZKlmXRD4NK8QJwKoaUyclkKmaSwjG66lw5IDTFjb7lZqVo= X-Google-Smtp-Source: AGHT+IG/d/do3HPgOHdwMufa+1WBx3SE5+2M9ZJFHzfK23ADwWeL8LJCvWvmechh6RZ++eueyjm0+g== X-Received: by 2002:a05:6a20:160e:b0:1ee:c598:7a93 with SMTP id adf61e73a8af0-1eec5987c6emr17652336637.42.1740035436186; Wed, 19 Feb 2025 23:10:36 -0800 (PST) Received: from mew.. (p3882177-ipxg22501hodogaya.kanagawa.ocn.ne.jp. [180.15.148.177]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-73242568af8sm13059672b3a.48.2025.02.19.23.10.28 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 19 Feb 2025 23:10:35 -0800 (PST) From: FUJITA Tomonori To: linux-kernel@vger.kernel.org Cc: Daniel Almeida , Andrew Lunn , Alice Ryhl , Gary Guo , Fiona Behrens , 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, david.laight.linux@gmail.com Subject: [PATCH v11 3/8] rust: time: Introduce Delta type Date: Thu, 20 Feb 2025 16:06:05 +0900 Message-ID: <20250220070611.214262-4-fujita.tomonori@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20250220070611.214262-1-fujita.tomonori@gmail.com> References: <20250220070611.214262-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. Tested-by: Daniel Almeida Reviewed-by: Andrew Lunn Reviewed-by: Alice Ryhl Reviewed-by: Gary Guo Reviewed-by: Fiona Behrens Signed-off-by: FUJITA Tomonori --- 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 Thu Feb 20 07:06:06 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: FUJITA Tomonori X-Patchwork-Id: 13983402 Received: from mail-pl1-f179.google.com (mail-pl1-f179.google.com [209.85.214.179]) (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 64A631E766E; Thu, 20 Feb 2025 07:10:45 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.179 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1740035447; cv=none; b=CYaoI6BxzTLYLLZWcvyP5fySnvEdm4wRcAhxw0xRr4Q3pYh84c/DnphxLocYWmUsLJfwwg7uJ3FpFb6b+z/AJ/wl+UL81zxXMiBcsDAtbMISubSVXhJTqbbsd6X5Fbv1BY9DKPxgAUwzItvDRhp9W6H+IeEh3XoH6KlNvLf4ifM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1740035447; c=relaxed/simple; bh=cQ340EQRb+JwkfoITetc/kRTqFCH9Dx8D0nhzsT/24s=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=ghGu0M+Q6phYYY4nzMrJzdQ19Q/VRnpM0+WvNhJ1lPoqQ0uZo+eDPISeXDQzDjZRv5ff1Ze8nra4jQ2Ylfe4cuJNS0UhDC9HKaYcgTT9Gjo7LxFg6cZn0hiDmDdaeVJpHt+7CN5Tnih5XsPFe7LPUBs59wRlSzfK3+7+TtdvYhs= 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=LhXcnZKU; arc=none smtp.client-ip=209.85.214.179 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="LhXcnZKU" Received: by mail-pl1-f179.google.com with SMTP id d9443c01a7336-220c8f38febso10462585ad.2; Wed, 19 Feb 2025 23:10:45 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1740035444; x=1740640244; 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=fKT6vKHpCFtbPYTy0cNxWdMoBAaxxzfIYLpKqSCQI9Y=; b=LhXcnZKUS5bSgh4TjkB8FCSlcocdI7eXkNw62IyJWyu/YxpIyq1N5WGLh9ny6t0RUn Vt1KEtRVuL2/hUzQFtb9ydgEttAAi/+xNsol+oWfu8AhvszdrIwQh0F0cLpw+q4xOu2B TLmTv3gIG2+NztjqztZl9kGoajc4rI/s0yqdLQxDmAwM0lO1d0EBljlgR+qEPM3PuBBX zq6JWL8oaJaKkBgwePINWbm6FM8UhzCmHlJdA4AiYmnD3QV7MFCA1cmvyslof9drB+0i cPGBzgSHdrhaFHQ/zJf/IH+nB7KhJciJ4dqsga9qYnh+EJLFibkAz88fQEJrsuq3oV9c qlEw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1740035444; x=1740640244; 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=fKT6vKHpCFtbPYTy0cNxWdMoBAaxxzfIYLpKqSCQI9Y=; b=gG0bS/w15cGYjWNcJ1FFm/qyYGLjGbTKgTiWxjqcYhp286PMnuXlQrNOkSJ3Axc9cY dXN8lLwFgJxdZCkHq4YxU7pbubdxLYbcybvBTac0kUh3x65bJmgtynQ2W96jqELx7ew/ FfX3ZrnErAArePxp+M0i3wYGYrZOAoFMiSIRwnv2F5ZKpWu//sVmdKt5OrSI1tsgxDeG e0I5J1D+ENsck5MoHwJnkLk+Qb/nUVDzVFe/a9KhnG3qPHBzdSY+ClnPZ0u1PkvaE0dO Fnxr4yml5kXK0ci505j5uqkNsVOiRAYz6Sh3JCxkStVTJGArOdx3T+v5inWzrqfgJPU8 MvrQ== X-Forwarded-Encrypted: i=1; AJvYcCUf+wmRfXmwenssYGAyhtXCorDDrekm//2eHXCYG9SS1vl/d0LpO3m25lKL9IntX+zDa+pU43WrzmoR0ywZVJA=@vger.kernel.org, AJvYcCWmTYm7rc2rVld4DDazBe1RIYoqZysGGajLAcniFksSZuAR0Y3YrWaJ7//7a6+9g+kHA+ykxRI=@vger.kernel.org X-Gm-Message-State: AOJu0YyCfn3/vzryFI1KCQsWonE6/WphCNd7hcSwt24yqOSJfAixsgw5 sXgDViiG/Vzb7EjGU0c327c9Fv411isIsL0cnPdkyi3VTJH/vF1rMBcBwYcy X-Gm-Gg: ASbGncs6R/g1RcEAFsaKo/esBfZI+bsVRaLUe7WueQ8hIcx4T5VcbGyET0ctS+XaOqM eKz6IhjVwJ4rsAGG1GHX76+yx+y1lAg2IMvVnQoFE9rUKzWYoXAo+Wmpx/Pwbb+rhyzS2VNVJjF 8wz3GZX3JqmTbWZhL9yiHGxVpNR74xAM6RU2D8BEsGLDTFu/GX6A1CmcEt/bckzxu3iF2yJmxzq CC3N5qYsN2JBtMZo8+2ERTnh4UZ2rzlJ1jvP70SXM46GOZhABcHraMEGdRoZvtRzxzMZ7r1Esl1 OETvs/n0KPiNHGAwu07ehrIx/HQ+VEP8LxS8ZAPdGUSVghwpl4rHgs+DgVLqyI9kLgI= X-Google-Smtp-Source: AGHT+IE3S1jZXIXxjrEblsh5peZaZj1Io/lUfdB+7IROxH19yodlQGg/oLUmu8fFv+vpgDv7G2dO6A== X-Received: by 2002:a05:6a21:339e:b0:1ee:b583:1b44 with SMTP id adf61e73a8af0-1eee5c25481mr2634566637.4.1740035444412; Wed, 19 Feb 2025 23:10:44 -0800 (PST) Received: from mew.. (p3882177-ipxg22501hodogaya.kanagawa.ocn.ne.jp. [180.15.148.177]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-73242568af8sm13059672b3a.48.2025.02.19.23.10.36 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 19 Feb 2025 23:10:44 -0800 (PST) From: FUJITA Tomonori To: linux-kernel@vger.kernel.org Cc: Daniel Almeida , Boqun Feng , Gary Guo , Fiona Behrens , 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, 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, david.laight.linux@gmail.com Subject: [PATCH v11 4/8] rust: time: Introduce Instant type Date: Thu, 20 Feb 2025 16:06:06 +0900 Message-ID: <20250220070611.214262-5-fujita.tomonori@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20250220070611.214262-1-fujita.tomonori@gmail.com> References: <20250220070611.214262-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 Tested-by: Daniel Almeida Reviewed-by: Boqun Feng Reviewed-by: Gary Guo Reviewed-by: Fiona Behrens Signed-off-by: FUJITA Tomonori --- 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 Thu Feb 20 07:06:07 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: FUJITA Tomonori X-Patchwork-Id: 13983408 Received: from mail-pl1-f174.google.com (mail-pl1-f174.google.com [209.85.214.174]) (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 847361E5729; Thu, 20 Feb 2025 07:10:53 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.174 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1740035455; cv=none; b=fD5oUmE+Lj3SaH3XHaZe4zdvTv2yYpLucTos/8Lhdmf1iZ4UXDPMdx9gl6NdoYK6yt+EPwTCY1p9euT4C/N2JvWc3lc2c4cPAhyIBxmDX5k2/xCkYyootDmixJWyKFclgjFmEDxE2wr3xg6EY626kVjqDw5nMNWkYLsiqcBLKU4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1740035455; c=relaxed/simple; bh=/RRpGKVyayfJ/YcifSTuCj6GHejOlGyYDsd4DuurJ98=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=OJ+h0N+mMKagqwQbtCfL+IEk/VlfnVFCJzkblAaHqrysZoD0ys/OY5Ls8um73LMrqad9Pu0paSO4BMnJvo0fzGS8ltEx6K8wPWNWsHbGat+ML2Up8FoYmPGvX48FSa6b6OacNS/IfPWOPMjP12bT8TQl8w8iRiNN6z0pA4OxpjM= 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=JNuJvXrb; arc=none smtp.client-ip=209.85.214.174 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="JNuJvXrb" Received: by mail-pl1-f174.google.com with SMTP id d9443c01a7336-220e83d65e5so9806705ad.1; Wed, 19 Feb 2025 23:10:53 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1740035452; x=1740640252; 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=Nsssdj1VdtbuAV0DR/KXzdfz8e0FeahT0VjJxd+Xp6Q=; b=JNuJvXrb4TshnAJzw9CZcNu23BxGRQVBiQgVwZhkYpPsrTRt9d3/NfGy+lec7bOmjg mfKPHnAuMf9quJYZYy7cGGHFvERBBxu7u7NxE3WnoZkEEGMNi9kvpq8GayS4y0VkB3jV LYW8kyP975y4nRxKP7c2X7TkfAsyZQFLfSaeRVU99dptQI46ftbkT0tI3k6Di3DGnH3P L12BQR+P2dAryTgVEeumJ/Hlof3PZ/6TvtcPlYYbh4XlfbOAzPLpo4hqnJhP+d3LVuwr Th9deSSaRlL+e8/TDZfUXPNSIGBvj+1LqEmkBZUOMFB1aD0lxG3a+8dHUVkvcDcUowpC 0Wbg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1740035452; x=1740640252; 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=Nsssdj1VdtbuAV0DR/KXzdfz8e0FeahT0VjJxd+Xp6Q=; b=vVA//ZodruWN6Pbkgc1zCvxi4U+C1sUVUZ1l8JrT4pjuJhbdqh/Urj+NcuXRiB0uzK ArqquxjJxU+bzbSXnkexKI8rXD/+QQ3bbEj5wm9FEh2kRkLxKd/4QZgw6LsNyOR2NENQ N37J+7lJeca+F+u/kO6XpC/B1e3n+l2sDtZh0DW7kzbA5gl5oplROaQ/ZYJMrKmDBNoD nwCQ6Bub4ceAz71q+NZiKdP/A6aE7Iy3znk0p6KAb7olj+jKu/aX++AVSbm/s5zqo4Ym NTftZio0QauSm9b3sA7hqr2bvB/jA270APQyEFzHcn4BBuG3Ho1k2NW9BJ8n16BKPCbE 0B/Q== X-Forwarded-Encrypted: i=1; AJvYcCUtp+UMqRp67mt6nUqZII+WcLuDrqW8v/QI0Nyz5R2YVCmxwb5HkLGZ8pgK+YOcINsgXbwQOVjbw+5CL/6pZkY=@vger.kernel.org, AJvYcCXo6OE8L3qDKHdMpXu4ynjfyWJOZ5+Pk5/rEpO46wINiiBT3w6teyxVtElbhXMtcU717ngh3S4=@vger.kernel.org X-Gm-Message-State: AOJu0YzxRVnt2k2qsEQAGyRGi3fS8p/oZENM5QOfFUI2lISE/5tkBEM4 zdwzDPQIoZSwHh87CbWb/LUHbHOwodDhSPd/GyqlTQV70dXme9gxo3/BWgww X-Gm-Gg: ASbGncuKi7mV5binStDZXaEWMO2qllA0F8lAXPTD+ZFT0OTO8JlYqfHcana3JkllMIX w83/DWXCSSz+mNGj44JcJoW+bCLol0s6GjrAn2PZqh0jxQUAMyx6sspeQDlO3msdPIeSG2lDl+k F/VruGnUXYcU4mRpfJnv9S4UqxSphH+92yjnRqG1ltBPP60ayPjF1LQKhjeUs6Q5ZXLfN7/sTCx 6GFIpCjwltJ2dH9GY09yU3jSs1YNGWUJrjm9frGUAh5rEjb9mH/ncuZHnGf8JTzesE6Xb41xRO1 c9dsUsDOUFF573DiqlybTvavVozZfXp2P5p+SvfMJT3alCDZpP0KsduwIXtqQdW57/E= X-Google-Smtp-Source: AGHT+IE+k6dC2A4/3RTkXs+kV0CV5XWP+hMywbAJpwGF7oaJMZm9MPO0W9QhCPndxkYVluZqfSScFg== X-Received: by 2002:a05:6a00:e1a:b0:732:2170:b6a3 with SMTP id d2e1a72fcca58-7329df287c1mr10200372b3a.21.1740035452416; Wed, 19 Feb 2025 23:10:52 -0800 (PST) Received: from mew.. (p3882177-ipxg22501hodogaya.kanagawa.ocn.ne.jp. [180.15.148.177]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-73242568af8sm13059672b3a.48.2025.02.19.23.10.44 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 19 Feb 2025 23:10:52 -0800 (PST) From: FUJITA Tomonori To: linux-kernel@vger.kernel.org Cc: Daniel Almeida , Gary Guo , Alice Ryhl , Fiona Behrens , 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, 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, david.laight.linux@gmail.com Subject: [PATCH v11 5/8] rust: time: Add wrapper for fsleep() function Date: Thu, 20 Feb 2025 16:06:07 +0900 Message-ID: <20250220070611.214262-6-fujita.tomonori@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20250220070611.214262-1-fujita.tomonori@gmail.com> References: <20250220070611.214262-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] Tested-by: Daniel Almeida Reviewed-by: Gary Guo Reviewed-by: Alice Ryhl Reviewed-by: Fiona Behrens Signed-off-by: FUJITA Tomonori --- 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 0640b7e115be..9565485a1a54 100644 --- a/rust/helpers/helpers.c +++ b/rust/helpers/helpers.c @@ -31,6 +31,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 Thu Feb 20 07:06:08 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: FUJITA Tomonori X-Patchwork-Id: 13983409 Received: from mail-pl1-f182.google.com (mail-pl1-f182.google.com [209.85.214.182]) (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 478001E5B6F; Thu, 20 Feb 2025 07:11:00 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.182 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1740035462; cv=none; b=J1rRsliVzHyJd1uY/JEtGNEZGh1E0W/7SdjwLzjtY+yUBGDkFmpkPk3McxpFmUBnReosvtYqXJjklvadMcBQNLngTr8AfZcvjn1g/4APFVS6RNeiR2cyk6zcKzfaUjToLTDPDgUDI6hqsmPUwjY0jy2ZZ2qVFfUusqLZlv+zOWY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1740035462; c=relaxed/simple; bh=XYXF8zRIP8DOqitvSbGR8LPQL/Tde+3rrDrirNd5LyY=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=cmgKGvpVrAs+7FpenZdT0HJYBkLD3UnPcDp4uYN+7cGhSyRNO3YId7KNRYSvwnlkccis7ric8+9AgSj4OD93zgEF2Mltu8/BASdCk1A8pt6oxQ8uLw/ulWiyxwPgTujCIlBW3WVc6hgHh1JtuoGfwoq1dr6q7qf/UCJI/Cy22Ks= 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=QiXP+3jG; arc=none smtp.client-ip=209.85.214.182 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="QiXP+3jG" Received: by mail-pl1-f182.google.com with SMTP id d9443c01a7336-22185cddbffso20798295ad.1; Wed, 19 Feb 2025 23:11:00 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1740035460; x=1740640260; 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=FjrteS14+rIGkmlmoSem3GknyTnLu0IBn0cQP7SPAMQ=; b=QiXP+3jG1j4yZyWXpZ16cHGbxAMdZF7HKvRP4efgoY2AF45qCvkNoSDQU3EnyGVFQD mcndKzD5XbtSFIzuTSqJx8zw7eMyteEn6UcV/IFQ9z1+psEGH8duI+r0Ndoi8D+kmNkQ PVzWwdjOlQ4yzcL95Z1UeFWvZigsgKOJW/Tpql9bFFcSHOMQHu1RGph8nG1O5uBxnRqR kaYYtEfQtj69EGRL2hcM2fYLvF9N3FlOnMHN42t5/WYOreaTXi1SjiC2pUKovbvM+Fbd uq6zUzrfoIxi6SEtKkiqyDGGvdO+c2jOuiTdDDc57gj0dVU0/oY5tuS/O62epSf0J0I4 XYBg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1740035460; x=1740640260; 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=FjrteS14+rIGkmlmoSem3GknyTnLu0IBn0cQP7SPAMQ=; b=HCXMZ7ulclUP8j0IUXqS/i9uRD+mpQJNevvSgTZ5+1gg69GEECpYURF6hp7b6Fzyff 5Ikoa9EWr0azCqX5ecJDcNInUmtWiObCbt0R+ifEiRXQ+lFpLNlXEthsr4Esj0deXoIo PfBg+AYKri1z5mV3qkOkEJjMAekAVgeK+/YeNp6lBh1MkJDmlHINWqORtAWnTkAVySmb LfVeCwf2MvgcS0PW8YSXR+2/4RLeLnlH+W33rpTPff47qTKekJaxqeLFlFKKiuLvLPVK wn7pHk8wXBxhyxELdJ+cCpN7F9QFCFzX8vz9ijjyijRD8eax4c2jnf6Dje9O7r5+A7hW bo3A== X-Forwarded-Encrypted: i=1; AJvYcCUAtgWhnuyXZjuxZrxtDnLFVcYGtZ8lDy3Zb1FWTBS/dEp/O92WJ9ct611f7oEPdXGSAeHqg50=@vger.kernel.org X-Gm-Message-State: AOJu0YxcMeC8GFZTELzuxlWC3MKzetZuG43cZbslGoQpx2J2soMVe1d4 kyjvBYnDRLv9MQnD9ftZbPWhjJKrc+6+l4UiDgJLNmkRY0ssa2i7bL0zKnh0 X-Gm-Gg: ASbGncv0aoobScRgyWXJIHAJHOWB8BmVIqGoffcFwSH5vXL9fO0rnZKx0mQ5G5Qym5/ wl9WYW9sTC0mbz8XVpUDh9wZEpr+71HUq/D1NJU9diWWbxh6mxVycuMHb9jjVXBSbQwPmbN9uuA FtqncK0bURulyJ+x2CaJzTu0cJR6iAZdiw6JF5deh7JdjFJe7T6ggn4bhwOG4JhZb8mhCl4QuJ+ j/7tJwqVe/BJz8zm6JCRUH4bj7tozyXNHgebyrMk9yla2pCUHCQI9jKVoCYP23iA5M0s6TFtYbM EnnMNWGXkLnUwt8zhIcYQvv9YS9I3bxiIeGvLgf716G8DCUVC6D3TPRH7LrwexIBRPk= X-Google-Smtp-Source: AGHT+IF/olItWHKnhk7VHLJaDwihB2R/tPG3XfOIUMtj0D3NS7sTlawx/IoM+5mlh8oTLjj4JuAQVw== X-Received: by 2002:a05:6a20:7f8b:b0:1ee:d19c:45f9 with SMTP id adf61e73a8af0-1eee2f09d2dmr3516641637.19.1740035460239; Wed, 19 Feb 2025 23:11:00 -0800 (PST) Received: from mew.. (p3882177-ipxg22501hodogaya.kanagawa.ocn.ne.jp. [180.15.148.177]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-73242568af8sm13059672b3a.48.2025.02.19.23.10.52 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 19 Feb 2025 23:10:59 -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, me@kloenk.dev, david.laight.linux@gmail.com Subject: [PATCH v11 6/8] MAINTAINERS: rust: Add new sections for DELAY/SLEEP and TIMEKEEPING API Date: Thu, 20 Feb 2025 16:06:08 +0900 Message-ID: <20250220070611.214262-7-fujita.tomonori@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20250220070611.214262-1-fujita.tomonori@gmail.com> References: <20250220070611.214262-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 new sections for DELAY/SLEEP and TIMEKEEPING abstractions respectively. It was possible to include both abstractions in a single section, but following precedent, two sections were created to correspond with the entries for the C language APIs. Signed-off-by: FUJITA Tomonori --- MAINTAINERS | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/MAINTAINERS b/MAINTAINERS index c8d9e8187eb0..775ea845f011 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -10355,6 +10355,13 @@ F: kernel/time/timer_list.c F: kernel/time/timer_migration.* F: tools/testing/selftests/timers/ +DELAY AND SLEEP API [RUST] +M: FUJITA Tomonori +L: rust-for-linux@vger.kernel.org +L: linux-kernel@vger.kernel.org +S: Maintained +F: rust/kernel/time/delay.rs + HIGH-SPEED SCC DRIVER FOR AX.25 L: linux-hams@vger.kernel.org S: Orphan @@ -23854,6 +23861,13 @@ F: kernel/time/timekeeping* F: kernel/time/time_test.c F: tools/testing/selftests/timers/ +TIMEKEEPING API [RUST] +M: FUJITA Tomonori +L: rust-for-linux@vger.kernel.org +L: linux-kernel@vger.kernel.org +S: Maintained +F: rust/kernel/time.rs + TIPC NETWORK LAYER M: Jon Maloy L: netdev@vger.kernel.org (core kernel code) From patchwork Thu Feb 20 07:06:09 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: FUJITA Tomonori X-Patchwork-Id: 13983410 Received: from mail-pl1-f173.google.com (mail-pl1-f173.google.com [209.85.214.173]) (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 7FAB91E5B6F; Thu, 20 Feb 2025 07:11:09 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.173 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1740035471; cv=none; b=aa1WsLb2jTotfqPFwj+EqU8MiQeS6dENM9rL54o07CYF/EJbpMd7GCh9YlPdA8FhVeuh3m2TgwTGqgLExt7uVkNEMG/9kfKSFpdMtxk8UjXAxTiWHefeQWo7VI+CROmmUVQl0ZAlcC4q+n9ABQ4KW2nT/OHj7rAE3tf2Nwf8lHE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1740035471; c=relaxed/simple; bh=w4iGBvNOz+zD7SL3U+67uQljwCududwg6tU10OLcju4=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=TnxOsUH2Ku0ng6EUgg/cInvUl43SX9ds057tRfxePAuZU/03clyCPm+SAiZf0QMHTddcJaVJgdmHP9KfJLp5J+ynq8eTa4GI61HkoBdR6HXs/sEwpVA2w0wRhNiuASMCV5RIEJPX6hl1TrU1HSDT69cMxL4UTshXgt7g3qJaMYg= 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=kfki2ASe; arc=none smtp.client-ip=209.85.214.173 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="kfki2ASe" Received: by mail-pl1-f173.google.com with SMTP id d9443c01a7336-22128b7d587so9656135ad.3; Wed, 19 Feb 2025 23:11:09 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1740035468; x=1740640268; 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=t+MBuGWnj96pqN8g4XrjCHzKJT0duecX7mmWDcQ6My8=; b=kfki2ASe1uB1S4l95v1V9ym2hd7E4YOEq1v6lnys1uyrRWbKfzQZ5+mXTyx+85p7u7 CiClbrx7YoqH5eeN4zwkEVQrEFLCsHVpKCQ5abZdQ8l4Zn3R0YIHjluqCHK9NfGGSct9 S4oA5bef7MP8Myu3BebttG8n3r/wR4prEz3od78yv0Vpbg5brAQT2SeYSLQvEj6EsgCt aWqdcBHIp6Qn7EXcGXkLvKLWl3CV+TqI255gTuDB+Eu4baN7bx3wG5VWcIiCJo3UicBK 8qKwl8rB+S1Thpl1cDlgP6SGiptNHDZKHGCPE8b8pF0kAXv4h0AtHdEFu5JRPTSVBR3D 5zFg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1740035468; x=1740640268; 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=t+MBuGWnj96pqN8g4XrjCHzKJT0duecX7mmWDcQ6My8=; b=Vzuv3Sdet2tQPNqVpjAFkjG5rqaJSSDd9+yiUl7BdcW9/PZFGP6puwOaDoYY5HCUIt GvN10eTRYXpx7+M8C3ne0AM+tds7HX0sNcG2Btc+C6uL7Fj4DbcR5/Zvh2uEaOjGYkCP k19UTxjWS9ARvnkbGJy0yDO3w/8QNMtL4/ARFtNzQzkcymtHlLyzrcn/1e5WkjUsHbv9 WWo+IGKd30nYX/dofi8/8KsCNld7ri2eIvp69j+wpU3CzHXFMXvMwVeeqTT5NgF0TdFR 3+DaeCJxeWIQuJIiB0TA/mj0i17qDSWRGWfuxFhjQTUnwXZBHYRFYIrotnkvpIPhPIoH AZTg== X-Forwarded-Encrypted: i=1; AJvYcCUToYX1hBYKP+FbJupse2SCCD28PAMLeuZ62V+j1k8Ppf9z0lHaOWDsrlsntFiiQ82owZLzsms=@vger.kernel.org, AJvYcCUsc5XJeE7XpRQqOQcsI5qNwaAOCUh5UykJAhqtN2JlOI0/K3k0FkE9HDr85bChCBcoXPXOYlVB0hNHAiv4IY4=@vger.kernel.org X-Gm-Message-State: AOJu0YzHZjMqsqwZIpHwvmz3FDDUR1d7r+BWcV/zQXwJRONNF1MvQWcV J5EXmmz0rIfdX1VgSbHbai9h50xJ6KDaV3z7fAgR9xPugIMOatFySIMBk+6J X-Gm-Gg: ASbGncsT59Hm6oRD6/tBYPfIs5BE/+91in1qVAFRC7n8NlpGROjmf1o2Gmf9tRU6Z14 5xSlkJqlBUNbb3Io//joRyZHxrMGGLRyQVL2R5gWjQFespCvloabcVchIqRjBczPLBi+bJ6cmBT W8PqtvrvjHWks+jMnYdxhYk2H4aQ+jgQijkek8uywwQNTGxoAohkwnqworHfPrKh/cMoOQKwaTI r3Sgizh1HW0T47D67BHx4JljGbBKUFlzi5luqLjMTRpARyzPU6BOnsL1pp6WLomAk4ieKpF4J9d voZzMLFG86Vm8fzKfuJDr0VN2zNP/NEHjmTyd0ZehK8RqdNsRJgVeCiPcozogwew0qo= X-Google-Smtp-Source: AGHT+IFvFieXVEzkxe7DkLDLt58IN6h8T4TU6a3jR2yOsz3aSd18Bp/NvUa/gEsW6sY+pOCRgd5M5g== X-Received: by 2002:a05:6a21:6b11:b0:1ee:cfaa:f174 with SMTP id adf61e73a8af0-1eed505be52mr10573169637.42.1740035468382; Wed, 19 Feb 2025 23:11:08 -0800 (PST) Received: from mew.. (p3882177-ipxg22501hodogaya.kanagawa.ocn.ne.jp. [180.15.148.177]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-73242568af8sm13059672b3a.48.2025.02.19.23.11.00 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 19 Feb 2025 23:11:08 -0800 (PST) From: FUJITA Tomonori To: linux-kernel@vger.kernel.org Cc: Daniel Almeida , 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, me@kloenk.dev, david.laight.linux@gmail.com Subject: [PATCH v11 7/8] rust: Add read_poll_timeout functions Date: Thu, 20 Feb 2025 16:06:09 +0900 Message-ID: <20250220070611.214262-8-fujita.tomonori@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20250220070611.214262-1-fujita.tomonori@gmail.com> References: <20250220070611.214262-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. 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] Tested-by: Daniel Almeida Signed-off-by: FUJITA Tomonori Reviewed-by: Fiona Behrens --- rust/helpers/helpers.c | 1 + rust/helpers/kernel.c | 18 +++++++ rust/kernel/cpu.rs | 13 +++++ rust/kernel/error.rs | 1 + rust/kernel/io.rs | 2 + rust/kernel/io/poll.rs | 120 +++++++++++++++++++++++++++++++++++++++++ rust/kernel/lib.rs | 1 + 7 files changed, 156 insertions(+) create mode 100644 rust/helpers/kernel.c create mode 100644 rust/kernel/cpu.rs create mode 100644 rust/kernel/io/poll.rs diff --git a/rust/helpers/helpers.c b/rust/helpers/helpers.c index 9565485a1a54..16d256897ccb 100644 --- a/rust/helpers/helpers.c +++ b/rust/helpers/helpers.c @@ -14,6 +14,7 @@ #include "cred.c" #include "device.c" #include "err.c" +#include "kernel.c" #include "fs.c" #include "io.c" #include "jump_label.c" diff --git a/rust/helpers/kernel.c b/rust/helpers/kernel.c new file mode 100644 index 000000000000..f04c04d4cc4f --- /dev/null +++ b/rust/helpers/kernel.c @@ -0,0 +1,18 @@ +// 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); +} + +void rust_helper_might_resched(void) +{ + might_resched(); +} 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 index d4a73e52e3ee..be63742f517b 100644 --- a/rust/kernel/io.rs +++ b/rust/kernel/io.rs @@ -7,6 +7,8 @@ use crate::error::{code::EINVAL, Result}; use crate::{bindings, build_assert}; +pub mod poll; + /// Raw representation of an MMIO region. /// /// By itself, the existence of an instance of this structure does not provide any guarantees that diff --git a/rust/kernel/io/poll.rs b/rust/kernel/io/poll.rs new file mode 100644 index 000000000000..5977b2082cc6 --- /dev/null +++ b/rust/kernel/io/poll.rs @@ -0,0 +1,120 @@ +// 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}, +}; + +/// Polls periodically until a condition is met or a timeout is reached. +/// +/// The function repeatedly executes the given operation `op` closure and +/// checks its result using the condition closure `cond`. +/// If `cond` returns `true`, the function returns successfully with the result of `op`. +/// Otherwise, it waits for a duration specified by `sleep_delta` +/// before executing `op` again. +/// This process continues until either `cond` returns `true` or the timeout, +/// specified by `timeout_delta`, is reached. If `timeout_delta` is `None`, +/// polling continues indefinitely until `cond` evaluates to `true` or an error occurs. +/// +/// # Examples +/// +/// ```rust,ignore +/// fn wait_for_hardware(dev: &mut Device) -> Result<()> { +/// // The `op` closure reads the value of a specific status register. +/// let op = || -> Result { dev.read_ready_register() }; +/// +/// // The `cond` closure takes a reference to the value returned by `op` +/// // and checks whether the hardware is ready. +/// let cond = |val: &u16| *val == HW_READY; +/// +/// match read_poll_timeout(op, cond, Delta::from_millis(50), Some(Delta::from_secs(3))) { +/// Ok(_) => { +/// // The hardware is ready. The returned value of the `op`` closure isn't used. +/// Ok(()) +/// } +/// Err(e) => Err(e), +/// } +/// } +/// ``` +/// +/// ```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), Some(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: Option, +) -> Result +where + Op: FnMut() -> Result, + Cond: FnMut(&T) -> bool, +{ + let start = Instant::now(); + let sleep = !sleep_delta.is_zero(); + + if sleep { + might_sleep(); + } + + 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 let Some(timeout_delta) = timeout_delta { + if 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(); + } +} + +/// Annotation for functions that can sleep. +/// +/// Equivalent to the C side [`might_sleep()`], this function serves as +/// a debugging aid and a potential scheduling point. +/// +/// This function can only be used in a nonatomic context. +#[track_caller] +fn might_sleep() { + #[cfg(CONFIG_DEBUG_ATOMIC_SLEEP)] + { + let loc = core::panic::Location::caller(); + // SAFETY: FFI call. + unsafe { + crate::bindings::__might_sleep_precision( + loc.file().as_ptr().cast(), + loc.file().len() as i32, + loc.line() as i32, + ) + } + } + + // SAFETY: FFI call. + unsafe { crate::bindings::might_resched() } +} diff --git a/rust/kernel/lib.rs b/rust/kernel/lib.rs index 496ed32b0911..415c500212dd 100644 --- a/rust/kernel/lib.rs +++ b/rust/kernel/lib.rs @@ -40,6 +40,7 @@ pub mod block; #[doc(hidden)] pub mod build_assert; +pub mod cpu; pub mod cred; pub mod device; pub mod device_id; From patchwork Thu Feb 20 07:06:10 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: FUJITA Tomonori X-Patchwork-Id: 13983411 X-Patchwork-Delegate: kuba@kernel.org 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 39FD61EDA09; Thu, 20 Feb 2025 07:11:16 +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=1740035479; cv=none; b=AdgM9K4244b2UXP7vArEH9YsuloKONHT5b067/PrX+6NgQBjcrLNR7F245pAA04s6dMoREeA8GoTRSe95TCPrC8A2tmUbZ8oXDbWpilquGcW03Nf0vSO1qlUVPx1rTF2oJaPJEPwLHjAc3AnM6I74YMXeRp08gj+pZoX1McZMzk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1740035479; c=relaxed/simple; bh=p/LLNspP63XeDluK8n1bB3ApPN0HiknOfwCoEOeBPsY=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=WCskgST5Df3vaThuzD/gfImI7kq6BrdI/Od9QSpMvdTI7Os7Vwwa5GT0rH4O2X7rViTlmqyBo+CtAiGimkCKIgVDoHfecwFe7Vtf6UKLCZIY+8c+D0VK+fMHyD3sFosioQBfDKPpgXrNvakHeYRLNQGtjNRwDDk4bmTqmWRThmQ= 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=EmN+9b6E; 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="EmN+9b6E" Received: by mail-pl1-f170.google.com with SMTP id d9443c01a7336-220c8eb195aso11255395ad.0; Wed, 19 Feb 2025 23:11:16 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1740035476; x=1740640276; 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=R57PZ40vsMBhmQU5DVB3HkIVq/yRk+LSm6ONd5T9PSk=; b=EmN+9b6EhknPszgZ8D9UrMz4TVHxSBZCQSawDPdWCJPlBsw5ARhQ+Wt1Iy/FXA51HU IcFY/W1dPfPjytHxqJs2gZKiFPo+s179YzEZGjAnE5JhyRhVy9HVNYaGgsf1wUTqZ2ZR X6dznxFA9FalQkMN52C+9cE4WbEkF2tRHxbXTUcWJ7JlfXlHDZvfSipW4z+qt0ZTmEdU v8lDUjVx3bQ4SL909VoF3A3VbOh/2hKoplABHuaQNBM01P4IVG/5NCuJQxD9sS2Wccle 2vVkPsAwZkC14JLc9CDphMwWUJSnXgBxsgjImNC7ZVfquToCComzpTZrOAPZG71AvBPK 1lhA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1740035476; x=1740640276; 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=R57PZ40vsMBhmQU5DVB3HkIVq/yRk+LSm6ONd5T9PSk=; b=ZlmEcL+vXdAKphXkGEjsGptJx5PpqQLYE4qU40e26BN2s8wW/QPgSqz5X73+W13VmE jUSN0QHu7Le/FtoJJcFz1w9QjsIuvNFic+XTTgz2Jgyn9vHsAWq1t2UA4sry7ozih9p0 avGWE5F0UzjR7Wy7FOPsE8HIE0E+kZ3DnYLFrER9qTgiaWlWHQA6dvHdX71gH2Ncn5vF w/arcquT96fHEqfvb9dnYm6Rj+Dw8teVXK4GLDqXtbKITSQsHC9gN7zH6ujklHqy6sEd L6KFHNsXvioMHJbNso+mYhSjuyObETRR2oYqaBYdv34jP03pXtFKbQY1YtbCFZp7DRuF hOow== X-Forwarded-Encrypted: i=1; AJvYcCUmG4zlOUDSxUkq097GrTDi0T58W7e4jN8s3z8bOUNAyMhh7eupUeHWPLognZ0XHg7ROre468DEKB0glrH93yg=@vger.kernel.org, AJvYcCXqBRH/miP7POEEp9vq7VvL8+iVdTn/KubKIwvVyeoGMcDpyqOd+x0pnCR4V58ZOFE5sIxhsGw=@vger.kernel.org X-Gm-Message-State: AOJu0YySw0DucvtsEJe6yYinxXqt/9o602cXOf0oPh/88JL3UKcgxOrb CQ3W8eb0feq2ktJibEglA0vrI3KTBtUN+hc6ilQimWXMw8+QOOcktzZab4YP X-Gm-Gg: ASbGncvK7/cR5zMbVFKbav+4I0y3XLcpAPIiOVBMQDxy77HwnwCoI9QCN40a9KX33IR k6gvnPADMk1sDSds7uXH1khaZzpptNVZkBvGj1G/tinew05TGco+Rv4ur8Nc9E5BYEX9q3KHvFR m1VOxS0IFJ41bQRMDVJffesBvFPhgZH9YPzX549jDbOXsFt6iLYaiS/krgi5Mpj6bgUF5bqysW1 SgEj2vm2PxEoC9ZhuhMr4NLO2glwawgR43ViKspji9AGjt3oICQpSvUMR+JX6cugyGo7jVadOHi Fv52KvVuYHwtkg98hYG7MAzMmHiiXaZJYAL7KLeueRto1278aMm5Xr/FKP8W+1XEj/A= X-Google-Smtp-Source: AGHT+IGgvvsOvoTjyFLbSMVPkqFMMTB4aJyC8QIwojlxrUp5IaE62lLYCwUR5sCAQaovzeTxC/kKkg== X-Received: by 2002:a05:6a00:2443:b0:732:5651:e897 with SMTP id d2e1a72fcca58-7329de80994mr8185974b3a.11.1740035476247; Wed, 19 Feb 2025 23:11:16 -0800 (PST) Received: from mew.. (p3882177-ipxg22501hodogaya.kanagawa.ocn.ne.jp. [180.15.148.177]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-73242568af8sm13059672b3a.48.2025.02.19.23.11.08 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 19 Feb 2025 23:11:15 -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, me@kloenk.dev, david.laight.linux@gmail.com Subject: [PATCH v11 8/8] net: phy: qt2025: Wait until PHY becomes ready Date: Thu, 20 Feb 2025 16:06:10 +0900 Message-ID: <20250220070611.214262-9-fujita.tomonori@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20250220070611.214262-1-fujita.tomonori@gmail.com> References: <20250220070611.214262-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..cdf0540f0a98 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), + Some(Delta::from_secs(3)), + )?; + Ok(()) }