From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-pl1-f176.google.com (mail-pl1-f176.google.com [209.85.214.176]) (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 C91EA4279EA for ; Mon, 27 Jul 2026 17:48:53 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.176 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1785174539; cv=none; b=ZuSCNzWb3NMMwngKwy2PUQiIFHOV680LOHndAL4eU9yZDBpYkXi0CG8/6U2uAapA90LpsLX/rFO7dLa5Wd3e02VQXx7o9NvP4QOFow/u+oCRjUvcHCmR/VzTTglv9wPT/gXgGIccYlpq6oDD9UgC/wWGKx4lZw0zmhmZsrCI9Tc= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1785174539; c=relaxed/simple; bh=APSDTPUEgNFwya6Uj8Y2KMU/1rodoV6LqxS3uvEU5rg=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=KEoBwjTWc1fBm6DEvbYTdDD1oNs91zB3Mp25CFeb9dGWUkNxh7mkZ+JiF4toWT3DUB3XJ7MR4g5MRUCdeGh7bgxeivZIN9wVdyaLQo03LKad6ERX/gDusCWN7DpxV8G873cTXUebckzVJFuwkmbEZuljxLpSedaRWyr4MHEvFtM= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=gY5N+qN+; arc=none smtp.client-ip=209.85.214.176 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.b="gY5N+qN+" Received: by mail-pl1-f176.google.com with SMTP id d9443c01a7336-2cf4c37dbe6so2403635ad.2 for ; Mon, 27 Jul 2026 10:48:51 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1785174531; x=1785779331; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to:content-type; bh=/0L8UcHD2YSWW64Z5FD/06NrkfkvZfdKRpj1bBpinDQ=; b=gY5N+qN+Zm+NioVy7pCHoKgj1Y3NLQ4/G6V+qe1AMgrjd6dLr/OYwGrLg04WRww9i1 j8wKaioEbpES9AWscMmkGPARLlHMVArAB3Jxhfp9iW3gKPejTRMajzDhtvuym6C0ojwK vsEfUF8c7X50P56WJpY54P32sxUgZm9XqVvhZbRInawoiZvZ+pIQdL0yxkPEUP52yNJj 1x4R08bymPEPF4QnvsTmH5TmFSrrQznooMlLxd5eEOgS9iFc9iQr9NEQF2TVrELpXtES 6FpiNU6084tLEhKiKSzXaRvBF7msTeGp3UX/3ou51LOvuXFG3fVGjeVFjyxaESUC3Sts rT+g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1785174531; x=1785779331; h=content-transfer-encoding:mime-version:references:in-reply-to :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=/0L8UcHD2YSWW64Z5FD/06NrkfkvZfdKRpj1bBpinDQ=; b=GMpjRaxC0rP4dYIjsWswX84PRYWxsLjsnPOoC5In3XL/kUPJ2aKkPkiriK/sxNrql3 +KFAPTJxw8V0I7u9S8CAZ+36qbUVMMLZ5lTcTeXFR+csRr9c4tSQQZILD3u+JAUD7R+9 qxegAuN8NRU10oCqCPlgmlcNUKCeWHuZTWdAKmRsN7YnkGHWiIBQKpLskerZe28x6v+d IKRGnGjPklCX4m0ZUg5xntz98V/Go5o9vFAYwVq41k2SEKO1eNX/nguLQHv0gId+xKe7 UB/JIaqG2NSmG34+JrNq1A9A75Gvwkas+Zn6Lei1pC9TXVUVkSXXMwifyVi0lTvufqxf Gyzw== X-Forwarded-Encrypted: i=1; AHgh+RphxSh5hyJSqs1bNaNpfKgLg+w225PjQ2tFiX+jxGhbO1j+4MzSGGuXvi2KRQzYATvOep7EWr2T4Hml2Ig=@vger.kernel.org X-Gm-Message-State: AOJu0YwWvyrv0mD0D5/v4xJxplHJGHreFIMwNbCrt3oD0f88fM8GTG+F e+Jnt6t0fX8iSCY+6UDFT1QUVVtyUOMekd58Qlp+K+3+zIHap5McJNW+/fMBWKYWAXk= X-Gm-Gg: AR+sD13iQ2LG0fcMatUCokC29+DliW6wkVjtr2iTNRtrDMhw50Ti4YMtBC5gYGBNU8+ eVLzMkVeHxyxrfKKEGMRIxZWByT4/gNKYIQf+sQKLNd0dqvL+mZxZHOGot8QTTClnFbpTJTNZ6g Mr0SW3QPjkzMiSv19Ou8KHr0Oeo/dh7joXHJyQRDYjN/7q7TkfM4g4I2IRui/akPkk1z+tLE8qt JYOjJtmJRo8zt+3cpwa34zbX6lyYAN+CLEvM4Gb0X/Z414CsBu46vDBUyqTnezPKzbJDHlz8ezD Ve9aV5owikcuXGFw0eulFKQklfIv90w5s+fBhmS+43juxc0o/ZouqsMPsxjD+Eh/OooIhFF8urW a6J7xbsiyYqJVRLP+YiiFL/oa269/GDY0tKyJMuwcsBkHnQmkKiReKchHTE/NzwHhk0QE8xdmr2 /ou+JACIzOOKS1C7uvsEbhGPzbEYLGzx79jjmUwoD3iCyw1lqjUpTiD4naQFgJx14MBKf5 X-Received: by 2002:a17:903:284:b0:2c9:99be:f011 with SMTP id d9443c01a7336-2d00ba3b644mr8041875ad.6.1785174530634; Mon, 27 Jul 2026 10:48:50 -0700 (PDT) Received: from localhost.localdomain (45.78.65.84.16clouds.com. [45.78.65.84]) by smtp.gmail.com with ESMTPSA id 5a478bee46e88-314bc3e878esm34560986eec.3.2026.07.27.10.48.46 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 27 Jul 2026 10:48:49 -0700 (PDT) From: Chengfeng Ye To: Allison Henderson , "David S. Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni , Simon Horman , Santosh Shilimkar , Ka-Cheong Poon , Dan Carpenter Cc: netdev@vger.kernel.org, linux-rdma@vger.kernel.org, rds-devel@oss.oracle.com, linux-kernel@vger.kernel.org, Chengfeng Ye Subject: [PATCH net-next v2] rds: synchronize info callbacks with module unload Date: Tue, 28 Jul 2026 01:48:26 +0800 Message-ID: <20260727174826.135662-1-nicoyip.dev@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20260720184955.3008978-1-nicoyip.dev@gmail.com> References: <20260720184955.3008978-1-nicoyip.dev@gmail.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit rds_info_getsockopt() reads a callback from rds_info_funcs and invokes it without protecting the callback's lifetime. Transport modules register functions stored in this array. For example, rds_tcp.ko registers rds_tcp_tc_info() for RDS_INFO_TCP_SOCKETS. This permits the following interleaving: CPU0 CPU1 rds_info_getsockopt() func = rds_tcp_tc_info rmmod rds_tcp rds_tcp_exit() rds_info_deregister_func() rds_info_funcs[offset] = NULL free rds_tcp module text func() The reader can therefore branch to an address in unloaded module text. Protect callback invocation with SRCU. Enter the SRCU read-side critical section before loading the callback and leave it only after the callback returns. Clear the callback with release semantics and call synchronize_srcu() before deregistration returns, preventing module unload from freeing its text while an old reader is still executing it. SRCU is required because callbacks such as RDS_INFO_COUNTERS can sleep. Keep the callback array unannotated and use acquire and release operations for publication so sparse does not have to apply __rcu through the function-pointer typedef. Replace the two callback-slot BUG_ON() checks with WARN_ON_ONCE() and return without changing the slot on mismatch. Fixes: 70041088e3b9 ("RDS: Add TCP transport to RDS") Link: https://lore.kernel.org/netdev/20260720184955.3008978-1-nicoyip.dev@gmail.com/ Suggested-by: Allison Henderson Signed-off-by: Chengfeng Ye --- Changes in v2: - Split the info callback/module-unload race from the independent inc->i_conn lifetime bug. - Keep the callback array unannotated and use smp_load_acquire() and smp_store_release() to resolve the sparse errors. - Replace the two touched callback-slot BUG_ON() checks with WARN_ON_ONCE() error paths. - Rewrite the commit message for the callback race and omit the unrelated inc->i_conn KASAN report. Link: https://lore.kernel.org/netdev/20260720184955.3008978-1-nicoyip.dev@gmail.com/ [v1] net/rds/info.c | 28 +++++++++++++++++++++++----- 1 file changed, 23 insertions(+), 5 deletions(-) diff --git a/net/rds/info.c b/net/rds/info.c index 21b32eb16559..408a4f0d04a3 100644 --- a/net/rds/info.c +++ b/net/rds/info.c @@ -32,6 +32,7 @@ */ #include #include +#include #include #include #include @@ -68,6 +69,7 @@ struct rds_info_iterator { unsigned long offset; }; +DEFINE_STATIC_SRCU(rds_info_srcu); static DEFINE_SPINLOCK(rds_info_lock); static rds_info_func rds_info_funcs[RDS_INFO_LAST - RDS_INFO_FIRST + 1]; @@ -78,8 +80,13 @@ void rds_info_register_func(int optname, rds_info_func func) BUG_ON(optname < RDS_INFO_FIRST || optname > RDS_INFO_LAST); spin_lock(&rds_info_lock); - BUG_ON(rds_info_funcs[offset]); - rds_info_funcs[offset] = func; + /* Validate the latest published slot value. */ + if (WARN_ON_ONCE(smp_load_acquire(&rds_info_funcs[offset]))) { + spin_unlock(&rds_info_lock); + return; + } + /* Pair with lockless callback lookup. */ + smp_store_release(&rds_info_funcs[offset], func); spin_unlock(&rds_info_lock); } EXPORT_SYMBOL_GPL(rds_info_register_func); @@ -91,9 +98,15 @@ void rds_info_deregister_func(int optname, rds_info_func func) BUG_ON(optname < RDS_INFO_FIRST || optname > RDS_INFO_LAST); spin_lock(&rds_info_lock); - BUG_ON(rds_info_funcs[offset] != func); - rds_info_funcs[offset] = NULL; + /* Validate the latest published slot value. */ + if (WARN_ON_ONCE(smp_load_acquire(&rds_info_funcs[offset]) != func)) { + spin_unlock(&rds_info_lock); + return; + } + /* Hide the callback before waiting for old readers. */ + smp_store_release(&rds_info_funcs[offset], NULL); spin_unlock(&rds_info_lock); + synchronize_srcu(&rds_info_srcu); } EXPORT_SYMBOL_GPL(rds_info_deregister_func); @@ -165,6 +178,7 @@ int rds_info_getsockopt(struct socket *sock, int optname, sockopt_t *opt) int npages = 0; int ret; int len; + int srcu_idx; int total; len = opt->optlen; @@ -214,8 +228,11 @@ int rds_info_getsockopt(struct socket *sock, int optname, sockopt_t *opt) rdsdebug("len %d nr_pages %lu\n", len, nr_pages); call_func: - func = rds_info_funcs[optname - RDS_INFO_FIRST]; + srcu_idx = srcu_read_lock(&rds_info_srcu); + /* Pair with callback slot publication and removal. */ + func = smp_load_acquire(&rds_info_funcs[optname - RDS_INFO_FIRST]); if (!func) { + srcu_read_unlock(&rds_info_srcu, srcu_idx); ret = -ENOPROTOOPT; goto out; } @@ -225,6 +242,7 @@ int rds_info_getsockopt(struct socket *sock, int optname, sockopt_t *opt) iter.offset = offset0; func(sock, len, &iter, &lens); + srcu_read_unlock(&rds_info_srcu, srcu_idx); BUG_ON(lens.each == 0); total = lens.nr * lens.each; -- 2.43.0