From patchwork Sat Mar 12 15:56:03 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Guo Ren X-Patchwork-Id: 12778898 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id E5C53C433F5 for ; Sat, 12 Mar 2022 15:56:52 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:Message-Id:Date:Subject:Cc :To:From:Reply-To:Content-ID:Content-Description:Resent-Date:Resent-From: Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:In-Reply-To:References: List-Owner; bh=wxVaM9FkvQkQvDO5t9pt1IxfHMSVzGmyDcbQ/wMnbeo=; b=PR8PA2tPXDq/yq EGbyR1NExwiTYp4kZkzS4+PYvO8cVkGPNzkgvMA34j689T8q7Az1YYbBN80oG63cH3TROAkFVgwPb 5oNjLhAo/UnKk5N3sEGU6XYZ5XkqMjwarbERsMZxPe/XWl1Zt2+ZW999tVUmc+/mzycq/AmQghSPY w//cUaRxLQvHwr3S6vCJ4NKUYFlL/hlvsbjm+GBFtliNP1r80VE4hB8EbXw0VC/Aw9nivImYvpoId ePABMu+J4bOf6ISsKiJwM5Pau517aZwijdyfSue6gCNBA4etPk7zDlUJEyfCUHldr2sF7wjtjL6Ul mx3eND6DaktTLHHfUM8g==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1nT46k-001YDr-UK; Sat, 12 Mar 2022 15:56:42 +0000 Received: from ams.source.kernel.org ([145.40.68.75]) by bombadil.infradead.org with esmtps (Exim 4.94.2 #2 (Red Hat Linux)) id 1nT46Y-001Y9i-TH; Sat, 12 Mar 2022 15:56:32 +0000 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 ams.source.kernel.org (Postfix) with ESMTPS id 8A777B80837; Sat, 12 Mar 2022 15:56:29 +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 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20220312_075631_263214_A2238C54 X-CRM114-Status: GOOD ( 17.73 ) X-BeenThere: linux-riscv@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-riscv" Errors-To: linux-riscv-bounces+linux-riscv=archiver.kernel.org@lists.infradead.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 Reviewed-by: Max Filippov --- 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 {