Received: by 2002:a05:6a10:413:0:0:0:0 with SMTP id 19csp1666193pxp; Sat, 12 Mar 2022 17:49:45 -0800 (PST) X-Google-Smtp-Source: ABdhPJyRI8Rix3ju1Q3XAGPiSzPIhA75iYptw2bMnNI1gIlUHrV4h8xmuqYdTyVbC2V0h/ACPduY X-Received: by 2002:a17:907:9703:b0:6da:6412:508a with SMTP id jg3-20020a170907970300b006da6412508amr14455630ejc.77.1647136185657; Sat, 12 Mar 2022 17:49:45 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1647136185; cv=none; d=google.com; s=arc-20160816; b=eJ5HmXTqqsK02WcO3wQgtEYBpjtVJYTRJGywbh53WMvCADk/XweVuaRdOmGnASt7kU qiekXA4VMxJVVOQHCWF8C/Kr0JaxCuJG9LptQMEEVxwRhBUewtARuDNZPB67DX8Wcco/ r7sjoasuGo2gqUuC9yP0pBgF1D8PKR+d5z179b62lu2ojxggtVfGV9hW63zrtjs87sdE 8oCcLpqyez+bdvKs5CUOVmGGXNo245hZ9z48NX6wx5Aecb8XjJkbonVed6I69XtmKyKp TrJ8wI8moIql41p2B5XJFM59bFWIqRkQaaZQwTfUbFJv5bnMC8x858M/U6FeiWvC00nQ 0Ilw== 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:date:subject:cc:to:from:dkim-signature; bh=adqjhpYPmz+fpSCAT6stuX57YjZeqT2Bq7d/nQ0e5R0=; b=U8+Ls/yMGSSuCS/lypSfpn+iNF3SUAqKqUlJK+RErOfpk4AQ1u0CaqNGkVe852YCDJ xl7MPpbAKMsVZVpXgKSi/4+FCxG9UUA3HKVfbDuSnFuY0Ul5MpHNKvT8tiINKGbyvqHb mo3OgCU6p3vzT/r0II0GapNUU3DoS4c7Gj3+cRG19OpOtsZQoSFAI1R7AsW1tr9kDWw0 oqWZjK5/C4t+cjPjYDLUOc99GyHd9OS62GSj7I2PrJtXMVdWDyNxfWVtYB15jaVkjwOu WeIUzqZKZCk98YvLnczz8kOpRR4qYszuJD/I43gjbvwr81ACu+rHxIKdGTJzYFo63JXF vL0g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=eH3BKmP1; 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 nb37-20020a1709071ca500b006d879078330si10261543ejc.246.2022.03.12.17.49.09; Sat, 12 Mar 2022 17:49:45 -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=eH3BKmP1; 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 S232118AbiCLP5g (ORCPT + 99 others); Sat, 12 Mar 2022 10:57:36 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45614 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232111AbiCLP5e (ORCPT ); Sat, 12 Mar 2022 10:57:34 -0500 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5C66D4C7B5; Sat, 12 Mar 2022 07:56:29 -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 dfw.source.kernel.org (Postfix) with ESMTPS id E706D60F90; Sat, 12 Mar 2022 15:56:28 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 8C6AFC340EB; Sat, 12 Mar 2022 15:56:23 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1647100588; bh=od4DZd0RLS1dMTaaAwprQNSP717OVfCCfXQ6agXS2qg=; h=From:To:Cc:Subject:Date:From; b=eH3BKmP1eY5WFgQDG8ivgfDeSu1O9Vq0VPXhyq9HqC9ovmTrvBIqL2dyRiJdecW7n blM7cR91Fff/k93Id3Nf17e9SSxYErKv8TnOxLwl/86d6SOLy3KFZzzEnwj3tsyPwg VeHjgsOQBqjMVnMwBo8SKlPtce90RPUU1IsALnIkjkOhB/vNZ2n9oNigYybOPRG1Sc 2CTl/S9hdWO3UEr4z/xNz6nP6ppzSHY/31HFNcM6cgLZLfILxwpRAQSAcToSsSiPbz tQG8FC7pKaX/2m4eX5vmXZDLkKAyQ3yTMHWxp2A1AOI7og63hBK4TnZs9TfQeZUI22 1OdZ9CRzsOwxA== From: guoren@kernel.org To: guoren@kernel.org Cc: linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, linux-csky@vger.kernel.org, linux-riscv@lists.infradead.org, linux-xtensa@linux-xtensa.org, Guo Ren , Will Deacon , Catalin Marinas , Palmer Dabbelt , Masami Hiramatsu , Chris Zankel , Max Filippov , Arnd Bergmann Subject: [RFC PATCH] arch: patch_text: Fixup last cpu should be master Date: Sat, 12 Mar 2022 23:56:03 +0800 Message-Id: <20220312155603.1752193-1-guoren@kernel.org> X-Mailer: git-send-email 2.25.1 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-7.6 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,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 From: Guo Ren These patch_text implementations are using stop_machine_cpuslocked infrastructure with atomic cpu_count. The origin idea is that when the master CPU patch_text, others should wait for it. But current implementation is using the first CPU as master, which couldn't guarantee continue CPUs are waiting. This patch changes the last CPU as the master to solve the potaintial risk. Signed-off-by: Guo Ren Signed-off-by: Guo Ren Cc: Will Deacon Cc: Catalin Marinas Cc: Palmer Dabbelt Cc: Peter Zijlstra Cc: Chris Zankel Cc: Max Filippov Cc: Arnd Bergmann --- arch/arm64/kernel/patching.c | 4 ++-- arch/csky/kernel/probes/kprobes.c | 2 +- arch/riscv/kernel/patch.c | 2 +- arch/xtensa/kernel/jump_label.c | 2 +- 4 files changed, 5 insertions(+), 5 deletions(-) diff --git a/arch/arm64/kernel/patching.c b/arch/arm64/kernel/patching.c index 771f543464e0..6cfea9650e65 100644 --- a/arch/arm64/kernel/patching.c +++ b/arch/arm64/kernel/patching.c @@ -117,8 +117,8 @@ static int __kprobes aarch64_insn_patch_text_cb(void *arg) int i, ret = 0; struct aarch64_insn_patch *pp = arg; - /* The first CPU becomes master */ - if (atomic_inc_return(&pp->cpu_count) == 1) { + /* The last CPU becomes master */ + if (atomic_inc_return(&pp->cpu_count) == (num_online_cpus() - 1)) { for (i = 0; ret == 0 && i < pp->insn_cnt; i++) ret = aarch64_insn_patch_text_nosync(pp->text_addrs[i], pp->new_insns[i]); diff --git a/arch/csky/kernel/probes/kprobes.c b/arch/csky/kernel/probes/kprobes.c index 42920f25e73c..19821a06a991 100644 --- a/arch/csky/kernel/probes/kprobes.c +++ b/arch/csky/kernel/probes/kprobes.c @@ -30,7 +30,7 @@ static int __kprobes patch_text_cb(void *priv) struct csky_insn_patch *param = priv; unsigned int addr = (unsigned int)param->addr; - if (atomic_inc_return(¶m->cpu_count) == 1) { + if (atomic_inc_return(¶m->cpu_count) == (num_online_cpus() - 1)) { *(u16 *) addr = cpu_to_le16(param->opcode); dcache_wb_range(addr, addr + 2); atomic_inc(¶m->cpu_count); diff --git a/arch/riscv/kernel/patch.c b/arch/riscv/kernel/patch.c index 0b552873a577..cca72a9388e3 100644 --- a/arch/riscv/kernel/patch.c +++ b/arch/riscv/kernel/patch.c @@ -104,7 +104,7 @@ static int patch_text_cb(void *data) struct patch_insn *patch = data; int ret = 0; - if (atomic_inc_return(&patch->cpu_count) == 1) { + if (atomic_inc_return(&patch->cpu_count) == (num_online_cpus() - 1)) { ret = patch_text_nosync(patch->addr, &patch->insn, GET_INSN_LENGTH(patch->insn)); diff --git a/arch/xtensa/kernel/jump_label.c b/arch/xtensa/kernel/jump_label.c index 61cf6497a646..7e1d3f952eb3 100644 --- a/arch/xtensa/kernel/jump_label.c +++ b/arch/xtensa/kernel/jump_label.c @@ -40,7 +40,7 @@ static int patch_text_stop_machine(void *data) { struct patch *patch = data; - if (atomic_inc_return(&patch->cpu_count) == 1) { + if (atomic_inc_return(&patch->cpu_count) == (num_online_cpus() - 1)) { local_patch_text(patch->addr, patch->data, patch->sz); atomic_inc(&patch->cpu_count); } else { -- 2.25.1