From patchwork Sun Mar 9 13:28:12 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Aleksandr Mikhalitsyn X-Patchwork-Id: 14008418 X-Patchwork-Delegate: kuba@kernel.org Received: from smtp-relay-internal-1.canonical.com (smtp-relay-internal-1.canonical.com [185.125.188.123]) (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 200F51DC99A for ; Sun, 9 Mar 2025 13:29:52 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=185.125.188.123 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1741526996; cv=none; b=pwjNMUROPYJi549AvuG6fB6sveUmFd1Xf3nB3dduo90jHw1HV9MuAeqAjsnyE1m4MxcxPYr7kfWzzYzY/wumrgtm8avxNHxlKnMROr8/tKwki+TKJjS0bL+aQCxrxlbAGMa0ps7km0yLVYFe6AaGextKYUASfYdEJSvV0xbYFeI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1741526996; c=relaxed/simple; bh=vnuWxUByDaK53vFXT9n7vqyzxlt2qtW+8f94W06a7gQ=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=e5QSQalscTbg8yubCmYkACZDqjsUM3kMNUz5k3psJHHzkzQkzI0nZdleb31SHz1qZ6DKNE/17ZVGuL589t+CUr3OxQsrrMZRiZh8DmQ3N/bufYaRk6UaWVDXDNA9NDKFxXs4YULA+HUDhzJCMxcEoVBNMSyfxisJqpR3KiAF0ng= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=canonical.com; spf=pass smtp.mailfrom=canonical.com; dkim=pass (2048-bit key) header.d=canonical.com header.i=@canonical.com header.b=H0VzkN6s; arc=none smtp.client-ip=185.125.188.123 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=canonical.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=canonical.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=canonical.com header.i=@canonical.com header.b="H0VzkN6s" Received: from mail-ej1-f69.google.com (mail-ej1-f69.google.com [209.85.218.69]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by smtp-relay-internal-1.canonical.com (Postfix) with ESMTPS id 195833F869 for ; Sun, 9 Mar 2025 13:29:42 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=canonical.com; s=20210705; t=1741526982; bh=shYgV0qT/lqjUQTrvdmbHNW7lzUO23tYACdmBi90n70=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=H0VzkN6sGHWgjqmuCE/jJLzf7z+Hsukx9CzPCt1BWMnInd8a6WmEjF2ZJLHVm730N z6pTws4qkdm0Djg/24mVbbecLDolZ4spbGN43IZoUTo7pQH3ohqL6nF3ypzCYEvSEJ RvVuPU/bXZxeSbcI43DHxbo5mRC18aR0xt5qr2eyk/JjviLK2CPGwKllgD3HYamXTn 4EptslktqAOekglwbBr1Dc8SmDQ/5scbCN/Vqd5fJaQutRumMvq89qis6hHjQrjzqW rB2unfkiLp1yBDDGV35oAas4n+PIzthx+Qtww50MURL5/NdsyWG1tfpkT5rZjQy5z7 xBdpN4r69eDHg== Received: by mail-ej1-f69.google.com with SMTP id a640c23a62f3a-ac287f28514so81508866b.0 for ; Sun, 09 Mar 2025 06:29:42 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1741526979; x=1742131779; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=shYgV0qT/lqjUQTrvdmbHNW7lzUO23tYACdmBi90n70=; b=l1JQ8ZOQgDqL0gB7tsdCSMU/mgtFQjGKAnf+7CYQqDsE6f87ci3MEVhnKwvkZmURkA 56mfgK1RJKTUQN3V8a0DRqgAgghrY9h+dIMY+bqnVgmr4eHHl0HIQe+m6B4oC+0OsFUe 0akxXiKUBMr/gCMGFB381NPytXthpnvyvOmR2HDivE4Z5OK7Ls06lVrkctSuP8VNaXpq jlMiCrc9g1hRAeTW6XxpAJqTg+ifaDcvL5jZQo/mYpjSNBj00B5YWDUtDFngSHp4eC49 eqgd9tgT34cMRTGMez5baOTrm2qExAVF0Prp0zl2WzYIKKM+uNHkT7X3YC0bF20GNWzu RdEA== X-Forwarded-Encrypted: i=1; AJvYcCUNwA2ElKdfqOtHV/iGCYiOgwpk9VvqzFEaBLmFrsRkCFx2/JplzTaKlDeuOYiKUflHc/+7J94=@vger.kernel.org X-Gm-Message-State: AOJu0YyydJZf/xPHaYJPCeMmxyfzDrksVBa+GAkj5SSaSn66CaMch70Z xWqpP0e6kTTIgy3aYMBBILABOBkCmF2lohpMH48217icB8+Hb2U59xfFMxPgjhi5DlUNbRu7xey nAUkK00VeKvC8t4l78rHqPIO92Cx5STGX2wcK/HPzNoje0o/vjE9l6dmnEuGNPmnEHxdXKA== X-Gm-Gg: ASbGnctddpryTGlJ2Y71zwCcScBshIJtlRYQYWNYI/u5oz+gVax9OOwTmm4XHH5ETws 8+Gb3A9sXnv+t1UfG24fxfCzPboCvP8eWRYICYq9PV9TwQqMlIozbwosB0D8ZtP41ITj89AUlj9 DX1wGxUvQy9XgFYKDrq/MuZjZ48atr1rTPVftZKwrwlCnD+miyClmoKl0bVdW3iMxGjv5/u4AY0 vErsYYxYmJ47kEzsbB6lgqXV7wSrUkN6pVlrrrrHl5MgabD2B781dvfKQJl1h8EdyvjUpD/rV5X 2Py5kBKt1qSIjCuoBxQC9CCfeILucjukYE0WjhD65/d2caX33geTmB7c7k9wH01o6KWAjvww0gy NSw8gvsxm9z4Yd/vm4g== X-Received: by 2002:a17:907:c302:b0:ac2:f93:b7c5 with SMTP id a640c23a62f3a-ac2526e1ba9mr1073986966b.31.1741526979544; Sun, 09 Mar 2025 06:29:39 -0700 (PDT) X-Google-Smtp-Source: AGHT+IEWS1LQrRdoibpiP6escIaOL/a7XzBpJqfiCaikM73Pa+IXdnzBUWE6E4F2v53XQL0h2fMQ5g== X-Received: by 2002:a17:907:c302:b0:ac2:f93:b7c5 with SMTP id a640c23a62f3a-ac2526e1ba9mr1073984866b.31.1741526979165; Sun, 09 Mar 2025 06:29:39 -0700 (PDT) Received: from localhost.localdomain (ipbcc0714d.dynamic.kabel-deutschland.de. [188.192.113.77]) by smtp.gmail.com with ESMTPSA id a640c23a62f3a-ac25943f55csm435897366b.137.2025.03.09.06.29.38 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 09 Mar 2025 06:29:38 -0700 (PDT) From: Alexander Mikhalitsyn To: kuniyu@amazon.com Cc: Alexander Mikhalitsyn , linux-kernel@vger.kernel.org, netdev@vger.kernel.org, cgroups@vger.kernel.org, "David S. Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni , Willem de Bruijn , Leon Romanovsky , Arnd Bergmann , Christian Brauner , Lennart Poettering , Luca Boccassi , Tejun Heo , Johannes Weiner , =?utf-8?q?Michal_Koutn=C3=BD?= Subject: [PATCH net-next 1/4] net: unix: print cgroup_id and peer_cgroup_id in fdinfo Date: Sun, 9 Mar 2025 14:28:12 +0100 Message-ID: <20250309132821.103046-2-aleksandr.mikhalitsyn@canonical.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20250309132821.103046-1-aleksandr.mikhalitsyn@canonical.com> References: <20250309132821.103046-1-aleksandr.mikhalitsyn@canonical.com> Precedence: bulk X-Mailing-List: netdev@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Patchwork-Delegate: kuba@kernel.org Cc: linux-kernel@vger.kernel.org Cc: netdev@vger.kernel.org Cc: cgroups@vger.kernel.org Cc: "David S. Miller" Cc: Eric Dumazet Cc: Jakub Kicinski Cc: Paolo Abeni Cc: Willem de Bruijn Cc: Leon Romanovsky Cc: Arnd Bergmann Cc: Christian Brauner Cc: Kuniyuki Iwashima Cc: Lennart Poettering Cc: Luca Boccassi Cc: Tejun Heo Cc: Johannes Weiner Cc: "Michal Koutný" Signed-off-by: Alexander Mikhalitsyn --- net/unix/af_unix.c | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/net/unix/af_unix.c b/net/unix/af_unix.c index 7f8f3859cdb3..2b2c0036efc9 100644 --- a/net/unix/af_unix.c +++ b/net/unix/af_unix.c @@ -117,6 +117,7 @@ #include #include #include +#include static atomic_long_t unix_nr_socks; static struct hlist_head bsd_socket_buckets[UNIX_HASH_SIZE / 2]; @@ -861,6 +862,11 @@ static void unix_show_fdinfo(struct seq_file *m, struct socket *sock) int nr_fds = 0; if (sk) { +#ifdef CONFIG_SOCK_CGROUP_DATA + struct sock *peer; + u64 sk_cgroup_id = 0; +#endif + s_state = READ_ONCE(sk->sk_state); u = unix_sk(sk); @@ -874,6 +880,21 @@ static void unix_show_fdinfo(struct seq_file *m, struct socket *sock) nr_fds = unix_count_nr_fds(sk); seq_printf(m, "scm_fds: %u\n", nr_fds); + +#ifdef CONFIG_SOCK_CGROUP_DATA + sk_cgroup_id = cgroup_id(sock_cgroup_ptr(&sk->sk_cgrp_data)); + seq_printf(m, "cgroup_id: %llu\n", sk_cgroup_id); + + peer = unix_peer_get(sk); + if (peer) { + u64 peer_cgroup_id = 0; + + peer_cgroup_id = cgroup_id(sock_cgroup_ptr(&peer->sk_cgrp_data)); + sock_put(peer); + + seq_printf(m, "peer_cgroup_id: %llu\n", peer_cgroup_id); + } +#endif } } #else From patchwork Sun Mar 9 13:28:13 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Aleksandr Mikhalitsyn X-Patchwork-Id: 14008419 X-Patchwork-Delegate: kuba@kernel.org Received: from smtp-relay-internal-0.canonical.com (smtp-relay-internal-0.canonical.com [185.125.188.122]) (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 02E3B1DE2AE for ; Sun, 9 Mar 2025 13:29:58 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=185.125.188.122 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1741527000; cv=none; b=ixO/xvo9wOWp8e0Xg48FWmTbz+58SaTX0c3DPdwM2ROsKH+mXcfHfnPmjqx+5hvnEZpz7VZ61IGipIDlKbnN/qMD1kO2eMc1xTJxHb/dNoZ/HlJhcF5i4SLzLnCVVu8sJcMKwKY/28zrbL1tk0UrDFIZovbYAF8e9GkW0bw4vP0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1741527000; c=relaxed/simple; bh=h11slZywu67PPCzd8E5r1pZxOSwbTAwOByW1hwfHQ8Y=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=qFla/SzI2J6W+Tz0za+a1d9kBk1ZTQWTSjWkxMCL91VZZ6Nec/wLFo44ZT8nDMr5/8wHunl3jKEWb7ZeVLz/ru/Y/OjjpWH0V8m2sbERgZhlIEOnuT0C73sB751L6pzedXjR1ob06OO57R/0aSIXl3uRGR3lYmUGad3LSMlKtKI= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=canonical.com; spf=pass smtp.mailfrom=canonical.com; dkim=pass (2048-bit key) header.d=canonical.com header.i=@canonical.com header.b=txhXF2bu; arc=none smtp.client-ip=185.125.188.122 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=canonical.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=canonical.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=canonical.com header.i=@canonical.com header.b="txhXF2bu" Received: from mail-ej1-f70.google.com (mail-ej1-f70.google.com [209.85.218.70]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by smtp-relay-internal-0.canonical.com (Postfix) with ESMTPS id B295F3FCE3 for ; Sun, 9 Mar 2025 13:29:47 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=canonical.com; s=20210705; t=1741526987; bh=oUkDN9VrMm5b6Y+/DOvznDBPjXORLJgfeSeYgeNTS6Y=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=txhXF2buvWMJtfq4kau+EI8lAgIPrA1rICrJTVlX+UzLDwOV43fOGpsFMG+MgZ6dN LD2/vB/ACv1SVOtfe3MbhdwrzMD0mAFSdnTIh/Ab6frnOx46feyjThUYLPOfdSvHhp SllOOYG7ZlDU55xsSQ0akha4zufOUz9dC91gzkOU18odVmU8ojOpYj/3qh43qMHiz/ g/BynDtceOwypqEK1PeyRSDRYeS4G4cUMeZvIdgYJ9H75sR4d1S/8eEbwX+3UPyu+i xHdvwKMhH4ZlwUsUYhw7NFV0sTC0xrR0oEmEjz30D8CRlbq39nKK2YVnwSSPDsYcen 8W9gftYLBDlWg== Received: by mail-ej1-f70.google.com with SMTP id a640c23a62f3a-ac21873859dso350682566b.2 for ; Sun, 09 Mar 2025 06:29:47 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1741526981; x=1742131781; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=oUkDN9VrMm5b6Y+/DOvznDBPjXORLJgfeSeYgeNTS6Y=; b=l0+nl3ymgDSKsk0Z6051opepLxFQutEzJQNhqAVap2WVTZPkjguyq/OCQ38vuBCt61 wOOSzRqcWutOHBsEpv05/FrP3x1jFWS1U8OkA0qhdv8HeO5mUGqOJbhJ7QaOfx+WUn+7 13S7booXJ4qwkLyUILslYEMgG3fD/m2FFqx5o5lIMd/CPl3x6GRBwQ37jTE6BzlVZePj fHQKjPdD+D0xa/1yeunsgd4oOLQZ77CnqeaKMFTGFKSnaUVTyEKln0nZRkxmH+K9IBp3 N43o6Vfd8Bo5IqbvfbuuR8bYRHL573peSyCYfV5eZ6nJYVjJDORhwRveO2LhwnxbaDxf 6Ocw== X-Forwarded-Encrypted: i=1; AJvYcCW68+4xjhhLWzI24znaGgWcirHDQY2OmTIVu7FkA/J6yNNma33HPez0gctBzQ/QjZ3HVeOe2v4=@vger.kernel.org X-Gm-Message-State: AOJu0Yy4cYzNXfU37RGld8FaLFRlcYeb+/xZyNLrDnvtarSkfh9U1FUc 90BgsiNCsqDe1hAC5NrsBj+P7PRpa2r5x57yAzqtTPvLH/Vo0R/ppzENWqxy403DQaUHr5ucQAu eKWSdUcBTqP6CdJlLit+FNknXwyC2BkvG6BlA6HcAUt140PET13aYlQhZ1F197yluLwq2zA== X-Gm-Gg: ASbGncvskNnle4IJbrFg4SQ7dxukE++dxPd/TkSmMvuPm+tJtY/1chVEq8m2ARTT1ck hQp/iaUUKHfT4el1bUfKdRnZPKoFZoYDdTHQSlD14nUDmY6kmznH+4OPV6IbdQhyWdddQB9Do4I U0w0B4EXMCPhWMuyXnl7/HpuXzy37X5gVx5N+ljSGq4RxLuZuzKc1i1MBUK+i8Yn+NrRdUrA3BU T3cNFtH1HsVcW9oX0tab2/wkG8tOYFIWuu3+O+eKn4zedDwAb0KzJXF7kcHfhxINgTRWjbHzagT 2NZJoHrBz3S+o+LcQRVuwJRJSRdZG6fcNyjE0KFmFzlzhga2h1zae8aHl3pdZ9InA9XAXTCvUWg LGalEQODqhtsYiXzkdA== X-Received: by 2002:a17:907:9615:b0:ac2:9c7d:e129 with SMTP id a640c23a62f3a-ac29c7dee94mr115089266b.38.1741526981521; Sun, 09 Mar 2025 06:29:41 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHnWyfFVWjU228SRznBeLkPmsqMd2onjEQ1MWjIegB/cFAZnvOBfloK324qPC2AGva8B49jNg== X-Received: by 2002:a17:907:9615:b0:ac2:9c7d:e129 with SMTP id a640c23a62f3a-ac29c7dee94mr115085566b.38.1741526981136; Sun, 09 Mar 2025 06:29:41 -0700 (PDT) Received: from localhost.localdomain (ipbcc0714d.dynamic.kabel-deutschland.de. [188.192.113.77]) by smtp.gmail.com with ESMTPSA id a640c23a62f3a-ac25943f55csm435897366b.137.2025.03.09.06.29.40 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 09 Mar 2025 06:29:40 -0700 (PDT) From: Alexander Mikhalitsyn To: kuniyu@amazon.com Cc: Alexander Mikhalitsyn , linux-kernel@vger.kernel.org, netdev@vger.kernel.org, cgroups@vger.kernel.org, "David S. Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni , Willem de Bruijn , Leon Romanovsky , Arnd Bergmann , Christian Brauner , Lennart Poettering , Luca Boccassi , Tejun Heo , Johannes Weiner , =?utf-8?q?Michal_Koutn=C3=BD?= Subject: [PATCH net-next 2/4] net: core: add getsockopt SO_PEERCGROUPID Date: Sun, 9 Mar 2025 14:28:13 +0100 Message-ID: <20250309132821.103046-3-aleksandr.mikhalitsyn@canonical.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20250309132821.103046-1-aleksandr.mikhalitsyn@canonical.com> References: <20250309132821.103046-1-aleksandr.mikhalitsyn@canonical.com> Precedence: bulk X-Mailing-List: netdev@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Patchwork-Delegate: kuba@kernel.org Add SO_PEERCGROUPID which allows to get cgroup_id for a socket. We already have analogical interfaces to retrieve this information: - inet_diag: INET_DIAG_CGROUP_ID - eBPF: bpf_sk_cgroup_id Having getsockopt() interface makes sense for many applications, because using eBPF is not always an option, while inet_diag has obvious complexety and performance drawbacks if we only want to get this specific info for one specific socket. Cc: linux-kernel@vger.kernel.org Cc: netdev@vger.kernel.org Cc: cgroups@vger.kernel.org Cc: "David S. Miller" Cc: Eric Dumazet Cc: Jakub Kicinski Cc: Paolo Abeni Cc: Willem de Bruijn Cc: Leon Romanovsky Cc: Arnd Bergmann Cc: Christian Brauner Cc: Kuniyuki Iwashima Cc: Lennart Poettering Cc: Luca Boccassi Cc: Tejun Heo Cc: Johannes Weiner Cc: "Michal Koutný" Signed-off-by: Alexander Mikhalitsyn --- arch/alpha/include/uapi/asm/socket.h | 2 + arch/mips/include/uapi/asm/socket.h | 2 + arch/parisc/include/uapi/asm/socket.h | 2 + arch/sparc/include/uapi/asm/socket.h | 2 + include/uapi/asm-generic/socket.h | 2 + net/core/sock.c | 17 +++++++ net/unix/af_unix.c | 63 +++++++++++++++++++++++++ tools/include/uapi/asm-generic/socket.h | 2 + 8 files changed, 92 insertions(+) diff --git a/arch/alpha/include/uapi/asm/socket.h b/arch/alpha/include/uapi/asm/socket.h index 3df5f2dd4c0f..58ce457b2c09 100644 --- a/arch/alpha/include/uapi/asm/socket.h +++ b/arch/alpha/include/uapi/asm/socket.h @@ -150,6 +150,8 @@ #define SO_RCVPRIORITY 82 +#define SO_PEERCGROUPID 83 + #if !defined(__KERNEL__) #if __BITS_PER_LONG == 64 diff --git a/arch/mips/include/uapi/asm/socket.h b/arch/mips/include/uapi/asm/socket.h index 22fa8f19924a..823fa67f7d79 100644 --- a/arch/mips/include/uapi/asm/socket.h +++ b/arch/mips/include/uapi/asm/socket.h @@ -161,6 +161,8 @@ #define SO_RCVPRIORITY 82 +#define SO_PEERCGROUPID 83 + #if !defined(__KERNEL__) #if __BITS_PER_LONG == 64 diff --git a/arch/parisc/include/uapi/asm/socket.h b/arch/parisc/include/uapi/asm/socket.h index aa9cd4b951fe..1ee2e858d177 100644 --- a/arch/parisc/include/uapi/asm/socket.h +++ b/arch/parisc/include/uapi/asm/socket.h @@ -142,6 +142,8 @@ #define SO_RCVPRIORITY 0x404D +#define SO_PEERCGROUPID 0x404E + #if !defined(__KERNEL__) #if __BITS_PER_LONG == 64 diff --git a/arch/sparc/include/uapi/asm/socket.h b/arch/sparc/include/uapi/asm/socket.h index 5b464a568664..2fe7d0c48a63 100644 --- a/arch/sparc/include/uapi/asm/socket.h +++ b/arch/sparc/include/uapi/asm/socket.h @@ -143,6 +143,8 @@ #define SO_RCVPRIORITY 0x005b +#define SO_PEERCGROUPID 0x005c + #if !defined(__KERNEL__) diff --git a/include/uapi/asm-generic/socket.h b/include/uapi/asm-generic/socket.h index aa5016ff3d91..903904bb537c 100644 --- a/include/uapi/asm-generic/socket.h +++ b/include/uapi/asm-generic/socket.h @@ -145,6 +145,8 @@ #define SO_RCVPRIORITY 82 +#define SO_PEERCGROUPID 83 + #if !defined(__KERNEL__) #if __BITS_PER_LONG == 64 || (defined(__x86_64__) && defined(__ILP32__)) diff --git a/net/core/sock.c b/net/core/sock.c index a0598518ce89..6dc0b1a8367b 100644 --- a/net/core/sock.c +++ b/net/core/sock.c @@ -1946,6 +1946,23 @@ int sk_getsockopt(struct sock *sk, int level, int optname, goto lenout; } +#ifdef CONFIG_SOCK_CGROUP_DATA + case SO_PEERCGROUPID: + { + const struct proto_ops *ops; + + if (sk->sk_family != AF_UNIX) + return -EOPNOTSUPP; + + ops = READ_ONCE(sock->ops); + if (!ops->getsockopt) + return -EOPNOTSUPP; + + return ops->getsockopt(sock, SOL_SOCKET, optname, optval.user, + optlen.user); + } +#endif + /* Dubious BSD thing... Probably nobody even uses it, but * the UNIX standard wants it for whatever reason... -DaveM */ diff --git a/net/unix/af_unix.c b/net/unix/af_unix.c index 2b2c0036efc9..3455f38f033d 100644 --- a/net/unix/af_unix.c +++ b/net/unix/af_unix.c @@ -901,6 +901,66 @@ static void unix_show_fdinfo(struct seq_file *m, struct socket *sock) #define unix_show_fdinfo NULL #endif +static int unix_getsockopt(struct socket *sock, int level, int optname, + char __user *optval, int __user *optlen) +{ + struct sock *sk = sock->sk; + + union { + int val; + u64 val64; + } v; + + int lv = sizeof(int); + int len; + + if (level != SOL_SOCKET) + return -ENOPROTOOPT; + + if (get_user(len, optlen)) + return -EFAULT; + + if (len < 0) + return -EINVAL; + + memset(&v, 0, sizeof(v)); + + switch (optname) { +#ifdef CONFIG_SOCK_CGROUP_DATA + case SO_PEERCGROUPID: + { + struct sock *peer; + u64 peer_cgroup_id = 0; + + lv = sizeof(u64); + if (len < lv) + return -EINVAL; + + peer = unix_peer_get(sk); + if (!peer) + return -ENODATA; + + peer_cgroup_id = cgroup_id(sock_cgroup_ptr(&peer->sk_cgrp_data)); + sock_put(peer); + + v.val64 = peer_cgroup_id; + break; + } +#endif + default: + return -ENOPROTOOPT; + } + + if (len > lv) + len = lv; + if (copy_to_user(optval, &v, len)) + return -EFAULT; + if (put_user(len, optlen)) + return -EFAULT; + + return 0; +} + static const struct proto_ops unix_stream_ops = { .family = PF_UNIX, .owner = THIS_MODULE, @@ -910,6 +970,7 @@ static const struct proto_ops unix_stream_ops = { .socketpair = unix_socketpair, .accept = unix_accept, .getname = unix_getname, + .getsockopt = unix_getsockopt, .poll = unix_poll, .ioctl = unix_ioctl, #ifdef CONFIG_COMPAT @@ -935,6 +996,7 @@ static const struct proto_ops unix_dgram_ops = { .socketpair = unix_socketpair, .accept = sock_no_accept, .getname = unix_getname, + .getsockopt = unix_getsockopt, .poll = unix_dgram_poll, .ioctl = unix_ioctl, #ifdef CONFIG_COMPAT @@ -959,6 +1021,7 @@ static const struct proto_ops unix_seqpacket_ops = { .socketpair = unix_socketpair, .accept = unix_accept, .getname = unix_getname, + .getsockopt = unix_getsockopt, .poll = unix_dgram_poll, .ioctl = unix_ioctl, #ifdef CONFIG_COMPAT diff --git a/tools/include/uapi/asm-generic/socket.h b/tools/include/uapi/asm-generic/socket.h index aa5016ff3d91..903904bb537c 100644 --- a/tools/include/uapi/asm-generic/socket.h +++ b/tools/include/uapi/asm-generic/socket.h @@ -145,6 +145,8 @@ #define SO_RCVPRIORITY 82 +#define SO_PEERCGROUPID 83 + #if !defined(__KERNEL__) #if __BITS_PER_LONG == 64 || (defined(__x86_64__) && defined(__ILP32__)) From patchwork Sun Mar 9 13:28:14 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Aleksandr Mikhalitsyn X-Patchwork-Id: 14008420 X-Patchwork-Delegate: kuba@kernel.org Received: from smtp-relay-internal-1.canonical.com (smtp-relay-internal-1.canonical.com [185.125.188.123]) (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 EBC301DD88D for ; Sun, 9 Mar 2025 13:29:58 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=185.125.188.123 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1741527000; cv=none; b=pqCeZ62CdkYgQ6fPkxn0yexC8DnJFM6QRyhuZf0SJ7QPaQW9bEI2rMakv0WZcT8eWhIBPhog1IUl5dKvLD/AWkWUsLQ3ZZjjbRgOYXiK0p7mNV1wbZ3DRwrJw4siOX55x1c+fPJ38EWZ7ftfexP67IH9T4/HIqaxOq+WnxrWfsw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1741527000; c=relaxed/simple; bh=HBzrftBgtmyJjSlrkubZ14+wzgBNDhuJgvNUC0ysav8=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=C6hQRtjHi0Ap47pwVhKfTwwGaNJr7LJTvP3RnMZeRynU4Q4i6SNyyfH/w0iWPRAzkJX6VsD2B75v3klKVLFvaZb0O/RX+6dJu7d2rh4Rdjt5+Vco73W1BL9QEhXb/TKCSybUxjEfh5RfNFx9hi9i7IoDXPWES2RHUTxsFjFM80c= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=canonical.com; spf=pass smtp.mailfrom=canonical.com; dkim=pass (2048-bit key) header.d=canonical.com header.i=@canonical.com header.b=Na0O5okq; arc=none smtp.client-ip=185.125.188.123 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=canonical.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=canonical.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=canonical.com header.i=@canonical.com header.b="Na0O5okq" Received: from mail-ej1-f69.google.com (mail-ej1-f69.google.com [209.85.218.69]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by smtp-relay-internal-1.canonical.com (Postfix) with ESMTPS id 8B55C403E3 for ; Sun, 9 Mar 2025 13:29:48 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=canonical.com; s=20210705; t=1741526988; bh=De9T9eT10I0LuI5V7o8PivgIMdFcydoHgbUnBTWTEUw=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=Na0O5okqMczoRCXSIY/inK7gbzvLkWF4vIxlnotNd5ER9Tm0UxYVyDREa4LzSSP0t 0rYbJqS5MfMhpjUY0kTVCOAary+U5oUASnagy1Q7csF9sqJvhz+oZ9qGj87ousHOYY 9QQfbAcCRqnQcg2mT5KmdYYat9Xv0MkOOpAMAdUq/sBMExnIb+Ml3fuBYw8CWtD8mb w03+wO9Zsmhp2lCra1lYZS5x9+w7/K/vlzxY+fGqvOOHiwvHfDiA5z298km4mAbCS6 rhz5FxQQv7vmsTmyGRKxW0+p0annYpH6904XBq/BuwNegAaMRyQeQEOmyJ5MM3KaCr 9oSKwAv3kxgNw== Received: by mail-ej1-f69.google.com with SMTP id a640c23a62f3a-ac1e442740cso317835466b.1 for ; Sun, 09 Mar 2025 06:29:48 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1741526983; x=1742131783; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=De9T9eT10I0LuI5V7o8PivgIMdFcydoHgbUnBTWTEUw=; b=ssLH4ejdEHXD5YxA2yqN41XaylQatyIvCGR5OrbWb1HltoDOs/5CWtZB4Iv+CtIE2j PGEALqMd6RbpFwbwnG3iZALxp/ajUDx7QKQwc89yuPn6y2TBaUZNWneAlxD4/Mgvu3QO PHCCDv3VgJ8kJ0s4n0aUml5fhPDWhOm29jea2sEMGPE1DAcOBzQ8P/sFAYKN/czBLyo+ D5vtnOBW3U0joxt25E3+fNl+xxSjKfyblVvDWd1TqCMhGQZMAYnypGq5uznm9WELjRPv pRvWQ65Wmvop7yIQcaYYMCW5IkfIgFkzqKnugsEuxLP7Qms20L5HJEb9hGua8LVY08O8 VaUA== X-Forwarded-Encrypted: i=1; AJvYcCVONdZ6H222dLLbi4tSLyDOVOL7gjOXVhVLKP+wkjfTAHr4g2RwazCn46t8kL/gXg9e49pigds=@vger.kernel.org X-Gm-Message-State: AOJu0YxMjMjuJcAWTzXRkyozv2riHH0SZUVFEInqX7i8uLrloG5JwVxH Q5c1HEDRy6HtCpCf1xWUz6JuvUhYaQbSViq02rnT9LkiuHqtAY2aY8SEDx1ufL3KlYVuok5iaa3 IRZyHx4O8OKABX1n7Zo2etuIbwGoEjmpkgUgkRIlIMjl6iV7K3ZFtjI+vR/KKnVmr8RjRRy1WSB zJJw== X-Gm-Gg: ASbGnctE4a3+hMCJH1LTlbOCdCAmH11JXIgXyVvToC8KnAXhAqyfW1klnA0HZu2paf4 Ge3+5Bc//T8gTOe1P7KNgXNkxVg+CGZhEZQAMrN6f5iJUV/xRIjyktwlnq0ktIOGXhP/0r/LLxk AEvsTE3P7UiAIt51eG40DmwvgqPvrkhpFZtNV+S5SuzcxA3FofnSxYLYmcwbPqle2XjMCef/38x 6LMr3+et16xGCb6KitRhNyEP3jRwz7nGSV5a/dysqf4Nj7xrtdQsHELhM8Avmz/kxs+XJOCFOd+ DWGdvHOtefcX/nOoEhJvDiMfjNqiEwGcYlDAw9irdMh2t5TOzf8mVjLE/wWPyPlnXBjXMPp6MXI ErqNOsnpPTATO+O6JtA== X-Received: by 2002:a17:907:97d5:b0:abf:6166:d0e0 with SMTP id a640c23a62f3a-ac252fb9c80mr1252827366b.35.1741526983265; Sun, 09 Mar 2025 06:29:43 -0700 (PDT) X-Google-Smtp-Source: AGHT+IG0Jp1lBL5UEDOmoLhr3/1+wDXXnEG000YcKS7uOUaGglmKZVGqoQ8LvIDpjopxUUJhpkTkFg== X-Received: by 2002:a17:907:97d5:b0:abf:6166:d0e0 with SMTP id a640c23a62f3a-ac252fb9c80mr1252824566b.35.1741526982866; Sun, 09 Mar 2025 06:29:42 -0700 (PDT) Received: from localhost.localdomain (ipbcc0714d.dynamic.kabel-deutschland.de. [188.192.113.77]) by smtp.gmail.com with ESMTPSA id a640c23a62f3a-ac25943f55csm435897366b.137.2025.03.09.06.29.41 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 09 Mar 2025 06:29:42 -0700 (PDT) From: Alexander Mikhalitsyn To: kuniyu@amazon.com Cc: Alexander Mikhalitsyn , linux-kselftest@vger.kernel.org, linux-kernel@vger.kernel.org, netdev@vger.kernel.org, cgroups@vger.kernel.org, "David S. Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni , Willem de Bruijn , Leon Romanovsky , Arnd Bergmann , Christian Brauner , Lennart Poettering , Luca Boccassi , Tejun Heo , Johannes Weiner , =?utf-8?q?Michal_Koutn=C3=BD?= , Shuah Khan Subject: [PATCH net-next 3/4] tools/testing/selftests/cgroup/cgroup_util: add cg_get_id helper Date: Sun, 9 Mar 2025 14:28:14 +0100 Message-ID: <20250309132821.103046-4-aleksandr.mikhalitsyn@canonical.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20250309132821.103046-1-aleksandr.mikhalitsyn@canonical.com> References: <20250309132821.103046-1-aleksandr.mikhalitsyn@canonical.com> Precedence: bulk X-Mailing-List: netdev@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Patchwork-Delegate: kuba@kernel.org Cc: linux-kselftest@vger.kernel.org Cc: linux-kernel@vger.kernel.org Cc: netdev@vger.kernel.org Cc: cgroups@vger.kernel.org Cc: "David S. Miller" Cc: Eric Dumazet Cc: Jakub Kicinski Cc: Paolo Abeni Cc: Willem de Bruijn Cc: Leon Romanovsky Cc: Arnd Bergmann Cc: Christian Brauner Cc: Kuniyuki Iwashima Cc: Lennart Poettering Cc: Luca Boccassi Cc: Tejun Heo Cc: Johannes Weiner Cc: "Michal Koutný" Cc: Shuah Khan Signed-off-by: Alexander Mikhalitsyn --- tools/testing/selftests/cgroup/cgroup_util.c | 15 +++++++++++++++ tools/testing/selftests/cgroup/cgroup_util.h | 2 ++ 2 files changed, 17 insertions(+) diff --git a/tools/testing/selftests/cgroup/cgroup_util.c b/tools/testing/selftests/cgroup/cgroup_util.c index 1e2d46636a0c..b60e0e1433f4 100644 --- a/tools/testing/selftests/cgroup/cgroup_util.c +++ b/tools/testing/selftests/cgroup/cgroup_util.c @@ -205,6 +205,21 @@ int cg_open(const char *cgroup, const char *control, int flags) return open(path, flags); } +/* + * Returns cgroup id on success, or -1 on failure. + */ +uint64_t cg_get_id(const char *cgroup) +{ + struct stat st; + int ret; + + ret = stat(cgroup, &st); + if (ret) + return -1; + + return st.st_ino; +} + int cg_write_numeric(const char *cgroup, const char *control, long value) { char buf[64]; diff --git a/tools/testing/selftests/cgroup/cgroup_util.h b/tools/testing/selftests/cgroup/cgroup_util.h index 19b131ee7707..3f2d9676ceda 100644 --- a/tools/testing/selftests/cgroup/cgroup_util.h +++ b/tools/testing/selftests/cgroup/cgroup_util.h @@ -1,5 +1,6 @@ /* SPDX-License-Identifier: GPL-2.0 */ #include +#include #include #include "../kselftest.h" @@ -39,6 +40,7 @@ long cg_read_key_long(const char *cgroup, const char *control, const char *key); extern long cg_read_lc(const char *cgroup, const char *control); extern int cg_write(const char *cgroup, const char *control, char *buf); extern int cg_open(const char *cgroup, const char *control, int flags); +extern uint64_t cg_get_id(const char *cgroup); int cg_write_numeric(const char *cgroup, const char *control, long value); extern int cg_run(const char *cgroup, int (*fn)(const char *cgroup, void *arg), From patchwork Sun Mar 9 13:28:15 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Aleksandr Mikhalitsyn X-Patchwork-Id: 14008421 X-Patchwork-Delegate: kuba@kernel.org Received: from smtp-relay-internal-1.canonical.com (smtp-relay-internal-1.canonical.com [185.125.188.123]) (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 C099D1D47A2 for ; Sun, 9 Mar 2025 13:30:04 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=185.125.188.123 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1741527006; cv=none; b=k2QpN5IIwbZtFbvQazJ+pe3GfYuZeXuDLU9j1cRVAgjbxmjCLr25khhwA7AWGv7XXRUvtZzreTVk5WN56vNxnxfq86ZZg9BNfoS6Nc+q8bmK6dOIzRvLtRBmZig5bzFHJDypdFUEjnokicnv7m7IaJMVlrr3H9BOFHu6plILP7k= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1741527006; c=relaxed/simple; bh=fnMK/Ta7Z6dW4Bo0ev4kpeSTLsBbk/uNRa2jnNMBxsM=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=Y4LO1c73iDYV7qaP9IV164vDzBl1cBDWtA7VEHO09a6XLX/7v6T3h9JuOwGIwreIwufwFoanjxp1nnZT/MgKN7gut9i8quQZgiuNq5LcxKz3p4x/sCr2ZcLY1wU4mIztbyOPDic9ms/DiVgBvd7Hy48E+I7pX4hM67aKAX6JTdI= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=canonical.com; spf=pass smtp.mailfrom=canonical.com; dkim=pass (2048-bit key) header.d=canonical.com header.i=@canonical.com header.b=I+PCb9od; arc=none smtp.client-ip=185.125.188.123 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=canonical.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=canonical.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=canonical.com header.i=@canonical.com header.b="I+PCb9od" Received: from mail-ej1-f69.google.com (mail-ej1-f69.google.com [209.85.218.69]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by smtp-relay-internal-1.canonical.com (Postfix) with ESMTPS id C858540633 for ; Sun, 9 Mar 2025 13:29:52 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=canonical.com; s=20210705; t=1741526992; bh=+WlniEeO1sxFM6/UgRu+OkcVvl4eFAZ3k4+mcLNjIi4=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=I+PCb9odWRj1VVSNBgqjzo5YJwiWPv4XZeHx3tD5mwDPNqeM9ZkYRkuV72468sQgj K26qr59aYIbjr5SEuz9fO5al4sCjMtLv1EtJJVghoPVmQ/MAtid+mfpZSOZlP88T9y ce3DlmRe4GG2tl2K8sR4A4w3VvYsnfyHyuNFgG4by7l/eI+aDEPSmDhKUvtCzo2AAb iPRBPBrYoD8O2CM7fmTbLyQyA6GHY2AZDBelvtrM/tuGImrUwBMl0NcFprD9ln5kfQ FnlBUmuxpv4wQHSI9ovjr/pPhrITwBAwvLCDNemX1xRz7//JIocA7hpQZpHmQhQj+I ixHFt3PdXLZnw== Received: by mail-ej1-f69.google.com with SMTP id a640c23a62f3a-ac28a2c7c48so59426666b.3 for ; Sun, 09 Mar 2025 06:29:52 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1741526986; x=1742131786; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=+WlniEeO1sxFM6/UgRu+OkcVvl4eFAZ3k4+mcLNjIi4=; b=VkZQU9EldDGjD2caavHyJgFm8IKjaXmvW9bf6tPLMJXuG2JpLTXbfBOR1EiO1q+972 SeCArmKkcq/Qh9Uq2lsa7NTZklfN2tpqagxO4zw9MVe6vJaoI9zgH4q4nKW8XVM0jiLY 0AcbjMbq5EGIvJ55OMqs2caSq6ZiIptrDAxoyENDfqzZt0Fvb61EDpGbUB/52+dd9BA6 V5m/ICBY0xS40PpxHGbC8UdiV5JnkxJLksYgxxmLEcDHKevYubWjQuwDKfglqfN3SzoC zWoUKgc0O5Nm/sZ4SbzHYiGCOrJ0b67L+QTbpMnPf757XAhLzNwso1cbaJrCwRcNyyzw cvJg== X-Forwarded-Encrypted: i=1; AJvYcCXY4SHem0lr6SGu3rs/BrGCkufVpWcsfxbOkD51PEKjUjlAFK8Lfjk/woX8/+VYaWdvmwdWSfg=@vger.kernel.org X-Gm-Message-State: AOJu0YwCdXYW9hgi5hrR/5zFIOz6GOpxJps+gMRptWwABlebNnNMbaDm EGqHVH1Yt2iCUQXjVj2WzaCwlNfShFkBzuirS07DY3SuhE3AVEJZqzfFSZluzLWs1h+3CA1zKOG SjZ9FWyeYk6Q3Z+7CP6eOlDV672uQN2X7PgqCT0qLLpnOhv5p8q4sDOkoD7RMHs8ITTIsNg== X-Gm-Gg: ASbGncvtNATEIFQlKkC9P8UILFxj9S5RvWKLbSS4DWaFJhXmm2Ot/c8YwTkNd9LRwCX wxDdBzg461fo9evyd/1RmEhufMyeHne46WFBV0oAtelIFGXtZa7NfS/nNgoWNpgFXMKJl5rElwl HN3vBxtWiQX3r+PZImggSVC0rptyOOuBlBAawdj+dQnVj/q0C2OefLCXXUAM1YJw8NZWYKu/NYx NFXiq7YKyQCvHZygU8XcUflheLQuhhKeIIkWGPqoa9a0ycgbe9FMVgn54xjRVsbKMOqB/Po3Oiy sCCU7ob3+Gh8vZxth/qqScJe+eCoKNDWxbPY13vLP+0wzpvQq7m4Y6nErKTLLTZi3/ZscqgxkSw oxejUlQUhA8nxTmfXxw== X-Received: by 2002:a17:907:720a:b0:ac1:e881:89aa with SMTP id a640c23a62f3a-ac2525b9c95mr1204986266b.5.1741526985916; Sun, 09 Mar 2025 06:29:45 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFFa95jUlBdWGTjYgXwcr9ZUQLkTVCYf0hxOKWN8Duf76vORogdoI0XKHA/oyP746iZKMUFGA== X-Received: by 2002:a17:907:720a:b0:ac1:e881:89aa with SMTP id a640c23a62f3a-ac2525b9c95mr1204982566b.5.1741526985467; Sun, 09 Mar 2025 06:29:45 -0700 (PDT) Received: from localhost.localdomain (ipbcc0714d.dynamic.kabel-deutschland.de. [188.192.113.77]) by smtp.gmail.com with ESMTPSA id a640c23a62f3a-ac25943f55csm435897366b.137.2025.03.09.06.29.44 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 09 Mar 2025 06:29:45 -0700 (PDT) From: Alexander Mikhalitsyn To: kuniyu@amazon.com Cc: Alexander Mikhalitsyn , linux-kselftest@vger.kernel.org, linux-kernel@vger.kernel.org, netdev@vger.kernel.org, cgroups@vger.kernel.org, "David S. Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni , Willem de Bruijn , Leon Romanovsky , Arnd Bergmann , Christian Brauner , Lennart Poettering , Luca Boccassi , Tejun Heo , Johannes Weiner , =?utf-8?q?Michal_Koutn=C3=BD?= , Shuah Khan Subject: [PATCH net-next 4/4] tools/testing/selftests/cgroup: add test for SO_PEERCGROUPID Date: Sun, 9 Mar 2025 14:28:15 +0100 Message-ID: <20250309132821.103046-5-aleksandr.mikhalitsyn@canonical.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20250309132821.103046-1-aleksandr.mikhalitsyn@canonical.com> References: <20250309132821.103046-1-aleksandr.mikhalitsyn@canonical.com> Precedence: bulk X-Mailing-List: netdev@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Patchwork-Delegate: kuba@kernel.org Cc: linux-kselftest@vger.kernel.org Cc: linux-kernel@vger.kernel.org Cc: netdev@vger.kernel.org Cc: cgroups@vger.kernel.org Cc: "David S. Miller" Cc: Eric Dumazet Cc: Jakub Kicinski Cc: Paolo Abeni Cc: Willem de Bruijn Cc: Leon Romanovsky Cc: Arnd Bergmann Cc: Christian Brauner Cc: Kuniyuki Iwashima Cc: Lennart Poettering Cc: Luca Boccassi Cc: Tejun Heo Cc: Johannes Weiner Cc: "Michal Koutný" Cc: Shuah Khan Signed-off-by: Alexander Mikhalitsyn --- tools/testing/selftests/cgroup/Makefile | 2 + .../selftests/cgroup/test_so_peercgroupid.c | 308 ++++++++++++++++++ 2 files changed, 310 insertions(+) create mode 100644 tools/testing/selftests/cgroup/test_so_peercgroupid.c diff --git a/tools/testing/selftests/cgroup/Makefile b/tools/testing/selftests/cgroup/Makefile index 1b897152bab6..a932ff068081 100644 --- a/tools/testing/selftests/cgroup/Makefile +++ b/tools/testing/selftests/cgroup/Makefile @@ -16,6 +16,7 @@ TEST_GEN_PROGS += test_kill TEST_GEN_PROGS += test_kmem TEST_GEN_PROGS += test_memcontrol TEST_GEN_PROGS += test_pids +TEST_GEN_PROGS += test_so_peercgroupid TEST_GEN_PROGS += test_zswap LOCAL_HDRS += $(selfdir)/clone3/clone3_selftests.h $(selfdir)/pidfd/pidfd.h @@ -31,4 +32,5 @@ $(OUTPUT)/test_kill: cgroup_util.c $(OUTPUT)/test_kmem: cgroup_util.c $(OUTPUT)/test_memcontrol: cgroup_util.c $(OUTPUT)/test_pids: cgroup_util.c +$(OUTPUT)/test_so_peercgroupid: cgroup_util.c $(OUTPUT)/test_zswap: cgroup_util.c diff --git a/tools/testing/selftests/cgroup/test_so_peercgroupid.c b/tools/testing/selftests/cgroup/test_so_peercgroupid.c new file mode 100644 index 000000000000..2bf1f00a45c7 --- /dev/null +++ b/tools/testing/selftests/cgroup/test_so_peercgroupid.c @@ -0,0 +1,308 @@ +// SPDX-License-Identifier: GPL-2.0 OR MIT +#define _GNU_SOURCE +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "../kselftest_harness.h" +#include "cgroup_util.h" + +#define clean_errno() (errno == 0 ? "None" : strerror(errno)) +#define log_err(MSG, ...) \ + fprintf(stderr, "(%s:%d: errno: %s) " MSG "\n", __FILE__, __LINE__, \ + clean_errno(), ##__VA_ARGS__) + +#ifndef SO_PEERCGROUPID +#define SO_PEERCGROUPID 83 +#endif + +static void child_die() +{ + exit(1); +} + +struct sock_addr { + char sock_name[32]; + struct sockaddr_un listen_addr; + socklen_t addrlen; +}; + +FIXTURE(so_peercgroupid) +{ + int server; + pid_t client_pid; + int sync_sk[2]; + struct sock_addr server_addr; + struct sock_addr *client_addr; + char cgroup_root[PATH_MAX]; + char *test_cgroup1; + char *test_cgroup2; +}; + +FIXTURE_VARIANT(so_peercgroupid) +{ + int type; + bool abstract; +}; + +FIXTURE_VARIANT_ADD(so_peercgroupid, stream_pathname) +{ + .type = SOCK_STREAM, + .abstract = 0, +}; + +FIXTURE_VARIANT_ADD(so_peercgroupid, stream_abstract) +{ + .type = SOCK_STREAM, + .abstract = 1, +}; + +FIXTURE_VARIANT_ADD(so_peercgroupid, seqpacket_pathname) +{ + .type = SOCK_SEQPACKET, + .abstract = 0, +}; + +FIXTURE_VARIANT_ADD(so_peercgroupid, seqpacket_abstract) +{ + .type = SOCK_SEQPACKET, + .abstract = 1, +}; + +FIXTURE_VARIANT_ADD(so_peercgroupid, dgram_pathname) +{ + .type = SOCK_DGRAM, + .abstract = 0, +}; + +FIXTURE_VARIANT_ADD(so_peercgroupid, dgram_abstract) +{ + .type = SOCK_DGRAM, + .abstract = 1, +}; + +FIXTURE_SETUP(so_peercgroupid) +{ + self->client_addr = mmap(NULL, sizeof(*self->client_addr), PROT_READ | PROT_WRITE, + MAP_SHARED | MAP_ANONYMOUS, -1, 0); + ASSERT_NE(MAP_FAILED, self->client_addr); + + self->cgroup_root[0] = '\0'; +} + +FIXTURE_TEARDOWN(so_peercgroupid) +{ + close(self->server); + + kill(self->client_pid, SIGKILL); + waitpid(self->client_pid, NULL, 0); + + if (!variant->abstract) { + unlink(self->server_addr.sock_name); + unlink(self->client_addr->sock_name); + } + + if (strlen(self->cgroup_root) > 0) { + cg_enter_current(self->cgroup_root); + + if (self->test_cgroup1) + cg_destroy(self->test_cgroup1); + free(self->test_cgroup1); + + if (self->test_cgroup2) + cg_destroy(self->test_cgroup2); + free(self->test_cgroup2); + } +} + +static void fill_sockaddr(struct sock_addr *addr, bool abstract) +{ + char *sun_path_buf = (char *)&addr->listen_addr.sun_path; + + addr->listen_addr.sun_family = AF_UNIX; + addr->addrlen = offsetof(struct sockaddr_un, sun_path); + snprintf(addr->sock_name, sizeof(addr->sock_name), "so_peercgroupid_%d", getpid()); + addr->addrlen += strlen(addr->sock_name); + if (abstract) { + *sun_path_buf = '\0'; + addr->addrlen++; + sun_path_buf++; + } else { + unlink(addr->sock_name); + } + memcpy(sun_path_buf, addr->sock_name, strlen(addr->sock_name)); +} + +static void client(FIXTURE_DATA(so_peercgroupid) *self, + const FIXTURE_VARIANT(so_peercgroupid) *variant) +{ + int cfd, err; + socklen_t len; + uint64_t peer_cgroup_id = 0, test_cgroup1_id = 0, test_cgroup2_id = 0; + char state; + + cfd = socket(AF_UNIX, variant->type, 0); + if (cfd < 0) { + log_err("socket"); + child_die(); + } + + if (variant->type == SOCK_DGRAM) { + fill_sockaddr(self->client_addr, variant->abstract); + + if (bind(cfd, (struct sockaddr *)&self->client_addr->listen_addr, self->client_addr->addrlen)) { + log_err("bind"); + child_die(); + } + } + + /* negative testcase: no peer for socket yet */ + len = sizeof(peer_cgroup_id); + err = getsockopt(cfd, SOL_SOCKET, SO_PEERCGROUPID, &peer_cgroup_id, &len); + if (!err || (errno != ENODATA)) { + log_err("getsockopt must fail with errno == ENODATA when socket has no peer"); + child_die(); + } + + if (connect(cfd, (struct sockaddr *)&self->server_addr.listen_addr, + self->server_addr.addrlen) != 0) { + log_err("connect"); + child_die(); + } + + state = 'R'; + write(self->sync_sk[1], &state, sizeof(state)); + + read(self->sync_sk[1], &test_cgroup1_id, sizeof(uint64_t)); + read(self->sync_sk[1], &test_cgroup2_id, sizeof(uint64_t)); + + len = sizeof(peer_cgroup_id); + if (getsockopt(cfd, SOL_SOCKET, SO_PEERCGROUPID, &peer_cgroup_id, &len)) { + log_err("Failed to get SO_PEERCGROUPID"); + child_die(); + } + + /* + * There is a difference between connection-oriented sockets + * and connectionless ones from the perspective of SO_PEERCGROUPID. + * + * sk->sk_cgrp_data is getting filled when we allocate struct sock (see call to cgroup_sk_alloc()). + * For DGRAM socket, self->server socket is our peer and by the time when we allocate it, + * parent process sits in a test_cgroup1. Then it changes cgroup to test_cgroup2, but it does not + * affect anything. + * For STREAM/SEQPACKET socket, self->server is not our peer, but that one we get from accept() + * syscall. And by the time when we call accept(), parent process sits in test_cgroup2. + * + * Let's ensure that it works like that and if it get changed then we should detect it + * as it's a clear UAPI change. + */ + if (variant->type == SOCK_DGRAM) { + /* cgroup id from SO_PEERCGROUPID should be equal to the test_cgroup1_id */ + if (peer_cgroup_id != test_cgroup1_id) { + log_err("peer_cgroup_id != test_cgroup1_id: %" PRId64 " != %" PRId64, peer_cgroup_id, test_cgroup1_id); + child_die(); + } + } else { + /* cgroup id from SO_PEERCGROUPID should be equal to the test_cgroup2_id */ + if (peer_cgroup_id != test_cgroup2_id) { + log_err("peer_cgroup_id != test_cgroup2_id: %" PRId64 " != %" PRId64, peer_cgroup_id, test_cgroup2_id); + child_die(); + } + } +} + +TEST_F(so_peercgroupid, test) +{ + uint64_t test_cgroup1_id, test_cgroup2_id; + int err; + int pfd; + char state; + int child_status = 0; + + if (cg_find_unified_root(self->cgroup_root, sizeof(self->cgroup_root), NULL)) + ksft_exit_skip("cgroup v2 isn't mounted\n"); + + self->test_cgroup1 = cg_name(self->cgroup_root, "so_peercgroupid_cg1"); + ASSERT_NE(NULL, self->test_cgroup1); + + self->test_cgroup2 = cg_name(self->cgroup_root, "so_peercgroupid_cg2"); + ASSERT_NE(NULL, self->test_cgroup2); + + err = cg_create(self->test_cgroup1); + ASSERT_EQ(0, err); + + err = cg_create(self->test_cgroup2); + ASSERT_EQ(0, err); + + test_cgroup1_id = cg_get_id(self->test_cgroup1); + ASSERT_LT(0, test_cgroup1_id); + + test_cgroup2_id = cg_get_id(self->test_cgroup2); + ASSERT_LT(0, test_cgroup2_id); + + /* enter test_cgroup1 before allocating a socket */ + err = cg_enter_current(self->test_cgroup1); + ASSERT_EQ(0, err); + + self->server = socket(AF_UNIX, variant->type, 0); + ASSERT_NE(-1, self->server); + + /* enter test_cgroup2 after allocating a socket */ + err = cg_enter_current(self->test_cgroup2); + ASSERT_EQ(0, err); + + fill_sockaddr(&self->server_addr, variant->abstract); + + err = bind(self->server, (struct sockaddr *)&self->server_addr.listen_addr, self->server_addr.addrlen); + ASSERT_EQ(0, err); + + if (variant->type != SOCK_DGRAM) { + err = listen(self->server, 1); + ASSERT_EQ(0, err); + } + + err = socketpair(AF_UNIX, SOCK_DGRAM | SOCK_CLOEXEC, 0, self->sync_sk); + EXPECT_EQ(err, 0); + + self->client_pid = fork(); + ASSERT_NE(-1, self->client_pid); + if (self->client_pid == 0) { + close(self->server); + close(self->sync_sk[0]); + client(self, variant); + exit(0); + } + close(self->sync_sk[1]); + + if (variant->type != SOCK_DGRAM) { + pfd = accept(self->server, NULL, NULL); + ASSERT_NE(-1, pfd); + } else { + pfd = self->server; + } + + /* wait until the child arrives at checkpoint */ + read(self->sync_sk[0], &state, sizeof(state)); + ASSERT_EQ(state, 'R'); + + write(self->sync_sk[0], &test_cgroup1_id, sizeof(uint64_t)); + write(self->sync_sk[0], &test_cgroup2_id, sizeof(uint64_t)); + + close(pfd); + waitpid(self->client_pid, &child_status, 0); + ASSERT_EQ(0, WIFEXITED(child_status) ? WEXITSTATUS(child_status) : 1); +} + +TEST_HARNESS_MAIN