From nobody Sat Jul 25 16:50:48 2026 Received: from mail-wr1-f41.google.com (mail-wr1-f41.google.com [209.85.221.41]) (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 D024A442FC0 for ; Wed, 15 Jul 2026 18:35:01 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.221.41 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1784140507; cv=none; b=EAbYM5G5YmxRHNnGy0TZJPnQgUeWNc59iPdjbEVOkMuwGsT/AxC8iBZNlwX4HSSWXKTHp6fVLmPvM/itKcBXoqFNp1THWxjpgEoJzfWnpeV6arwib9Yyse4HaU8qzwOzNbRhfDTOUvJzODQlGvBAjx+90Yc7PlkblNry1XTDUgM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1784140507; c=relaxed/simple; bh=F9FvkO6e0OOTaZLEX1jVMTYw8vMeEQxxHado2/UrHfw=; h=From:To:Cc:Subject:Date:Message-ID:MIME-Version; b=ode5OwVCgwQJqgGVsdVvAjo0rqDILfvYJ9flm71QydC2hipTuoAECTiamI1zMz7h4mcAjo3Qc8BTv4cSRsOec6HgMCR+qEMpR5CiFKlINuRydCJfs81fJn5RVYaBLPLVLELv2RRMf1GWpISmzPwUVbPtPruP0pFabK4md5UVevI= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=openvz.org; spf=pass smtp.mailfrom=openvz.org; dkim=pass (2048-bit key) header.d=openvz.org header.i=@openvz.org header.b=lNs7Iz6z; arc=none smtp.client-ip=209.85.221.41 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=openvz.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=openvz.org Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=openvz.org header.i=@openvz.org header.b="lNs7Iz6z" Received: by mail-wr1-f41.google.com with SMTP id ffacd0b85a97d-47db714766aso581181f8f.0 for ; Wed, 15 Jul 2026 11:34:59 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=openvz.org; s=google; t=1784140497; x=1784745297; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to:content-type; bh=b951GBwW4BJFY5WDUF5aftZu1xeOeok/A8gJCV5Kr+Q=; b=lNs7Iz6zYTJBaA9w7EpAKqBa9IbM47taeP7/SA/B7O9z4VLx3U7tMIZdaTmCRrUrAA oZvdJOGQn0K0gEUa9s8OR3hx/1WPoeeBDYX81s1PoBKQfhmYUfYOposXNnYHyJM7joY9 mPEoVZ+DYCOFIu0lX+l8GekgRH1jhN1SFti70Iy0KSISflo1aRIRoJVePtK0UyGZtr0f BVkeJbtUC8Qqn1HNpoy1ZyaQW5Sa0sFtiREzKT8TAmm7DC5KmqVJU+0BzgJm574LpDRv uJqJ2Px/uElt1sQy60eALgC+vxYpN6nvuM+VqcpHTMhFGcezbSbaTcWjL1k2OMf3tq7j tWCQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1784140497; x=1784745297; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-gg:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to:content-type; bh=b951GBwW4BJFY5WDUF5aftZu1xeOeok/A8gJCV5Kr+Q=; b=K2NysDcOmcVxPvbhQk2SGtSlRn8123MA2xY5bXDsujmuUQHLBrKN0GzNJqFEy9co6B ngp0S5/cJ3rBhOpjuLhukrcKYtCyLkw6IHTWCyNPvbA1lD4e4HeVH/SXSz+hN+EiMQbV I6md+xNn3YAxhfx9Ay2bsWf8TWOnV3rGn2/XP4LtpurnC827rt5C7MAhTjlWHaKqmGe8 u9HfR4fLW626P+Oa/f9KVzFESfTUZVxyvt8YUB2JGzH4ax1YXH52fIhug9Xfbr6NfAAE EzqrJd8fHdDZzhY6Xx2siBQX4tDg4WSN90CvWEKwfQ/B9zXgxELxXXFrYzPGVd0V4oAD LYmw== X-Forwarded-Encrypted: i=1; AHgh+RoptN0ibcyS5s1MhaS2pTIsdcyqSdiIFESurydvIQJQrgsP//8uz2Jx1VEflWtDK/L2koogDLwQIJyym8Q=@vger.kernel.org X-Gm-Message-State: AOJu0Yxm4ZyQbV85GWa0kK66Xkrya6/vRR4krHOegM9rdK3grklxLn+v HyNMvZ0EKet83EePaVCh00YqlbTCfWhcYJy8n2FVArryZSZghbeZ8gq8todgm9KQNZI= X-Gm-Gg: AfdE7clKvuTjeVnu0YjZNl3QflneIvXK9ij4/mhGfdp6WJAzbcja8c4GQTGyZC/JAI2 tevDJ8Er4fKAWwR6KQeAT9a0527ffpT/2fAoQo7VZyq/aOvCi4idFzZiGlFSbk2ZPI1fIRCGTM9 I5y+L6QU4aFW9erLGElaTjda8KjozvGMVMBPEAq+R7WHnZzOdDYxHYAwTnDzCqn9jfa6ChPaigk k7tkznp2Xg5bLMVoftScuLdKF5bxBocjqnBO6eLIRiE/0KxtUnT52pvqQf1HNsXfECxhfQ6Fcr8 e74B5HJFW6HBtimMXI6ugGPfxQkxl6BbUBMpvcRyfCZjubgXNLOqONqviVpDF562ePvJLdLAwXs DcyrxYLDal42kDWrSEu+Br3PZnHAQYEmvQWXM8Lgw4OOmFPMbNj8QJxFyog2cXhR5TJFS5SjVST kIhw== X-Received: by 2002:a05:6000:4187:b0:46f:8082:157d with SMTP id ffacd0b85a97d-47f567b62ccmr425650f8f.28.1784140497150; Wed, 15 Jul 2026 11:34:57 -0700 (PDT) Received: from athena ([2a06:5b06:b600:300:9de7:8ad8:9bc8:e973]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-47f464a96fdsm18632987f8f.24.2026.07.15.11.34.55 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 15 Jul 2026 11:34:56 -0700 (PDT) From: "Denis V. Lunev" To: Dave Hansen , Andy Lutomirski , Peter Zijlstra , x86@kernel.org Cc: den@openvz.org, Thomas Gleixner , Ingo Molnar , Borislav Petkov , "H . Peter Anvin" , Mike Rapoport , Juergen Gross , Kiryl Shutsemau , linux-kernel@vger.kernel.org Subject: [PATCH v2] x86/mm/pat: take cpa_lock around large-page collapse Date: Wed, 15 Jul 2026 20:34:52 +0200 Message-ID: <20260715183453.2381141-1-den@openvz.org> X-Mailer: git-send-email 2.53.0 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Loading and unloading modules concurrently on several CPUs on a KASAN build, with a short delay injected at the CPA page-table lookup to widen the window, faults within minutes: BUG: KASAN: use-after-free in __change_page_attr+0x7cc/0x7e0 Write of size 8 at addr ffff888181139718 by task modprobe ... The buggy address belongs to the physical page: pfn:0x181139 ... page_type: f2(table) cpa_collapse_large_pages() rebuilds a leaf PMD from its 4K PTEs and frees the old PTE-table pages, while __change_page_attr() fetches a PTE pointer from a lockless lookup_address_in_pgd_attr() and writes it with set_pte_atomic() only later. When module text is served from a shared large ROX mapping the two run on the same PMD: CPU A (module load) CPU B (module finalize) ------------------- ----------------------- execmem_make_temp_rw set_memory_nx __change_page_attr split 2M -> 4K table P kpte =3D &P[i] (lockless) execmem_restore_rox set_memory_rox (CPA_COLLAPSE) cpa_collapse_large_pages rebuild leaf PMD flush_tlb_all pagetable_free(P) set_pte_atomic(kpte, ...) -> writes into freed P P is a page-table page (page_type: table), reused at once, so the write corrupts whatever got the page next: a bad-pte or bad-page splat, or a fatal fault once P has been turned into read-only text. The flush_tlb_all() before the free does not close this: its IPI only serializes against page-table walkers that run with interrupts off (e.g. GUP-fast); the walk in __change_page_attr() runs with interrupts on, so nothing stops it from holding a stale pointer into P. Serialize the collapse - the PMD rebuild, TLB flush and PTE-table free - under cpa_lock, the same lock __change_page_attr() now takes unconditionally since commit ("x86/mm/pat: stop gating cpa_lock on debug_pagealloc_enabled()"), so a concurrent walker can no longer hold a pointer into a table the collapse is about to free. Fixes: 41d88484c71c ("x86/mm/pat: restore large ROX pages after fragmentati= on") Signed-off-by: Denis V. Lunev Acked-by: Kiryl Shutsemau (Meta) Acked-by: Mike Rapoport (Microsoft) --- v2: - drop the debug_pagealloc_enabled() skip and its comment: now that __change_page_attr() takes cpa_lock unconditionally, the skip is no longer needed to avoid locking only one side of the race arch/x86/mm/pat/set_memory.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/arch/x86/mm/pat/set_memory.c b/arch/x86/mm/pat/set_memory.c index e8316f5ffa8a..6dda81a629d6 100644 --- a/arch/x86/mm/pat/set_memory.c +++ b/arch/x86/mm/pat/set_memory.c @@ -417,6 +417,8 @@ static void cpa_collapse_large_pages(struct cpa_data *c= pa) int collapsed =3D 0; int i; =20 + spin_lock(&cpa_lock); + if (cpa->flags & (CPA_PAGES_ARRAY | CPA_ARRAY)) { for (i =3D 0; i < cpa->numpages; i++) collapsed +=3D collapse_large_pages(__cpa_addr(cpa, i), @@ -430,8 +432,10 @@ static void cpa_collapse_large_pages(struct cpa_data *= cpa) collapsed +=3D collapse_large_pages(addr, &pgtables); } =20 - if (!collapsed) + if (!collapsed) { + spin_unlock(&cpa_lock); return; + } =20 flush_tlb_all(); =20 @@ -439,6 +443,8 @@ static void cpa_collapse_large_pages(struct cpa_data *c= pa) list_del(&ptdesc->pt_list); pagetable_free(ptdesc); } + + spin_unlock(&cpa_lock); } =20 static void cpa_flush(struct cpa_data *cpa, int cache) base-commit: 4a0e2d0aa6a44155f9bf0289be3dd94024cee3b4 --=20 2.53.0