Received: by 2002:ac0:e350:0:0:0:0:0 with SMTP id g16csp1118914imn; Sat, 30 Jul 2022 16:21:54 -0700 (PDT) X-Google-Smtp-Source: AA6agR5mlqibIe+ubKG0JDHeZTWGaYPznVfWubCvMWgR5470Ab9IKIEt/FUvatRN6Rx5asBbt7r/ X-Received: by 2002:a17:902:7c83:b0:16d:3db9:fdc5 with SMTP id y3-20020a1709027c8300b0016d3db9fdc5mr9833967pll.153.1659223314159; Sat, 30 Jul 2022 16:21:54 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1659223314; cv=none; d=google.com; s=arc-20160816; b=mdZ4BE/3wLFSqen0aritVLdi1we4TIzR6yvcEcj8hGzo219o2iyVNu78zsn2hbbbdP KSPH57Y+svchKgMv8n0JCtH7GvRtrY6I5H2Stt3V7RBLEsD7LDKWnbeoPsd4TUu6RPdg OpAOdvYNB9zfiJ5qTiCNGsbiYb999T/lQN3kf67pkNxaVV7+iNi42mvtdZwKxa33n87b WByChoI0Cww1N7lFnRpIevWGoAdd46teXfAd697P55B5MCfsM0rpfO+/WlQVXfqpGfa6 0z5hr8T91LTNASFwf7USzBb3cSqUkC5j8gsHwBxMyCg9eY37bUbzdIfeI/MGRwYbOl98 ZFsQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:subject:message-id:date:from:in-reply-to :references:mime-version:dkim-signature; bh=X6NBj6+ntJUkcT0/4sGLbxaLA3JXPHmmaDaAjS2FsEM=; b=kigmxl5GYxGJBDeoCGFfPzmsRcJUoU2hbxpa23Xx/wDxUmmOeSxLqa0CcGuB0PykQ+ zhiz1txo1w9K3u9ojjlpH0QvyAUkWETgmU2E8CnwF80FTkvo5SIsa+ZpeWWk+efpybYb CqzYhZ5HLYyN8y4q+I87WQiNkMA/2Mh4fvlk3P/hCRiaQ7JImR7fsl8bcAgmr5uUTOMJ KGHZJPMQbPRB90RCe+qRK/0gs1pkK8iDYHp5LwjL42YgnCI8YiKVdrAvc+5oe3AZDboP Vj/15Au9agO7Qn9REbLczPoEKpLbFKhn0nCwBpdnGQUA7w3mSSZWu+rMgeQTIh8HyWao Jd5w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=HiblzlVe; 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 y12-20020a170902864c00b0016c5017ada7si7616195plt.373.2022.07.30.16.21.39; Sat, 30 Jul 2022 16:21:53 -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; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=HiblzlVe; 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 S233079AbiG3WdO (ORCPT + 99 others); Sat, 30 Jul 2022 18:33:14 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48856 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229895AbiG3WdL (ORCPT ); Sat, 30 Jul 2022 18:33:11 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id EDABC13D4F; Sat, 30 Jul 2022 15:33:10 -0700 (PDT) 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 4FAB960FA7; Sat, 30 Jul 2022 22:33:10 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id AC004C433D7; Sat, 30 Jul 2022 22:33:09 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1659220389; bh=/uOO/KL7L4YkvwKvPZQ++G9L5z04U+oo483Vn4Rn5eQ=; h=References:In-Reply-To:From:Date:Subject:To:Cc:From; b=HiblzlVe9bdmg1Y9KDD3TP9y4K4fTmHeo5z75qWEKNxIEbVuB73Cj8ztvGLnDuoLu zMxO7rLJnLMQN02lvaTjCRVg20Gt617LwiP8O7QesV4htlcVJ0GO/GlDQZjX50QR7p RwZONNKP80Z1jVrJjvfrxnAED1k85061WK0I5mwwDlAH2gwlkX26X1MlO1gTbaMRWD f8X1IIVayEYOhbkH9kV4x1ltChzoItgVit9TISUp/H7adT5hOb4n8PHRXkJp0D9mSo 4PK4LlYdOV34ajBshZRcAlAum6u8LtImp7IpmxyevdT1HqYc7Hu1Kq8TYTflGVabLK 8cBK/klHJJSXw== Received: by mail-yb1-f178.google.com with SMTP id n8so13577843yba.2; Sat, 30 Jul 2022 15:33:09 -0700 (PDT) X-Gm-Message-State: ACgBeo1cwTHoNoOF1GhcRJKFmiT65eQHQT75XCpjOOl+laqErY9lQfOG PmZqWPi4lvPd3frxvgLW6Nzgec79pXsORG+jyWM= X-Received: by 2002:a25:8b92:0:b0:66d:553a:f309 with SMTP id j18-20020a258b92000000b0066d553af309mr6838431ybl.322.1659220388722; Sat, 30 Jul 2022 15:33:08 -0700 (PDT) MIME-Version: 1.0 References: <20220721175147.214642-1-song@kernel.org> <20220726233302.zwloxsammnu7clu4@treble> In-Reply-To: From: Song Liu Date: Sat, 30 Jul 2022 15:32:58 -0700 X-Gmail-Original-Message-ID: Message-ID: Subject: Re: [PATCH v3] livepatch: Clear relocation targets on a module removal To: Josh Poimboeuf Cc: live-patching@vger.kernel.org, open list , Jiri Kosina , Miroslav Benes , Petr Mladek , Joe Lawrence , X86 ML , Josh Poimboeuf Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-7.7 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 On Tue, Jul 26, 2022 at 8:54 PM Song Liu wrote: > > On Tue, Jul 26, 2022 at 4:33 PM Josh Poimboeuf wrote: > > > > On Thu, Jul 21, 2022 at 10:51:47AM -0700, Song Liu wrote: > > > From: Miroslav Benes > > > > > > Josh reported a bug: > > > > > > When the object to be patched is a module, and that module is > > > rmmod'ed and reloaded, it fails to load with: > > > > > > module: x86/modules: Skipping invalid relocation target, existing value is nonzero for type 2, loc 00000000ba0302e9, val ffffffffa03e293c > > > 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 livepatch module has a relocation which references a symbol > > > in the _previous_ loading of nfsd. When apply_relocate_add() > > > tries to replace the old relocation with a new one, it sees that > > > the previous one is nonzero and it errors out. > > > > > > On ppc64le, we have a similar issue: > > > > > > 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' > > > > > > He also proposed three different solutions. We could remove the error > > > check in apply_relocate_add() introduced by commit eda9cec4c9a1 > > > ("x86/module: Detect and skip invalid relocations"). However the check > > > is useful for detecting corrupted modules. > > > > > > We could also deny the patched modules to be removed. If it proved to be > > > a major drawback for users, we could still implement a different > > > approach. The solution would also complicate the existing code a lot. > > > > > > We thus decided to reverse the relocation patching (clear all relocation > > > targets on x86_64). The solution is not > > > universal and is too much arch-specific, but it may prove to be simpler > > > in the end. > > > > > > Reported-by: Josh Poimboeuf > > > Signed-off-by: Miroslav Benes > > > Signed-off-by: Song Liu > > > > > > --- > > > > > > Changes from v2: > > > 1. Rewrite x86 changes to match current code style. > > > 2. Remove powerpc changes as there is no test coverage in v3. > > > 3. Only keep 1/3 of v2. > > > > 1) All the copy/paste is ugly and IMO guaranteed to eventually introduce > > bugs when somebody forgets to update the copy. Wouldn't it be more > > robust to reuse the existing apply_relocate_add() code by making it > > more generic somehow, like with a new 'clear' bool arg which sets > > 'val' to zero? > > Agreed. I can give it a try. I finished this part, though it is not really clean (added if else for each "case:"). > > > > > 2) We can't only fix x86, powerpc also needs a fix. > > I have very little experience with powerpc. Would someone be willing to > help with powerpc part of this? I guess folks are all busy. Any suggestions on how to test powerpc changes? > > > 3) A selftest would be a good idea. > I found it is pretty tricky to run the selftests inside a qemu VM. How about we test it with modules in samples/livepatch? Specifically, we can add a script try to reload livepatch-shadow-mod.ko. Thanks, Song