From patchwork Thu May 23 21:50:29 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Brian Johannesmeyer X-Patchwork-Id: 13672334 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 BB88CC25B75 for ; Thu, 23 May 2024 21:50:55 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 9DFEB6B0083; Thu, 23 May 2024 17:50:54 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 98F746B0085; Thu, 23 May 2024 17:50:54 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 87DB16B0088; Thu, 23 May 2024 17:50:54 -0400 (EDT) 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 6B02D6B0083 for ; Thu, 23 May 2024 17:50:54 -0400 (EDT) Received: from smtpin20.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id 9A14540575 for ; Thu, 23 May 2024 21:50:53 +0000 (UTC) X-FDA: 82151005986.20.925CA6D Received: from mail-ej1-f54.google.com (mail-ej1-f54.google.com [209.85.218.54]) by imf24.hostedemail.com (Postfix) with ESMTP id D7CE2180012 for ; Thu, 23 May 2024 21:50:51 +0000 (UTC) Authentication-Results: imf24.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=aV2DO5DR; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf24.hostedemail.com: domain of bjohannesmeyer@gmail.com designates 209.85.218.54 as permitted sender) smtp.mailfrom=bjohannesmeyer@gmail.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1716501051; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:mime-version:mime-version:content-type: content-transfer-encoding:content-transfer-encoding:in-reply-to: references:dkim-signature; bh=cL3CjB9yMkr+C9cTAGf3ev6sBpUayp6G2aVmnb0yyJM=; b=J9NOsS3s07aTWpm9sx7oHaiymAufO/+Zm6OjYzGbBeFOKzEiWEfAFagnP/MwZjojv+tH+U GIZ2/AoozV6qMh5NFTljQ3SSI1F6xlaXQQ3Un21ACeUEwLPINvtPi7cGc8N9NWkYmm+U8M HnBAne0Knxf6EZ55iFEW6+KbsHLRKQU= ARC-Authentication-Results: i=1; imf24.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=aV2DO5DR; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf24.hostedemail.com: domain of bjohannesmeyer@gmail.com designates 209.85.218.54 as permitted sender) smtp.mailfrom=bjohannesmeyer@gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1716501051; a=rsa-sha256; cv=none; b=HS33QEqi0dotnr3o5qse0MnjzE+CoDxGzhe2rPrTw+kGO6IzikT5QsvjBtwSmoD3wenj0U W41zlSNpWleBk0RNjNDo4rHIUa6n58eU6ZIL265Nxc91X6DQZGQx4cvc909H59LGRk2KK5 UCfGLZ+vLOc8WuSJ+EEWuhsBKSHRfh8= Received: by mail-ej1-f54.google.com with SMTP id a640c23a62f3a-a5a7d28555bso1238154166b.1 for ; Thu, 23 May 2024 14:50:51 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1716501050; x=1717105850; darn=kvack.org; h=content-transfer-encoding:mime-version:message-id:date:subject:to :from:from:to:cc:subject:date:message-id:reply-to; bh=cL3CjB9yMkr+C9cTAGf3ev6sBpUayp6G2aVmnb0yyJM=; b=aV2DO5DRNzbRr4DWgxIPQpgqWnKx6buSI+T6VMpF0ytR4csdsYbEpaJZTp6ybIHgIQ Sza0f1nl7/fLr6+UwTEnLGtT2iIyHAEaLJ+FsdfNV0BZm7Al2G6rv2XD0w59fKxNtH4R sS9326M4PqN7YH0ZfMGGgyIJI7PK27b4tD5brG3FDSvHOQl99UBXfqwKxc8uCK0BYnQZ Qo0h2R6JazjJJvP0o48DpvRuVBYf9tD0Wtc3s4HgmWBuHkN7wICCxYXcPZlgHYCRTxME AsRuRFgqfnKXxF8idyJYQboB9e0lFMJVRtxLrcUwNTEVPoSMUhhA0nVL8AJYBXgtKf/4 pcPA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1716501050; x=1717105850; h=content-transfer-encoding:mime-version:message-id:date:subject:to :from:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=cL3CjB9yMkr+C9cTAGf3ev6sBpUayp6G2aVmnb0yyJM=; b=YLcDukG/us50FOlaYLVlhaJHFgmNEV0cVAuQ3lROx9Cuxj6bx9PiQZ+fe9tqmd0Cd8 +YeQqrxiyDji0d9uPENro8NS/PniC3/Mk8YWzVL8mNv3qgJOhPauJ3OgKsTE/9+aS2Vj D3ehFPkKmsbpP9azkxd5dzvx/lnGzG0j0S5K7/ctK6vRQijWcX1Dy4LJfWLT0L0XijZw xpwJl53FkkrJT8EBh4l6TaBhzEPg6xAEJNlTdqEV3AnY86NbnR3nJSRwEQiBj996UCHA MZFT/vawmHEpFzyuu0h5hTetIJQSyclOIgXCVxOQpmQLDFRz26T5aVsXcmQa0LhecH5g jCaw== X-Forwarded-Encrypted: i=1; AJvYcCVZxKsRZzBMyHHYndnfOV7Lrxc+FqodwCIlNlZ1ki79qFs6jvXf9tM3Y7bJUW61M4UzYJTc/DPPW2DLGqD9VYwSdbk= X-Gm-Message-State: AOJu0YwtrrtqBOGF2c1oAdGsnBfXusRTleJTOutpxb96a5autVhpvIlf Obc+k6+kRAQHxCwgTgI+gQG14lL0Dbm6Uu/C2ArMroubOctdKGma X-Google-Smtp-Source: AGHT+IEC29YV+fMUmucu9I5TgwKcefAI5pI1xIvRh1BI4PbX9ZTy6D9SemY9LzkPKBy+JescYy+cvQ== X-Received: by 2002:a17:906:a252:b0:a58:eba0:6716 with SMTP id a640c23a62f3a-a6265128466mr23580566b.60.1716501049956; Thu, 23 May 2024 14:50:49 -0700 (PDT) Received: from rex.hwlab.vusec.net (lab-4.lab.cs.vu.nl. [192.33.36.4]) by smtp.gmail.com with ESMTPSA id a640c23a62f3a-a626cda9248sm14971066b.225.2024.05.23.14.50.48 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 23 May 2024 14:50:49 -0700 (PDT) From: Brian Johannesmeyer To: Brian Johannesmeyer , Alexander Potapenko , Marco Elver , Dmitry Vyukov , Andrew Morton , kasan-dev@googlegroups.com, linux-mm@kvack.org, linux-kernel@vger.kernel.org, Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , x86@kernel.org, "H . Peter Anvin" Subject: [PATCH] x86: kmsan: Fix hook for unaligned accesses Date: Thu, 23 May 2024 23:50:29 +0200 Message-Id: <20240523215029.4160518-1-bjohannesmeyer@gmail.com> X-Mailer: git-send-email 2.34.1 MIME-Version: 1.0 X-Rspamd-Queue-Id: D7CE2180012 X-Stat-Signature: wejrnuu1i88nh7mwge1ee9psfbcmd4ug X-Rspam-User: X-Rspamd-Server: rspam08 X-HE-Tag: 1716501051-477968 X-HE-Meta: U2FsdGVkX1/A0AdtLsWhb1ErEfEl0sAKCoiJrphVuZK0pR5QS/n3Hbn/YeNuultxhNmLkDRwB4nVid+VaZ2GerKNJFaisJ17yIcDF0rwBCE3k5QlD1SgNYLsCschFdVJ2ISqELUJJimxl5Vr29kCtfOx/QWDVbZ/RNhC/LGPrTXf1hVPVTYdiJWxXsr4VDA0/1fr++TOc89WQymi/6O6VPE5m0UaSO45Z3Il025LZhQ3zrpXMk0Vjd7BP82NQu14bP/HzrLV1f+DxUDKIFumJVySTG03tMi9SmB6pmfjUnyDyh1CsL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QQ5FwVAxXy5M5TfIds7r/s0SKsnPzSw3PdS74uwNEFW2 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: When called with a 'from' that is not 4-byte-aligned, string_memcpy_fromio() calls the movs() macro to copy the first few bytes, so that 'from' becomes 4-byte-aligned before calling rep_movs(). This movs() macro modifies 'to', and the subsequent line modifies 'n'. As a result, on unaligned accesses, kmsan_unpoison_memory() uses the updated (aligned) values of 'to' and 'n'. Hence, it does not unpoison the entire region. This patch saves the original values of 'to' and 'n', and passes those to kmsan_unpoison_memory(), so that the entire region is unpoisoned. Signed-off-by: Brian Johannesmeyer Reviewed-by: Alexander Potapenko --- arch/x86/lib/iomem.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/arch/x86/lib/iomem.c b/arch/x86/lib/iomem.c index e0411a3774d4..5eecb45d05d5 100644 --- a/arch/x86/lib/iomem.c +++ b/arch/x86/lib/iomem.c @@ -25,6 +25,9 @@ static __always_inline void rep_movs(void *to, const void *from, size_t n) static void string_memcpy_fromio(void *to, const volatile void __iomem *from, size_t n) { + const void *orig_to = to; + const size_t orig_n = n; + if (unlikely(!n)) return; @@ -39,7 +42,7 @@ static void string_memcpy_fromio(void *to, const volatile void __iomem *from, si } rep_movs(to, (const void *)from, n); /* KMSAN must treat values read from devices as initialized. */ - kmsan_unpoison_memory(to, n); + kmsan_unpoison_memory(orig_to, orig_n); } static void string_memcpy_toio(volatile void __iomem *to, const void *from, size_t n)