From patchwork Sun Jan 12 04:08:00 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kuniyuki Iwashima X-Patchwork-Id: 13936206 X-Patchwork-Delegate: kuba@kernel.org Received: from smtp-fw-2101.amazon.com (smtp-fw-2101.amazon.com [72.21.196.25]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 29EF06FB0 for ; Sun, 12 Jan 2025 04:08:55 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=72.21.196.25 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736654937; cv=none; b=Y4IvqLImkj1YoA5REYqKSYC1R7VGI1BKdblHyr4Ee2j43ucVmNNw722gER47t+BIl2TlMaIFASq8VekzBMkpXcm/XUFv0zEIRiEzW9sKaR7JAQqVITtzAuu2hdtVbmM0fU5zjoUifx+q0YVlrMDo93IbxORAVg5vEQwg/7i4nJI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736654937; c=relaxed/simple; bh=4zxWxSdxq5LjIMM1PjmSzSdckbS+zyZXXBZJjamGcY0=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=HgokqG8opByekfEhag7rADMJPRhLnYHx5uqNyXXffd05t0AT/rLrB2/qR8NQnHGIW6QtbuMGGO3DuIEp03eBYnLwTCy61ilUCWk3/2h2y/M5wit4VNpg7GU+wLsuzZp9BKUrRjhXLyZkv+m/FwKveWLsWdkf80FJ0jz+wujlGo8= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=amazon.com; spf=pass smtp.mailfrom=amazon.co.jp; dkim=pass (1024-bit key) header.d=amazon.com header.i=@amazon.com header.b=J7n2eLYp; arc=none smtp.client-ip=72.21.196.25 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=amazon.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=amazon.co.jp Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=amazon.com header.i=@amazon.com header.b="J7n2eLYp" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amazon.com; i=@amazon.com; q=dns/txt; s=amazon201209; t=1736654936; x=1768190936; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=ZWFaiuQZMrgEqcBsf6VsdoSQyFOIKYV9KciZxBb54VA=; b=J7n2eLYpzSeyYUH+Xx+mjerjnKhyCu12snkvsy4zNMZAKipxPZAzFeuN kE2TjikK8pzgeiktGxpfb0N+LAzyKM5uzlZGUB9f1Hz9jsLL9C7/rxRnI epH7U3F1ILizMCyUMn9ZY4HN8YuNoGmGeoGmuXj2QC+Ywybd7dNNu98HX 8=; X-IronPort-AV: E=Sophos;i="6.12,308,1728950400"; d="scan'208";a="458171934" Received: from iad12-co-svc-p1-lb1-vlan3.amazon.com (HELO smtpout.prod.us-west-2.prod.farcaster.email.amazon.dev) ([10.43.8.6]) by smtp-border-fw-2101.iad2.amazon.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 12 Jan 2025 04:08:53 +0000 Received: from EX19MTAUWB002.ant.amazon.com [10.0.21.151:45550] by smtpin.naws.us-west-2.prod.farcaster.email.amazon.dev [10.0.63.9:2525] with esmtp (Farcaster) id 6ac21fdf-1392-4f21-96c1-2026f128e608; Sun, 12 Jan 2025 04:08:51 +0000 (UTC) X-Farcaster-Flow-ID: 6ac21fdf-1392-4f21-96c1-2026f128e608 Received: from EX19D004ANA001.ant.amazon.com (10.37.240.138) by EX19MTAUWB002.ant.amazon.com (10.250.64.231) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA) id 15.2.1258.39; Sun, 12 Jan 2025 04:08:51 +0000 Received: from 6c7e67c6786f.amazon.com (10.119.14.156) by EX19D004ANA001.ant.amazon.com (10.37.240.138) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA) id 15.2.1258.39; Sun, 12 Jan 2025 04:08:47 +0000 From: Kuniyuki Iwashima To: "David S. Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni , Simon Horman CC: Donald Hunter , Kuniyuki Iwashima , Kuniyuki Iwashima , Subject: [PATCH v2 net-next 01/11] net: dropreason: Gather SOCKET_ drop reasons. Date: Sun, 12 Jan 2025 13:08:00 +0900 Message-ID: <20250112040810.14145-2-kuniyu@amazon.com> X-Mailer: git-send-email 2.39.5 (Apple Git-154) In-Reply-To: <20250112040810.14145-1-kuniyu@amazon.com> References: <20250112040810.14145-1-kuniyu@amazon.com> Precedence: bulk X-Mailing-List: netdev@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-ClientProxiedBy: EX19D036UWB004.ant.amazon.com (10.13.139.170) To EX19D004ANA001.ant.amazon.com (10.37.240.138) X-Patchwork-Delegate: kuba@kernel.org The following patches add new drop reasons starting with the SOCKET_ prefix. Let's gather the existing SOCKET_ reasons. Note that the order is not part of uAPI. Signed-off-by: Kuniyuki Iwashima --- include/net/dropreason-core.h | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/include/net/dropreason-core.h b/include/net/dropreason-core.h index 3a6602f37978..efeae9f0f956 100644 --- a/include/net/dropreason-core.h +++ b/include/net/dropreason-core.h @@ -6,9 +6,10 @@ #define DEFINE_DROP_REASON(FN, FNe) \ FN(NOT_SPECIFIED) \ FN(NO_SOCKET) \ + FN(SOCKET_FILTER) \ + FN(SOCKET_RCVBUFF) \ FN(PKT_TOO_SMALL) \ FN(TCP_CSUM) \ - FN(SOCKET_FILTER) \ FN(UDP_CSUM) \ FN(NETFILTER_DROP) \ FN(OTHERHOST) \ @@ -18,7 +19,6 @@ FN(UNICAST_IN_L2_MULTICAST) \ FN(XFRM_POLICY) \ FN(IP_NOPROTO) \ - FN(SOCKET_RCVBUFF) \ FN(PROTO_MEM) \ FN(TCP_AUTH_HDR) \ FN(TCP_MD5NOTFOUND) \ @@ -137,12 +137,14 @@ enum skb_drop_reason { * 3) no valid child socket during 3WHS process */ SKB_DROP_REASON_NO_SOCKET, + /** @SKB_DROP_REASON_SOCKET_FILTER: dropped by socket filter */ + SKB_DROP_REASON_SOCKET_FILTER, + /** @SKB_DROP_REASON_SOCKET_RCVBUFF: socket receive buff is full */ + SKB_DROP_REASON_SOCKET_RCVBUFF, /** @SKB_DROP_REASON_PKT_TOO_SMALL: packet size is too small */ SKB_DROP_REASON_PKT_TOO_SMALL, /** @SKB_DROP_REASON_TCP_CSUM: TCP checksum error */ SKB_DROP_REASON_TCP_CSUM, - /** @SKB_DROP_REASON_SOCKET_FILTER: dropped by socket filter */ - SKB_DROP_REASON_SOCKET_FILTER, /** @SKB_DROP_REASON_UDP_CSUM: UDP checksum error */ SKB_DROP_REASON_UDP_CSUM, /** @SKB_DROP_REASON_NETFILTER_DROP: dropped by netfilter */ @@ -173,8 +175,6 @@ enum skb_drop_reason { SKB_DROP_REASON_XFRM_POLICY, /** @SKB_DROP_REASON_IP_NOPROTO: no support for IP protocol */ SKB_DROP_REASON_IP_NOPROTO, - /** @SKB_DROP_REASON_SOCKET_RCVBUFF: socket receive buff is full */ - SKB_DROP_REASON_SOCKET_RCVBUFF, /** * @SKB_DROP_REASON_PROTO_MEM: proto memory limitation, such as * udp packet drop out of udp_memory_allocated. From patchwork Sun Jan 12 04:08:01 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kuniyuki Iwashima X-Patchwork-Id: 13936207 X-Patchwork-Delegate: kuba@kernel.org Received: from smtp-fw-2101.amazon.com (smtp-fw-2101.amazon.com [72.21.196.25]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 2B8F818E3F for ; Sun, 12 Jan 2025 04:09:19 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=72.21.196.25 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736654961; cv=none; b=SMdtwcr7CA92xcPcE04z/DA4FN8dX9UHvax5+UtD3Bjmo9OTIbTOX7zkBkyk8F0SzvK8K7QMbUWLfCUjzw5bkNX1XkzCtGmA0Kn0dX7+b6TWhZaDB5rko3SHm8nsWLubwRWm2Wzbewsw28fTNUxN8DH+ujfYSpAQiijQdzu3CEE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736654961; c=relaxed/simple; bh=5mUSIqvZKCzqOqlLhc4HTPnpUs3UM6+Ws+S5vzsOlCA=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=etePWP/a8BwqQhadlI1GwrCMP8yxko/CHyfq8i6fCX/bCoih1P9QqlcSNv+Sq/41/qvLRO451E0kjuxr/by0fOI7qnc6ul99Eyr4GWXOieqtoT6N4iv+RDZ2lAsEqOaDvHfI7e3USEeQtsiikwxXo0LHGmCfK5k1etI/3Tqgv1s= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=amazon.com; spf=pass smtp.mailfrom=amazon.co.jp; dkim=pass (1024-bit key) header.d=amazon.com header.i=@amazon.com header.b=gynJVUcg; arc=none smtp.client-ip=72.21.196.25 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=amazon.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=amazon.co.jp Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=amazon.com header.i=@amazon.com header.b="gynJVUcg" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amazon.com; i=@amazon.com; q=dns/txt; s=amazon201209; t=1736654960; x=1768190960; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=KSH9q4QLzGrgvHxoytg+FRGRTm++eL6aTnjpqTyExSU=; b=gynJVUcgJGoHuvczdcOguD0vsya6jnI4pY2lZCHSSdG0JBIOz6pDQI/g xminbPG2bA1eJANejm3obkYnqL7m6ZV1xdTN1TGqd/Tji5AFYOlYJNptW J8624pTZR0L6aINXdR+/cc+5f2unQJwc5P+scocQKpncSIh/WGn5sQDKB M=; X-IronPort-AV: E=Sophos;i="6.12,308,1728950400"; d="scan'208";a="458171992" Received: from iad12-co-svc-p1-lb1-vlan3.amazon.com (HELO smtpout.prod.us-west-2.prod.farcaster.email.amazon.dev) ([10.43.8.6]) by smtp-border-fw-2101.iad2.amazon.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 12 Jan 2025 04:09:19 +0000 Received: from EX19MTAUWA002.ant.amazon.com [10.0.38.20:20296] by smtpin.naws.us-west-2.prod.farcaster.email.amazon.dev [10.0.58.22:2525] with esmtp (Farcaster) id e3dd2e9a-140d-46f2-9f8e-5bc55dd02042; Sun, 12 Jan 2025 04:09:18 +0000 (UTC) X-Farcaster-Flow-ID: e3dd2e9a-140d-46f2-9f8e-5bc55dd02042 Received: from EX19D004ANA001.ant.amazon.com (10.37.240.138) by EX19MTAUWA002.ant.amazon.com (10.250.64.202) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA) id 15.2.1258.39; Sun, 12 Jan 2025 04:09:17 +0000 Received: from 6c7e67c6786f.amazon.com (10.119.14.156) by EX19D004ANA001.ant.amazon.com (10.37.240.138) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA) id 15.2.1258.39; Sun, 12 Jan 2025 04:09:14 +0000 From: Kuniyuki Iwashima To: "David S. Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni , Simon Horman CC: Donald Hunter , Kuniyuki Iwashima , Kuniyuki Iwashima , Subject: [PATCH v2 net-next 02/11] af_unix: Set drop reason in unix_release_sock(). Date: Sun, 12 Jan 2025 13:08:01 +0900 Message-ID: <20250112040810.14145-3-kuniyu@amazon.com> X-Mailer: git-send-email 2.39.5 (Apple Git-154) In-Reply-To: <20250112040810.14145-1-kuniyu@amazon.com> References: <20250112040810.14145-1-kuniyu@amazon.com> Precedence: bulk X-Mailing-List: netdev@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-ClientProxiedBy: EX19D038UWB001.ant.amazon.com (10.13.139.148) To EX19D004ANA001.ant.amazon.com (10.37.240.138) X-Patchwork-Delegate: kuba@kernel.org unix_release_sock() is called when the last refcnt of struct file is released. Let's define a new drop reason SKB_DROP_REASON_SOCKET_CLOSE and set it for kfree_skb() in unix_release_sock(). # echo 1 > /sys/kernel/tracing/events/skb/kfree_skb/enable # python3 >>> from socket import * >>> s1, s2 = socketpair(AF_UNIX) >>> s1.send(b'hello world') >>> s2.close() # cat /sys/kernel/tracing/trace_pipe ... python3-280 ... kfree_skb: ... protocol=0 location=unix_release_sock+0x260/0x420 reason: SOCKET_CLOSE To be precise, unix_release_sock() is also called for a new child socket in unix_stream_connect() when something fails, but the new sk does not have skb in the recv queue then and no event is logged. Note that only tcp_inbound_ao_hash() uses a similar drop reason, SKB_DROP_REASON_TCP_CLOSE, and this can be generalised later. Signed-off-by: Kuniyuki Iwashima --- include/net/dropreason-core.h | 3 +++ net/unix/af_unix.c | 4 ++-- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/include/net/dropreason-core.h b/include/net/dropreason-core.h index efeae9f0f956..8823de6539d1 100644 --- a/include/net/dropreason-core.h +++ b/include/net/dropreason-core.h @@ -6,6 +6,7 @@ #define DEFINE_DROP_REASON(FN, FNe) \ FN(NOT_SPECIFIED) \ FN(NO_SOCKET) \ + FN(SOCKET_CLOSE) \ FN(SOCKET_FILTER) \ FN(SOCKET_RCVBUFF) \ FN(PKT_TOO_SMALL) \ @@ -137,6 +138,8 @@ enum skb_drop_reason { * 3) no valid child socket during 3WHS process */ SKB_DROP_REASON_NO_SOCKET, + /** @SKB_DROP_REASON_SOCKET_CLOSE: socket is close()d */ + SKB_DROP_REASON_SOCKET_CLOSE, /** @SKB_DROP_REASON_SOCKET_FILTER: dropped by socket filter */ SKB_DROP_REASON_SOCKET_FILTER, /** @SKB_DROP_REASON_SOCKET_RCVBUFF: socket receive buff is full */ diff --git a/net/unix/af_unix.c b/net/unix/af_unix.c index 8f2b605ce5b3..a05d25cc5545 100644 --- a/net/unix/af_unix.c +++ b/net/unix/af_unix.c @@ -715,8 +715,8 @@ static void unix_release_sock(struct sock *sk, int embrion) if (state == TCP_LISTEN) unix_release_sock(skb->sk, 1); - /* passed fds are erased in the kfree_skb hook */ - kfree_skb(skb); + /* passed fds are erased in the kfree_skb hook */ + kfree_skb_reason(skb, SKB_DROP_REASON_SOCKET_CLOSE); } if (path.dentry) From patchwork Sun Jan 12 04:08:02 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kuniyuki Iwashima X-Patchwork-Id: 13936208 X-Patchwork-Delegate: kuba@kernel.org Received: from smtp-fw-6001.amazon.com (smtp-fw-6001.amazon.com [52.95.48.154]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 9C53C6FB0 for ; Sun, 12 Jan 2025 04:09:48 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=52.95.48.154 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736654990; cv=none; b=TZxqo9cTVMsI2GailkTxfCRMTVc/HmX/gkqrZvbqYHFugLz5wM4e9zWOmjPeCrZbD19WJm+cw/w8HfuCRvekS+yjIblA7SEZ422W9Ho277keWUc6dMxIC0MpQBGwAHZI8E+7XP/zbpCKvWGLtd3Ke9W9D9sVHGgo5AoU5rh9+pc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736654990; c=relaxed/simple; bh=ka5tddW9vT2AYR1FOk3xX2fQ2qXzdd6fU4YEbrZOTzI=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=hM5/wevOateZAh0kyFJd5tbon+9LQ/BN3hvphNxd41kuLq3qgtyCSUJgqzuKwPtvMTN4JjI5qTQiXf84O8+g84ciUfovLngzWhR/t8Jwqn+QcIs/C+ByyxjlMsAYEfV9RVEXzkvggDRzaoiXlt9uUZWRzpqzMHV1Vu9xIRXs0l8= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=amazon.com; spf=pass smtp.mailfrom=amazon.co.jp; dkim=pass (1024-bit key) header.d=amazon.com header.i=@amazon.com header.b=JBE1zaBf; arc=none smtp.client-ip=52.95.48.154 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=amazon.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=amazon.co.jp Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=amazon.com header.i=@amazon.com header.b="JBE1zaBf" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amazon.com; i=@amazon.com; q=dns/txt; s=amazon201209; t=1736654989; x=1768190989; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=pvTppOl7MKVCY59BRjIRFn5kMxrLpjCsRsywSXQxCDE=; b=JBE1zaBfVCG7765/AUNW/CoaATnhuJk7x1/m8vg+vla4XTX9D07/3ltm BsRqZbHrtFyDDAHgKTuo667XrN/ydrZ/5FPJ1pMYy9cZXBCMQnzviGOgO Q7mOX43jryKqVUF2wdxnNIj7wF3Hvcs0aOPy1C09zHgp/K0O/m8TfoN5D A=; X-IronPort-AV: E=Sophos;i="6.12,308,1728950400"; d="scan'208";a="453550548" Received: from iad12-co-svc-p1-lb1-vlan2.amazon.com (HELO smtpout.prod.us-west-2.prod.farcaster.email.amazon.dev) ([10.43.8.2]) by smtp-border-fw-6001.iad6.amazon.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 12 Jan 2025 04:09:45 +0000 Received: from EX19MTAUWB001.ant.amazon.com [10.0.21.151:27003] by smtpin.naws.us-west-2.prod.farcaster.email.amazon.dev [10.0.42.134:2525] with esmtp (Farcaster) id 5208dfa4-e2cf-41f7-87d3-b02a034daf76; Sun, 12 Jan 2025 04:09:44 +0000 (UTC) X-Farcaster-Flow-ID: 5208dfa4-e2cf-41f7-87d3-b02a034daf76 Received: from EX19D004ANA001.ant.amazon.com (10.37.240.138) by EX19MTAUWB001.ant.amazon.com (10.250.64.248) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA) id 15.2.1258.39; Sun, 12 Jan 2025 04:09:44 +0000 Received: from 6c7e67c6786f.amazon.com (10.119.14.156) by EX19D004ANA001.ant.amazon.com (10.37.240.138) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA) id 15.2.1258.39; Sun, 12 Jan 2025 04:09:40 +0000 From: Kuniyuki Iwashima To: "David S. Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni , Simon Horman CC: Donald Hunter , Kuniyuki Iwashima , Kuniyuki Iwashima , Subject: [PATCH v2 net-next 03/11] af_unix: Set drop reason in unix_sock_destructor(). Date: Sun, 12 Jan 2025 13:08:02 +0900 Message-ID: <20250112040810.14145-4-kuniyu@amazon.com> X-Mailer: git-send-email 2.39.5 (Apple Git-154) In-Reply-To: <20250112040810.14145-1-kuniyu@amazon.com> References: <20250112040810.14145-1-kuniyu@amazon.com> Precedence: bulk X-Mailing-List: netdev@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-ClientProxiedBy: EX19D032UWB002.ant.amazon.com (10.13.139.190) To EX19D004ANA001.ant.amazon.com (10.37.240.138) X-Patchwork-Delegate: kuba@kernel.org unix_sock_destructor() is called as sk->sk_destruct() just before the socket is actually freed. Let's use SKB_DROP_REASON_SOCKET_CLOSE for skb_queue_purge(). Signed-off-by: Kuniyuki Iwashima --- net/unix/af_unix.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/net/unix/af_unix.c b/net/unix/af_unix.c index a05d25cc5545..41b99984008a 100644 --- a/net/unix/af_unix.c +++ b/net/unix/af_unix.c @@ -640,7 +640,7 @@ static void unix_sock_destructor(struct sock *sk) { struct unix_sock *u = unix_sk(sk); - skb_queue_purge(&sk->sk_receive_queue); + skb_queue_purge_reason(&sk->sk_receive_queue, SKB_DROP_REASON_SOCKET_CLOSE); DEBUG_NET_WARN_ON_ONCE(refcount_read(&sk->sk_wmem_alloc)); DEBUG_NET_WARN_ON_ONCE(!sk_unhashed(sk)); From patchwork Sun Jan 12 04:08:03 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kuniyuki Iwashima X-Patchwork-Id: 13936209 X-Patchwork-Delegate: kuba@kernel.org Received: from smtp-fw-52003.amazon.com (smtp-fw-52003.amazon.com [52.119.213.152]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 1A06B2941C for ; Sun, 12 Jan 2025 04:10:14 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=52.119.213.152 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736655016; cv=none; b=fSupEeSUNsetG74tEt0UrdMEFH8AhOyDTrrUjClNW1bvv0UhbAXBKhf9CXLwpDX+kGixxDceOUNMC/4j2AxtILa9v9FnnPZ4Gj7Hac2mqWcD62GpKY07jfCOnjHWcQp7Bebkn0xSgZ9dO/Z9CsId/nPD2EeIezrSHqECGBmsYF4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736655016; c=relaxed/simple; bh=0Ap47lpyfQBoMXsC4C5t0FTb9u7+ECgci6hK7CBTaMI=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=k+cfHrNAvGmA4tmqdVTIkM7P/vNKp4H/FOQ5j7cAnLJChtLzw33ubdirnXPwA3TH0H/2QdFzW73DL37gFLLps6aWQZjL1E7cKc3YBPDXPmvLG1Jo7b8P2I6Sj3L37J0h4TZ7px42aW3R1qPbUItlY50h0sXhfU3Hx/9yr2GMChU= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=amazon.com; spf=pass smtp.mailfrom=amazon.co.jp; dkim=pass (1024-bit key) header.d=amazon.com header.i=@amazon.com header.b=EXCBQ1Rc; arc=none smtp.client-ip=52.119.213.152 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=amazon.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=amazon.co.jp Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=amazon.com header.i=@amazon.com header.b="EXCBQ1Rc" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amazon.com; i=@amazon.com; q=dns/txt; s=amazon201209; t=1736655015; x=1768191015; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=WRvz/jt2K+XR/8BEV3sbpF+J4ezyxNZwPZiP0vIZEyk=; b=EXCBQ1RcF0Y7jOd/uAJqoqq5b3STXQFTKVTJrQiv/e4QeXUtR3mP0Gli P2u4cn3V37Vkni6A+m6E2R9leay9K3/KSjjmA0jMGOmXbh+Nn4FlHd5t1 F22sdVJHxFBSSRJV+7qsZuqXpTu2T31VCZK8YOdJ1y9DO+Hsu4VlSs4b+ E=; X-IronPort-AV: E=Sophos;i="6.12,308,1728950400"; d="scan'208";a="57045646" Received: from iad12-co-svc-p1-lb1-vlan3.amazon.com (HELO smtpout.prod.us-west-2.prod.farcaster.email.amazon.dev) ([10.43.8.6]) by smtp-border-fw-52003.iad7.amazon.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 12 Jan 2025 04:10:11 +0000 Received: from EX19MTAUWA001.ant.amazon.com [10.0.21.151:41166] by smtpin.naws.us-west-2.prod.farcaster.email.amazon.dev [10.0.16.12:2525] with esmtp (Farcaster) id 369e3303-9c9d-43b2-a845-918e1ee6291c; Sun, 12 Jan 2025 04:10:11 +0000 (UTC) X-Farcaster-Flow-ID: 369e3303-9c9d-43b2-a845-918e1ee6291c Received: from EX19D004ANA001.ant.amazon.com (10.37.240.138) by EX19MTAUWA001.ant.amazon.com (10.250.64.218) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA) id 15.2.1258.39; Sun, 12 Jan 2025 04:10:10 +0000 Received: from 6c7e67c6786f.amazon.com (10.119.14.156) by EX19D004ANA001.ant.amazon.com (10.37.240.138) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA) id 15.2.1258.39; Sun, 12 Jan 2025 04:10:07 +0000 From: Kuniyuki Iwashima To: "David S. Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni , Simon Horman CC: Donald Hunter , Kuniyuki Iwashima , Kuniyuki Iwashima , Subject: [PATCH v2 net-next 04/11] af_unix: Set drop reason in __unix_gc(). Date: Sun, 12 Jan 2025 13:08:03 +0900 Message-ID: <20250112040810.14145-5-kuniyu@amazon.com> X-Mailer: git-send-email 2.39.5 (Apple Git-154) In-Reply-To: <20250112040810.14145-1-kuniyu@amazon.com> References: <20250112040810.14145-1-kuniyu@amazon.com> Precedence: bulk X-Mailing-List: netdev@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-ClientProxiedBy: EX19D046UWB004.ant.amazon.com (10.13.139.164) To EX19D004ANA001.ant.amazon.com (10.37.240.138) X-Patchwork-Delegate: kuba@kernel.org Inflight file descriptors by SCM_RIGHTS hold references to the struct file. AF_UNIX sockets could hold references to each other, forming reference cycles. Once such sockets are close()d without the fd recv()ed, they will be unaccessible from userspace but remain in kernel. __unix_gc() garbage-collects skb with the dead file descriptors and frees them by __skb_queue_purge(). Let's set SKB_DROP_REASON_SOCKET_CLOSE there. # echo 1 > /sys/kernel/tracing/events/skb/kfree_skb/enable # python3 >>> from socket import * >>> from array import array >>> >>> # Create a reference cycle >>> s1 = socket(AF_UNIX, SOCK_DGRAM) >>> s1.bind('') >>> s1.sendmsg([b"nop"], [(SOL_SOCKET, SCM_RIGHTS, array("i", [s1.fileno()]))], 0, s1.getsockname()) >>> s1.close() >>> >>> # Trigger GC >>> s2 = socket(AF_UNIX) >>> s2.close() # cat /sys/kernel/tracing/trace_pipe ... kworker/u16:2-42 ... kfree_skb: ... location=__unix_gc+0x4ad/0x580 reason: SOCKET_CLOSE Signed-off-by: Kuniyuki Iwashima --- net/unix/garbage.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/net/unix/garbage.c b/net/unix/garbage.c index 0068e758be4d..9848b7b78701 100644 --- a/net/unix/garbage.c +++ b/net/unix/garbage.c @@ -573,7 +573,7 @@ static void __unix_gc(struct work_struct *work) UNIXCB(skb).fp->dead = true; } - __skb_queue_purge(&hitlist); + __skb_queue_purge_reason(&hitlist, SKB_DROP_REASON_SOCKET_CLOSE); skip_gc: WRITE_ONCE(gc_in_progress, false); } From patchwork Sun Jan 12 04:08:04 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kuniyuki Iwashima X-Patchwork-Id: 13936210 X-Patchwork-Delegate: kuba@kernel.org Received: from smtp-fw-9106.amazon.com (smtp-fw-9106.amazon.com [207.171.188.206]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 46CF614293 for ; Sun, 12 Jan 2025 04:10:43 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=207.171.188.206 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736655044; cv=none; b=ryyfZdP3tNr3AdHVZKGVfKflveSRcxU++GnkjV7iuLwbE+98dYNaZ1ChbOPZjoRMMK00/9er5RfPK99kgHjWpZWFsMsR16zEX+igP5W4H1zr5Zg+577yfd+ysamzVq6+9rczODey9+B5cg7yaem6S3ocHiaCGD+Cod9FcuWb4mY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736655044; c=relaxed/simple; bh=CIDS+Q6aNlIT1yDXhmZ3EAyn1U4O7v2HsaIp3sJV0NM=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=tvQvc+iRV1AH4doQw70iY8Y1DSe0O+8J//5RceI3BcTUF4wvZDWBHDm3M7QfSyeSaJD3zdG1i2CqenYTdTmldrrrJybzNEztsnzAquA8PdYSWljhh6lHjMmM9JswyMEeUdE2kK4cLi/wEDR5ECEVZNOCoMbUAwvq7DgFH9CZdLw= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=amazon.com; spf=pass smtp.mailfrom=amazon.co.jp; dkim=pass (1024-bit key) header.d=amazon.com header.i=@amazon.com header.b=B6k+ZKKz; arc=none smtp.client-ip=207.171.188.206 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=amazon.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=amazon.co.jp Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=amazon.com header.i=@amazon.com header.b="B6k+ZKKz" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amazon.com; i=@amazon.com; q=dns/txt; s=amazon201209; t=1736655044; x=1768191044; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=mg59p3COVrzPdxR5HSibX8dKEzv7bL6fBavvhjfvC/0=; b=B6k+ZKKzglqff9qMC56xgW/2lprzBnaIDpPlpoH7ggeIRDaRJId2YM0d B7qJRmLs56Rb259AFi30QhqjT3AdFlqgbcBlr7OcmY7K5TW0ULRepKUVT MNmeJWNkRUPq15fwyB/J6KLpj00dJ5Smj816RIsEgf5ir0gqL2NdUDMzq E=; X-IronPort-AV: E=Sophos;i="6.12,308,1728950400"; d="scan'208";a="790432412" Received: from pdx4-co-svc-p1-lb2-vlan2.amazon.com (HELO smtpout.prod.us-west-2.prod.farcaster.email.amazon.dev) ([10.25.36.210]) by smtp-border-fw-9106.sea19.amazon.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 12 Jan 2025 04:10:39 +0000 Received: from EX19MTAUWC001.ant.amazon.com [10.0.7.35:41064] by smtpin.naws.us-west-2.prod.farcaster.email.amazon.dev [10.0.29.65:2525] with esmtp (Farcaster) id ea7ca1a5-3dcc-46f4-8c71-c60689737d03; Sun, 12 Jan 2025 04:10:37 +0000 (UTC) X-Farcaster-Flow-ID: ea7ca1a5-3dcc-46f4-8c71-c60689737d03 Received: from EX19D004ANA001.ant.amazon.com (10.37.240.138) by EX19MTAUWC001.ant.amazon.com (10.250.64.174) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA) id 15.2.1258.39; Sun, 12 Jan 2025 04:10:37 +0000 Received: from 6c7e67c6786f.amazon.com (10.119.14.156) by EX19D004ANA001.ant.amazon.com (10.37.240.138) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA) id 15.2.1258.39; Sun, 12 Jan 2025 04:10:33 +0000 From: Kuniyuki Iwashima To: "David S. Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni , Simon Horman CC: Donald Hunter , Kuniyuki Iwashima , Kuniyuki Iwashima , Subject: [PATCH v2 net-next 05/11] af_unix: Set drop reason in unix_stream_connect(). Date: Sun, 12 Jan 2025 13:08:04 +0900 Message-ID: <20250112040810.14145-6-kuniyu@amazon.com> X-Mailer: git-send-email 2.39.5 (Apple Git-154) In-Reply-To: <20250112040810.14145-1-kuniyu@amazon.com> References: <20250112040810.14145-1-kuniyu@amazon.com> Precedence: bulk X-Mailing-List: netdev@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-ClientProxiedBy: EX19D046UWB002.ant.amazon.com (10.13.139.181) To EX19D004ANA001.ant.amazon.com (10.37.240.138) X-Patchwork-Delegate: kuba@kernel.org connect() to a SOCK_STREAM socket could fail for various reasons. Let's set drop reasons respectively: * NO_SOCKET : No listening socket found * RCV_SHUTDOWN : The listening socket called shutdown(SHUT_RD) * SOCKET_RCVBUFF : The listening socket's accept queue is full * INVALID_STATE : The client is in TCP_ESTABLISHED or TCP_LISTEN * SECURITY_HOOK : LSM refused connect() Signed-off-by: Kuniyuki Iwashima --- include/net/dropreason-core.h | 6 ++++++ net/unix/af_unix.c | 22 ++++++++++++++++++---- 2 files changed, 24 insertions(+), 4 deletions(-) diff --git a/include/net/dropreason-core.h b/include/net/dropreason-core.h index 8823de6539d1..1b5e962f7f33 100644 --- a/include/net/dropreason-core.h +++ b/include/net/dropreason-core.h @@ -8,7 +8,9 @@ FN(NO_SOCKET) \ FN(SOCKET_CLOSE) \ FN(SOCKET_FILTER) \ + FN(SOCKET_INVALID_STATE) \ FN(SOCKET_RCVBUFF) \ + FN(SOCKET_RCV_SHUTDOWN) \ FN(PKT_TOO_SMALL) \ FN(TCP_CSUM) \ FN(UDP_CSUM) \ @@ -142,8 +144,12 @@ enum skb_drop_reason { SKB_DROP_REASON_SOCKET_CLOSE, /** @SKB_DROP_REASON_SOCKET_FILTER: dropped by socket filter */ SKB_DROP_REASON_SOCKET_FILTER, + /** @SKB_DROP_REASON_SOCKET_INVALID_STATE: sk->sk_state is invalid. */ + SKB_DROP_REASON_SOCKET_INVALID_STATE, /** @SKB_DROP_REASON_SOCKET_RCVBUFF: socket receive buff is full */ SKB_DROP_REASON_SOCKET_RCVBUFF, + /** @SKB_DROP_REASON_SOCKET_RCV_SHUTDOWN: socket is shutdown(SHUT_RD) */ + SKB_DROP_REASON_SOCKET_RCV_SHUTDOWN, /** @SKB_DROP_REASON_PKT_TOO_SMALL: packet size is too small */ SKB_DROP_REASON_PKT_TOO_SMALL, /** @SKB_DROP_REASON_TCP_CSUM: TCP checksum error */ diff --git a/net/unix/af_unix.c b/net/unix/af_unix.c index 41b99984008a..b190ea8b8e9d 100644 --- a/net/unix/af_unix.c +++ b/net/unix/af_unix.c @@ -1534,6 +1534,7 @@ static int unix_stream_connect(struct socket *sock, struct sockaddr *uaddr, struct sock *sk = sock->sk, *newsk = NULL, *other = NULL; struct unix_sock *u = unix_sk(sk), *newu, *otheru; struct net *net = sock_net(sk); + enum skb_drop_reason reason; struct sk_buff *skb = NULL; unsigned char state; long timeo; @@ -1581,6 +1582,7 @@ static int unix_stream_connect(struct socket *sock, struct sockaddr *uaddr, other = unix_find_other(net, sunaddr, addr_len, sk->sk_type); if (IS_ERR(other)) { err = PTR_ERR(other); + reason = SKB_DROP_REASON_NO_SOCKET; goto out_free_skb; } @@ -1593,15 +1595,22 @@ static int unix_stream_connect(struct socket *sock, struct sockaddr *uaddr, goto restart; } - if (other->sk_state != TCP_LISTEN || - other->sk_shutdown & RCV_SHUTDOWN) { + if (other->sk_state != TCP_LISTEN) { err = -ECONNREFUSED; + reason = SKB_DROP_REASON_NO_SOCKET; + goto out_unlock; + } + + if (other->sk_shutdown & RCV_SHUTDOWN) { + err = -ECONNREFUSED; + reason = SKB_DROP_REASON_SOCKET_RCV_SHUTDOWN; goto out_unlock; } if (unix_recvq_full_lockless(other)) { if (!timeo) { err = -EAGAIN; + reason = SKB_DROP_REASON_SOCKET_RCVBUFF; goto out_unlock; } @@ -1609,8 +1618,10 @@ static int unix_stream_connect(struct socket *sock, struct sockaddr *uaddr, sock_put(other); err = sock_intr_errno(timeo); - if (signal_pending(current)) + if (signal_pending(current)) { + reason = SKB_DROP_REASON_SOCKET_RCVBUFF; goto out_free_skb; + } goto restart; } @@ -1621,6 +1632,7 @@ static int unix_stream_connect(struct socket *sock, struct sockaddr *uaddr, state = READ_ONCE(sk->sk_state); if (unlikely(state != TCP_CLOSE)) { err = state == TCP_ESTABLISHED ? -EISCONN : -EINVAL; + reason = SKB_DROP_REASON_SOCKET_INVALID_STATE; goto out_unlock; } @@ -1629,12 +1641,14 @@ static int unix_stream_connect(struct socket *sock, struct sockaddr *uaddr, if (unlikely(sk->sk_state != TCP_CLOSE)) { err = sk->sk_state == TCP_ESTABLISHED ? -EISCONN : -EINVAL; unix_state_unlock(sk); + reason = SKB_DROP_REASON_SOCKET_INVALID_STATE; goto out_unlock; } err = security_unix_stream_connect(sk, other, newsk); if (err) { unix_state_unlock(sk); + reason = SKB_DROP_REASON_SECURITY_HOOK; goto out_unlock; } @@ -1699,7 +1713,7 @@ static int unix_stream_connect(struct socket *sock, struct sockaddr *uaddr, unix_state_unlock(other); sock_put(other); out_free_skb: - kfree_skb(skb); + kfree_skb_reason(skb, reason); out_free_sk: unix_release_sock(newsk, 0); out: From patchwork Sun Jan 12 04:08:05 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kuniyuki Iwashima X-Patchwork-Id: 13936211 X-Patchwork-Delegate: kuba@kernel.org Received: from smtp-fw-52005.amazon.com (smtp-fw-52005.amazon.com [52.119.213.156]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 0399A6FB0 for ; Sun, 12 Jan 2025 04:11:11 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=52.119.213.156 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736655074; cv=none; b=Gz5c8cIpJTLrXcUOGdpXV/OsHF3q0bUGYZzPWsw6BNzx5i7iFxfnRoeTU4D0B6F+kd9VJUnPje4wO19Lx+CDWQW9lap2QP4CSFJDkNf5jTXt9mWQj2shoLHpeITxGDnqaB4WLMjWIBcoCX1n4Mu2BLvBxz5e3yHt2VtZhBguhKc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736655074; c=relaxed/simple; bh=cQ1G3C2OcrFaEoFueeDlrVp+yfV5Cviip3JgUG/ojig=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=R5Q2S2+IfzBrDqemjK/YQHD9ANCMGlpbu+a+3hKEDvJIYIiAdCbo5sevahilTD6BFsWj0F5XYT+FMn7OT67LR2szvN9DqGgHrgEZ/lkk4kgsOFQ4GbJb07xGyjBU+I9knYecogwytDZ+0u+8OZLV4EiOoSjZ/cwshwuZl/ESY1c= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=amazon.com; spf=pass smtp.mailfrom=amazon.co.jp; dkim=pass (1024-bit key) header.d=amazon.com header.i=@amazon.com header.b=OJnwVFdV; arc=none smtp.client-ip=52.119.213.156 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=amazon.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=amazon.co.jp Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=amazon.com header.i=@amazon.com header.b="OJnwVFdV" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amazon.com; i=@amazon.com; q=dns/txt; s=amazon201209; t=1736655073; x=1768191073; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=Yq25I1r294buJuzBOSq9Xu6ix7QkaDslqAR9CMz5uFw=; b=OJnwVFdV7vrU77tjTr/c+K8wHRaCbu7OOtfkB5PGHoUmqywJjnSSUVVu YpHkDQ0F3X1QlKmoA3QotIyPYvXyu5YmjRUiIBPsY9tG0/K9ND7k12W7v XSCXT2qpN6XPht0i64MW/wrF7bz0Ov/jXpe6h7fNYTFqgXgvBe9/Wxyrs Q=; X-IronPort-AV: E=Sophos;i="6.12,308,1728950400"; d="scan'208";a="710225674" Received: from iad12-co-svc-p1-lb1-vlan3.amazon.com (HELO smtpout.prod.us-west-2.prod.farcaster.email.amazon.dev) ([10.43.8.6]) by smtp-border-fw-52005.iad7.amazon.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 12 Jan 2025 04:11:09 +0000 Received: from EX19MTAUWB002.ant.amazon.com [10.0.21.151:9658] by smtpin.naws.us-west-2.prod.farcaster.email.amazon.dev [10.0.63.9:2525] with esmtp (Farcaster) id 7ce7da8c-0e25-4314-93d5-7caf79c96d30; Sun, 12 Jan 2025 04:11:08 +0000 (UTC) X-Farcaster-Flow-ID: 7ce7da8c-0e25-4314-93d5-7caf79c96d30 Received: from EX19D004ANA001.ant.amazon.com (10.37.240.138) by EX19MTAUWB002.ant.amazon.com (10.250.64.231) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA) id 15.2.1258.39; Sun, 12 Jan 2025 04:11:04 +0000 Received: from 6c7e67c6786f.amazon.com (10.119.14.156) by EX19D004ANA001.ant.amazon.com (10.37.240.138) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA) id 15.2.1258.39; Sun, 12 Jan 2025 04:11:00 +0000 From: Kuniyuki Iwashima To: "David S. Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni , Simon Horman CC: Donald Hunter , Kuniyuki Iwashima , Kuniyuki Iwashima , Subject: [PATCH v2 net-next 06/11] af_unix: Set drop reason in unix_stream_sendmsg(). Date: Sun, 12 Jan 2025 13:08:05 +0900 Message-ID: <20250112040810.14145-7-kuniyu@amazon.com> X-Mailer: git-send-email 2.39.5 (Apple Git-154) In-Reply-To: <20250112040810.14145-1-kuniyu@amazon.com> References: <20250112040810.14145-1-kuniyu@amazon.com> Precedence: bulk X-Mailing-List: netdev@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-ClientProxiedBy: EX19D031UWC003.ant.amazon.com (10.13.139.252) To EX19D004ANA001.ant.amazon.com (10.37.240.138) X-Patchwork-Delegate: kuba@kernel.org sendmsg() to a SOCK_STREAM socket could fail for various reasons. Let's set drop reasons respectively. * NOMEM : Failed to allocate SCM_RIGHTS-related structs * UNIX_INFLIGHT_FD_LIMIT : The number of inflight fd reached RLIMIT_NOFILE * SKB_UCOPY_FAULT : Failed to copy data from iov_iter to skb * SOCKET_CLOSE : The peer socket was close()d * SOCKET_RCV_SHUTDOWN : The peer socket called shutdown(SHUT_RD) unix_scm_err_to_reason() will be reused in queue_oob() and unix_dgram_sendmsg(). While at it, size and data_len are moved to the while loop scope. Signed-off-by: Kuniyuki Iwashima --- include/net/dropreason-core.h | 6 +++++ net/unix/af_unix.c | 50 +++++++++++++++++++++++++++-------- 2 files changed, 45 insertions(+), 11 deletions(-) diff --git a/include/net/dropreason-core.h b/include/net/dropreason-core.h index 1b5e962f7f33..dea6bbe3ceaa 100644 --- a/include/net/dropreason-core.h +++ b/include/net/dropreason-core.h @@ -11,6 +11,7 @@ FN(SOCKET_INVALID_STATE) \ FN(SOCKET_RCVBUFF) \ FN(SOCKET_RCV_SHUTDOWN) \ + FN(UNIX_INFLIGHT_FD_LIMIT) \ FN(PKT_TOO_SMALL) \ FN(TCP_CSUM) \ FN(UDP_CSUM) \ @@ -150,6 +151,11 @@ enum skb_drop_reason { SKB_DROP_REASON_SOCKET_RCVBUFF, /** @SKB_DROP_REASON_SOCKET_RCV_SHUTDOWN: socket is shutdown(SHUT_RD) */ SKB_DROP_REASON_SOCKET_RCV_SHUTDOWN, + /** + * @SKB_DROP_REASON_UNIX_INFLIGHT_FD_LIMIT: too many file descriptors + * are passed via SCM_RIGHTS but not yet received, reaching RLIMIT_NOFILE. + */ + SKB_DROP_REASON_UNIX_INFLIGHT_FD_LIMIT, /** @SKB_DROP_REASON_PKT_TOO_SMALL: packet size is too small */ SKB_DROP_REASON_PKT_TOO_SMALL, /** @SKB_DROP_REASON_TCP_CSUM: TCP checksum error */ diff --git a/net/unix/af_unix.c b/net/unix/af_unix.c index b190ea8b8e9d..8c8d8fc3cb94 100644 --- a/net/unix/af_unix.c +++ b/net/unix/af_unix.c @@ -1907,6 +1907,22 @@ static int unix_scm_to_skb(struct scm_cookie *scm, struct sk_buff *skb, bool sen return err; } +static enum skb_drop_reason unix_scm_err_to_reason(int err) +{ + switch (err) { + case -ENOMEM: + return SKB_DROP_REASON_NOMEM; + case -ETOOMANYREFS: + return SKB_DROP_REASON_UNIX_INFLIGHT_FD_LIMIT; + } + + DEBUG_NET_WARN_ONCE(1, + "Define a drop reason for %d in unix_scm_to_skb().", + err); + + return SKB_DROP_REASON_NOT_SPECIFIED; +} + static bool unix_passcred_enabled(const struct socket *sock, const struct sock *other) { @@ -2249,14 +2265,13 @@ static int queue_oob(struct socket *sock, struct msghdr *msg, struct sock *other static int unix_stream_sendmsg(struct socket *sock, struct msghdr *msg, size_t len) { + enum skb_drop_reason reason; struct sock *sk = sock->sk; struct sock *other = NULL; - int err, size; - struct sk_buff *skb; - int sent = 0; struct scm_cookie scm; bool fds_sent = false; - int data_len; + struct sk_buff *skb; + int err, sent = 0; err = scm_send(sock, msg, &scm, false); if (err < 0) @@ -2294,7 +2309,8 @@ static int unix_stream_sendmsg(struct socket *sock, struct msghdr *msg, } while (sent < len) { - size = len - sent; + int size = len - sent; + int data_len; if (unlikely(msg->msg_flags & MSG_SPLICE_PAGES)) { skb = sock_alloc_send_pskb(sk, 0, 0, @@ -2320,8 +2336,10 @@ static int unix_stream_sendmsg(struct socket *sock, struct msghdr *msg, /* Only send the fds in the first buffer */ err = unix_scm_to_skb(&scm, skb, !fds_sent); - if (err < 0) + if (err < 0) { + reason = unix_scm_err_to_reason(err); goto out_free; + } fds_sent = true; @@ -2329,8 +2347,10 @@ static int unix_stream_sendmsg(struct socket *sock, struct msghdr *msg, skb->ip_summed = CHECKSUM_UNNECESSARY; err = skb_splice_from_iter(skb, &msg->msg_iter, size, sk->sk_allocation); - if (err < 0) + if (err < 0) { + reason = SKB_DROP_REASON_SKB_UCOPY_FAULT; goto out_free; + } size = err; refcount_add(size, &sk->sk_wmem_alloc); @@ -2339,15 +2359,23 @@ static int unix_stream_sendmsg(struct socket *sock, struct msghdr *msg, skb->data_len = data_len; skb->len = size; err = skb_copy_datagram_from_iter(skb, 0, &msg->msg_iter, size); - if (err) + if (err) { + reason = SKB_DROP_REASON_SKB_UCOPY_FAULT; goto out_free; + } } unix_state_lock(other); - if (sock_flag(other, SOCK_DEAD) || - (other->sk_shutdown & RCV_SHUTDOWN)) + if (sock_flag(other, SOCK_DEAD)) { + reason = SKB_DROP_REASON_SOCKET_CLOSE; + goto out_pipe; + } + + if (other->sk_shutdown & RCV_SHUTDOWN) { + reason = SKB_DROP_REASON_SOCKET_RCV_SHUTDOWN; goto out_pipe; + } maybe_add_creds(skb, sock, other); scm_stat_add(other, skb); @@ -2376,7 +2404,7 @@ static int unix_stream_sendmsg(struct socket *sock, struct msghdr *msg, send_sig(SIGPIPE, current, 0); err = -EPIPE; out_free: - kfree_skb(skb); + kfree_skb_reason(skb, reason); out_err: scm_destroy(&scm); return sent ? : err; From patchwork Sun Jan 12 04:08:06 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kuniyuki Iwashima X-Patchwork-Id: 13936212 X-Patchwork-Delegate: kuba@kernel.org Received: from smtp-fw-9106.amazon.com (smtp-fw-9106.amazon.com [207.171.188.206]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 408E31E517 for ; Sun, 12 Jan 2025 04:11:32 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=207.171.188.206 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736655093; cv=none; b=p2pevkd69dxRpNPiSQp4hXf1KyJ473xhnEJCAiG/Euobj1EX0nRWOU7M3FFDGuni1psr+moJo21vdQQGfjPBe4sSz3BSw400ghJzIWkhlOq0Jpb3ZVxBLWYCDin9BVlibFtQqcGydBOoHOTMELv9xOp7nvjYX48HYYjGYzJbFUA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736655093; c=relaxed/simple; bh=2SuYl/RkIG9m7kBqVOcg1il7berthmOidtFyNdy2WD4=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=eEl4ehqbwYYAUVb3RS+Wyhlf3bPBIQwc5Y69j655ohGcexjfvM6B11xTZHiw3I+g/LOjy2vyjWnJX5p3KbDGzfdlHWNIJIdc8ga9MmuoOcW1WO48/HHfNKiK9qaaG1ixIHpE6pV1xqg1Qznfny3vORyz6yViYCOW/ss8BHyKVCk= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=amazon.com; spf=pass smtp.mailfrom=amazon.co.jp; dkim=pass (1024-bit key) header.d=amazon.com header.i=@amazon.com header.b=FOl16amd; arc=none smtp.client-ip=207.171.188.206 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=amazon.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=amazon.co.jp Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=amazon.com header.i=@amazon.com header.b="FOl16amd" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amazon.com; i=@amazon.com; q=dns/txt; s=amazon201209; t=1736655093; x=1768191093; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=Gxm8kJ8emMVLkb2JWLuQNUyltOYqXwlla4FGOtfGA2A=; b=FOl16amdEWnIKIPf3e59Mp/NVoTlyieO9R57ecgZa99sqkp3kamr65Bm SoegFHHubeBeXywqHiimm5ETTBhgynVDEJsv7jOJHWkgJktdX7XfNZWRS p6V9kgr7rs5+6c/vNamuEwAA9gARc9sI75kHSZ4uXgEq7TrIHMLrVd10E s=; X-IronPort-AV: E=Sophos;i="6.12,308,1728950400"; d="scan'208";a="790432459" Received: from pdx4-co-svc-p1-lb2-vlan2.amazon.com (HELO smtpout.prod.us-west-2.prod.farcaster.email.amazon.dev) ([10.25.36.210]) by smtp-border-fw-9106.sea19.amazon.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 12 Jan 2025 04:11:32 +0000 Received: from EX19MTAUWC002.ant.amazon.com [10.0.7.35:49157] by smtpin.naws.us-west-2.prod.farcaster.email.amazon.dev [10.0.29.114:2525] with esmtp (Farcaster) id 199b919c-2447-47dd-bedd-619ec1c9acd1; Sun, 12 Jan 2025 04:11:31 +0000 (UTC) X-Farcaster-Flow-ID: 199b919c-2447-47dd-bedd-619ec1c9acd1 Received: from EX19D004ANA001.ant.amazon.com (10.37.240.138) by EX19MTAUWC002.ant.amazon.com (10.250.64.143) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA) id 15.2.1258.39; Sun, 12 Jan 2025 04:11:31 +0000 Received: from 6c7e67c6786f.amazon.com (10.119.14.156) by EX19D004ANA001.ant.amazon.com (10.37.240.138) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA) id 15.2.1258.39; Sun, 12 Jan 2025 04:11:27 +0000 From: Kuniyuki Iwashima To: "David S. Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni , Simon Horman CC: Donald Hunter , Kuniyuki Iwashima , Kuniyuki Iwashima , Subject: [PATCH v2 net-next 07/11] af_unix: Set drop reason in queue_oob(). Date: Sun, 12 Jan 2025 13:08:06 +0900 Message-ID: <20250112040810.14145-8-kuniyu@amazon.com> X-Mailer: git-send-email 2.39.5 (Apple Git-154) In-Reply-To: <20250112040810.14145-1-kuniyu@amazon.com> References: <20250112040810.14145-1-kuniyu@amazon.com> Precedence: bulk X-Mailing-List: netdev@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-ClientProxiedBy: EX19D046UWA004.ant.amazon.com (10.13.139.76) To EX19D004ANA001.ant.amazon.com (10.37.240.138) X-Patchwork-Delegate: kuba@kernel.org sendmsg(MSG_OOB) to a SOCK_STREAM socket could fail for various reasons. Let's set drop reasons respectively. The drop reasons are exactly the same as in the previous patch. Signed-off-by: Kuniyuki Iwashima --- net/unix/af_unix.c | 37 ++++++++++++++++++++++++------------- 1 file changed, 24 insertions(+), 13 deletions(-) diff --git a/net/unix/af_unix.c b/net/unix/af_unix.c index 8c8d8fc3cb94..8623e3368c45 100644 --- a/net/unix/af_unix.c +++ b/net/unix/af_unix.c @@ -2216,34 +2216,37 @@ static int queue_oob(struct socket *sock, struct msghdr *msg, struct sock *other struct scm_cookie *scm, bool fds_sent) { struct unix_sock *ousk = unix_sk(other); + enum skb_drop_reason reason; struct sk_buff *skb; - int err = 0; + int err; skb = sock_alloc_send_skb(sock->sk, 1, msg->msg_flags & MSG_DONTWAIT, &err); - if (!skb) - return err; + goto out; err = unix_scm_to_skb(scm, skb, !fds_sent); if (err < 0) { - kfree_skb(skb); - return err; + reason = unix_scm_err_to_reason(err); + goto out_free; } + skb_put(skb, 1); err = skb_copy_datagram_from_iter(skb, 0, &msg->msg_iter, 1); - if (err) { - kfree_skb(skb); - return err; + reason = SKB_DROP_REASON_SKB_UCOPY_FAULT; + goto out_free; } unix_state_lock(other); - if (sock_flag(other, SOCK_DEAD) || - (other->sk_shutdown & RCV_SHUTDOWN)) { - unix_state_unlock(other); - kfree_skb(skb); - return -EPIPE; + if (sock_flag(other, SOCK_DEAD)) { + reason = SKB_DROP_REASON_SOCKET_CLOSE; + goto out_unlock; + } + + if (other->sk_shutdown & RCV_SHUTDOWN) { + reason = SKB_DROP_REASON_SOCKET_RCV_SHUTDOWN; + goto out_unlock; } maybe_add_creds(skb, sock, other); @@ -2258,6 +2261,14 @@ static int queue_oob(struct socket *sock, struct msghdr *msg, struct sock *other unix_state_unlock(other); other->sk_data_ready(other); + return 0; + +out_unlock: + unix_state_unlock(other); + err = -EPIPE; +out_free: + kfree_skb_reason(skb, reason); +out: return err; } #endif From patchwork Sun Jan 12 04:08:07 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kuniyuki Iwashima X-Patchwork-Id: 13936213 X-Patchwork-Delegate: kuba@kernel.org Received: from smtp-fw-80009.amazon.com (smtp-fw-80009.amazon.com [99.78.197.220]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id DEDF86FB0 for ; Sun, 12 Jan 2025 04:12:02 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=99.78.197.220 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736655124; cv=none; b=fd/kdAHlzi43VT3sQyA5zsg7P1ydqX3MBfULqUECrEf4GA72ZHZZZk0GNUTamoKxKFywXMwI0oXsyjQyYhVJjFGP+zLqx6VLsjVqYj6haqk1+RFL1gqnVaHC7vUJ6UbTubSb4Blvpi1/Fdh7e7ZzDDedDU7uwyInXG9CnZqUv/8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736655124; c=relaxed/simple; bh=EW5qHUoHq4YzzUlr9G2dw/sl88BXkj3oSanRFIQpbK0=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=o/AeFjYJu83o7SIBqDI5PkvKcW57qQNLy+9m6ZZj9XbvPJX14wQDfCAb1wR1H5HklcncqcbCi8XM4gV53xMS28SqAyrcj24xdsLg8psDjLPt9hIcAshF19CmcDutekZKacC4/g52DIHboYHbKcSEPNJFw5WiVupQo0Wa2NFuST8= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=amazon.com; spf=pass smtp.mailfrom=amazon.co.jp; dkim=pass (1024-bit key) header.d=amazon.com header.i=@amazon.com header.b=vZR9Ft/6; arc=none smtp.client-ip=99.78.197.220 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=amazon.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=amazon.co.jp Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=amazon.com header.i=@amazon.com header.b="vZR9Ft/6" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amazon.com; i=@amazon.com; q=dns/txt; s=amazon201209; t=1736655122; x=1768191122; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=qoHqRzyOsC6QtQIsgxhMnQ4/pXmzLS85a9CGq3xUQ14=; b=vZR9Ft/6MN+7Eb63HyC0eBLYMDvqHjY5EbSpsLg3QC1KZlCkB63cGy1V c5oMn9FlYK6sJEKSLzLLu5e9Tx5X2mo5Z+PwsuLXiYvGdaMG5KMRIYMNU c/tab9DkRhL9wUEVo70+68L4REbK43zR5G/x3WOTpDSIETgSnpslxfEae U=; X-IronPort-AV: E=Sophos;i="6.12,308,1728950400"; d="scan'208";a="163513194" Received: from pdx4-co-svc-p1-lb2-vlan2.amazon.com (HELO smtpout.prod.us-west-2.prod.farcaster.email.amazon.dev) ([10.25.36.210]) by smtp-border-fw-80009.pdx80.corp.amazon.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 12 Jan 2025 04:12:01 +0000 Received: from EX19MTAUWA001.ant.amazon.com [10.0.7.35:60733] by smtpin.naws.us-west-2.prod.farcaster.email.amazon.dev [10.0.16.12:2525] with esmtp (Farcaster) id 9026c126-bf3d-4305-9b82-86d83633b86e; Sun, 12 Jan 2025 04:12:00 +0000 (UTC) X-Farcaster-Flow-ID: 9026c126-bf3d-4305-9b82-86d83633b86e Received: from EX19D004ANA001.ant.amazon.com (10.37.240.138) by EX19MTAUWA001.ant.amazon.com (10.250.64.204) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA) id 15.2.1258.39; Sun, 12 Jan 2025 04:11:57 +0000 Received: from 6c7e67c6786f.amazon.com (10.119.14.156) by EX19D004ANA001.ant.amazon.com (10.37.240.138) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA) id 15.2.1258.39; Sun, 12 Jan 2025 04:11:54 +0000 From: Kuniyuki Iwashima To: "David S. Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni , Simon Horman CC: Donald Hunter , Kuniyuki Iwashima , Kuniyuki Iwashima , Subject: [PATCH v2 net-next 08/11] af_unix: Set drop reason in manage_oob(). Date: Sun, 12 Jan 2025 13:08:07 +0900 Message-ID: <20250112040810.14145-9-kuniyu@amazon.com> X-Mailer: git-send-email 2.39.5 (Apple Git-154) In-Reply-To: <20250112040810.14145-1-kuniyu@amazon.com> References: <20250112040810.14145-1-kuniyu@amazon.com> Precedence: bulk X-Mailing-List: netdev@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-ClientProxiedBy: EX19D031UWC002.ant.amazon.com (10.13.139.212) To EX19D004ANA001.ant.amazon.com (10.37.240.138) X-Patchwork-Delegate: kuba@kernel.org AF_UNIX SOCK_STREAM socket supports MSG_OOB. When OOB data is sent to a socket, recv() will break at that point. If the next recv() does not have MSG_OOB, the normal data following the OOB data is returned. Then, the OOB skb is dropped. Let's define a new drop reason for that case in manage_oob(). # echo 1 > /sys/kernel/tracing/events/skb/kfree_skb/enable # python3 >>> from socket import * >>> s1, s2 = socketpair(AF_UNIX) >>> s1.send(b'a', MSG_OOB) >>> s1.send(b'b') >>> s2.recv(2) b'b' # cat /sys/kernel/tracing/trace_pipe ... python3-223 ... kfree_skb: ... location=unix_stream_read_generic+0x59e/0xc20 reason: UNIX_SKIP_OOB Signed-off-by: Kuniyuki Iwashima --- include/net/dropreason-core.h | 6 ++++++ net/unix/af_unix.c | 2 +- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/include/net/dropreason-core.h b/include/net/dropreason-core.h index dea6bbe3ceaa..43e20acaa98a 100644 --- a/include/net/dropreason-core.h +++ b/include/net/dropreason-core.h @@ -12,6 +12,7 @@ FN(SOCKET_RCVBUFF) \ FN(SOCKET_RCV_SHUTDOWN) \ FN(UNIX_INFLIGHT_FD_LIMIT) \ + FN(UNIX_SKIP_OOB) \ FN(PKT_TOO_SMALL) \ FN(TCP_CSUM) \ FN(UDP_CSUM) \ @@ -156,6 +157,11 @@ enum skb_drop_reason { * are passed via SCM_RIGHTS but not yet received, reaching RLIMIT_NOFILE. */ SKB_DROP_REASON_UNIX_INFLIGHT_FD_LIMIT, + /** + * @SKB_DROP_REASON_UNIX_SKIP_OOB: Out-Of-Band data is skipped by + * recv() without MSG_OOB so dropped. + */ + SKB_DROP_REASON_UNIX_SKIP_OOB, /** @SKB_DROP_REASON_PKT_TOO_SMALL: packet size is too small */ SKB_DROP_REASON_PKT_TOO_SMALL, /** @SKB_DROP_REASON_TCP_CSUM: TCP checksum error */ diff --git a/net/unix/af_unix.c b/net/unix/af_unix.c index 8623e3368c45..efd1b83b152a 100644 --- a/net/unix/af_unix.c +++ b/net/unix/af_unix.c @@ -2748,7 +2748,7 @@ static struct sk_buff *manage_oob(struct sk_buff *skb, struct sock *sk, spin_unlock(&sk->sk_receive_queue.lock); consume_skb(read_skb); - kfree_skb(unread_skb); + kfree_skb_reason(unread_skb, SKB_DROP_REASON_UNIX_SKIP_OOB); return skb; } From patchwork Sun Jan 12 04:08:08 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kuniyuki Iwashima X-Patchwork-Id: 13936214 X-Patchwork-Delegate: kuba@kernel.org Received: from smtp-fw-52004.amazon.com (smtp-fw-52004.amazon.com [52.119.213.154]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 903D26FB0 for ; Sun, 12 Jan 2025 04:12:28 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=52.119.213.154 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736655150; cv=none; b=ufNS1tab1KzdrT23I7VkCV6gJY01w0wRBgsYietriA32AUxdNjrfTxgF71ZF3w4sAj9RK6DB7gglVhOGWls3+D+G8BflGjpqfzijvim4Mx18goHBAYsP3/5QE85S6f5Cat6A+c5KTKvx3wtxAh1MMGVl8UQj7OHh+unouZc7m6Q= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736655150; c=relaxed/simple; bh=AccllP+Cpxs4Imcd4zXg2JkBsTq1fRBvhGakV8IIqow=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=JOxbdNwcKcPS5S2Nm8uGcdywBXBuuLAYnZWaqd06X7tS2wREfmLiGFJZoEJbIdMAfwA/+r3XI2JikgBPMfjiCDaB93luqp18Y6uocg1AyfgXBpnuLgiUEuttdq5WOoUvlxPoBHn/UiFMoYVSDjr5AFHdEYUaz7E94FZqigViseU= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=amazon.com; spf=pass smtp.mailfrom=amazon.co.jp; dkim=pass (1024-bit key) header.d=amazon.com header.i=@amazon.com header.b=CfQC0RPu; arc=none smtp.client-ip=52.119.213.154 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=amazon.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=amazon.co.jp Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=amazon.com header.i=@amazon.com header.b="CfQC0RPu" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amazon.com; i=@amazon.com; q=dns/txt; s=amazon201209; t=1736655149; x=1768191149; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=QqGe80cPio8XAhljA/c8PqPGeu0wHcGL5mCeCIYDaKs=; b=CfQC0RPu1yYKQu2yHjljvnr90CCWPZeZ2E35wop7X7w2SZ/6l9hehhGh 3QJN/vtfb50pB4Dz4oa3d6p3tLQbm/NChgtTg231IcNNPGoDSKkONtG+f TkD0hPi45hsGp6jUNKgF0sdk9FCwTS9f7Ve5c9NQrnoEj4X0fxTKbJ7dR 4=; X-IronPort-AV: E=Sophos;i="6.12,308,1728950400"; d="scan'208";a="262419357" Received: from iad12-co-svc-p1-lb1-vlan2.amazon.com (HELO smtpout.prod.us-west-2.prod.farcaster.email.amazon.dev) ([10.43.8.2]) by smtp-border-fw-52004.iad7.amazon.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 12 Jan 2025 04:12:26 +0000 Received: from EX19MTAUWC001.ant.amazon.com [10.0.7.35:59593] by smtpin.naws.us-west-2.prod.farcaster.email.amazon.dev [10.0.42.134:2525] with esmtp (Farcaster) id 033d6d6d-ca15-4baf-9af6-78e428b4b728; Sun, 12 Jan 2025 04:12:24 +0000 (UTC) X-Farcaster-Flow-ID: 033d6d6d-ca15-4baf-9af6-78e428b4b728 Received: from EX19D004ANA001.ant.amazon.com (10.37.240.138) by EX19MTAUWC001.ant.amazon.com (10.250.64.174) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA) id 15.2.1258.39; Sun, 12 Jan 2025 04:12:24 +0000 Received: from 6c7e67c6786f.amazon.com (10.119.14.156) by EX19D004ANA001.ant.amazon.com (10.37.240.138) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA) id 15.2.1258.39; Sun, 12 Jan 2025 04:12:20 +0000 From: Kuniyuki Iwashima To: "David S. Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni , Simon Horman CC: Donald Hunter , Kuniyuki Iwashima , Kuniyuki Iwashima , Subject: [PATCH v2 net-next 09/11] af_unix: Set drop reason in unix_stream_read_skb(). Date: Sun, 12 Jan 2025 13:08:08 +0900 Message-ID: <20250112040810.14145-10-kuniyu@amazon.com> X-Mailer: git-send-email 2.39.5 (Apple Git-154) In-Reply-To: <20250112040810.14145-1-kuniyu@amazon.com> References: <20250112040810.14145-1-kuniyu@amazon.com> Precedence: bulk X-Mailing-List: netdev@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-ClientProxiedBy: EX19D032UWA002.ant.amazon.com (10.13.139.81) To EX19D004ANA001.ant.amazon.com (10.37.240.138) X-Patchwork-Delegate: kuba@kernel.org unix_stream_read_skb() is called when BPF SOCKMAP reads some data from a socket in the map. SOCKMAP does not support MSG_OOB, and reading OOB results in a drop. Let's set drop reasons respectively. * SOCKET_CLOSE : the socket in SOCKMAP was close()d * UNIX_SKIP_OOB : OOB was read from the socket in SOCKMAP Signed-off-by: Kuniyuki Iwashima --- net/unix/af_unix.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/net/unix/af_unix.c b/net/unix/af_unix.c index efd1b83b152a..ed577276bd27 100644 --- a/net/unix/af_unix.c +++ b/net/unix/af_unix.c @@ -2777,7 +2777,7 @@ static int unix_stream_read_skb(struct sock *sk, skb_read_actor_t recv_actor) if (sock_flag(sk, SOCK_DEAD)) { unix_state_unlock(sk); - kfree_skb(skb); + kfree_skb_reason(skb, SKB_DROP_REASON_SOCKET_CLOSE); return -ECONNRESET; } @@ -2791,7 +2791,7 @@ static int unix_stream_read_skb(struct sock *sk, skb_read_actor_t recv_actor) unix_state_unlock(sk); if (drop) { - kfree_skb(skb); + kfree_skb_reason(skb, SKB_DROP_REASON_UNIX_SKIP_OOB); return -EAGAIN; } } From patchwork Sun Jan 12 04:08:09 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kuniyuki Iwashima X-Patchwork-Id: 13936215 X-Patchwork-Delegate: kuba@kernel.org Received: from smtp-fw-9106.amazon.com (smtp-fw-9106.amazon.com [207.171.188.206]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 55A6F14293 for ; Sun, 12 Jan 2025 04:12:52 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=207.171.188.206 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736655173; cv=none; b=rvIFy7Vl+be7iVqfDFSmI+NaAS4+7YOdew4EkLT+GjXa3Ex0jkWmXgQNCIFXAmF9/59wKK5kEibRXJhH1Nwo/MLMEIJ6a9rv3uHpaOvyicXT0GDaoH+uR/4D5wXLj4PanhoXB1Uup8wSZKZsS0RzTCE7Y+fkygkQN4l4+9r6TKM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736655173; c=relaxed/simple; bh=3+nONSUDtdlcgTiTcAxyYMV7OfuT36ipXqK9s2f8yyU=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=upN1/kQ6axqT/7vsjz9yXEqIoTCo2V2ZHbqIpeJ7Ulf/mFhCzE8whwTVoWej3ihTPDaFkrUGvvcBEqFy8Z1Hw9iJogz665803CGuJ8lSCt9mTEJF261WngwYuzG5q6KORjjCP0GVIsXMJf9QIBia4qRTVomw4IDkfJQxdPOnVqc= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=amazon.com; spf=pass smtp.mailfrom=amazon.co.jp; dkim=pass (1024-bit key) header.d=amazon.com header.i=@amazon.com header.b=Y2xeBxta; arc=none smtp.client-ip=207.171.188.206 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=amazon.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=amazon.co.jp Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=amazon.com header.i=@amazon.com header.b="Y2xeBxta" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amazon.com; i=@amazon.com; q=dns/txt; s=amazon201209; t=1736655173; x=1768191173; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=axHSZOVSl2hIHS46F2W8wsOJ3Qr55ftuoMvh4OhTkt0=; b=Y2xeBxtaIB9kyJ0km1FlSPb71ercdP/WbrDwUVImpNhmCE7p5QeUjXH7 a2DG0HOSQ/pqnbDrM6P035Q+FUrYDpwbmiJU5+B+HOUpbJacMrLUfAnHJ 1YI023IRGKsuucYfZerbWau3/ZeN9ZFbxKP4m7BHwDt5U1LLuhxuwnRJa o=; X-IronPort-AV: E=Sophos;i="6.12,308,1728950400"; d="scan'208";a="790432545" Received: from pdx4-co-svc-p1-lb2-vlan2.amazon.com (HELO smtpout.prod.us-west-2.prod.farcaster.email.amazon.dev) ([10.25.36.210]) by smtp-border-fw-9106.sea19.amazon.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 12 Jan 2025 04:12:52 +0000 Received: from EX19MTAUWB002.ant.amazon.com [10.0.38.20:5111] by smtpin.naws.us-west-2.prod.farcaster.email.amazon.dev [10.0.58.22:2525] with esmtp (Farcaster) id 2f8adcb5-1c7a-4af9-ba08-c3b7603f0a48; Sun, 12 Jan 2025 04:12:51 +0000 (UTC) X-Farcaster-Flow-ID: 2f8adcb5-1c7a-4af9-ba08-c3b7603f0a48 Received: from EX19D004ANA001.ant.amazon.com (10.37.240.138) by EX19MTAUWB002.ant.amazon.com (10.250.64.231) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA) id 15.2.1258.39; Sun, 12 Jan 2025 04:12:51 +0000 Received: from 6c7e67c6786f.amazon.com (10.119.14.156) by EX19D004ANA001.ant.amazon.com (10.37.240.138) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA) id 15.2.1258.39; Sun, 12 Jan 2025 04:12:47 +0000 From: Kuniyuki Iwashima To: "David S. Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni , Simon Horman CC: Donald Hunter , Kuniyuki Iwashima , Kuniyuki Iwashima , Subject: [PATCH v2 net-next 10/11] af_unix: Set drop reason in unix_dgram_disconnected(). Date: Sun, 12 Jan 2025 13:08:09 +0900 Message-ID: <20250112040810.14145-11-kuniyu@amazon.com> X-Mailer: git-send-email 2.39.5 (Apple Git-154) In-Reply-To: <20250112040810.14145-1-kuniyu@amazon.com> References: <20250112040810.14145-1-kuniyu@amazon.com> Precedence: bulk X-Mailing-List: netdev@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-ClientProxiedBy: EX19D031UWA003.ant.amazon.com (10.13.139.47) To EX19D004ANA001.ant.amazon.com (10.37.240.138) X-Patchwork-Delegate: kuba@kernel.org unix_dgram_disconnected() is called from two places: 1. when a connect()ed socket dis-connect()s or re-connect()s to another socket 2. when sendmsg() fails because the peer socket that the client has connect()ed to has been close()d Then, the client's recv queue is purged to remove all messages from the old peer socket. Let's define a new drop reason for that case. # echo 1 > /sys/kernel/tracing/events/skb/kfree_skb/enable # python3 >>> from socket import * >>> >>> # s1 has a message from s2 >>> s1, s2 = socketpair(AF_UNIX, SOCK_DGRAM) >>> s2.send(b'hello world') >>> >>> # re-connect() drops the message from s2 >>> s3 = socket(AF_UNIX, SOCK_DGRAM) >>> s3.bind('') >>> s1.connect(s3.getsockname()) # cat /sys/kernel/tracing/trace_pipe python3-250 ... kfree_skb: ... location=skb_queue_purge_reason+0xdc/0x110 reason: UNIX_DISCONNECT Signed-off-by: Kuniyuki Iwashima --- include/net/dropreason-core.h | 7 +++++++ net/unix/af_unix.c | 4 +++- 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/include/net/dropreason-core.h b/include/net/dropreason-core.h index 43e20acaa98a..580be34ffa4f 100644 --- a/include/net/dropreason-core.h +++ b/include/net/dropreason-core.h @@ -11,6 +11,7 @@ FN(SOCKET_INVALID_STATE) \ FN(SOCKET_RCVBUFF) \ FN(SOCKET_RCV_SHUTDOWN) \ + FN(UNIX_DISCONNECT) \ FN(UNIX_INFLIGHT_FD_LIMIT) \ FN(UNIX_SKIP_OOB) \ FN(PKT_TOO_SMALL) \ @@ -152,6 +153,12 @@ enum skb_drop_reason { SKB_DROP_REASON_SOCKET_RCVBUFF, /** @SKB_DROP_REASON_SOCKET_RCV_SHUTDOWN: socket is shutdown(SHUT_RD) */ SKB_DROP_REASON_SOCKET_RCV_SHUTDOWN, + /** + * @SKB_DROP_REASON_UNIX_DISCONNECT: recv queue is purged when SOCK_DGRAM + * or SOCK_SEQPACKET socket re-connect()s to another socket or notices + * during send() that the peer has been close()d. + */ + SKB_DROP_REASON_UNIX_DISCONNECT, /** * @SKB_DROP_REASON_UNIX_INFLIGHT_FD_LIMIT: too many file descriptors * are passed via SCM_RIGHTS but not yet received, reaching RLIMIT_NOFILE. diff --git a/net/unix/af_unix.c b/net/unix/af_unix.c index ed577276bd27..1c374bac7b48 100644 --- a/net/unix/af_unix.c +++ b/net/unix/af_unix.c @@ -622,7 +622,9 @@ static void unix_write_space(struct sock *sk) static void unix_dgram_disconnected(struct sock *sk, struct sock *other) { if (!skb_queue_empty(&sk->sk_receive_queue)) { - skb_queue_purge(&sk->sk_receive_queue); + skb_queue_purge_reason(&sk->sk_receive_queue, + SKB_DROP_REASON_UNIX_DISCONNECT); + wake_up_interruptible_all(&unix_sk(sk)->peer_wait); /* If one link of bidirectional dgram pipe is disconnected, From patchwork Sun Jan 12 04:08:10 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kuniyuki Iwashima X-Patchwork-Id: 13936216 X-Patchwork-Delegate: kuba@kernel.org Received: from smtp-fw-6001.amazon.com (smtp-fw-6001.amazon.com [52.95.48.154]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 4F06B6FB0 for ; Sun, 12 Jan 2025 04:13:20 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=52.95.48.154 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736655203; cv=none; b=UkqaOBa2Lul8kOgA2mBTqAIyAigtREa3jy6suAHOuuWIzzoUHrs64eTrhlEk241gGl39PcNVHxPQt3Qb5How2HBdoLUGbZoRlPsamxVqggt5DsvwNdcGZK9itJdwzJM6cyEfM/ogA41Yvr0ClfAdqEK4HoQha2Kdgz69K0XoovE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736655203; c=relaxed/simple; bh=pXedfodtMQ5b86MDOA4msamRKRy/hZk0Z4/d/b+cSqA=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=TjxKmSFyfNgqeMF/2AG887kS/ww0l+oLLPayvwHRRK8AYP8aJ2v6gYNCSwQT0bVUoeSmxkX4/00Sp/HnzFtu1pt0iLBeRdpGVqD2xnXLj2Pgrau2b0yKr9J8afiB+J9Fgdn3lk3AxIw1FxZuVOFKUhIrWP75Sqyng9xwCdonDWk= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=amazon.com; spf=pass smtp.mailfrom=amazon.co.jp; dkim=pass (1024-bit key) header.d=amazon.com header.i=@amazon.com header.b=f1zVjC4a; arc=none smtp.client-ip=52.95.48.154 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=amazon.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=amazon.co.jp Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=amazon.com header.i=@amazon.com header.b="f1zVjC4a" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amazon.com; i=@amazon.com; q=dns/txt; s=amazon201209; t=1736655201; x=1768191201; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=zd2JHetZDnRohUwDqFlCoTQyO/QYvecNJVFL9toFOwg=; b=f1zVjC4aopIflrXl3q2eft0yPn+xpqUHmjVDDAZJtWUeD653gHZuI5+T pE6b4AGzRgRl2NfayF2uPHwh8F/2T6LAl3vLDku5MKCzNrBW75ovvjcXT 4acN/I/F+WSvfPRhRnsPeVBQ7BSM+up3aKJj11VYcKsUdD5G+FpL+Trqq s=; X-IronPort-AV: E=Sophos;i="6.12,308,1728950400"; d="scan'208";a="453550818" Received: from iad12-co-svc-p1-lb1-vlan2.amazon.com (HELO smtpout.prod.us-west-2.prod.farcaster.email.amazon.dev) ([10.43.8.2]) by smtp-border-fw-6001.iad6.amazon.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 12 Jan 2025 04:13:19 +0000 Received: from EX19MTAUWB001.ant.amazon.com [10.0.7.35:25929] by smtpin.naws.us-west-2.prod.farcaster.email.amazon.dev [10.0.13.17:2525] with esmtp (Farcaster) id bb0e8e45-ebf2-4627-9e15-997b83139498; Sun, 12 Jan 2025 04:13:18 +0000 (UTC) X-Farcaster-Flow-ID: bb0e8e45-ebf2-4627-9e15-997b83139498 Received: from EX19D004ANA001.ant.amazon.com (10.37.240.138) by EX19MTAUWB001.ant.amazon.com (10.250.64.248) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA) id 15.2.1258.39; Sun, 12 Jan 2025 04:13:17 +0000 Received: from 6c7e67c6786f.amazon.com (10.119.14.156) by EX19D004ANA001.ant.amazon.com (10.37.240.138) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA) id 15.2.1258.39; Sun, 12 Jan 2025 04:13:14 +0000 From: Kuniyuki Iwashima To: "David S. Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni , Simon Horman CC: Donald Hunter , Kuniyuki Iwashima , Kuniyuki Iwashima , Subject: [PATCH v2 net-next 11/11] af_unix: Set drop reason in unix_dgram_sendmsg(). Date: Sun, 12 Jan 2025 13:08:10 +0900 Message-ID: <20250112040810.14145-12-kuniyu@amazon.com> X-Mailer: git-send-email 2.39.5 (Apple Git-154) In-Reply-To: <20250112040810.14145-1-kuniyu@amazon.com> References: <20250112040810.14145-1-kuniyu@amazon.com> Precedence: bulk X-Mailing-List: netdev@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-ClientProxiedBy: EX19D038UWC002.ant.amazon.com (10.13.139.238) To EX19D004ANA001.ant.amazon.com (10.37.240.138) X-Patchwork-Delegate: kuba@kernel.org sendmsg() to a SOCK_DGRAM / SOCK_SEQPACKET socket could fail for various reasons. Let's set drop reasons respectively. * SOCKET_FILTER : BPF filter dropped the skb * UNIX_NO_PERM : the peer connect()ed to another socket To reproduce UNIX_NO_PERM: # echo 1 > /sys/kernel/tracing/events/skb/kfree_skb/enable # python3 >>> from socket import * >>> s1, s2 = socketpair(AF_UNIX, SOCK_DGRAM) >>> s2.bind('') >>> s3 = socket(AF_UNIX, SOCK_DGRAM) >>> s3.sendto(b'hello world', s2.getsockname()) Traceback (most recent call last): File "", line 1, in PermissionError: [Errno 1] Operation not permitted # cat /sys/kernel/tracing/trace_pipe ... python3-196 ... kfree_skb: ... location=unix_dgram_sendmsg+0x3d1/0x970 reason: UNIX_NO_PERM Signed-off-by: Kuniyuki Iwashima --- include/net/dropreason-core.h | 6 ++++++ net/unix/af_unix.c | 28 +++++++++++++++++++++++----- 2 files changed, 29 insertions(+), 5 deletions(-) diff --git a/include/net/dropreason-core.h b/include/net/dropreason-core.h index 580be34ffa4f..a8db05defa5a 100644 --- a/include/net/dropreason-core.h +++ b/include/net/dropreason-core.h @@ -13,6 +13,7 @@ FN(SOCKET_RCV_SHUTDOWN) \ FN(UNIX_DISCONNECT) \ FN(UNIX_INFLIGHT_FD_LIMIT) \ + FN(UNIX_NO_PERM) \ FN(UNIX_SKIP_OOB) \ FN(PKT_TOO_SMALL) \ FN(TCP_CSUM) \ @@ -164,6 +165,11 @@ enum skb_drop_reason { * are passed via SCM_RIGHTS but not yet received, reaching RLIMIT_NOFILE. */ SKB_DROP_REASON_UNIX_INFLIGHT_FD_LIMIT, + /** + * @SKB_DROP_REASON_UNIX_NO_PERM: the peer socket is already connect()ed + * to another SOCK_DGRAM/SOCK_SEQPACKET socket. + */ + SKB_DROP_REASON_UNIX_NO_PERM, /** * @SKB_DROP_REASON_UNIX_SKIP_OOB: Out-Of-Band data is skipped by * recv() without MSG_OOB so dropped. diff --git a/net/unix/af_unix.c b/net/unix/af_unix.c index 1c374bac7b48..c34a0cd396c2 100644 --- a/net/unix/af_unix.c +++ b/net/unix/af_unix.c @@ -1991,6 +1991,7 @@ static int unix_dgram_sendmsg(struct socket *sock, struct msghdr *msg, { struct sock *sk = sock->sk, *other = NULL; struct unix_sock *u = unix_sk(sk); + enum skb_drop_reason reason; struct scm_cookie scm; struct sk_buff *skb; int data_len = 0; @@ -2051,15 +2052,19 @@ static int unix_dgram_sendmsg(struct socket *sock, struct msghdr *msg, goto out; err = unix_scm_to_skb(&scm, skb, true); - if (err < 0) + if (err < 0) { + reason = unix_scm_err_to_reason(err); goto out_free; + } skb_put(skb, len - data_len); skb->data_len = data_len; skb->len = len; err = skb_copy_datagram_from_iter(skb, 0, &msg->msg_iter, len); - if (err) + if (err) { + reason = SKB_DROP_REASON_SKB_UCOPY_FAULT; goto out_free; + } timeo = sock_sndtimeo(sk, msg->msg_flags & MSG_DONTWAIT); @@ -2069,12 +2074,14 @@ static int unix_dgram_sendmsg(struct socket *sock, struct msghdr *msg, msg->msg_namelen, sk->sk_type); if (IS_ERR(other)) { err = PTR_ERR(other); + reason = SKB_DROP_REASON_NO_SOCKET; goto out_free; } } else { other = unix_peer_get(sk); if (!other) { err = -ENOTCONN; + reason = SKB_DROP_REASON_NO_SOCKET; goto out_free; } } @@ -2082,6 +2089,7 @@ static int unix_dgram_sendmsg(struct socket *sock, struct msghdr *msg, if (sk_filter(other, skb) < 0) { /* Toss the packet but do not return any error to the sender */ err = len; + reason = SKB_DROP_REASON_SOCKET_FILTER; goto out_sock_put; } @@ -2092,6 +2100,7 @@ static int unix_dgram_sendmsg(struct socket *sock, struct msghdr *msg, if (!unix_may_send(sk, other)) { err = -EPERM; + reason = SKB_DROP_REASON_UNIX_NO_PERM; goto out_unlock; } @@ -2106,6 +2115,7 @@ static int unix_dgram_sendmsg(struct socket *sock, struct msghdr *msg, * unlike SOCK_DGRAM wants. */ err = -EPIPE; + reason = SKB_DROP_REASON_SOCKET_CLOSE; goto out_sock_put; } @@ -2122,6 +2132,7 @@ static int unix_dgram_sendmsg(struct socket *sock, struct msghdr *msg, unix_dgram_disconnected(sk, other); sock_put(other); err = -ECONNREFUSED; + reason = SKB_DROP_REASON_SOCKET_CLOSE; goto out_sock_put; } @@ -2129,6 +2140,7 @@ static int unix_dgram_sendmsg(struct socket *sock, struct msghdr *msg, if (!msg->msg_namelen) { err = -ECONNRESET; + reason = SKB_DROP_REASON_SOCKET_CLOSE; goto out_sock_put; } @@ -2137,13 +2149,16 @@ static int unix_dgram_sendmsg(struct socket *sock, struct msghdr *msg, if (other->sk_shutdown & RCV_SHUTDOWN) { err = -EPIPE; + reason = SKB_DROP_REASON_SOCKET_RCV_SHUTDOWN; goto out_unlock; } if (sk->sk_type != SOCK_SEQPACKET) { err = security_unix_may_send(sk->sk_socket, other->sk_socket); - if (err) + if (err) { + reason = SKB_DROP_REASON_SECURITY_HOOK; goto out_unlock; + } } /* other == sk && unix_peer(other) != sk if @@ -2157,8 +2172,10 @@ static int unix_dgram_sendmsg(struct socket *sock, struct msghdr *msg, timeo = unix_wait_for_peer(other, timeo); err = sock_intr_errno(timeo); - if (signal_pending(current)) + if (signal_pending(current)) { + reason = SKB_DROP_REASON_SOCKET_RCVBUFF; goto out_sock_put; + } goto restart; } @@ -2171,6 +2188,7 @@ static int unix_dgram_sendmsg(struct socket *sock, struct msghdr *msg, if (unix_peer(sk) != other || unix_dgram_peer_wake_me(sk, other)) { err = -EAGAIN; + reason = SKB_DROP_REASON_SOCKET_RCVBUFF; sk_locked = 1; goto out_unlock; } @@ -2202,7 +2220,7 @@ static int unix_dgram_sendmsg(struct socket *sock, struct msghdr *msg, out_sock_put: sock_put(other); out_free: - kfree_skb(skb); + kfree_skb_reason(skb, reason); out: scm_destroy(&scm); return err;