From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 6170B343D75 for ; Mon, 29 Jun 2026 07:35:28 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.129.124 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1782718532; cv=none; b=i7vfDJK73S8dgEjQJPhirykY6jp9WnTh/IgV4p9Y1VsQwz/+2jc05miBP6oVvlh4BxfOGfGEaonbdgxgQtbj+d4sZPwgwH0qKmkzI8uuY6yL1SJ8TqIeu8RMt+OkVBpiKM5SaXUGtUc5Bj9aRGG7aWVG3DO+hBly5kOJzr79b+U= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1782718532; c=relaxed/simple; bh=klIDKNbhnlXm4+EZXLzePYjjEuLW5jpNa1qqh72tJFc=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=hPYtb82VPyoUFQ+gDZFk5+ZjXR/ChO2O37xDHX2zCpj66AvyaqHDOLFbZvAZVQbwcG6wo5bUO2U/ciWijPFKejXQnw+dpCb1H+JNGQvlUB5z+jLRO4MDhTxjOUHAYXYAOXV0cH4tjnMilENR2hUCWSXFv1F9VRhAij7CgqcYjN8= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=Nfue+seE; dkim=pass (2048-bit key) header.d=redhat.com header.i=@redhat.com header.b=FD+jpUje; arc=none smtp.client-ip=170.10.129.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="Nfue+seE"; dkim=pass (2048-bit key) header.d=redhat.com header.i=@redhat.com header.b="FD+jpUje" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1782718528; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=YhwD5e0TAt0qKmZwhk19yeLkiMwZuJytbMfHyxkkjWg=; b=Nfue+seEbG9Ah9TyxMBk/8o9YvmrEvfwrOCLwpP/XYtWOPuQVzf+pzjJrbTM/CJDsP7AqX NYM/Mkne1+/2xmgGLUV6M8cCiekZ0b8AkrsFoxI5ykpwu908iJUZjhz5vTxaB94RWD+Iz1 Wdwrm43XkRgx5SbjKBGXYvSnOG9IlVs= Received: from mail-pg1-f199.google.com (mail-pg1-f199.google.com [209.85.215.199]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-267-V4yXqmJoOeS8upz3pc2NQA-1; Mon, 29 Jun 2026 03:35:26 -0400 X-MC-Unique: V4yXqmJoOeS8upz3pc2NQA-1 X-Mimecast-MFC-AGG-ID: V4yXqmJoOeS8upz3pc2NQA_1782718526 Received: by mail-pg1-f199.google.com with SMTP id 41be03b00d2f7-c88cfe287e1so1640549a12.1 for ; Mon, 29 Jun 2026 00:35:26 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=google; t=1782718526; x=1783323326; darn=vger.kernel.org; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:from:to:cc:subject:date:message-id:reply-to; bh=YhwD5e0TAt0qKmZwhk19yeLkiMwZuJytbMfHyxkkjWg=; b=FD+jpUjeNGptMliRtYdstu740TnMfvFntSmL5bMBLWMaMF2ZSDAcq64ymQktVq86yW Q0PnrNl3mV+S6Og1pXUf6UmVypeIzZWXh20eZ1dy9RhR20vkSUQdrk4g6YTxKiSk3o2Y f2MM2stUHyqRzEfCxIrQUQjZPAOpC6UiQQ/NZo+8+QxVLBVzESMeIKGfw34vkcZGhpFN jNqCUAcLUVAZxCiMNKpFRuEQ/Js403eMD4FZSdbo0n6PHGOcj4yLhjlFVDWVVsiRNdX3 RhiS9uCe0YSBqwOQRjkQdTVdVrbbuqxSqp51+w27lAwJ5vLsjxezNPs0dDwpvE7ebs9v Ao4g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1782718526; x=1783323326; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:x-gm-gg:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=YhwD5e0TAt0qKmZwhk19yeLkiMwZuJytbMfHyxkkjWg=; b=f36AtJiVdwe1ywnTExdCAV8y3v3GoW2iLw9i2QJ/wEEYzFYlHHZ2U0YSi97yt1hZLC WhDqKh1/tPwNaxWNRM8c9efayoYsF1DKxvgzgb9zsjRLdmaizXOhSGwqehrlDjmKx2lw klWvwqSJDIH1DAhp11baKY5iXe0+ll2yPZ5qVE2olks7Kylf/rDHnssxahseDyK75V3E z+/AYDLrHk7ot757YmICWy0/U6ZAJO4xDEG7YftvwCalsl+XLanzzQn3aZav0V7VKJK4 vncOvnxAArT4YcbRuQAaOS56qMVQmh274VZMQwnp1eBNrwPuxT80eASEIbxq+hWOy3No uM7g== X-Gm-Message-State: AOJu0Yzf38HydMTSqh3HuWEg0NKYyzhUxBzb4tP16iSD+wibyhO/d4OH awvAxwRN1Dm9cDf12+rZ7Ayjl4Wp/4PJDYyYgX3wCvU3fknwPDe8WYBwyTaHF/yaShPGvVwsksJ 4jctIbv4owAjYKYMww+U90EZrZo66VMJwWG63Hc6+5azuEZhDwEvC3Q83S/5ahb5PNA== X-Gm-Gg: AfdE7cnymzR8XoyEkNAm04r26ajEK71ZAHubQV7XdVEeAPGCH9CQT2ffX+xlgHX1Go+ 91982Vk0j5WmYdFx0FNEOZvb0rEm0UZfSnmw19w1UxIWTvdavkApWNJafoWWBkw5onjmT5T0Hh3 oCNQSVdjW9W6IN8EXBSJUPftY33EpPQeOJhvXaLLXLFCvrUr2vImCScY2OOqSyRtthfPC0F0UbY 8rsVV9bkPOAvTnFZ+B1CG/x+SwE4cHS7q/S74AplDQWESHvhEkRgtoEeOXwtihA23wQqAobgjem E1wTfMfEParN+FxOkcpt+CY4RReIraeRHDFDkX74mVpzJfs/aCwPSUvoM8Yk5duvCL8lwYc5bLe 1CbL6TBsQ/HtLUMiLRbk1HBYoPNRLsA4+ X-Received: by 2002:a05:6a21:a08:b0:3bf:79d6:f063 with SMTP id adf61e73a8af0-3bf79d740bdmr5424842637.43.1782718525443; Mon, 29 Jun 2026 00:35:25 -0700 (PDT) X-Received: by 2002:a05:6a21:a08:b0:3bf:79d6:f063 with SMTP id adf61e73a8af0-3bf79d740bdmr5424801637.43.1782718524686; Mon, 29 Jun 2026 00:35:24 -0700 (PDT) Received: from redhat.com (IGLD-80-230-85-71.inter.net.il. [80.230.85.71]) by smtp.gmail.com with ESMTPSA id 41be03b00d2f7-c92bd55985asm7323848a12.31.2026.06.29.00.35.04 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 29 Jun 2026 00:35:24 -0700 (PDT) Date: Mon, 29 Jun 2026 03:34:59 -0400 From: "Michael S. Tsirkin" To: "David Hildenbrand (Arm)" Cc: linux-kernel@vger.kernel.org, Miaohe Lin , Naoya Horiguchi , Andrew Morton , Oscar Salvador , Andi Kleen , Hidehiro Kawai , Rik van Riel , Vlastimil Babka , Lorenzo Stoakes , "Liam R. Howlett" , Mike Rapoport , Suren Baghdasaryan , Michal Hocko , Brendan Jackman , Johannes Weiner , Zi Yan , Baolin Wang , Nico Pache , Ryan Roberts , Dev Jain , Barry Song , Lance Yang , Christoph Lameter , David Rientjes , Roman Gushchin , Harry Yoo , Hao Li , Kiryl Shutsemau , Byungchul Park , linux-mm@kvack.org, linux-cxl@vger.kernel.org Subject: Re: [PATCH 0/2] mm: memory-failure: fix HWPoison flag race with non-atomic page flag ops Message-ID: <20260629030657-mutt-send-email-mst@kernel.org> References: <0b5f8b4b-d7dc-4b79-9555-a5b36265f3a9@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <0b5f8b4b-d7dc-4b79-9555-a5b36265f3a9@kernel.org> On Mon, Jun 29, 2026 at 08:49:37AM +0200, David Hildenbrand (Arm) wrote: > On 6/28/26 23:45, Michael S. Tsirkin wrote: > > I don't like it that we are adding overhead to the good path for > > the benefit of memory failure, which never triggers on many systems, > > but I don't have a better idea. Pls take a look. > > As I said on Friday. > > "It's also doesn't address the mf_mutex implications and the x86 thingies I > mentioned. Well I did attempt addressing this. These would be these two: (a) We don't hold the mf_mutex on all call paths, but we really need it so a page_test_set_hwpoison() cannot race in weird ways with the other primitives I think. page_test_set_hwpoison was this code you wrote: +static void page_set_hwpoison(struct page *page) +{ + lockdep_assert_held(&mf_mutex); + + while (!PageHWPoison(page)) { + SetPageHWPoison(page); + + /* Make sure concurrent non-atomic writers completed. */ + synchronize_rcu(); + } +} and indeed the test+set combination seems racy. But consider the version I posted, for example: +/* + * Drain any in-flight non-atomic page flag operations that could + * clobber a concurrently set HWPoison bit. Retries until the bit sticks. + */ +static void set_hwpoison_drain_rcu(struct page *p) +{ + do { + synchronize_rcu(); + } while (!TestSetPageHWPoison(p)); +} + ... +static bool test_and_set_hwpoison_drain_rcu(struct page *p) +{ + bool was_set = TestSetPageHWPoison(p); + + set_hwpoison_drain_rcu(p); + return was_set; +} does not seem racy without a lock. But maybe I don't get it. (b) There are some leftover SetPageHWPoison etc. instances. The ones in arch/x86/kernel/cpu/mce/core.c likely cannot grab the mutex, but maybe they are corner cases either way and we can document the situation. Well, I did try to document the situation - it's in the commit log for patch 1: Note: the MCE handler in arch/x86/kernel/cpu/mce/core.c also calls SetPageHWPoison() and is subject to the same race. It cannot use the drain helpers (MCE context cannot call synchronize_rcu()). For recoverable MCE errors, memory_failure() is queued via work items (kill_me_maybe/kill_me_never) and will re-set the bit via test_and_set_hwpoison_drain_rcu() if it was clobbered. The mce_panic() path sets HWPoison for kdump right before panic() so the race is irrelevant there. The MCG_STATUS_SEAM_NR path does not queue memory_failure(), but the affected page belongs to a TDX guest whose CPU core has already been marked dead - the page is not subject to concurrent non-atomic flag operations in the buddy allocator, so the race does not apply. > ... > > I'll either take care of that myself or find someone that can work on this with > attention to all details. > " > > This is nothing to vibe-code. This needs a real expert. Well I had this sitting on the disk anyway, so I thought I'd post. I wouldn't call this vibe-code - a bunch of manual work went into this, llms mostly as a grep/sed replacement. But hey. I don't object to someone taking over, for sure. Was fun, and maybe these patches will be helpful as a starting point. In particular, maybe I should have been more explicit about how your points from Friday are addressed. If you want to add a bit more to explain the exact concerns here, for whoever works on this next, feel free to do so. > > > > Non-atomic page flag operations (page->flags.f &= ~mask, __set_bit, > > __clear_bit) can race with atomic TestSetPageHWPoison() in > > memory_failure(). The non-atomic RMW reads flags, memory_failure() > > atomically sets HWPoison, then the RMW writes back the old value > > without HWPoison, clobbering the bit. > > > > The race was confirmed by injecting a cpu_relax() delay between the > > load and store of the non-atomic RMW in __free_pages_prepare, then > > running concurrent MADV_HWPOISON injection. The clobbered HWPoison > > bit was observed repeatedly. > > > > This series fixes the race by: > > > > 1. Having memory_failure() call synchronize_rcu() + retry after > > setting HWPoison, so that any in-flight non-atomic RMW that > > read the old flags value completes before we proceed. > > > > 2. Wrapping all non-atomic page flag operations in > > rcu_read_lock/rcu_read_unlock (CONFIG_MEMORY_FAILURE only), > > so that synchronize_rcu() actually drains them. > > > > Performance impact (page alloc+free microbenchmark, 200K iterations, > > 20 runs, KVM guest, error bars are 3-sigma): > > > > !PREEMPT_RCU (x86): > > insns/iter cycles/iter > > base: 12237 +/- 1 17954 +/- 136 > > patched: +22 +/- 1 -124 +/- 122 > > (+0.18%) (within noise) > > > > PREEMPT_RCU: > > insns/iter cycles/iter > > base: 12512 +/- 3 18541 +/- 214 > > patched: +95 +/- 3 -12 +/- 161 > > (+0.76%) (within noise) > > > > When !CONFIG_MEMORY_FAILURE, all wrappers compile away completely. > > > > Suggested-by: David Hildenbrand > > No ;) > > -- > Cheers, > > David