Received: by 2002:a05:6358:a55:b0:ec:fcf4:3ecf with SMTP id 21csp3318238rwb; Fri, 20 Jan 2023 14:26:31 -0800 (PST) X-Google-Smtp-Source: AMrXdXuVTr6M6+8jzS+Jwbl045WdT5GxJZ35pSr78VSMjh/+VA7efGP1Cc7NIWG6Dv2gZ3DysSPs X-Received: by 2002:a17:902:da90:b0:189:469c:dc0 with SMTP id j16-20020a170902da9000b00189469c0dc0mr24034033plx.7.1674253591307; Fri, 20 Jan 2023 14:26:31 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1674253591; cv=none; d=google.com; s=arc-20160816; b=cLn3+5vkwolfvZiNRsntz+4PT71ndrZkuinXWXk+B5qE9g7LVMwqJKQ18KKWsLKEqx lSl4qY0L7YHm3NrcJ4jmjsJuQpPX8RUy/QkxSBscjgMtmDRA7N+t2LV0ZkrZJE7YDQUQ LACAsHLkk1PIQytZe8wk9OvuEwdJxWfO1yoFYVoN57mZ9PSllB3ZCMqV9fKDrpXvDZdS BA0UlbSTHAn9sxlBVHJePjZUEygB8cuJVf0qFyMaIhE46y9Kln8RizFiokclXz2Eilvz dbsxlyZbdKJPoMgkglTdaE5mz54TTgf8MTMRopCRUBbxdvmZPQjfmSwW3n4ODk+eviV/ jWfw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:in-reply-to:references:message-id :content-transfer-encoding:mime-version:subject:date:from :dkim-signature; bh=wbodpTKPKmmsf6sP0FY882Wjf4zzytHSiP84SDKBVQY=; b=jcSph+GboSSjC4Crz/A+LMrFripC8EMZ//MZY1VhkobeXuE3roQwzVNmVGNfRej9G6 0LZx2NfBiprJR7DF29ZKveNBY7UPo7mS+JgASbHXKwakEC5ILjxJLVOrrrU5T/UtrSu5 hgD7TT9lsVHTVGLVE7q1sNgRXfPjtHKbyL95k4wh8P04kD7tIF6Te0xDd//SlFU/0DSW qgM6UeX/cpKa/tBaWfxOkc0T7TJjhberPebUAow1oxTGCNZ44rrqUl/Yzxq14rCzqUHL ki06ATDZAecEtFceN6ipMvZxOZWSq7uqhh1wO90nGbVvcxUh/Rb+LGLVkD8jEnsrO3Gu 2UjQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@digitalocean.com header.s=google header.b=cBx1flC4; 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=REJECT sp=REJECT dis=NONE) header.from=digitalocean.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id j4-20020a170902da8400b00180680b8f1bsi20467696plx.264.2023.01.20.14.26.24; Fri, 20 Jan 2023 14:26:31 -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=@digitalocean.com header.s=google header.b=cBx1flC4; 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=REJECT sp=REJECT dis=NONE) header.from=digitalocean.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230010AbjATWMm (ORCPT + 50 others); Fri, 20 Jan 2023 17:12:42 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60674 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229966AbjATWMh (ORCPT ); Fri, 20 Jan 2023 17:12:37 -0500 Received: from mail-il1-x132.google.com (mail-il1-x132.google.com [IPv6:2607:f8b0:4864:20::132]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4DA461BDD for ; Fri, 20 Jan 2023 14:12:35 -0800 (PST) Received: by mail-il1-x132.google.com with SMTP id h15so3117597ilh.4 for ; Fri, 20 Jan 2023 14:12:35 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=digitalocean.com; s=google; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=wbodpTKPKmmsf6sP0FY882Wjf4zzytHSiP84SDKBVQY=; b=cBx1flC4qcgu9rrDuKrrO1mDCaZBSPWObcfiVypcdCKvHYtSrTfEce40+s+2J4zyI+ /gVtMyOF+lDW0t4v19/baQh3Pyc5u+wRFnQyhKRSMaQy3zxBme7NG1c5DDjp/LRecDSI rpavxneoCsLF96wzrzdzqfisrzGoJBzZXI09M= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=wbodpTKPKmmsf6sP0FY882Wjf4zzytHSiP84SDKBVQY=; b=N9oQFT9SzuWbs6V6AAYktgmPbPTN8WNLf6bzjnaUBEkiUP1d86vZrALHoIDpU0BEZz N5VE+r3zqCHwCcQmDhDeIcp74TCp/I4ABqB9+lCCM28RA3ePKUL7xHY1Ocz8C4MzzLhj VYx9K8DfJ1eyZErb5FEjHTWumVgcYlrmfFbWX7KAH8fo7rJQSP+BuNwi99rsn3Pwx74r jBPEwVL5RHlAhQ7iau6AtJotjA9VK4z7L8j6UtHWIf8D25r2Sy8QLrMgBqBuWHzG8pOG fBYy2lulpwIUcI6wTBwfyCFD1Cx6uh5MhIpCB+wEiiFiUc2+vKUUiWQlu+lFpdCs9Puc hzAg== X-Gm-Message-State: AFqh2korEcSckeeV29UDb6bUitMitnVz7Wi/vUyTSAGgY0a7p69cVZgg rV6ObV5iD+Inr5tsYKeZiFQ7SQ== X-Received: by 2002:a05:6e02:6cc:b0:30c:4846:57c3 with SMTP id p12-20020a056e0206cc00b0030c484657c3mr12392861ils.4.1674252754641; Fri, 20 Jan 2023 14:12:34 -0800 (PST) Received: from localhost ([136.37.131.79]) by smtp.gmail.com with ESMTPSA id b7-20020a920b07000000b0030f04dcbc2fsm4679183ilf.66.2023.01.20.14.12.34 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 20 Jan 2023 14:12:34 -0800 (PST) From: "Seth Forshee (DigitalOcean)" X-Google-Original-From: "Seth Forshee (DigitalOcean)" Date: Fri, 20 Jan 2023 16:12:21 -0600 Subject: [PATCH 1/2] livepatch: add an interface for safely switching kthreads MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 8bit Message-Id: <20230120-vhost-klp-switching-v1-1-7c2b65519c43@kernel.org> References: <20230120-vhost-klp-switching-v1-0-7c2b65519c43@kernel.org> In-Reply-To: <20230120-vhost-klp-switching-v1-0-7c2b65519c43@kernel.org> To: Petr Mladek , Jason Wang , "Michael S. Tsirkin" , Jiri Kosina , Miroslav Benes , Joe Lawrence , Josh Poimboeuf Cc: virtualization@lists.linux-foundation.org, kvm@vger.kernel.org, "Seth Forshee (DigitalOcean)" , netdev@vger.kernel.org, live-patching@vger.kernel.org, linux-kernel@vger.kernel.org X-Mailer: b4 0.10.1 X-Developer-Signature: v=1; a=openpgp-sha256; l=2403; i=sforshee@kernel.org; h=from:subject:message-id; bh=r9mU45ndBE6+c+nVzo6X6SmMiX3LFRgk6Wk7Kz1IlfY=; b=owEBbQGS/pANAwAKAVMDma7l9DHJAcsmYgBjyxHM7TI1pPLXBaX/blEl/AF4BuJfI3qcZu1Ba0Pi 7RCvBguJATMEAAEKAB0WIQSQnt+rKAvnETy4Hc9TA5mu5fQxyQUCY8sRzAAKCRBTA5mu5fQxyQajCA CQHOboQjqLYEklPYMzSh3LIcU+lmYlGq/PU5Tvw2uXXdt5OuZ4wKr1bfrXZpqEQQFOQ5GyQIs8TWme 68r4FYf+XBDN8IA7Am+RvNbij9LDM0ErUGMkEMlMUK2XSI68SKL5/NbPtA12cLqwyqbrMtSU0V1lOS eSDjz6e/IF+cCI3MRJJbXNn7lxroUBmuaxIeYS1ctnsDnBTdo+Q9+7o2kgP03u4T/S2KgzhrTilX6j /2IzHtl3Yan7ZRmpHjyJHOjBbOK75ZYYBEdb8isQRJrPJDUfZru9Cy8XEYoGElACzFBHIrUhHU8zMH BcKdbg7Z6g/u8WS7oj3yqkyZRxDEBW X-Developer-Key: i=sforshee@kernel.org; a=openpgp; fpr=2ABCA7498D83E1D32D51D3B5AB4800A62DB9F73A X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_NONE autolearn=unavailable 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 The only existing solution for transitioning a busy kernel thread is to call klp_update_patch_state() from a safe location. However, this does not account for the fact that even the main function of the kthread could potentially be patched, leaving the patch switched but still running the old version of a patched function. To address this, add klp_switch_current() for use by kthreads to safely transition themselves. This is just a wrapper around klp_try_switch_task(), which can already transition the current task with stack checking. Signed-off-by: Seth Forshee (DigitalOcean) --- include/linux/livepatch.h | 2 ++ kernel/livepatch/transition.c | 11 +++++++++++ 2 files changed, 13 insertions(+) diff --git a/include/linux/livepatch.h b/include/linux/livepatch.h index 293e29960c6e..00b5981684a4 100644 --- a/include/linux/livepatch.h +++ b/include/linux/livepatch.h @@ -199,6 +199,7 @@ void klp_module_going(struct module *mod); void klp_copy_process(struct task_struct *child); void klp_update_patch_state(struct task_struct *task); +void klp_switch_current(void); static inline bool klp_patch_pending(struct task_struct *task) { @@ -240,6 +241,7 @@ static inline int klp_module_coming(struct module *mod) { return 0; } static inline void klp_module_going(struct module *mod) {} static inline bool klp_patch_pending(struct task_struct *task) { return false; } static inline void klp_update_patch_state(struct task_struct *task) {} +static inline void klp_switch_current(void) {} static inline void klp_copy_process(struct task_struct *child) {} static inline diff --git a/kernel/livepatch/transition.c b/kernel/livepatch/transition.c index f1b25ec581e0..ff328b912916 100644 --- a/kernel/livepatch/transition.c +++ b/kernel/livepatch/transition.c @@ -455,6 +455,17 @@ void klp_try_complete_transition(void) klp_free_replaced_patches_async(patch); } +/* + * Safely try to switch the current task to the target patch state. This can + * be used by kthreads to transition if no to-be-patched or to-be-unpatched + * functions are on the call stack. + */ +void klp_switch_current(void) +{ + klp_try_switch_task(current); +} +EXPORT_SYMBOL_GPL(klp_switch_current); + /* * Start the transition to the specified target patch state so tasks can begin * switching to it. -- b4 0.10.1