From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-dy2-f12.google.com (mail-dy2-f12.google.com [74.125.229.12]) (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 31997363C51 for ; Sat, 3 Oct 2026 18:33:07 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=74.125.229.12 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1791052389; cv=none; b=iVHxRnqQiThJd7Y2WwJJRMUBMUBzyOuRHCd5OeX+/Xkxfe9OKsGpqXQOAmFPtlk/kpH7+RUk1VbGaZLbXjIzvVS+EEZ4ITbXPhweFArGo/7KTkrKUy6Y7uFCUXyV9KpGzbjk2q024j37jE6/MaTPyuPdT1c9LnSrFSfff9JjK48= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1791052389; c=relaxed/simple; bh=/h6lOlOa1TOSE6/YjJjYsJ0AjizmijOCWan0WqqB9oo=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=BvnJ+xO28bGtZScboLvHpiTc2X8guDak1S7FGNSJsklmyb79VSXQKSI0eZaHRGiBAV5L96CNHNhJcPUL9jHanAxIqVSjwz6QkBwA9XEsYn1ab36W/62ohJb63UOHX8l+uEx0EG6/n+oP6j0emLiF8bdK7wrmaWXezV/Wn6X3LjI= 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=F0WpIzaM; arc=none smtp.client-ip=74.125.229.12 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="F0WpIzaM" Received: by mail-dy2-f12.google.com with SMTP id 5a478bee46e88-33bc6ff6cadso91487eec.0 for ; Sat, 03 Oct 2026 11:33:07 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1791052386; x=1791657186; 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=cuY5nPRthZWhch4MsboYh+YgCm2kORhdKEQMo3rY0hU=; b=F0WpIzaM6tSQn+CNycvLFISX+cx69ZeNXtg2a8BKWIg8Q2fWh4uiFviZ8Qis+H0Z2c T4am0OTIweASubfA/lTGDoVFVnzet/hy2Z46tnNUQ9ETazQqdYP6G1B5ZxBwNdukx295 IgT6l0A51kYcvMVEgdS+yWicheKgJYsCndIG+pMnCFGIswrKTPmH8KRlC2RsJvjKoTM7 D/gK38O3zQC2EVdjChOIYSqDnJUp/WwqMEaRPcWVpXXevzk2HgxltgElN+jBpKqxLMBq 6KbPiI6c5qKfbo/aW3oXel0QmhvcI/bpRyuEVLs53VS1g9aGPuLL+wArLvxRw/cvqnvE tRcw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1791052386; x=1791657186; 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=cuY5nPRthZWhch4MsboYh+YgCm2kORhdKEQMo3rY0hU=; b=TuZtLwXsoeu//LUlREkGEhCjxVvWh45sIbfyiDlXdwUY9++3PUYQMAuQ/ULfBFHBFU mDpVbGIkgEVhyGXSM/bD1CDZIwwCvqcDlEazxfLh6g0DmLce1VBQ7H2SwfsCdl/mGo77 z3tPAqUQzleCBgvN2vq0c3uo5nZ9vNkmqv+CcG0xjoxnNPr/3AjKUWlbUHKPxr79eWSk 2FbYEf7RwLC5bLKmhc5jJ/qETGaKnDKCqt0aupEMi9jPfkBXLVPlp3hRfHBXOxT52svO xznFdpCllp3VlMiYT8Fjcv9PNpY8FoSx1qBBhwvUIO3wt4sfPoiItG25RrjXp8b1qoD7 /OtA== X-Forwarded-Encrypted: i=1; AKwUvBzWl5Yo8usAfz4Nhi9EtwkkNoAwQUJwOiwVdVM1R1XsMDux6uW3k5rTGY01DYqe2Fe1FI0hZ2LlD8mT7Ho=@vger.kernel.org X-Gm-Message-State: AFq9FYIBPfhZjYBZ9yjb30HHpd3kZNiz7O3yY0n6o0/62jOwPlW6YA8w QARAm+0Dd/DwVrDnC+EvmHtDLA9t0mFopRktVOBdpPBPBnZLmHYxS80u X-Gm-Gg: AYBFou17PW9zN11UjphkKD+ad90Azu7Utbr9yz5KNFePAvllKmoO34EG+Qb3q62+2hF NE0cus89IQRlfg13/XvTm+YZ1YRub7i69OslE5Hir7IIT3jI//B0zM+hx1QyX4oHKcMixpTcUmj DWxAwRBI8ji5TupxLbGSIPfALD7+WEaOyXq1FlCrGh/yMwC+UwJtItyYAUvDShXygXhokZJHlVs GEvzbn/D0BsWZjhbHshJWFy7WMEs6bz9uGZ9hCG0bjf19wSXt2lLnHpYBmXTLlWIxATzIClN7aA iO/9ezO6JphU5MNo1aihfKEi4BEr4b7boRiCwg+l0uivY1OyxotgpsoggIZ6aJMjLGfhhc9Ifvp ghzr5gvY8iEcpIYIEXuHnGV35A297S++pexRjbk33KfD2D/ncY+rZq2zVbuf4f59qIcbDKEVZlA t/Yo8ROgUdhVnkCCWOABLo+dA0D0epTRligzSwopgMj/McZTs4MYuupHiZvZQgSVOs1Vmrvqdil 9EMBFdW334c36cP0zDJ5cKAqbhA8HA4D+8EALUbcR4x0+FWaLRDfLJ6IwogTPT12Fbzrg== X-Received: by 2002:a05:693c:8858:b0:33e:8552:6e06 with SMTP id 5a478bee46e88-34f06674f4fmr11369817eec.0.1791052386124; Sat, 03 Oct 2026 11:33:06 -0700 (PDT) Received: from localhost.localdomain (95.169.12.199.16clouds.com. [95.169.12.199]) by smtp.gmail.com with ESMTPSA id 5a478bee46e88-34f14fd9698sm17118191eec.23.2026.10.03.11.33.02 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 03 Oct 2026 11:33:05 -0700 (PDT) From: Chengfeng Ye To: alibuda@linux.alibaba.com, dust.li@linux.alibaba.com, sidraya@linux.ibm.com, mjambigi@linux.ibm.com, davem@davemloft.net, edumazet@kernel.org, kuba@kernel.org, pabeni@redhat.com Cc: tonylu@linux.alibaba.com, guwen@linux.alibaba.com, horms@kernel.org, linux-rdma@vger.kernel.org, linux-s390@vger.kernel.org, netdev@vger.kernel.org, linux-kernel@vger.kernel.org, stable@vger.kernel.org Subject: [PATCH net v2 2/2] net/smc: serialize early link group cleanup with termination Date: Sun, 4 Oct 2026 02:32:37 +0800 Message-ID: <20261003183237.2284245-3-nicoyip.dev@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20261003183237.2284245-1-nicoyip.dev@gmail.com> References: <20261003183237.2284245-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 smc_lgr_cleanup_early() enters __smc_lgr_terminate() even when another teardown path has already claimed lgr->freeing. Both callers can observe lgr->terminating clear before either sets it, then tear down the same link group, causing use-after-free and duplicate resource release. BUG: KASAN: use-after-free in __smc_lgr_terminate+0x393/0x3a0 Write of size 1 at addr ffff8880bf2c0300 by task poc/106 Call Trace: __smc_lgr_terminate+0x393/0x3a0 __smc_connect+0x2e3d/0x4930 smc_connect+0x42c/0x580 __sys_connect+0xfc/0x130 __x64_sys_connect+0x6d/0xb0 Check freeing under the link group list lock before unlinking the group or entering termination. The existing locked freeing transition then claims exclusive teardown ownership; an early cleanup that loses the claim leaves teardown to the existing owner and does not remove the group from a device teardown's private list. Keep a temporary link group reference across smc_conn_free() and early cleanup in smc_conn_abort(). Once the connection is unregistered, a termination worker can finish without taking its socket lock. The extra reference keeps the allocation alive until the ownership check returns. The registration failure path in smc_conn_create() needs the same protection. A new group is published before registration, but the connection reference is taken only on success. Concurrent termination can release the group after conns_lock is dropped and before early cleanup. Hold the new group before publication and release that temporary reference after failed-registration cleanup. On success, acquire the ordinary connection reference before releasing the temporary hold. Reused groups retain their existing reference protocol. Fixes: f9aab6f2ce57 ("net/smc: immediate freeing in smc_lgr_cleanup_early()") Cc: stable@vger.kernel.org Assisted-by: GPT-6.1-Sol Signed-off-by: Chengfeng Ye --- Changes in v2: - Pin newly created link groups before publication, covering the registration failure handoff to early cleanup. Drop the temporary hold after cleanup on failure, or after the connection hold on success. - Send after the free-work scheduling fix and reuse its standalone freeing bool and locked early-cleanup transition. - Pin the link group across the smc_conn_abort() early-cleanup ownership check. v1: https://lore.kernel.org/r/20260927063607.3691520-1-nicoyip.dev@gmail.com/ net/smc/af_smc.c | 8 ++++++-- net/smc/smc_core.c | 8 ++++++++ 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/net/smc/af_smc.c b/net/smc/af_smc.c index e9f93b3ab435..97b0a0b51066 100644 --- a/net/smc/af_smc.c +++ b/net/smc/af_smc.c @@ -1011,12 +1011,16 @@ static void smc_conn_abort(struct smc_sock *smc, int local_first) struct smc_link_group *lgr = conn->lgr; bool lgr_valid = false; - if (smc_conn_lgr_valid(conn)) + if (local_first && smc_conn_lgr_valid(conn)) { lgr_valid = true; + smc_lgr_hold(lgr); + } smc_conn_free(conn); - if (local_first && lgr_valid) + if (lgr_valid) { smc_lgr_cleanup_early(lgr); + smc_lgr_put(lgr); + } } /* check if there is a rdma device available for this connection. */ diff --git a/net/smc/smc_core.c b/net/smc/smc_core.c index 478c8bc2759a..1918a79ff60e 100644 --- a/net/smc/smc_core.c +++ b/net/smc/smc_core.c @@ -693,6 +693,10 @@ void smc_lgr_cleanup_early(struct smc_link_group *lgr) smc_lgr_list_head(lgr, &lgr_lock); spin_lock_bh(lgr_lock); + if (lgr->freeing) { + spin_unlock_bh(lgr_lock); + return; + } /* do not use this link group for new connections */ if (!list_empty(&lgr->list)) list_del_init(&lgr->list); @@ -999,6 +1003,7 @@ static int smc_lgr_create(struct smc_sock *smc, struct smc_init_info *ini) lgr->buf_type = lgr->net->smc.sysctl_smcr_buf_type; atomic_inc(&lgr_cnt); } + smc_lgr_hold(lgr); /* lgr_put in smc_conn_create() */ smc->conn.lgr = lgr; spin_lock_bh(lgr_lock); list_add_tail(&lgr->list, lgr_list); @@ -2054,10 +2059,13 @@ int smc_conn_create(struct smc_sock *smc, struct smc_init_info *ini) write_unlock_bh(&lgr->conns_lock); if (rc) { smc_lgr_cleanup_early(lgr); + smc_lgr_put(lgr); /* lgr_hold in smc_lgr_create() */ goto out; } } smc_lgr_hold(conn->lgr); /* lgr_put in smc_conn_free() */ + if (ini->first_contact_local) + smc_lgr_put(conn->lgr); /* lgr_hold in smc_lgr_create() */ if (!conn->lgr->is_smcd) smcr_link_hold(conn->lnk); /* link_put in smc_conn_free() */ conn->freed = 0; -- 2.43.0