From patchwork Fri Jan 10 09:26:30 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kuniyuki Iwashima X-Patchwork-Id: 13934080 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 528DC209F27 for ; Fri, 10 Jan 2025 09:27:25 +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=1736501247; cv=none; b=DqDIeYNXET72hD/EIibaSlP+jSe3Hx7hgq4N5NvVZdeUAWxPovfOUL/zcfiId6TA/pfcG3FrQ2npIep4jfrUgYDQM4/wVs1eWM64RJVSuMBecTEpk7t74g6ixNa3DubUMmYDH11iZgtiUPqoRYqHvmchuYODSu16GoVdju8HK7k= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736501247; c=relaxed/simple; bh=4zxWxSdxq5LjIMM1PjmSzSdckbS+zyZXXBZJjamGcY0=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=JLYCwpzDuVgNbqbhhAMFwQ4tFM4JjnBmL/nK/A9TJ7ewewTlvwMx5OENFsqmZR+vDuIpvjRN4Zyq/n/h0unfdWvky3jVXP1nzFcecjoXMvhZFgPezYEkOnyW4Y9aa2ShiYs7hoANMvr4CtKlF+/Fw5v1nPI8+dhsuQmNV6tfBwg= 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=epu6HwkU; 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="epu6HwkU" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amazon.com; i=@amazon.com; q=dns/txt; s=amazon201209; t=1736501246; x=1768037246; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=ZWFaiuQZMrgEqcBsf6VsdoSQyFOIKYV9KciZxBb54VA=; b=epu6HwkUPolo3Ze71lhgL98ZtCqzfbikaMAfHoxzhVDzQqmioDgtH8sv Gwz3+0CKF451R8B4cBmhaA7x8WSUYqoFDdFW0LUNcUXa2DFRXY3E/iBlI yflFQoYWhA041V2vEXTEG60ANJDtdLwHDp83UDoqE1S6xUDvRp4tMt/Al M=; X-IronPort-AV: E=Sophos;i="6.12,303,1728950400"; d="scan'208";a="453215272" 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; 10 Jan 2025 09:27:24 +0000 Received: from EX19MTAUWA001.ant.amazon.com [10.0.38.20:33497] by smtpin.naws.us-west-2.prod.farcaster.email.amazon.dev [10.0.16.12:2525] with esmtp (Farcaster) id a5410bb8-b721-49a4-8de6-abe57e0e4a50; Fri, 10 Jan 2025 09:27:23 +0000 (UTC) X-Farcaster-Flow-ID: a5410bb8-b721-49a4-8de6-abe57e0e4a50 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; Fri, 10 Jan 2025 09:27:22 +0000 Received: from 6c7e67c6786f.amazon.com (10.118.252.101) 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; Fri, 10 Jan 2025 09:27:18 +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 v1 net-next 01/12] net: dropreason: Gather SOCKET_ drop reasons. Date: Fri, 10 Jan 2025 18:26:30 +0900 Message-ID: <20250110092641.85905-2-kuniyu@amazon.com> X-Mailer: git-send-email 2.39.5 (Apple Git-154) In-Reply-To: <20250110092641.85905-1-kuniyu@amazon.com> References: <20250110092641.85905-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 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 Fri Jan 10 09:26:31 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kuniyuki Iwashima X-Patchwork-Id: 13934081 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 567872054E2 for ; Fri, 10 Jan 2025 09:27:54 +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=1736501276; cv=none; b=XOiR0vfpKDmTdREh5oG8aPAvsjJjqh5kgatSAPo42vRNX0N0dkBDJDhlhvU9rqgwo8T4l6Js51zy5WW+roB9wgEu1EP5mNKgEDwXmyaek/ja1AEHjbbAA7dx5/TW7jCPXFVBYLJf4W2g84q/ooeSuye83/jiPDXOoCfOXrWK34s= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736501276; c=relaxed/simple; bh=5mUSIqvZKCzqOqlLhc4HTPnpUs3UM6+Ws+S5vzsOlCA=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=f8t56LDY3ETX6ymUM0LKFDcRmYAEMhS7tBYtSOroMJRfjxY/Dh5Rux7olDYWMt2Ow482KKVHlfrsKSlmfoifgnyGGHSqPKRC4qkJshDW7Qji7r0XlrlYkj/n4c8rYHUZnFu1mWXYlB/m44xo/W+2Jakxr3BiblhvLIFb3hQzhmc= 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=hPvBP0U1; 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="hPvBP0U1" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amazon.com; i=@amazon.com; q=dns/txt; s=amazon201209; t=1736501273; x=1768037273; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=KSH9q4QLzGrgvHxoytg+FRGRTm++eL6aTnjpqTyExSU=; b=hPvBP0U1Nwwnk2QJDcAcSi+e7dloHGfkSbrEPl3I6jHaod3F4sLhfLDx xvvxoK1UDuJV6EfSOgaNiKhUgEB+XtzmF7quNYwvyypgDrWVGnBkAsS5X FJV+WI/RuXudND/SF/6lPzuEHPz9fYUPFKiny30PI/hcHjckZ5tyqniUk 8=; X-IronPort-AV: E=Sophos;i="6.12,303,1728950400"; d="scan'208";a="56711877" 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; 10 Jan 2025 09:27:50 +0000 Received: from EX19MTAUWC002.ant.amazon.com [10.0.7.35:42191] by smtpin.naws.us-west-2.prod.farcaster.email.amazon.dev [10.0.63.9:2525] with esmtp (Farcaster) id 2c778f8a-83fc-4af5-a064-c9a5fb3bad03; Fri, 10 Jan 2025 09:27:50 +0000 (UTC) X-Farcaster-Flow-ID: 2c778f8a-83fc-4af5-a064-c9a5fb3bad03 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; Fri, 10 Jan 2025 09:27:49 +0000 Received: from 6c7e67c6786f.amazon.com (10.118.252.101) 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; Fri, 10 Jan 2025 09:27:45 +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 v1 net-next 02/12] af_unix: Set drop reason in unix_release_sock(). Date: Fri, 10 Jan 2025 18:26:31 +0900 Message-ID: <20250110092641.85905-3-kuniyu@amazon.com> X-Mailer: git-send-email 2.39.5 (Apple Git-154) In-Reply-To: <20250110092641.85905-1-kuniyu@amazon.com> References: <20250110092641.85905-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: EX19D031UWA004.ant.amazon.com (10.13.139.19) 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 Fri Jan 10 09:26:32 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kuniyuki Iwashima X-Patchwork-Id: 13934083 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 ED2F320ADE3 for ; Fri, 10 Jan 2025 09:28:24 +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=1736501306; cv=none; b=cENPyfkuVP1nynN/2czDdWWmCIJd4+F8TtGtCIHOMSqstOCB6sqYliHzWIal1PE4wT6MY0JbeO5MEPlUA1XXcdrG/xf8NZhvnXNFdXXXD82SxUJ8PjP9dCi7THzMmTfFgALlRX8BnBvQCz3xNYaMOnh7t+G+jF2YDNA9VdaJ8W8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736501306; c=relaxed/simple; bh=YBFNC/z40WX/1GOmQ+b5h9+ml+NEtHp1K3eBSwvEhUM=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=F26Q6JzsDQNPiChU0X7Cs5beUcuP6jWof5Xv249tLMSSZL4fGwxrILRGBJaVVdUOrX2ki964QTg9XeQUYT5BEf2ntvVjOlX6v68z4kukCMNXNfvIyhvM5IN+nm9WXsSIrsnm2o4kxTybdOMhB3Yjt49wf9ZKsiGn/ZKqFXfmWI0= 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=jNgqdVhi; 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="jNgqdVhi" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amazon.com; i=@amazon.com; q=dns/txt; s=amazon201209; t=1736501305; x=1768037305; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=EEYrvzro4pXAy2ePhNFvASRwMz4gzEE3m8pZiNb4R4A=; b=jNgqdVhiD3wXVj28dCKjyAySyxooX+2QEN/oeRKdRTeOyYulHkl3pW3b tMLSsGY8HJDrp6ltFlZOh6hUf9369SyESvaTcWC+09bUACIRqsEZL6UXT n2axnqjvXs3U4jjXIkDeAOhl9kBR1I5/ZavikM3c8rbY/ODzsA4xMra2v s=; X-IronPort-AV: E=Sophos;i="6.12,303,1728950400"; d="scan'208";a="262089466" 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; 10 Jan 2025 09:28:17 +0000 Received: from EX19MTAUWC002.ant.amazon.com [10.0.7.35:46917] by smtpin.naws.us-west-2.prod.farcaster.email.amazon.dev [10.0.29.114:2525] with esmtp (Farcaster) id d820f865-e127-42f2-8c40-b572352c0d3d; Fri, 10 Jan 2025 09:28:17 +0000 (UTC) X-Farcaster-Flow-ID: d820f865-e127-42f2-8c40-b572352c0d3d 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; Fri, 10 Jan 2025 09:28:16 +0000 Received: from 6c7e67c6786f.amazon.com (10.118.252.101) 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; Fri, 10 Jan 2025 09:28:12 +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 v1 net-next 03/12] af_unix: Set drop reason in unix_sock_destructor(). Date: Fri, 10 Jan 2025 18:26:32 +0900 Message-ID: <20250110092641.85905-4-kuniyu@amazon.com> X-Mailer: git-send-email 2.39.5 (Apple Git-154) In-Reply-To: <20250110092641.85905-1-kuniyu@amazon.com> References: <20250110092641.85905-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: EX19D038UWB004.ant.amazon.com (10.13.139.177) 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 Fri Jan 10 09:26:33 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kuniyuki Iwashima X-Patchwork-Id: 13934084 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 CEF2620A5F5 for ; Fri, 10 Jan 2025 09:28:49 +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=1736501331; cv=none; b=DREM+Oo1/ca01eE/lF7osT0zPl3g6Czcwdgh5NIroih9pdbkJqvMi1B6Eje4LLcUF+UJUUSWQ3Di6EzRoFQOAhp/vCVTqV1B+ACUlXE2Z3h3V+mWaE4QiPr9j7HL8M+9G3ZrsivFgPybsQpw1d1CIf6jHS/o5Ns6zcBlla6M8xM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736501331; c=relaxed/simple; bh=0Ap47lpyfQBoMXsC4C5t0FTb9u7+ECgci6hK7CBTaMI=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=VC5lscJTnGjZH5ITb3QaG0+kzUbEdzgdJPcxP7nqjT9edIdL2x3Q9d0S0EVWBf/6SgHRbPlVK5f87vqNiQl6JXGQEmFbr9i2eNCNf+qixySvPbrZQWX+lTIpYnwWr8jGJAgqzsUKGYo+DIpwOzNfWVKrnX5OvtHkkHQYuD7nDAM= 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=BSzJgeyH; 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="BSzJgeyH" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amazon.com; i=@amazon.com; q=dns/txt; s=amazon201209; t=1736501329; x=1768037329; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=WRvz/jt2K+XR/8BEV3sbpF+J4ezyxNZwPZiP0vIZEyk=; b=BSzJgeyH0BfvTZWv+1DnkBobFEVm40HqD80Oj18eaGiM1kR/LjlrXNtK BG1dyvMnhVGNt90lI2NerPtaAW5iW2mHWTFXchQxaIQamOJN4hnkL/G1Z X2MNarThGCIGIUhTlbvamdzBZYJOC+JllnjweOx1UkcoNh7+QIwJQ8A9W 4=; X-IronPort-AV: E=Sophos;i="6.12,303,1728950400"; d="scan'208";a="790117096" 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; 10 Jan 2025 09:28:44 +0000 Received: from EX19MTAUWB002.ant.amazon.com [10.0.38.20:42245] by smtpin.naws.us-west-2.prod.farcaster.email.amazon.dev [10.0.63.9:2525] with esmtp (Farcaster) id 35e9a0d8-ccbd-4c07-8857-3bd9908dd81b; Fri, 10 Jan 2025 09:28:44 +0000 (UTC) X-Farcaster-Flow-ID: 35e9a0d8-ccbd-4c07-8857-3bd9908dd81b 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; Fri, 10 Jan 2025 09:28:43 +0000 Received: from 6c7e67c6786f.amazon.com (10.118.252.101) 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; Fri, 10 Jan 2025 09:28: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 v1 net-next 04/12] af_unix: Set drop reason in __unix_gc(). Date: Fri, 10 Jan 2025 18:26:33 +0900 Message-ID: <20250110092641.85905-5-kuniyu@amazon.com> X-Mailer: git-send-email 2.39.5 (Apple Git-154) In-Reply-To: <20250110092641.85905-1-kuniyu@amazon.com> References: <20250110092641.85905-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: EX19D039UWB001.ant.amazon.com (10.13.138.119) 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 Fri Jan 10 09:26:34 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kuniyuki Iwashima X-Patchwork-Id: 13934088 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 CD4A2207DFC for ; Fri, 10 Jan 2025 09:29:17 +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=1736501362; cv=none; b=qdXRlJG0R6GUQPxno0ckugqtF+y6+7P4aFG4kMUMSzQ3x+Qepex/piUDhzlhjmfw+KIGu55n7v/zdWxAc0aClKb1wVf0EM7vXLeMCoElc6WIFScLqGbgTzljylYU8YCWlYYyOnbyV19RCIS1HXNcjW3qC5PrRd/qBLo7qEP/lng= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736501362; c=relaxed/simple; bh=CIDS+Q6aNlIT1yDXhmZ3EAyn1U4O7v2HsaIp3sJV0NM=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=nm7saH0t2hmlTj51mgLAQ8rLKsxUYNlpoz8fnS+Gdhm9zpsCYsEK2qyvc78+e4tHcBHsqBZK3ffsWou0IElOKchkaLvxVILVNtn/yxn5zccTgUOvxj09D6JNZw5Pv1+EPIxFd1bymLiMxZ0Ekk754ONnuJUOqIEqAeBLULCRX14= 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=VINCRfSD; 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="VINCRfSD" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amazon.com; i=@amazon.com; q=dns/txt; s=amazon201209; t=1736501361; x=1768037361; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=mg59p3COVrzPdxR5HSibX8dKEzv7bL6fBavvhjfvC/0=; b=VINCRfSD7hbr434meOSq11JhNO66Ob59kvAM966zISVRhyj1jPtZxCGU bY2YrxReqoWDw68YnCHEUn29KecAYbpVMokbKHLhEICyPF0XtchifisK8 fV53PLrCMSQ6CLKhMagSFfGMl6UG8sA/Z2sxCuYZ9LCia7DrhxyBoe1K4 0=; X-IronPort-AV: E=Sophos;i="6.12,303,1728950400"; d="scan'208";a="457845184" 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; 10 Jan 2025 09:29:14 +0000 Received: from EX19MTAUWA001.ant.amazon.com [10.0.38.20:14868] by smtpin.naws.us-west-2.prod.farcaster.email.amazon.dev [10.0.29.65:2525] with esmtp (Farcaster) id 0323833d-46c8-4a38-97db-53311206f70a; Fri, 10 Jan 2025 09:29:12 +0000 (UTC) X-Farcaster-Flow-ID: 0323833d-46c8-4a38-97db-53311206f70a 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; Fri, 10 Jan 2025 09:29:11 +0000 Received: from 6c7e67c6786f.amazon.com (10.118.252.101) 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; Fri, 10 Jan 2025 09:29: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 v1 net-next 05/12] af_unix: Set drop reason in unix_stream_connect(). Date: Fri, 10 Jan 2025 18:26:34 +0900 Message-ID: <20250110092641.85905-6-kuniyu@amazon.com> X-Mailer: git-send-email 2.39.5 (Apple Git-154) In-Reply-To: <20250110092641.85905-1-kuniyu@amazon.com> References: <20250110092641.85905-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: EX19D046UWA003.ant.amazon.com (10.13.139.18) 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 Fri Jan 10 09:26:35 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kuniyuki Iwashima X-Patchwork-Id: 13934089 X-Patchwork-Delegate: kuba@kernel.org Received: from smtp-fw-80006.amazon.com (smtp-fw-80006.amazon.com [99.78.197.217]) (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 9C01320C46E for ; Fri, 10 Jan 2025 09:29:40 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=99.78.197.217 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736501382; cv=none; b=tDSRI2Cq3XiATDW+ZmkJTw4EzmaWdZCF2+ioGWVsKuoR8IrRrRsIamGlZy8Gf2ICEa7Q6jHTPqU/CfPwdzuqhfS46hS96Jr/ruRxVqc0XqzuZNKRTixsfVA56xjXkcUS2EJy7M3MzSVjVXiekl/BKxth5I4R091XGZc1mosWb6g= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736501382; c=relaxed/simple; bh=8h8sjxSYvc03RuT3+nX5S1XuL4N5VDBMjfLQggkcSg4=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=CZDSKNMV+QGEAiLePZwJc9KyXBdzvV0+vIIbx1dfMtPjjiw6m8TYfbfk7u5m+w68/4m944S5+e7911s+DyzBaN4FQFyS/v1VOI6h7mAnwUnq1V2KKiqh8BGSl+UGxGzV3ZNV34v8mXnGSxUDYG7o9Ma0rYj330GQKDv3Zuyfy5g= 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=gwi9DenA; arc=none smtp.client-ip=99.78.197.217 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="gwi9DenA" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amazon.com; i=@amazon.com; q=dns/txt; s=amazon201209; t=1736501381; x=1768037381; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=80uBm5XK40kyyRdwYeGa58p6tHnZvw7FqLJ+2n5VezQ=; b=gwi9DenAiOoYWVWUpgNtgDaOrRY4ioNitPGn4itPC4B1NAxFsuPPluTd JSuGFp9KfskH6Q9qz5om/9v89oaIhN1Y/C/TdEbjPLxfXKNMpkMfs780s Fx04hW9kX73x2VVml8QGPlLXMPaWwfM5wVgPC9Aunt/Zd3WzfjhfyUKzm g=; X-IronPort-AV: E=Sophos;i="6.12,303,1728950400"; d="scan'208";a="13335074" Received: from pdx4-co-svc-p1-lb2-vlan3.amazon.com (HELO smtpout.prod.us-west-2.prod.farcaster.email.amazon.dev) ([10.25.36.214]) by smtp-border-fw-80006.pdx80.corp.amazon.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 10 Jan 2025 09:29:39 +0000 Received: from EX19MTAUWB002.ant.amazon.com [10.0.38.20:51011] by smtpin.naws.us-west-2.prod.farcaster.email.amazon.dev [10.0.42.134:2525] with esmtp (Farcaster) id bf29fd59-7e2a-44cc-9f7c-b33f8b2d4a00; Fri, 10 Jan 2025 09:29:38 +0000 (UTC) X-Farcaster-Flow-ID: bf29fd59-7e2a-44cc-9f7c-b33f8b2d4a00 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; Fri, 10 Jan 2025 09:29:37 +0000 Received: from 6c7e67c6786f.amazon.com (10.118.252.101) 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; Fri, 10 Jan 2025 09:29:34 +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 v1 net-next 06/12] af_unix: Reuse out_pipe label in unix_stream_sendmsg(). Date: Fri, 10 Jan 2025 18:26:35 +0900 Message-ID: <20250110092641.85905-7-kuniyu@amazon.com> X-Mailer: git-send-email 2.39.5 (Apple Git-154) In-Reply-To: <20250110092641.85905-1-kuniyu@amazon.com> References: <20250110092641.85905-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: EX19D041UWA001.ant.amazon.com (10.13.139.124) To EX19D004ANA001.ant.amazon.com (10.37.240.138) X-Patchwork-Delegate: kuba@kernel.org This is a follow-up of commit d460b04bc452 ("af_unix: Clean up error paths in unix_stream_sendmsg()."). If we initialise skb with NULL in unix_stream_sendmsg(), we can reuse the existing out_pipe label for the SEND_SHUTDOWN check. Let's rename do it and adjust the existing label as out_pipe_lock. While at it, size and data_len are moved to the while loop scope. Suggested-by: Paolo Abeni Signed-off-by: Kuniyuki Iwashima --- net/unix/af_unix.c | 23 +++++++++-------------- 1 file changed, 9 insertions(+), 14 deletions(-) diff --git a/net/unix/af_unix.c b/net/unix/af_unix.c index b190ea8b8e9d..6505eeab9957 100644 --- a/net/unix/af_unix.c +++ b/net/unix/af_unix.c @@ -2250,13 +2250,11 @@ static int unix_stream_sendmsg(struct socket *sock, struct msghdr *msg, size_t len) { struct sock *sk = sock->sk; + struct sk_buff *skb = NULL; 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; + int err, sent = 0; err = scm_send(sock, msg, &scm, false); if (err < 0) @@ -2285,16 +2283,12 @@ static int unix_stream_sendmsg(struct socket *sock, struct msghdr *msg, } } - if (READ_ONCE(sk->sk_shutdown) & SEND_SHUTDOWN) { - if (!(msg->msg_flags & MSG_NOSIGNAL)) - send_sig(SIGPIPE, current, 0); - - err = -EPIPE; - goto out_err; - } + if (READ_ONCE(sk->sk_shutdown) & SEND_SHUTDOWN) + goto out_pipe; 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, @@ -2347,7 +2341,7 @@ static int unix_stream_sendmsg(struct socket *sock, struct msghdr *msg, if (sock_flag(other, SOCK_DEAD) || (other->sk_shutdown & RCV_SHUTDOWN)) - goto out_pipe; + goto out_pipe_unlock; maybe_add_creds(skb, sock, other); scm_stat_add(other, skb); @@ -2370,8 +2364,9 @@ static int unix_stream_sendmsg(struct socket *sock, struct msghdr *msg, return sent; -out_pipe: +out_pipe_unlock: unix_state_unlock(other); +out_pipe: if (!sent && !(msg->msg_flags & MSG_NOSIGNAL)) send_sig(SIGPIPE, current, 0); err = -EPIPE; From patchwork Fri Jan 10 09:26:36 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kuniyuki Iwashima X-Patchwork-Id: 13934090 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 D8485209F38 for ; Fri, 10 Jan 2025 09:30:12 +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=1736501414; cv=none; b=gBPLSyllbxh4Ompt8wFUWcV6Wv6QPzTowD4Q2zVCa0i88jsaZWVoyyL4AU5QASAv7xME5J4Vh4A23ujevwuSvj6wXs2cF2C6w+Qr9ZceIFQw6GYl16ZvX9uDrRIgN3oqEhcQq20Nko/GmaJ/GFrll9GdyOLqKw/Znf686rJweeY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736501414; c=relaxed/simple; bh=xT4XRrxCb/VGArLZh51hm11TmE5zQ6NNQWzATlNQdwo=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=nnBv87H2tSYIyB2CiSc90iohjzuoZlJrG4YRmUMWP9Rlc8jA+ldl6PONO/MUkuaf23hLQzWWUF8LYDYnVu3DY3+UpWvtGYMVavZkHCnJPqmV+dLmNyTW35r+JCy6J0h0UPqUCpao+JG3ri1ZspHJ31F8lwampGEuMQOIc+IYzjQ= 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=ex0dNZcl; 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="ex0dNZcl" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amazon.com; i=@amazon.com; q=dns/txt; s=amazon201209; t=1736501413; x=1768037413; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=/PxlOJHunfq5Qyeg2TKAnZcJ1aS74/HChKuENexILO8=; b=ex0dNZcl/t6oZZ8oBzA69XZPnL2BtZCLunz2BarQ8R0i6YmByLbEnLo0 CL8z/baCii2kCEaymzXnZ9/XHruaEsTKQIVJOOaoYkmyVjlBfjI3FHav8 BQZttFHAAdIPfxb7G9GKiMTyVu8zU5AOyhAXhK6c0iL1YS2tm+Fqr9RVR Y=; X-IronPort-AV: E=Sophos;i="6.12,303,1728950400"; d="scan'208";a="163180613" 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; 10 Jan 2025 09:30:11 +0000 Received: from EX19MTAUWA002.ant.amazon.com [10.0.21.151:12565] by smtpin.naws.us-west-2.prod.farcaster.email.amazon.dev [10.0.0.73:2525] with esmtp (Farcaster) id dfa61a1e-d74a-4538-94b1-cd68d97b1151; Fri, 10 Jan 2025 09:30:10 +0000 (UTC) X-Farcaster-Flow-ID: dfa61a1e-d74a-4538-94b1-cd68d97b1151 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; Fri, 10 Jan 2025 09:30:04 +0000 Received: from 6c7e67c6786f.amazon.com (10.118.252.101) 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; Fri, 10 Jan 2025 09:30: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 v1 net-next 07/12] af_unix: Set drop reason in unix_stream_sendmsg(). Date: Fri, 10 Jan 2025 18:26:36 +0900 Message-ID: <20250110092641.85905-8-kuniyu@amazon.com> X-Mailer: git-send-email 2.39.5 (Apple Git-154) In-Reply-To: <20250110092641.85905-1-kuniyu@amazon.com> References: <20250110092641.85905-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: EX19D035UWA001.ant.amazon.com (10.13.139.101) 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(). Signed-off-by: Kuniyuki Iwashima --- include/net/dropreason-core.h | 6 +++++ net/unix/af_unix.c | 41 ++++++++++++++++++++++++++++++----- 2 files changed, 41 insertions(+), 6 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 6505eeab9957..17b9e9bce055 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,6 +2265,7 @@ 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 sk_buff *skb = NULL; struct sock *other = NULL; @@ -2314,8 +2331,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; @@ -2323,8 +2342,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); @@ -2333,15 +2354,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_unlock; + } + + if (other->sk_shutdown & RCV_SHUTDOWN) { + reason = SKB_DROP_REASON_SOCKET_RCV_SHUTDOWN; + goto out_pipe_unlock; + } maybe_add_creds(skb, sock, other); scm_stat_add(other, skb); @@ -2371,7 +2400,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 Fri Jan 10 09:26:37 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kuniyuki Iwashima X-Patchwork-Id: 13934091 X-Patchwork-Delegate: kuba@kernel.org Received: from smtp-fw-9102.amazon.com (smtp-fw-9102.amazon.com [207.171.184.29]) (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 B137E20A5FD for ; Fri, 10 Jan 2025 09:30:37 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=207.171.184.29 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736501439; cv=none; b=kMVBmq2FFveCFeE968c4RnoPqOlKcW98AS2RLxKa957UfXFDOVTzTq6VlBZCBf29HjXxSuxwM90bgVQpFUr34MGKmT6LD4OS1GGZ3i47LFGo380zWHvv1mz27j1Prt/gPC/XK84ZxRU71TVf11ZYrOn1i2jFVRxpLsLSNFoOong= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736501439; c=relaxed/simple; bh=VDmivZ+wboA3DQxaMNav2Fu92+VG28LycmwI/CMxZjc=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=qDafxEnw5ltNaDktC5pcJxybGV3/bM6h1Gv6MeVr3nHfvsjrujjZiIG0oWWiROnYOEFLq67xNmJ7S/nG36qFP/u+XONUqy+NeV1dAybmk7uRy4aRBUrrkyz6JrCrezrtrihD4ojI8pM3whvynXlMa2BdR2oxO6hmu4F8h8RUfwk= 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=pQ0VOrFi; arc=none smtp.client-ip=207.171.184.29 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="pQ0VOrFi" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amazon.com; i=@amazon.com; q=dns/txt; s=amazon201209; t=1736501438; x=1768037438; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=PAtZfotiwh0ZHqfa311ORPKXAGdnsz7bqubfYRwOQNU=; b=pQ0VOrFiMTriL6vj7atdCNpKyfJuw65/VQT+lxBMNnhmpZUCcLFcDmKY NPzV0GCEhe/BaRhf3ac3R93RLMWktZyOn/Spo3NeP77To5iLG2XSh95ak Fc/JUSdW9PJBgriVJ6xHDwmK2GDrRcP4vPhYcPvsBbyNc5Lj8xoClJlvi k=; X-IronPort-AV: E=Sophos;i="6.12,303,1728950400"; d="scan'208";a="485019655" Received: from pdx4-co-svc-p1-lb2-vlan3.amazon.com (HELO smtpout.prod.us-west-2.prod.farcaster.email.amazon.dev) ([10.25.36.214]) by smtp-border-fw-9102.sea19.amazon.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 10 Jan 2025 09:30:32 +0000 Received: from EX19MTAUWB001.ant.amazon.com [10.0.38.20:47667] by smtpin.naws.us-west-2.prod.farcaster.email.amazon.dev [10.0.13.17:2525] with esmtp (Farcaster) id 57fc1d94-4316-481b-ba0c-4f9dbe4ee329; Fri, 10 Jan 2025 09:30:31 +0000 (UTC) X-Farcaster-Flow-ID: 57fc1d94-4316-481b-ba0c-4f9dbe4ee329 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; Fri, 10 Jan 2025 09:30:31 +0000 Received: from 6c7e67c6786f.amazon.com (10.118.252.101) 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; Fri, 10 Jan 2025 09:30: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 v1 net-next 08/12] af_unix: Set drop reason in queue_oob(). Date: Fri, 10 Jan 2025 18:26:37 +0900 Message-ID: <20250110092641.85905-9-kuniyu@amazon.com> X-Mailer: git-send-email 2.39.5 (Apple Git-154) In-Reply-To: <20250110092641.85905-1-kuniyu@amazon.com> References: <20250110092641.85905-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: EX19D043UWA001.ant.amazon.com (10.13.139.45) 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 17b9e9bce055..1dfe791e8991 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 Fri Jan 10 09:26:38 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kuniyuki Iwashima X-Patchwork-Id: 13934092 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 E5E40209F24 for ; Fri, 10 Jan 2025 09:31:03 +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=1736501465; cv=none; b=n+8EYaPP0F2DPxkAqr7Tz+94NJ++KmxnbydyFPttVsis9RgWj7rEN7LQIWxaSY8sSwfF5Gihi6bjJDnMPMcmlb+dsAg0f08n4pi4SbN54Ro5/rlrDXnsi2aAgEZJLmMTjJQAC5rx3Zdgl6sJPjlYZ0uV3jtNhS1ylY+mBjAXi4Q= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736501465; c=relaxed/simple; bh=+IFfkmkD30I2ERIZGAPEBmULIGk1pZOwX6IZIzOe9J4=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=UNekYlVCxf9TdlJ9vYiQOKD+BduUshthCyImr58Z7GFAkcXgjIQmrLCNOwoiRwxyvhSQ8WPwSIGCFi+R9k+vlu8L07JfXW0a9+oYu/7hjA5FMG++cVJTBiWOczp62LbJ2Y+DPjsbVqgcSplLB3Ho1gKuYFpj5gk/6X3ktii8hkQ= 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=enun1ml+; 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="enun1ml+" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amazon.com; i=@amazon.com; q=dns/txt; s=amazon201209; t=1736501464; x=1768037464; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=QmvH7MsnuBqbuAqJ9S+kZcIpMcXI/lqlIJpryvCN59o=; b=enun1ml+2Pw+nqgCyNnLET8bKcgz8LZc7qCYu/ULOy0gcoFuPnwL46tc mNk/6jlVvuDobBmAOCQHm/Tz9uSuqGn6Dxd33/MAH9fjR4ubMycSV3M0M TMlVqBc3ml1sZH8XeQ601mSOPAD2dWN7TKk/tMuiVJd4IBp8lqqCTUaJ/ k=; X-IronPort-AV: E=Sophos;i="6.12,303,1728950400"; d="scan'208";a="262090098" 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; 10 Jan 2025 09:31:02 +0000 Received: from EX19MTAUWA001.ant.amazon.com [10.0.38.20:37401] by smtpin.naws.us-west-2.prod.farcaster.email.amazon.dev [10.0.29.65:2525] with esmtp (Farcaster) id da46f367-30a7-48f1-b507-78b844182553; Fri, 10 Jan 2025 09:31:01 +0000 (UTC) X-Farcaster-Flow-ID: da46f367-30a7-48f1-b507-78b844182553 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; Fri, 10 Jan 2025 09:30:58 +0000 Received: from 6c7e67c6786f.amazon.com (10.118.252.101) 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; Fri, 10 Jan 2025 09:30: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 v1 net-next 09/12] af_unix: Set drop reason in manage_oob(). Date: Fri, 10 Jan 2025 18:26:38 +0900 Message-ID: <20250110092641.85905-10-kuniyu@amazon.com> X-Mailer: git-send-email 2.39.5 (Apple Git-154) In-Reply-To: <20250110092641.85905-1-kuniyu@amazon.com> References: <20250110092641.85905-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: EX19D041UWB003.ant.amazon.com (10.13.139.176) 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 1dfe791e8991..06d90767040e 100644 --- a/net/unix/af_unix.c +++ b/net/unix/af_unix.c @@ -2744,7 +2744,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 Fri Jan 10 09:26:39 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kuniyuki Iwashima X-Patchwork-Id: 13934093 X-Patchwork-Delegate: kuba@kernel.org Received: from smtp-fw-52002.amazon.com (smtp-fw-52002.amazon.com [52.119.213.150]) (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 F2B24209F31 for ; Fri, 10 Jan 2025 09:31:28 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=52.119.213.150 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736501490; cv=none; b=kdgz4P+5VriYPEv33vdaPvXEJGqWLelEn3/2mXU0A9/rKA969mvze9gt7Hcf4skb/la0uoUMS4NcPBToLa+1aCsfyNHVL1dB9zrLWagmHtqI4dnaF6VqR2t3qdjIGQSIl9f6gBoUaBaEHCGfWP1LyzzyXSQklyvB3j2DFUu4+sI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736501490; c=relaxed/simple; bh=Nc9wgKHYkq8sTiPEOynTWTtT7mloaNt70oydqUe0PBU=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=WhU1IiLFPxY70AeCVeh9zl0YKZlU7rK6rhrJbL24Q0olahGcmgOBZfZ60jncNK6dazQe+OYCDnSuFidev2onPLN0Ro3p6q663W9iwVuqCmlgExVmnKvH4c+LALYDLRz6n+avxcFqto3n2ugLNpsvYkCgIPCmeR/+vQkDOzCYgag= 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=KtvYipWE; arc=none smtp.client-ip=52.119.213.150 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="KtvYipWE" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amazon.com; i=@amazon.com; q=dns/txt; s=amazon201209; t=1736501489; x=1768037489; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=ir1honBL5vaRXaL2ulf1SyE6LQGBYV1ssL0hzSJK4+M=; b=KtvYipWEVyWNI2ZgDtJ8DjHCrHzS6JKLKDYKl366CEdeo/iMdhzdeD1g gBDmFG5FpxvgQHgXnoEthtZGiiRG24sUFVGEEpw6liJ1S7Dhrac3RYzRg T1wSeb2OfikUwq37kQF8aTNMfm4/KG1MRjGV403wasU4vib2R+DfUVTUt c=; X-IronPort-AV: E=Sophos;i="6.12,303,1728950400"; d="scan'208";a="688274748" 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-52002.iad7.amazon.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 10 Jan 2025 09:31:26 +0000 Received: from EX19MTAUWB002.ant.amazon.com [10.0.21.151:53212] by smtpin.naws.us-west-2.prod.farcaster.email.amazon.dev [10.0.45.174:2525] with esmtp (Farcaster) id d1a24d83-a3e2-443e-aebe-71e2b68b8113; Fri, 10 Jan 2025 09:31:25 +0000 (UTC) X-Farcaster-Flow-ID: d1a24d83-a3e2-443e-aebe-71e2b68b8113 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; Fri, 10 Jan 2025 09:31:25 +0000 Received: from 6c7e67c6786f.amazon.com (10.118.252.101) 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; Fri, 10 Jan 2025 09:31:21 +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 v1 net-next 10/12] af_unix: Set drop reason in unix_stream_read_skb(). Date: Fri, 10 Jan 2025 18:26:39 +0900 Message-ID: <20250110092641.85905-11-kuniyu@amazon.com> X-Mailer: git-send-email 2.39.5 (Apple Git-154) In-Reply-To: <20250110092641.85905-1-kuniyu@amazon.com> References: <20250110092641.85905-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: EX19D036UWC001.ant.amazon.com (10.13.139.233) 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 06d90767040e..80c979266d37 100644 --- a/net/unix/af_unix.c +++ b/net/unix/af_unix.c @@ -2773,7 +2773,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; } @@ -2787,7 +2787,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 Fri Jan 10 09:26:40 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kuniyuki Iwashima X-Patchwork-Id: 13934094 X-Patchwork-Delegate: kuba@kernel.org Received: from smtp-fw-52002.amazon.com (smtp-fw-52002.amazon.com [52.119.213.150]) (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 B56D51FECB1 for ; Fri, 10 Jan 2025 09:31:55 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=52.119.213.150 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736501517; cv=none; b=m5qLUqHV63BkLgYtqcy+cAk+qGgVu2hQZduVMh+CV9pwIoJaZMyH8GRaZFu1JR2sumDO05N0T+nQgGvRflXXOMLcSwOIVdzaUr1N0LUcyQSdz3B2DjKL+wHtJke8tYgrIf6KgZz9ELn39H+h/WnOjnSDqal6m01MnFdg4lTH918= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736501517; c=relaxed/simple; bh=S4zrMgl7NyixYd02n/Q3dyfdZS0DSw1ZjFm9TgSRwpc=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=bv63T+WUOJGaP17MjFM0luv6+vVjYzpw4B9gP1eeH/6FaQtMy5nvTzoztsLUC9VdgFwfCL763iQKJlILY5vmaclH9u3kk36ydV4C11pwUPWudMauH3oeoMD1FWYTOFFJEOqEVg5Ij1laa7Na3qzbKQnOJ6Tm1kONgOYJd2aCWfo= 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=a/2LXzW5; arc=none smtp.client-ip=52.119.213.150 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="a/2LXzW5" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amazon.com; i=@amazon.com; q=dns/txt; s=amazon201209; t=1736501516; x=1768037516; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=VcfmV2krEnkd9vEiozkfxfOtUeg3hiAitDwT1lRHmEc=; b=a/2LXzW5DtQzztXMPHgV+6niSs7vnNAT94TLa0M/OuNhvOXkanqgkZMm 1OG3gQWuDymUpTCWeTPACY6afbOS1H5f8iy8htHCdMdfuLwqamggqZGUw 5OmiR8S7A8Z6ks8QEayJzEddezwVKhp4QE19F2JjJSKeOBXPuhtVFpCzG c=; X-IronPort-AV: E=Sophos;i="6.12,303,1728950400"; d="scan'208";a="688274840" 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-52002.iad7.amazon.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 10 Jan 2025 09:31:53 +0000 Received: from EX19MTAUWB001.ant.amazon.com [10.0.38.20:39259] by smtpin.naws.us-west-2.prod.farcaster.email.amazon.dev [10.0.16.12:2525] with esmtp (Farcaster) id dd335672-883a-4fae-b8ca-99b1f6b1b3da; Fri, 10 Jan 2025 09:31:52 +0000 (UTC) X-Farcaster-Flow-ID: dd335672-883a-4fae-b8ca-99b1f6b1b3da 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; Fri, 10 Jan 2025 09:31:51 +0000 Received: from 6c7e67c6786f.amazon.com (10.118.252.101) 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; Fri, 10 Jan 2025 09:31:48 +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 v1 net-next 11/12] af_unix: Set drop reason in unix_dgram_disconnected(). Date: Fri, 10 Jan 2025 18:26:40 +0900 Message-ID: <20250110092641.85905-12-kuniyu@amazon.com> X-Mailer: git-send-email 2.39.5 (Apple Git-154) In-Reply-To: <20250110092641.85905-1-kuniyu@amazon.com> References: <20250110092641.85905-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: EX19D041UWB004.ant.amazon.com (10.13.139.143) 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 disconnect() 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 80c979266d37..b976f59dbcdd 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 Fri Jan 10 09:26:41 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kuniyuki Iwashima X-Patchwork-Id: 13934095 X-Patchwork-Delegate: kuba@kernel.org Received: from smtp-fw-80008.amazon.com (smtp-fw-80008.amazon.com [99.78.197.219]) (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 7FDE613C80E for ; Fri, 10 Jan 2025 09:32:26 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=99.78.197.219 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736501550; cv=none; b=Slk6rbXQVyzWKbsNx3SzxptWb2hxkA5H3yiTEzWTf1T2dzAxV0VR1FPhIOMPPypnMt7VbK8uAhSyqMvuswbpMMS6Y7ZrYYXH90w1J1B8cH5/IyZOlWt9c5mOWFLDSTx7gdeUbFg/PDj98YIeZJOiXh7zT49qeyORZ4n4crNcu/A= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736501550; c=relaxed/simple; bh=a6w6+e75Xdmv0Ub/MEz5NTwNXqMMAzDsLeaAWckYJqY=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=O4C6ptJIn433LN6cQFaHMUKWozHpQ7dSisblXUTHQ0yupr1AfOy/ItSdw8P3NF3K2lhcWNBYZYflR6ROKrRCaEfWcrhHIRHpBLWjPsW6zpZ92cuwMmwTcWsAWJkOKFLu8OzS/Y9eEQX1x5PaN0jAKcyYm2ZljigI79/16DkAVnA= 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=T7rGCXSu; arc=none smtp.client-ip=99.78.197.219 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="T7rGCXSu" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amazon.com; i=@amazon.com; q=dns/txt; s=amazon201209; t=1736501548; x=1768037548; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=qfeTGL6XLM9ecoWzIi0BDPCcEYjNgsmEthrtLjV7iZk=; b=T7rGCXSuPz6/0UryhJyfqOCSY7Ba7/JybzeeVE7VrbbD0WUnj6snR8yR FmF/VZ54My6YPXLVbqxL5VUTuBA9u+ZHeydsBbk8udP2bIDcFYpJLeJDd H+r5Z8+a9CJyI9z+FcSUW5oMZzTzAN7xOgzEBif1ovjh6y1UlBeHvn9EQ c=; X-IronPort-AV: E=Sophos;i="6.12,303,1728950400"; d="scan'208";a="160423177" Received: from pdx4-co-svc-p1-lb2-vlan3.amazon.com (HELO smtpout.prod.us-west-2.prod.farcaster.email.amazon.dev) ([10.25.36.214]) by smtp-border-fw-80008.pdx80.corp.amazon.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 10 Jan 2025 09:32:19 +0000 Received: from EX19MTAUWC001.ant.amazon.com [10.0.38.20:2231] by smtpin.naws.us-west-2.prod.farcaster.email.amazon.dev [10.0.58.22:2525] with esmtp (Farcaster) id 1a192ec6-4cd6-4430-b330-c0def9906862; Fri, 10 Jan 2025 09:32:19 +0000 (UTC) X-Farcaster-Flow-ID: 1a192ec6-4cd6-4430-b330-c0def9906862 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; Fri, 10 Jan 2025 09:32:18 +0000 Received: from 6c7e67c6786f.amazon.com (10.118.252.101) 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; Fri, 10 Jan 2025 09:32:15 +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 v1 net-next 12/12] af_unix: Set drop reason in unix_dgram_sendmsg(). Date: Fri, 10 Jan 2025 18:26:41 +0900 Message-ID: <20250110092641.85905-13-kuniyu@amazon.com> X-Mailer: git-send-email 2.39.5 (Apple Git-154) In-Reply-To: <20250110092641.85905-1-kuniyu@amazon.com> References: <20250110092641.85905-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: EX19D045UWC004.ant.amazon.com (10.13.139.203) 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 For UNIX_NO_PERM to reproduce: # 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 b976f59dbcdd..158c6b8ba141 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;