Received: by 2002:a05:6a10:22f:0:0:0:0 with SMTP id 15csp2213384pxk; Sat, 26 Sep 2020 22:39:18 -0700 (PDT) X-Google-Smtp-Source: ABdhPJz5PdvlDXInAumktCH16n6I3Oa61KcV6LISkMV3hWEX+lwZWFN1yJBixYqYhtjzJXJMuz1s X-Received: by 2002:aa7:d750:: with SMTP id a16mr9734824eds.362.1601185158756; Sat, 26 Sep 2020 22:39:18 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1601185158; cv=none; d=google.com; s=arc-20160816; b=FHUSo/nEENo2p0UnPYVs1vU561k/nIPMcuuN7M7oPYDwCf5dIig50ylHuLsntmulEw bYZA6jJtfmrcO4Igdx76juM7KEoMwZnw7K/arSrv0vKb6Fr9c2dZ9qXn1PvcTykXox/e HV3gBoUlgm4zP1Hx1l4SW5XqjmJ3xQMtVqIjyLecJIjq+n1/C2/J0zo27q9v+CvN+K90 Q4V0TY5afwhsH9as52elQ6e90Nnm9fbyi/LY97OlqZk6ZQFkuoY4XjyUu8Gb7KCiNAFR 1RiWngA7xAeJd6972uM/qXX6nXQmxJkkm3FKwDPJs9k2t1ZE05IoQulPXlcr7FgMSqFT Cm4A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :message-id:to:from:cc:in-reply-to:subject:date:dkim-signature; bh=6vQ7uwNEeNjn5nyRTVkXYl319JRvBv7r0RTNPxYsLpk=; b=pPjydvjyt+DKjY3oBf1iewzCDx6LRQ8MpIAbmvvnKzWEsQDxn30CkMQcWTei3KBLIX qj9ADBh53eSRhS2nT7kaRRN94eonIhrXoRriAi0Y7/uRmsAGeSg69SoTsmb1s8qyuPF7 +6HwHqdgVo8HR0HFYXhhpILpOUh/t/7QV5Aer6smxbc2qYdQvJ7pF+kSB7RL6ZFm9Pqp vxrF4I6dMourUi90pzeSA8rKFvakvjBi5uXlls11MduAgZKUwKcfF2P3Eju1cTJB/FCX bsCAcZiOeFF/GhAaCb9YLG5Ye0witaAE9G7i0FYGO8R8rRrXqA9U/bFhAOETtDluhhvM Zl2g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=iPFgK2kD; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id f2si5265271ejq.262.2020.09.26.22.38.55; Sat, 26 Sep 2020 22:39:18 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=iPFgK2kD; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1729291AbgI0Fh4 (ORCPT + 99 others); Sun, 27 Sep 2020 01:37:56 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47892 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726382AbgI0Fh4 (ORCPT ); Sun, 27 Sep 2020 01:37:56 -0400 Received: from mail-pj1-x1044.google.com (mail-pj1-x1044.google.com [IPv6:2607:f8b0:4864:20::1044]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id ED146C0613CE for ; Sat, 26 Sep 2020 22:37:55 -0700 (PDT) Received: by mail-pj1-x1044.google.com with SMTP id q4so1704188pjh.5 for ; Sat, 26 Sep 2020 22:37:55 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=date:subject:in-reply-to:cc:from:to:message-id:mime-version :content-transfer-encoding; bh=6vQ7uwNEeNjn5nyRTVkXYl319JRvBv7r0RTNPxYsLpk=; b=iPFgK2kD3/yis/Pwn1tUpOH+16ZnAkgwAT/Jz3e/qZnEnJlCedVu2XaiaDlXESfbWN AGN/Lzju647hK1UnAE2iUsS2qlduZYSv9syXu1VnpyJvJSSwAYQdFvHzeyOMHXdxJLSZ Wcu8cC7cpCkG19ahE0YROfWwPkZujPpeF67kyi1hmKJYPODsb6h9t7kG3tCXAXQ7VLo2 bNxdumt/86VL5mC+Fu2cOe7FjWyUNCYRiC5Fe7+DBLvIQ5wV/HE/QCi3yRWrtnD3AwwT tYfb6xVhRVqy46knrz6+yehDQVubZY7/vjsucYYdIf2dWqAP5pKADp+2OM31gsbYNOwY 0rVg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:subject:in-reply-to:cc:from:to:message-id :mime-version:content-transfer-encoding; bh=6vQ7uwNEeNjn5nyRTVkXYl319JRvBv7r0RTNPxYsLpk=; b=TcsUTm44aV8V7gXsfd/FxMTiOzoKzQqvjRkoJI+uf19LncgX0rlePcvzP1dwYJQTJi HEQvCur0tu9puE/a3Hu14FdtZdPdRqeTKLgaH+cC0tuNewPJ1Wbld/ltoJVVx6HwcGLC FFTvfAM58JbAcRWvtMeIk9gC2kjYu4/SjVLccALdMC6IPPp0qj9hYiruqbJ4heZaZiEA O2evqvboktc1xlSEznzoQ1DSbw6jnHxsgr3GWNGeR4tp8D1Kn21qYXpcT5Ph3YcXdKoe e9RAxSVvkDFZHzjXTeakuItlvHQ/YF5VWNcG3ieP9dd+bRJDT7JE4qxqhEgQ+sBVfye/ NxeQ== X-Gm-Message-State: AOAM5335YSMZoOkOPw8sNiujVal/NaGQ9iHRXt1kzUIDxLuLfqnmhhZA B1pTH9WQWDTd/LhOTb7gBjSGZm0M5FkPpY5B X-Received: by 2002:a17:90a:e02:: with SMTP id v2mr4324857pje.6.1601185074419; Sat, 26 Sep 2020 22:37:54 -0700 (PDT) Received: from localhost (76-210-143-223.lightspeed.sntcca.sbcglobal.net. [76.210.143.223]) by smtp.gmail.com with ESMTPSA id w185sm7477380pfc.36.2020.09.26.22.37.53 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 26 Sep 2020 22:37:53 -0700 (PDT) Date: Sat, 26 Sep 2020 22:37:53 -0700 (PDT) X-Google-Original-Date: Sat, 26 Sep 2020 22:37:51 PDT (-0700) Subject: Re: [PATCH v2] RISC-V: Check clint_time_val before use In-Reply-To: CC: Damien Le Moal , Paul Walmsley , Anup Patel , aou@eecs.berkeley.edu, linux-riscv@lists.infradead.org, Atish Patra , Alistair Francis , linux-kernel@vger.kernel.org From: Palmer Dabbelt To: anup@brainfault.org Message-ID: Mime-Version: 1.0 (MHng) Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Sat, 26 Sep 2020 22:35:39 PDT (-0700), anup@brainfault.org wrote: > On Sun, Sep 27, 2020 at 5:50 AM Palmer Dabbelt wrote: >> >> On Sat, 26 Sep 2020 03:31:29 PDT (-0700), Damien Le Moal wrote: >> > On Sat, 2020-09-26 at 15:51 +0530, Anup Patel wrote: >> >> The NoMMU kernel is broken for QEMU virt machine from Linux-5.9-rc6 >> >> because the get_cycles() and friends are called very early from >> >> rand_initialize() before CLINT driver is probed. To fix this, we >> >> should check clint_time_val before use in get_cycles() and friends. >> >> I don't think this is the right way to solve that problem, as we're essentially >> just lying about the timer rather than informing the system we can't get >> timer-based entropy right now. MIPS is explicit about this, I don't see any >> reason why we shouldn't be as well. >> >> Does this fix the boot issue (see below for the NULL)? There's some other >> random-related arch functions so this might not be quite the right way to do >> it. >> >> diff --git a/arch/riscv/include/asm/timex.h b/arch/riscv/include/asm/timex.h >> index 7f659dda0032..7e39b0068932 100644 >> --- a/arch/riscv/include/asm/timex.h >> +++ b/arch/riscv/include/asm/timex.h >> @@ -33,6 +33,18 @@ static inline u32 get_cycles_hi(void) >> #define get_cycles_hi get_cycles_hi >> #endif /* CONFIG_64BIT */ >> >> +/* >> + * Much like MIPS, we may not have a viable counter to use at an early point in >> + * the boot process. Unfortunately we don't have a fallback, so instead we >> + * just return 0. >> + */ >> +static inline unsigned long random_get_entropy(void) >> +{ >> + if (unlikely(clint_time_val == NULL)) >> + return 0; >> + return get_cycles(); >> +} >> + > > Overall, this approach is good but this change is incomplete so does not work. > > The linux/timex.h expects random_get_entropy() to be macro so we need a > "#define" as well. > > After fixing rand_initialize() with custom random_get_entropy(), we get another > issue in boot_init_stack_canary() because boot_init_stack_canary() directly > calls get_cycles() so we remove use of get_cycles() from > boot_init_stack_canary() > and this is similar to ARM, ARM64, and MIPS kernel. OK, are you going to send that patch or do you want me to? > > Regards, > Anup > >> #else /* CONFIG_RISCV_M_MODE */ >> >> static inline cycles_t get_cycles(void) >> >> >> Fixes: d5be89a8d118 ("RISC-V: Resurrect the MMIO timer implementation >> >> for M-mode systems") >> >> Signed-off-by: Anup Patel >> >> --- >> >> Changes since v1: >> >> - Explicitly initialize clint_time_val to NULL in CLINT driver to >> >> avoid hang on Kendryte K210 >> >> --- >> >> arch/riscv/include/asm/timex.h | 12 +++++++++--- >> >> drivers/clocksource/timer-clint.c | 2 +- >> >> 2 files changed, 10 insertions(+), 4 deletions(-) >> >> >> >> diff --git a/arch/riscv/include/asm/timex.h b/arch/riscv/include/asm/timex.h >> >> index 7f659dda0032..6e7b04874755 100644 >> >> --- a/arch/riscv/include/asm/timex.h >> >> +++ b/arch/riscv/include/asm/timex.h >> >> @@ -17,18 +17,24 @@ typedef unsigned long cycles_t; >> >> #ifdef CONFIG_64BIT >> >> static inline cycles_t get_cycles(void) >> >> { >> >> - return readq_relaxed(clint_time_val); >> >> + if (clint_time_val) >> >> + return readq_relaxed(clint_time_val); >> >> + return 0; >> >> } >> >> #else /* !CONFIG_64BIT */ >> >> static inline u32 get_cycles(void) >> >> { >> >> - return readl_relaxed(((u32 *)clint_time_val)); >> >> + if (clint_time_val) >> >> + return readl_relaxed(((u32 *)clint_time_val)); >> >> + return 0; >> >> } >> >> #define get_cycles get_cycles >> >> >> >> static inline u32 get_cycles_hi(void) >> >> { >> >> - return readl_relaxed(((u32 *)clint_time_val) + 1); >> >> + if (clint_time_val) >> >> + return readl_relaxed(((u32 *)clint_time_val) + 1); >> >> + return 0; >> >> } >> >> #define get_cycles_hi get_cycles_hi >> >> #endif /* CONFIG_64BIT */ >> >> diff --git a/drivers/clocksource/timer-clint.c b/drivers/clocksource/timer-clint.c >> >> index d17367dee02c..8dbec85979fd 100644 >> >> --- a/drivers/clocksource/timer-clint.c >> >> +++ b/drivers/clocksource/timer-clint.c >> >> @@ -37,7 +37,7 @@ static unsigned long clint_timer_freq; >> >> static unsigned int clint_timer_irq; >> >> >> >> #ifdef CONFIG_RISCV_M_MODE >> >> -u64 __iomem *clint_time_val; >> >> +u64 __iomem *clint_time_val = NULL; >> >> This one I definately don't get. According the internet, the C standard says >> >> If an object that has static storage duration is not initialized >> explicitly, it is initialized implicitly as if every member that has >> arithmetic type were assigned 0 and every member that has pointer type were >> assigned a null pointer constant. >> >> so unless I'm missing something there shouldn't be any difference between these >> two lines. When I just apply this I get exactly the same "objdump -dt" before >> and after. I do see some difference in assembly, but only when I don't pass >> "-fno-common" and that ends up being passed during my Linux builds. >> >> >> #endif >> >> >> >> static void clint_send_ipi(const struct cpumask *target) >> > >> > For Kendryte: >> > >> > Tested-by: Damien Le Moal >> > >> > -- >> > Damien Le Moal >> > Western Digital