Received: by 2002:a05:6358:d09b:b0:dc:cd0c:909e with SMTP id jc27csp4386833rwb; Tue, 8 Nov 2022 16:38:16 -0800 (PST) X-Google-Smtp-Source: AMsMyM63R27raJ6WuBumqC6yOSdQcmE7NEt+NpDsTa5nXn7nqB07NARMInp4gy2foy8OEwJ80oPX X-Received: by 2002:a50:c31c:0:b0:461:f5ce:63fe with SMTP id a28-20020a50c31c000000b00461f5ce63femr1030044edb.362.1667954295865; Tue, 08 Nov 2022 16:38:15 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1667954295; cv=none; d=google.com; s=arc-20160816; b=t1Yk8YpgQZ4Um3HCv1xLuccR5Cz4HcW/ozS3MO8AqB5xq6pISd80q96s62rwKtOYgt YtZNqI/bEglxyKOVI5m02/NWBmsKnUMxA7303/1HsdVhLLGawdSam80+fNCrGO0w54RZ gAiVS4p8TCX+OqaNCu4T0bN1XV3yeT6UwkfomUgGk7ry542zc54/JlUFWTb6dX6NoWv9 O2yLSQzOYLP6sCa2RMhis4qBxcyX2I6DLCXacFIdqNWzWvw8DF3n2wCAjMOOdFwnjdXJ 5fm74oJyVbVYl/+7kbAWDp2qj4+d26M5mbfejl61OV78HLd14rJ66pmzSriXyIjdJMZf 7Rxg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:subject:message-id:date:from:in-reply-to :references:mime-version:dkim-signature; bh=TGJ6HG5WKwoNCJL7PTb+yaNm0cYpmcgcbrbERa+Cu3k=; b=rm15IoLprZSGDRrUMQxUnwTXwA5z9yIQMH9DkK6WpCMtB1nyEsOmXrMkwmXtiK9o9j ccNJ7aknOSSNf6TLfTHU3tOoeqg2ZNj5cyUJ7SeUQbUJF+O8RXFyaXA5lerKfAsUUGa6 m0n/0EFITdVKbIzldhvqXfz+0+QfQVoLVZsr+YhXC0IUiM5Iu8KcDg42/aMn670DjbG4 TbyXA32ZRaxgTo+/3ReKH19IQakruNLliiyh1qK/Ovlim5Zz9OqDoU29KzoTvQHGXcMy BQQzbLF1BrZFmSpm7b8aAPcOBD9vMxMWcK1vnM5lPD5QoBsPMW6RHsC4RnnqpRdvS0SM 3e/w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=T6pifDpy; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id f7-20020a056402354700b00461e426dcb0si16866192edd.609.2022.11.08.16.37.54; Tue, 08 Nov 2022 16:38:15 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=T6pifDpy; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229560AbiKIAae (ORCPT + 93 others); Tue, 8 Nov 2022 19:30:34 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35142 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229452AbiKIAad (ORCPT ); Tue, 8 Nov 2022 19:30:33 -0500 Received: from ams.source.kernel.org (ams.source.kernel.org [IPv6:2604:1380:4601:e00::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id DFAE01EC74 for ; Tue, 8 Nov 2022 16:30:31 -0800 (PST) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ams.source.kernel.org (Postfix) with ESMTPS id 9C080B81CC8 for ; Wed, 9 Nov 2022 00:30:30 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 4EC1EC43142 for ; Wed, 9 Nov 2022 00:30:29 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1667953829; bh=4U+Q54//+8Mboh9W6Q6zPTJ7xO2e6TEWWaIKq4egqf4=; h=References:In-Reply-To:From:Date:Subject:To:Cc:From; b=T6pifDpyDY9W/BL+dD3rbswhsVaUX1R6D4bPDtYnqoxioAPUe7Z2yIdJDv2C7CNR7 gey6571QstRyfw90KQzYoYezE74GTPls3RGqcoywjIbI5f48IPuuQ1pTosN/MmAPMt 30CTxEwePs9oFrGruIfhBKbfiNrkur/4Sx/6kzu6GQAUNRJNrlc1uI6LmwH0+EANLz pSF5R4hx23vPDyZi+SHAddMr/9tOPmkqbj32I+T6Tl/ZWZRqk1TPaQgVBvuc4WhDk/ AbZPllCfx7wBtFoxk11XVrRx9Kd6mccHTtJIoTNjZSqhxWKKy+QavRPbBE/PvfNG8K 7HPJGlGO+1YcA== Received: by mail-oa1-f51.google.com with SMTP id 586e51a60fabf-13d9a3bb27aso17325799fac.11 for ; Tue, 08 Nov 2022 16:30:29 -0800 (PST) X-Gm-Message-State: ACrzQf2cm83geYsCK8W2tQfq2LRzH1zSRjmYoIcd0QWFOlMJWJ5U5PPh /OiPq8sGPvdovQbsDxbKyA/4fEDz3GGxfdUJsFw= X-Received: by 2002:a05:6870:64a1:b0:13c:d09d:79f9 with SMTP id cz33-20020a05687064a100b0013cd09d79f9mr32769397oab.112.1667953828298; Tue, 08 Nov 2022 16:30:28 -0800 (PST) MIME-Version: 1.0 References: <20221108102044.3317793-1-guoren@kernel.org> In-Reply-To: From: Guo Ren Date: Wed, 9 Nov 2022 08:30:16 +0800 X-Gmail-Original-Message-ID: Message-ID: Subject: Re: [PATCH RESEND] riscv: asid: Fixup stale TLB entry cause application crash To: Conor Dooley Cc: linux-riscv@lists.infradead.org, anup@brainfault.org, paul.walmsley@sifive.com, palmer@dabbelt.com, conor.dooley@microchip.com, heiko@sntech.de, philipp.tomsich@vrull.eu, linux-kernel@vger.kernel.org, Guo Ren , Anup Patel , Palmer Dabbelt Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-7.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_HI, SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Tue, Nov 8, 2022 at 6:27 PM Conor Dooley wrote: > > > > On 8 November 2022 10:20:44 GMT, guoren@kernel.org wrote: > >From: Guo Ren > > > >After use_asid_allocator enabled, the userspace application will > >crash for stale tlb entry. Because only using cpumask_clear_cpu without > >local_flush_tlb_all couldn't guarantee CPU's tlb entries fresh. Then > >set_mm_asid would cause user space application get a stale value by > >the stale tlb entry, but set_mm_noasid is okay. > > > >Here is the symptom of the bug: > >unhandled signal 11 code 0x1 (coredump) > > 0x0000003fd6d22524 <+4>: auipc s0,0x70 > > 0x0000003fd6d22528 <+8>: ld s0,-148(s0) # 0x3fd6d92490 > >=> 0x0000003fd6d2252c <+12>: ld a5,0(s0) > >(gdb) i r s0 > >s0 0x8082ed1cc3198b21 0x8082ed1cc3198b21 > >(gdb) x/16 0x3fd6d92490 > >0x3fd6d92490: 0xd80ac8a8 0x0000003f > >The core dump file shows that the value of register s0 is wrong, but the > >value in memory is right. This is because 'ld s0, -148(s0)' use a stale > >mapping entry in TLB and got a wrong value from a stale physical > >address. > > > >When task run on CPU0, the task loaded/speculative-loaded the value of > >address(0x3fd6d92490), and the first version of tlb mapping entry was > >PTWed into CPU0's tlb. > >When the task switched from CPU0 to CPU1 without local_tlb_flush_all > >(because of asid), the task happened to write a value on address > >(0x3fd6d92490). It caused do_page_fault -> wp_page_copy -> > >ptep_clear_flush -> ptep_get_and_clear & flush_tlb_page. > >The flush_tlb_page used mm_cpumask(mm) to determine which CPUs need > >tlb flush, but CPU0 had cleared the CPU0's mm_cpumask in previous switch_mm. > >So we only flushed the CPU1 tlb, and setted second version mapping > >of the pte. When the task switch from CPU1 to CPU0 again, CPU0 still used a > >stale tlb mapping entry which contained a wrong target physical address. > >When the task happened to read that value, the bug would be raised. > > > >Fixes: 65d4b9c53017 ("RISC-V: Implement ASID allocator") > >Signed-off-by: Guo Ren > >Signed-off-by: Guo Ren > >Cc: Anup Patel > >Cc: Palmer Dabbelt > >--- > > arch/riscv/mm/context.c | 4 +++- > > 1 file changed, 3 insertions(+), 1 deletion(-) > > > >diff --git a/arch/riscv/mm/context.c b/arch/riscv/mm/context.c > >index 7acbfbd14557..8ad6c2493e93 100644 > >--- a/arch/riscv/mm/context.c > >+++ b/arch/riscv/mm/context.c > >@@ -317,7 +317,9 @@ void switch_mm(struct mm_struct *prev, struct mm_struct *next, > > */ > > cpu = smp_processor_id(); > > > >- cpumask_clear_cpu(cpu, mm_cpumask(prev)); > >+ if (!static_branch_unlikely(&use_asid_allocator)) > >+ cpumask_clear_cpu(cpu, mm_cpumask(prev)); > >+ > > cpumask_set_cpu(cpu, mm_cpumask(next)); > > > > set_mm(next, cpu); > > This is a completely different patch to what you already sent. Why have you marked it RESEND rather than v2? Okay, I should send v2. -- Best Regards Guo Ren