From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.128.52 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796178921; b=e4m7iG/mvf58YFZNygYACCiLLyzXPdKaYWwKsI9rNVb+0L/G+Q/OCvUZOFvbjrr0kWv5 C9TqZ8zDxqt4RshiZzFPG3MRieIEpZL9Xxg3q7wvDe/yHZVqA9YaKsfj7GpFCpLvbpX51 zeTKYIOIJMHsU2eI1u0eT0Kn39266vo1g6Rkv1jam+aGvmoldhCLaNfZQ6t7YykMMjwfc 2T+1WqkCAwvqtBDPLeqPFs9nvZnd03Tlvk8fTp223wJFA7SUSIxOzio7HGV/T5Yez4w2o /oAkNualNxaXvo/T+8o0G+soO7Rp4Ts759VedBmPGRaCQTubsHD/1vKutr0338267Ag== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796178921; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Forwarded-Encrypted:X-Gm-Message-State:X-Gm-Gg:X-Received:Received: From:To:Cc:Subject:Date:Message-Id:X-Mailer:MIME-Version: Content-Transfer-Encoding; bh=fGFj/1CKw77FDNsA6i4gP/GtNHeQOiJNKuy2lM4LenU=; b=Dj96ZQ56J9jZ1b8WAlb6Lt8ACtwMo5cs4V5gBDrFFzjfCymHBWg5sHI6HjJGBAbPxXKl Fbl5rLdw3KorD+ot4HS0ivZ+30H8sUYHq4WU30IqQDvbj3Ozif5Dg/vXRBrOacF+KxcZN piLSxqjhpUdWx0rDoKRjfi2QRjlUSvP9fnLJGYgeDBgysGN2fgBrLBgcyH6wuhCUYN+fD WIMiCcqt5/AnbUebKLY4kLAQPqN63EEyJSmlok0JH4KraRgxTiIOtYaspGGP8eAYXA9iW ydrMDX5RxANpQMktCDvFBb+cBZNR5WE0GqP0eqBmIw2Qmf0ltsCjXC084QySPsd3TJg== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=agUSa6Oi; arc=none smtp.remote-ip=209.85.128.52 Received: from mail-wm1-f52.google.com (mail-wm1-f52.google.com [209.85.128.52]) (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 07F25606A32D for ; Wed, 2 Dec 2026 02:35:20 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 07F25606A32D Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=agUSa6Oi Received: by mail-wm1-f52.google.com with SMTP id 5b1f17b1804b1-4a35a60499eso96825265e9.0 for ; Tue, 01 Dec 2026 18:35:20 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1796178919; x=1796783719; 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=fGFj/1CKw77FDNsA6i4gP/GtNHeQOiJNKuy2lM4LenU=; b=agUSa6OidkUNQparRTWGAXAIKgt0c9InE//mVz02f6iyWglDW7M7uYeEzxia1To4Pl M2+zBwaxgiLFT+Ua7l6c0ipmVFzMRqfHyark5YAUruOq3ibeRumGrUwiupSm/4avSs87 GxgcxHfeseHqTN43oQ36QT9h7GBR3wzTHN2siairLALxoRe27qxeUEyhYfh2gbZElFmN eJ8AMYBjul97hqXg76YSMqxaGG2S/Je2j1+yRQr2Q2BVk6Qt57+83uInZbQKuA7G/Hub /HhBuOZekz4wChPJPWCOevuDkeEoEczOz/uMmViJnhFMfKal86lIwrEkRwEckUoOFqoh 7uig== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796178919; x=1796783719; 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=fGFj/1CKw77FDNsA6i4gP/GtNHeQOiJNKuy2lM4LenU=; b=giB09y0yWzgbGB9EBF597aZyjVcJx6rnifw5jLT82I3912u912IQmmKSem2g+fDtSH mrMrQuvNiDlgtCxApRZAmZMQ3IcRse+Y2NvyLgexytCFN9e1Bbq/WO9SsuHjfOiBUAcM YXMwgiojmAZ4BmBliLFCaO8g+XFUpOzlgfyRix1lhE5H8Ewivd+DqHFjVBmchS9ryhSV ZTrUjJiiqa/xHP9Z2/uUKuWlPKa3XCN1/ZXv9GnKLwCAUqK40i6mJ9VvENfz9+ZMpggo KS3w1ylSdOHOlYokrEhlN14ITqgl8ayM9Jg25G/U3N7gZVSy9JTYVucz6DT82FV6Loa5 e5Cw== X-Forwarded-Encrypted: i=1; AL+hErE+tPlyn/yBopILs7R9ZMmyiCRaBTGcYarG7hKQkx5pa8JzL5FWkAfVb39fZLPEpxnGGOexbmV5t+ZL/DA=@vger.kernel.org X-Gm-Message-State: APuU4Z81rQFa36wUNCXN1Q5dIhbKRzYpcf66nm7APr6XJ47+WJVCPIkb ZWW/C03Q63na8PkEQMv+r9m+W7QUPpU5UthrlCs5ZA+T6qlg3SXNVZXu X-Gm-Gg: AZFdTZeX9g77nQz4DSRaIYUnNih9Swibq+jKv1e7q7svNLA0ZTpU3ldRgekVAuW/x3e dKW7mITAVvpxDkexzSX2FojQE33rRj+aYpNbyliWiqrNQfgcizNmJjBkf9gHSFV7cYlyqveSuBR e10+3TL1fgYjjE8rJY0hy1mZYgC1QqQD2cyeMXiLZEiCFNtt4RPNdh2bJrR9+HbteZYF8NVcAgK XWO01Z1p5Ec0kjGQ2Jirzq7rciMBAxfs7syrEmuF+5UudSJ5Wm9Wwv9b0xpImQiXb/DnrBaur1W 6QWjd35Oka2i4HB4+nvSK4uxtC6obwBOtu0zIrzRVf41ihpOgAza6sLsZI6f5c0zabkzxRu4gm5 OE7wbpX7R3x8ypXAu1303mFz6LZaZ7hVU8YEhYB3zyMpPpD7vkddTQ+IymxJbcJ0bdnIi4a3cDS k5L2uoIREEr/eCY+3aQofkw0pN9WM80En/SMNZ5XVbe4qr3KtU+3BLgVdVGngYRegcEMaeIWRq9 AQY46BJJv8SnxmeGD0l0m9uPPAgoyywE909zhv+0DoSUjeDiCfD4V3H2Iubp7yREVXiG6LGs5Ij KpGxI9Df7anXhn6Elu1UeIXqTQ/wp7o2eCEATum12qTYp9zVHF/nhcbh0pK1Ie710GBYIYYrrsY uDjqdLS6/rCcsO+othHCKkeGYHg7Q66m9Ar8UOua5qoJxUSZ+du82QuvVdxjZdPfiX+IE7xUrRW vqZVM/Ywc+jy6y1iVIvjBwuOtAewl3CVPO X-Received: by 2002:a05:600c:45d4:b0:4a2:84c:52f3 with SMTP id 5b1f17b1804b1-4bb8880988cmr796949525e9.6.1796178919367; Tue, 01 Dec 2026 18:35:19 -0800 (PST) Received: from localhost.localdomain (dynamic-2a02-3100-a5c9-8d01-34fb-c870-a93e-3922.310.pool.telefonica.de. [2a02:3100:a5c9:8d01:34fb:c870:a93e:3922]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-4bb90660cb4sm1056804345e9.2.2026.12.01.18.35.17 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Tue, 01 Dec 2026 18:35:18 -0800 (PST) From: Karl Mehltretter To: Sebastian Andrzej Siewior , Alexei Starovoitov , Vlastimil Babka Cc: Karl Mehltretter , Harry Yoo , Andrew Morton , Peter Zijlstra , Thomas Gleixner , Daniel Borkmann , Andrii Nakryiko , Martin KaFai Lau , Eduard Zingerman , Kumar Kartikeya Dwivedi , Song Liu , Yonghong Song , Jiri Olsa , Emil Tsalapatis , Ihor Solodrai , Amery Hung , David Hildenbrand , Lorenzo Stoakes , "Liam R . Howlett" , Mike Rapoport , Suren Baghdasaryan , Michal Hocko , Swaraj Gaikwad , Clark Williams , Steven Rostedt , bpf@vger.kernel.org, linux-mm@kvack.org, linux-rt-devel@lists.linux.dev, linux-kernel@vger.kernel.org Subject: [RFC PATCH v3 0/2] bpf, mm: Avoid allocator deadlocks on PREEMPT_RT Date: Wed, 2 Dec 2026 03:35:04 +0100 Message-Id: <20261202023506.35176-1-kmehltretter@gmail.com> X-Mailer: git-send-email 2.39.5 (Apple Git-154) 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-Transfer-Encoding: 8bit On PREEMPT_RT, a BPF program on sched_waking that creates task storage can hang the machine. kmalloc_nolock() runs in try_to_wake_up(p) with p->pi_lock held and IRQs off, and trylocks rtmutex-based slab locks. Unlocking one can wake p and re-enter try_to_wake_up(p), deadlocking on p->pi_lock (confirmed in GDB). Patch 2 has the details. v1 restored v6.19's !preemptible() check in kmalloc_nolock(). Alexei pointed out that this broke BPF arena on RT, which allocated range-tree nodes (and a fallback page on fault) under arena->spinlock. v2 tried to make rtmutex trylocks usable from there instead. Following Sebastian's feedback, that approach is dropped. This version moves the arena allocations and range-node frees outside arena->spinlock first (patch 1), then restricts can_spin_trylock() to preemptible context on PREEMPT_RT (patch 2). A held raw spinlock implies !preemptible(), so the check does not need to identify specific caller locks. This is deliberately a conservative RT-only guard rather than an attempt to classify tracepoint contexts; non-RT is unchanged. On PREEMPT_RT, the nolock allocation helpers now return NULL when called with preemption or IRQs disabled. This restores the earlier RT restriction for kmalloc_nolock() and extends it to alloc_pages_nolock() (whose zone and PCP locks are also rtmutex-backed on RT); free_pages_nolock() falls back to its deferred list. Sleepable arena use, arena faults and preemptible BPF programs keep working. New task-storage allocations from sched_waking and kprobes fail on RT, as do arena allocations (and reservations, returning -ENOMEM) under an outer bpf_spin_lock. Patch 2 depends on patch 1 so arena does not break on RT. Both should be applied in order, ideally through the same tree with subsystem acks. Open questions: - Is !preemptible() in can_spin_trylock() on RT an acceptable tradeoff? - Non-sleepable bpf_arena_free_pages() under a bpf_spin_lock on RT now always fails to allocate the deferred-free span, retaining the pages until map destruction. Hardirq and NMI callers already behave this way, but because bpf_arena_free_pages() returns void, callers cannot detect or retry it. What would be the preferred way to preserve deferred freeing from these contexts? - kfree_nolock() on RT still trylocks the sheaves and memcg stock locks in non-preemptible context. This is outside the scope of the allocation-side fix here; would Harry's recent kmalloc_nolock() series be a suitable basis for addressing it separately? Tested on x86-64 QEMU/KVM (4 vCPUs, v7.4-rc5): - RT + lockdep, sched_waking: circular lockdep splats in 2/2 unpatched v7.4-rc5 boots; none in 4 patched boots (2 targeted, 2 with the v1 reproducer). No task-storage allocations succeeded on RT, as expected. - RT without lockdep, sched_waking + kmalloc-cg-192 churn (40 s): 0/16 hangs on patched v7.4-rc5 (unpatched v7.4-rc3 hung 3/21 KVM and 1/12 TCG). - Arena test (alloc, free, reserve, faults; single-thread and 4-thread adjacent ranges): passes on RT (4 prod, 2 lockdep) and !RT + lockdep (2 runs). - !RT + lockdep, sched_waking: 2/2 clean, every allocation succeeds. - Both commits build cleanly on their own with W=1 (RT lockdep, RT prod and !RT lockdep). Assisted by an LLM. v1: https://lore.kernel.org/r/20260919171443.90512-1-kmehltretter@gmail.com/ v2: https://lore.kernel.org/r/20261005070625.8871-1-kmehltretter@gmail.com/ pi_lock thread: https://lore.kernel.org/r/awbvek-HS3OJYJGF@thinkstation/ Harry's kmalloc_nolock series: https://lore.kernel.org/r/20261115-kmalloc-nolock-fixes-v3-0-9034dd4ecaab@kernel.org/ Karl Mehltretter (2): bpf: Move arena allocations and frees outside arena->spinlock mm: restrict can_spin_trylock() to preemptible context on PREEMPT_RT kernel/bpf/arena.c | 129 +++++++++++++++++++++++++--------------- kernel/bpf/range_tree.c | 80 ++++++++++++++++++------- kernel/bpf/range_tree.h | 15 ++++- mm/internal.h | 10 +++- 4 files changed, 159 insertions(+), 75 deletions(-) base-commit: fe6ff8d5d675c01793b9f051d16055dd4aa75244 -- 2.39.5 (Apple Git-154)