From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id A195C27470; Tue, 15 Jul 2025 13:57:18 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1752587838; cv=none; b=A3F+Ockm4QOmfifrcjz/bRwlWDtOT+Jk0gkzQ/ue6WrumSwjoWOqTvj6XHc8P5ZE43RBwNgDiM4gZJQnGto14RtYlgl176IeTkg8mo+EGeBQy/zLH3+4rZqp+J0fbYlRFaIpnuZk65uGbiE9PgJ9VGfipXS8lvMT1tw7lnwk9Oc= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1752587838; c=relaxed/simple; bh=cWoFGhsX3Drn12tLMnkbxHSzNAR9EJfONkqKeSRD7IE=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=bbqyhFQkNPC/VOKYzRFOMOr9EfgpZgIDJIkAo5fRUQ2m5Cd9nv9lsaYEFUSqWjBx/eW7gncNThQt8WoFf+NTRZW6hPvzm/tT3sZVQJ4emAlCIqWWzUI081kEYrmODx+N3NCWcxpH6mYQCs4b0ibzs6V7iW/ftcnNGWH5WDSKj84= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linuxfoundation.org header.i=@linuxfoundation.org header.b=HBRj2pDN; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linuxfoundation.org header.i=@linuxfoundation.org header.b="HBRj2pDN" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 2E12DC4CEE3; Tue, 15 Jul 2025 13:57:18 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1752587838; bh=cWoFGhsX3Drn12tLMnkbxHSzNAR9EJfONkqKeSRD7IE=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=HBRj2pDNwmSwI21WtEhVi8+Y0OxfEufgh2pIFgNB6twgetOp0ojV3odVSoPSADQCY L7B4FnAMai7hCV5nUnZc/gOiVurwgFSiCBklccu9DkWekQClgH+vC6DzxnXZnyIgpV 0YfXOe28u4q9cqYsJ5szHbxcgzbjMr6rxjG65Q5g= From: Greg Kroah-Hartman To: stable@vger.kernel.org Cc: Greg Kroah-Hartman , patches@lists.linux.dev, "Borislav Petkov (AMD)" , "Peter Zijlstra (Intel)" , Pawan Gupta Subject: [PATCH 5.10 161/208] x86/alternative: Optimize returns patching Date: Tue, 15 Jul 2025 15:14:30 +0200 Message-ID: <20250715130817.431019615@linuxfoundation.org> X-Mailer: git-send-email 2.50.1 In-Reply-To: <20250715130810.830580412@linuxfoundation.org> References: <20250715130810.830580412@linuxfoundation.org> User-Agent: quilt/0.68 X-stable: review X-Patchwork-Hint: ignore Precedence: bulk X-Mailing-List: stable@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 5.10-stable review patch. If anyone has any objections, please let me know. ------------------ From: "Borislav Petkov (AMD)" commit d2408e043e7296017420aa5929b3bba4d5e61013 upstream. Instead of decoding each instruction in the return sites range only to realize that that return site is a jump to the default return thunk which is needed - X86_FEATURE_RETHUNK is enabled - lift that check before the loop and get rid of that loop overhead. Add comments about what gets patched, while at it. Signed-off-by: Borislav Petkov (AMD) Acked-by: Peter Zijlstra (Intel) Link: https://lore.kernel.org/r/20230512120952.7924-1-bp@alien8.de Signed-off-by: Pawan Gupta Signed-off-by: Greg Kroah-Hartman --- arch/x86/kernel/alternative.c | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) --- a/arch/x86/kernel/alternative.c +++ b/arch/x86/kernel/alternative.c @@ -775,13 +775,12 @@ static int patch_return(void *addr, stru { int i = 0; + /* Patch the custom return thunks... */ if (cpu_feature_enabled(X86_FEATURE_RETHUNK)) { - if (x86_return_thunk == __x86_return_thunk) - return -1; - i = JMP32_INSN_SIZE; __text_gen_insn(bytes, JMP32_INSN_OPCODE, addr, x86_return_thunk, i); } else { + /* ... or patch them out if not needed. */ bytes[i++] = RET_INSN_OPCODE; } @@ -794,6 +793,14 @@ void __init_or_module noinline apply_ret { s32 *s; + /* + * Do not patch out the default return thunks if those needed are the + * ones generated by the compiler. + */ + if (cpu_feature_enabled(X86_FEATURE_RETHUNK) && + (x86_return_thunk == __x86_return_thunk)) + return; + for (s = start; s < end; s++) { void *dest = NULL, *addr = (void *)s + *s; struct insn insn;