From patchwork Wed Jan 24 17:31:34 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexander Potapenko X-Patchwork-Id: 13529501 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 C98EFC47E49 for ; Wed, 24 Jan 2024 17:31:44 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 60DF98D0006; Wed, 24 Jan 2024 12:31:44 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 5BDFA8D0001; Wed, 24 Jan 2024 12:31:44 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 486388D0006; Wed, 24 Jan 2024 12:31:44 -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 3A36D8D0001 for ; Wed, 24 Jan 2024 12:31:44 -0500 (EST) Received: from smtpin21.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id 09E5040CAD for ; Wed, 24 Jan 2024 17:31:44 +0000 (UTC) X-FDA: 81714896928.21.86937AE Received: from mail-ed1-f74.google.com (mail-ed1-f74.google.com [209.85.208.74]) by imf23.hostedemail.com (Postfix) with ESMTP id 394E814001E for ; Wed, 24 Jan 2024 17:31:42 +0000 (UTC) Authentication-Results: imf23.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=uouW4gcO; spf=pass (imf23.hostedemail.com: domain of 3fEmxZQYKCAkpurmn0pxxpun.lxvurw36-vvt4jlt.x0p@flex--glider.bounces.google.com designates 209.85.208.74 as permitted sender) smtp.mailfrom=3fEmxZQYKCAkpurmn0pxxpun.lxvurw36-vvt4jlt.x0p@flex--glider.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1706117502; 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:in-reply-to: references:dkim-signature; bh=kMQl+PaRJqmy3cHaG1IzDq8AzN3RMVuSbCxHhfx+H/0=; b=Wwx7KeFmwWFMNjpUB2Rm1XRQWxuPcgO1ju0MPXiFNMv9RpobzzLxToSFaXgv2J09cXAOIb MqsuwcHCa862oeobqn2KmDZr/OQFVRlHQg+gOxx5QhTb1FfM5BPTMRFtguwSFkHaxogw5D 9AefJd1tZNm5SnAxWbLF1S4ehbVdQm0= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1706117502; a=rsa-sha256; cv=none; b=Yw3NP9iKReP6xLLeTrlH2OuAt/rnsShQKsfCCk2g1s/g77YlfIPjXV3MXOFET7NbiwPqFG qKx0PjuJCvlZVUnu6jDVDl6rnUfLRt5sX/S+vZXqKiYpdFrr/AR+fTnCjhCmCSZuYsezpT nyE0cit4AovX3yKnhQ6hAbmMtXp1kf8= ARC-Authentication-Results: i=1; imf23.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=uouW4gcO; spf=pass (imf23.hostedemail.com: domain of 3fEmxZQYKCAkpurmn0pxxpun.lxvurw36-vvt4jlt.x0p@flex--glider.bounces.google.com designates 209.85.208.74 as permitted sender) smtp.mailfrom=3fEmxZQYKCAkpurmn0pxxpun.lxvurw36-vvt4jlt.x0p@flex--glider.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com Received: by mail-ed1-f74.google.com with SMTP id 4fb4d7f45d1cf-55a6e18fdb6so2673423a12.1 for ; Wed, 24 Jan 2024 09:31:41 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1706117501; x=1706722301; darn=kvack.org; h=cc:to:from:subject:message-id:mime-version:date:from:to:cc:subject :date:message-id:reply-to; bh=kMQl+PaRJqmy3cHaG1IzDq8AzN3RMVuSbCxHhfx+H/0=; b=uouW4gcOJZ14Ov4wymC8YpzJTsCl4tzdlmZ0D8HAaCw8jBQ18Veomo6lpmsRrsj+8B bcNJpJdQPX9GEhijaKcMf+y082WrDmeorOU++V97LvQdDIrfOEIeUYVFYefxsaxTHwsd oULjCK+lHBJZGAHL3lgj9P3UMPBlOv3UaIXoW2+ME64xMCj6W2brFvGg7ThKhfbJCbnC rhlhCb9aDVi9Y2vauJZW+AFvj3zz7/YQEY8INo5XK4dcEKiNfQwgEniYuLdOJ6jbi/W/ cHuNraYeBAB4Vn6vLOMXgYuqmHTv/M1Nsui4s/rbuiIOLPmjShL2i2WGCbh1RBYU5zp3 a8Gg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1706117501; x=1706722301; h=cc:to:from:subject:message-id:mime-version:date:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=kMQl+PaRJqmy3cHaG1IzDq8AzN3RMVuSbCxHhfx+H/0=; b=YpY1ASdYpwtCLI5zrW2umwW2mcqvLnjv1Kgomrka8w0UEQeBNOTaUM8WEL8tMmXu44 4HM01ByS1zIjdOpYyspwy5y6u+U2Suhw/prV/iy6/K1gjhxWeLfghzj9SEs8M2dEQS6/ +BuAuSJQImM0dOhe2fn/1SQSWfzENL9VgHfhP+EWIcqvuMALNpEtJmnA2ixjHvVvkb2o UmHH6OcChRMWIakVAtobISSfLnsdYxxDw73JPnlMv7m08XzcOfblSEWOUBxbdtn8Lv7R SpJvzZfAVvHZ06updA69iqWbDtGitLegwn3o3hGmWpBV128awj/8sKe62SIIZzALREpK 0xMA== X-Gm-Message-State: AOJu0Yz7fZwP70zmc82EDFK6YNV8PUDGRlOjMVhss4v7vXJZTWPVDKaL DuQs7dYEOXnwthHXk8qp5JWsAfr5BoyVPtMdiDO4VwCR2Yac+fn/1nnswv6dbWv4f4Xjo2+kVgC LZQ== X-Google-Smtp-Source: AGHT+IGlOFACEt43WhKtTpDn0s55boHwwfL2w7oDhlo8EQrE3IXloxlTtf+hDKUXP+HJQ1NueuFOrd0k+3M= X-Received: from glider.muc.corp.google.com ([2a00:79e0:9c:201:ca56:4222:6893:5055]) (user=glider job=sendgmr) by 2002:a05:6402:2491:b0:559:6fa1:bbec with SMTP id q17-20020a056402249100b005596fa1bbecmr26136eda.6.1706117500691; Wed, 24 Jan 2024 09:31:40 -0800 (PST) Date: Wed, 24 Jan 2024 18:31:34 +0100 Mime-Version: 1.0 X-Mailer: git-send-email 2.43.0.429.g432eaa2c6b-goog Message-ID: <20240124173134.1165747-1-glider@google.com> Subject: [PATCH v2] mm: kmsan: remove runtime checks from kmsan_unpoison_memory() From: Alexander Potapenko To: glider@google.com, akpm@linux-foundation.org Cc: linux-kernel@vger.kernel.org, linux-mm@kvack.org, kasan-dev@googlegroups.com, Marco Elver , Dmitry Vyukov , Ilya Leoshkevich , Nicholas Miehlbradt X-Stat-Signature: tyk4ecnbdj758anpgbeautprsuwjkj43 X-Rspamd-Server: rspam10 X-Rspamd-Queue-Id: 394E814001E X-Rspam-User: X-HE-Tag: 1706117502-882914 X-HE-Meta: U2FsdGVkX1/Mx0ownATEo6n9pG58Hnrrc/QfynD+wSCtbkp6jtJJMEfkyI9oK0NVTVIccc+aSmvm9gaqyn6c1k+dydj+XsZfnhmcOV6vYFSNKVjdJYsH4Fzu/7yi4km/zc7HPYQoYQLWgm/ogO2MAPG8PanuIXubNRVABErtN0g/KyZfJ0Pics/UsQ5FN/inESS4lYJKnKCrhMXOYFNExqYMhpJQ+0ukEBpOBFMoPYUnniyMmvPxh+VK2Vy7agDDcnsT7GwNw54Sryuv12QRLpaoj7DQ40Gwd3B0rKx6ZEOME2OfjN0kuctrPDCav1pTsooLRYxSUu8KQRE5oLnKZigdHtROMDGOthSp+AVAp0f7PPNW/tc7YqMGn4HU/U1D/UOBXJVQQFQnh5y5Qm6hV9PkNHNZZdTQ6xE/c3YPjCuIchgH1IqfR9lI0YrY5DKWFTMQSrzlkR8ijZDys55AP2rDUs2GwuMawsMuEpUFqsnZTJZfXAg628UzsxoTkS7ZbcpkaLCrVRVjmexUFTMmlxySqYOWICopgG+72BT8nz7QgPOWMfFxa/Lxkw+78/l0kVEKKeY6H5NV3zGe3PwjBicVSSYRv61uYOU417Dt6DfzU/MP7i2Qc8gRkb9nyKe37Ja7y2P2s3mfS8Pjspai2AHdWZsQaShnj3CQbdgvHINYNjE/47FeiIgkflOfFFJUG1N9JBPgzvhiSbL8X+9FX0w5xIsw8cMXGqygJ93UetQFn5t1nOrZkm14nuiGFdHJ5kO0LGjpoCNkJCub+ykfouI2AqtVmA/D5dq3DYxaoaNgDOkNgOPKqvGg/vLTShHozLr9aY2Ek6VoGxlIWcPXWHL7vKFx59hJqoBqkyqNXnlI4ZLGTRffs0RLRRRKkThchUJCM25sV3vNfgdE06tN5gtBjzBjdGf7cbKIPhf411HsfdyPfe/ULZq2uqQQGVvUUidpVKpvh7+ymwbXYHN LJUkbJUx TqlYXEUTNzUqxWdO8tBZ5xCgfDbDAZfpSDxpa0O0gK00lNxm8ALddYb5b/RZ8+ZgJr8J5pRIAFaKDLfAJ8eSXm4vjQm11I3bulIiCB14OpC394iCS4Q4sXawnt7cpuMtSyO8oApPYIfCIWP+X4t2g36zsgWtScG/U/I8VdArk697bwZaoqm9/2s8PdwDdaYcUYyR1BNYx1bf/LJGcK30fFds+NXDxzp4UoNLykJyjIYtld17pY/Trn1VlfVpDYvFwQb+46vjyfxW/UegKCsvdo4fSOwSZkCjLWEk/EIe/1SggE//6mTe+/Nsm5YPf2+9VOgotKzw4Rdl57m8uW63QBFMPDNT9/MomBjlOnfmBgMj7jTJyOI3Yu5fUnWB3r9VvSBsa3ejx68c6cWU= X-Bogosity: Ham, tests=bogofilter, spamicity=0.000003, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: Similarly to what's been done in commit ff444efbbb9be ("kmsan: allow using __msan_instrument_asm_store() inside runtime"), it should be safe to call kmsan_unpoison_memory() from within the runtime, as it does not allocate memory or take locks. Remove the redundant runtime checks. This should fix false positives seen with CONFIG_DEBUG_LIST=y when the non-instrumented lib/stackdepot.c failed to unpoison the memory chunks later checked by the instrumented lib/list_debug.c Also replace the implementation of kmsan_unpoison_entry_regs() with a call to kmsan_unpoison_memory(). Signed-off-by: Alexander Potapenko Tested-by: Marco Elver Cc: Dmitry Vyukov Cc: Ilya Leoshkevich Cc: Nicholas Miehlbradt --- mm/kmsan/hooks.c | 36 +++++++++++++----------------------- 1 file changed, 13 insertions(+), 23 deletions(-) diff --git a/mm/kmsan/hooks.c b/mm/kmsan/hooks.c index 5d6e2dee5692a..0b09daa188ef6 100644 --- a/mm/kmsan/hooks.c +++ b/mm/kmsan/hooks.c @@ -359,6 +359,12 @@ void kmsan_handle_dma_sg(struct scatterlist *sg, int nents, } /* Functions from kmsan-checks.h follow. */ + +/* + * To create an origin, kmsan_poison_memory() unwinds the stacks and stores it + * into the stack depot. This may cause deadlocks if done from within KMSAN + * runtime, therefore we bail out if kmsan_in_runtime(). + */ void kmsan_poison_memory(const void *address, size_t size, gfp_t flags) { if (!kmsan_enabled || kmsan_in_runtime()) @@ -371,47 +377,31 @@ void kmsan_poison_memory(const void *address, size_t size, gfp_t flags) } EXPORT_SYMBOL(kmsan_poison_memory); +/* + * Unlike kmsan_poison_memory(), this function can be used from within KMSAN + * runtime, because it does not trigger allocations or call instrumented code. + */ void kmsan_unpoison_memory(const void *address, size_t size) { unsigned long ua_flags; - if (!kmsan_enabled || kmsan_in_runtime()) + if (!kmsan_enabled) return; ua_flags = user_access_save(); - kmsan_enter_runtime(); /* The users may want to poison/unpoison random memory. */ kmsan_internal_unpoison_memory((void *)address, size, KMSAN_POISON_NOCHECK); - kmsan_leave_runtime(); user_access_restore(ua_flags); } EXPORT_SYMBOL(kmsan_unpoison_memory); /* - * Version of kmsan_unpoison_memory() that can be called from within the KMSAN - * runtime. - * - * Non-instrumented IRQ entry functions receive struct pt_regs from assembly - * code. Those regs need to be unpoisoned, otherwise using them will result in - * false positives. - * Using kmsan_unpoison_memory() is not an option in entry code, because the - * return value of in_task() is inconsistent - as a result, certain calls to - * kmsan_unpoison_memory() are ignored. kmsan_unpoison_entry_regs() ensures that - * the registers are unpoisoned even if kmsan_in_runtime() is true in the early - * entry code. + * Version of kmsan_unpoison_memory() called from IRQ entry functions. */ void kmsan_unpoison_entry_regs(const struct pt_regs *regs) { - unsigned long ua_flags; - - if (!kmsan_enabled) - return; - - ua_flags = user_access_save(); - kmsan_internal_unpoison_memory((void *)regs, sizeof(*regs), - KMSAN_POISON_NOCHECK); - user_access_restore(ua_flags); + kmsan_unpoison_memory((void *)regs, sizeof(*regs)); } void kmsan_check_memory(const void *addr, size_t size)