From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (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 6535A12836D; Wed, 24 Jan 2024 14:29:27 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706106567; cv=none; b=BOjdG1f3KTyj+EhwzWNlCGqbdh7OAwKoZxXsohp/fVvPjCBtaTM0Op5wSDr7IA5DM1LvfwGM7MVsT5/UHjLtVlnElZMckgMdK4AjlkN+DRk2BlcB8t3mGZmj0f4wJVI/c/CfeAmnKMFt9RzruzHU8KpH5KCODklQF/xyVa5uYt4= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706106567; c=relaxed/simple; bh=0HoOYdpY7mCqkF6yX9oqUlAHYhcdLh1Mqx9QVhuN/ns=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=ieGiwLNVtiITgCq778S9n+90arxm5qDkJQ2rjd0jSWXzP9AVcxD63E+S7W8W6mwt2BcEHnczj5e7oAX3Jon8OcPWpkqyRbqHkPZUEfLD5Miaoatydk4amGCKhb5xO/3Bwysz0GGOAE0L1mWymcyU5MhGSinM3ee1GwiOcD0nidI= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=Eo7vvr+c; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="Eo7vvr+c" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 6E188C43141; Wed, 24 Jan 2024 14:29:25 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1706106567; bh=0HoOYdpY7mCqkF6yX9oqUlAHYhcdLh1Mqx9QVhuN/ns=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Eo7vvr+ckbTuBZat2RdWFJjHzIcP0SwYM1KZvHPgrEfBf7zFfOgH5WsdqsF+D0181 0Rn89k7MWFiPAn0WOOgzBXeKIgQdIhqJ5YSL5ixbzTcQu8G7uLcrFiIq//+i0gc4/r 3GUJOC1drJT6Kg5iCI1AzI2Uxrc3PhgJBrCTWQ23eJUzssoeJe9c6PXCQk4vP49zsD owTdleksy7oUCpd1tezRtQug4Plu65atsuG5KejaRxdpx5yPnONUIHeiGX+piDr+4T cBmvpw46e3xQISHuitZo2us19zLLSlWn5p2TX7lWTxdv96NrkN7emgUZHR5rbUQfON q8/X22xYxeLUA== From: Sasha Levin To: linux-kernel@vger.kernel.org, stable@vger.kernel.org Cc: Johannes Berg , Vincent Whitchurch , Richard Weinberger , Sasha Levin , anton.ivanov@cambridgegreys.com, johannes@sipsolutions.net, linux-um@lists.infradead.org Subject: [PATCH AUTOSEL 6.6 06/11] um: time-travel: fix time corruption Date: Wed, 24 Jan 2024 09:28:49 -0500 Message-ID: <20240124142907.1283546-6-sashal@kernel.org> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240124142907.1283546-1-sashal@kernel.org> References: <20240124142907.1283546-1-sashal@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-stable: review X-Patchwork-Hint: Ignore X-stable-base: Linux 6.6.13 Content-Transfer-Encoding: 8bit From: Johannes Berg [ Upstream commit abe4eaa8618bb36c2b33e9cdde0499296a23448c ] In 'basic' time-travel mode (without =inf-cpu or =ext), we still get timer interrupts. These can happen at arbitrary points in time, i.e. while in timer_read(), which pushes time forward just a little bit. Then, if we happen to get the interrupt after calculating the new time to push to, but before actually finishing that, the interrupt will set the time to a value that's incompatible with the forward, and we'll crash because time goes backwards when we do the forwarding. Fix this by reading the time_travel_time, calculating the adjustment, and doing the adjustment all with interrupts disabled. Reported-by: Vincent Whitchurch Signed-off-by: Johannes Berg Signed-off-by: Richard Weinberger Signed-off-by: Sasha Levin --- arch/um/kernel/time.c | 32 +++++++++++++++++++++++++++----- 1 file changed, 27 insertions(+), 5 deletions(-) diff --git a/arch/um/kernel/time.c b/arch/um/kernel/time.c index fddd1dec27e6..3e270da6b6f6 100644 --- a/arch/um/kernel/time.c +++ b/arch/um/kernel/time.c @@ -432,9 +432,29 @@ static void time_travel_update_time(unsigned long long next, bool idle) time_travel_del_event(&ne); } +static void time_travel_update_time_rel(unsigned long long offs) +{ + unsigned long flags; + + /* + * Disable interrupts before calculating the new time so + * that a real timer interrupt (signal) can't happen at + * a bad time e.g. after we read time_travel_time but + * before we've completed updating the time. + */ + local_irq_save(flags); + time_travel_update_time(time_travel_time + offs, false); + local_irq_restore(flags); +} + void time_travel_ndelay(unsigned long nsec) { - time_travel_update_time(time_travel_time + nsec, false); + /* + * Not strictly needed to use _rel() version since this is + * only used in INFCPU/EXT modes, but it doesn't hurt and + * is more readable too. + */ + time_travel_update_time_rel(nsec); } EXPORT_SYMBOL(time_travel_ndelay); @@ -568,7 +588,11 @@ static void time_travel_set_start(void) #define time_travel_time 0 #define time_travel_ext_waiting 0 -static inline void time_travel_update_time(unsigned long long ns, bool retearly) +static inline void time_travel_update_time(unsigned long long ns, bool idle) +{ +} + +static inline void time_travel_update_time_rel(unsigned long long offs) { } @@ -720,9 +744,7 @@ static u64 timer_read(struct clocksource *cs) */ if (!irqs_disabled() && !in_interrupt() && !in_softirq() && !time_travel_ext_waiting) - time_travel_update_time(time_travel_time + - TIMER_MULTIPLIER, - false); + time_travel_update_time_rel(TIMER_MULTIPLIER); return time_travel_time / TIMER_MULTIPLIER; } -- 2.43.0