All of lore.kernel.org
 help / color / mirror / Atom feed
From: Ard Biesheuvel <ard.biesheuvel@linaro.org>
To: linux-efi@vger.kernel.org, Ingo Molnar <mingo@kernel.org>,
	Thomas Gleixner <tglx@linutronix.de>
Cc: Arnd Bergmann <arnd@arndb.de>,
	Ard Biesheuvel <ard.biesheuvel@linaro.org>,
	linux-kernel@vger.kernel.org
Subject: [PATCH 4/8] efi: cper: avoid using get_seconds()
Date: Wed, 11 Jul 2018 11:40:36 +0200	[thread overview]
Message-ID: <20180711094040.12506-5-ard.biesheuvel@linaro.org> (raw)
In-Reply-To: <20180711094040.12506-1-ard.biesheuvel@linaro.org>

From: Arnd Bergmann <arnd@arndb.de>

get_seconds() is deprecated because of the 32-bit time overflow
in y2038/y2106 on 32-bit architectures. The way it is used in
cper_next_record_id() causes an overflow in 2106 when unsigned UTC
seconds overflow, even on 64-bit architectures.

This starts using ktime_get_real_seconds() to give us more than 32 bits
of timestamp on all architectures, and then changes the algorithm to use
39 bits for the timestamp after the y2038 wrap date, plus an always-1
bit at the top. This gives us another 127 epochs of 136 years, with
strictly monotonically increasing sequence numbers across boots.

This is almost certainly overkill, but seems better than just extending
the deadline from 2038 to 2106.

Signed-off-by: Arnd Bergmann <arnd@arndb.de>
Signed-off-by: Ard Biesheuvel <ard.biesheuvel@linaro.org>
---
 drivers/firmware/efi/cper.c | 17 +++++++++++++++--
 1 file changed, 15 insertions(+), 2 deletions(-)

diff --git a/drivers/firmware/efi/cper.c b/drivers/firmware/efi/cper.c
index 3bf0dca378a6..b73fc4cab083 100644
--- a/drivers/firmware/efi/cper.c
+++ b/drivers/firmware/efi/cper.c
@@ -48,8 +48,21 @@ u64 cper_next_record_id(void)
 {
 	static atomic64_t seq;
 
-	if (!atomic64_read(&seq))
-		atomic64_set(&seq, ((u64)get_seconds()) << 32);
+	if (!atomic64_read(&seq)) {
+		time64_t time = ktime_get_real_seconds();
+
+		/*
+		 * This code is unlikely to still be needed in year 2106,
+		 * but just in case, let's use a few more bits for timestamps
+		 * after y2038 to be sure they keep increasing monotonically
+		 * for the next few hundred years...
+		 */
+		if (time < 0x80000000)
+			atomic64_set(&seq, (ktime_get_real_seconds()) << 32);
+		else
+			atomic64_set(&seq, 0x8000000000000000ull |
+					   ktime_get_real_seconds() << 24);
+	}
 
 	return atomic64_inc_return(&seq);
 }
-- 
2.17.1


  parent reply	other threads:[~2018-07-11  9:41 UTC|newest]

Thread overview: 23+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-07-11  9:40 [GIT PULL 0/8] EFI changes for v4.19 Ard Biesheuvel
2018-07-11  9:40 ` [PATCH 1/8] efi/x86: Clean up the eboot code Ard Biesheuvel
2018-07-15 23:36   ` [tip:efi/core] " tip-bot for Ingo Molnar
2018-07-11  9:40 ` [PATCH 2/8] efi/x86: Use non-blocking SetVariable() for efi_delete_dummy_variable() Ard Biesheuvel
2018-07-15 22:38   ` Ingo Molnar
2018-07-15 23:49     ` Prakhya, Sai Praneeth
2018-07-16  1:02       ` Ingo Molnar
2018-07-15 23:37   ` [tip:efi/core] " tip-bot for Sai Praneeth
2018-07-11  9:40 ` [PATCH 3/8] efi: Use a work queue to invoke EFI Runtime Services Ard Biesheuvel
2018-07-15 23:37   ` [tip:efi/core] " tip-bot for Sai Praneeth
2018-07-11  9:40 ` Ard Biesheuvel [this message]
2018-07-15 23:38   ` [tip:efi/core] efi/cper: Avoid using get_seconds() tip-bot for Arnd Bergmann
2018-07-11  9:40 ` [PATCH 5/8] efi: Remove the declaration of efi_late_init() as the function is unused Ard Biesheuvel
2018-07-15 23:38   ` [tip:efi/core] " tip-bot for Sai Praneeth
2018-07-11  9:40 ` [PATCH 6/8] efi/libstub/arm: add opt-in Kconfig option for the DTB loader Ard Biesheuvel
2018-07-15 23:39   ` [tip:efi/core] efi/libstub/arm: Add " tip-bot for Ard Biesheuvel
2018-07-11  9:40 ` [PATCH 7/8] efi: drop type and attribute checks in efi_mem_desc_lookup() Ard Biesheuvel
2018-07-15 23:39   ` [tip:efi/core] efi: Drop " tip-bot for Ard Biesheuvel
2018-07-11  9:40 ` [PATCH 8/8] fbdev/efifb: honour UEFI memory map attributes when mapping the fb Ard Biesheuvel
2018-07-15 23:40   ` [tip:efi/core] fbdev/efifb: Honour UEFI memory map attributes when mapping the FB tip-bot for Ard Biesheuvel
2019-04-20 19:02   ` [PATCH 8/8] fbdev/efifb: honour UEFI memory map attributes when mapping the fb James Hilliard
2019-04-23  6:50     ` Ard Biesheuvel
2019-04-23 12:21       ` James Hilliard

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20180711094040.12506-5-ard.biesheuvel@linaro.org \
    --to=ard.biesheuvel@linaro.org \
    --cc=arnd@arndb.de \
    --cc=linux-efi@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mingo@kernel.org \
    --cc=tglx@linutronix.de \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.