Received: by 2002:a05:6358:3188:b0:123:57c1:9b43 with SMTP id q8csp5233075rwd; Sun, 4 Jun 2023 23:55:48 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ47KuxYjRih7UHVTSlAI8P2pYqkMxXEqBwv0RbH9kHkGm3awz0W7aG+MZmZ6RKVbnP2oiWJ X-Received: by 2002:a17:90a:6802:b0:255:fe76:d13c with SMTP id p2-20020a17090a680200b00255fe76d13cmr7838522pjj.15.1685948148242; Sun, 04 Jun 2023 23:55:48 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1685948148; cv=none; d=google.com; s=arc-20160816; b=dg5+kWN7OjN0CoQnM3AIyF10iWOCafuQv/1K+ea4wBp5YJLZU9sbCaivpngA/Oo2/H gJ7BHFzrehrvgwCdsahCSz/QfN5KcrQL5+h2ejYgBfKLPyv1NuJFLDq84d+I3EJbsHHp zwai6hBaO5hBAnZIvgcgsn/aK7RMmNgXWS13IGHEX/a6lNbOqvsgJwHt/t9bsID433Vy 8JxsU+tTjhPLUX+bX2HKCrZm8XOny6riE9bhnH99anmAqo1bZcsONkntVROUJ6HyJups idRfFfKNpivxR0/TkbPnw1RRo4SV3tCe/443Nwk61G94qfZTZJqUrx8oKRRjmtWeglPl vZfg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:references:in-reply-to:message-id:date:subject :cc:to:from; bh=0VmvYXdiDTzgw2uYgfnxwlGM4nQ0X3aPSdXnq57+8ao=; b=F72V/GH31PO2e2IL72N9CjznbrllJ0Ui1G+RYKtnnevc56G5XT2SRa52Z5Tlvb+N+L nKlfcw7ptHc+VXrpUZ4BrEmc1FQoowgz2z63ynuPWjv+xE1R2LwGCzxXXaQj6HQL0TGA bpPF0z2G+mpaVQjQjLmPVFTiFbo8PsFhY62VAjW6dvloJhUK5K2iLACg5Xmc1e7628VS r0pit6FqssOmf/BTFBE+NvsUdR1hdf7gWiLowNm1FgM2jj47NQmTYlmD+DonVAHM2cvz 8iVxm6UVe1112U5EaLopuW1lfCKS22rTOrSNXDwE526olc024CGzqHtPeSRCrdgbqn4S BC+A== ARC-Authentication-Results: i=1; mx.google.com; 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 26-20020a17090a031a00b002536b553afdsi6796031pje.98.2023.06.04.23.55.34; Sun, 04 Jun 2023 23:55:48 -0700 (PDT) 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; 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 S229459AbjFEGjh (ORCPT + 99 others); Mon, 5 Jun 2023 02:39:37 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52088 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229897AbjFEGjT (ORCPT ); Mon, 5 Jun 2023 02:39:19 -0400 Received: from mail.loongson.cn (mail.loongson.cn [114.242.206.163]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 4D118F0 for ; Sun, 4 Jun 2023 23:39:16 -0700 (PDT) Received: from loongson.cn (unknown [113.200.148.30]) by gateway (Coremail) with SMTP id _____8CxT_ATg31kAwYAAA--.171S3; Mon, 05 Jun 2023 14:39:15 +0800 (CST) Received: from linux.localdomain (unknown [113.200.148.30]) by localhost.localdomain (Coremail) with SMTP id AQAAf8Axa9YPg31kHQcAAA--.72S7; Mon, 05 Jun 2023 14:39:13 +0800 (CST) From: Tiezhu Yang To: Huacai Chen Cc: loongarch@lists.linux.dev, linux-kernel@vger.kernel.org, loongson-kernel@lists.loongnix.cn Subject: [PATCH v5 5/6] LoongArch: Check atomic instructions in insns_not_supported() Date: Mon, 5 Jun 2023 14:39:09 +0800 Message-Id: <1685947150-4949-6-git-send-email-yangtiezhu@loongson.cn> X-Mailer: git-send-email 2.1.0 In-Reply-To: <1685947150-4949-1-git-send-email-yangtiezhu@loongson.cn> References: <1685947150-4949-1-git-send-email-yangtiezhu@loongson.cn> X-CM-TRANSID: AQAAf8Axa9YPg31kHQcAAA--.72S7 X-CM-SenderInfo: p1dqw3xlh2x3gn0dqz5rrqw2lrqou0/ X-Coremail-Antispam: 1Uk129KBj93XoWxWw45Zw4xZF4rWryrKF13Jrc_yoWrurW3pF ZrZrn5Gr48W3Z3Cr9rtasa9r4Utw4ku390q3W3Z34xC347Xr15Jr1xKry7XFn8t395Kr4F qrW7Jr9Fva13JagCm3ZEXasCq-sJn29KB7ZKAUJUUUUr529EdanIXcx71UUUUU7KY7ZEXa sCq-sGcSsGvfJ3Ic02F40EFcxC0VAKzVAqx4xG6I80ebIjqfuFe4nvWSU5nxnvy29KBjDU 0xBIdaVrnRJUUU9jb4IE77IF4wAFF20E14v26r1j6r4UM7CY07I20VC2zVCF04k26cxKx2 IYs7xG6rWj6s0DM7CIcVAFz4kK6r1Y6r17M28lY4IEw2IIxxk0rwA2F7IY1VAKz4vEj48v e4kI8wA2z4x0Y4vE2Ix0cI8IcVAFwI0_Xr0_Ar1l84ACjcxK6xIIjxv20xvEc7CjxVAFwI 0_Gr0_Cr1l84ACjcxK6I8E87Iv67AKxVW8JVWxJwA2z4x0Y4vEx4A2jsIEc7CjxVAFwI0_ Gr0_Gr1UM2kKe7AKxVWUXVWUAwAS0I0E0xvYzxvE52x082IY62kv0487Mc804VCY07AIYI kI8VC2zVCFFI0UMc02F40EFcxC0VAKzVAqx4xG6I80ewAv7VC0I7IYx2IY67AKxVWUtVWr XwAv7VC2z280aVAFwI0_Gr0_Cr1lOx8S6xCaFVCjc4AY6r1j6r4UM4x0Y48IcxkI7VAKI4 8JMxAIw28IcxkI7VAKI48JMxC20s026xCaFVCjc4AY6r1j6r4UMxCIbckI1I0E14v26r1Y 6r17MI8I3I0E5I8CrVAFwI0_Jr0_Jr4lx2IqxVCjr7xvwVAFwI0_JrI_JrWlx4CE17CEb7 AF67AKxVWUAVWUtwCIc40Y0x0EwIxGrwCI42IY6xIIjxv20xvE14v26ryj6F1UMIIF0xvE 2Ix0cI8IcVCY1x0267AKxVW8JVWxJwCI42IY6xAIw20EY4v20xvaj40_Jr0_JF4lIxAIcV C2z280aVAFwI0_Gr0_Cr1lIxAIcVC2z280aVCY1x0267AKxVW8JVW8JrUvcSsGvfC2Kfnx nUUI43ZEXa7IU8EeHDUUUUU== X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,SPF_HELO_NONE, SPF_PASS,T_SCC_BODY_TEXT_LINE 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 Like llsc instructions, the atomic memory access instructions should not be supported for probing, check them in insns_not_supported(). Here is a simple example with CONFIG_UPROBE_EVENTS=y: # cat pthread.c #include #include #include static pthread_spinlock_t lock; static pthread_t t1, t2; static int count = 0; void *t1_start(void *arg) { for (int i = 0; i < 10000; i++) { pthread_spin_lock(&lock); count++; pthread_spin_unlock(&lock); } } void *t2_start(void *arg) { for (int i = 0; i < 20000; i++) { pthread_spin_lock(&lock); count++; pthread_spin_unlock(&lock); } } int main() { int ret; ret = pthread_spin_init(&lock, PTHREAD_PROCESS_PRIVATE); if (ret) return -1; ret = pthread_create(&t1, NULL, t1_start, NULL); if (ret) exit(1); ret = pthread_create(&t2, NULL, t2_start, NULL); if (ret) exit(1); pthread_join(t1, NULL); pthread_join(t2, NULL); pthread_spin_destroy(&lock); printf("%d\n", count); return 0; } # gcc pthread.c -o /tmp/pthread # objdump -d /lib64/libc.so.6 | grep -w "pthread_spin_lock" -A 5 | head -5 00000000000886a4 : 886a4: 0280040d addi.w $t1, $zero, 1(0x1) 886a8: 3869348c amswap_db.w $t0, $t1, $a0 886ac: 0040818c slli.w $t0, $t0, 0x0 886b0: 44003180 bnez $t0, 48(0x30) # 886e0 # cd /sys/kernel/debug/tracing # echo > uprobe_events # echo "p:myuprobe /lib64/libc.so.6:0x886a4" > uprobe_events Without this patch: # echo 1 > events/uprobes/enable # echo 1 > tracing_on # /tmp/pthread Trace/breakpoint trap (core dumped) With this patch: # echo 1 > events/uprobes/enable bash: echo: write error: Invalid argument Reported-by: Hengqi Chen Closes: https://lore.kernel.org/all/SY4P282MB351877A70A0333C790FE85A5C09C9@SY4P282MB3518.AUSP282.PROD.OUTLOOK.COM/ Signed-off-by: Tiezhu Yang Tested-by: Jeff Xie --- arch/loongarch/include/asm/inst.h | 26 ++++++++++++++++++++++++++ arch/loongarch/kernel/inst.c | 6 ++++++ arch/loongarch/kernel/uprobes.c | 9 +++++---- 3 files changed, 37 insertions(+), 4 deletions(-) diff --git a/arch/loongarch/include/asm/inst.h b/arch/loongarch/include/asm/inst.h index 01fb789..ea625c4 100644 --- a/arch/loongarch/include/asm/inst.h +++ b/arch/loongarch/include/asm/inst.h @@ -178,6 +178,32 @@ enum reg3_op { amord_op = 0x70c7, amxorw_op = 0x70c8, amxord_op = 0x70c9, + ammaxw_op = 0x70ca, + ammaxd_op = 0x70cb, + amminw_op = 0x70cc, + ammind_op = 0x70cd, + ammaxwu_op = 0x70ce, + ammaxdu_op = 0x70cf, + amminwu_op = 0x70d0, + ammindu_op = 0x70d1, + amswapdbw_op = 0x70d2, + amswapdbd_op = 0x70d3, + amadddbw_op = 0x70d4, + amadddbd_op = 0x70d5, + amanddbw_op = 0x70d6, + amanddbd_op = 0x70d7, + amordbw_op = 0x70d8, + amordbd_op = 0x70d9, + amxordbw_op = 0x70da, + amxordbd_op = 0x70db, + ammaxdbw_op = 0x70dc, + ammaxdbd_op = 0x70dd, + ammindbw_op = 0x70de, + ammindbd_op = 0x70df, + ammaxdbwu_op = 0x70e0, + ammaxdbdu_op = 0x70e1, + ammindbwu_op = 0x70e2, + ammindbdu_op = 0x70e3, fldgts_op = 0x70e8, fldgtd_op = 0x70e9, fldles_op = 0x70ea, diff --git a/arch/loongarch/kernel/inst.c b/arch/loongarch/kernel/inst.c index 1d7d579..ce25a63 100644 --- a/arch/loongarch/kernel/inst.c +++ b/arch/loongarch/kernel/inst.c @@ -135,6 +135,12 @@ void simu_branch(struct pt_regs *regs, union loongarch_instruction insn) bool insns_not_supported(union loongarch_instruction insn) { + switch (insn.reg3_format.opcode) { + case amswapw_op ... ammindbdu_op: + pr_notice("atomic memory access instructions are not supported\n"); + return true; + } + switch (insn.reg2i14_format.opcode) { case llw_op: case lld_op: diff --git a/arch/loongarch/kernel/uprobes.c b/arch/loongarch/kernel/uprobes.c index 3aa0046..3a56443 100644 --- a/arch/loongarch/kernel/uprobes.c +++ b/arch/loongarch/kernel/uprobes.c @@ -15,10 +15,11 @@ int arch_uprobe_analyze_insn(struct arch_uprobe *auprobe, if (addr & 0x3) return -EILSEQ; - insn.word = auprobe->insn[0]; - - if (insns_not_supported(insn)) - return -EINVAL; + for (int idx = ARRAY_SIZE(auprobe->insn) - 1; idx >= 0; idx--) { + insn.word = auprobe->insn[idx]; + if (insns_not_supported(insn)) + return -EINVAL; + } if (insns_need_simulation(insn)) { auprobe->ixol[0] = larch_insn_gen_nop(); -- 2.1.0