From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.214.200 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1795920430; b=4FqDgsxjYK89yTEZ3n7eXs80pUWqkl5bas6KzRplGYMHbuA4AzufXHI5XBQD+uyv7Bxo wLzchdPJ6td5v92pPVxUZ0vSZ07kV4XX/gNlabaVN/9Hi7aOL1Y5MqvRtkHb41kZt/m68 TRVbmflz1iNV9nGdZ3hdRvEWYwQGW6eULoe0kDVQLLzaBRUiFL7rhtRAFGRorSkQZqCgp 9pXQ7xyf23yAZo7L3kAvZthRi16D5UcdbUFjWZnjXTdy89QBDqQTINQVdejgU6o6kszA2 z8x8W9udY7lB+vjo1tlf082Gw0BlrDgVj+lkcOtrhtwif+Zr1ZBVFKCMFIkHZOf9sjQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795920430; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Forwarded-Encrypted:X-Gm-Message-State:X-Received:Date:Mime-Version: X-Mailer:Message-ID:Subject:From:To:Cc:Content-Type; bh=gBbXHTwEjfO97+F0alNb96APnD2buVitJchL6Xc5LcY=; b=oKSr7wLSE8j/Fg2FSOXzb1r73oLRSjuweg0l3a+dKFT8K0FOIdQh/FvIPPXZrK368gtS eDuglSa5OeY/5fgil50b6SMj8xgrkzU8JLpnVIveDxJeho7fOOeX9J4siIHzOZ15GFFrX BPzcHRuGDLJHcIQ1jPCiv364/NUVSXqjHmG17kiOJmqFWwlAKpB8UCSi6E/CAnjnQp3l0 XO6I6Cx8Gl/NBasYCdcldeAV/ql9LKtxTsaTPBtZilV7P+C41xp5XwpFMl5sMUnFbRY08 ec8hJqOto69ydhg8qaM3jDJLWjr8gq5CGIX2S60xJ8OvI09RVTL0KE+wj6KkBe3Mpog== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=google.com; spf=pass smtp.mailfrom=flex--zhenwenliang.bounces.google.com; dkim=pass header.d=google.com header.i=@google.com header.a=rsa-sha256 header.s=20260707 header.b=iNtlQZeH; arc=none smtp.remote-ip=209.85.214.200 Received: from mail-pl1-f200.google.com (mail-pl1-f200.google.com [209.85.214.200]) (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 CEA2D606A323 for ; Sun, 29 Nov 2026 02:47:09 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org CEA2D606A323 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--zhenwenliang.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.a=rsa-sha256 header.s=20260707 header.b=iNtlQZeH Received: by mail-pl1-f200.google.com with SMTP id d9443c01a7336-2f03a137f6aso79514195ad.3 for ; Sat, 28 Nov 2026 18:47:09 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20260707; t=1795920429; x=1796525229; darn=vger.kernel.org; h=content-type:cc:to:from:subject:message-id:mime-version:date:from :to:cc:subject:date:message-id:reply-to:content-type; bh=gBbXHTwEjfO97+F0alNb96APnD2buVitJchL6Xc5LcY=; b=iNtlQZeHArkrbse+LY6vjG1H4sqNBBNjVm72CZTS0ls8Y+xWbWxN/3GCD2oZuXGmsJ KvDUiWtL7cO4+7PG33fCnfbO07ZVN3Zpty6X3FLVe5U8FEWTRY8k+tVMmFxAdVzGGD98 gW9nO3EC8XumfxLZIrZmHW9J00foWo+8cp+oeRzVehZMT26tyXe5u85e0KrlsdsddyGb lHLFnDN8K8YzCMCb1ndYn654SwWJ4sQYOOyMp8tCR1RqStkF/XA3TXQ+AC6KMgnQDpkv neLwnIoPQ3VDjegdhVRe+uVE3Sah9FQ7QKfbCNXJJcZrE6SPmkFTP6iSdE4HWfXPBKEq bvWQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1795920429; x=1796525229; h=content-type:cc:to:from:subject:message-id:mime-version:date :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to :content-type; bh=gBbXHTwEjfO97+F0alNb96APnD2buVitJchL6Xc5LcY=; b=UAW2o8TUiUyiMJ/8FEVHYt13MasXONEJFXA0mJh02THuSyl0UWydfz4givjQWI31/F wUIKqB3rgqSbGGI75BX1oy7kDspgU/YJY0P3A74sFAQv3dV+j+4M/4GVUoJea1tRD0i+ GmTyIobAhEcMZh2v1sMJhKUrGXLd59x/3UzVMZ39A+xcToj2D05bDjEQMJjMmZTlAJC3 GCcLpzGHiVReb/qhunOce0gXwaUXVlji0uOm8pEbU0Iqz+zF5VF9N4JmvYQ7OCi3V21f wSr/Vzalrd8inkwImglRW2tXlJv0p0tW52B4552RpZdovzaRrhEeXKl7WKjUE7Zj/Hzm MyNw== X-Forwarded-Encrypted: i=1; AL+hErGKnPLb4T0Btbc18ucRJQzNqjdU35TiilvsQ91GIaoSzcHRrtZdOgEG7zOkwHHDmPL0mkI9ZIf3QXs7@vger.kernel.org X-Gm-Message-State: APuU4Z8YcrIyKBQBEpoy15ShHVICbAQtxSzwgaptjAz4KTvx30fQ+jAi C+BStB/Ye0vHMpTJt/Pxg+7HpWUWiS4nl1N80JrDS33pwEM5Kelxm1t1GBKD/cFiHqC5woV1cU8 Kk0QxDTg3QXQ/SBaBK5Lznl3GjiTIxA== X-Received: from plqw24.prod.google.com ([2002:a17:902:a718:b0:2ef:d8b5:15f5]) (user=zhenwenliang job=prod-delivery.src-stubby-dispatcher) by 2002:a17:903:1a0e:b0:2ed:38c0:332d with SMTP id d9443c01a7336-2f03f16eabamr293975885ad.23.1795920428793; Sat, 28 Nov 2026 18:47:08 -0800 (PST) Date: Sun, 29 Nov 2026 02:47:08 +0000 X-Mailing-List: linux-rdma@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list Mime-Version: 1.0 X-Mailer: git-send-email 2.56.1.839.g22a8290db9-goog Message-ID: <20261129024708.279044-1-zhenwenliang@google.com> Subject: [PATCH] RDMA/rtrs-clt: Fix a lost wakeup in rtrs_clt_put_permit() From: Zhenwen Liang To: "Md. Haris Iqbal" , Jack Wang Cc: Jason Gunthorpe , Leon Romanovsky , linux-rdma@vger.kernel.org Content-Type: text/plain; charset="UTF-8" rtrs_clt_put_permit() releases a permit with clear_bit_unlock() and then calls waitqueue_active() without holding the wait queue lock. clear_bit_unlock() is a release operation: it orders earlier accesses before the clear, but it does not order the clear before the later waitqueue_active() load. The comment on waitqueue_active() in include/linux/wait.h requires a full barrier on the waker side for this lockless check. The waiter, rtrs_clt_get_permit(), calls prepare_to_wait(), which adds it to permits_wait and sets the task state (a full barrier), and then looks for a free bit in permits_map. Without a barrier on the waker side, the waker can see an empty wait queue while the waiter still sees the bit set, and the wakeup is lost. The waiter sleeps in TASK_UNINTERRUPTIBLE with no timeout; if the freed permit was the last outstanding one and no later put follows, nothing wakes it. The existing comment argues that a sleeping waiter must have added itself to the list before the put finished. That does not hold on weakly ordered CPUs, because the waker's load of the wait queue head can be satisfied before its clear of the bit becomes visible. Add smp_mb__after_atomic() right after clear_bit_unlock() in __rtrs_put_permit(), as Documentation/atomic_bitops.txt and Documentation/atomic_t.txt prescribe for upgrading the ordering of an RMW bit operation, and replace the comment. This does not remove every race on permits_map. The waiter looks for a free permit with find_next_zero_bit()/find_first_zero_bit() in __rtrs_get_permit(), whose plain loads race with clear_bit_unlock(); in the Linux kernel memory model this is a data race. The barrier fixes the reordering on the waker side that loses the wakeup; the data race between those plain loads and the clear remains. It is left as it is because the find_*_bit() helpers have no marked variant and changing them would go beyond this function. Marking the waiter's re-check after prepare_to_wait() would also remove the race in the memory model; that is left for a separate change. A litmus test of this pattern (the waiter's enqueue, full barrier and bit test against the waker's release clear and wait queue check) showed the reordering 2875 times in 10^8 runs on arm64 (Neoverse-V2), and 6343 times with an LL/SC clear; with smp_mb__after_atomic() added, 0 in 10^8 for both. The same pattern on x86-64 gave 0 in 10^8, as expected, because clear_bit_unlock() is a locked instruction there. herd7's AArch64 and PPC models allow the pattern and forbid it with the barrier. The problem was found, and this patch prepared, with the help of a formal analysis tool we are developing that uses AI assistance; I have reviewed the analysis and the patch. Compile-tested with clang on arm64 (W=1) and x86-64, and checked that the barrier is emitted between the clear and the wait queue load in arm64 object code; not runtime-tested beyond the litmus tests. Fixes: 6a98d71daea1 ("RDMA/rtrs: client: main functionality") Signed-off-by: Zhenwen Liang --- drivers/infiniband/ulp/rtrs/rtrs-clt.c | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/drivers/infiniband/ulp/rtrs/rtrs-clt.c b/drivers/infiniband/ulp/rtrs/rtrs-clt.c index 85eaa2bdb470..c6befc4303af 100644 --- a/drivers/infiniband/ulp/rtrs/rtrs-clt.c +++ b/drivers/infiniband/ulp/rtrs/rtrs-clt.c @@ -102,6 +102,12 @@ static inline void __rtrs_put_permit(struct rtrs_clt_sess *clt, struct rtrs_permit *permit) { clear_bit_unlock(permit->mem_id, clt->permits_map); + /* + * Order the clear before the lockless waitqueue_active() load in + * rtrs_clt_put_permit(). Pairs with the barrier in set_current_state() + * in prepare_to_wait() in rtrs_clt_get_permit(). + */ + smp_mb__after_atomic(); } /** @@ -162,11 +168,9 @@ void rtrs_clt_put_permit(struct rtrs_clt_sess *clt, __rtrs_put_permit(clt, permit); /* - * rtrs_clt_get_permit() adds itself to the &clt->permits_wait list - * before calling schedule(). So if rtrs_clt_get_permit() is sleeping - * it must have added itself to &clt->permits_wait before - * __rtrs_put_permit() finished. - * Hence it is safe to guard wake_up() with a waitqueue_active() test. + * The barrier in __rtrs_put_permit() orders the clear before this + * check: either a waiter in rtrs_clt_get_permit() sees the cleared + * bit, or we see the waiter on &clt->permits_wait and wake it up. */ if (waitqueue_active(&clt->permits_wait)) wake_up(&clt->permits_wait); -- 2.56.1.839.g22a8290db9-goog