From patchwork Tue Mar 19 14:59:29 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Anup Patel X-Patchwork-Id: 1913616 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (2048-bit key; secure) header.d=lists.infradead.org header.i=@lists.infradead.org header.a=rsa-sha256 header.s=bombadil.20210309 header.b=G3ZhPFP5; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=ventanamicro.com header.i=@ventanamicro.com header.a=rsa-sha256 header.s=google header.b=Rx45eCIV; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=none (no SPF record) smtp.mailfrom=lists.infradead.org (client-ip=2607:7c80:54:3::133; helo=bombadil.infradead.org; envelope-from=opensbi-bounces+incoming=patchwork.ozlabs.org@lists.infradead.org; receiver=patchwork.ozlabs.org) Received: from bombadil.infradead.org (bombadil.infradead.org [IPv6:2607:7c80:54:3::133]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4TzZdW2C7Nz1yXD for ; Wed, 20 Mar 2024 01:59:59 +1100 (AEDT) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:References:In-Reply-To: Message-Id:Date:Subject:Cc:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=Ik85va0by/Bea+ZnUP4c6Niqea0eJkXZfKPuDSs/JAs=; b=G3ZhPFP5Yyp80x r88Hfo7IXoWyxaLfiQ3Qs8hJH8NQoINtClgmgJNgfH0gWVgf8fHbxTL0K3w06kG3lA4Gi7bxOBbf2 Dh45Yb5gPd3Lm53JwC2jMNUsS/xePXGgQoPZ9wHxDAGun/TMt/DGi9dYGWDE6AOTVeH+MNu9vTgFy akoYLPjql1CyLZlsmjVZjbOKH3Fk64EILCCdDYft/mcMmCLgtuK6OywX529i5oVevjc2qfIPmR83L n3eks6Zf8gHGkWoakwfjgBWB/z1WonF7O8aZZe8+OsXzziZ/LMlan3C+uQcuU6TL7IJc/cXgMOSFK Va7c/4sdDqq+3uQLDYIQ==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1rmawQ-0000000D3l3-3occ; Tue, 19 Mar 2024 14:59:50 +0000 Received: from mail-oo1-xc2c.google.com ([2607:f8b0:4864:20::c2c]) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1rmawL-0000000D3gf-03Fb for opensbi@lists.infradead.org; Tue, 19 Mar 2024 14:59:49 +0000 Received: by mail-oo1-xc2c.google.com with SMTP id 006d021491bc7-5a21b920209so2271221eaf.1 for ; Tue, 19 Mar 2024 07:59:43 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ventanamicro.com; s=google; t=1710860383; x=1711465183; darn=lists.infradead.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=39e80/ThqKuoIWuVFJy8jC8AuKZyX2Ezwl+4t4L0dPs=; b=Rx45eCIVVGaG9puO4TJSZ0dVLm25/uA4ohui0nt01xkGWumPbyb4qEuGYfLeebdAwC ktUZt4nwwle3ls7x2F+FnTtGBzLc//Lei25fbF0sq4nnP3pMMbuu3nBTgmgzfAT9HCcN linxofsjqMqW6c5CCdrpfAKEAYYeeVcvsBhzJ9P87yVx0f5sMgaIoalf/qhn6G0R0/Me +FDMgFxYcEU6d4A1Fzymq9eifKCVWB2Mr+9Q9mwZxfz2mexVIeY1EUJLv9fQqrpQrJvk nD2DzP3vdIxbMKj7cy5pwYPhrQYNK+3CPI9/MTVv7mlv9fkCdombjNtm8ycvnHolTxl2 wJUA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1710860383; x=1711465183; 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=39e80/ThqKuoIWuVFJy8jC8AuKZyX2Ezwl+4t4L0dPs=; b=wb/bXh7Aml6EwXG8dkdgTUf4VYTGOFW341Vc3aZ+YFgPz3c7XcBYPzl1VmuYklYOHD qVmAbqplZnZdHDcg79iQCyP3sdAX4Rsv4lZHbuGIhRnp/mWsBNRiYU45B8RbBJbzn/MU beDwzq21+soxmjS8PS1VVMW8mywdATbEMFxKoI1C03NMKmK2sy06HmaCF1iPzDJPQ8y4 w3Ana/9/zUBvfZWZr9PuPYTeOhb8J0NLr4CQPQiDdvreeVTmPGlZ8jCGjyTRLpZBYF1j eslBCg8m577W4vcZ9S130pa0LQYBaBy7qB6XuAaiJJXFDevDPS/YuifobnHKC3uBB9R1 S4ng== X-Forwarded-Encrypted: i=1; AJvYcCUG1IPDXdGGOxUoW2eoqzeDWYGc2cS4x7w+DScOWbnpae1SoHB4H6bpMbjZLvZOsvQh3BJJK2QJZ6Z7NSp/MlrfgFExYG8KhXfaNfk= X-Gm-Message-State: AOJu0YyHfwqijwtAJ6d/F0a0GEoPvm4Om+mSZAqiAN/fbcrBn5mhtOXr N/Opx1k9gw6zb3T24lqsXJ+wZvCgTEdJTcrqXUllZSYvDo2fOzf/4a1bTean4wM= X-Google-Smtp-Source: AGHT+IFb4z2fvSnK9k9vVXwALwpo8E6Knkf+kKj64ymQsnaFHr6upwkHnBZSgY9OhOkq8aa75qVWCg== X-Received: by 2002:a05:6820:995:b0:5a4:d253:1a52 with SMTP id cg21-20020a056820099500b005a4d2531a52mr2882972oob.1.1710860382748; Tue, 19 Mar 2024 07:59:42 -0700 (PDT) Received: from anup-ubuntu-vm.localdomain ([103.97.165.210]) by smtp.gmail.com with ESMTPSA id cz8-20020a056820268800b005a4bb400a0fsm720113oob.4.2024.03.19.07.59.39 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 19 Mar 2024 07:59:42 -0700 (PDT) From: Anup Patel To: Atish Patra Cc: Andrew Jones , Anup Patel , opensbi@lists.infradead.org, Anup Patel Subject: [PATCH 1/2] lib: sbi: Simplify wait_for_coldboot() implementation Date: Tue, 19 Mar 2024 20:29:29 +0530 Message-Id: <20240319145930.1827176-2-apatel@ventanamicro.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240319145930.1827176-1-apatel@ventanamicro.com> References: <20240319145930.1827176-1-apatel@ventanamicro.com> MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240319_075945_152558_4DC65254 X-CRM114-Status: GOOD ( 16.07 ) X-Spam-Score: -0.2 (/) X-Spam-Report: Spam detection software, running on the system "bombadil.infradead.org", has NOT identified this incoming email as spam. The original message has been attached to this so you can view it or label similar future email. If you have any questions, see the administrator of that system for details. Content preview: On QEMU virt machine with large number of HARTs, some of the HARTs randomly fail to come out of wait_for_coldboot() due to one of the following race-conditions: 1) Failing HARTs are not able to acquire the coldboot_lock and update the coldboot_hartmask in wait_for_coldboot() before the coldboot HART acquires the coldboot_lock and sends IPI in wake_coldboot_ha [...] Content analysis details: (-0.2 points, 5.0 required) pts rule name description ---- ---------------------- -------------------------------------------------- -0.0 RCVD_IN_DNSWL_NONE RBL: Sender listed at https://www.dnswl.org/, no trust [2607:f8b0:4864:20:0:0:0:c2c listed in] [list.dnswl.org] -0.0 SPF_PASS SPF: sender matches SPF record 0.0 SPF_HELO_NONE SPF: HELO does not publish an SPF Record -0.1 DKIM_VALID_AU Message has a valid DKIM or DK signature from author's domain 0.1 DKIM_SIGNED Message has a DKIM or DK signature, not necessarily valid -0.1 DKIM_VALID Message has at least one valid DKIM or DK signature -0.1 DKIM_VALID_EF Message has a valid DKIM or DK signature from envelope-from domain -0.0 T_SCC_BODY_TEXT_LINE No description available. X-BeenThere: opensbi@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "opensbi" Errors-To: opensbi-bounces+incoming=patchwork.ozlabs.org@lists.infradead.org On QEMU virt machine with large number of HARTs, some of the HARTs randomly fail to come out of wait_for_coldboot() due to one of the following race-conditions: 1) Failing HARTs are not able to acquire the coldboot_lock and update the coldboot_hartmask in wait_for_coldboot() before the coldboot HART acquires the coldboot_lock and sends IPI in wake_coldboot_harts() hence the failing HARTs never receive IPI from the coldboot HART. 2) Failing HARTs acquire the coldbood_lock and update the coldboot_hartmask before coldboot HART does sbi_scratch_init() so the sbi_hartmask_set_hartid() does not update the coldboot_hartmask on the failing HARTs hence they never receive IPI from the coldboot HART. To address this, use a simple busy-loop in wait_for_coldboot() for polling on coldboot_done flag. Signed-off-by: Anup Patel --- include/sbi/riscv_barrier.h | 6 +++- lib/sbi/sbi_init.c | 70 ++----------------------------------- 2 files changed, 8 insertions(+), 68 deletions(-) diff --git a/include/sbi/riscv_barrier.h b/include/sbi/riscv_barrier.h index 1fba8b8..3d4a038 100644 --- a/include/sbi/riscv_barrier.h +++ b/include/sbi/riscv_barrier.h @@ -40,7 +40,11 @@ #define smp_wmb() RISCV_FENCE(w,w) /* CPU relax for busy loop */ -#define cpu_relax() asm volatile ("" : : : "memory") +#define cpu_relax() \ +do { \ + unsigned long __t; \ + __asm__ __volatile__ ("div %0, %0, zero" : "=r" (__t)); \ +} while (0) /* clang-format on */ diff --git a/lib/sbi/sbi_init.c b/lib/sbi/sbi_init.c index 796cccc..b3f3e38 100644 --- a/lib/sbi/sbi_init.c +++ b/lib/sbi/sbi_init.c @@ -10,7 +10,6 @@ #include #include #include -#include #include #include #include @@ -190,82 +189,19 @@ static void sbi_boot_print_hart(struct sbi_scratch *scratch, u32 hartid) sbi_hart_delegation_dump(scratch, "Boot HART ", " "); } -static spinlock_t coldboot_lock = SPIN_LOCK_INITIALIZER; -static struct sbi_hartmask coldboot_wait_hmask = { 0 }; - static unsigned long coldboot_done; static void wait_for_coldboot(struct sbi_scratch *scratch, u32 hartid) { - unsigned long saved_mie, cmip; - - if (__smp_load_acquire(&coldboot_done)) - return; - - /* Save MIE CSR */ - saved_mie = csr_read(CSR_MIE); - - /* Set MSIE and MEIE bits to receive IPI */ - csr_set(CSR_MIE, MIP_MSIP | MIP_MEIP); - - /* Acquire coldboot lock */ - spin_lock(&coldboot_lock); - - /* Mark current HART as waiting */ - sbi_hartmask_set_hartid(hartid, &coldboot_wait_hmask); - - /* Release coldboot lock */ - spin_unlock(&coldboot_lock); - - /* Wait for coldboot to finish using WFI */ - while (!__smp_load_acquire(&coldboot_done)) { - do { - wfi(); - cmip = csr_read(CSR_MIP); - } while (!(cmip & (MIP_MSIP | MIP_MEIP))); - } - - /* Acquire coldboot lock */ - spin_lock(&coldboot_lock); - - /* Unmark current HART as waiting */ - sbi_hartmask_clear_hartid(hartid, &coldboot_wait_hmask); - - /* Release coldboot lock */ - spin_unlock(&coldboot_lock); - - /* Restore MIE CSR */ - csr_write(CSR_MIE, saved_mie); - - /* - * The wait for coldboot is common for both warm startup and - * warm resume path so clearing IPI here would result in losing - * an IPI in warm resume path. - * - * Also, the sbi_platform_ipi_init() called from sbi_ipi_init() - * will automatically clear IPI for current HART. - */ + /* Wait for coldboot to finish */ + while (!__smp_load_acquire(&coldboot_done)) + cpu_relax(); } static void wake_coldboot_harts(struct sbi_scratch *scratch, u32 hartid) { - u32 i, hartindex = sbi_hartid_to_hartindex(hartid); - /* Mark coldboot done */ __smp_store_release(&coldboot_done, 1); - - /* Acquire coldboot lock */ - spin_lock(&coldboot_lock); - - /* Send an IPI to all HARTs waiting for coldboot */ - sbi_hartmask_for_each_hartindex(i, &coldboot_wait_hmask) { - if (i == hartindex) - continue; - sbi_ipi_raw_send(i); - } - - /* Release coldboot lock */ - spin_unlock(&coldboot_lock); } static unsigned long entry_count_offset;