From nobody Mon Apr 6 09:12:33 2026 Received: from mail-pj1-f74.google.com (mail-pj1-f74.google.com [209.85.216.74]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id C7FA24035DF for ; Thu, 19 Mar 2026 23:30:56 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.74 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1773963057; cv=none; b=NRpOf1zWKif7vt/5/esKqYMhIJKWKGehMhZjr4ofa1vlauY4QrBH1snEac3DgAnmWP1AtUm9c3ZbbJyNsJn2Pdui8ZimLHRH8eAqUVOwRceoVvgi5wbw6gCK5fpSAuedy/H6Ab5X6cdZCLJD4a8XpP5mosVEKvQM0myEsLQ/DD8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1773963057; c=relaxed/simple; bh=wOqfNAc1NvD9OIIK2IFuT9hSXcRKmOriHgtHRjPE5Bg=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=t69UGEAbA3iXT+li/hwIv91B3886ppESaGSxYZEHxKSUcWhUDRaf4J0PrEPWpGyTUDOnoKZlfCZ6t8/DUIGHqyRGn0oZbjIjoLgL4N7CJhc0c8rWaEEaLax3B7YQpOIRUjn5LxyGs4/WXonQ9Cmso2ba1ir+oTIGEC68zk9RnYo= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--wyihan.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=HGaV+NDi; arc=none smtp.client-ip=209.85.216.74 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--wyihan.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="HGaV+NDi" Received: by mail-pj1-f74.google.com with SMTP id 98e67ed59e1d1-35b96fbfc64so638398a91.2 for ; Thu, 19 Mar 2026 16:30:56 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1773963056; x=1774567856; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=sCHbx83iK3r3PTq425PBvHDhuCzYUl5sN2W6Ton0yI0=; b=HGaV+NDiMEwkOvFzpApfF2e5KIRVd0goCxK0Vx35cSeGT4nTdQxZDfuD+EonWkY5xl H6sjXYoeqhZtC5aOe4qNdluoVvTV94G2nOK7uDVTzUa/EzbQTvA3DPyS9uwt/SJWNZxD 01++syQG2Jb9rOkselcyNyfm+cdlcSOtJiW3qpzu4bBsRCQ7YyQYJiaVoRwiJPRFRFu4 9b8eR10pOV+FdQOHr/2UI8eChLDSsIAIE7cKqLUahZVjtTS6ZH2/bEqp1UfidqWMNRPy deXTlBic0mkjlrHzxSuabHLHjDRCmh/p9xAUqiagBUXbQMsJ/nWKVMpFXmUWO1IchJDR kQrg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1773963056; x=1774567856; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=sCHbx83iK3r3PTq425PBvHDhuCzYUl5sN2W6Ton0yI0=; b=BN1MwEy4m+JXvjz47E0nDFIhOjY7LeN1kJ9m7eyMdOF1/OBG0TLdBErCpFhQozkvSj a5d4vPN5pu5IhWwPrsKmSxFzsYap+uDZ0DHX0xWiIIFETN1DqNzH7wLT+Bm5ux6aoRUT ajaZBobJlPAZeUR34judUYkEeXTdC+ZyVBtmN6PsyxMsnM2jH/KswdsfANMC3gK8l1ns ptYBr7NyiPGeqtOIuO2MPrKxvchpYFUhPhP+hiW1NIm/+zOCE0lNnR1l2iR7uPOBCsZS /E4QKm6qNOMVbK5oBKMr3cPRaDjgJABMJnin+qWHbUqwNKRazXemi7kEU9XiSQ0S55co NfLg== X-Forwarded-Encrypted: i=1; AJvYcCXj0q/x5Ry8WbCAAm0X6OcRm26boMInQG/SszHg5zm+XZAnFLcpGCJgwVfBrKWJQWiL91kJl50WYF+vAbw=@vger.kernel.org X-Gm-Message-State: AOJu0YxORhCNPDQk0iUiHblPR9IpdpsBFmgKy0c3FLqF2Fh+UcB55uiq ed1yQv4uVia6Sf3MvDkd/O1C66qPPRsK0+D47s7av82POtdB/DIa7WSOM+m9ZyTzuyRIX/fkKlh X46BZMQ== X-Received: from pjvi6.prod.google.com ([2002:a17:90a:dc06:b0:35b:9c1e:a503]) (user=wyihan job=prod-delivery.src-stubby-dispatcher) by 2002:a17:90b:3e44:b0:359:9158:7459 with SMTP id 98e67ed59e1d1-35bd297b148mr776486a91.0.1773963056138; Thu, 19 Mar 2026 16:30:56 -0700 (PDT) Date: Thu, 19 Mar 2026 23:30:28 +0000 In-Reply-To: <20260319-memory-failure-mf-delayed-fix-rfc-v2-v2-0-92c596402a7a@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20260319-memory-failure-mf-delayed-fix-rfc-v2-v2-0-92c596402a7a@google.com> X-Developer-Key: i=wyihan@google.com; a=ed25519; pk=cRi0fKzS5BMxlHyHY2pJv3w/1zcgfYKr6EYGYppdMYc= X-Developer-Signature: v=1; a=ed25519-sha256; t=1773963053; l=1352; i=wyihan@google.com; s=20260319; h=from:subject:message-id; bh=wOqfNAc1NvD9OIIK2IFuT9hSXcRKmOriHgtHRjPE5Bg=; b=YPNL8jxwq4kWmS/RsAYB4N4wCDzvDeNXcDvt36H56rqlisScdhaT3wHvfkS9+Dbew0Z0kkT6f pQOjgJ05T0TC8Im8YK9ko5dIngtrrkkjKMNkE/VDDjMrdxjo70z5Djw X-Mailer: b4 0.14.3 Message-ID: <20260319-memory-failure-mf-delayed-fix-rfc-v2-v2-1-92c596402a7a@google.com> Subject: [PATCH RFC v2 1/7] mm: memory_failure: Clarify the MF_DELAYED definition From: Lisa Wang To: Miaohe Lin , Naoya Horiguchi , Andrew Morton , Paolo Bonzini , Shuah Khan , Hugh Dickins , Baolin Wang , David Hildenbrand , Lorenzo Stoakes , "Liam R. Howlett" , Vlastimil Babka , Mike Rapoport , Suren Baghdasaryan , Michal Hocko , linux-mm@kvack.org, linux-kernel@vger.kernel.org, kvm@vger.kernel.org, linux-kselftest@vger.kernel.org Cc: rientjes@google.com, seanjc@google.com, ackerleytng@google.com, vannapurve@google.com, michael.roth@amd.com, jiaqiyan@google.com, tabba@google.com, dave.hansen@linux.intel.com, Lisa Wang Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable This patch clarifies the definition of MF_DELAYED to represent cases where a folio's removal is initiated but not immediately completed (e.g., due to remaining metadata references). Signed-off-by: Lisa Wang Reviewed-by: Jiaqi Yan --- mm/memory-failure.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/mm/memory-failure.c b/mm/memory-failure.c index ee42d4361309..4f143334d5a1 100644 --- a/mm/memory-failure.c +++ b/mm/memory-failure.c @@ -862,9 +862,10 @@ static int kill_accessing_process(struct task_struct *= p, unsigned long pfn, * by the m-f() handler immediately. * * MF_DELAYED - The m-f() handler marks the page as PG_hwpoisoned'ed. - * The page is unmapped, and is removed from the LRU or file mapping. - * An attempt to access the page again will trigger page fault and the - * PF handler will kill the process. + * It means the page was partially isolated (e.g. removed from file mapping + * or the LRU) but full cleanup is deferred (e.g. the metadata for the + * memory, as in struct page/folio, is still referenced). Any further + * access to the page will result in the process being killed. * * MF_RECOVERED - The m-f() handler marks the page as PG_hwpoisoned'ed. * The page has been completely isolated, that is, unmapped, taken out of --=20 2.53.0.959.g497ff81fa9-goog From nobody Mon Apr 6 09:12:33 2026 Received: from mail-pj1-f74.google.com (mail-pj1-f74.google.com [209.85.216.74]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 79A65407562 for ; Thu, 19 Mar 2026 23:30:58 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.74 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1773963059; cv=none; b=MtJCYwOTpTxde40wBXSvSOO9HtApXMHrQ5BYLNqQXVEOgAe+q0MaHZrkKnOEFATP4TMBwk19nLNw1EvXZcN6PooQv5+dgjqZ5kCSZxjkQVJbaZkF38FrlD2XosW+zOTKFiIiEZQBND1cw4PyQkcRfR7eDuuf4mt+gtx0t7DT5ic= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1773963059; c=relaxed/simple; bh=bEQvMaA9O2hDkO+lF+QBqIJNJg07ecLAkODpS+u5iOg=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=uPLzMFAEh2FHphNgpFOYoaIIS5EMUKiZAS5DnUitnKM34D8watssugAny3SKRlUTKk+hyJ9Tl6RorkaRA/wfQ3mdgzS5Rlks0mAGj7vM3Z5aNS/48LVH1aIR1JCJayk8LFkOdBMPS0CYNarzzQtWQnuk2P1aU69IbdM/msTTZuo= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--wyihan.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=p9fjIFLk; arc=none smtp.client-ip=209.85.216.74 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--wyihan.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="p9fjIFLk" Received: by mail-pj1-f74.google.com with SMTP id 98e67ed59e1d1-3568090851aso1108611a91.1 for ; Thu, 19 Mar 2026 16:30:58 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1773963058; x=1774567858; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=OCMt2z1jL5wVFi5HOfim8vFneIc22rcNHAIsqy/Chyw=; b=p9fjIFLkzIR9Ae1HaRpzpp9b8NHcI6Vw0wSI/UiXheSo8esH6onJitFdhhKGCvkwzm IPi6lIJ7ayVRKIrTBVzrtynjeM0KibXi9SCbUwyoLkIKJE3ETh2JqlALrzt5EjmIgPMI vJXOTU0/ZY7Ema0h2mca3IpExQ5EqaE0vtG58v2hKehddIMG9f1QYy3+UymPMsdYI7rR tGi7EGv+Ly0RD1IiMNsTOF6EPyXRB6BCP7Ha/0f/roN0dqb1CrY8cUbBEKxf/8bxmT8+ g2zbnMHJYqDwlRwnNKj26aZHpSfWLHi2Jx3mpv60wZ8viVtC/ATgfsL0ih3DlVbQoVhh GRYw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1773963058; x=1774567858; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=OCMt2z1jL5wVFi5HOfim8vFneIc22rcNHAIsqy/Chyw=; b=c+Lg9OiLXNC4rdzn0suB3Ji2QEHEDs0z45aR0Epuv9AhmI4QGmYOvtvhR9Cfq69ugY RNJbpWU7r05orglXjuOte86Cqc7AlfsAeHc/IaA/u0ovyLyuCQYtODWQhFZKGvdcRfH1 15RT5iKmnlEgIKcvKy5RQLOmpkwMr5Nu+Oy/tbxCHIp+tMdbMBdw+WTYz/5kJqofyH9a MWWVOKrUjw46aw0XQkfdDHp1hjFE0bNGpwXJL7+xkFEePouB/kMjrGOledjEhy4FnYBv vYvHDo7GSAknGrGgn4dyhJr6eAw8RJQfA2g/cmiFIryd4+s4joGEHINYrCQCQNN9p3Pm 9Zdw== X-Forwarded-Encrypted: i=1; AJvYcCVVEGHiGbmNJJzxvO8hjKX55A52/RCYLs5it7YiYQHYiXh2+Jq5MV/eY9ndUO7MEvjSLh2o9RtIAWj7UL4=@vger.kernel.org X-Gm-Message-State: AOJu0Yw6y0TswoZgrsNvZ1lYv7R4WivWGf2frnfLmdL6edjRuiAumVxn 7uYYLSco9RqFqUbJT+1Zl5d/CwUvMqTSIc1qAx8CEDV+NnmcDFCGLuYA3ZFcmlDzSYvC7PHRqDu GzHNYtg== X-Received: from pger18.prod.google.com ([2002:a63:a012:0:b0:c75:bd4a:f509]) (user=wyihan job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a20:7d9e:b0:398:f1ed:7fa3 with SMTP id adf61e73a8af0-39bcec301e7mr930308637.57.1773963057622; Thu, 19 Mar 2026 16:30:57 -0700 (PDT) Date: Thu, 19 Mar 2026 23:30:29 +0000 In-Reply-To: <20260319-memory-failure-mf-delayed-fix-rfc-v2-v2-0-92c596402a7a@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20260319-memory-failure-mf-delayed-fix-rfc-v2-v2-0-92c596402a7a@google.com> X-Developer-Key: i=wyihan@google.com; a=ed25519; pk=cRi0fKzS5BMxlHyHY2pJv3w/1zcgfYKr6EYGYppdMYc= X-Developer-Signature: v=1; a=ed25519-sha256; t=1773963053; l=1473; i=wyihan@google.com; s=20260319; h=from:subject:message-id; bh=bEQvMaA9O2hDkO+lF+QBqIJNJg07ecLAkODpS+u5iOg=; b=13DaSlI9UabzaKWe4h2fh2HtJjjXwRHu/w4sC/PfYfQR83rUFlAnEp/P6cSUes8BAsTQjFGZ7 scwG/rZVGLFDn5Cdb0QuA+zOHJK4OdPK+rIY+aD7esoRAD8+cY0a+dy X-Mailer: b4 0.14.3 Message-ID: <20260319-memory-failure-mf-delayed-fix-rfc-v2-v2-2-92c596402a7a@google.com> Subject: [PATCH RFC v2 2/7] mm: memory_failure: Allow truncate_error_folio to return MF_DELAYED From: Lisa Wang To: Miaohe Lin , Naoya Horiguchi , Andrew Morton , Paolo Bonzini , Shuah Khan , Hugh Dickins , Baolin Wang , David Hildenbrand , Lorenzo Stoakes , "Liam R. Howlett" , Vlastimil Babka , Mike Rapoport , Suren Baghdasaryan , Michal Hocko , linux-mm@kvack.org, linux-kernel@vger.kernel.org, kvm@vger.kernel.org, linux-kselftest@vger.kernel.org Cc: rientjes@google.com, seanjc@google.com, ackerleytng@google.com, vannapurve@google.com, michael.roth@amd.com, jiaqiyan@google.com, tabba@google.com, dave.hansen@linux.intel.com, Lisa Wang Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable The .error_remove_folio a_ops is used by different filesystems to handle folio truncation upon discovery of a memory failure in the memory associated with the given folio. Currently, MF_DELAYED is treated as an error, causing "Failed to punch page" to be written to the console. MF_DELAYED is then relayed to the caller of truncate_error_folio() as MF_FAILED. This further causes memory_failure() to return -EBUSY, which then always causes a SIGBUS. This is also implies that regardless of whether the thread's memory corruption kill policy is PR_MCE_KILL_EARLY or PR_MCE_KILL_LATE, a memory failure with MF_DELAYED will always cause a SIGBUS. Update truncate_error_folio() to return MF_DELAYED to the caller if the .error_remove_folio() callback reports MF_DELAYED. Signed-off-by: Lisa Wang --- mm/memory-failure.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/mm/memory-failure.c b/mm/memory-failure.c index 4f143334d5a1..57f7762e7418 100644 --- a/mm/memory-failure.c +++ b/mm/memory-failure.c @@ -941,6 +941,8 @@ static int truncate_error_folio(struct folio *folio, un= signed long pfn, if (mapping->a_ops->error_remove_folio) { int err =3D mapping->a_ops->error_remove_folio(mapping, folio); =20 + if (err =3D=3D MF_DELAYED) + return err; if (err !=3D 0) pr_info("%#lx: Failed to punch page: %d\n", pfn, err); else if (!filemap_release_folio(folio, GFP_NOIO)) --=20 2.53.0.959.g497ff81fa9-goog From nobody Mon Apr 6 09:12:33 2026 Received: from mail-pj1-f73.google.com (mail-pj1-f73.google.com [209.85.216.73]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 1CF08405AA3 for ; Thu, 19 Mar 2026 23:31:00 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.73 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1773963061; cv=none; b=KDpZX7bOlXJexofTZl1MhFPUSNlydELQ3tW54pyAOUsFMPpIV1HjXeE9nXDt5eD5deXOByZTD08dopssMOij79bxXxV4Ux9A9/kyxGRHXHCS0VvHD4froGRacS29x0vkEyIC7/IZFGKzHY0R6O4zGwR9tVtNiF9UQgQZ0d1R1/E= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1773963061; c=relaxed/simple; bh=4nBmEq8PTxZ6noHdf3XYrTEMDbspLx8WC3hJcQV/KoI=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=X7HQEDZDI39WpxzfhmDPACBtfC0wH1rhbKCHoJ37mMZiNeD4DCdBMEWO+KKtSGxTa/4+HriBfIX/H2uzOEIj3g9cDip5L3sVDzhSbCvb2RWKJ47Pr6SSZPl9OOkM5VUr75d2lIbSXnQLX0FaELS2zle6QxXzob8bsUHAzTeOrL0= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--wyihan.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=p+j0/diJ; arc=none smtp.client-ip=209.85.216.73 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--wyihan.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="p+j0/diJ" Received: by mail-pj1-f73.google.com with SMTP id 98e67ed59e1d1-354bc535546so94047a91.3 for ; Thu, 19 Mar 2026 16:31:00 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1773963059; x=1774567859; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=ovB857WaY8E/qhB9OVlkciDwSTU3lGKfzkX9KK2x9wk=; b=p+j0/diJlY77CuXR9Tk54HsPz5ZqsgCDDIWCVmWTZ84Pw455+A4833FDXRQpK/fJKn 3oOnBeaov91Y5rKz5YbghTf3NYdHPpLxdqy9dtC2JL5hFMXrIILkljV4Em7LmbP2grSx cqnACMcEBZDF4zHjH9SDA+PkgNp8fDc+SndHLXgkoQl1GiIUMZtsXJNhKkxL0Ja7BB6J +YoQIQ4tnZsrSaW05T+6wWdlQTYzfELFO4n3368Sjz5kzQjrrLbuk07PinNja4eQubhy LsREzRmbjOgQh6qluOVQyI99vPptGXdRFh4/hEPpQ5laJfT9W1cRQlTF/LsNqXiuGJFx zUhA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1773963059; x=1774567859; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=ovB857WaY8E/qhB9OVlkciDwSTU3lGKfzkX9KK2x9wk=; b=ssC4WPNAGaL26P7V8F0JBgjfILG6OWIThIEnb03Kd1hfmXpG/1MBrzJnHmE/nPVeTQ r/vtaC4kkjBBFWgL8l3WqBHKHfr3rJxf2k853GWr0pjzdfX25nHfGaLIZwhD+dCgG3M3 zMrO8dOORyBUeafvjamW6ybRoKtsm+lmLfGvLmDg7WUwfmwglhGAQcqoBrQ/Oo9YxUnA AbYK8enphgCKnuLYgNyxDSMvjxBhfTk8pHAVneSBTgK8Kc+JpRY1E28/Y4lZe3Ucf6N0 m4/uKsuMuufblGldy1xto9cZxH/VNjMJXQeoa9Iw+hVvrIPM7ZemBm9QCPCrOQ1SmOfP Ul3g== X-Forwarded-Encrypted: i=1; AJvYcCVHjdVee5Kq3qf/kEB5GeSH+64RKNinLNIB9om1drlCGQ5R4wkwtSH7QuiTgggrAqoXBc3O5lP6ClGDxVQ=@vger.kernel.org X-Gm-Message-State: AOJu0YykUHs+DcJIagiLgrv7f1zjpcH6lNPtt4OgSRHWeeYBGvZzeGmu wStFwopcvIyuqI8sbavv/reN63hTw0nXkDAQUAtvcMnabEuDZUIsd1zFTH4bN4J3qtEht+GAA/G cYa7DoA== X-Received: from pfbha19.prod.google.com ([2002:a05:6a00:8513:b0:82a:5ddb:b051]) (user=wyihan job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a20:6a2c:b0:398:8f38:441a with SMTP id adf61e73a8af0-39bce778777mr953630637.0.1773963059297; Thu, 19 Mar 2026 16:30:59 -0700 (PDT) Date: Thu, 19 Mar 2026 23:30:30 +0000 In-Reply-To: <20260319-memory-failure-mf-delayed-fix-rfc-v2-v2-0-92c596402a7a@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20260319-memory-failure-mf-delayed-fix-rfc-v2-v2-0-92c596402a7a@google.com> X-Developer-Key: i=wyihan@google.com; a=ed25519; pk=cRi0fKzS5BMxlHyHY2pJv3w/1zcgfYKr6EYGYppdMYc= X-Developer-Signature: v=1; a=ed25519-sha256; t=1773963053; l=1597; i=wyihan@google.com; s=20260319; h=from:subject:message-id; bh=4nBmEq8PTxZ6noHdf3XYrTEMDbspLx8WC3hJcQV/KoI=; b=vxpkmKNhTZAlitbCxGvzVvglenrXGw3kA9VLn/NaSu26fMFzma1N7B2oDY+7WGhTEHrWyRZfI Xc+uBjA/xs8Cf3TsOP1JQPD7CsnWMD8SRzBy9zxDOlQIv328bdjw/Ai X-Mailer: b4 0.14.3 Message-ID: <20260319-memory-failure-mf-delayed-fix-rfc-v2-v2-3-92c596402a7a@google.com> Subject: [PATCH RFC v2 3/7] mm: shmem: Update shmem handler to the MF_DELAYED definition From: Lisa Wang To: Miaohe Lin , Naoya Horiguchi , Andrew Morton , Paolo Bonzini , Shuah Khan , Hugh Dickins , Baolin Wang , David Hildenbrand , Lorenzo Stoakes , "Liam R. Howlett" , Vlastimil Babka , Mike Rapoport , Suren Baghdasaryan , Michal Hocko , linux-mm@kvack.org, linux-kernel@vger.kernel.org, kvm@vger.kernel.org, linux-kselftest@vger.kernel.org Cc: rientjes@google.com, seanjc@google.com, ackerleytng@google.com, vannapurve@google.com, michael.roth@amd.com, jiaqiyan@google.com, tabba@google.com, dave.hansen@linux.intel.com, Lisa Wang Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable To align with the definition of MF_DELAYED, update shmem_error_remove_folio() to return MF_DELAYED. shmem handles memory failures but defers the actual file truncation. The function's return value should therefore be MF_DELAYED to accurately reflect the state. Currently, this logical error does not cause a bug, because: - For shmem folios, folio->private is not set. - As a result, filemap_release_folio() is a no-op and returns true. - This, in turn, causes truncate_error_folio() to incorrectly return MF_RECOVERED. - The caller then treats MF_RECOVERED as a success condition, masking the issue. The previous patch relays MF_DELAYED to the caller of truncate_error_folio() before any logging, so returning MF_DELAYED from shmem_error_remove_folio() will retain the original behavior of not adding any logs. The return value of truncate_error_folio() is consumed in action_result(), which treats MF_DELAYED the same way as MF_RECOVERED, hence action_result() also returns the same thing after this change. Signed-off-by: Lisa Wang --- mm/shmem.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mm/shmem.c b/mm/shmem.c index b40f3cd48961..fd8f90540361 100644 --- a/mm/shmem.c +++ b/mm/shmem.c @@ -5207,7 +5207,7 @@ static void __init shmem_destroy_inodecache(void) static int shmem_error_remove_folio(struct address_space *mapping, struct folio *folio) { - return 0; + return MF_DELAYED; } =20 static const struct address_space_operations shmem_aops =3D { --=20 2.53.0.959.g497ff81fa9-goog From nobody Mon Apr 6 09:12:33 2026 Received: from mail-pj1-f73.google.com (mail-pj1-f73.google.com [209.85.216.73]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id C456C408226 for ; Thu, 19 Mar 2026 23:31:01 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.73 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1773963062; cv=none; b=NPIUGvUaZ4cjMVN1e3JptAzIk5ntPu4icCLDmBE4rliLynPuvUAtCCo3txabSMMb8OE0AsvMq7NKJNIxzSwHR+91vDDWIbRmL6xMolQlPbOccZubKWkCfONs/CpNwapP3OxcbMFw6HeWJZ+STwUHOBcevMdq3Q4likckAdfGc+g= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1773963062; c=relaxed/simple; bh=zHitcA41pFBEUJ383H8n8swfI4BJin4muVKIFkkU2XU=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=s5Rnhr4VFDARGDlOVAJa1HdXw1IkhFvz8HVnPgNJIsX5m+tLbhrJqEl+r5ETBJlfJqCszQXMLQ0+T0jHcP83iWB8lkKtbSGX5lqMb0/GIz+PRRrBpzQFQG/m2MhHeFPuWlIeCuzQKVFof5v/jljVGKsqXDuUtO3VOixTcPum3nY= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--wyihan.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=ZIFPnV6n; arc=none smtp.client-ip=209.85.216.73 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--wyihan.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="ZIFPnV6n" Received: by mail-pj1-f73.google.com with SMTP id 98e67ed59e1d1-354c0234c1fso57158a91.2 for ; Thu, 19 Mar 2026 16:31:01 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1773963061; x=1774567861; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=aTc9DooR/2VPop6AXcvWPH42yPmf9OOMr5Mq1SUAhF8=; b=ZIFPnV6nmfhkxRoHm5zCORUYJWL+IprrPqQKD+F53fyZx/KDWk5IUOd/bvE1QefhCr f/FlMB4sOwyHjD8dM7ngB9/Ew/nXm1q0a11PTd+TtatXEjLVgSLyFfHpKAxIvWx228le HXjNCN1uQ5JZLkfqLng9Nr7XDp/0FDatfzkFCK8MZWYwuhafPOZY51ff2QAsLKKwGeNm 4uuALBqy8TjPbpC94E3kq7AnFCEahx4R/xTlJpCcH6aMcDaS8t5wumqbMe5OXgFkoNUm 5gZoDQyW2t3GIkpujoXMMjfax2aJsDnuusDLasxQfq5VKBliMKxAVGjifgMNXApneI2R M/+g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1773963061; x=1774567861; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=aTc9DooR/2VPop6AXcvWPH42yPmf9OOMr5Mq1SUAhF8=; b=byn2m3fc7u5hEGF2bpDlf4mQcorQtK+Ai1Q78E/pHjdSkqMZD7dFLeimq3iqP5XWmZ 1n48Kx7rRV/7wcKOt0LfQJjNXShO8iVnCLsda2CzbliO5Dsv/S2Qy+R+VMppsbHTm6jr OkkrUzz+TJCGPTtMFnsmwOVFEQGy6CwqwAfbla63H/jve+Iy5HCrPAaEYpyL+YfLQd1e MzasQwkhKPdhT7QlM2dSDqHTWKEtjBu5cFROfFWFf3HoI3iXyU92/0pN3W0c8wT5ushr j5GOVrbsYdQqszXNHbdE8ceXlBYchggycWqfHgwXz8s+37DDKULVypXzFcYlQHhznh0N eqAQ== X-Forwarded-Encrypted: i=1; AJvYcCWufXA6WgseYCLSjNC4gm4Z+N1cUvkpKdg+dLe9G+KXVNVvhSOEoLx6SeGceMWR2moFZCbxJDqYCWEEj0o=@vger.kernel.org X-Gm-Message-State: AOJu0Yz/fOWlcCE+RCbeHTxW0rRwuyC+p9sD8QPhY4FQXZ8TvZB9PS+1 2Wy6xz/mcCUdC0v4S3Zn+Lunlu1r84B7pyrk9fNcY9lJ9eNOi6yvQQP9jwslnKQ1YWiIUEFkR/n 3Ah2MNw== X-Received: from pjbgz11.prod.google.com ([2002:a17:90b:ecb:b0:35a:624:7b40]) (user=wyihan job=prod-delivery.src-stubby-dispatcher) by 2002:a17:90b:5405:b0:35b:9397:7073 with SMTP id 98e67ed59e1d1-35bd2d668admr547168a91.30.1773963061106; Thu, 19 Mar 2026 16:31:01 -0700 (PDT) Date: Thu, 19 Mar 2026 23:30:31 +0000 In-Reply-To: <20260319-memory-failure-mf-delayed-fix-rfc-v2-v2-0-92c596402a7a@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20260319-memory-failure-mf-delayed-fix-rfc-v2-v2-0-92c596402a7a@google.com> X-Developer-Key: i=wyihan@google.com; a=ed25519; pk=cRi0fKzS5BMxlHyHY2pJv3w/1zcgfYKr6EYGYppdMYc= X-Developer-Signature: v=1; a=ed25519-sha256; t=1773963053; l=1542; i=wyihan@google.com; s=20260319; h=from:subject:message-id; bh=zHitcA41pFBEUJ383H8n8swfI4BJin4muVKIFkkU2XU=; b=55H40VUL7GxMofCPXgAP+Uk3iLTL72SaLmREsmblKKl9G+ZOSJY8iKHX8YOxAFgnJqdH5XZzf fXaJWZTbAZmDgPciWDasEHmQx7TAolEylUlNZ3mq/X9WXooK2QChYEc X-Mailer: b4 0.14.3 Message-ID: <20260319-memory-failure-mf-delayed-fix-rfc-v2-v2-4-92c596402a7a@google.com> Subject: [PATCH RFC v2 4/7] mm: memory_failure: Generalize extra_pins handling to all MF_DELAYED cases From: Lisa Wang To: Miaohe Lin , Naoya Horiguchi , Andrew Morton , Paolo Bonzini , Shuah Khan , Hugh Dickins , Baolin Wang , David Hildenbrand , Lorenzo Stoakes , "Liam R. Howlett" , Vlastimil Babka , Mike Rapoport , Suren Baghdasaryan , Michal Hocko , linux-mm@kvack.org, linux-kernel@vger.kernel.org, kvm@vger.kernel.org, linux-kselftest@vger.kernel.org Cc: rientjes@google.com, seanjc@google.com, ackerleytng@google.com, vannapurve@google.com, michael.roth@amd.com, jiaqiyan@google.com, tabba@google.com, dave.hansen@linux.intel.com, Lisa Wang Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Generalize extra_pins handling to all MF_DELAYED cases not only shmem_mapping. If MF_DELAYED is returned, the filemap continues to hold refcounts on the folio. Hence, take that into account when checking for extra refcounts. As clarified in an earlier patch, a return value of MF_DELAYED implies that the page still has elevated refcounts. Hence, set extra_pins to true if the return value is MF_DELAYED. This is aligned with the implementation in me_swapcache_dirty(), where, if a folio is still in the swap cache, ret is set to MF_DELAYED and extra_pins is set to true. Signed-off-by: Lisa Wang --- mm/memory-failure.c | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/mm/memory-failure.c b/mm/memory-failure.c index 57f7762e7418..86b6f7ba5d3a 100644 --- a/mm/memory-failure.c +++ b/mm/memory-failure.c @@ -1052,18 +1052,14 @@ static int me_pagecache_clean(struct page_state *ps= , struct page *p) goto out; } =20 - /* - * The shmem page is kept in page cache instead of truncating - * so is expected to have an extra refcount after error-handling. - */ - extra_pins =3D shmem_mapping(mapping); - /* * Truncation is a bit tricky. Enable it per file system for now. * * Open: to take i_rwsem or not for this? Right now we don't. */ ret =3D truncate_error_folio(folio, page_to_pfn(p), mapping); + + extra_pins =3D ret =3D=3D MF_DELAYED; if (has_extra_refcount(ps, p, extra_pins)) ret =3D MF_FAILED; =20 --=20 2.53.0.959.g497ff81fa9-goog From nobody Mon Apr 6 09:12:33 2026 Received: from mail-pg1-f201.google.com (mail-pg1-f201.google.com [209.85.215.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 62D2D40B6C1 for ; Thu, 19 Mar 2026 23:31:03 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.215.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1773963064; cv=none; b=EmhHg/9NhFM6Sm3SJ/zJ+juqxeB6hIU0U6ymDYqWyhFCG+FQq8TftF2tII8p91Fdo/qVg9cm8BMKQGAx/p10/1ONB3v+MkPsNcY/ZF5nCehHhd96iuEhS3m5feGBzbaapzMHCRkG1/S7XkC6W+HKulTb3azDvvyTrQN1AfBenXg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1773963064; c=relaxed/simple; bh=XqXOZx8bRJ+MW3QLKZ9eLemXOW8pkkhqg+4Xehhhu34=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=jT7YMgD2AlhklGa9pxky++icvJvrjcUg9kHlNjssL42iYnAwucG3iRoYBfCKXBUgnBrkWR+6qy1gCbVEK+Xm+HW0zHolQvso2oefgB5ljsHvO0scgw0qY1k61tszhFgpzssUj9+/W3dYlUeB1TAPz3vdk4PzDhIpCrhubRREaLI= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--wyihan.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=cVMxfYa1; arc=none smtp.client-ip=209.85.215.201 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--wyihan.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="cVMxfYa1" Received: by mail-pg1-f201.google.com with SMTP id 41be03b00d2f7-b6ce1b57b9cso21560a12.1 for ; Thu, 19 Mar 2026 16:31:03 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1773963063; x=1774567863; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=dLN80hi+wV7RsIDgwy/tRG94iKBvpQ4g3gjUbQ44niY=; b=cVMxfYa1W6IQapW66DifvfmqGeW1bX5Uk4m4dDH10M9MOLnFryOUekLi0MN8LKGyL1 2Ybw57YiHa+Q4xyHGSTsZhEAtC1pBZqzziARktV8t3mTDxTnx3KWd1VmB04as0/ZX38h GfeSrJuNmyBGbFv0Dqd89X/SqttvYswqRtQX+Eztg6fhDXp4jgR2XgEdzcJboKrnaufA tAlrT3bXWqIagGiHMThMx0kJKc2mBGElsImcotPjy+RlR7c20owkmRN/IFWGw3vQut1n mdFIX4cIRnJT+8vwYAJfQKFU9aNy4eCOjPsT2hUAOeWWcEuG8tP7wDuYg2M8h6rcfgmQ W5Fg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1773963063; x=1774567863; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=dLN80hi+wV7RsIDgwy/tRG94iKBvpQ4g3gjUbQ44niY=; b=rC6sau3U31CHZPrwcKC9hl4WOKIqEwAPFXd/YemXbt6dAZKnASFFwFL/yYs9e2DcU0 wx0TJPUHL9PCa5SDpCiSFt31lUVkCD/lEGWXOvInTWDlq69Of+rBMYEttZ7pusldd0jQ 4f4U5+eR2Fq0ZnHCHaX/PsWMtoFBza0AtrK3v3u0oefkP3d4mIwGCU9VcjDpp/zLbhYv xqc5WYMt1VKy32BUMiDDXd+NryhByUzRR1e4bkH8m8BQx0EDsK4Qm2Vvvejj/BSIB3eu joPzUZN8XV23gLmM/ACa/1T4Y1cENkM2KYrpIoX+/gIS/98xtRqIMbNMWIu/bFvm58Ar IMrQ== X-Forwarded-Encrypted: i=1; AJvYcCXbXmVhXJLg5TjNIUk8qCq3NP7cymXvNsUnCCXJFYB0B4P7Ipb9/UdPpCH0dkML0SUqcCS0pku1gresMoY=@vger.kernel.org X-Gm-Message-State: AOJu0YysKXToc8rPqcHZResXqyk+SXWxmkizMR0EwDj8aUn3cQ/jxOfy sRoroAPoy6a2uWkKlXcf5jCqmO6Az35Jd+XVIbii11Go/MqmsP9JB5jXZ80dgTxF+w8PswWreVK jEI2YhQ== X-Received: from pgab123.prod.google.com ([2002:a63:3481:0:b0:c73:9919:c4f8]) (user=wyihan job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a20:3d81:b0:398:8546:c3fc with SMTP id adf61e73a8af0-39bce9b7e6emr924394637.7.1773963062623; Thu, 19 Mar 2026 16:31:02 -0700 (PDT) Date: Thu, 19 Mar 2026 23:30:32 +0000 In-Reply-To: <20260319-memory-failure-mf-delayed-fix-rfc-v2-v2-0-92c596402a7a@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20260319-memory-failure-mf-delayed-fix-rfc-v2-v2-0-92c596402a7a@google.com> X-Developer-Key: i=wyihan@google.com; a=ed25519; pk=cRi0fKzS5BMxlHyHY2pJv3w/1zcgfYKr6EYGYppdMYc= X-Developer-Signature: v=1; a=ed25519-sha256; t=1773963053; l=5121; i=wyihan@google.com; s=20260319; h=from:subject:message-id; bh=XqXOZx8bRJ+MW3QLKZ9eLemXOW8pkkhqg+4Xehhhu34=; b=nez3ePfz2DyHfRD1EU96GSlzQoAPO6925YzKCLhpMi6Z7BSIWwpw9+JzE9uSu8N4PKdbla1ke 3gFKoffsCYJChdWiQqNpWcqhpXJunH0VukcDhLctTtRU/LD9KRdsv3T X-Mailer: b4 0.14.3 Message-ID: <20260319-memory-failure-mf-delayed-fix-rfc-v2-v2-5-92c596402a7a@google.com> Subject: [PATCH RFC v2 5/7] mm: selftests: Add shmem memory failure test From: Lisa Wang To: Miaohe Lin , Naoya Horiguchi , Andrew Morton , Paolo Bonzini , Shuah Khan , Hugh Dickins , Baolin Wang , David Hildenbrand , Lorenzo Stoakes , "Liam R. Howlett" , Vlastimil Babka , Mike Rapoport , Suren Baghdasaryan , Michal Hocko , linux-mm@kvack.org, linux-kernel@vger.kernel.org, kvm@vger.kernel.org, linux-kselftest@vger.kernel.org Cc: rientjes@google.com, seanjc@google.com, ackerleytng@google.com, vannapurve@google.com, michael.roth@amd.com, jiaqiyan@google.com, tabba@google.com, dave.hansen@linux.intel.com, Lisa Wang Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Add a shmem memory failure selftest to test the shmem memory failure is correct after modifying shmem return value. Test that + madvise() call returns 0 at the first time + trigger a SIGBUS when the poisoned shmem page is fault-in again. Signed-off-by: Lisa Wang --- tools/testing/selftests/mm/Makefile | 3 + tools/testing/selftests/mm/run_vmtests.sh | 1 + .../selftests/mm/shmem_memory_failure_test.c | 98 ++++++++++++++++++= ++++ 3 files changed, 102 insertions(+) diff --git a/tools/testing/selftests/mm/Makefile b/tools/testing/selftests/= mm/Makefile index 7a5de4e9bf52..ac033851c9eb 100644 --- a/tools/testing/selftests/mm/Makefile +++ b/tools/testing/selftests/mm/Makefile @@ -72,6 +72,7 @@ TEST_GEN_FILES +=3D madv_populate TEST_GEN_FILES +=3D map_fixed_noreplace TEST_GEN_FILES +=3D map_hugetlb TEST_GEN_FILES +=3D map_populate +TEST_GEN_FILES +=3D shmem_memory_failure_test ifneq (,$(filter $(ARCH),arm64 riscv riscv64 x86 x86_64 loongarch32 loonga= rch64)) TEST_GEN_FILES +=3D memfd_secret endif @@ -259,6 +260,8 @@ $(OUTPUT)/migration: LDLIBS +=3D -lnuma =20 $(OUTPUT)/rmap: LDLIBS +=3D -lnuma =20 +$(OUTPUT)/shmem_memory_failure_test: CFLAGS +=3D -I$(top_srcdir)/tools/inc= lude + local_config.mk local_config.h: check_config.sh CC=3D"$(CC)" CFLAGS=3D"$(CFLAGS)" ./check_config.sh =20 diff --git a/tools/testing/selftests/mm/run_vmtests.sh b/tools/testing/self= tests/mm/run_vmtests.sh index afdcfd0d7cef..58fb959a7936 100755 --- a/tools/testing/selftests/mm/run_vmtests.sh +++ b/tools/testing/selftests/mm/run_vmtests.sh @@ -402,6 +402,7 @@ CATEGORY=3D"hugetlb" run_test ./hugetlb-soft-offline echo "$nr_hugepages_tmp" > /proc/sys/vm/nr_hugepages echo "$enable_soft_offline" > /proc/sys/vm/enable_soft_offline CATEGORY=3D"hugetlb" run_test ./hugetlb-read-hwpoison +CATEGORY=3D"mmap" run_test ./shmem_memory_failure_test fi =20 if [ $VADDR64 -ne 0 ]; then diff --git a/tools/testing/selftests/mm/shmem_memory_failure_test.c b/tools= /testing/selftests/mm/shmem_memory_failure_test.c new file mode 100644 index 000000000000..44752024a7fc --- /dev/null +++ b/tools/testing/selftests/mm/shmem_memory_failure_test.c @@ -0,0 +1,98 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * This test makes sure when memory failure happens, shmem can handle + * successfully. + */ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "kselftest.h" +#include "vm_util.h" + +static sigjmp_buf sigbuf; + +static void signal_handler(int sig, siginfo_t *info, void *ucontext) +{ + siglongjmp(sigbuf, 1); +} + +static void set_signal_handler(int sig, void (*handler)(int, siginfo_t *, = void *)) +{ + struct sigaction sa =3D {}; + + sa.sa_sigaction =3D handler; + sa.sa_flags =3D SA_SIGINFO; + sigemptyset(&sa.sa_mask); + if (sigaction(sig, &sa, NULL) =3D=3D -1) + ksft_exit_fail_msg("Failed to set SIGBUS handler: %s\n", strerror(errno)= ); +} + +static unsigned long addr_to_pfn(char *addr) +{ + int pagemap_fd; + unsigned long pfn; + + pagemap_fd =3D open("/proc/self/pagemap", O_RDONLY); + if (pagemap_fd < 0) + ksft_exit_fail_msg("Failed to open /proc/self/pagemap: %s\n", strerror(e= rrno)); + pfn =3D pagemap_get_pfn(pagemap_fd, addr); + close(pagemap_fd); + + return pfn; +} + +static void test_shmem_memory_failure(size_t total_size, size_t page_size) +{ + unsigned long memory_failure_pfn; + char *memory_failure_mem; + char *memory_failure_addr; + int fd; + + fd =3D memfd_create("shmem_hwpoison_test", 0); + if (fd < 0) + ksft_exit_skip("memfd_create failed: %s\n", strerror(errno)); + + if (ftruncate(fd, total_size) < 0) + ksft_exit_fail_msg("ftruncate failed: %s\n", strerror(errno)); + + memory_failure_mem =3D mmap(NULL, total_size, PROT_READ | PROT_WRITE, MAP= _SHARED, fd, 0); + if (memory_failure_mem =3D=3D MAP_FAILED) + ksft_exit_fail_msg("mmap failed: %s\n", strerror(errno)); + memory_failure_addr =3D memory_failure_mem + page_size; + READ_ONCE(memory_failure_addr[0]); + memory_failure_pfn =3D addr_to_pfn(memory_failure_addr); + + if (madvise(memory_failure_addr, page_size, MADV_HWPOISON) !=3D 0) + ksft_exit_fail_msg("MADV_HWPOISON failed: %s\n", strerror(errno)); + + if (sigsetjmp(sigbuf, 1) =3D=3D 0) { + READ_ONCE(memory_failure_addr[0]); + ksft_test_result_fail("Read from poisoned page should have triggered SIG= BUS\n"); + } else { + ksft_test_result_pass("SIGBUS triggered as expected on poisoned page\n"); + } + + munmap(memory_failure_mem, total_size); + close(fd); + if (unpoison_memory(memory_failure_pfn) < 0) + ksft_exit_fail_msg("unpoison_memory failed: %s\n", strerror(errno)); +} + +int main(int argc, char *argv[]) +{ + const size_t pagesize =3D getpagesize(); + + ksft_print_header(); + ksft_set_plan(1); + + set_signal_handler(SIGBUS, signal_handler); + test_shmem_memory_failure(pagesize * 4, pagesize); + ksft_finished(); +} --=20 2.53.0.959.g497ff81fa9-goog From nobody Mon Apr 6 09:12:33 2026 Received: from mail-pl1-f202.google.com (mail-pl1-f202.google.com [209.85.214.202]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 1DB85405AA1 for ; Thu, 19 Mar 2026 23:31:05 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1773963066; cv=none; b=BLLwRplWtyStIAM+X8h1YMoKexkBtsNeUCBT65iPQZDz6RQdy0J1BnkZvbKb5RTg5uUwr9HbyJsmqMRnmgj5WcF1vrsz8wEhhNarq2V1L1NkQDfkjz5AZB+1MvbwnxF5h6iP3mOZtuJNhvXDrn8NY8kyj8+QdgM6ajgCgmlYUtY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1773963066; c=relaxed/simple; bh=/IV7lWhsJHF0x5A2Fq993vbJ4R9Cdskri5oidvrL+zU=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=lVFsHUgnERoQAII9GpeZSTBdMPxWonfyIS3oFeumLWhrP6AsfrpLHkIjKtwaExUkGrCXx87KCGxvdUQBfii4okvA5prpycMEzLAgHmm0eujEoET3Blw1Lbz9hyxXRAzDH3PbKQ00LDA3xIMj1v30oQm8ic90lvZBzN/rPprCUDo= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--wyihan.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=TmIHOONQ; arc=none smtp.client-ip=209.85.214.202 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--wyihan.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="TmIHOONQ" Received: by mail-pl1-f202.google.com with SMTP id d9443c01a7336-2b06395b8deso21935035ad.1 for ; Thu, 19 Mar 2026 16:31:05 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1773963064; x=1774567864; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=0XVkZljvWyJMdTaa7/apTU+xEzctUK1Me6jsTW57n1c=; b=TmIHOONQUNy44x1sTWBFqlkHdqTEomJZoN54pDRZUl4sUg2AqTTs3IGuqUzO8W3yX3 mnMGPYgiumbNW4d54rWedlct2YDB4us9EXWUKxH5598Cq82FWPUlp3402ohIUmNvYYwW 6NpEF0KHJFK1JT77BM8CmqSJWxLwx+oeFM49cVDAgE9oqlOyZRRJOCg2DCs5gIobVSNi 7Rd9r2jTGRKMd/NjOozSWUpfFEM0n/sZayMMn8tTH4/gfSzKTmgWESnAnnbO3uW+1Axy Gsgon+oEnEHVTptkmzuAqxmsPSNHHpd8pILjNXIW5DOTJhYKzJ7OuFAUWm1l9QQ/yGAQ Qzqg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1773963064; x=1774567864; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=0XVkZljvWyJMdTaa7/apTU+xEzctUK1Me6jsTW57n1c=; b=YDQ9LAuq5IPRaRP0zUfUIXTkDX/0vYqBhAlhFDltZ5JpfFXphR49MtKssb+GytEc4f RXxL/9GgxB8B/SP/JWFmuBzVIHhJhcxEGyIKRMT/z6GejCGVFKVLZL521LGgW1T+kB4V lARp5qxif8JXZxq7necJc8sHjnXfpWIlPv4wzkbjQhXsWWZ7eyNxTO6uLRP2f7trM3E5 X0mGujaENQLHMNUtWlm8Gucxp1r1K6mn4Tmu7jGTj1rdAbm+I3tmec4tZIzMfJIcAW2W sANwFhAbVqdfEdPIrjPW5+dko420ReLrgyz0FYE/guPv/NmdxxQejLRxUPZhqBmSws4u k3NQ== X-Forwarded-Encrypted: i=1; AJvYcCVcbdIDWLS0eGM9D7SULFezREGKRCQnXepikDiRpq6s8+gUMRusUW6cuUOuGvqTeol3ZcoNkjcQujxn+7I=@vger.kernel.org X-Gm-Message-State: AOJu0YySFnysMU32AsJjGNfK93AbkpDSBu7e/0HsYQe4a+al+R1pEcnH kOUQjwnp/98+/YatPQPiD0+ZW4f4crAWsep44yUFkqG8BvPpkpEayZwl22lT90ptwOICQe4h/oC 0Qzq55A== X-Received: from plgc17.prod.google.com ([2002:a17:902:d491:b0:2b0:6147:a0ee]) (user=wyihan job=prod-delivery.src-stubby-dispatcher) by 2002:a17:903:8c7:b0:2b0:5d60:7f43 with SMTP id d9443c01a7336-2b0826b89fbmr7160295ad.8.1773963064231; Thu, 19 Mar 2026 16:31:04 -0700 (PDT) Date: Thu, 19 Mar 2026 23:30:33 +0000 In-Reply-To: <20260319-memory-failure-mf-delayed-fix-rfc-v2-v2-0-92c596402a7a@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20260319-memory-failure-mf-delayed-fix-rfc-v2-v2-0-92c596402a7a@google.com> X-Developer-Key: i=wyihan@google.com; a=ed25519; pk=cRi0fKzS5BMxlHyHY2pJv3w/1zcgfYKr6EYGYppdMYc= X-Developer-Signature: v=1; a=ed25519-sha256; t=1773963053; l=7959; i=wyihan@google.com; s=20260319; h=from:subject:message-id; bh=/IV7lWhsJHF0x5A2Fq993vbJ4R9Cdskri5oidvrL+zU=; b=qXdjX2Q9/G5HKD4xyXrWxsOPx0Rn2uqxCML7evRdxyatc8uCc0KSmI17m7e+7pLSD0ksRHo+C w8h0S2a/h+KAmDqsho3FoLLL9CKZl1BZ7RTrb4jtLnaSxH9mgAItGtl X-Mailer: b4 0.14.3 Message-ID: <20260319-memory-failure-mf-delayed-fix-rfc-v2-v2-6-92c596402a7a@google.com> Subject: [PATCH RFC v2 6/7] KVM: selftests: Add memory failure tests in guest_memfd_test From: Lisa Wang To: Miaohe Lin , Naoya Horiguchi , Andrew Morton , Paolo Bonzini , Shuah Khan , Hugh Dickins , Baolin Wang , David Hildenbrand , Lorenzo Stoakes , "Liam R. Howlett" , Vlastimil Babka , Mike Rapoport , Suren Baghdasaryan , Michal Hocko , linux-mm@kvack.org, linux-kernel@vger.kernel.org, kvm@vger.kernel.org, linux-kselftest@vger.kernel.org Cc: rientjes@google.com, seanjc@google.com, ackerleytng@google.com, vannapurve@google.com, michael.roth@amd.com, jiaqiyan@google.com, tabba@google.com, dave.hansen@linux.intel.com, Lisa Wang Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable After modifying truncate_error_folio(), we expect memory_failure() will return 0 instead of MF_FAILED. Also, we want to make sure memory_failure() signaling function is same. Test that memory_failure() returns 0 for guest_memfd, where .error_remove_folio() is handled by not actually truncating, and returning MF_DELAYED. In addition, test that SIGBUS signaling behavior is not changed before and after this modification. There are two kinds of guest memory failure injections - madvise or debugfs. When memory failure is injected using madvise, the MF_ACTION_REQUIRED flag is set, and the page is mapped and dirty, the process should get a SIGBUS. When memory is failure is injected using debugfs, the KILL_EARLY machine check memory corruption kill policy is set, and the page is mapped and dirty, the process should get a SIGBUS. Co-developed-by: Ackerley Tng Signed-off-by: Ackerley Tng Signed-off-by: Lisa Wang --- tools/testing/selftests/kvm/guest_memfd_test.c | 168 +++++++++++++++++++++= ++++ 1 file changed, 168 insertions(+) diff --git a/tools/testing/selftests/kvm/guest_memfd_test.c b/tools/testing= /selftests/kvm/guest_memfd_test.c index 618c937f3c90..445e8155ee1e 100644 --- a/tools/testing/selftests/kvm/guest_memfd_test.c +++ b/tools/testing/selftests/kvm/guest_memfd_test.c @@ -10,6 +10,8 @@ #include #include #include +#include +#include =20 #include #include @@ -193,6 +195,171 @@ static void test_fault_overflow(int fd, size_t total_= size) test_fault_sigbus(fd, total_size, total_size * 4); } =20 +static unsigned long addr_to_pfn(void *addr) +{ + const uint64_t pagemap_pfn_mask =3D BIT(54) - 1; + const uint64_t pagemap_page_present =3D BIT(63); + uint64_t page_info; + ssize_t n_bytes; + int pagemap_fd; + + pagemap_fd =3D open("/proc/self/pagemap", O_RDONLY); + TEST_ASSERT(pagemap_fd > 0, "Opening pagemap should succeed."); + + n_bytes =3D pread(pagemap_fd, &page_info, 8, (uint64_t)addr / page_size *= 8); + TEST_ASSERT(n_bytes =3D=3D 8, "pread of pagemap failed. n_bytes=3D%ld", n= _bytes); + + close(pagemap_fd); + + TEST_ASSERT(page_info & pagemap_page_present, "The page for addr should b= e present"); + return page_info & pagemap_pfn_mask; +} + +static void write_memory_failure(unsigned long pfn, bool mark, int return_= code) +{ + char path[PATH_MAX]; + char *filename; + char buf[20]; + int ret; + int len; + int fd; + + filename =3D mark ? "corrupt-pfn" : "unpoison-pfn"; + snprintf(path, PATH_MAX, "/sys/kernel/debug/hwpoison/%s", filename); + + fd =3D open(path, O_WRONLY); + TEST_ASSERT(fd > 0, "Failed to open %s.", path); + + len =3D snprintf(buf, sizeof(buf), "0x%lx\n", pfn); + if (len < 0 || (unsigned int)len > sizeof(buf)) + TEST_ASSERT(0, "snprintf failed or truncated."); + + ret =3D write(fd, buf, len); + if (return_code =3D=3D 0) { + /* + * If the memory_failure() returns 0, write() should be successful, + * which returns how many bytes it writes. + */ + TEST_ASSERT(ret > 0, "Writing memory failure (path: %s) failed: %s", pat= h, + strerror(errno)); + } else { + TEST_ASSERT_EQ(ret, -1); + /* errno is memory_failure() return code. */ + TEST_ASSERT_EQ(errno, return_code); + } + + close(fd); +} + +static void mark_memory_failure(unsigned long pfn, int return_code) +{ + write_memory_failure(pfn, true, return_code); +} + +static void unmark_memory_failure(unsigned long pfn, int return_code) +{ + write_memory_failure(pfn, false, return_code); +} + +enum memory_failure_injection_method { + MF_INJECT_DEBUGFS, + MF_INJECT_MADVISE, +}; + +static void do_test_memory_failure(int fd, size_t total_size, + enum memory_failure_injection_method method, int kill_config, + bool map_page, bool dirty_page, bool sigbus_expected, + int return_code) +{ + unsigned long memory_failure_pfn; + char *memory_failure_addr; + char *mem; + int ret; + + mem =3D mmap(NULL, total_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); + TEST_ASSERT(mem !=3D MAP_FAILED, "mmap() for guest_memfd should succeed."= ); + memory_failure_addr =3D mem + page_size; + if (dirty_page) + *memory_failure_addr =3D 'A'; + else + READ_ONCE(*memory_failure_addr); + + /* Fault in page to read pfn, then unmap page for testing if needed. */ + memory_failure_pfn =3D addr_to_pfn(memory_failure_addr); + if (!map_page) + madvise(memory_failure_addr, page_size, MADV_DONTNEED); + + ret =3D prctl(PR_MCE_KILL, PR_MCE_KILL_SET, kill_config, 0, 0); + TEST_ASSERT_EQ(ret, 0); + + ret =3D 0; + switch (method) { + case MF_INJECT_DEBUGFS: { + /* DEBUGFS injection handles return_code test inside the mark_memory_fai= lure(). */ + if (sigbus_expected) + TEST_EXPECT_SIGBUS(mark_memory_failure(memory_failure_pfn, return_code)= ); + else + mark_memory_failure(memory_failure_pfn, return_code); + break; + } + case MF_INJECT_MADVISE: { + /* + * MADV_HWPOISON uses get_user_pages() so the page will always + * be faulted in at the point of memory_failure() + */ + if (sigbus_expected) + TEST_EXPECT_SIGBUS(ret =3D madvise(memory_failure_addr, + page_size, MADV_HWPOISON)); + else + ret =3D madvise(memory_failure_addr, page_size, MADV_HWPOISON); + + if (return_code =3D=3D 0) + TEST_ASSERT(ret =3D=3D return_code, "Memory failure failed. Errno: %s", + strerror(errno)); + else { + /* errno is memory_failure() return code. */ + TEST_ASSERT_EQ(errno, return_code); + } + break; + } + default: + TEST_FAIL("Unhandled memory failure injection method %d.", method); + } + + TEST_EXPECT_SIGBUS(READ_ONCE(*memory_failure_addr)); + TEST_EXPECT_SIGBUS(*memory_failure_addr =3D 'A'); + + ret =3D munmap(mem, total_size); + TEST_ASSERT(!ret, "munmap() should succeed."); + + ret =3D fallocate(fd, FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE, 0, + total_size); + TEST_ASSERT(!ret, "Truncate the entire file (cleanup) should succeed."); + + ret =3D prctl(PR_MCE_KILL, PR_MCE_KILL_SET, PR_MCE_KILL_DEFAULT, 0, 0); + TEST_ASSERT_EQ(ret, 0); + + unmark_memory_failure(memory_failure_pfn, 0); +} + +static void test_memory_failure(int fd, size_t total_size) +{ + do_test_memory_failure(fd, total_size, MF_INJECT_DEBUGFS, PR_MCE_KILL_EAR= LY, true, true, true, 0); + do_test_memory_failure(fd, total_size, MF_INJECT_DEBUGFS, PR_MCE_KILL_EAR= LY, true, false, false, 0); + do_test_memory_failure(fd, total_size, MF_INJECT_DEBUGFS, PR_MCE_KILL_EAR= LY, false, true, false, 0); + do_test_memory_failure(fd, total_size, MF_INJECT_DEBUGFS, PR_MCE_KILL_LAT= E, true, true, false, 0); + do_test_memory_failure(fd, total_size, MF_INJECT_DEBUGFS, PR_MCE_KILL_LAT= E, true, false, false, 0); + do_test_memory_failure(fd, total_size, MF_INJECT_DEBUGFS, PR_MCE_KILL_LAT= E, false, true, false, 0); + /* + * If madvise() is used to inject errors, memory_failure() handling is in= voked with the + * MF_ACTION_REQUIRED flag set, aligned with memory failure handling for = a consumed memory + * error, where the machine check memory corruption kill policy is ignore= d. Hence, testing with + * PR_MCE_KILL_DEFAULT covers all cases. + */ + do_test_memory_failure(fd, total_size, MF_INJECT_MADVISE, PR_MCE_KILL_DEF= AULT, true, true, true, 0); + do_test_memory_failure(fd, total_size, MF_INJECT_MADVISE, PR_MCE_KILL_DEF= AULT, true, false, false, 0); +} + static void test_fault_private(int fd, size_t total_size) { test_fault_sigbus(fd, 0, total_size); @@ -370,6 +537,7 @@ static void __test_guest_memfd(struct kvm_vm *vm, uint6= 4_t flags) gmem_test(mmap_supported, vm, flags); gmem_test(fault_overflow, vm, flags); gmem_test(numa_allocation, vm, flags); + gmem_test(memory_failure, vm, flags); } else { gmem_test(fault_private, vm, flags); } --=20 2.53.0.959.g497ff81fa9-goog From nobody Mon Apr 6 09:12:33 2026 Received: from mail-pl1-f202.google.com (mail-pl1-f202.google.com [209.85.214.202]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id D492540F8C2 for ; Thu, 19 Mar 2026 23:31:06 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1773963068; cv=none; b=tSIKfIHBz6r9+tN95qZ9ZqjBkOWKCR9IS1/LstiKY5cVpyaj9xIgFoMy3hu5U+Zh4xcan4O3nGLpejkDdvH9LnSEAzRBgZUAF/fHoTUiH2iPD7zZmle22QbpvJtO2umfgxSeptMQXA2LjJEWY1FrHbE91OF4BZsUyZjCoAyVnww= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1773963068; c=relaxed/simple; bh=wltySrG4vozgVjDjb43L2B3dD6dgSygZ1tpNGMuiOvU=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=XV7h2DVHr3w1jZ4nrQ3mT1vleuxxRRePNIS0KKPY4RbZ+4j4kwtELCmaP4kx2CIkuWuOTZN22UdLcEhs8J0AwGz7cnuXNQkHlkAk4z9tndB988VU9vdYVQCQbRDw0CRBL6mzZdQcIdh/oOihFyqQRlYG3bS4MkoxR6KhyeWL+yE= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--wyihan.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=iYucmjAS; arc=none smtp.client-ip=209.85.214.202 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--wyihan.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="iYucmjAS" Received: by mail-pl1-f202.google.com with SMTP id d9443c01a7336-2adc527eaf5so14307495ad.0 for ; Thu, 19 Mar 2026 16:31:06 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1773963066; x=1774567866; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=Zfyz6j+K0eCUgYIAireZB6wFZ/MNqUoA9mP0eBvWMq8=; b=iYucmjASYPBw8+929cUl1yJm3d6Esry5KMVnxg252ptKGZWlr3zsjeVrOlIP3qeziV 9iPe5EeWHQmpLHcF5lz8HAMUGD+8mo4v0QhMnCaAu1ld5+VW6T9sjgLEKS+NL5JEq3y+ VlpKcruLFos2R+STo+3uFGn6KTTbZDjcxhr8jAM3Hwrim/CoKvA6vXrfMx/hoLp2Qbhn toqKhyoHj3HJgNVgRtzOJQ83tGWUXQzX0cV/pi+WPxgRP+qwij5WrTPBcsvKNr/Wf8DW craCZdUNcJJ1JvW2Cwgevx65buGiwdJCzcT7i97d+AuFlVfrp2ADuOiUbIh/9rkIcs3Q fFGQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1773963066; x=1774567866; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=Zfyz6j+K0eCUgYIAireZB6wFZ/MNqUoA9mP0eBvWMq8=; b=l3c15KC3iRmZkchVzg0IBHxOUzlUYBFek/puKerg6gL2fWvoPfClRG/oQWLT9MNsmd oWS0j8dtdBdTbCPtKncg5IqLuXnXNfCnkB9QkB/RXT40tdalQ9E1Bu0hXivrpoZuvH2v b9q6Fj/ijnLExwNDAPL2n441gjWkU+9mJ+nGShk7zRzQZT80uSG0NJfmmi75d5H/t4/V RDqDNZuhzHNlLSOdAHlpzburZDb1LziA9vlTyVIwDbNPJE5e21ggkf0/tE32/ke1fP4l +RMp+kusSo3buCDNbv+y4jj0R73y42B/lZ/eHwd4lHbbhm21+xx/kn1lgUgiXf3jBb5l 24Xg== X-Forwarded-Encrypted: i=1; AJvYcCUk840i+tdFYc7fYA6T/0oFIi3z/HOqmFKsITH6T8MrEPsp3NiMF2kCec+MMt9hMwhnpC99gFX4IO7AV0g=@vger.kernel.org X-Gm-Message-State: AOJu0YzBTpl7k+h61dlftaVQI5hR+WAarjx0Cz1kf/IInwGL5nlc9EqQ IdKOlKJg7+7viqvziJj70DdosMUq21ID5Opq3GFbsEg1v1AJjJ82D230zoaFFF5gND/MNOGff9Y 9nZmZsA== X-Received: from plbks8.prod.google.com ([2002:a17:903:848:b0:2b0:52e8:584]) (user=wyihan job=prod-delivery.src-stubby-dispatcher) by 2002:a17:903:2f84:b0:2b0:4b3a:9b4b with SMTP id d9443c01a7336-2b0826e3368mr7472585ad.16.1773963066002; Thu, 19 Mar 2026 16:31:06 -0700 (PDT) Date: Thu, 19 Mar 2026 23:30:34 +0000 In-Reply-To: <20260319-memory-failure-mf-delayed-fix-rfc-v2-v2-0-92c596402a7a@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20260319-memory-failure-mf-delayed-fix-rfc-v2-v2-0-92c596402a7a@google.com> X-Developer-Key: i=wyihan@google.com; a=ed25519; pk=cRi0fKzS5BMxlHyHY2pJv3w/1zcgfYKr6EYGYppdMYc= X-Developer-Signature: v=1; a=ed25519-sha256; t=1773963053; l=2809; i=wyihan@google.com; s=20260319; h=from:subject:message-id; bh=wltySrG4vozgVjDjb43L2B3dD6dgSygZ1tpNGMuiOvU=; b=MzayBc1lrUG8AtictP2UDRMFjSP2+93iY+nziVK024vQNTr5SivQhuoSDPM9eVFFuI+tPuw4N h7fKV+BGoAJDypYm6vtelBw4anbbG1sxuJC+lRnOm/wX8wAEripkYXw X-Mailer: b4 0.14.3 Message-ID: <20260319-memory-failure-mf-delayed-fix-rfc-v2-v2-7-92c596402a7a@google.com> Subject: [PATCH RFC v2 7/7] KVM: selftests: Test guest_memfd behavior with respect to stage 2 page tables From: Lisa Wang To: Miaohe Lin , Naoya Horiguchi , Andrew Morton , Paolo Bonzini , Shuah Khan , Hugh Dickins , Baolin Wang , David Hildenbrand , Lorenzo Stoakes , "Liam R. Howlett" , Vlastimil Babka , Mike Rapoport , Suren Baghdasaryan , Michal Hocko , linux-mm@kvack.org, linux-kernel@vger.kernel.org, kvm@vger.kernel.org, linux-kselftest@vger.kernel.org Cc: rientjes@google.com, seanjc@google.com, ackerleytng@google.com, vannapurve@google.com, michael.roth@amd.com, jiaqiyan@google.com, tabba@google.com, dave.hansen@linux.intel.com, Lisa Wang Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Test that + memory failure handling results in unmapping of bad memory from stage 2 page tables, hence requiring faulting on next guest access + when the guest tries to fault a poisoned page from guest_memfd, the userspace VMM informed with EHWPOISON Co-developed-by: Ackerley Tng Signed-off-by: Ackerley Tng Signed-off-by: Lisa Wang --- tools/testing/selftests/kvm/guest_memfd_test.c | 65 ++++++++++++++++++++++= ++++ 1 file changed, 65 insertions(+) diff --git a/tools/testing/selftests/kvm/guest_memfd_test.c b/tools/testing= /selftests/kvm/guest_memfd_test.c index 445e8155ee1e..50907875dc43 100644 --- a/tools/testing/selftests/kvm/guest_memfd_test.c +++ b/tools/testing/selftests/kvm/guest_memfd_test.c @@ -637,6 +637,70 @@ static void test_guest_memfd_guest(void) kvm_vm_free(vm); } =20 +static void __guest_code_read(uint8_t *mem) +{ + READ_ONCE(*mem); + GUEST_SYNC(0); + READ_ONCE(*mem); + GUEST_DONE(); +} + +static void guest_read(struct kvm_vcpu *vcpu, uint64_t gpa, int expected_e= rrno) +{ + vcpu_args_set(vcpu, 1, gpa); + + if (expected_errno) { + TEST_ASSERT_EQ(_vcpu_run(vcpu), -1); + TEST_ASSERT_EQ(errno, expected_errno); + } else { + vcpu_run(vcpu); + TEST_ASSERT_EQ(get_ucall(vcpu, NULL), UCALL_SYNC); + } +} + +static void test_memory_failure_guest(void) +{ + const uint64_t gpa =3D SZ_4G; + const int slot =3D 1; + + unsigned long memory_failure_pfn; + struct kvm_vcpu *vcpu; + struct kvm_vm *vm; + uint8_t *mem; + size_t size; + int fd; + + if (!kvm_has_cap(KVM_CAP_GUEST_MEMFD_FLAGS)) + return; + + vm =3D __vm_create_shape_with_one_vcpu(VM_SHAPE_DEFAULT, &vcpu, 1, __gues= t_code_read); + + size =3D vm->page_size; + fd =3D vm_create_guest_memfd(vm, size, GUEST_MEMFD_FLAG_MMAP | GUEST_MEMF= D_FLAG_INIT_SHARED); + vm_set_user_memory_region2(vm, slot, KVM_MEM_GUEST_MEMFD, gpa, size, NULL= , fd, 0); + + mem =3D mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); + TEST_ASSERT(mem !=3D MAP_FAILED, "mmap() for guest_memfd should succeed."= ); + virt_pg_map(vm, gpa, gpa); + + /* Fault in page to read pfn, then unmap page for testing. */ + READ_ONCE(*mem); + memory_failure_pfn =3D addr_to_pfn(mem); + munmap(mem, size); + + /* Fault page into stage2 page tables. */ + guest_read(vcpu, gpa, 0); + + mark_memory_failure(memory_failure_pfn, 0); + + guest_read(vcpu, gpa, EHWPOISON); + + close(fd); + kvm_vm_free(vm); + + unmark_memory_failure(memory_failure_pfn, 0); +} + int main(int argc, char *argv[]) { unsigned long vm_types, vm_type; @@ -657,4 +721,5 @@ int main(int argc, char *argv[]) test_guest_memfd(vm_type); =20 test_guest_memfd_guest(); + test_memory_failure_guest(); } --=20 2.53.0.959.g497ff81fa9-goog