From patchwork Wed Jan 25 03:38:04 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Josh Poimboeuf X-Patchwork-Id: 48012 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:eb09:0:0:0:0:0 with SMTP id s9csp78125wrn; Tue, 24 Jan 2023 19:50:30 -0800 (PST) X-Google-Smtp-Source: AK7set/l+cWPOrP64lDh/N1aO8l30spb9OzxBjo2t+rJzVgcxVQuu/anEkTneS+IlWUArNmyLw+8 X-Received: by 2002:a05:6a20:a001:b0:bb:aabe:7a56 with SMTP id p1-20020a056a20a00100b000bbaabe7a56mr4942321pzj.58.1674618630298; Tue, 24 Jan 2023 19:50:30 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1674618630; cv=none; d=google.com; s=arc-20160816; b=mJnxiTD8thyXdI0lxHcONcDbh0Lsa1o6vzfUDKNIlrrB5Dt/CEf5tj0aC7WVJSuft5 DFCzfHGE1IM6XpKZPJxaFCRcCuAJICK0+swlg+91SwytQ6NcGiGSg5KQhPoL3BtF7qLi 6tkEBQ3AQaizsGLWvq1LNmddEl3ZV3uMeWeQC0FrgFPaVxZGl0B684VX9LLOUJtvDiJo 6S2FyuHSiaPEFgARgghTXhBsShJDlKOTW2VfJocGUseHiFwT4RAigaOzgftBV08LJIrx uLjbwiAW+ivE2Y4jP0RznB2v15ApqzaO3gzCosXZLO68LjLR3R7OU1WHQql2dEZe9cif DWRA== 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 :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=DDlygiGwRXMABhfbvGPApGuQc6HhiuReqz7UTxWOU1o=; b=h/o4LVpWrj6WYuAB0w5/Bvhvv3jAuERLlIKNGmjzOzMkBj/E1HSpMzXrAI81mLORmn QcS2ah9aG5KTdrOqIy6ZI+m1ydKgS6WaU51KgjBNxakwa7QITLC38stZqMBFjOvjeEet +LnOV7EtQ1ROguDxB/xqUTFLxvTlctBwjzNrR7Wkwm/2cPs7hi6yLknwzzIAhgDQEGmm YqmvxQASKvBqtvrQdKaHJBjQioX5jSkLKeiDDUl+NJjvSSntpgUEgscNa7ASyTnbG5go z74G6iIOvz5I2BOVfqYyW02ZXr/RXE3B7dM/N5yYK/t1ux5vzuAa6VBjx5MDFQImhUJ9 PDPA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=QQ5MUOhZ; 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: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id c136-20020a63358e000000b004b64146f245si4022141pga.148.2023.01.24.19.50.18; Tue, 24 Jan 2023 19:50:30 -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=QQ5MUOhZ; 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 S234164AbjAYDiZ (ORCPT + 99 others); Tue, 24 Jan 2023 22:38:25 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51562 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229534AbjAYDiX (ORCPT ); Tue, 24 Jan 2023 22:38:23 -0500 Received: from ams.source.kernel.org (ams.source.kernel.org [145.40.68.75]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9E569E39E; Tue, 24 Jan 2023 19:38:14 -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 ams.source.kernel.org (Postfix) with ESMTPS id 4BF73B81894; Wed, 25 Jan 2023 03:38:13 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id AA63EC4339B; Wed, 25 Jan 2023 03:38:11 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1674617891; bh=ruVwyKu5r4Ho2K3Po1rwNTyU1GonlvOJe83MwngFuEc=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=QQ5MUOhZsTGTNvx7meE+NywZEQQIvOZZka611CH+C5ghKPCbcIScqQ2e+k1N4h/vy Bc8eHDGq19u7fMKXy/f5BsHXtm8MxeAVAqamvYn7GDTbpKj6FIitkVWiNZiPNYJHOz UcHudz0NMmCYZcV3+45a0E9FISyJrgUnrJs30QzyeK79da3K7qdSDw0Y+gXPMfQCnM TCqRQdXphPGuN/bvq/VkvzMPDN77GbcRzG8sFnOzMsLzm7vA7+jH1hbOfxXapiAXyL Z2OFRHjhIpvbAJ2Wc1AJXhpu2ZvbowIKuJlC8labNnEkDlnPT0xqBjAarIwBFMJ0Q1 MH55q5qylN0lw== From: Josh Poimboeuf To: Michael Ellerman Cc: live-patching@vger.kernel.org, Nicholas Piggin , Christophe Leroy , linuxppc-dev@lists.ozlabs.org, linux-kernel@vger.kernel.org, Song Liu Subject: [PATCH 1/2] powerpc/module_64: Improve restore_r2() return semantics Date: Tue, 24 Jan 2023 19:38:04 -0800 Message-Id: <15baf76c271a0ae09f7b8556e50f2b4251e7049d.1674617130.git.jpoimboe@kernel.org> X-Mailer: git-send-email 2.39.0 In-Reply-To: References: MIME-Version: 1.0 Content-type: text/plain X-Spam-Status: No, score=-7.1 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 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 X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1755964904929003576?= X-GMAIL-MSGID: =?utf-8?q?1755964904929003576?= restore_r2() returns 1 on success, which is surprising for a non-boolean function. Change it to return 0 on success and -errno on error to match kernel coding convention. Signed-off-by: Josh Poimboeuf Acked-by: Song Liu Reviewed-by: Petr Mladek Reviewed-by: Miroslav Benes --- arch/powerpc/kernel/module_64.c | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/arch/powerpc/kernel/module_64.c b/arch/powerpc/kernel/module_64.c index 1096d6b3a62c..016e79bba531 100644 --- a/arch/powerpc/kernel/module_64.c +++ b/arch/powerpc/kernel/module_64.c @@ -504,7 +504,7 @@ static int restore_r2(const char *name, u32 *instruction, struct module *me) u32 *prev_insn = instruction - 1; if (is_mprofile_ftrace_call(name)) - return 1; + return 0; /* * Make sure the branch isn't a sibling call. Sibling calls aren't @@ -512,19 +512,16 @@ static int restore_r2(const char *name, u32 *instruction, struct module *me) * restore afterwards. */ if (!instr_is_relative_link_branch(ppc_inst(*prev_insn))) - return 1; + return 0; if (*instruction != PPC_RAW_NOP()) { pr_err("%s: Expected nop after call, got %08x at %pS\n", me->name, *instruction, instruction); - return 0; + return -ENOEXEC; } /* ld r2,R2_STACK_OFFSET(r1) */ - if (patch_instruction(instruction, ppc_inst(PPC_INST_LD_TOC))) - return 0; - - return 1; + return patch_instruction(instruction, ppc_inst(PPC_INST_LD_TOC)); } int apply_relocate_add(Elf64_Shdr *sechdrs, @@ -648,8 +645,8 @@ int apply_relocate_add(Elf64_Shdr *sechdrs, strtab + sym->st_name); if (!value) return -ENOENT; - if (!restore_r2(strtab + sym->st_name, - (u32 *)location + 1, me)) + if (restore_r2(strtab + sym->st_name, + (u32 *)location + 1, me)) return -ENOEXEC; } else value += local_entry_offset(sym); From patchwork Wed Jan 25 03:38:05 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Josh Poimboeuf X-Patchwork-Id: 48011 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:eb09:0:0:0:0:0 with SMTP id s9csp77427wrn; Tue, 24 Jan 2023 19:47:57 -0800 (PST) X-Google-Smtp-Source: AMrXdXsAtMQVsHO7pYTP/SFdX+eGXeZbhsaxCh+lvxv3R3V/CnL09U0JaYq5KCxMTIFCk/rj2BAo X-Received: by 2002:a17:90b:1bc7:b0:22b:bbe3:672b with SMTP id oa7-20020a17090b1bc700b0022bbbe3672bmr16772244pjb.9.1674618477479; Tue, 24 Jan 2023 19:47:57 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1674618477; cv=none; d=google.com; s=arc-20160816; b=NkCVBvANCoClGNv1SZqdazBRvk5CHQSTAK5cRgRkAcLMJt/FJFYxibUbecovxygK/V Aqp+m3+1XJ4tMYo6O6OdqoFBNxbu2x9TNiXDzAUvhMCOzSXy6+Dtc6UM1OJLr9d2TOs1 i0qzS0GVfAzzJmu4zb4p56F7QhDZ8PsFz+8iwPNC/jZNAkOYyGiyZj1PwmR8cVN9dM2N AC5nSRsRioEDQlCzySezAnj3SyEqCSP1AVkya4ZGBw6MoFx7irHIaUI/wMlmhR3lgyeP iy0OM5kAob7qEasa4PDUH6TMq9NvO1CicdKy+aub76hRiiRgoV9rrDQJR9HX5BhkIsjj 4N4A== 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 :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=k1bCbNUT9Y3VFPimjn1OvEYYDu3G+vfKH3M54CeWgWw=; b=PGs2FYthXgGmSUfRnEwwt12JlC7kuxyA/nmbSj9BGq2chOmAOD7dUnK3qW/OSNLxix U5OYAeJmYBEOuE59q/OFmzaZLN8ax+m+YiB6LcuqERh2YtavzV3uh0DbPG+sxYHNg/3j ERORn2ZdBoosmVmRFipKuzx6At/w23Ei/AgdtmSELxKEPK2MGzcDNv2NePKoCtGYbtPy n6nWn9f5JcMqamgmYx6Ig2aYd3RR3g4hUTM7KRcFhaFYXuZ2hIQELbFwew7yEM6O8YGU f23Z23OlxjDHGpd3pH8vzcYa2SYsw0Dc12xiksaqfg8bUEyN1zw/iLQIzHBBuIQIUsjb KmYQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=P9J1452o; 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: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id c18-20020a17090ab29200b00229c1a4a338si802772pjr.106.2023.01.24.19.47.44; Tue, 24 Jan 2023 19:47:57 -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=P9J1452o; 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 S232166AbjAYDiY (ORCPT + 99 others); Tue, 24 Jan 2023 22:38:24 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51558 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233279AbjAYDiW (ORCPT ); Tue, 24 Jan 2023 22:38:22 -0500 Received: from dfw.source.kernel.org (dfw.source.kernel.org [IPv6:2604:1380:4641:c500::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7085CCA26; Tue, 24 Jan 2023 19:38:13 -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 ED74D61444; Wed, 25 Jan 2023 03:38:12 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 1A1AAC433A0; Wed, 25 Jan 2023 03:38:12 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1674617892; bh=7M3ieHPKjncgevNaIxm++BYHAAhc0S9LqYRA8tUI6Pc=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=P9J1452odLsdx9doxYgTkxrLdCcOxbiWEcNN3c1SS4mdkU1oxRzvV4a4XV/0BbQ9l SBxFaspcH9MniyYLQbBXgGXc/40u0BVnlosMkZ59hGsQD2ZtlCSgjxkQ6YRqMLPlSZ PrGLsThlzNCrIgruY4wNOh2EKMnjXOqodtyk565vVkwRA9c3ZM9fgB+A5DldgqdSYt tQw6v6Rp0lqJA5umtMy0jcsmp3utJc6DJPUHg7Qip/IOYG6z7+TqLVowgk8ynnZS3N 8r+3N4ZEqgI0VoBXDnvPlvm6BvZES1t+sHzbRcI1WtgVfxlZ53ysJD7yxDpif/2MR6 Y9i+Oqrub8C7Q== From: Josh Poimboeuf To: Michael Ellerman Cc: live-patching@vger.kernel.org, Nicholas Piggin , Christophe Leroy , linuxppc-dev@lists.ozlabs.org, linux-kernel@vger.kernel.org, Song Liu Subject: [PATCH 2/2] powerpc/module_64: Fix "expected nop" error on module re-patching Date: Tue, 24 Jan 2023 19:38:05 -0800 Message-Id: <2f6329ffd9674df6ff57e03edeb2ca54414770ab.1674617130.git.jpoimboe@kernel.org> X-Mailer: git-send-email 2.39.0 In-Reply-To: References: MIME-Version: 1.0 Content-type: text/plain X-Spam-Status: No, score=-7.1 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 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 X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1755964744206022611?= X-GMAIL-MSGID: =?utf-8?q?1755964744206022611?= When a module with a livepatched function is unloaded and then reloaded, klp attempts to dynamically re-patch it. On ppc64, that fails with the following error: module_64: livepatch_nfsd: Expected nop after call, got e8410018 at e_show+0x60/0x548 [livepatch_nfsd] livepatch: failed to initialize patch 'livepatch_nfsd' for module 'nfsd' (-8) livepatch: patch 'livepatch_nfsd' failed for module 'nfsd', refusing to load module 'nfsd' The error happens because the restore r2 instruction had already previously been written into the klp module's replacement function when the original function was patched the first time. So the instruction wasn't a nop as expected. When the restore r2 instruction has already been patched in, detect that and skip the warning and the instruction write. Signed-off-by: Josh Poimboeuf Reviewed-by: Petr Mladek Acked-by: Song Liu Reviewed-by: Miroslav Benes --- arch/powerpc/kernel/module_64.c | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/arch/powerpc/kernel/module_64.c b/arch/powerpc/kernel/module_64.c index 016e79bba531..bf1da99fff74 100644 --- a/arch/powerpc/kernel/module_64.c +++ b/arch/powerpc/kernel/module_64.c @@ -502,6 +502,7 @@ static unsigned long stub_for_addr(const Elf64_Shdr *sechdrs, static int restore_r2(const char *name, u32 *instruction, struct module *me) { u32 *prev_insn = instruction - 1; + u32 insn_val = *instruction; if (is_mprofile_ftrace_call(name)) return 0; @@ -514,9 +515,18 @@ static int restore_r2(const char *name, u32 *instruction, struct module *me) if (!instr_is_relative_link_branch(ppc_inst(*prev_insn))) return 0; - if (*instruction != PPC_RAW_NOP()) { + /* + * For livepatch, the restore r2 instruction might have already been + * written previously, if the referenced symbol is in a previously + * unloaded module which is now being loaded again. In that case, skip + * the warning and the instruction write. + */ + if (insn_val == PPC_INST_LD_TOC) + return 0; + + if (insn_val != PPC_RAW_NOP()) { pr_err("%s: Expected nop after call, got %08x at %pS\n", - me->name, *instruction, instruction); + me->name, insn_val, instruction); return -ENOEXEC; }