From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.222.177 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796275650; b=L70Cx3F6UyEi5D6/8fjcDu1bVOIl8YaFyl3LUh8SwxEAvUl+fFc4ArGp78+4/jQc0Pr/ Iq8gFbiKSo9tlgRKy08vDxuQ0tH7OqmtL0ELqBNbYZ4IBaF7XUThHtdSe0T50KEdrQxxA ltC2DKAj1ivJuEnTCOwrAzOp9SoPFH8CodIMDsvmZfeTCVWhDzyizjl4Tc7Cmt4Ekp448 r5OKnPGeM04oZ89tB2SGFmRSK7LaNSrhy9kE2no0I/X38kxmkQUoOSHbhRHYF1RmePAzx 77bl3kUTbyYKoVkcmZ6Ma0/WGiK3sBw8JWHw9CbkpdTgfddHpW18OeYhg1RWNmSNuHQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796275650; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Forwarded-Encrypted:X-Gm-Message-State:X-Gm-Gg:X-Received:Received: MIME-Version:Content-Type:Content-Transfer-Encoding:Subject:From:To: Cc:In-Reply-To:References:Date:Message-Id:X-Mailer; bh=3oAE/g5lTSCNAO/STdfn2kdRtUI6uMRb7d/xOZwFAq4=; b=VQGbYHlUcm4HxygsoujfmVxkzM4iw3vyWwRw32rsOkKr/XgvkMe0VHzsrUTXLlejJIJp p5EJQ1QFhP/xC3twa2SPpzq7hPx/yrBj4Lt0tSpck1k3ydwMwTJYrK3u+ScxEPG1ho+HX gYHic1KivLHvJ14553vQL2x68/Hwona4WOYvmavat0t3Fg4daEWbHAwVWDwwJwMPMfUAn 8llZJYP7HoyjJrmQ/euqKTzGtl7YYWhYbofVu+cSP/y7Lz5MwaCUqD9i7ScDG5ybxuHwi 2fvsOzGuTv4ipnPLv+KDk4vrWFuw64qB0gAVtUqhqrPBF5qKfimKjayoWhW6kxIUzLg== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none header.from=gourry.net; spf=pass smtp.mailfrom=gourry.net; dkim=pass header.d=gourry.net header.i=@gourry.net header.a=rsa-sha256 header.s=google header.b=bPi3RfbV; arc=none smtp.remote-ip=209.85.222.177 Received: from mail-qk1-f177.google.com (mail-qk1-f177.google.com [209.85.222.177]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 21492606A32C for ; Thu, 3 Dec 2026 05:27:28 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 21492606A32C Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=gourry.net Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gourry.net Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gourry.net header.i=@gourry.net header.a=rsa-sha256 header.s=google header.b=bPi3RfbV Received: by mail-qk1-f177.google.com with SMTP id af79cd13be357-94959217274so1376111285a.0 for ; Wed, 02 Dec 2026 21:27:28 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gourry.net; s=google; t=1796275648; x=1796880448; darn=vger.kernel.org; h=message-id:date:references:in-reply-to:cc:to:from:subject :content-transfer-encoding:content-type:mime-version:from:to:cc :subject:date:message-id:reply-to:content-type; bh=3oAE/g5lTSCNAO/STdfn2kdRtUI6uMRb7d/xOZwFAq4=; b=bPi3RfbVxrqnHibnypnaf8dQJp5gXzpKE3C/aJPzdCR3D29SppRNuPqWC2vf7y5mwF zAYfDok07os8Zn+f+EWX81EFHMz+5zUwTAV6BVvBqP3QLSa3GZ9C/rY0lFtQdos5x3nH yja+VVO3KVr0J7+DuGhXGyFMdy6ohToUS200yRAY2rZEPa+j8L9sneBn8Qe6GzI899tZ 7uKLXXUtJbbXRgWJESPcOHnD8thNDHwvPtUMRcZ0Hd+Jgm6XOvXNgEyH5NiP3v+6RkS0 HISPo/cQQtvglXF9o3OvMtQqW4mmYOAxIF9pCF3JyOYGiKNCjmgvh/njIueIP7ChTdxS C7VA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796275648; x=1796880448; h=message-id:date:references:in-reply-to:cc:to:from:subject :content-transfer-encoding:content-type:mime-version:x-gm-gg :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to :content-type; bh=3oAE/g5lTSCNAO/STdfn2kdRtUI6uMRb7d/xOZwFAq4=; b=hSGKYKHECJWiEueThLzeh3J84sYzGIYKBVPkbNlH4tucqf/B6d1PErnmb0ZdJH1tJ0 jwCal/bL+T6d3GwJAk/VWBlYYlRrtlBY3baz7xxMFJiFri1kI3b2OCfU9XIuCdhuc2h5 Ra3g3NVg7cgbsUg5PTjsbzL5DlcbiqykegPHzaonGUEdEmkvTaWLAwh5MIhvfP/+AiNM uedK97VnyZiyobf9V00po8ES1MRLZ9Ja78gSYxHh2ohCij0EGlJ4OJ8IoOI5XjRG/eWW 165I+mI2yFagco74r0dwLPkcFX+ueJECDxNDij79hO+PqL61fR/SpMXyw9s0Ew0h+9ia tvbw== X-Forwarded-Encrypted: i=1; AL+hErHgrkaQp6IL1x5YP9LYLLcPuxZMn4MMWNc04Lmf6vRv0lcND6wACNXWNHUmuwB06RxtqEtmkIAJWqfKpoQ=@vger.kernel.org X-Gm-Message-State: APuU4Z8BKqXob7Tch+bnbOD5DShoGYk49RGUqAdlcirbrCNwyFy0pXDu FZIsskgJ3xtdZ5bzwhbJuQlaaFuv3o1FJrIuShE/d1ZzKE/6e6Fr77FlXNWHB4lfu9E= X-Gm-Gg: AZFdTZfc9FaXYJ+psuHaWU5HGqKYi2Z0iJaqLdT7Hzu3+WZE5yato26usLq2kgT23df bC0FjHrlNYuV+xOoh8PrZMPkU7I905a1Occd6IkDqjMV7jsmFL9ArJigRY11UYJF0c5dh8zYHZ5 kjCQSkSiwPorb0UMzB0kRoZ/ExMwfE8yUyZYEYbuXbnujHrk6HERwupTUdbGkQY7EpZld5ct4rz Pb8cOq9WEslruH3ZvuWfY1nv6XNrNER8WwP87uUoSjncMtQj7VKI1A9OOyRH9t3gh2TCrl7K3a4 BBUga3Ce6ogvONQRuv90K6aZ6RplUvT8xXVXSXue9Myb/ziaD6uiXnBUuokfBkNPzUPjGaSwUHj l3/t7VH5bfpIzrKH6eLccc8BNbVktz+Ww53n9KWRlyszRDumA3bvqsk2GZ3B+HkNAa/Rg4jHCHG z/zbCHP677tPnE8I265Okv1UBga6xa4OXvC+8zwczd54Loz5ubyjZckqaOqvYbgbuz9qmkBbiN+ nFm+AEfVioA/1QBE43yYl41K6Ye0p6+kasI2qn+hIjwlif0F307u5+QtVHNqwTVEE2DTEIQdziL QB/ZQPbtKZ3J08zRqXJ4mdgp5vqyA/xlg/rGX/evqcr8YuifwXyS0hB1QKd0VAJwwSKM9BFF2ma KJf7AP+I= X-Received: by 2002:a05:620a:6494:b0:93e:bdeb:f067 with SMTP id af79cd13be357-949ba9fdbb5mr7335469785a.48.1796275647941; Wed, 02 Dec 2026 21:27:27 -0800 (PST) Received: from [192.168.50.231] (pool-71-191-145-202.washdc.fios.verizon.net. [71.191.145.202]) by smtp.gmail.com with ESMTPSA id af79cd13be357-94a0175c67fsm2250746485a.31.2026.12.02.21.27.25 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 02 Dec 2026 21:27:27 -0800 (PST) X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit Subject: Re: [PATCH 29/32] mm/ksm: use a migration counter to detect KSM folio migration From: "Gregory Price (Meta)" To: "David Hildenbrand (Arm)" Cc: Hugh Dickins , Baolin Wang , Andrew Morton , Mike Rapoport , Peter Xu , Michal Hocko , Kiryl Shutsemau , Manfred Spraul , Davidlohr Bueso , Cyrill Gorcunov , Zi Yan , "Liam R. Howlett" , Nico Pache , Ryan Roberts , Dev Jain , Barry Song , Lance Yang , Usama Arif , Lorenzo Stoakes , Rik van Riel , Vlastimil Babka , Harry Yoo , Jann Horn , Yang Shi , Zach O'Keefe , Song Liu , David Stevens , Kefeng Wang , Alex Williamson , Suren Baghdasaryan , Dave Jiang , Matthew Wilcox , Miaohe Lin , David Rientjes , Shakeel Butt , Jan Kara , Mel Gorman , Jason Gunthorpe , John Hubbard , Gerd Hoffmann , Vivek Kasireddy , Dave Airlie , Matthew Brost , Joshua Hahn , Byungchul Park , Gregory Price , Ying Huang , Alistair Popple , Hagen Paul Pfeifer , James Bottomley , Felix Kuehling , Alex Sierra , Balbir Singh , Kairui Song , Qi Zheng , Axel Rasmussen , Yuanchu Xie , Wei Xu , Baoquan He , Oscar Salvador , Muchun Song , Dan Williams , Rakie Kim , Hao Li , Christoph Lameter , Roman Gushchin , Sebastian Andrzej Siewior , Clark Williams , Steven Rostedt , Alexei Starovoitov , Xu Xin , Chengming Zhou , Shivank Garg , linux-mm@kvack.org, linux-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-cxl@vger.kernel.org, linux-rt-devel@lists.linux.dev, SJ Park , Chris Mason In-Reply-To: <20261129-fixes_core_3-v1-29-f66094116801@kernel.org> References: <20261129-fixes_core_3-v1-0-f66094116801@kernel.org> <20261129-fixes_core_3-v1-29-f66094116801@kernel.org> Date: Thu, 03 Dec 2026 00:27:25 -0500 Message-Id: <179627564549.2841792.13187972375134523049.b4-reply@b4> X-Mailer: b4 0.17-dev On 2026-11-29 11:07 +0100, David Hildenbrand (Arm) wrote: > ksm_get_folio() holds no reference on the folio referenced by > stable_node->kpfn. If the folio at stable_node->kpfn is no longer our KSM > folio, it was either freed or migrated. When freed, we can drop the > stable node; when migrated, we have to look again for the migrated > folio. > > However, a second migration might have migrated the KSM folio back to > the very same page in the meantime: stable_node->kpfn would be > effectively unchanged and the KSM folio is still alive. > > Consequently, we might remove the stable node of a KSM folio that is > still mapped, resulting in a use-after-free of the stable node, for > example, in rmap_walk_ksm(). > > Let's fix it by storing a migration counter in the upper bits of > stable_node->kpfn, incremented on each migration of the KSM folio. The > existing check whether the value changed will then also detect a KSM > folio that migrated back, and a freed KSM folio will still be detected > as such. > > As a pfn never needs more than 64 - PAGE_SHIFT bits, convert > stable_node->kpfn to a u64, and use the remaining PAGE_SHIFT bits (at > least 12) for the counter. A wrap-around would require 4096 migrations > of the KSM folio while ksmd is between two reads in ksm_get_folio(): > not relevant in practice, because even two migrations back and forth > would be hard to hit. > > This change doesn't grow the size of a stable node on 64-bit, which is > what we care about. We don't care about 32-bit and likely we should make > KSM 64-bit-only soon. > > Note that on 32-bit, the pfn will always reside in the lower 32 bits, > and every update changes the upper 32 bits, which hold the counter. So > even if we read a value torn by a concurrent update (2x32-bit accesses), > the pfn will be valid, and due to the existing barriers, the value > cannot look unchanged after a migration. > > Rename stable_node->kpfn to stable_node->kpfn_seq to catch all users, > and add helpers to access the pfn. > > This has low urgency, as it should be extremely hard to hit in practice. > As this was not reproducible and wasn't reported so far, and the change > is not trivial to backport, let's not CC stable. > > Fixes: c8d6553b9580 ("ksm: make KSM page migration possible") > Reported-by: Chris Mason > Signed-off-by: David Hildenbrand (Arm) Did finally reproduce the UAF. It was very convoluted and required root to get there. I don't love the new counter but i don't have a better suggestion so bleugh. Tested-by: Gregory Price (Meta) Reviewed-by: Gregory Price (Meta) -- ~Gregory