Received: by 2002:a05:6358:d09b:b0:dc:cd0c:909e with SMTP id jc27csp2525385rwb; Fri, 9 Dec 2022 03:18:43 -0800 (PST) X-Google-Smtp-Source: AA0mqf5l8a0yP0iWDg47UtJKI1R3IQdSITeEbfjySCY79d4xgH4ylykFodvNqcdJSD8Wzc4Zp7S0 X-Received: by 2002:a17:906:a206:b0:78d:f455:3104 with SMTP id r6-20020a170906a20600b0078df4553104mr4871446ejy.44.1670584723572; Fri, 09 Dec 2022 03:18:43 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1670584723; cv=none; d=google.com; s=arc-20160816; b=ft7RtEOvhD7/6vmsvZHdXb051XiBw+uxoQhqcMCsLRexeLCr7l2lfdVJFuouq0Tury uFKDm80PppzFx99IlZROTamUGcI526QapeVHfp0Kdiyzv8Xv7WK/MCheOgL6W6NwTcwY 8CIZWWZdRvAQUgOoxwK5wdRAsqs7Zy2H3yt2Iqyg1p73TDOuvQsLgvrsknSBDxKJxFiw azynA3YLXF66b3QnxIuGqSPb6fEvypf3H0jMBYzyXA+ustEvDE0Pv0IFoGmYdsND81CT qI2ny5kzmsPPEfGVvVPiRwdAN/VZ8PcfcPGkDzRXur1HrVWwHNiyZt/6ZnluYPcP9ht9 z6Vg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date; bh=jFH1R2RHckZpNQ2dC8azS1l5FBwAsk6TRceaw7sP6+k=; b=Xpj2WuzC557XKlpZVMMTfd0MkXh0EJ1j7/Ma0c4cyOsE8qn7/lkDOEdpKlaqHGbFpa bNuAgeuuOcpDkYCoXVpnooN+ht/wXbZfaP4PhmWsEL8aOJuYy1P7o/Xa0Mjl2YMNzS5L VPXBLyZnM1Txlt3GMiv8afXC5Sp9jr8Icpi1/o6rzsmX6eTBHsvw6WmXa+HCA2Q9tYQH u3IX87p/JSrIAGbpCVcOG+IB9wcPOqYaZNrJMaS85m+6kKv3fBOY4mZMNYOD+lQrBCej 96pgSV/TnvxoEeWgd4U6S2X48c6w4QqCOcPwsa2rA7gCY4W5a7eRMiDJqVeH66wydFZm EgTw== 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; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=arm.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id qa12-20020a170907868c00b007c1252d6e27si1051659ejc.79.2022.12.09.03.18.26; Fri, 09 Dec 2022 03:18:43 -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; 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=fail (p=NONE sp=NONE dis=NONE) header.from=arm.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229840AbiLILJ5 (ORCPT + 74 others); Fri, 9 Dec 2022 06:09:57 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39386 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229573AbiLILJw (ORCPT ); Fri, 9 Dec 2022 06:09:52 -0500 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 579143AC0A for ; Fri, 9 Dec 2022 03:09:51 -0800 (PST) Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id DA46023A; Fri, 9 Dec 2022 03:09:57 -0800 (PST) Received: from FVFF77S0Q05N (unknown [10.57.39.232]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id E52CD3F73B; Fri, 9 Dec 2022 03:09:49 -0800 (PST) Date: Fri, 9 Dec 2022 11:09:47 +0000 From: Mark Rutland To: ruanjinjie Cc: catalin.marinas@arm.com, will@kernel.org, haibinzhang@tencent.com, hewenliang4@huawei.com, linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org Subject: Re: [PATCH] arm64: fix a concurrency issue in emulation_proc_handler() Message-ID: References: <20221209105556.47621-1-ruanjinjie@huawei.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20221209105556.47621-1-ruanjinjie@huawei.com> X-Spam-Status: No, score=-4.2 required=5.0 tests=BAYES_00,RCVD_IN_DNSWL_MED, SPF_HELO_NONE,SPF_NONE 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, Dec 09, 2022 at 06:55:56PM +0800, ruanjinjie wrote: > In emulation_proc_handler(), read and write operations are performed on > insn->current_mode. In the concurrency scenario, mutex only protects > writing insn->current_mode, and not protects the read. Suppose there are > two concurrent tasks, task1 updates insn->current_mode to INSN_EMULATE > in the critical section, the prev_mode of task2 is still the old data > INSN_UNDEF of insn->current_mode. As a result, two tasks call > update_insn_emulation_mode twice with prev_mode = INSN_UNDEF and > current_mode = INSN_EMULATE, then call register_emulation_hooks twice, > resulting in a list_add double problem. > > Call trace: > __list_add_valid+0xd8/0xe4 > register_undef_hook+0x94/0x13c > update_insn_emulation_mode+0xd0/0x12c > emulation_proc_handler+0xd8/0xf4 > proc_sys_call_handler+0x140/0x250 > proc_sys_write+0x1c/0x2c > new_sync_write+0xec/0x18c > vfs_write+0x214/0x2ac > ksys_write+0x70/0xfc > __arm64_sys_write+0x24/0x30 > el0_svc_common.constprop.0+0x7c/0x1bc > do_el0_svc+0x2c/0x94 > el0_svc+0x20/0x30 > el0_sync_handler+0xb0/0xb4 > el0_sync+0x160/0x180 The version queued in the arm64 for-next/core branch no longer has the list manipulation, but we do need to fix this for stable, and there is a remaining race on reading insn->current_mode in emulation_proc_handler(). > Fixes: af483947d472 ("arm64: fix oops in concurrently setting insn_emulation sysctls") > Signed-off-by: ruanjinjie > --- > arch/arm64/kernel/armv8_deprecated.c | 6 ++++-- > 1 file changed, 4 insertions(+), 2 deletions(-) > > diff --git a/arch/arm64/kernel/armv8_deprecated.c b/arch/arm64/kernel/armv8_deprecated.c > index fb0e7c7b2e20..d33e5d9e6990 100644 > --- a/arch/arm64/kernel/armv8_deprecated.c > +++ b/arch/arm64/kernel/armv8_deprecated.c > @@ -208,10 +208,12 @@ static int emulation_proc_handler(struct ctl_table *table, int write, > loff_t *ppos) > { > int ret = 0; > - struct insn_emulation *insn = container_of(table->data, struct insn_emulation, current_mode); > - enum insn_emulation_mode prev_mode = insn->current_mode; > + struct insn_emulation *insn; > + enum insn_emulation_mode prev_mode; > > mutex_lock(&insn_emulation_mutex); > + insn = container_of(table->data, struct insn_emulation, current_mode); > + prev_mode = insn->current_mode; > ret = proc_dointvec_minmax(table, write, buffer, lenp, ppos); We don't strictly need to move the container_of(), but it makes no odds either way, and this looks good to me: Acked-by: Mark Rutland Mark. > > if (ret || !write || prev_mode == insn->current_mode) > -- > 2.25.1 >