From patchwork Fri Feb 23 23:54:16 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tetsuo Handa X-Patchwork-Id: 13570151 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 950EBC5478C for ; Fri, 23 Feb 2024 23:54:53 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 177A36B0075; Fri, 23 Feb 2024 18:54:53 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 127F06B007B; Fri, 23 Feb 2024 18:54:53 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 015FB6B007D; Fri, 23 Feb 2024 18:54:52 -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 E6ED66B0075 for ; Fri, 23 Feb 2024 18:54:52 -0500 (EST) Received: from smtpin13.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id 408201A0150 for ; Fri, 23 Feb 2024 23:54:52 +0000 (UTC) X-FDA: 81824726424.13.0A8D0AA Received: from www262.sakura.ne.jp (www262.sakura.ne.jp [202.181.97.72]) by imf12.hostedemail.com (Postfix) with ESMTP id 70FB840004 for ; Fri, 23 Feb 2024 23:54:49 +0000 (UTC) Authentication-Results: imf12.hostedemail.com; dkim=none; dmarc=none; spf=pass (imf12.hostedemail.com: domain of penguin-kernel@I-love.SAKURA.ne.jp designates 202.181.97.72 as permitted sender) smtp.mailfrom=penguin-kernel@I-love.SAKURA.ne.jp ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1708732490; 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-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=XkDjNvtENO62VhSLqN5+leigVsfoPMITLL9pxVXC9Uk=; b=B03ffUH9s1CWRAfNEYLBSjvm1Q7HDyZ5DHx0LeY6yIBHe+B5oxcDmX7HOJ57Dj2/Ymxon9 7bEyjirphlFrRKOncmbJ0z16uvl5vmzLif4Orij609w50/8VnzH23+hOMM1D3u6xn+mzEZ bY0FxBBU1hRE6fcNyQsiTwvQLgqcz58= ARC-Authentication-Results: i=1; imf12.hostedemail.com; dkim=none; dmarc=none; spf=pass (imf12.hostedemail.com: domain of penguin-kernel@I-love.SAKURA.ne.jp designates 202.181.97.72 as permitted sender) smtp.mailfrom=penguin-kernel@I-love.SAKURA.ne.jp ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1708732490; a=rsa-sha256; cv=none; b=7IuYpS2ct1/Fr3MWHpgn2azuyoQNsdBXGo2i0B8E8KXWy3C0zJlQtkP/kishNV6NOdwZlw 6bvzYjrDtVPawtLGtBwh2y66QdP83R699yQcJsIaMlx0o0WJkQRqRFAwXyg8CFf1MGCSTH /OYvqhV/di2u12evD5t8Vbu/uAlq4x4= Received: from fsav113.sakura.ne.jp (fsav113.sakura.ne.jp [27.133.134.240]) by www262.sakura.ne.jp (8.15.2/8.15.2) with ESMTP id 41NNsIeZ081563; Sat, 24 Feb 2024 08:54:18 +0900 (JST) (envelope-from penguin-kernel@I-love.SAKURA.ne.jp) Received: from www262.sakura.ne.jp (202.181.97.72) by fsav113.sakura.ne.jp (F-Secure/fsigk_smtp/550/fsav113.sakura.ne.jp); Sat, 24 Feb 2024 08:54:18 +0900 (JST) X-Virus-Status: clean(F-Secure/fsigk_smtp/550/fsav113.sakura.ne.jp) Received: from [192.168.1.6] (M106072142033.v4.enabler.ne.jp [106.72.142.33]) (authenticated bits=0) by www262.sakura.ne.jp (8.15.2/8.15.2) with ESMTPSA id 41NNsH57081559 (version=TLSv1.2 cipher=AES256-GCM-SHA384 bits=256 verify=NO); Sat, 24 Feb 2024 08:54:17 +0900 (JST) (envelope-from penguin-kernel@I-love.SAKURA.ne.jp) Message-ID: <678f31f8-5890-47fa-972e-df966aeb783d@I-love.SAKURA.ne.jp> Date: Sat, 24 Feb 2024 08:54:16 +0900 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: [PATCH] x86: disable non-instrumented version of copy_page when KMSAN is enabled Content-Language: en-US From: Tetsuo Handa To: Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , the arch/x86 maintainers , "H. Peter Anvin" Cc: Johannes Weiner , Yosry Ahmed , Nhat Pham , Minchan Kim , linux-mm , kasan-dev , Mark-PK Tsai , Sergey Senozhatsky , Alexander Potapenko References: <20240223044356.GJ11472@google.com> <6dd78966-1459-465d-a80a-39b17ecc38a6@I-love.SAKURA.ne.jp> In-Reply-To: <6dd78966-1459-465d-a80a-39b17ecc38a6@I-love.SAKURA.ne.jp> X-Rspamd-Queue-Id: 70FB840004 X-Rspam-User: X-Rspamd-Server: rspam02 X-Stat-Signature: predw515uzr9efk95qahdpwg9g4kgcxq X-HE-Tag: 1708732489-99887 X-HE-Meta: U2FsdGVkX1+GECm/DNi4wr9YjB/NFWD3tYBvOZxtFHFGD54o3zbvcvL1hfuisGlvQudyezVse15YdhK85trbiZM+/QfHvlzGI/jNUc6Qwo3kOwn/TxHLezDjIioIy1qOWNUFT+nePbWSJgAOk1BkPdtCm4u7xjXWlDhex4BdrGPYT6H61dXPqbtryt+CBbjT82N3fQTY2r6cQsY9qJoAPbu/0iiJuJH9NZVDBuQlmUNi3nvq/2/g6IdPJK/pXVtRdgz4+T8j2FkDC77sHtfsEO8V7jnALqP5pBkasHDJtLnPKVMomHxNH/HsjslfjkqUxa0ryjNW/pygqhiNehf2TK5WITU1l/heF/bJY0lAT4DbshB+cTbFdS6JEFb2VpNITOg+bskicwXbW59Sr2RTesTpEf27stWmdHfM6b4gKquf0okSlkWuQj8j1X+Wimpe+Q38gQKlI7klzOjpqY5i/ivIU4G4QcB2d5A8dQjuEt1tSMt3gRyA600eM/68LVKXfPIvUveKg871DU9qAozEl+JgVloTA2bCOkiGcHARrYf6wMQDRCljNil/nPgOYAd+6jFs6oreMa4Ot2K4Jci027jLFO0B+a6uu2srxXZEnWIODMn4YE/SKWHH+OjY3mbbKImwAV7um+awJPgDP2YI+8/bakQjz7KMs3tvmUgbpSuQP6HLeVkxKug3kkHbRnax80utrXMILFrLWHAfCdcOu6Fm7h2qQk4O63dTnVY0cX5MJR3LUSkKAdF6tzb2s9zu7AAAIKRyxbdc8pS+bNtj8mx/5Liroa4ah4MXqPDhSjS6v6ByG/R6nL9EtF6kzN0cwx37ne8hMbK1lYheQFNUE9gQSzHN0or0rIr0nMGurBNIQGGND8LywoK//c/uQ51GYg+OR620tUY0PnZPwbXv9k040XzvJzi5tTzfg1E1mrgtW9PzK39o2VBHmxLG8Q2HIWldl6wepp9Tf5dvNy1 LiZ8ADWP g5fr0ThDcQ2It2VIRIioBvHnXOCnRbhLNIPc6xU6lRmc2f3ZlRM5CoGTpVhlzw+FBpU8FE5bLFrD5AfzgXFk6gMfaGyFQ/kDVdx4aMqb0lL5yqgeTJU5hOznxt8dmMJU1pjLiVAAdBoUhPY8TsfW+ge3HR/bb/4x8roD44kA07I+iwrxAOy9nEkflkupbB2InNv4RT9hn1MVqfvObGAeyBQY5pK6fd6ji7Nby6EFRBLLtQ/ya/E1JgQf9XCqdZkserDcrHdbYV9Nxs+kLwYBrtHy1Umvndkk4MT9YwsucAHuS2+U= 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: I found that commit afb2d666d025 ("zsmalloc: use copy_page for full page copy") caused KMSAN warning. We need to fallback to instrumented version when KMSAN is enabled. [ 50.030627][ T2974] BUG: KMSAN: use-after-free in obj_malloc+0x6cc/0x7b0 [ 50.165956][ T2974] Uninit was stored to memory at: [ 50.170819][ T2974] obj_malloc+0x70a/0x7b0 [ 50.328931][ T2974] Uninit was created at: [ 50.341845][ T2974] free_unref_page_prepare+0x130/0xfc0 Since the destination page likely already holds previously written value (i.e. KMSAN considers that the page was already initialized), whether to globally enforce an instrumented version when KMSAN is enabled might be questionable. But since finding why KMSAN considers that value is not initialized is difficult (developers tend to choose optimized version without knowing KMSAN), let's choose human-friendly version. That is, since arch/x86/include/asm/page_32.h implements copy_page() using memcpy(), let arch/x86/include/asm/page_64.h implement copy_page() using memcpy() when KMSAN is enabled. Signed-off-by: Tetsuo Handa --- arch/x86/include/asm/page_64.h | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/arch/x86/include/asm/page_64.h b/arch/x86/include/asm/page_64.h index cc6b8e087192..f13bba3a9dab 100644 --- a/arch/x86/include/asm/page_64.h +++ b/arch/x86/include/asm/page_64.h @@ -58,7 +58,16 @@ static inline void clear_page(void *page) : "cc", "memory", "rax", "rcx"); } +#ifdef CONFIG_KMSAN +/* Use of non-instrumented assembly version confuses KMSAN. */ +void *memcpy(void *to, const void *from, __kernel_size_t len); +static inline void copy_page(void *to, void *from) +{ + memcpy(to, from, PAGE_SIZE); +} +#else void copy_page(void *to, void *from); +#endif #ifdef CONFIG_X86_5LEVEL /*