From patchwork Fri Dec 27 07:28:18 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mike Rapoport X-Patchwork-Id: 13921583 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 kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 3E0F1E7718B for ; Fri, 27 Dec 2024 07:28:59 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id AE5F86B0083; Fri, 27 Dec 2024 02:28:58 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id A94DA6B0085; Fri, 27 Dec 2024 02:28:58 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 935C26B0088; Fri, 27 Dec 2024 02:28:58 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id 75A606B0083 for ; Fri, 27 Dec 2024 02:28:58 -0500 (EST) Received: from smtpin01.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id 00935121BB8 for ; Fri, 27 Dec 2024 07:28:57 +0000 (UTC) X-FDA: 82939910214.01.68BB48E Received: from nyc.source.kernel.org (nyc.source.kernel.org [147.75.193.91]) by imf03.hostedemail.com (Postfix) with ESMTP id 1518D20005 for ; Fri, 27 Dec 2024 07:28:36 +0000 (UTC) Authentication-Results: imf03.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b="XIKjst/1"; spf=pass (imf03.hostedemail.com: domain of rppt@kernel.org designates 147.75.193.91 as permitted sender) smtp.mailfrom=rppt@kernel.org; dmarc=pass (policy=quarantine) header.from=kernel.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1735284516; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=+HrNZ99qkNC8KEVuL9mBInkUyujn5g9T+2i2xoZ40eA=; b=QiUF8KvUgI4dJefQ0DmqT5+aM+L+pRD7tf5jshPOLl/rps4fVQfTOXU3y7hWno39um6SbF aZmMdqKN+tGy8bOpuf9FKKHpgC3zJEIUcmlqoAA2s7+DM4xX+Z0tNme2DDM/yrULN72rfj IOS99SEfj5XHUIOwoZE/eyZ3UiavfDk= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1735284516; a=rsa-sha256; cv=none; b=tkfd+8MeRgYkjMsfyi93ahpb4eeQZk8hnmp/KK8bVDNSt4GJKG0VbJRt/uJGsY/VrKknAi wvBESO/wUg9kifZq0mtD69F1O2St8BSk7FTZYvc+U1LQgXp62QyXLaRnQTkk2Sr1a16NMU Z911iRnh2xWbnDynqVC0PGvf+M45vds= ARC-Authentication-Results: i=1; imf03.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b="XIKjst/1"; spf=pass (imf03.hostedemail.com: domain of rppt@kernel.org designates 147.75.193.91 as permitted sender) smtp.mailfrom=rppt@kernel.org; dmarc=pass (policy=quarantine) header.from=kernel.org Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by nyc.source.kernel.org (Postfix) with ESMTP id 121E2A411C2; Fri, 27 Dec 2024 07:27:06 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id BE67BC4CEE0; Fri, 27 Dec 2024 07:28:45 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1735284535; bh=dDY3jvGc45EmH43gWTWL5MYq9/Q1o2IM8rTUBWv64F4=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=XIKjst/1wY9OLHzpWhKj4UYzvvFmcR4hmg80K6xgftHuqpsmSc/WhiGud59GDjjzi aNz3dTtAuVNVEHjllJa1agh/PcGzzQ66TpAnxGLd8odvdPn/NGL5AyLFLNK0v+Lnhp bhPN1Os0xv0DFA2Rowt02pnOqtwzGCgnOcp8m0lYJbqUII24zTaeAR3jrs4wuFfxUY +QIvMRxsnN0EZtVPbUliy1r7vLNbumpP1LYjLd+z4MU8NZ06lZWiXEn3S3nkWyCjKQ N/1w2f1V8lMe32qPhndZ+dbjWAY10MCC6iOXhG754bFZ9u/qEZfhDQflknVMn5jliH ZskGTUVYsxgjw== From: Mike Rapoport To: Andrew Morton Cc: Andy Lutomirski , Anton Ivanov , Borislav Petkov , Brendan Higgins , Daniel Gomez , Daniel Thompson , Dave Hansen , David Gow , Douglas Anderson , Ingo Molnar , Jason Wessel , Jiri Kosina , Joe Lawrence , Johannes Berg , Josh Poimboeuf , "Kirill A. Shutemov" , Luis Chamberlain , Mark Rutland , Masami Hiramatsu , Mike Rapoport , Miroslav Benes , "H. Peter Anvin" , Peter Zijlstra , Petr Mladek , Petr Pavlu , Rae Moar , Richard Weinberger , Sami Tolvanen , Shuah Khan , Song Liu , Steven Rostedt , Thomas Gleixner , kgdb-bugreport@lists.sourceforge.net, kunit-dev@googlegroups.com, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, linux-mm@kvack.org, linux-modules@vger.kernel.org, linux-trace-kernel@vger.kernel.org, linux-um@lists.infradead.org, live-patching@vger.kernel.org, x86@kernel.org Subject: [PATCH 1/8] x86/mm/pat: cpa-test: fix length for CPA_ARRAY test Date: Fri, 27 Dec 2024 09:28:18 +0200 Message-ID: <20241227072825.1288491-2-rppt@kernel.org> X-Mailer: git-send-email 2.45.2 In-Reply-To: <20241227072825.1288491-1-rppt@kernel.org> References: <20241227072825.1288491-1-rppt@kernel.org> MIME-Version: 1.0 X-Rspamd-Server: rspam10 X-Rspamd-Queue-Id: 1518D20005 X-Stat-Signature: 86exncaerx71btuhgqmusoqydjh3b7zp X-Rspam-User: X-HE-Tag: 1735284516-384223 X-HE-Meta: U2FsdGVkX183Jndjaj5XgkqTqa/MmxEw09v04TNRY+uf7LM0NWoi2PPBJz2T1TO+vZ8X7kAu+AJjbkc0RzMmfUHULlWNQv2JkWbDIPZRVWaDg883npRqb/TKIfDmYN5Kpo+ZlgkMqM3jvYizc/BGTcnFF/gwqnuBUKDaXYQDH0oqODjfjKpJTNQ+YOvP34IvGbh/OkHR+gM4p27b3orD2cKHjyLN7P3cS7zfpBfs7XEugMyVLSgHDoeTOCvamXLS1neavNf3FkHyXiuO5Sw8MI5fE46wGTInOOUqeyVU0bgr+d6HZ3FkEkMCpXG9lzuHZFw6uSMBiHg7eCzgbSztZYHfKO06EP4xgmZq7ifZJDHJ77R6z0uMp6XCqvfGFEzpCkneJ8eZhHcOSGiqP8HOat+utXQuHq4ZfH3THBaCCtz9QGGM5eMRyNJTJSbOM9PxS4jW1V2IB8116Ssl2635IIaWwPTyzt00Tm0/FMh9NNv8dpaPqwV4ifZgVrxV8vZQ04AFxK3YooB2P6OISeol+dRSC2m91mndUwOcQj4DlqJoPMa9cfakFh3bFW7rDur1eELlkOP15l/B/BISBDPQj/ABa8vEkjDBCxLao/CiacVJQtQ0ye67s4kiz4bnimYwfD0exWXfugxEZGBgz9AOhIXfFLKrAax6Mq8eKiypMlIQ12ZQhLyE6iF/IGggTrzoUdAFaMl0tM5ER5S+WHO/9h5XQQpe+JFP/bmgB6v+elp3UiSuJQdWLuogM12yDLvmmiS3+j3cyZOyS1zbE8h4nO3qrJlTKku3bmGxWdZimz/gTaGae2PrVsj8/3fMteFF3Tf8izXR1oDy8qYoh2Es569Tss+kN4HVkDefYuGQUEpi4wf+hNTUDVgheY9ta56kleH1T+GyC3CgthCwN6m7Te8Nz+LBpDnQoLjK4RbjcIPAV3vG1zyV2iy2bOtqpDmvnxBA8I9mu1nubGcfwTX VzooNYex hQTUMvs8mV6WcNg9QDDa5VBRKa/pk4kFc1bBNuad5u0+5SU8CxeY+i1HBsMjdeVybu6x2Ao6hTxaoTuiQzxX1uMDwM4CmF8qj8m26T6U77qbVHSyBgRhi1HRlkbo9k/oyRL+R75/4jJepWagBUUqRMLx9bApO2LIa2KJqfJJ/XJhW+V/gk4gtHG/Wi+17c0iuZjPo37LVK9YMU/ccEDlOOp1CRHgjQ891xo4CXkQFArfPhqPvRjF+Gr5hck2T0suL3K4DYaN1+5MPAYl/9nFmkC2HVw== X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: From: "Mike Rapoport (Microsoft)" The CPA_ARRAY test always uses len[1] as numpages argument to change_page_attr_set() although the addresses array is different each iteration of the test loop. Replace len[1] with len[i] to have numpages matching the addresses array. Fixes: ecc729f1f471 ("x86/mm/cpa: Add ARRAY and PAGES_ARRAY selftests") Signed-off-by: Mike Rapoport (Microsoft) --- arch/x86/mm/pat/cpa-test.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/arch/x86/mm/pat/cpa-test.c b/arch/x86/mm/pat/cpa-test.c index 3d2f7f0a6ed1..ad3c1feec990 100644 --- a/arch/x86/mm/pat/cpa-test.c +++ b/arch/x86/mm/pat/cpa-test.c @@ -183,7 +183,7 @@ static int pageattr_test(void) break; case 1: - err = change_page_attr_set(addrs, len[1], PAGE_CPA_TEST, 1); + err = change_page_attr_set(addrs, len[i], PAGE_CPA_TEST, 1); break; case 2: From patchwork Fri Dec 27 07:28:19 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mike Rapoport X-Patchwork-Id: 13921584 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 kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 692CAE7718B for ; Fri, 27 Dec 2024 07:29:09 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 085396B0088; Fri, 27 Dec 2024 02:29:09 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 036056B0089; Fri, 27 Dec 2024 02:29:08 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id E18356B008C; Fri, 27 Dec 2024 02:29:08 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id B9AF76B0088 for ; Fri, 27 Dec 2024 02:29:08 -0500 (EST) Received: from smtpin21.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id 3C5C51C6179 for ; Fri, 27 Dec 2024 07:29:08 +0000 (UTC) X-FDA: 82939911348.21.AED9EDC Received: from nyc.source.kernel.org (nyc.source.kernel.org [147.75.193.91]) by imf27.hostedemail.com (Postfix) with ESMTP id 8C26E40015 for ; Fri, 27 Dec 2024 07:28:20 +0000 (UTC) Authentication-Results: imf27.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=I62KKwaq; spf=pass (imf27.hostedemail.com: domain of rppt@kernel.org designates 147.75.193.91 as permitted sender) smtp.mailfrom=rppt@kernel.org; dmarc=pass (policy=quarantine) header.from=kernel.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1735284514; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=1vYutUlTp3JX+hNbxqRR2ioYt+s65i5HZVu0KwP9iP0=; b=LQ2G43AnCHf0dUoZ3g5IjMxZ7Z7md1tY1U7C+w7ZUH4p5ft7467f+lUQwgJU5GBjalghjq K4MLXbJlQVCBS1AMquYf9Kf0B/Q8T3X2Vj5kcg4pVrgS0oPKQFo+PQzqSLvo6xbZZ//UcH h8W15K84wWHW+62l9jvid0jr59MBjsg= ARC-Authentication-Results: i=1; imf27.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=I62KKwaq; spf=pass (imf27.hostedemail.com: domain of rppt@kernel.org designates 147.75.193.91 as permitted sender) smtp.mailfrom=rppt@kernel.org; dmarc=pass (policy=quarantine) header.from=kernel.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1735284514; a=rsa-sha256; cv=none; b=mR6JbUHvv9UpKy0cCL4rMSOvl1DkLCKKE+xHkwJsuJ+9cb2JX5sjCatdiXfaaQ9ZiUtqGu r39fzdBFBeLNklRNpXPSrUUhtovikYb1mreE9wY4VpEY4wBljvZUqr8WaZjR8K4rZluuOv exvpFi4x3e90/iucTG9UKoY2+lzluKQ= Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by nyc.source.kernel.org (Postfix) with ESMTP id 5AA68A411CA; Fri, 27 Dec 2024 07:27:16 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 12AF3C4CED3; Fri, 27 Dec 2024 07:28:55 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1735284545; bh=UnbrmexawgONPYMAXBvIF3DzXjQu5/b5KeCmUo86HRs=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=I62KKwaqJJhDmiRCOCFElqHOy0IrCUY7TgPD5oMFrVNNByrLNnJBAipwrg+0oazxz y3mtq9sKsa10GB3u8d7homMHDG3ejg6hZyXmqysnv3y72zQQVlk8gv9d80xJdQcAJE 7YWjLe1ZojkJECdhcV0Wy41WZB6wFXsuMBr1Qx0M17eU6vI/kUU9rpV2Q13ZDYTuj1 lwQUffG/rxRE/zOezN0IZLWyGMwQ12f4fnQs2qkkCEoxxkcP/2Pn4WBnEkiWebofpa 38EaHsODPEdVfQM4+KUaaXDC7p/NU+TzuG54YRN5ADzQA7S0uqpNaiowx7uOZeEfu3 COzDVqsamtyWA== From: Mike Rapoport To: Andrew Morton Cc: Andy Lutomirski , Anton Ivanov , Borislav Petkov , Brendan Higgins , Daniel Gomez , Daniel Thompson , Dave Hansen , David Gow , Douglas Anderson , Ingo Molnar , Jason Wessel , Jiri Kosina , Joe Lawrence , Johannes Berg , Josh Poimboeuf , "Kirill A. Shutemov" , Luis Chamberlain , Mark Rutland , Masami Hiramatsu , Mike Rapoport , Miroslav Benes , "H. Peter Anvin" , Peter Zijlstra , Petr Mladek , Petr Pavlu , Rae Moar , Richard Weinberger , Sami Tolvanen , Shuah Khan , Song Liu , Steven Rostedt , Thomas Gleixner , kgdb-bugreport@lists.sourceforge.net, kunit-dev@googlegroups.com, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, linux-mm@kvack.org, linux-modules@vger.kernel.org, linux-trace-kernel@vger.kernel.org, linux-um@lists.infradead.org, live-patching@vger.kernel.org, x86@kernel.org Subject: [PATCH 2/8] x86/mm/pat: drop duplicate variable in cpa_flush() Date: Fri, 27 Dec 2024 09:28:19 +0200 Message-ID: <20241227072825.1288491-3-rppt@kernel.org> X-Mailer: git-send-email 2.45.2 In-Reply-To: <20241227072825.1288491-1-rppt@kernel.org> References: <20241227072825.1288491-1-rppt@kernel.org> MIME-Version: 1.0 X-Rspamd-Queue-Id: 8C26E40015 X-Rspamd-Server: rspam12 X-Stat-Signature: ke1h151cyypa3ms6tf5x41ie6amr8xcb X-Rspam-User: X-HE-Tag: 1735284500-693387 X-HE-Meta: U2FsdGVkX19rvbX1zHkj/dK/Z8sqXkHDYH0lmqLiONNqms60yb1D+b/RDCK8LkTBVpwHSY56gEJYf07T33ExilLv5vI3nKpKaO2R6o1Uef/05rcEm6ge+QqVDLd911J/KvboFkENu2n+TWOMF2KRDvUD7AmixxJIsFDczvBoUJsTJwBP4J1Z4JcLw0AJ1CwQDnJIN2FCMeaIaxgD2dxkfNbR0ZJJ/NGo3MdYSbxyiKkHNggfx422b5oJFTo6CasvcDmsg8K3hyJFM3r5xSxoLdqXJ5YXjwfgs8XRzG+zgYaRbw7Gochz9ZCOvVNtRjW91vwqPE3BQ68+ZkDR2NhHaARlgnRjWtMGqw1wxj301YKQN4INvgMpOh4DayXTJattiUsJjkMY389F+BA7/n4/tZgiM5uqHePsK88dxIhGmhYip7wUy1tiwjjl0WznDEgtzN2XXx+D2X7ZdpESncJTNPuEHEwHB3q5H2f71Pg59dr1nr6VKUD5J/6NNe2K+10uMm5KatTHjxr03pUlCCJGVBTcjEQJzUbqF6vxewVYUONCJLLfotmPd1KqI41TLhpwCdbvLq6FzxByrMJmjU5kFpIVEwo0HmdFBhtZoWLuUr5uwwUnPM8cqaUYgaotXfbSsNN6WRnECudW29kkv7VtKbrBMdGjaUm+zpZyhrlDxMN2CG3AsBCBhuD200lyB5N34sxDERFmVNNZk+GjWHDeSZz1Kek1EmKBhLWh4yE7g2xAD6moq6UQDDHtxNIdfgXFx11Gp7x1FE2Yn37mVrF/ODyhwir+XRtGCrtcKEui7JkbCqEWlsxD5HwxdmuE0Gm/uIO5wJlGLm1z2ZFjLrJ3BzlYrVcGUmWOusbPkrcJ9H50dFW9UrlddgwcsKfANMqrtfd48cGrKPa74riaKQaUbZgocsfz4LsabQZi6129YVMXV4dqJVSK4TYtAVqW6/xjTuOH2om4YmwcUjvovZD 8hmLvwdp QVPUOgouugZmH2HZGUCmkUDYus5u7VdlfWcyYN3SWc4RbKQGkcVfOB66sohgL0wDEYZpR0VmyFQZ2OfbBNQU2Ex2q/6fIs0hXj+eHiUETzaWkXo3VltLzX7JdMAcWbsyAY5S+5F5/FdYBeGJjkg1ctlJ8UebxsFAncwQHGBGH1tQ7tYi6mHELcpwZgakBUFPne1uELOI51mmMN7lQDE10jnP42HGqkSvHlCvPwQvyKQyUoKxIGyv3n54ScQQVNoXWToGKzx5ZUzyqU+QAoLiWoBvdWw== X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: From: "Mike Rapoport (Microsoft)" There is a 'struct cpa_data *data' parameter in cpa_flush() that is assigned to a local 'struct cpa_data *cpa' variable. Rename the parameter from 'data' to 'cpa' and drop declaration of the local 'cpa' variable. Signed-off-by: Mike Rapoport (Microsoft) --- arch/x86/mm/pat/set_memory.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/arch/x86/mm/pat/set_memory.c b/arch/x86/mm/pat/set_memory.c index 95bc50a8541c..d43b919b11ae 100644 --- a/arch/x86/mm/pat/set_memory.c +++ b/arch/x86/mm/pat/set_memory.c @@ -396,9 +396,8 @@ static void __cpa_flush_tlb(void *data) flush_tlb_one_kernel(fix_addr(__cpa_addr(cpa, i))); } -static void cpa_flush(struct cpa_data *data, int cache) +static void cpa_flush(struct cpa_data *cpa, int cache) { - struct cpa_data *cpa = data; unsigned int i; BUG_ON(irqs_disabled() && !early_boot_irqs_disabled); From patchwork Fri Dec 27 07:28:20 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mike Rapoport X-Patchwork-Id: 13921585 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 kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id E7BEAE7718B for ; Fri, 27 Dec 2024 07:29:19 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 8272F6B008C; Fri, 27 Dec 2024 02:29:19 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 7D68A6B0092; Fri, 27 Dec 2024 02:29:19 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 6501D6B0093; Fri, 27 Dec 2024 02:29:19 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id 458A36B008C for ; Fri, 27 Dec 2024 02:29:19 -0500 (EST) Received: from smtpin27.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id C94D2C04AD for ; Fri, 27 Dec 2024 07:29:18 +0000 (UTC) X-FDA: 82939911474.27.5071357 Received: from nyc.source.kernel.org (nyc.source.kernel.org [147.75.193.91]) by imf28.hostedemail.com (Postfix) with ESMTP id 0C6CBC0003 for ; Fri, 27 Dec 2024 07:28:30 +0000 (UTC) Authentication-Results: imf28.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=hh80oV+h; spf=pass (imf28.hostedemail.com: domain of rppt@kernel.org designates 147.75.193.91 as permitted sender) smtp.mailfrom=rppt@kernel.org; dmarc=pass (policy=quarantine) header.from=kernel.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1735284517; a=rsa-sha256; cv=none; b=aOAJbvQZurcDek/d9YEMA8efUF07CxnnEXMrBMf3nojgUko/+STzBlekriJhqIKPlW9aV6 xAPBu6pjFGwWdpMWQsQWCLUkjjWWh9VlPG8jdRLn2eZFjM1olzoK/y18Jsqo481m/DrAtK JNZrcJTsgDNnHmZv0a4LQ7VNJlq190I= ARC-Authentication-Results: i=1; imf28.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=hh80oV+h; spf=pass (imf28.hostedemail.com: domain of rppt@kernel.org designates 147.75.193.91 as permitted sender) smtp.mailfrom=rppt@kernel.org; dmarc=pass (policy=quarantine) header.from=kernel.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1735284517; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=oXG5MyIbQzv7o0Lx6HDLDLWEGr49bAOyHkpQqyIf/po=; b=MZzc+t87eN2M0Tz0xmc89irBhxmnOwYta1zo6MyxFmS2lC1dBrRiAcCuz2J2tBrLmKEixI dpUOEvFQSkFXJZNkT7CrhgISuY8tbFNhif2Yg157BNpZpksi6Gja+56rLM7nLyyeJATlYZ C3lH8AvCtTf+ZCdbSJxz6eE8F4TfUAY= Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by nyc.source.kernel.org (Postfix) with ESMTP id A2B37A4120A; Fri, 27 Dec 2024 07:27:26 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 5AE6AC4CED0; Fri, 27 Dec 2024 07:29:06 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1735284556; bh=FqmeKOdxdJ7a2m5t/X9aYwdfjOV0nVl0+/73FfCFau0=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=hh80oV+hFNDxk2u7u2M5S6a+d8LX0hjSp0XpKgfOj04wjrYxAJPeXs+POJREwL41U JR/qH4kYzWMQgMWbk83uSdtwe1A697JTr8qTOO/i80nDu1xSbLzIckQGQn5r3usg6A F8kTaV1y3sT0bYQOwSKqT85evFAgwX/N45O5XAmWgAOGuH2SGPoTSeWB0r1yVzxqxT PCRKOs3HflTSffBeVV44vMRIuO4wADCmWaX125GZGZdGW2ewck9DqhC39XLO6ytaEG 3tywgHm1LfzkrvBLGY77jOs0OxfTdEou09qrxJGsizJkWldALgu8KjGTRPsACisDMf RIdYinWh5g3ew== From: Mike Rapoport To: Andrew Morton Cc: Andy Lutomirski , Anton Ivanov , Borislav Petkov , Brendan Higgins , Daniel Gomez , Daniel Thompson , Dave Hansen , David Gow , Douglas Anderson , Ingo Molnar , Jason Wessel , Jiri Kosina , Joe Lawrence , Johannes Berg , Josh Poimboeuf , "Kirill A. Shutemov" , Luis Chamberlain , Mark Rutland , Masami Hiramatsu , Mike Rapoport , Miroslav Benes , "H. Peter Anvin" , Peter Zijlstra , Petr Mladek , Petr Pavlu , Rae Moar , Richard Weinberger , Sami Tolvanen , Shuah Khan , Song Liu , Steven Rostedt , Thomas Gleixner , kgdb-bugreport@lists.sourceforge.net, kunit-dev@googlegroups.com, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, linux-mm@kvack.org, linux-modules@vger.kernel.org, linux-trace-kernel@vger.kernel.org, linux-um@lists.infradead.org, live-patching@vger.kernel.org, x86@kernel.org Subject: [PATCH 3/8] x86/mm/pat: Restore large pages after fragmentation Date: Fri, 27 Dec 2024 09:28:20 +0200 Message-ID: <20241227072825.1288491-4-rppt@kernel.org> X-Mailer: git-send-email 2.45.2 In-Reply-To: <20241227072825.1288491-1-rppt@kernel.org> References: <20241227072825.1288491-1-rppt@kernel.org> MIME-Version: 1.0 X-Rspamd-Queue-Id: 0C6CBC0003 X-Stat-Signature: zde5cjg5zdzxg6dztqb3hzdb5ep4yftu X-Rspam-User: X-Rspamd-Server: rspam09 X-HE-Tag: 1735284510-324106 X-HE-Meta: U2FsdGVkX18nI9kLJPT76pOlCaN7PcYggC5gwNxQLnafjxy8BQSJSTEep07bCS2v4VZEULPueSvCeoka+eK77Y+ghNk9Vm8h3ASRznxacw/aD0yRvLMR8f0sTQUCJaQRjyEa1PWScLsAl4krS3YMkfR/J/pqB7Tc0VsXquM6NA+9ClMQUlh1ZBSC9Iika9tvNh7mHYLmReSe24F3w0h7oMOYAtE2zbeuFA24P0xredQC+S3pbLpZ0irS4FiT/rlT49hjnqMxPgaYWwzla4GNE5vIjpoXoNzfWm7F+OLXLibtLESEQsditx5Qe/Pgnp8oXiINUetwpY2mfoQE/rJ7Rkk+zp3v5F+IyeAF1Fzj673EwVwjU1I3dx8aBfFVUnfAuyszX3QgnSi82dAXbsP62oOUGPvEpbzm/GNyiMVI2EvP2vo49I1Xhnw1bmd39CgxxVvH/uBWbfmMuF2vZ35wKt8v0iSrQrbco/FnHGUT/QyPPMClrLbv8fnw16yHcAu0jVgMCj37v0cJ5hCWHDGD+KByGyGvlvo29l7SSjaiMnzPUVAFq8EKtdN4yls0z6Vp8DO3cT3CWGG17tp2ssewy4HmwAQof5xd2aA0tzbJOulYoa0vVmroW13pXzYWXPtY1R+8yiWNhOSWo/iYaIgohZj9vBQJ9F80/xtGJ+6NB/KQImYlNdahLJytT5Ym5h+oHNZe1O4KYhdYorPMieSKqRiV5eFT5PgKQxNDTW4fytWnHCbsW7AMZBsY9O4DUwSamZclkxIRn/q1JRdB8l2g0YuybwAo5q7wKJh45916fUla8YgYvcBzn5icFcVTK934HLXl8nffdFxbboDWcCCn2TJW/gzJHRF4Gpx/0F6REXOHM5KZiNyD/dozv3zYuGG+TCtVCPGJwaKNJx8FUc+tVVA/oTQ4nyYErtUxdOoS/wMHVXoaD7B2uQ1T3tzjfJb4cbTuZNKpff50BmlufRU as/OTQo/ HgCOqcDgJ0eNRldapXC7Vt7g7JH0E0k8PnUCCtg698vEnAyn1azJb0Cos4zST/cE238BqTC/T+xBNCs/nyz3ixryPphJgF3UGGcxqCu+yw/mXfZV9F/vx46tZcuhVVkdA8UHgS4EHzArsKTvHXHhwVzCsWaImmOJ8ls/YFpW+516Mk/yG8p4WmyH3m44O1qfQU2x5p7+IKTl5J8T+tH1MlmoMkBkEZfjy3TWERnm60k1s9Fg5CgiofaXjz93MzqStYpGI1spJAGRs6YKsVP7HaM1tSA== X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: From: "Kirill A. Shutemov" Change of attributes of the pages may lead to fragmentation of direct mapping over time and performance degradation as result. With current code it's one way road: kernel tries to avoid splitting large pages, but it doesn't restore them back even if page attributes got compatible again. Any change to the mapping may potentially allow to restore large page. Hook up into cpa_flush() path to check if there's any pages to be recovered in PUD_SIZE range around pages we've just touched. CPUs don't like[1] to have to have TLB entries of different size for the same memory, but looks like it's okay as long as these entries have matching attributes[2]. Therefore it's critical to flush TLB before any following changes to the mapping. Note that we already allow for multiple TLB entries of different sizes for the same memory now in split_large_page() path. It's not a new situation. set_memory_4k() provides a way to use 4k pages on purpose. Kernel must not remap such pages as large. Re-use one of software PTE bits to indicate such pages. [1] See Erratum 383 of AMD Family 10h Processors [2] https://lore.kernel.org/linux-mm/1da1b025-cabc-6f04-bde5-e50830d1ecf0@amd.com/ [rppt@kernel.org: * s/restore/collapse/ * update formatting per peterz * use 'struct ptdesc' instead of 'struct page' for list of page tables to be freed * try to collapse PMD first and if it succeeds move on to PUD as peterz suggested * flush TLB twice: for changes done in the original CPA call and after collapsing of large pages ] Link: https://lore.kernel.org/all/20200416213229.19174-1-kirill.shutemov@linux.intel.com Signed-off-by: Kirill A. Shutemov Co-developed-by: Mike Rapoport (Microsoft) Signed-off-by: Mike Rapoport (Microsoft) --- arch/x86/include/asm/pgtable_types.h | 2 + arch/x86/mm/pat/set_memory.c | 213 ++++++++++++++++++++++++++- include/linux/vm_event_item.h | 2 + mm/vmstat.c | 2 + 4 files changed, 216 insertions(+), 3 deletions(-) diff --git a/arch/x86/include/asm/pgtable_types.h b/arch/x86/include/asm/pgtable_types.h index 4b804531b03c..c90e9c51edb7 100644 --- a/arch/x86/include/asm/pgtable_types.h +++ b/arch/x86/include/asm/pgtable_types.h @@ -33,6 +33,7 @@ #define _PAGE_BIT_CPA_TEST _PAGE_BIT_SOFTW1 #define _PAGE_BIT_UFFD_WP _PAGE_BIT_SOFTW2 /* userfaultfd wrprotected */ #define _PAGE_BIT_SOFT_DIRTY _PAGE_BIT_SOFTW3 /* software dirty tracking */ +#define _PAGE_BIT_KERNEL_4K _PAGE_BIT_SOFTW3 /* page must not be converted to large */ #define _PAGE_BIT_DEVMAP _PAGE_BIT_SOFTW4 #ifdef CONFIG_X86_64 @@ -64,6 +65,7 @@ #define _PAGE_PAT_LARGE (_AT(pteval_t, 1) << _PAGE_BIT_PAT_LARGE) #define _PAGE_SPECIAL (_AT(pteval_t, 1) << _PAGE_BIT_SPECIAL) #define _PAGE_CPA_TEST (_AT(pteval_t, 1) << _PAGE_BIT_CPA_TEST) +#define _PAGE_KERNEL_4K (_AT(pteval_t, 1) << _PAGE_BIT_KERNEL_4K) #ifdef CONFIG_X86_INTEL_MEMORY_PROTECTION_KEYS #define _PAGE_PKEY_BIT0 (_AT(pteval_t, 1) << _PAGE_BIT_PKEY_BIT0) #define _PAGE_PKEY_BIT1 (_AT(pteval_t, 1) << _PAGE_BIT_PKEY_BIT1) diff --git a/arch/x86/mm/pat/set_memory.c b/arch/x86/mm/pat/set_memory.c index d43b919b11ae..89cd8176c99f 100644 --- a/arch/x86/mm/pat/set_memory.c +++ b/arch/x86/mm/pat/set_memory.c @@ -107,6 +107,18 @@ static void split_page_count(int level) direct_pages_count[level - 1] += PTRS_PER_PTE; } +static void collapse_page_count(int level) +{ + direct_pages_count[level]++; + if (system_state == SYSTEM_RUNNING) { + if (level == PG_LEVEL_2M) + count_vm_event(DIRECT_MAP_LEVEL2_COLLAPSE); + else if (level == PG_LEVEL_1G) + count_vm_event(DIRECT_MAP_LEVEL3_COLLAPSE); + } + direct_pages_count[level - 1] -= PTRS_PER_PTE; +} + void arch_report_meminfo(struct seq_file *m) { seq_printf(m, "DirectMap4k: %8lu kB\n", @@ -124,6 +136,7 @@ void arch_report_meminfo(struct seq_file *m) } #else static inline void split_page_count(int level) { } +static inline void collapse_page_count(int level) { } #endif #ifdef CONFIG_X86_CPA_STATISTICS @@ -396,15 +409,50 @@ static void __cpa_flush_tlb(void *data) flush_tlb_one_kernel(fix_addr(__cpa_addr(cpa, i))); } +static int collapse_large_pages(unsigned long addr, struct list_head *pgtables); + +static void cpa_collapse_large_pages(struct cpa_data *cpa) +{ + unsigned long start, addr, end; + struct ptdesc *ptdesc, *tmp; + LIST_HEAD(pgtables); + int collapsed = 0; + int i; + + if (cpa->flags & (CPA_PAGES_ARRAY | CPA_ARRAY)) { + for (i = 0; i < cpa->numpages; i++) + collapsed += collapse_large_pages(__cpa_addr(cpa, i), + &pgtables); + } else { + addr = __cpa_addr(cpa, 0); + start = addr & PMD_MASK; + end = addr + PAGE_SIZE * cpa->numpages; + + for (addr = start; within(addr, start, end); addr += PMD_SIZE) + collapsed += collapse_large_pages(addr, &pgtables); + } + + if (!collapsed) + return; + + flush_tlb_all(); + + list_for_each_entry_safe(ptdesc, tmp, &pgtables, pt_list) { + list_del(&ptdesc->pt_list); + __free_page(ptdesc_page(ptdesc)); + } +} + static void cpa_flush(struct cpa_data *cpa, int cache) { + LIST_HEAD(pgtables); unsigned int i; BUG_ON(irqs_disabled() && !early_boot_irqs_disabled); if (cache && !static_cpu_has(X86_FEATURE_CLFLUSH)) { cpa_flush_all(cache); - return; + goto collapse_large_pages; } if (cpa->force_flush_all || cpa->numpages > tlb_single_page_flush_ceiling) @@ -413,7 +461,7 @@ static void cpa_flush(struct cpa_data *cpa, int cache) on_each_cpu(__cpa_flush_tlb, cpa, 1); if (!cache) - return; + goto collapse_large_pages; mb(); for (i = 0; i < cpa->numpages; i++) { @@ -429,6 +477,9 @@ static void cpa_flush(struct cpa_data *cpa, int cache) clflush_cache_range_opt((void *)fix_addr(addr), PAGE_SIZE); } mb(); + +collapse_large_pages: + cpa_collapse_large_pages(cpa); } static bool overlaps(unsigned long r1_start, unsigned long r1_end, @@ -1198,6 +1249,161 @@ static int split_large_page(struct cpa_data *cpa, pte_t *kpte, return 0; } +static int collapse_pmd_page(pmd_t *pmd, unsigned long addr, + struct list_head *pgtables) +{ + pmd_t _pmd, old_pmd; + pte_t *pte, first; + unsigned long pfn; + pgprot_t pgprot; + int i = 0; + + addr &= PMD_MASK; + pte = pte_offset_kernel(pmd, addr); + first = *pte; + pfn = pte_pfn(first); + + /* Make sure alignment is suitable */ + if (PFN_PHYS(pfn) & ~PMD_MASK) + return 0; + + /* The page is 4k intentionally */ + if (pte_flags(first) & _PAGE_KERNEL_4K) + return 0; + + /* Check that the rest of PTEs are compatible with the first one */ + for (i = 1, pte++; i < PTRS_PER_PTE; i++, pte++) { + pte_t entry = *pte; + + if (!pte_present(entry)) + return 0; + if (pte_flags(entry) != pte_flags(first)) + return 0; + if (pte_pfn(entry) != pte_pfn(first) + i) + return 0; + } + + old_pmd = *pmd; + + /* Success: set up a large page */ + pgprot = pgprot_4k_2_large(pte_pgprot(first)); + pgprot_val(pgprot) |= _PAGE_PSE; + _pmd = pfn_pmd(pfn, pgprot); + set_pmd(pmd, _pmd); + + /* Queue the page table to be freed after TLB flush */ + list_add(&page_ptdesc(pmd_page(old_pmd))->pt_list, pgtables); + + if (IS_ENABLED(CONFIG_X86_32) && !SHARED_KERNEL_PMD) { + struct page *page; + + /* Update all PGD tables to use the same large page */ + list_for_each_entry(page, &pgd_list, lru) { + pgd_t *pgd = (pgd_t *)page_address(page) + pgd_index(addr); + p4d_t *p4d = p4d_offset(pgd, addr); + pud_t *pud = pud_offset(p4d, addr); + pmd_t *pmd = pmd_offset(pud, addr); + /* Something is wrong if entries doesn't match */ + if (WARN_ON(pmd_val(old_pmd) != pmd_val(*pmd))) + continue; + set_pmd(pmd, _pmd); + } + } + + if (virt_addr_valid(addr) && pfn_range_is_mapped(pfn, pfn + 1)) + collapse_page_count(PG_LEVEL_2M); + + return 1; +} + +static int collapse_pud_page(pud_t *pud, unsigned long addr, + struct list_head *pgtables) +{ + unsigned long pfn; + pmd_t *pmd, first; + int i; + + if (!direct_gbpages) + return 0; + + addr &= PUD_MASK; + pmd = pmd_offset(pud, addr); + first = *pmd; + + /* + * To restore PUD page all PMD entries must be large and + * have suitable alignment + */ + pfn = pmd_pfn(first); + if (!pmd_leaf(first) || (PFN_PHYS(pfn) & ~PUD_MASK)) + return 0; + + /* + * To restore PUD page, all following PMDs must be compatible with the + * first one. + */ + for (i = 1, pmd++; i < PTRS_PER_PMD; i++, pmd++) { + pmd_t entry = *pmd; + + if (!pmd_present(entry) || !pmd_leaf(entry)) + return 0; + if (pmd_flags(entry) != pmd_flags(first)) + return 0; + if (pmd_pfn(entry) != pmd_pfn(first) + i * PTRS_PER_PTE) + return 0; + } + + /* Restore PUD page and queue page table to be freed after TLB flush */ + list_add(&page_ptdesc(pud_page(*pud))->pt_list, pgtables); + set_pud(pud, pfn_pud(pfn, pmd_pgprot(first))); + + if (virt_addr_valid(addr) && pfn_range_is_mapped(pfn, pfn + 1)) + collapse_page_count(PG_LEVEL_1G); + + return 1; +} + +/* + * Collapse PMD and PUD pages in the kernel mapping around the address where + * possible. + * + * Caller must flush TLB and free page tables queued on the list before + * touching the new entries. CPU must not see TLB entries of different size + * with different attributes. + */ +static int collapse_large_pages(unsigned long addr, struct list_head *pgtables) +{ + int collapsed = 0; + pgd_t *pgd; + p4d_t *p4d; + pud_t *pud; + pmd_t *pmd; + + addr &= PMD_MASK; + + spin_lock(&pgd_lock); + pgd = pgd_offset_k(addr); + if (pgd_none(*pgd)) + goto out; + p4d = p4d_offset(pgd, addr); + if (p4d_none(*p4d)) + goto out; + pud = pud_offset(p4d, addr); + if (!pud_present(*pud) || pud_leaf(*pud)) + goto out; + pmd = pmd_offset(pud, addr); + if (!pmd_present(*pmd) || pmd_leaf(*pmd)) + goto out; + + collapsed = collapse_pmd_page(pmd, addr, pgtables); + if (collapsed) + collapsed += collapse_pud_page(pud, addr, pgtables); + +out: + spin_unlock(&pgd_lock); + return collapsed; +} + static bool try_to_free_pte_page(pte_t *pte) { int i; @@ -2148,7 +2354,8 @@ int set_memory_p(unsigned long addr, int numpages) int set_memory_4k(unsigned long addr, int numpages) { - return change_page_attr_set_clr(&addr, numpages, __pgprot(0), + return change_page_attr_set_clr(&addr, numpages, + __pgprot(_PAGE_KERNEL_4K), __pgprot(0), 1, 0, NULL); } diff --git a/include/linux/vm_event_item.h b/include/linux/vm_event_item.h index f70d0958095c..5a37cb2b6f93 100644 --- a/include/linux/vm_event_item.h +++ b/include/linux/vm_event_item.h @@ -151,6 +151,8 @@ enum vm_event_item { PGPGIN, PGPGOUT, PSWPIN, PSWPOUT, #ifdef CONFIG_X86 DIRECT_MAP_LEVEL2_SPLIT, DIRECT_MAP_LEVEL3_SPLIT, + DIRECT_MAP_LEVEL2_COLLAPSE, + DIRECT_MAP_LEVEL3_COLLAPSE, #endif #ifdef CONFIG_PER_VMA_LOCK_STATS VMA_LOCK_SUCCESS, diff --git a/mm/vmstat.c b/mm/vmstat.c index 4d016314a56c..d5e1d7f34f3c 100644 --- a/mm/vmstat.c +++ b/mm/vmstat.c @@ -1435,6 +1435,8 @@ const char * const vmstat_text[] = { #ifdef CONFIG_X86 "direct_map_level2_splits", "direct_map_level3_splits", + "direct_map_level2_collapses", + "direct_map_level3_collapses", #endif #ifdef CONFIG_PER_VMA_LOCK_STATS "vma_lock_success", From patchwork Fri Dec 27 07:28:21 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mike Rapoport X-Patchwork-Id: 13921586 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 kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 9F3D0E7718B for ; Fri, 27 Dec 2024 07:29:30 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 121996B0093; Fri, 27 Dec 2024 02:29:30 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 0D2786B0095; Fri, 27 Dec 2024 02:29:30 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id EB47C6B0096; Fri, 27 Dec 2024 02:29:29 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id CE4EA6B0093 for ; Fri, 27 Dec 2024 02:29:29 -0500 (EST) Received: from smtpin28.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id 4190D140430 for ; Fri, 27 Dec 2024 07:29:29 +0000 (UTC) X-FDA: 82939913154.28.3694B90 Received: from nyc.source.kernel.org (nyc.source.kernel.org [147.75.193.91]) by imf16.hostedemail.com (Postfix) with ESMTP id 37973180004 for ; Fri, 27 Dec 2024 07:28:44 +0000 (UTC) Authentication-Results: imf16.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b="V7rL/1Cg"; spf=pass (imf16.hostedemail.com: domain of rppt@kernel.org designates 147.75.193.91 as permitted sender) smtp.mailfrom=rppt@kernel.org; dmarc=pass (policy=quarantine) header.from=kernel.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1735284518; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=2g+SxczDYlrf43JjC8UJiDwBf+Z9UmJvZ80gCTVUkgY=; b=GJEz2VhAeb/a8PlvAM3VAW60NJ/MxVrNEmdfgZq1rtSxXLu8pYinAYjKPqFrigWPx0pkC3 foT0Q3dfjvlYctTQVwJyxnAiLU0kcnt3aUTTmZPeohT5odrE3r9BqBrETf+1B77psfXAG2 wDpn8SW9eAdaaSt/ZO18E1Z5RKQfUs4= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1735284518; a=rsa-sha256; cv=none; b=H+9i5OwzAup5F4FGPsDxhaYZ5pg/GY/WPh8onauNi95DC/mD5gWAofnD5NfRF9ifFhh5RR 1FkI5LAZqdgoU5DesHeLGHNHOZA95/Y0TMp+P7E3Zbq1m6euTH4FAVSGvQWH40Oa7nSYOV UqzYF/HRnD5qm3gz/ku0uWLMNDX7QXc= ARC-Authentication-Results: i=1; imf16.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b="V7rL/1Cg"; spf=pass (imf16.hostedemail.com: domain of rppt@kernel.org designates 147.75.193.91 as permitted sender) smtp.mailfrom=rppt@kernel.org; dmarc=pass (policy=quarantine) header.from=kernel.org Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by nyc.source.kernel.org (Postfix) with ESMTP id EAFEDA41216; Fri, 27 Dec 2024 07:27:36 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id A41ABC4CED7; Fri, 27 Dec 2024 07:29:16 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1735284566; bh=eadd2fhesnrBbYUtyPuDOjZKluNBdPkoQ6My17z11ds=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=V7rL/1CgjCZvQAHg7NIcED5Na02EGjDigmzncnfAGdsE4oPXN9GFMNUqOgGKcIAxK hSznXGjRdJakuaXWQ8k1hzlFSbmpLCpkjLNAHE9LAlkf8Pyn2im17Lax3zToCNnlDO sF35OhD0vR+mDq6BWIXP/rORjFkoB2OmxqrXTUnH03R297ViXYi/DxGzjgG/PiNdu0 UHjn6xvvqWwhXwUiJ2EvGaz72tB1oOF/eaVLHsZ/Slq/nm9qigaXwYR1thVtB6yGTd HfkagPFgCDXBJAQttID2sWzOm9fw+FJoE9uewLZw8sH2jQV7a2Ah68AXl/hMetNBpC 0CKPg/BqE8lzg== From: Mike Rapoport To: Andrew Morton Cc: Andy Lutomirski , Anton Ivanov , Borislav Petkov , Brendan Higgins , Daniel Gomez , Daniel Thompson , Dave Hansen , David Gow , Douglas Anderson , Ingo Molnar , Jason Wessel , Jiri Kosina , Joe Lawrence , Johannes Berg , Josh Poimboeuf , "Kirill A. Shutemov" , Luis Chamberlain , Mark Rutland , Masami Hiramatsu , Mike Rapoport , Miroslav Benes , "H. Peter Anvin" , Peter Zijlstra , Petr Mladek , Petr Pavlu , Rae Moar , Richard Weinberger , Sami Tolvanen , Shuah Khan , Song Liu , Steven Rostedt , Thomas Gleixner , kgdb-bugreport@lists.sourceforge.net, kunit-dev@googlegroups.com, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, linux-mm@kvack.org, linux-modules@vger.kernel.org, linux-trace-kernel@vger.kernel.org, linux-um@lists.infradead.org, live-patching@vger.kernel.org, x86@kernel.org Subject: [PATCH 4/8] execmem: add API for temporal remapping as RW and restoring ROX afterwards Date: Fri, 27 Dec 2024 09:28:21 +0200 Message-ID: <20241227072825.1288491-5-rppt@kernel.org> X-Mailer: git-send-email 2.45.2 In-Reply-To: <20241227072825.1288491-1-rppt@kernel.org> References: <20241227072825.1288491-1-rppt@kernel.org> MIME-Version: 1.0 X-Rspamd-Queue-Id: 37973180004 X-Rspam-User: X-Rspamd-Server: rspam07 X-Stat-Signature: fhkat8gymodwoq16p94fowitujfmdzcq X-HE-Tag: 1735284524-478883 X-HE-Meta: U2FsdGVkX18L5EbdOHbsXLgr2A0iYpEMrNI0ibdVvhxUZwXKNVdQHsi6h7vg5EN5rS2tmxiQCp2AhEibwp+LVGzUghzzMtz6NTxW9/cfQT4b4QAWzA/0B7cu11WXWZFK2yYM/vD7zTzfEYJ4mqPVaRfL6HuANqz6i0vUrZ/9IytJPVOlJiRxTEB7EgWy12QThA75GDQ8bLOdOM+PoHXfTf3RanU311PzCzj34iOSq5JNlcwPfeO8tb+9/iCasN83HknrJbw+HBsTzckOxxfftsVcuXbfXP2gZszLG6VfTPG7d2qgpjDYoXgprZl7redIAR9/MwKa+1Vd89h/FMG34jXSt0+Hmmtq9i3rhq+2/BpAMITEkiqWKAUPwGSa09xaGKSeEOJThSBii5ZwmUzMJ/67IxdZ4osmEFaDZ2aKJrZVTEq0lYZGQAxf2HaWCd914S3wrWqTLrMzEHmkp+Y+5BnJjZETTb+GX08kYbhvBFMKrZnZZ63ZxCjOpD8tdup+Ehcz0bVeVwdl6GZkd3RsZhJBc6wFOiHBXcH7GT7Yf9WJ9XEsg5yBSwKXhrO2sKUYbFSRhD9b2QbqzUaTsiiWNFzIPMaKU0mW4kP2PpRVQN4C1ReS5XFUKj4zIDoFwAK33RK+Jcm3wO5D+WnYcllmt5DLGN/PT0hbmXLX1JVAGHpRr2sUfZD04pmNK20jw7eEo4Uq0mEihsxOzcUusPttPRrwcDxMu0wosSQsYaGWyHOpXY1hk3f37O21iVB6Wx5wU1BRPdo1v0hChX4KvvssBPyg8uWBiTI6bhks2Bdg6pZeBVTPIMcXVVKvQsMiuB57eg+tr703J6JNRexBoKPa9eIr7JrWdckFgdyjxcbL0+56ybcphacVUPGTDHcZHEt+RRVwbupvvaoJ1QjmiWhFFtw1IXj1KtPcKJmt6sb1nO9Y0izhgRlN3un/cB4biABMIw4kC+idItOt23DwqJV PrEm/pwN 1SDBBw8e84GszHEJd/ZK+rFkCiNT8Jnr5yWhX41ctvC7g81x7fb55i/6/KCymGlWn6B12NMSPjZ9ChKjdw/XEoLUgrbcDGBsnY3xZ9KpA3Bfms793FYkzkNEEMDOdj22TfgSCTxD58l4IQbl4VYWdIe+KNB4DYyn/bjw/7e6IqT8cR6CLXYpX/gPlCYZpWB+JP3mpzOJ9douZMyu9NqWyVGQ0zsC4WFmWWpr0sFBd1fD8QJM= X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: From: "Mike Rapoport (Microsoft)" Using a writable copy for ROX memory is cumbersome and error prone. Add API that allow temporarily remapping of ranges in the ROX cache as writable and then restoring their read-only-execute permissions. This API will be later used in modules code and will allow removing nasty games with writable copy in alternatives patching on x86. The restoring of the ROX permissions relies on the ability of architecture to reconstruct large pages in its set_memory_rox() method. Signed-off-by: Mike Rapoport (Microsoft) --- include/linux/execmem.h | 31 +++++++++++ mm/execmem.c | 118 +++++++++++++++++++++++++++++++++------- 2 files changed, 130 insertions(+), 19 deletions(-) diff --git a/include/linux/execmem.h b/include/linux/execmem.h index 64130ae19690..65655a5d1be2 100644 --- a/include/linux/execmem.h +++ b/include/linux/execmem.h @@ -65,6 +65,37 @@ enum execmem_range_flags { * Architectures that use EXECMEM_ROX_CACHE must implement this. */ void execmem_fill_trapping_insns(void *ptr, size_t size, bool writable); + +/** + * execmem_make_temp_rw - temporarily remap region with read-write + * permissions + * @ptr: address of the region to remap + * @size: size of the region to remap + * + * Remaps a part of the cached large page in the ROX cache in the range + * [@ptr, @ptr + @size) as writable and not executable. The caller must + * have exclusive ownership of this range and ensure nothing will try to + * execute code in this range. + * + * Return: 0 on success or negative error code on failure. + */ +int execmem_make_temp_rw(void *ptr, size_t size); + +/** + * execmem_restore_rox - restore read-only-execute permissions + * @ptr: address of the region to remap + * @size: size of the region to remap + * + * Restores read-only-execute permissions on a range [@ptr, @ptr + @size) + * after it was temporarily remapped as writable. Relies on architecture + * implementation of set_memory_rox() to restore mapping using large pages. + * + * Return: 0 on success or negative error code on failure. + */ +int execmem_restore_rox(void *ptr, size_t size); +#else +static inline int execmem_make_temp_rw(void *ptr, size_t size) { return 0; } +static inline int execmem_restore_rox(void *ptr, size_t size) { return 0; } #endif /** diff --git a/mm/execmem.c b/mm/execmem.c index 317b6a8d35be..be6b234c032e 100644 --- a/mm/execmem.c +++ b/mm/execmem.c @@ -89,6 +89,12 @@ static void *execmem_vmalloc(struct execmem_range *range, size_t size, #endif /* CONFIG_MMU */ #ifdef CONFIG_ARCH_HAS_EXECMEM_ROX +struct execmem_area { + struct vm_struct *vm; + unsigned int rw_mappings; + size_t size; +}; + struct execmem_cache { struct mutex mutex; struct maple_tree busy_areas; @@ -135,7 +141,7 @@ static void execmem_cache_clean(struct work_struct *work) struct maple_tree *free_areas = &execmem_cache.free_areas; struct mutex *mutex = &execmem_cache.mutex; MA_STATE(mas, free_areas, 0, ULONG_MAX); - void *area; + struct execmem_area *area; mutex_lock(mutex); mas_for_each(&mas, area, ULONG_MAX) { @@ -143,11 +149,12 @@ static void execmem_cache_clean(struct work_struct *work) if (IS_ALIGNED(size, PMD_SIZE) && IS_ALIGNED(mas.index, PMD_SIZE)) { - struct vm_struct *vm = find_vm_area(area); + struct vm_struct *vm = area->vm; execmem_set_direct_map_valid(vm, true); mas_store_gfp(&mas, NULL, GFP_KERNEL); - vfree(area); + vfree(vm->addr); + kfree(area); } } mutex_unlock(mutex); @@ -155,30 +162,31 @@ static void execmem_cache_clean(struct work_struct *work) static DECLARE_WORK(execmem_cache_clean_work, execmem_cache_clean); -static int execmem_cache_add(void *ptr, size_t size) +static int execmem_cache_add(void *ptr, size_t size, struct execmem_area *area) { struct maple_tree *free_areas = &execmem_cache.free_areas; struct mutex *mutex = &execmem_cache.mutex; unsigned long addr = (unsigned long)ptr; MA_STATE(mas, free_areas, addr - 1, addr + 1); + struct execmem_area *lower_area = NULL; + struct execmem_area *upper_area = NULL; unsigned long lower, upper; - void *area = NULL; int err; lower = addr; upper = addr + size - 1; mutex_lock(mutex); - area = mas_walk(&mas); - if (area && mas.last == addr - 1) + lower_area = mas_walk(&mas); + if (lower_area && lower_area == area && mas.last == addr - 1) lower = mas.index; - area = mas_next(&mas, ULONG_MAX); - if (area && mas.index == addr + size) + upper_area = mas_next(&mas, ULONG_MAX); + if (upper_area && upper_area == area && mas.index == addr + size) upper = mas.last; mas_set_range(&mas, lower, upper); - err = mas_store_gfp(&mas, (void *)lower, GFP_KERNEL); + err = mas_store_gfp(&mas, area, GFP_KERNEL); mutex_unlock(mutex); if (err) return err; @@ -209,7 +217,8 @@ static void *__execmem_cache_alloc(struct execmem_range *range, size_t size) MA_STATE(mas_busy, busy_areas, 0, ULONG_MAX); struct mutex *mutex = &execmem_cache.mutex; unsigned long addr, last, area_size = 0; - void *area, *ptr = NULL; + struct execmem_area *area; + void *ptr = NULL; int err; mutex_lock(mutex); @@ -228,20 +237,18 @@ static void *__execmem_cache_alloc(struct execmem_range *range, size_t size) /* insert allocated size to busy_areas at range [addr, addr + size) */ mas_set_range(&mas_busy, addr, addr + size - 1); - err = mas_store_gfp(&mas_busy, (void *)addr, GFP_KERNEL); + err = mas_store_gfp(&mas_busy, area, GFP_KERNEL); if (err) goto out_unlock; mas_store_gfp(&mas_free, NULL, GFP_KERNEL); if (area_size > size) { - void *ptr = (void *)(addr + size); - /* * re-insert remaining free size to free_areas at range * [addr + size, last] */ mas_set_range(&mas_free, addr + size, last); - err = mas_store_gfp(&mas_free, ptr, GFP_KERNEL); + err = mas_store_gfp(&mas_free, area, GFP_KERNEL); if (err) { mas_store_gfp(&mas_busy, NULL, GFP_KERNEL); goto out_unlock; @@ -257,16 +264,21 @@ static void *__execmem_cache_alloc(struct execmem_range *range, size_t size) static int execmem_cache_populate(struct execmem_range *range, size_t size) { unsigned long vm_flags = VM_ALLOW_HUGE_VMAP; + struct execmem_area *area; unsigned long start, end; struct vm_struct *vm; size_t alloc_size; int err = -ENOMEM; void *p; + area = kzalloc(sizeof(*area), GFP_KERNEL); + if (!area) + return err; + alloc_size = round_up(size, PMD_SIZE); p = execmem_vmalloc(range, alloc_size, PAGE_KERNEL, vm_flags); if (!p) - return err; + goto err_free_area; vm = find_vm_area(p); if (!vm) @@ -289,7 +301,9 @@ static int execmem_cache_populate(struct execmem_range *range, size_t size) if (err) goto err_free_mem; - err = execmem_cache_add(p, alloc_size); + area->size = alloc_size; + area->vm = vm; + err = execmem_cache_add(p, alloc_size, area); if (err) goto err_free_mem; @@ -297,6 +311,8 @@ static int execmem_cache_populate(struct execmem_range *range, size_t size) err_free_mem: vfree(p); +err_free_area: + kfree(area); return err; } @@ -305,6 +321,9 @@ static void *execmem_cache_alloc(struct execmem_range *range, size_t size) void *p; int err; + /* make sure everything in the cache is page aligned */ + size = PAGE_ALIGN(size); + p = __execmem_cache_alloc(range, size); if (p) return p; @@ -322,8 +341,8 @@ static bool execmem_cache_free(void *ptr) struct mutex *mutex = &execmem_cache.mutex; unsigned long addr = (unsigned long)ptr; MA_STATE(mas, busy_areas, addr, addr); + struct execmem_area *area; size_t size; - void *area; mutex_lock(mutex); area = mas_walk(&mas); @@ -338,12 +357,73 @@ static bool execmem_cache_free(void *ptr) execmem_fill_trapping_insns(ptr, size, /* writable = */ false); - execmem_cache_add(ptr, size); + execmem_cache_add(ptr, size, area); schedule_work(&execmem_cache_clean_work); return true; } + +int execmem_make_temp_rw(void *ptr, size_t size) +{ + struct maple_tree *busy_areas = &execmem_cache.busy_areas; + unsigned int nr = PAGE_ALIGN(size) >> PAGE_SHIFT; + struct mutex *mutex = &execmem_cache.mutex; + unsigned long addr = (unsigned long)ptr; + MA_STATE(mas, busy_areas, addr, addr); + struct execmem_area *area; + int ret = -ENOMEM; + + mutex_lock(mutex); + area = mas_walk(&mas); + if (!area) + goto out; + + ret = set_memory_nx(addr, nr); + if (ret) + goto out; + + /* + * If a split of large page was required, it already happened when + * we marked the pages NX which guarantees that this call won't + * fail + */ + set_memory_rw(addr, nr); + area->rw_mappings++; + +out: + mutex_unlock(mutex); + return ret; +} + +int execmem_restore_rox(void *ptr, size_t size) +{ + struct maple_tree *busy_areas = &execmem_cache.busy_areas; + struct mutex *mutex = &execmem_cache.mutex; + unsigned long addr = (unsigned long)ptr; + MA_STATE(mas, busy_areas, addr, addr); + struct execmem_area *area; + int err = 0; + + size = PAGE_ALIGN(size); + + mutex_lock(mutex); + mas_for_each(&mas, area, addr + size - 1) { + area->rw_mappings--; + if (!area->rw_mappings) { + unsigned int nr = area->size >> PAGE_SHIFT; + + addr = (unsigned long)area->vm->addr; + err = set_memory_rox(addr, nr); + if (err) + break; + } + } + mutex_unlock(mutex); + + return err; +} + #else /* CONFIG_ARCH_HAS_EXECMEM_ROX */ static void *execmem_cache_alloc(struct execmem_range *range, size_t size) { From patchwork Fri Dec 27 07:28:22 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mike Rapoport X-Patchwork-Id: 13921587 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 kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 74553E77191 for ; Fri, 27 Dec 2024 07:29:40 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 085DD6B0096; Fri, 27 Dec 2024 02:29:40 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 0355C6B0098; Fri, 27 Dec 2024 02:29:39 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id DF17A6B0099; Fri, 27 Dec 2024 02:29:39 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id C04396B0096 for ; Fri, 27 Dec 2024 02:29:39 -0500 (EST) Received: from smtpin29.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id 48C461201BA for ; Fri, 27 Dec 2024 07:29:39 +0000 (UTC) X-FDA: 82939913658.29.3F574CF Received: from nyc.source.kernel.org (nyc.source.kernel.org [147.75.193.91]) by imf04.hostedemail.com (Postfix) with ESMTP id 66CBF4000A for ; Fri, 27 Dec 2024 07:28:53 +0000 (UTC) Authentication-Results: imf04.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=ScuKV2kD; dmarc=pass (policy=quarantine) header.from=kernel.org; spf=pass (imf04.hostedemail.com: domain of rppt@kernel.org designates 147.75.193.91 as permitted sender) smtp.mailfrom=rppt@kernel.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1735284558; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=bhAs81WsdmaB1BC5mVN5DKU+/2vSD1EVP4twaFT9Vd4=; b=ktjVVEnqI9Yrz9g0PeYiz0DVBr5n8ARegR0aWQYSPUljSxZZLjj4zzMMVFtyRthFEU0mXn FH/a2t/PSE/V1DIFcFtlAAP1sUBnFm3hODz4oUzJYameREaPkKPmHn2spQUQ8zsSce6MH3 ozfEkCWYo87f5l52XVU3yLMkvd0F75Y= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1735284558; a=rsa-sha256; cv=none; b=waLzjuZSjRltKDek3oOLWyzNPqY2N+3U2dogiHNUj7QsnF5X7n3lmb2d7SCNNqd7rHHj+T I6HZG9Uxwf1cnjsn6sGuVz1MJLpLhonaZ3Nixwg6Xr/hG8R5FEIwFOMZ5Kj3ILLcyVX3Ca DfQO+TwgQuO6TqjeOxQEurMbhtfstw0= ARC-Authentication-Results: i=1; imf04.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=ScuKV2kD; dmarc=pass (policy=quarantine) header.from=kernel.org; spf=pass (imf04.hostedemail.com: domain of rppt@kernel.org designates 147.75.193.91 as permitted sender) smtp.mailfrom=rppt@kernel.org Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by nyc.source.kernel.org (Postfix) with ESMTP id 40680A411C6; Fri, 27 Dec 2024 07:27:47 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id ED23AC4CEDE; Fri, 27 Dec 2024 07:29:26 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1735284576; bh=FmTrET8nL2UmyRc6aQAH2J1SgS8cwR3X1MoKmnTcl78=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=ScuKV2kD6qnanPStFqpYQm0FgQDVh0DLb3NJ4mtpO81hTHINYHF0xekTtwDQfutNa 5HKFQzwtfQTvNvihXCd5kB4OjdDHB0EgoIFx02GxxN3l+EmLdLZCev+8FpmEu9CC4k eMT9y14Y+PeB1o2Tfc/girl/nvsZSOezNPfNaMWFzyJZUyEKRLnNC7r43j4jRHie3x /okfVqQiHOWC+vCecEU5ddItPuJAZnoPoFpdYKjuRTORzV98ljXX49DwlOe+fDP1f0 KYHcHr1ZIK6uxPwndLMeq8O6yIz3Hp7YubMI/Xsj65vbvOwrCzxasJFI+iE6G6/Qwt XiyJ1o1+qm82Q== From: Mike Rapoport To: Andrew Morton Cc: Andy Lutomirski , Anton Ivanov , Borislav Petkov , Brendan Higgins , Daniel Gomez , Daniel Thompson , Dave Hansen , David Gow , Douglas Anderson , Ingo Molnar , Jason Wessel , Jiri Kosina , Joe Lawrence , Johannes Berg , Josh Poimboeuf , "Kirill A. Shutemov" , Luis Chamberlain , Mark Rutland , Masami Hiramatsu , Mike Rapoport , Miroslav Benes , "H. Peter Anvin" , Peter Zijlstra , Petr Mladek , Petr Pavlu , Rae Moar , Richard Weinberger , Sami Tolvanen , Shuah Khan , Song Liu , Steven Rostedt , Thomas Gleixner , kgdb-bugreport@lists.sourceforge.net, kunit-dev@googlegroups.com, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, linux-mm@kvack.org, linux-modules@vger.kernel.org, linux-trace-kernel@vger.kernel.org, linux-um@lists.infradead.org, live-patching@vger.kernel.org, x86@kernel.org Subject: [PATCH 5/8] module: introduce MODULE_STATE_GONE Date: Fri, 27 Dec 2024 09:28:22 +0200 Message-ID: <20241227072825.1288491-6-rppt@kernel.org> X-Mailer: git-send-email 2.45.2 In-Reply-To: <20241227072825.1288491-1-rppt@kernel.org> References: <20241227072825.1288491-1-rppt@kernel.org> MIME-Version: 1.0 X-Stat-Signature: naubf4tofqjgqaszr3nceaa9pgqbmo77 X-Rspamd-Queue-Id: 66CBF4000A X-Rspam-User: X-Rspamd-Server: rspam01 X-HE-Tag: 1735284533-26155 X-HE-Meta: U2FsdGVkX1+8dZfkswvUNvXlFz770WgaN4d6GKttXMzZrPFA1ooqZLrQvu6Qai422NR3mDagLgpz3ciS9NWwl+EAuwjOXEbFKIsikxyCFA6TBxYkzzLuGiR3n7ztpp6yDMnEm6RnOtejYrwa7Ckf1InMi9nlP39+NS4Lmyvk+U4RIzpsaVOwJ8UlWoWkmLF4edxKDUx7phkxo/cegSFHnb7tzr9iMyPYFWWvpmLcBFgPBFFJMd1km+wWcbR7ur7f9jeXX+Lv43CpzwBdI42a3/ZnsUMy8sRChvrth35SvmmBsozFkwUoeE8unLujkSivB9CR/lJD/bWE4EIpLtKt/jaPpLkewVJEah8wvBEw5d54b4og0mZsk14F0ooc1WgP8WkQbqEl7gxDiIZnn9rjcfitiaPwRGLv8eKamcOjqazGNHim1pGIMnanXR+Ou1ByAob4KaouYeneAi69xS3rgJrrfBsT0jAUQG9Bhlmdy2h3xbA4Oct8AHaOlhgDCmRsEE+mOz4qBRJ+Lg+e013S391y/wKWcTCqwQ8vUSjAFdTL3ORRecuYuhHlvPtl39Peaq4Knnk/ItSirNw+RRGwnH4QZsm7EwKor4Iyil1BveNGLFMIeU/xJi8u0+L11Ymx4etZ7ur1T55ckQV4BV6KR6hEw1KJJt7yifP+/LpLKlMK36j7k4qaR2lpr8H6xahYXsKDs18jv2BJZHCu/X5BBQoZuVDzX1sXBdJX+BUzGuDX5LBZcY5NJmU0o3ndTMIh8SAuEtku6wrok0QxE77JR+OHnUEgadpL50nXp4i+WaMNWsYEHZDG80snUWya+Synm8cuxAuoKt0+NGMhSxY770FRxdO00fT2GvpLb+I913y1DOuCbnyR1p23eJjVJvekvkLn37w+XYtFPe0cdkU7pvXgsJhMLqfj4zPlFR8U8LeEz+0pFEorM/TkITpVSevABzyIvr0+fVqueYHwNUa bq4ERSSO paOgZ0gCZcvcrfNaLF1mROnHWQIz4i+xiyWO8FBLdojqxWxPe0Lcl84r0qnjRZSzA7v67Hz3pm3pq1TMOEE03eaecPsHs5jDDaTOgEoE8SOQtVSqtN8hsKsRNNC5e2S8kDRcGCnX/0POoyTarA7Qmu7Erg+LzYdf2kMyL8H1SCiKj7EJ5NteB7k6xIcH+vt+BxmMI8eMR3x9G13qHUd2zngERAJAkDAJklZ5909/6q59JQrYZfTyotHKBEB5eJsCVwQFI0a82z6nfbEkPo9tc/kqZV5mTAxG3oJhf X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: From: "Mike Rapoport (Microsoft)" In order to use execmem's API for temporal remapping of the memory allocated from ROX cache as writable, there is a need to distinguish between the state when the module is being formed and the state when it is deconstructed and freed so that when module_memory_free() is called from error paths during module loading it could restore ROX mappings. Replace open coded checks for MODULE_STATE_UNFORMED with a helper function module_is_formed() and add a new MODULE_STATE_GONE that will be set when the module is deconstructed and freed. Signed-off-by: Mike Rapoport (Microsoft) --- include/linux/module.h | 6 ++++++ kernel/module/kallsyms.c | 8 ++++---- kernel/module/kdb.c | 2 +- kernel/module/main.c | 19 +++++++++---------- kernel/module/procfs.c | 2 +- kernel/tracepoint.c | 2 ++ lib/kunit/test.c | 2 ++ samples/livepatch/livepatch-callbacks-demo.c | 1 + .../test_modules/test_klp_callbacks_demo.c | 1 + .../test_modules/test_klp_callbacks_demo2.c | 1 + .../livepatch/test_modules/test_klp_state.c | 1 + .../livepatch/test_modules/test_klp_state2.c | 1 + 12 files changed, 30 insertions(+), 16 deletions(-) diff --git a/include/linux/module.h b/include/linux/module.h index 94acbacdcdf1..bd8cf93d32c8 100644 --- a/include/linux/module.h +++ b/include/linux/module.h @@ -320,6 +320,7 @@ enum module_state { MODULE_STATE_COMING, /* Full formed, running module_init. */ MODULE_STATE_GOING, /* Going away. */ MODULE_STATE_UNFORMED, /* Still setting it up. */ + MODULE_STATE_GONE, /* Deconstructing and freeing. */ }; struct mod_tree_node { @@ -620,6 +621,11 @@ static inline bool module_is_coming(struct module *mod) return mod->state == MODULE_STATE_COMING; } +static inline bool module_is_formed(struct module *mod) +{ + return mod->state < MODULE_STATE_UNFORMED; +} + struct module *__module_text_address(unsigned long addr); struct module *__module_address(unsigned long addr); bool is_module_address(unsigned long addr); diff --git a/kernel/module/kallsyms.c b/kernel/module/kallsyms.c index bf65e0c3c86f..daf9a9b3740f 100644 --- a/kernel/module/kallsyms.c +++ b/kernel/module/kallsyms.c @@ -361,7 +361,7 @@ int lookup_module_symbol_name(unsigned long addr, char *symname) preempt_disable(); list_for_each_entry_rcu(mod, &modules, list) { - if (mod->state == MODULE_STATE_UNFORMED) + if (!module_is_formed(mod)) continue; if (within_module(addr, mod)) { const char *sym; @@ -389,7 +389,7 @@ int module_get_kallsym(unsigned int symnum, unsigned long *value, char *type, list_for_each_entry_rcu(mod, &modules, list) { struct mod_kallsyms *kallsyms; - if (mod->state == MODULE_STATE_UNFORMED) + if (!module_is_formed(mod)) continue; kallsyms = rcu_dereference_sched(mod->kallsyms); if (symnum < kallsyms->num_symtab) { @@ -441,7 +441,7 @@ static unsigned long __module_kallsyms_lookup_name(const char *name) list_for_each_entry_rcu(mod, &modules, list) { unsigned long ret; - if (mod->state == MODULE_STATE_UNFORMED) + if (!module_is_formed(mod)) continue; ret = __find_kallsyms_symbol_value(mod, name); if (ret) @@ -484,7 +484,7 @@ int module_kallsyms_on_each_symbol(const char *modname, list_for_each_entry(mod, &modules, list) { struct mod_kallsyms *kallsyms; - if (mod->state == MODULE_STATE_UNFORMED) + if (!module_is_formed(mod)) continue; if (modname && strcmp(modname, mod->name)) diff --git a/kernel/module/kdb.c b/kernel/module/kdb.c index 995c32d3698f..14f14700ffc2 100644 --- a/kernel/module/kdb.c +++ b/kernel/module/kdb.c @@ -23,7 +23,7 @@ int kdb_lsmod(int argc, const char **argv) kdb_printf("Module Size modstruct Used by\n"); list_for_each_entry(mod, &modules, list) { - if (mod->state == MODULE_STATE_UNFORMED) + if (!module_is_formed(mod)) continue; kdb_printf("%-20s%8u", mod->name, mod->mem[MOD_TEXT].size); diff --git a/kernel/module/main.c b/kernel/module/main.c index 5399c182b3cb..ad8ef20c120f 100644 --- a/kernel/module/main.c +++ b/kernel/module/main.c @@ -153,7 +153,7 @@ EXPORT_SYMBOL(unregister_module_notifier); */ static inline int strong_try_module_get(struct module *mod) { - BUG_ON(mod && mod->state == MODULE_STATE_UNFORMED); + BUG_ON(mod && !module_is_formed(mod)); if (mod && mod->state == MODULE_STATE_COMING) return -EBUSY; if (try_module_get(mod)) @@ -361,7 +361,7 @@ bool find_symbol(struct find_symbol_arg *fsa) GPL_ONLY }, }; - if (mod->state == MODULE_STATE_UNFORMED) + if (!module_is_formed(mod)) continue; for (i = 0; i < ARRAY_SIZE(arr); i++) @@ -386,7 +386,7 @@ struct module *find_module_all(const char *name, size_t len, list_for_each_entry_rcu(mod, &modules, list, lockdep_is_held(&module_mutex)) { - if (!even_unformed && mod->state == MODULE_STATE_UNFORMED) + if (!even_unformed && !module_is_formed(mod)) continue; if (strlen(mod->name) == len && !memcmp(mod->name, name, len)) return mod; @@ -457,7 +457,7 @@ bool __is_module_percpu_address(unsigned long addr, unsigned long *can_addr) preempt_disable(); list_for_each_entry_rcu(mod, &modules, list) { - if (mod->state == MODULE_STATE_UNFORMED) + if (!module_is_formed(mod)) continue; if (!mod->percpu_size) continue; @@ -1326,7 +1326,7 @@ static void free_module(struct module *mod) * that noone uses it while it's being deconstructed. */ mutex_lock(&module_mutex); - mod->state = MODULE_STATE_UNFORMED; + mod->state = MODULE_STATE_GONE; mutex_unlock(&module_mutex); /* Arch-specific cleanup. */ @@ -3048,8 +3048,7 @@ static int module_patient_check_exists(const char *name, if (old == NULL) return 0; - if (old->state == MODULE_STATE_COMING || - old->state == MODULE_STATE_UNFORMED) { + if (old->state == MODULE_STATE_COMING || !module_is_formed(old)) { /* Wait in case it fails to load. */ mutex_unlock(&module_mutex); err = wait_event_interruptible(module_wq, @@ -3608,7 +3607,7 @@ char *module_flags(struct module *mod, char *buf, bool show_state) { int bx = 0; - BUG_ON(mod->state == MODULE_STATE_UNFORMED); + BUG_ON(!module_is_formed(mod)); if (!mod->taints && !show_state) goto out; if (mod->taints || @@ -3702,7 +3701,7 @@ struct module *__module_address(unsigned long addr) mod = mod_find(addr, &mod_tree); if (mod) { BUG_ON(!within_module(addr, mod)); - if (mod->state == MODULE_STATE_UNFORMED) + if (!module_is_formed(mod)) mod = NULL; } return mod; @@ -3756,7 +3755,7 @@ void print_modules(void) /* Most callers should already have preempt disabled, but make sure */ preempt_disable(); list_for_each_entry_rcu(mod, &modules, list) { - if (mod->state == MODULE_STATE_UNFORMED) + if (!module_is_formed(mod)) continue; pr_cont(" %s%s", mod->name, module_flags(mod, buf, true)); } diff --git a/kernel/module/procfs.c b/kernel/module/procfs.c index 0a4841e88adb..2c617e6f8bc0 100644 --- a/kernel/module/procfs.c +++ b/kernel/module/procfs.c @@ -79,7 +79,7 @@ static int m_show(struct seq_file *m, void *p) unsigned int size; /* We always ignore unformed modules. */ - if (mod->state == MODULE_STATE_UNFORMED) + if (!module_is_formed(mod)) return 0; size = module_total_size(mod); diff --git a/kernel/tracepoint.c b/kernel/tracepoint.c index 1848ce7e2976..e94247afb2c6 100644 --- a/kernel/tracepoint.c +++ b/kernel/tracepoint.c @@ -668,6 +668,8 @@ static int tracepoint_module_notify(struct notifier_block *self, break; case MODULE_STATE_UNFORMED: break; + case MODULE_STATE_GONE: + break; } return notifier_from_errno(ret); } diff --git a/lib/kunit/test.c b/lib/kunit/test.c index 089c832e3cdb..54eaed92a2d3 100644 --- a/lib/kunit/test.c +++ b/lib/kunit/test.c @@ -836,6 +836,8 @@ static int kunit_module_notify(struct notifier_block *nb, unsigned long val, break; case MODULE_STATE_UNFORMED: break; + case MODULE_STATE_GONE: + break; } return 0; diff --git a/samples/livepatch/livepatch-callbacks-demo.c b/samples/livepatch/livepatch-callbacks-demo.c index 11c3f4357812..324bddaef9a6 100644 --- a/samples/livepatch/livepatch-callbacks-demo.c +++ b/samples/livepatch/livepatch-callbacks-demo.c @@ -93,6 +93,7 @@ static const char *const module_state[] = { [MODULE_STATE_COMING] = "[MODULE_STATE_COMING] Full formed, running module_init", [MODULE_STATE_GOING] = "[MODULE_STATE_GOING] Going away", [MODULE_STATE_UNFORMED] = "[MODULE_STATE_UNFORMED] Still setting it up", + [MODULE_STATE_GONE] = "[MODULE_STATE_GONE] Deconstructing and freeing", }; static void callback_info(const char *callback, struct klp_object *obj) diff --git a/tools/testing/selftests/livepatch/test_modules/test_klp_callbacks_demo.c b/tools/testing/selftests/livepatch/test_modules/test_klp_callbacks_demo.c index 3fd8fe1cd1cc..8435e3254f85 100644 --- a/tools/testing/selftests/livepatch/test_modules/test_klp_callbacks_demo.c +++ b/tools/testing/selftests/livepatch/test_modules/test_klp_callbacks_demo.c @@ -16,6 +16,7 @@ static const char *const module_state[] = { [MODULE_STATE_COMING] = "[MODULE_STATE_COMING] Full formed, running module_init", [MODULE_STATE_GOING] = "[MODULE_STATE_GOING] Going away", [MODULE_STATE_UNFORMED] = "[MODULE_STATE_UNFORMED] Still setting it up", + [MODULE_STATE_GONE] = "[MODULE_STATE_GONE] Deconstructing and freeing", }; static void callback_info(const char *callback, struct klp_object *obj) diff --git a/tools/testing/selftests/livepatch/test_modules/test_klp_callbacks_demo2.c b/tools/testing/selftests/livepatch/test_modules/test_klp_callbacks_demo2.c index 5417573e80af..78c1fff5d977 100644 --- a/tools/testing/selftests/livepatch/test_modules/test_klp_callbacks_demo2.c +++ b/tools/testing/selftests/livepatch/test_modules/test_klp_callbacks_demo2.c @@ -16,6 +16,7 @@ static const char *const module_state[] = { [MODULE_STATE_COMING] = "[MODULE_STATE_COMING] Full formed, running module_init", [MODULE_STATE_GOING] = "[MODULE_STATE_GOING] Going away", [MODULE_STATE_UNFORMED] = "[MODULE_STATE_UNFORMED] Still setting it up", + [MODULE_STATE_GONE] = "[MODULE_STATE_GONE] Deconstructing and freeing", }; static void callback_info(const char *callback, struct klp_object *obj) diff --git a/tools/testing/selftests/livepatch/test_modules/test_klp_state.c b/tools/testing/selftests/livepatch/test_modules/test_klp_state.c index 57a4253acb01..bdebf1d24c98 100644 --- a/tools/testing/selftests/livepatch/test_modules/test_klp_state.c +++ b/tools/testing/selftests/livepatch/test_modules/test_klp_state.c @@ -18,6 +18,7 @@ static const char *const module_state[] = { [MODULE_STATE_COMING] = "[MODULE_STATE_COMING] Full formed, running module_init", [MODULE_STATE_GOING] = "[MODULE_STATE_GOING] Going away", [MODULE_STATE_UNFORMED] = "[MODULE_STATE_UNFORMED] Still setting it up", + [MODULE_STATE_GONE] = "[MODULE_STATE_GONE] Deconstructing and freeing", }; static void callback_info(const char *callback, struct klp_object *obj) diff --git a/tools/testing/selftests/livepatch/test_modules/test_klp_state2.c b/tools/testing/selftests/livepatch/test_modules/test_klp_state2.c index c978ea4d5e67..1a55f84a8eb3 100644 --- a/tools/testing/selftests/livepatch/test_modules/test_klp_state2.c +++ b/tools/testing/selftests/livepatch/test_modules/test_klp_state2.c @@ -18,6 +18,7 @@ static const char *const module_state[] = { [MODULE_STATE_COMING] = "[MODULE_STATE_COMING] Full formed, running module_init", [MODULE_STATE_GOING] = "[MODULE_STATE_GOING] Going away", [MODULE_STATE_UNFORMED] = "[MODULE_STATE_UNFORMED] Still setting it up", + [MODULE_STATE_GONE] = "[MODULE_STATE_GONE] Deconstructing and freeing", }; static void callback_info(const char *callback, struct klp_object *obj) From patchwork Fri Dec 27 07:28:23 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mike Rapoport X-Patchwork-Id: 13921588 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 kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 5EE24E7718B for ; Fri, 27 Dec 2024 07:29:51 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id EB69B6B0099; Fri, 27 Dec 2024 02:29:50 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id E66BF6B009A; Fri, 27 Dec 2024 02:29:50 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id D07DC6B009B; Fri, 27 Dec 2024 02:29:50 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id B316E6B0099 for ; Fri, 27 Dec 2024 02:29:50 -0500 (EST) Received: from smtpin16.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id 6D7AB161A73 for ; Fri, 27 Dec 2024 07:29:50 +0000 (UTC) X-FDA: 82939913700.16.A9B9896 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by imf17.hostedemail.com (Postfix) with ESMTP id 4430340015 for ; Fri, 27 Dec 2024 07:29:16 +0000 (UTC) Authentication-Results: imf17.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=ityZElnH; dmarc=pass (policy=quarantine) header.from=kernel.org; spf=pass (imf17.hostedemail.com: domain of rppt@kernel.org designates 139.178.84.217 as permitted sender) smtp.mailfrom=rppt@kernel.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1735284568; a=rsa-sha256; cv=none; b=zoRPwpZK1wKD96uwcLINDmqzTX7+gNgFLH0HwUWkClQXbekNftPLqyr+CH2ilBxJCqzr02 AFfYx8h5ia8qBrefyygUMi1jJj9A2gXWrycBN9mEd1N36p499S0Vlj9qVwD6vs8Xu3FfFO xYRWgyQuPKo7PRPj3jnSI/G6mUK/bcg= ARC-Authentication-Results: i=1; imf17.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=ityZElnH; dmarc=pass (policy=quarantine) header.from=kernel.org; spf=pass (imf17.hostedemail.com: domain of rppt@kernel.org designates 139.178.84.217 as permitted sender) smtp.mailfrom=rppt@kernel.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1735284568; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=c9js65l60Su315uXAlOqXw9A8MNZmLUOrzYiZK6HhTI=; b=wyb1MsAcrOcvE87qdCtVX0BSXMaLlFbfwzJTJ7ttI2kIBWzitBEFUCjDex/nxtZwDGJ3LE eDt6HMPFQvo2GFodntrxSyEX9lRU0lBWeryWSSHxcwJvVyCIIJuecvk3LJwF8GR6l61kxE 116m18MWL2WXG/lQkQXXLuAVZjttu4Y= Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by dfw.source.kernel.org (Postfix) with ESMTP id ADE7C5C5F22; Fri, 27 Dec 2024 07:29:05 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 425F4C4CEE4; Fri, 27 Dec 2024 07:29:37 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1735284587; bh=0UxQHTn4TxkT8qmjfGtdSjB+pNz0UnZkmPm8yiWN9mY=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=ityZElnHqm6bnzepLB2TjYY5Xw64scud99fphM4K1mtbmfOR9sgV+ui9VzMP5Bbrd WAsiuLhOTqNxEV4obo+fmG4IgxNkKoWkqPjI3gSKxnUHKdT5J9R58acZhaIp7+Zoa+ I3hnWpS9JE8LSapQsx5idWBVzDKgY0bKdy/omyG9hpXcSvQanUQqiU9X63K9Bu8r/A TMhqPCdTXSdJrA99yKZehHNw3bS5RBVbLpEZXDIK1C0BaXf+rf5+PHWbcjQ8HNcSHm ojXoSIADD6lPcFlN/NUt/6bZKQvoa/h7bQh85wHb568JsA8kAl0Az4krpLGHtHwzGP QoEU6y2Lsn3CQ== From: Mike Rapoport To: Andrew Morton Cc: Andy Lutomirski , Anton Ivanov , Borislav Petkov , Brendan Higgins , Daniel Gomez , Daniel Thompson , Dave Hansen , David Gow , Douglas Anderson , Ingo Molnar , Jason Wessel , Jiri Kosina , Joe Lawrence , Johannes Berg , Josh Poimboeuf , "Kirill A. Shutemov" , Luis Chamberlain , Mark Rutland , Masami Hiramatsu , Mike Rapoport , Miroslav Benes , "H. Peter Anvin" , Peter Zijlstra , Petr Mladek , Petr Pavlu , Rae Moar , Richard Weinberger , Sami Tolvanen , Shuah Khan , Song Liu , Steven Rostedt , Thomas Gleixner , kgdb-bugreport@lists.sourceforge.net, kunit-dev@googlegroups.com, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, linux-mm@kvack.org, linux-modules@vger.kernel.org, linux-trace-kernel@vger.kernel.org, linux-um@lists.infradead.org, live-patching@vger.kernel.org, x86@kernel.org Subject: [PATCH 6/8] modules: switch to execmem API for remapping as RW and restoring ROX Date: Fri, 27 Dec 2024 09:28:23 +0200 Message-ID: <20241227072825.1288491-7-rppt@kernel.org> X-Mailer: git-send-email 2.45.2 In-Reply-To: <20241227072825.1288491-1-rppt@kernel.org> References: <20241227072825.1288491-1-rppt@kernel.org> MIME-Version: 1.0 X-Rspam-User: X-Rspamd-Server: rspam03 X-Rspamd-Queue-Id: 4430340015 X-Stat-Signature: u8niz4rr65jxcby7he4c7dznhbbrpuce X-HE-Tag: 1735284556-444050 X-HE-Meta: U2FsdGVkX181Lp5Ob/501Hf52irJMnkWeY1vn3t0QEtY5DxSb18u1iJxUqKTu3YR7nXuMf1x4y/J5Ve6THvQvGMjz1+R2blh8yyy61pKzBHCJHnmsIYSBBJlpBwh2G7aaBuyNps0LW6lUbzemnacDoc7dG3qpMNgFRAUAkjMJ3dI9twXeuygJjuGoXe8+jBDsIOch95rmzQzetLSNv6EQp3T4RIRRucEeULcD+eWRNuXiQbvRPmhHiuiISgg/OnK25zFoJ0SoZgAniI95FkpN37QSDHuKAndaVkqezGwL4Iv+v+LU/Hxx3DtDxmdNr/QWdlzcX7AD+Y4HHadJToaSqP4/0J9OEHCugC/Rc6NnbLWq79h57lm782R4wRquqCgfNXQuP6HcU9NCOL3PeMALqIiFps3LhEAbzLzPQh76wJj61/n+hOt3zlh60Z0PpEtocair8yXHADoI/s6K9Btoj7aO/ULwF62q6FX/ohVMDfIYEUR1QZOyICZegPUD1DlNYOPTh1Q1b1RIUSn3UlHgzi/rT5SVSDuSzlPoyM5KGYTb3WobCusG4kzJkFdY3s+JcNXMY+lpp3bf9j9TTl1PxgzgtqlZUiPnSO2AZ0XF8Sy64eiE9sAXiCyQXGywBOlvm1w5lp2rLi6iYMUUaCqudczSDEVszP4MT0lq+UcKhRFD3VFV4TBIyLvkRBhGmSc8UB/W2xN6LDzVe+hcvagu5xeWTnpI39Kj8dVjgTUC07MWoqCat3OuPUSOfgEgA/dAZLNzVQtsxtWAdEd0GO4FP1XNWeAatvtSRup9RUdtCdVFuJM/OmArU3o4TslH54rCMj/CMZeDFT0O4D9XY1b/yL9NL/yUPmvLVbQ181kpKeJYIMXfn6tim1yagk3aO39+WDpA4AUETI9Ab4uNkLw9jDM2NoB4VcoDK3AuIl28cff3AnHTc/CnK9nf/HhBAuIq5PZbM6URLWOcSQzwII WzzOV5B0 ZnytxORWYIRT4T8U3uZZHPHezXpTo7fbrYasPDgU70qZnus849HZssnMUXop9fzHo0RB9H/yQAMuDsTCZr+bkgoWKBgjMoH6K5ee8WJLiaOSxPwgfVxiPsYskrQ4lRejaHSzzT78/59ra/7bwV/L3LO476VuNCKUCeEgtb/nO0L7LZRikeO1pEup3Cm/MWz7I+TwMNdHzqcbn0GjV3s/pRCb/k83eG+ZfzxLR1M8Q3fgz8/8= X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: From: "Mike Rapoport (Microsoft)" Instead of using writable copy for module text sections, temporarily remap the memory allocated from execmem's ROX cache as writable and restore its ROX permissions after the module is formed. This will allow removing nasty games with writable copy in alternatives patching on x86. Signed-off-by: Mike Rapoport (Microsoft) --- include/linux/module.h | 7 +--- include/linux/moduleloader.h | 4 --- kernel/module/main.c | 67 ++++++------------------------------ kernel/module/strict_rwx.c | 9 ++--- 4 files changed, 17 insertions(+), 70 deletions(-) diff --git a/include/linux/module.h b/include/linux/module.h index bd8cf93d32c8..e9fc9d1fa476 100644 --- a/include/linux/module.h +++ b/include/linux/module.h @@ -368,7 +368,6 @@ enum mod_mem_type { struct module_memory { void *base; - void *rw_copy; bool is_rox; unsigned int size; @@ -775,13 +774,9 @@ static inline bool is_livepatch_module(struct module *mod) void set_module_sig_enforced(void); -void *__module_writable_address(struct module *mod, void *loc); - static inline void *module_writable_address(struct module *mod, void *loc) { - if (!IS_ENABLED(CONFIG_ARCH_HAS_EXECMEM_ROX) || !mod) - return loc; - return __module_writable_address(mod, loc); + return loc; } #else /* !CONFIG_MODULES... */ diff --git a/include/linux/moduleloader.h b/include/linux/moduleloader.h index 1f5507ba5a12..e395461d59e5 100644 --- a/include/linux/moduleloader.h +++ b/include/linux/moduleloader.h @@ -108,10 +108,6 @@ int module_finalize(const Elf_Ehdr *hdr, const Elf_Shdr *sechdrs, struct module *mod); -int module_post_finalize(const Elf_Ehdr *hdr, - const Elf_Shdr *sechdrs, - struct module *mod); - #ifdef CONFIG_MODULES void flush_module_init_free_work(void); #else diff --git a/kernel/module/main.c b/kernel/module/main.c index ad8ef20c120f..ee6b46e753a0 100644 --- a/kernel/module/main.c +++ b/kernel/module/main.c @@ -1221,18 +1221,6 @@ void __weak module_arch_freeing_init(struct module *mod) { } -void *__module_writable_address(struct module *mod, void *loc) -{ - for_class_mod_mem_type(type, text) { - struct module_memory *mem = &mod->mem[type]; - - if (loc >= mem->base && loc < mem->base + mem->size) - return loc + (mem->rw_copy - mem->base); - } - - return loc; -} - static int module_memory_alloc(struct module *mod, enum mod_mem_type type) { unsigned int size = PAGE_ALIGN(mod->mem[type].size); @@ -1250,21 +1238,15 @@ static int module_memory_alloc(struct module *mod, enum mod_mem_type type) if (!ptr) return -ENOMEM; - mod->mem[type].base = ptr; - if (execmem_is_rox(execmem_type)) { - ptr = vzalloc(size); + int err = execmem_make_temp_rw(ptr, size); - if (!ptr) { - execmem_free(mod->mem[type].base); + if (err) { + execmem_free(ptr); return -ENOMEM; } - mod->mem[type].rw_copy = ptr; mod->mem[type].is_rox = true; - } else { - mod->mem[type].rw_copy = mod->mem[type].base; - memset(mod->mem[type].base, 0, size); } /* @@ -1280,6 +1262,9 @@ static int module_memory_alloc(struct module *mod, enum mod_mem_type type) */ kmemleak_not_leak(ptr); + memset(ptr, 0, size); + mod->mem[type].base = ptr; + return 0; } @@ -1287,8 +1272,8 @@ static void module_memory_free(struct module *mod, enum mod_mem_type type) { struct module_memory *mem = &mod->mem[type]; - if (mem->is_rox) - vfree(mem->rw_copy); + if (mod->state == MODULE_STATE_UNFORMED && mem->is_rox) + execmem_restore_rox(mem->base, mem->size); execmem_free(mem->base); } @@ -2561,7 +2546,6 @@ static int move_module(struct module *mod, struct load_info *info) for_each_mod_mem_type(type) { if (!mod->mem[type].size) { mod->mem[type].base = NULL; - mod->mem[type].rw_copy = NULL; continue; } @@ -2578,7 +2562,6 @@ static int move_module(struct module *mod, struct load_info *info) void *dest; Elf_Shdr *shdr = &info->sechdrs[i]; const char *sname; - unsigned long addr; if (!(shdr->sh_flags & SHF_ALLOC)) continue; @@ -2599,14 +2582,12 @@ static int move_module(struct module *mod, struct load_info *info) ret = PTR_ERR(dest); goto out_err; } - addr = (unsigned long)dest; codetag_section_found = true; } else { enum mod_mem_type type = shdr->sh_entsize >> SH_ENTSIZE_TYPE_SHIFT; unsigned long offset = shdr->sh_entsize & SH_ENTSIZE_OFFSET_MASK; - addr = (unsigned long)mod->mem[type].base + offset; - dest = mod->mem[type].rw_copy + offset; + dest = mod->mem[type].base + offset; } if (shdr->sh_type != SHT_NOBITS) { @@ -2629,7 +2610,7 @@ static int move_module(struct module *mod, struct load_info *info) * users of info can keep taking advantage and using the newly * minted official memory area. */ - shdr->sh_addr = addr; + shdr->sh_addr = (unsigned long)dest; pr_debug("\t0x%lx 0x%.8lx %s\n", (long)shdr->sh_addr, (long)shdr->sh_size, info->secstrings + shdr->sh_name); } @@ -2782,17 +2763,8 @@ int __weak module_finalize(const Elf_Ehdr *hdr, return 0; } -int __weak module_post_finalize(const Elf_Ehdr *hdr, - const Elf_Shdr *sechdrs, - struct module *me) -{ - return 0; -} - static int post_relocation(struct module *mod, const struct load_info *info) { - int ret; - /* Sort exception table now relocations are done. */ sort_extable(mod->extable, mod->extable + mod->num_exentries); @@ -2804,24 +2776,7 @@ static int post_relocation(struct module *mod, const struct load_info *info) add_kallsyms(mod, info); /* Arch-specific module finalizing. */ - ret = module_finalize(info->hdr, info->sechdrs, mod); - if (ret) - return ret; - - for_each_mod_mem_type(type) { - struct module_memory *mem = &mod->mem[type]; - - if (mem->is_rox) { - if (!execmem_update_copy(mem->base, mem->rw_copy, - mem->size)) - return -ENOMEM; - - vfree(mem->rw_copy); - mem->rw_copy = NULL; - } - } - - return module_post_finalize(info->hdr, info->sechdrs, mod); + return module_finalize(info->hdr, info->sechdrs, mod); } /* Call module constructors. */ diff --git a/kernel/module/strict_rwx.c b/kernel/module/strict_rwx.c index 239e5013359d..ce47b6346f27 100644 --- a/kernel/module/strict_rwx.c +++ b/kernel/module/strict_rwx.c @@ -9,6 +9,7 @@ #include #include #include +#include #include "internal.h" static int module_set_memory(const struct module *mod, enum mod_mem_type type, @@ -32,12 +33,12 @@ static int module_set_memory(const struct module *mod, enum mod_mem_type type, int module_enable_text_rox(const struct module *mod) { for_class_mod_mem_type(type, text) { + const struct module_memory *mem = &mod->mem[type]; int ret; - if (mod->mem[type].is_rox) - continue; - - if (IS_ENABLED(CONFIG_STRICT_MODULE_RWX)) + if (mem->is_rox) + ret = execmem_restore_rox(mem->base, mem->size); + else if (IS_ENABLED(CONFIG_STRICT_MODULE_RWX)) ret = module_set_memory(mod, type, set_memory_rox); else ret = module_set_memory(mod, type, set_memory_x); From patchwork Fri Dec 27 07:28:24 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mike Rapoport X-Patchwork-Id: 13921589 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 kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 6D3FFE77191 for ; Fri, 27 Dec 2024 07:30:01 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 0AE1A6B009B; Fri, 27 Dec 2024 02:30:01 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 05D346B009C; Fri, 27 Dec 2024 02:30:01 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id DF2766B009D; Fri, 27 Dec 2024 02:30:00 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id B55186B009B for ; Fri, 27 Dec 2024 02:30:00 -0500 (EST) Received: from smtpin16.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id 3E59EC04AD for ; Fri, 27 Dec 2024 07:30:00 +0000 (UTC) X-FDA: 82939914120.16.F486DBC Received: from nyc.source.kernel.org (nyc.source.kernel.org [147.75.193.91]) by imf02.hostedemail.com (Postfix) with ESMTP id C26658000B for ; Fri, 27 Dec 2024 07:28:36 +0000 (UTC) Authentication-Results: imf02.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=jlHZ25qy; dmarc=pass (policy=quarantine) header.from=kernel.org; spf=pass (imf02.hostedemail.com: domain of rppt@kernel.org designates 147.75.193.91 as permitted sender) smtp.mailfrom=rppt@kernel.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1735284579; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=13TmkeVkr2qGdMj9Vqok33BQm9CxYM1haCBTyjvpy0Q=; b=UE64R1OczDrYvnyt1ToasvkqH2w5NhVWVupVv8zFyCMVYwyz7jZCKs1qOPx44iSSYGv620 rXYAxXicAA4ldnmEyHtAR0vi6QIQADlBo6JRl2Ofnapkwhl6vhHWq86cSKaRO3gW3jSLv8 zCZ8O2L5lWEO5tEDeWenPzGst3lTV0w= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1735284579; a=rsa-sha256; cv=none; b=QVu3O/AramV97HVEG6Sal2Igj1XfC+gQiG1FWZaPto+5zEU+Y4V1YZur5xonVEkHlxH53B SEA5lqp2RMB7ZRl110qL87rUohnjRLKmnAxyJFZPsF4TFCCI8+veWUBvitnnE1RU13GHxX CvZNAYHIMnXb8rom9/9bA8N4juBo7tc= ARC-Authentication-Results: i=1; imf02.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=jlHZ25qy; dmarc=pass (policy=quarantine) header.from=kernel.org; spf=pass (imf02.hostedemail.com: domain of rppt@kernel.org designates 147.75.193.91 as permitted sender) smtp.mailfrom=rppt@kernel.org Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by nyc.source.kernel.org (Postfix) with ESMTP id 1348AA41245; Fri, 27 Dec 2024 07:28:08 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 8C23AC4CED0; Fri, 27 Dec 2024 07:29:47 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1735284597; bh=5WcbMLleT0o3eKZQNgsMEVrIOTvbPCyMPrwQ3+fNd90=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=jlHZ25qygTt2Ury+pMoPvV9Zb4oYhrjJ1pPQEZOmimuBXD2abMRyso+nUxYobpNXE 7IZ+RpDwLXV373hHtGSUHhafE5yyIZHU6AXF2ugv8SuxdeXc05KlarEYcclHxwsLgy 5A6mLy74SK1LE9Z9/DVwj9MlNxf1L6vuYFvd+v3q6dEosXPPY3xbIFi7b2XZYw87v8 HXggWgrf7JMrndHd1XWZIagsSMrCbwXyeBlAYfSa04XnkV36FWqvmctblC7mKkyN0S DMmszSaB9IjjzP+B/X9Kd16zVeqUpFyFjjd5RM0dCTb3JiHDOF1Kbir97dYE06jenS 2UbAijkL5QYTw== From: Mike Rapoport To: Andrew Morton Cc: Andy Lutomirski , Anton Ivanov , Borislav Petkov , Brendan Higgins , Daniel Gomez , Daniel Thompson , Dave Hansen , David Gow , Douglas Anderson , Ingo Molnar , Jason Wessel , Jiri Kosina , Joe Lawrence , Johannes Berg , Josh Poimboeuf , "Kirill A. Shutemov" , Luis Chamberlain , Mark Rutland , Masami Hiramatsu , Mike Rapoport , Miroslav Benes , "H. Peter Anvin" , Peter Zijlstra , Petr Mladek , Petr Pavlu , Rae Moar , Richard Weinberger , Sami Tolvanen , Shuah Khan , Song Liu , Steven Rostedt , Thomas Gleixner , kgdb-bugreport@lists.sourceforge.net, kunit-dev@googlegroups.com, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, linux-mm@kvack.org, linux-modules@vger.kernel.org, linux-trace-kernel@vger.kernel.org, linux-um@lists.infradead.org, live-patching@vger.kernel.org, x86@kernel.org Subject: [PATCH 7/8] Revert "x86/module: prepare module loading for ROX allocations of text" Date: Fri, 27 Dec 2024 09:28:24 +0200 Message-ID: <20241227072825.1288491-8-rppt@kernel.org> X-Mailer: git-send-email 2.45.2 In-Reply-To: <20241227072825.1288491-1-rppt@kernel.org> References: <20241227072825.1288491-1-rppt@kernel.org> MIME-Version: 1.0 X-Stat-Signature: s3rsp8ce33s7npk41p3ftcx56ywpteo1 X-Rspamd-Queue-Id: C26658000B X-Rspam-User: X-Rspamd-Server: rspam01 X-HE-Tag: 1735284516-197943 X-HE-Meta: U2FsdGVkX1+FCX70bkjTqeFrlRgz35Ry7nYaucG/PsxQN9ZOtRVt4ZCs8mK+syE6P7a1shAWHG6xtBuBpREXEICCQGqSm/wJdQvnxjKoP8NzH6FKTjblRePE/UEYgBgblUYQ8+IwXNoWqjB+jNQ9tmWnOntmSPmO/pQlUXek46RgC+3Avj6fxQmzKbNJBePhOSq41Glv7arJkj2aPUKI9OkBeMzHYVvlw36zxA6vwUK0JA71Dgr5TcBxGqYza2kTBeByFLY12xrtHj6M7U/XrbWiycnh333Kii/+9Y5u7NNNXAUjotsX782QYwQ1l6SlnzUn7z3hpPV/JmMPX4JZBzCvsIk2jPs+SSiWChM6+qzQPYRKeklAdWRSlH1GDrx9JYVTOG1Vnvxx6wW/WajQlFCtE1xvLQLkM2c+sVo/UvhIrx48TutMG1TOEND0roWcf0854gmwarPtFoqe46tO2SIxyBWaxPSWgDBRwmZgNYqPyZdc48RIB+B6cn7rlVothNmzHeZ060N84fk38FmdzkaOWb4PQqN9JiAkOeIeYSYSCTE0jw5JoYX3IyBcWo+6Ni8gnSkBcKKJTt8/qCsHZL2WRVNVeYzxRY1UeoL/E+jhXMFB4pMBfvHlNJkVPzzz9B5I0BosYkj+wDdVXpd3X7LDUx9HYLqbF1t/orQjLXe9aCkJLeXb4IjMn0baJj6/6Ybmld/s3DcQhrB3ISutVs0J0rQ7APzHF6nDv4ByxSXORuJzk9cQn1ZiE9PLMlecpxD+BBQjNU+163HNCaVyVPJWO2Yd2/YDCWn4fz9NANous6Mkv7vM5JRNJ6+Z5JlYFnNt5sL/LKVcaSstc/9kocyKeRuoy7zIrgjj6TX9gOvllMTNnUefUV9AJOSfPa1Xo7bh3tfJOVXp+zgWYUXNK2LFQ8iPoF5PEbGI3aqXod6PTnNh/bXJhK8GoYpVFtlXl6raDQPFBrD4Aqkw2YD yC7PVkoF EILifbNjUBd94WAFDZAbDoAcgn/mFciSJNrBKxpmDYTy020HY0YARGwhg9CLobUtP46Lh+OfsSljlBRERedqAjpK8OL8y1f4AE0E+FrMNErmFf92Nnti1Ms2N2NG+WxQti1B9cJaU6+zXaK45oS26atTQnamCjuEI8I+nQShZTC+0mCD8QrMvO5eU6GugSwiaEAEj5prWJDIryU+58X3qIj840kN0HR6fr54xY4HvpJxjbLI= X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: From: "Mike Rapoport (Microsoft)" The module code does not create a writable copy of the executable memory anymore so there is no need to handle it in module relocation and alternatives patching. This reverts commit 9bfc4824fd4836c16bb44f922bfaffba5da3e4f3. Signed-off-by: Mike Rapoport (Microsoft) --- arch/um/kernel/um_arch.c | 11 +- arch/x86/entry/vdso/vma.c | 3 +- arch/x86/include/asm/alternative.h | 14 +-- arch/x86/kernel/alternative.c | 181 ++++++++++++----------------- arch/x86/kernel/ftrace.c | 30 +++-- arch/x86/kernel/module.c | 45 +++---- 6 files changed, 117 insertions(+), 167 deletions(-) diff --git a/arch/um/kernel/um_arch.c b/arch/um/kernel/um_arch.c index 8037a967225d..d2cc2c69a8c4 100644 --- a/arch/um/kernel/um_arch.c +++ b/arch/um/kernel/um_arch.c @@ -440,25 +440,24 @@ void __init arch_cpu_finalize_init(void) os_check_bugs(); } -void apply_seal_endbr(s32 *start, s32 *end, struct module *mod) +void apply_seal_endbr(s32 *start, s32 *end) { } -void apply_retpolines(s32 *start, s32 *end, struct module *mod) +void apply_retpolines(s32 *start, s32 *end) { } -void apply_returns(s32 *start, s32 *end, struct module *mod) +void apply_returns(s32 *start, s32 *end) { } void apply_fineibt(s32 *start_retpoline, s32 *end_retpoline, - s32 *start_cfi, s32 *end_cfi, struct module *mod) + s32 *start_cfi, s32 *end_cfi) { } -void apply_alternatives(struct alt_instr *start, struct alt_instr *end, - struct module *mod) +void apply_alternatives(struct alt_instr *start, struct alt_instr *end) { } diff --git a/arch/x86/entry/vdso/vma.c b/arch/x86/entry/vdso/vma.c index 39e6efc1a9ca..bfc7cabf4017 100644 --- a/arch/x86/entry/vdso/vma.c +++ b/arch/x86/entry/vdso/vma.c @@ -48,8 +48,7 @@ int __init init_vdso_image(const struct vdso_image *image) apply_alternatives((struct alt_instr *)(image->data + image->alt), (struct alt_instr *)(image->data + image->alt + - image->alt_len), - NULL); + image->alt_len)); return 0; } diff --git a/arch/x86/include/asm/alternative.h b/arch/x86/include/asm/alternative.h index dc03a647776d..ca9ae606aab9 100644 --- a/arch/x86/include/asm/alternative.h +++ b/arch/x86/include/asm/alternative.h @@ -96,16 +96,16 @@ extern struct alt_instr __alt_instructions[], __alt_instructions_end[]; * instructions were patched in already: */ extern int alternatives_patched; -struct module; extern void alternative_instructions(void); -extern void apply_alternatives(struct alt_instr *start, struct alt_instr *end, - struct module *mod); -extern void apply_retpolines(s32 *start, s32 *end, struct module *mod); -extern void apply_returns(s32 *start, s32 *end, struct module *mod); -extern void apply_seal_endbr(s32 *start, s32 *end, struct module *mod); +extern void apply_alternatives(struct alt_instr *start, struct alt_instr *end); +extern void apply_retpolines(s32 *start, s32 *end); +extern void apply_returns(s32 *start, s32 *end); +extern void apply_seal_endbr(s32 *start, s32 *end); extern void apply_fineibt(s32 *start_retpoline, s32 *end_retpoine, - s32 *start_cfi, s32 *end_cfi, struct module *mod); + s32 *start_cfi, s32 *end_cfi); + +struct module; struct callthunk_sites { s32 *call_start, *call_end; diff --git a/arch/x86/kernel/alternative.c b/arch/x86/kernel/alternative.c index 243843e44e89..d17518ca19b8 100644 --- a/arch/x86/kernel/alternative.c +++ b/arch/x86/kernel/alternative.c @@ -392,10 +392,8 @@ EXPORT_SYMBOL(BUG_func); * Rewrite the "call BUG_func" replacement to point to the target of the * indirect pv_ops call "call *disp(%ip)". */ -static int alt_replace_call(u8 *instr, u8 *insn_buff, struct alt_instr *a, - struct module *mod) +static int alt_replace_call(u8 *instr, u8 *insn_buff, struct alt_instr *a) { - u8 *wr_instr = module_writable_address(mod, instr); void *target, *bug = &BUG_func; s32 disp; @@ -405,14 +403,14 @@ static int alt_replace_call(u8 *instr, u8 *insn_buff, struct alt_instr *a, } if (a->instrlen != 6 || - wr_instr[0] != CALL_RIP_REL_OPCODE || - wr_instr[1] != CALL_RIP_REL_MODRM) { + instr[0] != CALL_RIP_REL_OPCODE || + instr[1] != CALL_RIP_REL_MODRM) { pr_err("ALT_FLAG_DIRECT_CALL set for unrecognized indirect call\n"); BUG(); } /* Skip CALL_RIP_REL_OPCODE and CALL_RIP_REL_MODRM */ - disp = *(s32 *)(wr_instr + 2); + disp = *(s32 *)(instr + 2); #ifdef CONFIG_X86_64 /* ff 15 00 00 00 00 call *0x0(%rip) */ /* target address is stored at "next instruction + disp". */ @@ -450,8 +448,7 @@ static inline u8 * instr_va(struct alt_instr *i) * to refetch changed I$ lines. */ void __init_or_module noinline apply_alternatives(struct alt_instr *start, - struct alt_instr *end, - struct module *mod) + struct alt_instr *end) { u8 insn_buff[MAX_PATCH_LEN]; u8 *instr, *replacement; @@ -480,7 +477,6 @@ void __init_or_module noinline apply_alternatives(struct alt_instr *start, */ for (a = start; a < end; a++) { int insn_buff_sz = 0; - u8 *wr_instr, *wr_replacement; /* * In case of nested ALTERNATIVE()s the outer alternative might @@ -494,11 +490,7 @@ void __init_or_module noinline apply_alternatives(struct alt_instr *start, } instr = instr_va(a); - wr_instr = module_writable_address(mod, instr); - replacement = (u8 *)&a->repl_offset + a->repl_offset; - wr_replacement = module_writable_address(mod, replacement); - BUG_ON(a->instrlen > sizeof(insn_buff)); BUG_ON(a->cpuid >= (NCAPINTS + NBUGINTS) * 32); @@ -509,9 +501,9 @@ void __init_or_module noinline apply_alternatives(struct alt_instr *start, * patch if feature is *NOT* present. */ if (!boot_cpu_has(a->cpuid) == !(a->flags & ALT_FLAG_NOT)) { - memcpy(insn_buff, wr_instr, a->instrlen); + memcpy(insn_buff, instr, a->instrlen); optimize_nops(instr, insn_buff, a->instrlen); - text_poke_early(wr_instr, insn_buff, a->instrlen); + text_poke_early(instr, insn_buff, a->instrlen); continue; } @@ -521,12 +513,11 @@ void __init_or_module noinline apply_alternatives(struct alt_instr *start, instr, instr, a->instrlen, replacement, a->replacementlen, a->flags); - memcpy(insn_buff, wr_replacement, a->replacementlen); + memcpy(insn_buff, replacement, a->replacementlen); insn_buff_sz = a->replacementlen; if (a->flags & ALT_FLAG_DIRECT_CALL) { - insn_buff_sz = alt_replace_call(instr, insn_buff, a, - mod); + insn_buff_sz = alt_replace_call(instr, insn_buff, a); if (insn_buff_sz < 0) continue; } @@ -536,11 +527,11 @@ void __init_or_module noinline apply_alternatives(struct alt_instr *start, apply_relocation(insn_buff, instr, a->instrlen, replacement, a->replacementlen); - DUMP_BYTES(ALT, wr_instr, a->instrlen, "%px: old_insn: ", instr); + DUMP_BYTES(ALT, instr, a->instrlen, "%px: old_insn: ", instr); DUMP_BYTES(ALT, replacement, a->replacementlen, "%px: rpl_insn: ", replacement); DUMP_BYTES(ALT, insn_buff, insn_buff_sz, "%px: final_insn: ", instr); - text_poke_early(wr_instr, insn_buff, insn_buff_sz); + text_poke_early(instr, insn_buff, insn_buff_sz); } kasan_enable_current(); @@ -731,20 +722,18 @@ static int patch_retpoline(void *addr, struct insn *insn, u8 *bytes) /* * Generated by 'objtool --retpoline'. */ -void __init_or_module noinline apply_retpolines(s32 *start, s32 *end, - struct module *mod) +void __init_or_module noinline apply_retpolines(s32 *start, s32 *end) { s32 *s; for (s = start; s < end; s++) { void *addr = (void *)s + *s; - void *wr_addr = module_writable_address(mod, addr); struct insn insn; int len, ret; u8 bytes[16]; u8 op1, op2; - ret = insn_decode_kernel(&insn, wr_addr); + ret = insn_decode_kernel(&insn, addr); if (WARN_ON_ONCE(ret < 0)) continue; @@ -772,9 +761,9 @@ void __init_or_module noinline apply_retpolines(s32 *start, s32 *end, len = patch_retpoline(addr, &insn, bytes); if (len == insn.length) { optimize_nops(addr, bytes, len); - DUMP_BYTES(RETPOLINE, ((u8*)wr_addr), len, "%px: orig: ", addr); + DUMP_BYTES(RETPOLINE, ((u8*)addr), len, "%px: orig: ", addr); DUMP_BYTES(RETPOLINE, ((u8*)bytes), len, "%px: repl: ", addr); - text_poke_early(wr_addr, bytes, len); + text_poke_early(addr, bytes, len); } } } @@ -810,8 +799,7 @@ static int patch_return(void *addr, struct insn *insn, u8 *bytes) return i; } -void __init_or_module noinline apply_returns(s32 *start, s32 *end, - struct module *mod) +void __init_or_module noinline apply_returns(s32 *start, s32 *end) { s32 *s; @@ -820,13 +808,12 @@ void __init_or_module noinline apply_returns(s32 *start, s32 *end, for (s = start; s < end; s++) { void *dest = NULL, *addr = (void *)s + *s; - void *wr_addr = module_writable_address(mod, addr); struct insn insn; int len, ret; u8 bytes[16]; u8 op; - ret = insn_decode_kernel(&insn, wr_addr); + ret = insn_decode_kernel(&insn, addr); if (WARN_ON_ONCE(ret < 0)) continue; @@ -846,35 +833,32 @@ void __init_or_module noinline apply_returns(s32 *start, s32 *end, len = patch_return(addr, &insn, bytes); if (len == insn.length) { - DUMP_BYTES(RET, ((u8*)wr_addr), len, "%px: orig: ", addr); + DUMP_BYTES(RET, ((u8*)addr), len, "%px: orig: ", addr); DUMP_BYTES(RET, ((u8*)bytes), len, "%px: repl: ", addr); - text_poke_early(wr_addr, bytes, len); + text_poke_early(addr, bytes, len); } } } #else -void __init_or_module noinline apply_returns(s32 *start, s32 *end, - struct module *mod) { } +void __init_or_module noinline apply_returns(s32 *start, s32 *end) { } #endif /* CONFIG_MITIGATION_RETHUNK */ #else /* !CONFIG_MITIGATION_RETPOLINE || !CONFIG_OBJTOOL */ -void __init_or_module noinline apply_retpolines(s32 *start, s32 *end, - struct module *mod) { } -void __init_or_module noinline apply_returns(s32 *start, s32 *end, - struct module *mod) { } +void __init_or_module noinline apply_retpolines(s32 *start, s32 *end) { } +void __init_or_module noinline apply_returns(s32 *start, s32 *end) { } #endif /* CONFIG_MITIGATION_RETPOLINE && CONFIG_OBJTOOL */ #ifdef CONFIG_X86_KERNEL_IBT -static void poison_cfi(void *addr, void *wr_addr); +static void poison_cfi(void *addr); -static void __init_or_module poison_endbr(void *addr, void *wr_addr, bool warn) +static void __init_or_module poison_endbr(void *addr, bool warn) { u32 endbr, poison = gen_endbr_poison(); - if (WARN_ON_ONCE(get_kernel_nofault(endbr, wr_addr))) + if (WARN_ON_ONCE(get_kernel_nofault(endbr, addr))) return; if (!is_endbr(endbr)) { @@ -889,7 +873,7 @@ static void __init_or_module poison_endbr(void *addr, void *wr_addr, bool warn) */ DUMP_BYTES(ENDBR, ((u8*)addr), 4, "%px: orig: ", addr); DUMP_BYTES(ENDBR, ((u8*)&poison), 4, "%px: repl: ", addr); - text_poke_early(wr_addr, &poison, 4); + text_poke_early(addr, &poison, 4); } /* @@ -898,23 +882,22 @@ static void __init_or_module poison_endbr(void *addr, void *wr_addr, bool warn) * Seal the functions for indirect calls by clobbering the ENDBR instructions * and the kCFI hash value. */ -void __init_or_module noinline apply_seal_endbr(s32 *start, s32 *end, struct module *mod) +void __init_or_module noinline apply_seal_endbr(s32 *start, s32 *end) { s32 *s; for (s = start; s < end; s++) { void *addr = (void *)s + *s; - void *wr_addr = module_writable_address(mod, addr); - poison_endbr(addr, wr_addr, true); + poison_endbr(addr, true); if (IS_ENABLED(CONFIG_FINEIBT)) - poison_cfi(addr - 16, wr_addr - 16); + poison_cfi(addr - 16); } } #else -void __init_or_module apply_seal_endbr(s32 *start, s32 *end, struct module *mod) { } +void __init_or_module apply_seal_endbr(s32 *start, s32 *end) { } #endif /* CONFIG_X86_KERNEL_IBT */ @@ -1136,7 +1119,7 @@ static u32 decode_caller_hash(void *addr) } /* .retpoline_sites */ -static int cfi_disable_callers(s32 *start, s32 *end, struct module *mod) +static int cfi_disable_callers(s32 *start, s32 *end) { /* * Disable kCFI by patching in a JMP.d8, this leaves the hash immediate @@ -1148,23 +1131,20 @@ static int cfi_disable_callers(s32 *start, s32 *end, struct module *mod) for (s = start; s < end; s++) { void *addr = (void *)s + *s; - void *wr_addr; u32 hash; addr -= fineibt_caller_size; - wr_addr = module_writable_address(mod, addr); - hash = decode_caller_hash(wr_addr); - + hash = decode_caller_hash(addr); if (!hash) /* nocfi callers */ continue; - text_poke_early(wr_addr, jmp, 2); + text_poke_early(addr, jmp, 2); } return 0; } -static int cfi_enable_callers(s32 *start, s32 *end, struct module *mod) +static int cfi_enable_callers(s32 *start, s32 *end) { /* * Re-enable kCFI, undo what cfi_disable_callers() did. @@ -1174,115 +1154,106 @@ static int cfi_enable_callers(s32 *start, s32 *end, struct module *mod) for (s = start; s < end; s++) { void *addr = (void *)s + *s; - void *wr_addr; u32 hash; addr -= fineibt_caller_size; - wr_addr = module_writable_address(mod, addr); - hash = decode_caller_hash(wr_addr); + hash = decode_caller_hash(addr); if (!hash) /* nocfi callers */ continue; - text_poke_early(wr_addr, mov, 2); + text_poke_early(addr, mov, 2); } return 0; } /* .cfi_sites */ -static int cfi_rand_preamble(s32 *start, s32 *end, struct module *mod) +static int cfi_rand_preamble(s32 *start, s32 *end) { s32 *s; for (s = start; s < end; s++) { void *addr = (void *)s + *s; - void *wr_addr = module_writable_address(mod, addr); u32 hash; - hash = decode_preamble_hash(wr_addr); + hash = decode_preamble_hash(addr); if (WARN(!hash, "no CFI hash found at: %pS %px %*ph\n", addr, addr, 5, addr)) return -EINVAL; hash = cfi_rehash(hash); - text_poke_early(wr_addr + 1, &hash, 4); + text_poke_early(addr + 1, &hash, 4); } return 0; } -static int cfi_rewrite_preamble(s32 *start, s32 *end, struct module *mod) +static int cfi_rewrite_preamble(s32 *start, s32 *end) { s32 *s; for (s = start; s < end; s++) { void *addr = (void *)s + *s; - void *wr_addr = module_writable_address(mod, addr); u32 hash; - hash = decode_preamble_hash(wr_addr); + hash = decode_preamble_hash(addr); if (WARN(!hash, "no CFI hash found at: %pS %px %*ph\n", addr, addr, 5, addr)) return -EINVAL; - text_poke_early(wr_addr, fineibt_preamble_start, fineibt_preamble_size); - WARN_ON(*(u32 *)(wr_addr + fineibt_preamble_hash) != 0x12345678); - text_poke_early(wr_addr + fineibt_preamble_hash, &hash, 4); + text_poke_early(addr, fineibt_preamble_start, fineibt_preamble_size); + WARN_ON(*(u32 *)(addr + fineibt_preamble_hash) != 0x12345678); + text_poke_early(addr + fineibt_preamble_hash, &hash, 4); } return 0; } -static void cfi_rewrite_endbr(s32 *start, s32 *end, struct module *mod) +static void cfi_rewrite_endbr(s32 *start, s32 *end) { s32 *s; for (s = start; s < end; s++) { void *addr = (void *)s + *s; - void *wr_addr = module_writable_address(mod, addr); - poison_endbr(addr + 16, wr_addr + 16, false); + poison_endbr(addr+16, false); } } /* .retpoline_sites */ -static int cfi_rand_callers(s32 *start, s32 *end, struct module *mod) +static int cfi_rand_callers(s32 *start, s32 *end) { s32 *s; for (s = start; s < end; s++) { void *addr = (void *)s + *s; - void *wr_addr; u32 hash; addr -= fineibt_caller_size; - wr_addr = module_writable_address(mod, addr); - hash = decode_caller_hash(wr_addr); + hash = decode_caller_hash(addr); if (hash) { hash = -cfi_rehash(hash); - text_poke_early(wr_addr + 2, &hash, 4); + text_poke_early(addr + 2, &hash, 4); } } return 0; } -static int cfi_rewrite_callers(s32 *start, s32 *end, struct module *mod) +static int cfi_rewrite_callers(s32 *start, s32 *end) { s32 *s; for (s = start; s < end; s++) { void *addr = (void *)s + *s; - void *wr_addr; u32 hash; addr -= fineibt_caller_size; - wr_addr = module_writable_address(mod, addr); - hash = decode_caller_hash(wr_addr); + hash = decode_caller_hash(addr); if (hash) { - text_poke_early(wr_addr, fineibt_caller_start, fineibt_caller_size); - WARN_ON(*(u32 *)(wr_addr + fineibt_caller_hash) != 0x12345678); - text_poke_early(wr_addr + fineibt_caller_hash, &hash, 4); + text_poke_early(addr, fineibt_caller_start, fineibt_caller_size); + WARN_ON(*(u32 *)(addr + fineibt_caller_hash) != 0x12345678); + text_poke_early(addr + fineibt_caller_hash, &hash, 4); } /* rely on apply_retpolines() */ } @@ -1291,9 +1262,8 @@ static int cfi_rewrite_callers(s32 *start, s32 *end, struct module *mod) } static void __apply_fineibt(s32 *start_retpoline, s32 *end_retpoline, - s32 *start_cfi, s32 *end_cfi, struct module *mod) + s32 *start_cfi, s32 *end_cfi, bool builtin) { - bool builtin = mod ? false : true; int ret; if (WARN_ONCE(fineibt_preamble_size != 16, @@ -1311,7 +1281,7 @@ static void __apply_fineibt(s32 *start_retpoline, s32 *end_retpoline, * rewrite them. This disables all CFI. If this succeeds but any of the * later stages fails, we're without CFI. */ - ret = cfi_disable_callers(start_retpoline, end_retpoline, mod); + ret = cfi_disable_callers(start_retpoline, end_retpoline); if (ret) goto err; @@ -1322,11 +1292,11 @@ static void __apply_fineibt(s32 *start_retpoline, s32 *end_retpoline, cfi_bpf_subprog_hash = cfi_rehash(cfi_bpf_subprog_hash); } - ret = cfi_rand_preamble(start_cfi, end_cfi, mod); + ret = cfi_rand_preamble(start_cfi, end_cfi); if (ret) goto err; - ret = cfi_rand_callers(start_retpoline, end_retpoline, mod); + ret = cfi_rand_callers(start_retpoline, end_retpoline); if (ret) goto err; } @@ -1338,7 +1308,7 @@ static void __apply_fineibt(s32 *start_retpoline, s32 *end_retpoline, return; case CFI_KCFI: - ret = cfi_enable_callers(start_retpoline, end_retpoline, mod); + ret = cfi_enable_callers(start_retpoline, end_retpoline); if (ret) goto err; @@ -1348,17 +1318,17 @@ static void __apply_fineibt(s32 *start_retpoline, s32 *end_retpoline, case CFI_FINEIBT: /* place the FineIBT preamble at func()-16 */ - ret = cfi_rewrite_preamble(start_cfi, end_cfi, mod); + ret = cfi_rewrite_preamble(start_cfi, end_cfi); if (ret) goto err; /* rewrite the callers to target func()-16 */ - ret = cfi_rewrite_callers(start_retpoline, end_retpoline, mod); + ret = cfi_rewrite_callers(start_retpoline, end_retpoline); if (ret) goto err; /* now that nobody targets func()+0, remove ENDBR there */ - cfi_rewrite_endbr(start_cfi, end_cfi, mod); + cfi_rewrite_endbr(start_cfi, end_cfi); if (builtin) pr_info("Using FineIBT CFI\n"); @@ -1377,7 +1347,7 @@ static inline void poison_hash(void *addr) *(u32 *)addr = 0; } -static void poison_cfi(void *addr, void *wr_addr) +static void poison_cfi(void *addr) { switch (cfi_mode) { case CFI_FINEIBT: @@ -1389,8 +1359,8 @@ static void poison_cfi(void *addr, void *wr_addr) * ud2 * 1: nop */ - poison_endbr(addr, wr_addr, false); - poison_hash(wr_addr + fineibt_preamble_hash); + poison_endbr(addr, false); + poison_hash(addr + fineibt_preamble_hash); break; case CFI_KCFI: @@ -1399,7 +1369,7 @@ static void poison_cfi(void *addr, void *wr_addr) * movl $0, %eax * .skip 11, 0x90 */ - poison_hash(wr_addr + 1); + poison_hash(addr + 1); break; default: @@ -1410,21 +1380,22 @@ static void poison_cfi(void *addr, void *wr_addr) #else static void __apply_fineibt(s32 *start_retpoline, s32 *end_retpoline, - s32 *start_cfi, s32 *end_cfi, struct module *mod) + s32 *start_cfi, s32 *end_cfi, bool builtin) { } #ifdef CONFIG_X86_KERNEL_IBT -static void poison_cfi(void *addr, void *wr_addr) { } +static void poison_cfi(void *addr) { } #endif #endif void apply_fineibt(s32 *start_retpoline, s32 *end_retpoline, - s32 *start_cfi, s32 *end_cfi, struct module *mod) + s32 *start_cfi, s32 *end_cfi) { return __apply_fineibt(start_retpoline, end_retpoline, - start_cfi, end_cfi, mod); + start_cfi, end_cfi, + /* .builtin = */ false); } #ifdef CONFIG_SMP @@ -1721,16 +1692,16 @@ void __init alternative_instructions(void) paravirt_set_cap(); __apply_fineibt(__retpoline_sites, __retpoline_sites_end, - __cfi_sites, __cfi_sites_end, NULL); + __cfi_sites, __cfi_sites_end, true); /* * Rewrite the retpolines, must be done before alternatives since * those can rewrite the retpoline thunks. */ - apply_retpolines(__retpoline_sites, __retpoline_sites_end, NULL); - apply_returns(__return_sites, __return_sites_end, NULL); + apply_retpolines(__retpoline_sites, __retpoline_sites_end); + apply_returns(__return_sites, __return_sites_end); - apply_alternatives(__alt_instructions, __alt_instructions_end, NULL); + apply_alternatives(__alt_instructions, __alt_instructions_end); /* * Now all calls are established. Apply the call thunks if @@ -1741,7 +1712,7 @@ void __init alternative_instructions(void) /* * Seal all functions that do not have their address taken. */ - apply_seal_endbr(__ibt_endbr_seal, __ibt_endbr_seal_end, NULL); + apply_seal_endbr(__ibt_endbr_seal, __ibt_endbr_seal_end); #ifdef CONFIG_SMP /* Patch to UP if other cpus not imminent. */ diff --git a/arch/x86/kernel/ftrace.c b/arch/x86/kernel/ftrace.c index 4dd0ad6c94d6..adb09f78edb2 100644 --- a/arch/x86/kernel/ftrace.c +++ b/arch/x86/kernel/ftrace.c @@ -118,13 +118,10 @@ ftrace_modify_code_direct(unsigned long ip, const char *old_code, return ret; /* replace the text with the new text */ - if (ftrace_poke_late) { + if (ftrace_poke_late) text_poke_queue((void *)ip, new_code, MCOUNT_INSN_SIZE, NULL); - } else { - mutex_lock(&text_mutex); - text_poke((void *)ip, new_code, MCOUNT_INSN_SIZE); - mutex_unlock(&text_mutex); - } + else + text_poke_early((void *)ip, new_code, MCOUNT_INSN_SIZE); return 0; } @@ -321,7 +318,7 @@ create_trampoline(struct ftrace_ops *ops, unsigned int *tramp_size) unsigned const char op_ref[] = { 0x48, 0x8b, 0x15 }; unsigned const char retq[] = { RET_INSN_OPCODE, INT3_INSN_OPCODE }; union ftrace_op_code_union op_ptr; - void *ret; + int ret; if (ops->flags & FTRACE_OPS_FL_SAVE_REGS) { start_offset = (unsigned long)ftrace_regs_caller; @@ -352,15 +349,15 @@ create_trampoline(struct ftrace_ops *ops, unsigned int *tramp_size) npages = DIV_ROUND_UP(*tramp_size, PAGE_SIZE); /* Copy ftrace_caller onto the trampoline memory */ - ret = text_poke_copy(trampoline, (void *)start_offset, size); - if (WARN_ON(!ret)) + ret = copy_from_kernel_nofault(trampoline, (void *)start_offset, size); + if (WARN_ON(ret < 0)) goto fail; ip = trampoline + size; if (cpu_feature_enabled(X86_FEATURE_RETHUNK)) __text_gen_insn(ip, JMP32_INSN_OPCODE, ip, x86_return_thunk, JMP32_INSN_SIZE); else - text_poke_copy(ip, retq, sizeof(retq)); + memcpy(ip, retq, sizeof(retq)); /* No need to test direct calls on created trampolines */ if (ops->flags & FTRACE_OPS_FL_SAVE_REGS) { @@ -368,7 +365,8 @@ create_trampoline(struct ftrace_ops *ops, unsigned int *tramp_size) ip = trampoline + (jmp_offset - start_offset); if (WARN_ON(*(char *)ip != 0x75)) goto fail; - if (!text_poke_copy(ip, x86_nops[2], 2)) + ret = copy_from_kernel_nofault(ip, x86_nops[2], 2); + if (ret < 0) goto fail; } @@ -381,7 +379,7 @@ create_trampoline(struct ftrace_ops *ops, unsigned int *tramp_size) */ ptr = (unsigned long *)(trampoline + size + RET_SIZE); - text_poke_copy(ptr, &ops, sizeof(unsigned long)); + *ptr = (unsigned long)ops; op_offset -= start_offset; memcpy(&op_ptr, trampoline + op_offset, OP_REF_SIZE); @@ -397,7 +395,7 @@ create_trampoline(struct ftrace_ops *ops, unsigned int *tramp_size) op_ptr.offset = offset; /* put in the new offset to the ftrace_ops */ - text_poke_copy(trampoline + op_offset, &op_ptr, OP_REF_SIZE); + memcpy(trampoline + op_offset, &op_ptr, OP_REF_SIZE); /* put in the call to the function */ mutex_lock(&text_mutex); @@ -407,9 +405,9 @@ create_trampoline(struct ftrace_ops *ops, unsigned int *tramp_size) * the depth accounting before the call already. */ dest = ftrace_ops_get_func(ops); - text_poke_copy_locked(trampoline + call_offset, - text_gen_insn(CALL_INSN_OPCODE, trampoline + call_offset, dest), - CALL_INSN_SIZE, false); + memcpy(trampoline + call_offset, + text_gen_insn(CALL_INSN_OPCODE, trampoline + call_offset, dest), + CALL_INSN_SIZE); mutex_unlock(&text_mutex); /* ALLOC_TRAMP flags lets us know we created it */ diff --git a/arch/x86/kernel/module.c b/arch/x86/kernel/module.c index 8984abd91c00..837450b6e882 100644 --- a/arch/x86/kernel/module.c +++ b/arch/x86/kernel/module.c @@ -146,21 +146,18 @@ static int __write_relocate_add(Elf64_Shdr *sechdrs, } if (apply) { - void *wr_loc = module_writable_address(me, loc); - - if (memcmp(wr_loc, &zero, size)) { + if (memcmp(loc, &zero, size)) { pr_err("x86/modules: Invalid relocation target, existing value is nonzero for type %d, loc %p, val %Lx\n", (int)ELF64_R_TYPE(rel[i].r_info), loc, val); return -ENOEXEC; } - write(wr_loc, &val, size); + write(loc, &val, size); } else { if (memcmp(loc, &val, size)) { pr_warn("x86/modules: Invalid relocation target, existing value does not match expected value for type %d, loc %p, val %Lx\n", (int)ELF64_R_TYPE(rel[i].r_info), loc, val); return -ENOEXEC; } - /* FIXME: needs care for ROX module allocations */ write(loc, &zero, size); } } @@ -227,7 +224,7 @@ int module_finalize(const Elf_Ehdr *hdr, const Elf_Shdr *sechdrs, struct module *me) { - const Elf_Shdr *s, *alt = NULL, + const Elf_Shdr *s, *alt = NULL, *locks = NULL, *orc = NULL, *orc_ip = NULL, *retpolines = NULL, *returns = NULL, *ibt_endbr = NULL, *calls = NULL, *cfi = NULL; @@ -236,6 +233,8 @@ int module_finalize(const Elf_Ehdr *hdr, for (s = sechdrs; s < sechdrs + hdr->e_shnum; s++) { if (!strcmp(".altinstructions", secstrings + s->sh_name)) alt = s; + if (!strcmp(".smp_locks", secstrings + s->sh_name)) + locks = s; if (!strcmp(".orc_unwind", secstrings + s->sh_name)) orc = s; if (!strcmp(".orc_unwind_ip", secstrings + s->sh_name)) @@ -266,20 +265,20 @@ int module_finalize(const Elf_Ehdr *hdr, csize = cfi->sh_size; } - apply_fineibt(rseg, rseg + rsize, cseg, cseg + csize, me); + apply_fineibt(rseg, rseg + rsize, cseg, cseg + csize); } if (retpolines) { void *rseg = (void *)retpolines->sh_addr; - apply_retpolines(rseg, rseg + retpolines->sh_size, me); + apply_retpolines(rseg, rseg + retpolines->sh_size); } if (returns) { void *rseg = (void *)returns->sh_addr; - apply_returns(rseg, rseg + returns->sh_size, me); + apply_returns(rseg, rseg + returns->sh_size); } if (alt) { /* patch .altinstructions */ void *aseg = (void *)alt->sh_addr; - apply_alternatives(aseg, aseg + alt->sh_size, me); + apply_alternatives(aseg, aseg + alt->sh_size); } if (calls || alt) { struct callthunk_sites cs = {}; @@ -298,28 +297,8 @@ int module_finalize(const Elf_Ehdr *hdr, } if (ibt_endbr) { void *iseg = (void *)ibt_endbr->sh_addr; - apply_seal_endbr(iseg, iseg + ibt_endbr->sh_size, me); + apply_seal_endbr(iseg, iseg + ibt_endbr->sh_size); } - - if (orc && orc_ip) - unwind_module_init(me, (void *)orc_ip->sh_addr, orc_ip->sh_size, - (void *)orc->sh_addr, orc->sh_size); - - return 0; -} - -int module_post_finalize(const Elf_Ehdr *hdr, - const Elf_Shdr *sechdrs, - struct module *me) -{ - const Elf_Shdr *s, *locks = NULL; - char *secstrings = (void *)hdr + sechdrs[hdr->e_shstrndx].sh_offset; - - for (s = sechdrs; s < sechdrs + hdr->e_shnum; s++) { - if (!strcmp(".smp_locks", secstrings + s->sh_name)) - locks = s; - } - if (locks) { void *lseg = (void *)locks->sh_addr; void *text = me->mem[MOD_TEXT].base; @@ -329,6 +308,10 @@ int module_post_finalize(const Elf_Ehdr *hdr, text, text_end); } + if (orc && orc_ip) + unwind_module_init(me, (void *)orc_ip->sh_addr, orc_ip->sh_size, + (void *)orc->sh_addr, orc->sh_size); + return 0; } From patchwork Fri Dec 27 07:28:25 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mike Rapoport X-Patchwork-Id: 13921590 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 kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 50068E77191 for ; Fri, 27 Dec 2024 07:30:11 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id E1A346B009D; Fri, 27 Dec 2024 02:30:10 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id DCA8F6B009E; Fri, 27 Dec 2024 02:30:10 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id C6C7C6B009F; Fri, 27 Dec 2024 02:30:10 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id A5BB86B009D for ; Fri, 27 Dec 2024 02:30:10 -0500 (EST) Received: from smtpin15.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 67DD3A135F for ; Fri, 27 Dec 2024 07:30:10 +0000 (UTC) X-FDA: 82939914330.15.C778908 Received: from nyc.source.kernel.org (nyc.source.kernel.org [147.75.193.91]) by imf25.hostedemail.com (Postfix) with ESMTP id 3A403A0011 for ; Fri, 27 Dec 2024 07:29:38 +0000 (UTC) Authentication-Results: imf25.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=ojHOLd1j; dmarc=pass (policy=quarantine) header.from=kernel.org; spf=pass (imf25.hostedemail.com: domain of rppt@kernel.org designates 147.75.193.91 as permitted sender) smtp.mailfrom=rppt@kernel.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1735284589; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=mVhqgsU2xvF7EXptjIpaUTgDXd0zYOHitWWau0YEx3Y=; b=P7WTT01PJUCpVF9mzF6Hjvmf0Vz9fvovoiG4itIgCZgwzBwVgTjILYj9zcB3NOTDjpBnv7 BEPq6ebC1wLcglANXZQvd06QHwCYVTinVROwRzj0F/HyDvZdRpp0o5bRLn9eEbEs9iFkDL bapqF7T8KKVAeOhA4Dtmd6QeZGcoY9w= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1735284589; a=rsa-sha256; cv=none; b=xNO2NyrdN/gFuveQZ8i3AlKaU5m0XQ4OHyY+vMTuM6168+a3j9t2iLt3/P8T64v7gIiqUi CmBRfwbtCIlkd9P/PN0/69RJfznB3UBvJBJB+EIVjB0wH3t2XNVj+d/bpCTKEMon2M/aRg JawdsubFscmi0uTMXAYONMq0dxX1me0= ARC-Authentication-Results: i=1; imf25.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=ojHOLd1j; dmarc=pass (policy=quarantine) header.from=kernel.org; spf=pass (imf25.hostedemail.com: domain of rppt@kernel.org designates 147.75.193.91 as permitted sender) smtp.mailfrom=rppt@kernel.org Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by nyc.source.kernel.org (Postfix) with ESMTP id 5DF8DA4120F; Fri, 27 Dec 2024 07:28:18 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 14BE3C4CED7; Fri, 27 Dec 2024 07:29:57 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1735284607; bh=4TvuKJvEyJTpjYu41+xgE0YbwjdhXodJecvgvQg8Q9g=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=ojHOLd1jY+IbJlMWHlQqmy11U2unw6udkZruAXzLiwt6p3tf38LSDBsdZlSGAr5Ti oWt8KIUmukiINpZQ30rlaQW/esY9vKKs3doeOUrzcy8pW5ZYTPFGO0ZI3gJxReLmOn YaQcqvRdKh4p95/gxdtlnhOY0W/KmRAcMm6L8WPojyDC4zVUscK6EE9nVOGL4nLjWR r1cjGnKgKxQGMvk4aIJGohIjP0qxuM3ubVIpYcu1eURVo5rELi+KYU6DKD9THakf4i 3tyThFwaKvxwFltVsRKo7mTf+dPkTgS+g+XxaWhLAOBU/pmfaSEwU9bFMm6R9seeLv 0fvxg0DCRWzQw== From: Mike Rapoport To: Andrew Morton Cc: Andy Lutomirski , Anton Ivanov , Borislav Petkov , Brendan Higgins , Daniel Gomez , Daniel Thompson , Dave Hansen , David Gow , Douglas Anderson , Ingo Molnar , Jason Wessel , Jiri Kosina , Joe Lawrence , Johannes Berg , Josh Poimboeuf , "Kirill A. Shutemov" , Luis Chamberlain , Mark Rutland , Masami Hiramatsu , Mike Rapoport , Miroslav Benes , "H. Peter Anvin" , Peter Zijlstra , Petr Mladek , Petr Pavlu , Rae Moar , Richard Weinberger , Sami Tolvanen , Shuah Khan , Song Liu , Steven Rostedt , Thomas Gleixner , kgdb-bugreport@lists.sourceforge.net, kunit-dev@googlegroups.com, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, linux-mm@kvack.org, linux-modules@vger.kernel.org, linux-trace-kernel@vger.kernel.org, linux-um@lists.infradead.org, live-patching@vger.kernel.org, x86@kernel.org Subject: [PATCH 8/8] module: drop unused module_writable_address() Date: Fri, 27 Dec 2024 09:28:25 +0200 Message-ID: <20241227072825.1288491-9-rppt@kernel.org> X-Mailer: git-send-email 2.45.2 In-Reply-To: <20241227072825.1288491-1-rppt@kernel.org> References: <20241227072825.1288491-1-rppt@kernel.org> MIME-Version: 1.0 X-Stat-Signature: 1bt3kdykhqdfsixytcmqk5ic9ssahapy X-Rspamd-Queue-Id: 3A403A0011 X-Rspam-User: X-Rspamd-Server: rspam01 X-HE-Tag: 1735284578-169371 X-HE-Meta: U2FsdGVkX1/8Mug5pJXuIj3q7RNVoATcGnOscErxFlO7Fb7Wd9KitHb/d5ZvljkTdENjOV4AOBYChti10TSuJZE+RarpFnxkH1UtH3BDwqnW+x5BNyalFYkeoBA8Sy8WfdcWJ3zD6yZW9NFG1ZEKG8cNAkn01cQkC0mRhr9J8c7j9/lKM8BqcYHhS9WKn9+1fChXoQ+6bxFa7tGLtzMsx5ZvWZO7wbmGoGLEUihbHRXIi0Q2d9s/ufKMlauNz/Z6XCcYjR5t03kOIjHj667rlPxPBsQ0gNQp8b6oYzFEnbspZO6iPzT4RrAY/yGNrle4JVqFcjlD+gEkmoipB/TpLmHeav0I/DoU8dPMfgaRu5AEhrSOpEHEGyw0OMP3gUZ3TfbE9BEkMjYnUOFAgesxfa7hXsfb45JtQ/eG1b6DAVIykY/VbFmjJRmZjG3cnmhwdt/+qhVdkg4pi1BjsiTDOxCW8AH6pBPZSJ09OZVFMxrbSVmL4lpQy3D9hivko1F05ASJO5SKvC1Qc7HYkxoijbhCUvklog7AZ++zaPBa9jTVhrsB5QPQZtv/gmzDKErNj0UeHUgeLXvG81HNtamhFjd/4l7XiHxeUr5J6qutxCUP8Vl+CdIL6xqdOE8VhrWG+DuddwNKNFTDTNDSeMhaM3stsN6wd9E3/vAtoRltIH6uFIAV1onyDR0R7oRCtWxQ9HhPg4eyIvCnVF67t/PIqoOrOdsbEfi45i3XdR3sTqsS/fOZ2SpR9kguMGoNfkpRshGlvhPVt31ysND11pNKEVLcv3bDrIiE3SHo6pOUNkhlDshvYpLbaYjGrlNQn7RjZF191s1i0OH8uL5n/KJH6MKsKqNA11HsY9h46SJ53FY0uxLIkpCPhX1TtOT1EssxAjPMrT+3xnoahlHtN3k2rPjLNg/jlMao9r0xVAir3A2I2M3TA4XESK1kWEHBoWn+eX+uZJUADPIBF672bik DGdcRqoz nuYkOYaz52B67uFDf3/8NuZCiRnlkoSbpiOwzvWD0otlExCIBaReYOXz0pqng4AIl8KVdMwQhv2TpfaOUAFQ9mduWgI7C8KibAJw1/WGnLpowdfkkKAYWZi4OUmYAvtHxGxQin3cG4jX4MZnPLd2dDz/09qdcMw70ryZtFnzBXGsAfft9cdoPbwQGmXYHobxvixeQmduPx8Aq+NeKyKNFt2rwA8rwqht+aPQ0YnPZdmxKMfZ7TerUhqvkSL0oyedF1dRrGZV7wr58E32yi0qoX5jkPABGqkdc8nF9 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: From: "Mike Rapoport (Microsoft)" module_writable_address() is unused and can be removed. Signed-off-by: Mike Rapoport (Microsoft) --- include/linux/module.h | 10 ---------- 1 file changed, 10 deletions(-) diff --git a/include/linux/module.h b/include/linux/module.h index e9fc9d1fa476..222099bb07cf 100644 --- a/include/linux/module.h +++ b/include/linux/module.h @@ -774,11 +774,6 @@ static inline bool is_livepatch_module(struct module *mod) void set_module_sig_enforced(void); -static inline void *module_writable_address(struct module *mod, void *loc) -{ - return loc; -} - #else /* !CONFIG_MODULES... */ static inline struct module *__module_address(unsigned long addr) @@ -886,11 +881,6 @@ static inline bool module_is_coming(struct module *mod) { return false; } - -static inline void *module_writable_address(struct module *mod, void *loc) -{ - return loc; -} #endif /* CONFIG_MODULES */ #ifdef CONFIG_SYSFS