From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-alma10-1.taild15c8.ts.net [100.103.45.18]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id AEEFA2AD2C for ; Fri, 31 Jul 2026 00:40:21 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=100.103.45.18 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1785458422; cv=none; b=opvkQNx63waKUt72b9BVRy2oS4arH62NgYlSjZbLsqip/OGjSHBQVKfEPcJnCbFGP3TiiNlYAfQrXVxmaXfShvHtZa1sxpTC3xLZ0HLFIwjupfBbXlPfjhkRspV8zxz79kW+9I8JeqW9St8eeZVal0ynRio7gsw+MCV7a+6zq0g= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1785458422; c=relaxed/simple; bh=HutlKeMrXw0hL6wqEl1pQWHJ20Wog+wWcYM6wkQyKYY=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=SewOd2lhKcRwK+EYA0eZeYpJH/RLojjXcw1G9HdgM4gSSJtns8378EuF6QOKmbBPPSFF3rjtQqsF2vouL8+F2LyssAHACkeQ9J/9csMl+ynxXN5tH9DqQCmGxZnjQ47P188SZrKp2ELwljvbJIDHfnTmX9fglFhhAKRBCLsjgg8= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=XsL/tx2b; arc=none smtp.client-ip=100.103.45.18 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="XsL/tx2b" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 5CFBE1F000E9; Fri, 31 Jul 2026 00:40:21 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1785458421; bh=Xjj+PbRtvC3DBF14NmfH69JvLUq0xwJiX7NOHCu7abY=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=XsL/tx2bIzbvpHcfKAmIKW5aQSNEG1wbDPXvwrpmct12EfZFwEPA8rLQMMP38tIGU EBOL7lYCFL5v4n20pu+ZxmZ620RlR8WFFwo/SeGtm1NiTPSwZW6R7EiOCmDlas7KbJ TBC+BK964aBoe8Tcg7aY2tUk6vBtnFGH7oSi+eTgFU0/zC3aefbrHRwdCU4e7Bt+9+ QECcf1rVOLTCPKbwpKcciYIjyleO/NV6RU+igf8oXyDELjqtdN+kd5Bnnt3BCcEXNH YhkAmxPH7xaT9ZaWitRkhVWW0Vl+cdpkzLA4l8+Oo556EE9cYd/u23/KMfzlilQcMB +XGFXfe0F5ZxA== Received: by paulmck-ThinkPad-P17-Gen-1.home (Postfix, from userid 1000) id 349E6CE0F69; Thu, 30 Jul 2026 17:40:21 -0700 (PDT) From: "Paul E. McKenney" To: Anna-Maria Behnsen , Frederic Weisbecker , Thomas Gleixner Cc: "Peter Zijlstra (Intel)" , linux-kernel@vger.kernel.org, kernel-team@meta.com, "Paul E. McKenney" Subject: [PATCH RFC 1/9] hrtimer: Mark data-racy accesses to hrtimer_sleeper ->task field Date: Thu, 30 Jul 2026 17:40:11 -0700 Message-Id: <20260731004019.3530210-1-paulmck@kernel.org> X-Mailer: git-send-email 2.40.1 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit The hrtimer_sleeper structure's ->task field is used as a flag to indicate that the associated hrtimer has expired. This means that the hrtimer handler can be storing to this field while other code is loading from it to check for expiry. Note that additional races appear for hrtimers that can be restarted, which could be argued to be a user error. However, that is no reason to let the compiler introduce additional confusion. Therefore, mark data-racy accesses to the hrtimer_sleeper ->task field using READ_ONCE() (using a new hrtimer_sleeper_task_get() access function) and WRITE_ONCE() (using a new hrtimer_sleeper_task_set() access function). KCSAN located this issue. Signed-off-by: Paul E. McKenney Cc: Anna-Maria Behnsen Cc: Frederic Weisbecker Cc: Thomas Gleixner --- include/linux/hrtimer.h | 8 ++++++++ kernel/time/hrtimer.c | 14 +++++++------- 2 files changed, 15 insertions(+), 7 deletions(-) diff --git a/include/linux/hrtimer.h b/include/linux/hrtimer.h index 6862dea0acc52f..838ea7bce99c1d 100644 --- a/include/linux/hrtimer.h +++ b/include/linux/hrtimer.h @@ -350,6 +350,14 @@ extern int schedule_hrtimeout_range_clock(ktime_t *expires, const enum hrtimer_mode mode, clockid_t clock_id); extern int schedule_hrtimeout(ktime_t *expires, const enum hrtimer_mode mode); +static inline struct task_struct *hrtimer_sleeper_task_get(struct hrtimer_sleeper *sl) +{ + return READ_ONCE(sl->task); +} +static inline void hrtimer_sleeper_task_set(struct hrtimer_sleeper *sl, struct task_struct *t) +{ + WRITE_ONCE(sl->task, t); +} /* Soft interrupt function to run the hrtimer queues: */ extern void hrtimer_run_queues(void); diff --git a/kernel/time/hrtimer.c b/kernel/time/hrtimer.c index 313dcea127fe48..84ea341a6efcc4 100644 --- a/kernel/time/hrtimer.c +++ b/kernel/time/hrtimer.c @@ -2286,9 +2286,9 @@ void hrtimer_run_queues(void) static enum hrtimer_restart hrtimer_wakeup(struct hrtimer *timer) { struct hrtimer_sleeper *t = container_of(timer, struct hrtimer_sleeper, timer); - struct task_struct *task = t->task; + struct task_struct *task = hrtimer_sleeper_task_get(t); - t->task = NULL; + hrtimer_sleeper_task_set(t, NULL); if (task) wake_up_process(task); @@ -2317,7 +2317,7 @@ void hrtimer_sleeper_start_expires(struct hrtimer_sleeper *sl, enum hrtimer_mode /* If already expired, clear the task pointer and set current state to running */ if (!hrtimer_start_expires_user(&sl->timer, mode)) { - sl->task = NULL; + hrtimer_sleeper_task_set(sl, NULL); __set_current_state(TASK_RUNNING); } } @@ -2351,7 +2351,7 @@ static void __hrtimer_setup_sleeper(struct hrtimer_sleeper *sl, clockid_t clock_ } __hrtimer_setup(&sl->timer, hrtimer_wakeup, clock_id, mode); - sl->task = current; + hrtimer_sleeper_task_set(sl, current); } /** @@ -2395,17 +2395,17 @@ static int __sched do_nanosleep(struct hrtimer_sleeper *t, enum hrtimer_mode mod set_current_state(TASK_INTERRUPTIBLE|TASK_FREEZABLE); hrtimer_sleeper_start_expires(t, mode); - if (likely(t->task)) + if (likely(hrtimer_sleeper_task_get(t))) schedule(); hrtimer_cancel(&t->timer); mode = HRTIMER_MODE_ABS; - } while (t->task && !signal_pending(current)); + } while (hrtimer_sleeper_task_get(t) && !signal_pending(current)); __set_current_state(TASK_RUNNING); - if (!t->task) + if (!hrtimer_sleeper_task_get(t)) return 0; restart = ¤t->restart_block; -- 2.40.1