From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-wr1-f52.google.com (mail-wr1-f52.google.com [209.85.221.52]) (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 2FEA2360745 for ; Fri, 26 Jun 2026 16:32:18 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.221.52 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1782491539; cv=none; b=k1ElCUYLtkchi/Fi4y0D4RqP5WF5AfALDS5/lndcYeG6Pez5VUzm7gN8wUt64YYm93NlyYKzq+/sQFrW44MRQlKzESu2u8WNkOFmEZuyi/YKYAw+4qd9z84RaHX7VbWfwzlKF4/a0NuonldSrBEj7nwiRNqUzwk6aMyQOnjeUSk= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1782491539; c=relaxed/simple; bh=8Gxns+6jzDIktXIVpScVZ+0SkifOS6rER8WWhopDijE=; h=From:To:Cc:Subject:Date:Message-ID:MIME-Version; b=aIHhWUthoc8L0af0PAkWqPnJapvPInLpKfrwiYwmo3kp8J1zcJiWT8RAeSSO8SohWXaCljerM5vaf/qiru/Njf/TOdxTc8Iwtlq/oK5Le0QRd8K5LVlBG5tZczME29gTx4RGY81KgLlY5oWbe6ri++czO/sfaA8dbfcQNi3Crk4= 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=oGgplrTl; arc=none smtp.client-ip=209.85.221.52 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="oGgplrTl" Received: by mail-wr1-f52.google.com with SMTP id ffacd0b85a97d-4629051c9d1so639000f8f.2 for ; Fri, 26 Jun 2026 09:32:18 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=openvz.org; s=google; t=1782491537; x=1783096337; 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; bh=l/0ruEUU6azdlpm6QZmOW5vSfPjxnrrOFk6OP+iW2No=; b=oGgplrTlM3Bcn7tBxDAd51XRPbsV4Up7cE22IPHx37MXlr1FghY/jqJzUDiNPOuITh X2W/pc+/LFeqU/iKJ99qU2HJTf9AJIY0371YxvBAUfBdIwqDXWx4J2LeRSpSQdLvhce5 OxWYKFe1KCDHGcqBRY0ZfoFoG2cclFKfRpCemk/jZFaVemF0XDKql/hwrsiMhxJiKBGS veAJjzGk6whGE8QVbyAgsxqcEhCqGVGDt5p+Lu3sTgg2/+YHpKsgGO9zFIl5w/KRPs6P N+pQ9bygmXQn0ygRcwZL1XGZo/KK4ZClKBFHMWMtqY2fcU2pLXPB+OMu0Bdk3CsluC7W drnQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1782491537; x=1783096337; 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; bh=l/0ruEUU6azdlpm6QZmOW5vSfPjxnrrOFk6OP+iW2No=; b=G+XXZVudjVQiKAbwN+8zHe2zaYG/Q+VhVFAFHHxDaVl+aietD1bOxV0bV8aFcP5glS ag27OlDph+AP+CFjVr3KgyaM7LwaysBJR2czz9L66IvXwm/cbFeCFdeXivtnUIfblMYk L0YDB7zq+hBQb8C7gfefsePa58x+6UwNAJKtV5/QSqBUmadcWiUC4O6h4zhqhI01rPw4 4nRWzqAyJdyk5vBVcxVlVW25Y8uJffBVRUEXWIXew6DNVJvcXopbHrIJyLeLkhvH1Zv0 jGqCslistBoVf1lfI527Aaw7UQjOG1tOm1cm4hoQIlTaYp5VRHjdTcBjaRr3/oF9sXR9 H+kA== X-Forwarded-Encrypted: i=1; AHgh+RqBWU4dZV1Q3KdMctpSrm1QIw1VfZaIkZ7ghApH1gNh1XMnJHTZI6lKxM9cRPxhrc8n7BAMjFA2VSTKANQ=@vger.kernel.org X-Gm-Message-State: AOJu0YzvOh7cdqQMkIxpcbLz78HwOZnrHv3jV7ZaiXQ3O4XcWvlnv1zn E0EOBXsqyRCZWUdF6iyMVrBzNg6LCUADsVokqKKRTHCMEZmMw/u3/qH+Rj4fLHDqtQk= X-Gm-Gg: AfdE7cm8bqjWm7y3bW2V2N6NPwoza+Iy0Kkz6CcFHinC64yFak4Ac3NwRBe/DsuctdS 5POnMRG9qIKR+qEFkjFewKDG7qLbGnsUtZU83ILjIik8UKPBtZVvV+gc/3+02StiXPlcjtoNb+p n8Az9LHYi7AUevLENWlS+7RLZ0zvF1Je/R8PTkl7q4Bst3jkxnlnmh6+h2b2EBwFTt/xsb7IGLw kIBuqXm4Mpt00QtnKf1s9HnqSEwKTojEcemmd6ZIMSvNMm+p290lBD23v+mvL7Bp+PyqtsU5zG3 f0hAUanmT6WNOlS2vL3vKQHQyZYPLxCQzvNgjDE9AWv7F469Qo+ub3fInuEM2vDaNh03vJb5J67 sCo/36gtnG+FJY9fUB/3SLqzHl0E9rN9MTpCWH+I4MgPgSZM7uKG0ohWYyOgIZmqzyb1d5VoSu6 Sv9aEtQAyEIA== X-Received: by 2002:a05:6000:4b1b:b0:469:3037:37d6 with SMTP id ffacd0b85a97d-46dbed361c5mr12568993f8f.2.1782491536662; Fri, 26 Jun 2026 09:32:16 -0700 (PDT) Received: from athena.sw.ru ([2a06:5b06:b600:300:c1e0:9518:3f85:c424]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-46c221d93eesm25105863f8f.20.2026.06.26.09.32.15 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 26 Jun 2026 09:32:16 -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 (Microsoft)" , Juergen Gross , Kiryl Shutsemau , linux-kernel@vger.kernel.org Subject: [PATCH] x86/mm/pat: take cpa_lock around large-page collapse Date: Fri, 26 Jun 2026 18:32:11 +0200 Message-ID: <20260626163213.2284080-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: 8bit 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 = &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 lock __change_page_attr() takes for the split path, so a concurrent walker can no longer hold a pointer into a table the collapse is about to free. debug_pagealloc bypasses cpa_lock in __change_page_attr() (the direct map is 4K then, with no large pages to serialize), so the lock cannot order the two there. Skip the collapse in that config: it is only an optimization, and not freeing the tables leaves the unserialized walk nothing to race. Fixes: 41d88484c71c ("x86/mm/pat: restore large ROX pages after fragmentation") Signed-off-by: Denis V. Lunev --- arch/x86/mm/pat/set_memory.c | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/arch/x86/mm/pat/set_memory.c b/arch/x86/mm/pat/set_memory.c index d023a40a1e03..ff6e3f612986 100644 --- a/arch/x86/mm/pat/set_memory.c +++ b/arch/x86/mm/pat/set_memory.c @@ -418,6 +418,16 @@ static void cpa_collapse_large_pages(struct cpa_data *cpa) int collapsed = 0; int i; + /* + * debug_pagealloc bypasses cpa_lock, so __change_page_attr() walks + * unserialized and freeing collapsed PTE-tables could race it; skip + * the optional merge there. + */ + if (debug_pagealloc_enabled()) + return; + + spin_lock(&cpa_lock); + if (cpa->flags & (CPA_PAGES_ARRAY | CPA_ARRAY)) { for (i = 0; i < cpa->numpages; i++) collapsed += collapse_large_pages(__cpa_addr(cpa, i), @@ -431,8 +441,10 @@ static void cpa_collapse_large_pages(struct cpa_data *cpa) collapsed += collapse_large_pages(addr, &pgtables); } - if (!collapsed) + if (!collapsed) { + spin_unlock(&cpa_lock); return; + } flush_tlb_all(); @@ -440,6 +452,8 @@ static void cpa_collapse_large_pages(struct cpa_data *cpa) list_del(&ptdesc->pt_list); pagetable_free(ptdesc); } + + spin_unlock(&cpa_lock); } static void cpa_flush(struct cpa_data *cpa, int cache) base-commit: b81d185839fade27f7c4e885856696cf497d53c1 -- 2.53.0