Received: by 2002:a05:6358:d09b:b0:dc:cd0c:909e with SMTP id jc27csp1824655rwb; Thu, 8 Dec 2022 16:05:50 -0800 (PST) X-Google-Smtp-Source: AA0mqf48XdwsNvOiEy/bQ0jlQVdmotk2t9hbIa0CCGA2kxFIlLOkdBmknQJ29mOFOZE+r2LbXo1m X-Received: by 2002:a05:6a21:3397:b0:9d:efbf:48d5 with SMTP id yy23-20020a056a21339700b0009defbf48d5mr6595370pzb.25.1670544350200; Thu, 08 Dec 2022 16:05:50 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1670544350; cv=none; d=google.com; s=arc-20160816; b=UAPXi8H5FYr+FvViONOkITf2LOiCVpSFJHgAjaVmSk/ATo7ciAltRA30hOYsjWSi7n 91X8NYUV5bj4HxMgNFae732bPDoxrj1csr3Mt69HcNdtQIcGKVmp6xizmwvBT2Kh9Wvr tOXJCaL+iyOE35oDdNRVSgt9HG//q+PeZmVKtpaq3yrgWl8adWTzbi8QhKhK3aEz56I+ pm+wnCpBPJ9hfXzA7uJkhueIxU32TDm3unbbvtR126jp8rH4u2mgW+QSy+HqWVWnbTwq 6RcP4ThMo1U1QXERn6oZqjbilGoJbnyoiIJsIGUf9g6O1GOVeaMAoN8f/DkeLPXgbwGW 4AgA== 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=9xjcdkPHdm6uKkgcgf2wZxDGHKg/K8iOgm6bCXEn/oQ=; b=H3RmNSbXxWfmVKNlaoKcdNLUJmQ56fg6bYZfD4G03zrFjlXWeDwSXU2Dy0v9C990ku zQG5KtXP4KwHoNDx/9li+m5wUfatoPcaVuWveSFteFGeCOdZ28+RARz/+j5DTsv6JeRB RjSc+N6RPehaaF5TqGkoW+AZuwO5GDJqczNp5Wbdrx9qpef41bafzjAhsORU3RE3ctBn QH8y4sYHPlWdjmZ8m9Dq3LTDDjLGenifk2sk97q5utwXC3u21XwyB3XKJZ/XwEKM6nNp Svr4iz2ZBApfYyqUcEyJT2oFxSTNwwGHLxbD272jG6xMFDN5Sz1swyFXvQ6WUbdf24oH N2SA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@rivosinc-com.20210112.gappssmtp.com header.s=20210112 header.b="rb5booq/"; 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 Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id p1-20020a635b01000000b00477b64d8e5csi25125632pgb.160.2022.12.08.16.05.40; Thu, 08 Dec 2022 16:05:50 -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=@rivosinc-com.20210112.gappssmtp.com header.s=20210112 header.b="rb5booq/"; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229735AbiLHXaR (ORCPT + 73 others); Thu, 8 Dec 2022 18:30:17 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41164 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229660AbiLHXaO (ORCPT ); Thu, 8 Dec 2022 18:30:14 -0500 Received: from mail-pg1-x52b.google.com (mail-pg1-x52b.google.com [IPv6:2607:f8b0:4864:20::52b]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 746A86ACED for ; Thu, 8 Dec 2022 15:30:09 -0800 (PST) Received: by mail-pg1-x52b.google.com with SMTP id r18so2383339pgr.12 for ; Thu, 08 Dec 2022 15:30:09 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20210112.gappssmtp.com; s=20210112; h=content-transfer-encoding:mime-version:message-id:to:from:cc :in-reply-to:subject:date:from:to:cc:subject:date:message-id :reply-to; bh=9xjcdkPHdm6uKkgcgf2wZxDGHKg/K8iOgm6bCXEn/oQ=; b=rb5booq/HHdSewBCekfBW2j8iIo4ZxuYd7rba/kPyLbrSTVQ/N+gk8E8V90ISLagt8 43UERn2GsQVbsvToJftGQMxF01nC8iLFbvTjt2YthHiu3RCEHoga3QFwA29gjNEdUxDa JFFyD2YYXauV+1tjc6UoWjtZWR8TKqzSiFae91cjyhBF3c2XgraZTyxU8MNxGRVpy8kr 2u2QrGtPwQTk9tZamvfsyPStS/ootU8voYx9kWfrBxHa7aQZ3z+oUTSA4tyuI5pdBXVK jyKSEEbm07aXR9ii2qXKosDYfcc6OfcSkmTHMYWf49aGsqjXQ2fDRZYQkWLEvLIqu2y/ qtUQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:message-id:to:from:cc :in-reply-to:subject:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=9xjcdkPHdm6uKkgcgf2wZxDGHKg/K8iOgm6bCXEn/oQ=; b=OLopxjV4Q6yhax52H3XKEcodRElWOBFaBeXb3JyvhTS276bOSN8Z/wFlSG/Kv2oqRb GjKJH8JBKVfS8P6doSC3hHIWCoHO9SCfVxVbuMRYZ+8dqjki8iZrJ+r+Byf8ttWiQdjt mbieeRLXgf6MNvdI5/Iq36ySZFTK751JMesHAFPH/5kSimh41gXcstHlqgjyDPqcmm0u C7sFqK3KxZr2FtvnamqivK9+MG/W2BuQKsUEzi0t7S66jZ3NGZMsRR1plmvjpoZFavzL DO42BcvmeQU9io45dQk4rTe16FvoioTXPldIcxJMd1nBNig22HuYbIxNsW8yiqyjr0dd /RCg== X-Gm-Message-State: ANoB5pllJVRHZ+Z/BZayM+DGey2hQjnHoyVKBlRsLDu8Iw2JTPKGfSSe 0/qIN3Nc4kR1BI41WcxggcWdAA== X-Received: by 2002:aa7:9555:0:b0:576:c9a1:ec35 with SMTP id w21-20020aa79555000000b00576c9a1ec35mr4679501pfq.17.1670542208857; Thu, 08 Dec 2022 15:30:08 -0800 (PST) Received: from localhost ([50.221.140.188]) by smtp.gmail.com with ESMTPSA id c2-20020aa79522000000b00575acb243besm52081pfp.1.2022.12.08.15.30.07 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 08 Dec 2022 15:30:08 -0800 (PST) Date: Thu, 08 Dec 2022 15:30:08 -0800 (PST) X-Google-Original-Date: Thu, 08 Dec 2022 15:29:52 PST (-0800) Subject: Re: [PATCH V3] riscv: asid: Fixup stale TLB entry cause application crash In-Reply-To: CC: guoren@kernel.org, anup@brainfault.org, Paul Walmsley , Conor Dooley , heiko@sntech.de, philipp.tomsich@vrull.eu, alex@ghiti.fr, Christoph Hellwig , ajones@ventanamicro.com, gary@garyguo.net, jszhang@kernel.org, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, guoren@linux.alibaba.com, apatel@ventanamicro.com From: Palmer Dabbelt To: geomatsi@gmail.com Message-ID: Mime-Version: 1.0 (MHng) Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,RCVD_IN_DNSWL_NONE,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 Fri, 18 Nov 2022 12:57:21 PST (-0800), geomatsi@gmail.com wrote: > Hi Guo Ren, > > >> After use_asid_allocator is enabled, the userspace application will >> crash by stale TLB entries. Because only using cpumask_clear_cpu without >> local_flush_tlb_all couldn't guarantee CPU's TLB entries were fresh. >> Then set_mm_asid would cause the user space application to get a stale >> value by stale TLB entry, but set_mm_noasid is okay. > > ... [snip] > >> + /* >> + * The mm_cpumask indicates which harts' TLBs contain the virtual >> + * address mapping of the mm. Compared to noasid, using asid >> + * can't guarantee that stale TLB entries are invalidated because >> + * the asid mechanism wouldn't flush TLB for every switch_mm for >> + * performance. So when using asid, keep all CPUs footmarks in >> + * cpumask() until mm reset. >> + */ >> + cpumask_set_cpu(cpu, mm_cpumask(next)); >> + if (static_branch_unlikely(&use_asid_allocator)) { >> + set_mm_asid(next, cpu); >> + } else { >> + cpumask_clear_cpu(cpu, mm_cpumask(prev)); >> + set_mm_noasid(next); >> + } >> } > > I observe similar user-space crashes on my SMP systems with enabled ASID. > My attempt to fix the issue was a bit different, see the following patch: > > https://lore.kernel.org/linux-riscv/20220829205219.283543-1-geomatsi@gmail.com/ > > In brief, the idea was borrowed from flush_icache_mm handling: > - keep track of CPUs not running the task > - perform per-ASID TLB flush on such CPUs only if the task is switched there That way looks better to me: leaking hartids in the ASID allocator might make the crashes go away, but it's just going to end up trending towards flushing everything and that doesn't seem like the right long-term solution. So I've got that one on for-next, sorry I missed it before. Thanks! > > Your patch also works fine in my tests fixing those crashes. I have a > question though, regarding removed cpumask_clear_cpu. How CPUs no more > running the task are removed from its mm_cpumask ? If they are not > removed, then flush_tlb_mm/flush_tlb_page will broadcast unnecessary > TLB flushes to those CPUs when ASID is enabled. > > Regards, > Sergey