From patchwork Tue Jul 23 14:47:50 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrew Zaborowski X-Patchwork-Id: 13740152 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 B1051C3DA63 for ; Tue, 23 Jul 2024 14:48:06 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 26E816B0099; Tue, 23 Jul 2024 10:48:06 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 21E836B009A; Tue, 23 Jul 2024 10:48:06 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 10C656B009C; Tue, 23 Jul 2024 10:48:06 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id E1D6B6B0099 for ; Tue, 23 Jul 2024 10:48:05 -0400 (EDT) Received: from smtpin11.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id 4E0D91A018D for ; Tue, 23 Jul 2024 14:48:05 +0000 (UTC) X-FDA: 82371297330.11.0C75398 Received: from mail-lf1-f41.google.com (mail-lf1-f41.google.com [209.85.167.41]) by imf13.hostedemail.com (Postfix) with ESMTP id 748292000E for ; Tue, 23 Jul 2024 14:48:03 +0000 (UTC) Authentication-Results: imf13.hostedemail.com; dkim=none; spf=pass (imf13.hostedemail.com: domain of balrogg@gmail.com designates 209.85.167.41 as permitted sender) smtp.mailfrom=balrogg@gmail.com; dmarc=fail reason="SPF not aligned (relaxed), No valid DKIM" header.from=intel.com (policy=none) ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1721746021; 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:references; bh=jiq/XSgm3wHfgQ0AwWBkVwnpN/AUkG0btoxuVvCcj1g=; b=mD44dMMePpVHajncm30qxn7CnOSTa3Glvl6KZzQ1Mg7w6ah4puAFhxUO3NQ0+QVoxa4QRK s8m9rUumYCsnh4sblIP3CDWZUpBBB7v6hDt/RqiIMN8D665uLLEmPIwRLVbcKujQUFdHiY e2iWNtTS/wvKLRZyscRV3PTZj4aZu68= ARC-Authentication-Results: i=1; imf13.hostedemail.com; dkim=none; spf=pass (imf13.hostedemail.com: domain of balrogg@gmail.com designates 209.85.167.41 as permitted sender) smtp.mailfrom=balrogg@gmail.com; dmarc=fail reason="SPF not aligned (relaxed), No valid DKIM" header.from=intel.com (policy=none) ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1721746021; a=rsa-sha256; cv=none; b=oEJ3xcLzBRXbKxywEnv8FRki6G5iBsMFXLjHZDAByLA8IdQ2LVz9/s4ITaxeMw8rVrVzTm Z5e2Xwp3kfja94t90HXAmAUO6NCd0XudOgTz0/YQ9oy4aXucXYXdouT6kIGy+GBX52RD5Q AA0ZLw+/kXHlaDabaegekE1EbJ5Utio= Received: by mail-lf1-f41.google.com with SMTP id 2adb3069b0e04-52f01afa11cso3812008e87.0 for ; Tue, 23 Jul 2024 07:48:03 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1721746082; x=1722350882; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=jiq/XSgm3wHfgQ0AwWBkVwnpN/AUkG0btoxuVvCcj1g=; b=hli/sLB3u6bJTSJv9rLaXc5/ni8Wgdjlo2QPBMRlAeyD5TYF6oxfouDgpdf2Ty4huy Lt9I5DLvGK49gkVQLEo96mF21+oa0ft9+Sl1w/1xJb69N44Pjf1CKdB0d6Szh1u35kbc Y6Gkho1KpHbwOyQLKCIsGDteHjeEKZbtsNoz19vuHrDsudizeJLiD5c+eVgYMLpeCs/7 SgRMpd66IeAajBqTEDwN7SiMIaVbl2eDO4jPtBV5Pb3YEqEM9TSXsJsumpH+k1xa983m 7Tfkhl/ZbJ2Xo9eRAUK7Esn0NLFUvn/18n8mskGJ+ERGE9ChNfYyqMS9M1si813KdAOl TU9A== X-Forwarded-Encrypted: i=1; AJvYcCWjJ/h1eAiWzV1xmxYsvN+edjfGfo/PTHEU8f0LYYK30nDtKxCapyKy0rqZVf+npN6TlV+UOhrajdyA6qEh4YlckSY= X-Gm-Message-State: AOJu0Yy2CXW5tva+AxYS97xYSEhVVMmaOV1vd3R8vbYrnBArfzqQmcIH Reu0CExVQV3wLedfafjt10RDWGkLnZsmQ00nRDuTtPPXvgiSkZcc X-Google-Smtp-Source: AGHT+IHCQf5Q3yQ7wuWmrukqnqvyvVU47RfROrNLyLGWiYU8odqK5t3MOgLCXUBBhnWdnvC2PwqZCw== X-Received: by 2002:a05:6512:132a:b0:52c:825e:3b1c with SMTP id 2adb3069b0e04-52fc404b78fmr2191607e87.26.1721746081489; Tue, 23 Jul 2024 07:48:01 -0700 (PDT) Received: from localhost.localdomain ([2a01:110f:4a11:8500:e7a:15ff:fe95:b9d8]) by smtp.gmail.com with ESMTPSA id 4fb4d7f45d1cf-5a30c7d36f3sm7555071a12.91.2024.07.23.07.48.00 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 23 Jul 2024 07:48:00 -0700 (PDT) From: Andrew Zaborowski To: linux-edac@vger.kernel.org, linux-mm@kvack.org Cc: Kees Cook , Tony Luck , Eric Biederman , Borislav Petkov Subject: [RESEND][PATCH 1/3] x86: Add task_struct flag to force SIGBUS on MCE Date: Tue, 23 Jul 2024 16:47:50 +0200 Message-ID: <20240723144752.1478226-1-andrew.zaborowski@intel.com> X-Mailer: git-send-email 2.43.0 MIME-Version: 1.0 X-Rspamd-Server: rspam06 X-Rspamd-Queue-Id: 748292000E X-Stat-Signature: gp9hm8ps3s87wyjw7ic55g7gyurrnez7 X-Rspam-User: X-HE-Tag: 1721746083-195271 X-HE-Meta: U2FsdGVkX1+9Xdq9d5o+Bt0hwMMSh6DOw/yKT58OPv3+zcMH8SxBEztflFLD/YpOEQCUPwmVlH6XaVHjKa5uKRef1CZlzKTUk1sfzX5eQEI1vkvja/Ln5U2QfPeCSCnaPDc4eN/EjpeIi+aY7x9tK8NZJt9Q3z7Nad2QL++qnHJu6b1Aw2N27U2qFgoHJrquL10fpVkdbPtJ2RsoTcFl6dkD51H7VdDeNHKGdzFlouVAj8wYLdJYr752UiEj9U44PxiijqPmD4XbFpLfh02qbsO8RQn0E8sbHm8DMJ4um75YTcTRHhir4TK8vQuGWjz7lD77RtydFjmWkhphMVaiAX0xFzqAwNQ20vZwDNB1p4oa9wc3eu2Oc2gxAXgaTSAGAjf7aU6NYmRZTYMSIX/rH7aDGa8fZQXCHWWDkHiNYuT5MLemC9pdda1RokUfmOc6avgMQna0hmn/ZKr0QHhguo72UKifxc1K5wR/TmWqUFdaUtXtotMZj+/KnIsNyuxqduaJJWE3KQQog6fMdRtiX5SjQ96mdCWs6jQUymPHYuZslhMBUpjOjncv6RkBT734c4ibjmqP2+aYsSIVeNDPYkXbAiiQGLbK3rHyIDYDmW6jjfcK1vCIT04A1+m8iCb/NYqu3rWqZFwQmFPqkQmslzBo+HCG57u2Jd5VsKgR3q39xrq7C64TiXKofre7Lm0/CvxKHzv30RMe2X0YG7wyu1H6fZMvVzcjj5iLySIPO3WP08qdf70M8yexjg67+jLGcGfpCUDTJAaQskvPXN9C+EOBNSG2kmKhRDI7GaLbnC1Ryu8FrhMn/olkhgNaXCmoqJd+QeLLm0m3/3VLH6idMQMsBNu1BdgzYnb7nno4CGj8H3oS8sb+fVHOhpIqaDofTkbZKYtSq40swS12V/922XaFCWQ2WX0MUmfu5l+/chEfsxDlkwfDHfC7D5j1PBB4MzCup2UUyHJiJqo3VTx JlDNJrkg JRn/gETrj+VxxAg9+05tB/qxFUf0JO/lQ77DQrw/eIyKAqPSxnhpOX27O3029WtCmIYqDF1SQzyiYl+IxaxCEgZiR85HAAmIBWh9CJfOqCVcI3BysQ70mbbuH8AwFjpLx/YBQSD0jmq0oI8jxpTZPxVyIYaTcD2sUmuhvsU19zskYDSrgpb6aoJm8vcF3tOq7rvDsqCY9xYgcq0uLeK8wk1IW3F05tRu83Ig12mxKzXW7pmdLYmyR5OVPWORlZ83/6NQzu7ozsxLgOcktD2OzRGJNbESFudqHFvYzu1yXzSaljViAqYjGyks9SIuMl9C693c1D9dMMp5Pk/MFf9XfSwI00F+HAbGL2JvISkvIPNoIFOfXY2/Es5c3il1fvHMTyDePEdbvyXlSbKChLxxgtAHjoG9uJMWykHicVRGFQda5rIfHkT+kw3d0NF2kROqxuGz4K2Jg0o79wfrwJcHoZQxCcA0cRus8T7q21MIt3puwKLP8e6qfKYGitb0FOnAMTLMWrVHergCsx/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: Uncorrected memory errors for user pages are signaled to processes using SIGBUS or, if the error happens in a syscall, an error retval from the syscall. The SIGBUS is documented in Documentation/mm/hwpoison.rst#failure-recovery-modes But there are corner cases where we cannot or don't want to return a plain error from the syscall. Subsequent commits covers two such cases: execve and rseq. Current code, in both places, will kill the task with a SIGSEGV on error. While not explicitly stated, it can be argued that it should be a SIGBUS, for consistency and for the benefit of the userspace signal handlers. Even if the process cannot handle the signal, perhaps the parent process can. This was the case in the scenario that motivated this patch. In both cases, the architecture's exception handler (MCE handler on x86) will queue a call to memory_failure. This doesn't work because the syscall-specific code sees the -EFAULT and terminates the task before the queued work runs. To fix this: 1. let pending work run in the error cases in both places. And 2. on MCE, ensure memory_failure() is passed MF_ACTION_REQUIRED so that the SIGBUS is queued. Normally when the MCE is in a syscall, a fixup of return IP and a call to kill_me_never() are what we want. But in this case it's necessary to queue kill_me_maybe() which will set MF_ACTION_REQUIRED which is checked by memory_failure(). To do this the syscall code will set current->kill_on_efault, a new task_struct flag. Check that flag in arch/x86/kernel/cpu/mce/core.c:do_machine_check() Note: the flag is not x86 specific even if only x86 handling is being added here. The definition could be guarded by #ifdef CONFIG_MEMORY_FAILURE, but it would then need set/clear utilities. Signed-off-by: Andrew Zaborowski --- Resending through an SMTP server that won't add the company footer. This is a v2 of https://lore.kernel.org/linux-mm/20240501015340.3014724-1-andrew.zaborowski@intel.com/ In the v1 the existing flag current->in_execve was being reused instead of adding a new one. Kees Cook commented in https://lore.kernel.org/linux-mm/202405010915.465AF19@keescook/ that current->in_execve is going away. Lacking a better idea and seeing that execve() and rseq() would benefit from using a common mechanism, I decided to add this new flag. Perhaps with a better name current->kill_on_efault could replace brpm->point_of_no_return to offset the pain of having this extra flag. --- arch/x86/kernel/cpu/mce/core.c | 18 +++++++++++++++++- include/linux/sched.h | 2 ++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/arch/x86/kernel/cpu/mce/core.c b/arch/x86/kernel/cpu/mce/core.c index ad0623b65..13f2ace3d 100644 --- a/arch/x86/kernel/cpu/mce/core.c +++ b/arch/x86/kernel/cpu/mce/core.c @@ -1611,7 +1611,7 @@ noinstr void do_machine_check(struct pt_regs *regs) if (p) SetPageHWPoison(p); } - } else { + } else if (!current->kill_on_efault) { /* * Handle an MCE which has happened in kernel space but from * which the kernel can recover: ex_has_fault_handler() has @@ -1628,6 +1628,22 @@ noinstr void do_machine_check(struct pt_regs *regs) if (m.kflags & MCE_IN_KERNEL_COPYIN) queue_task_work(&m, msg, kill_me_never); + } else { + /* + * Even with recovery code extra handling is required when + * we're not returning to userspace after error (e.g. in + * execve() beyond the point of no return) to ensure that + * a SIGBUS is delivered. + */ + if (m.kflags & MCE_IN_KERNEL_RECOV) { + if (!fixup_exception(regs, X86_TRAP_MC, 0, 0)) + mce_panic("Failed kernel mode recovery", &m, msg); + } + + if (!mce_usable_address(&m)) + queue_task_work(&m, msg, kill_me_now); + else + queue_task_work(&m, msg, kill_me_maybe); } out: diff --git a/include/linux/sched.h b/include/linux/sched.h index 61591ac6e..0cde1ba11 100644 --- a/include/linux/sched.h +++ b/include/linux/sched.h @@ -975,6 +975,8 @@ struct task_struct { /* delay due to memory thrashing */ unsigned in_thrashing:1; #endif + /* Kill task on user memory access error */ + unsigned kill_on_efault:1; unsigned long atomic_flags; /* Flags requiring atomic access. */