Received: by 2002:a05:7412:b10a:b0:f3:1519:9f41 with SMTP id az10csp14222rdb; Wed, 29 Nov 2023 18:03:43 -0800 (PST) X-Google-Smtp-Source: AGHT+IENHW8/Kh1QolSVuPpaJ+bsSZxRn34gZ5/KQHDkFh5vp9gF6GAyiKSr/mZa11tg4ayBi8Lq X-Received: by 2002:a05:6a20:42a3:b0:187:ce5a:2a90 with SMTP id o35-20020a056a2042a300b00187ce5a2a90mr29204340pzj.51.1701309823273; Wed, 29 Nov 2023 18:03:43 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1701309823; cv=none; d=google.com; s=arc-20160816; b=LQoCQ3X2rBr1Qnqm6SHYY3dNEDiXKrOauUqCsnzLfWQgYMIq+htENu3EWS483egzYS Ro2bskK/6/6o1KY3vYRZyqiZyZw633EuMaliJDg4OoW4eyUMRbDHPtsRCsPsDlNFy4kd 3kDLmaeh9roI5YM+N+BxT4wAkXNuo2SU0C0oxD7si99Up76IYwzqTnkevbyjSI4tNdN7 4bHRs5iueO2YUg8kx6Q00Hvgi3mi4WSFQ6nsbxi3tSvk2NgSKJa73RzkuW4LnA0aWm16 SEzdFGGYVvhNZxgZ89LV/zr6pMsXX4yIPNdPpvmuRCfqVl+B9jdhLRTHlYav7+wf+h3E XaiQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:to:subject:message-id:date:from:mime-version :dkim-signature; bh=CMHvFiMROSG+UECf9EDgEvz9utpLKQRQIUCySBf/XdM=; fh=UQxkOGvSEiB4VeB00BFPJDJp7X6KefLYr1xofxlGtRs=; b=KZuHwH25FHe5pG7CWUff4++caVi9/NPtNh+3rt/ietLCUGMs+SK/Sf/ZBTZsk4X+7G UEQ2gNbl0ASrWGA3seft3mMj8QKV42ivYnQzG1G38oKMqBr34LWiG0y8Ip9VNhpjpoVq Qd6fq0tDljSISyZZYcFQ24JRWkiN7o2A8/eSMCW3wZ4dhXFyASnTrQgLC0M9lzIetE9x PIzAZu1Fqnj0M5pQlZepAe2oyM+qPw7Gb30zetK2Mq3o2DYjENfKAQVNSNHd5fNamU56 q4ytrA7MU15CAtXQ+CjfrXZqGjXLMd31bkmDRbPcgm1exa1GJHpbkicaOJ+arqORebek 26Vw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@sifive.com header.s=google header.b=CWzQ1udr; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:4 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=sifive.com Return-Path: Received: from howler.vger.email (howler.vger.email. [2620:137:e000::3:4]) by mx.google.com with ESMTPS id q5-20020a056a00150500b006cb4d47cfa9si127647pfu.270.2023.11.29.18.03.42 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 29 Nov 2023 18:03:43 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:4 as permitted sender) client-ip=2620:137:e000::3:4; Authentication-Results: mx.google.com; dkim=pass header.i=@sifive.com header.s=google header.b=CWzQ1udr; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:4 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=sifive.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by howler.vger.email (Postfix) with ESMTP id B095380C7ACB; Wed, 29 Nov 2023 18:03:40 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at howler.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1343936AbjK3CDR (ORCPT + 99 others); Wed, 29 Nov 2023 21:03:17 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41184 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229658AbjK3CDR (ORCPT ); Wed, 29 Nov 2023 21:03:17 -0500 Received: from mail-io1-xd36.google.com (mail-io1-xd36.google.com [IPv6:2607:f8b0:4864:20::d36]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0656D1B3 for ; Wed, 29 Nov 2023 18:03:23 -0800 (PST) Received: by mail-io1-xd36.google.com with SMTP id ca18e2360f4ac-7b3b819f8a3so9212839f.1 for ; Wed, 29 Nov 2023 18:03:22 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sifive.com; s=google; t=1701309802; x=1701914602; darn=vger.kernel.org; h=to:subject:message-id:date:from:mime-version:from:to:cc:subject :date:message-id:reply-to; bh=CMHvFiMROSG+UECf9EDgEvz9utpLKQRQIUCySBf/XdM=; b=CWzQ1udrJIppsx2FCyEVGlvaGszNZBW8Tmt8FYCKNJwRc2A5mKWobPn/jbt8k10uYQ RuNGfFd4XY6IJsN3RDFMmyt1LeY9yk6Ig0/sMVC+rDdfpM8aEya09mmyVDQXSBB80rd5 +EigSKiIso8TY+Wt+AuFVQahud84VIPJ8Ccq1+HccUUHt+jnlngjtfEKYAYkdWfRYHg/ 19qba0x2KGrO6SACz8hPO3/eYkVLltItoUgaKn0cXF7SI3cTGfREBZoRUTazP8D//BMF 1ZyDHCDIqQYC98eZ4ovdorxcdfhOS74zJGZrsztbCmy60PmKmy1VoNwZGovEm+rcrAj0 9XnQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1701309802; x=1701914602; h=to:subject:message-id:date:from:mime-version:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=CMHvFiMROSG+UECf9EDgEvz9utpLKQRQIUCySBf/XdM=; b=MCG2jKVbVCeWhGEHV8A2nz7POEgpnX/B3OC1gOw/Yp+N83cWeu9WR9wk2ltlNxyzZc VErmWnLYMjj8b3o4kHEoVeT5yvvZHOXFVDoXqL8Zp3Dwa6oT8l47Y3DjLoD+FPoqk4y+ w8tL+uITMfJAsj8fMDhXEBHPusOOayeaLxs2S6jnRR/neI7jt/72j/9eOmsRVPu0Xhom WDYTi34E7j/27sBZCGmivsMPRCIr8t6WRfjxEYIAa1p8J62QGjMRv28FS2CWV6Oe3vvS 30kPDS8z75u+z+kkCyTs3W5Hr+Ma2l5ernZ7WjWmIkKLZil4jMLeEpPHMoRhnWdejw8X YIQQ== X-Gm-Message-State: AOJu0YwPCJFJLWos3izgDFMqG8+Q/ysJ3kHb0h5Xz9QTZW0J5Mw6laTO aDbTkZhvbOmgJBH9G93k0dvb6I2w/7MZvz7dxnTZZA== X-Received: by 2002:a05:6602:2285:b0:79f:dbec:ac4d with SMTP id d5-20020a056602228500b0079fdbecac4dmr21483885iod.19.1701309802205; Wed, 29 Nov 2023 18:03:22 -0800 (PST) MIME-Version: 1.0 From: Zong Li Date: Thu, 30 Nov 2023 10:03:11 +0800 Message-ID: Subject: Re: [PATCH] riscv: Invalid instruction cache after copy the xol area To: Palmer Dabbelt , linux-riscv , "linux-kernel@vger.kernel.org List" , Paul Walmsley , Greentime Hu Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-0.9 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on howler.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (howler.vger.email [0.0.0.0]); Wed, 29 Nov 2023 18:03:40 -0800 (PST) > On Wed, 18 May 2022 01:17:53 PDT (-0700), po-kai.chi@sifive.com wrote: > > We need to invalid the relevant instruction cache after > > copying the xol area, to ensure the changes takes effect. > > > > Signed-off-by: Po-Kai Chi > > --- > > arch/riscv/kernel/probes/uprobes.c | 8 ++++---- > > 1 file changed, 4 insertions(+), 4 deletions(-) > > > > diff --git a/arch/riscv/kernel/probes/uprobes.c b/arch/riscv/kernel/probes/uprobes.c > > index 7a057b5f0adc..9d52beeac73c 100644 > > --- a/arch/riscv/kernel/probes/uprobes.c > > +++ b/arch/riscv/kernel/probes/uprobes.c > > @@ -165,6 +165,7 @@ void arch_uprobe_copy_ixol(struct page *page, unsigned long vaddr, > > /* Initialize the slot */ > > void *kaddr = kmap_atomic(page); > > void *dst = kaddr + (vaddr & ~PAGE_MASK); > > + unsigned long addr = (unsigned long)dst; > > > > memcpy(dst, src, len); > > > > @@ -177,10 +178,9 @@ void arch_uprobe_copy_ixol(struct page *page, unsigned long vaddr, > > kunmap_atomic(kaddr); > > > > /* > > - * We probably need flush_icache_user_page() but it needs vma. > > - * This should work on most of architectures by default. If > > - * architecture needs to do something different it can define > > - * its own version of the function. > > + * Flush both I/D cache to ensure instruction modification > > + * takes effect. > > */ > > flush_dcache_page(page); > > + flush_icache_range(addr, addr + len); > > } > > This brings up a handful of issues: > > * This always inserts a 32-bit breakpoint, but that's not quite correct. > This should really be checking the _next_ instruction as well to > insert a 16-bit breakpoint if it's a 16-bit instruction as otherwise > userspace might jump into the middle of the breakpoint. > * These instructions can be concurrently executing, which relies on some > instruction fetch ordering that's very lightly specified. We don't > rely on that elsewhere (see stop_machine() in kprobes), but we > probably should. It's probably worth adding something to probe the HW > to make sure this is supported. > * Adding the icache flush defeats a uprobes advantage in that we'll now > be triggering remote execution (to do the remote fence.i). One option > could be to defer the fence and wait on it, but not sure if that's > sane and it'd likely require a lot of > > This also leaves a bit undefined WRT icache aliasing, at least in terms > of the API used. IMO it'd be > > diff --git a/arch/riscv/kernel/probes/uprobes.c b/arch/riscv/kernel/probes/uprobes.c > index 9d52beeac73c..c857346864fc 100644 > --- a/arch/riscv/kernel/probes/uprobes.c > +++ b/arch/riscv/kernel/probes/uprobes.c > @@ -165,7 +165,6 @@ void arch_uprobe_copy_ixol(struct page *page, unsigned long vaddr, > /* Initialize the slot */ > void *kaddr = kmap_atomic(page); > void *dst = kaddr + (vaddr & ~PAGE_MASK); > - unsigned long addr = (unsigned long)dst; > > memcpy(dst, src, len); > > @@ -179,8 +178,10 @@ void arch_uprobe_copy_ixol(struct page *page, unsigned long vaddr, > > /* > * Flush both I/D cache to ensure instruction modification > - * takes effect. > + * takes effect. We don't need to flush the whole icache, but that's > + * all RISC-V defines so rather than worry about aliasing this just > + * flushes everything. > */ > flush_dcache_page(page); > - flush_icache_range(addr, addr + len); > + flush_icache_all(); > } > > which will end up doing the same thing but avoids the ambiguity. I went > ahead and put this at palmer/riscv-uprobe_fencei with that and some > other minor things fixed up, LMK if that looks OK and I'll take it on > fixes. > Hi Palmer, Could I know if you have plans to include this fix in the upcoming RC versions? Thanks > Thanks!