From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796380465; b=4NcVHUl32MfTxrpzHgG+A+dr7686RHChe7BXYq+92teVJ0omDdtCHsCyKdffNHr0LU5L WCXSK1Kcnwxmnwz4Ck16h1zOiiJeKQsEdzD3mVNCJ3KO2FAjymL/E7psbZj2HR6UUD8Ip 1WEc8GYfI5mtm7JQ9EtWujVXOfbeP+eo2Ho023mc6xRwKgbBk06ljmaVt1db4zPGxCEPV blNKioK/03PoyqAdterbMLr8UFKF/rbUKipYjmrTcEv5vS7qjo0ah/+meULnsvWYmcVbA jlJxD2dvpKTfmhL+g3MmHXGfVIjCAofTRVyt+G3+8P2A8sPIEQer6BXF9D/ypmQcDdQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796380465; h=Received:DKIM-Signature:From:To:Cc:Subject:Date:Message-ID:X-Mailer: In-Reply-To:References:User-Agent:X-stable:X-Patchwork-Hint: MIME-Version:Content-Transfer-Encoding; bh=Whbq5T0/L0TuuD0A0bGn2C1RcaLb69s8xC7JJyBwmNw=; b=eU53/+mlHyUtREG1aqXeSiXYMw2gX8ysa1QikydSByZ7VJ6anYXMzUrKKbxMV4mqilHM baX8w0IJHg+K5aeOgSvEwdQIBsAv44y0GvCsvzYR8j6kW7sZorzMubWvEZa5jGP1ATjkC ZmgXl7SKxdzx4Nql2UVefTKpRpaLYmu0PvPySfN7ZRTL1re5wESPhpU6NaJ3R2jp9Acnh JuJXexF6twfj7v7v/OBT4zaMe29MteNtlwF2cLdv5EG1/dlsVJ2qj6dWrawTlBt5rw6oH 1nuLrLK+H5pnNH0AK3bjT0eiyem9GN7GCKhGeZa3Aea2yH9jzyMr7aOdtCjRpbxhrCw== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=linuxfoundation.org header.i=@linuxfoundation.org header.a=rsa-sha256 header.s=korg header.b=TQZAQZYw; arc=none smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id BD51D606A69B; Fri, 4 Dec 2026 10:34:25 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linuxfoundation.org header.i=@linuxfoundation.org header.a=rsa-sha256 header.s=korg header.b=TQZAQZYw Received: by smtp.kernel.org (Postfix) with ESMTPSA id 3C5CF1F00897; Fri, 4 Dec 2026 10:34:25 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linuxfoundation.org; s=korg; t=1796380465; bh=Whbq5T0/L0TuuD0A0bGn2C1RcaLb69s8xC7JJyBwmNw=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=TQZAQZYwWpONDCp33ktZpBl6xMJOwZlaEcYemArq5fTAgpOi3ySgYZV8rEh2eOn/i J/A+8jtHofhQplOoE+jRg0ljb6K+SxF03bLV6lHPt5iCByBBKNxoU7IVPI3zWxVLL1 HAjDT/UNBojkFxuiXlmqNDCRRXyo4wqTr0m81qiQ= From: Greg Kroah-Hartman To: stable@vger.kernel.org Cc: Greg Kroah-Hartman , patches@lists.linux.dev, Alexei Starovoitov , Sasha Levin Subject: [PATCH 7.3 285/742] bpf: Flush TLB when a page of arena replaces the scratch page Date: Fri, 4 Dec 2026 19:02:29 +0900 Message-ID: <20261204100642.614912873@linuxfoundation.org> X-Mailer: git-send-email 2.56.1 In-Reply-To: <20261204100635.034361255@linuxfoundation.org> References: <20261204100635.034361255@linuxfoundation.org> User-Agent: quilt/0.69 X-stable: review X-Patchwork-Hint: ignore X-Mailing-List: stable@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 7.3-stable review patch. If anyone has any objections, please let me know. ------------------ From: Alexei Starovoitov [ Upstream commit 16829046f0a0df75fe0a0b937771ffa301b650f7 ] A fault of a kfunc maps the scratch page. An allocation at that address puts a new page in its place. flush_tlb_before_set() is empty on x86, so all cpus that have touched the address keep the scratch page in TLB. The program reads junk from the page that it has got, and its writes are lost. The flush takes an IPI. Do it after the lock is dropped when the caller can sleep. Otherwise fail and free the range: the work unmaps the scratch page and flushes TLB. The range is taken until then. As with any deferred free, it stays taken when kmalloc_nolock() of the span fails. Fixes: f64c723741c9 ("bpf: Replace scratch PTE atomically when allocating arena pages") Signed-off-by: Alexei Starovoitov Link: https://patch.msgid.link/20261125032938.670542-23-alexei.starovoitov@gmail.com Signed-off-by: Sasha Levin --- kernel/bpf/arena.c | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/kernel/bpf/arena.c b/kernel/bpf/arena.c index 10fa8e7f3fa6a..f1958196c7a8d 100644 --- a/kernel/bpf/arena.c +++ b/kernel/bpf/arena.c @@ -149,6 +149,8 @@ struct apply_range_data { struct bpf_arena *arena; struct llist_head *pages; int i; + bool can_flush; /* the caller can flush TLB of all cpus after it drops the lock */ + bool flush; }; struct clear_range_data { @@ -189,6 +191,12 @@ static int apply_range_set_cb(pte_t *pte, unsigned long addr, void *data) continue; if (WARN_ON_ONCE(pte_page(old) != d->arena->scratch_page)) return -EBUSY; +#ifndef flush_tlb_before_set + /* The scratch page is in TLB, and it takes an IPI to flush it */ + if (!d->can_flush) + return -EBUSY; + d->flush = true; +#endif ptep_get_and_clear(&init_mm, addr, pte); flush_tlb_before_set(addr); } @@ -571,6 +579,7 @@ static vm_fault_t arena_vm_fault(struct vm_fault *vmf) } llist_add(&new_page->pcp_llist, &pages); data.arena = arena; + data.can_flush = false; data.pages = &pages; data.i = 0; @@ -802,6 +811,8 @@ static long arena_alloc_pages(struct bpf_arena *arena, long uaddr, long page_cnt goto out_memcg; data.arena = arena; + data.can_flush = sleepable; + data.flush = false; data.pages = &pages; data.i = 0; @@ -832,17 +843,18 @@ static long arena_alloc_pages(struct bpf_arena *arena, long uaddr, long page_cnt flush_vmap_cache(kern_vm_start + uaddr32, mapped << PAGE_SHIFT); raw_res_spin_unlock_irqrestore(&arena->spinlock, flags); + if (data.flush) + flush_tlb_kernel_range(kern_vm_start + uaddr32, + kern_vm_start + uaddr32 + (mapped << PAGE_SHIFT)); addr = clear_lo32(arena->user_vm_start) + uaddr32; goto out_memcg; out_unmap: - /* Error handling: Undo partial mappings. */ + /* Error handling: Undo partial mappings, unmap the scratch page. */ flush_vmap_cache(kern_vm_start + uaddr32, mapped << PAGE_SHIFT); - range_tree_set(&arena->rt, pgoff + mapped, page_cnt - mapped); raw_res_spin_unlock_irqrestore(&arena->spinlock, flags); - if (mapped) - arena_free_pages(arena, uaddr32, mapped, sleepable); + arena_free_pages(arena, uaddr32, page_cnt, sleepable); out_free_pages: /* Error handling: Free back any unmapped pages. */ -- 2.53.0