From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mta1.migadu.com (out-131.mta1.migadu.com [95.215.58.131]) (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 7BEDC2C11CF for ; Sat, 15 Aug 2026 18:16:42 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=95.215.58.131 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1786817806; cv=none; b=tW7U7XnTxG/W1Q8eWbUZ/TMilTEJAPKL8tg5HyaLZxcVJJa1k3ssQMoaknuYejlaA/oGPYoNp0rNBTXXbYYEOwsL7GtXNlIylawfhj5uAkgRLB4T2xVYVb6/tMiIR3QlF1/8rjpmYMbOhRr9S2tboTNtk2qtw7fLl6Eppw+WDrA= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1786817806; c=relaxed/simple; bh=sxz3FbT3P3NUYsxm7p5UauLMyNdZFqQ91NsLQoVFsio=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version:Content-Type; b=dK+LUrbyXjQ1KrzHnH7gqqi0gw1arM0dT3yOGySE/h96j7YfK4TTUTsD06XXGVCDpoHAWmT+mcg7b8zWKp6+ccYICybblyf0vA0vzPHOGr1p1TREi3fpx/cwrftQYhixLCADtb5/jhLGOErgShZfhK6pKRCrHQLHWJksAQd8uoc= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev; spf=pass smtp.mailfrom=linux.dev; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b=GIQa/i3B; arc=none smtp.client-ip=95.215.58.131 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.dev Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b="GIQa/i3B" X-Envelope-To: linux-kernel@vger.kernel.org DKIM-Signature: a=rsa-sha256; bh=sxz3FbT3P3NUYsxm7p5UauLMyNdZFqQ91NsLQoVFsio=; c=simple/simple; d=linux.dev; h=from:to:subject:date:message-id:mime-version:content-type; s=key1; t=1786817801; v=1; x=1787422601; b=GIQa/i3BJ+fCWkaB9FqBDEdFyF/+LX5y0ZszsKT0Mc2Gq8oyusHmKjgO1V0olpgq1pm62sai QmnFiBJji0IcSZlBDLYle4fdfMqgcPN0EgZbp+B6VrAUq2hfCRBWEB87cSunuTE4CafZu2CVmAK ++tMM0wwxK27LgtAq2O4tOas= X-Envelope-To: linux-kernel@vger.kernel.org Received: from localhost (2602:fce1:44f:115e::) by smtp.migadu.com with ESMTPS id 18741eb5a36b713c; Sat, 15 Aug 2026 18:16:40 +0000 X-Migadu-Flow: FLOW_OUT From: Lance Yang To: baolin.wang@linux.alibaba.com Cc: vernon2gm@gmail.com, akpm@linux-foundation.org, david@kernel.org, ljs@kernel.org, nico.pache@linux.dev, ryan.roberts@arm.com, dev.jain@arm.com, baohua@kernel.org, usama.arif@linux.dev, zokeefe@google.com, linux-kernel@vger.kernel.org, linux-mm@kvack.org, Lance Yang Subject: Re: [PATCH v2 0/3] mm: khugepaged: fix tracepoint UAF Date: Sun, 16 Aug 2026 02:16:32 +0800 Message-Id: <20260815181632.21453-1-lance.yang@linux.dev> X-Mailer: git-send-email 2.39.3 (Apple Git-146) In-Reply-To: <20260815174444.4164-1-lance.yang@linux.dev> References: <20260815174444.4164-1-lance.yang@linux.dev> 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=UTF-8 Content-Transfer-Encoding: 8bit +Cc Baolin On Sun, Aug 16, 2026 at 01:44:44AM +0800, Lance Yang wrote: > >On Sat, Aug 15, 2026 at 01:19:21PM +0800, Vernon Yang wrote: >>From: Vernon Yang >> >>The khugepaged tracepoints take a folio pointer and call folio_pfn(), >>but by then the folio may no longer be valid: freed after folio_put(), >>folio_unlock() or pte_unmap_unlock(), or not a folio at all but an >>xarray-encoded swap entry. On classic SPARSEMEM, dereferencing it oopses >>khugepaged as soon as the trace event is enabled; on other memory models >>it merely prints a bogus pfn. >> >>Pass the pfn to the tracepoints directly, captured while the folio is >>still pinned, closing the use-after-free windows in >>mm_khugepaged_scan_file(), mm_khugepaged_scan_pmd() and >>mm_khugepaged_collapse_file(). > >Well spotted! > >Gave the series a run on x86_64 (KVM), all good (only classic SPARSEMEM >untested) :) Hmm ... stumbled over something else while testing this ... With tmpfs mounted huge=advise, one MADV_HUGEPAGE isn't enough to get an unregistered mm onto khugepaged's list. Do it twice, and khugepaged starts scanning right away. The pending flags make it into khugepaged just fine: int hugepage_madvise(struct vm_area_struct *vma, vm_flags_t *vm_flags, int advice) { switch (advice) { case MADV_HUGEPAGE: *vm_flags &= ~VM_NOHUGEPAGE; *vm_flags |= VM_HUGEPAGE; ... khugepaged_enter_vma(vma, *vm_flags); break; ... } return 0; } and survive the common eligibility check: unsigned long __thp_vma_allowable_orders(struct vm_area_struct *vma, vm_flags_t vm_flags, enum tva_type type, unsigned long orders) { ... /* * Enabled via shmem mount options or sysfs settings. * Must be done before hugepage flags check since shmem has its * own flags. */ if (!in_pf && shmem_file(vma->vm_file)) return orders & shmem_allowable_huge_orders(file_inode(vma->vm_file), vma, vma_start_pgoff(vma), 0, forced_collapse); ... } But then the shmem helper reads them back from the VMA: unsigned long shmem_allowable_huge_orders(struct inode *inode, struct vm_area_struct *vma, pgoff_t index, loff_t write_end, bool shmem_huge_force) { ... vm_flags_t vm_flags = vma ? vma->vm_flags : 0; ... } At that point vma->vm_flags still has the old value, so huge=advise quietly gives us no allowable order. First madvise still succeeds, of course. The second one works because it finds VM_HUGEPAGE already installed by the first call. Looked at history too ... we've been here before. 2cf1338454a8 ("mm: fix khugepaged with shmem_enabled=advise") fixed this exact ordering bug and tagged cd89fb065099 as the culprit. Then 6beeab870e70 was meant to be "No functional changes", but moving shmem_huge_global_enabled() into shmem_allowable_huge_orders() seems to have wired the stale vma->vm_flags read back in. So AFAICT, this regressed in v6.12 with 6beeab870e70. @Baolin, does that ring a bell? Any reason shmem_allowable_huge_orders() can't just take the pending vm_flags as well? Cheers, Lance