From patchwork Tue Dec 12 10:17:28 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jiri Pirko X-Patchwork-Id: 13488872 X-Patchwork-Delegate: kuba@kernel.org Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=resnulli-us.20230601.gappssmtp.com header.i=@resnulli-us.20230601.gappssmtp.com header.b="CiBren3R" Received: from mail-ej1-x631.google.com (mail-ej1-x631.google.com [IPv6:2a00:1450:4864:20::631]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 16B6F95 for ; Tue, 12 Dec 2023 02:17:41 -0800 (PST) Received: by mail-ej1-x631.google.com with SMTP id a640c23a62f3a-a1c7d8f89a5so726690466b.2 for ; Tue, 12 Dec 2023 02:17:41 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=resnulli-us.20230601.gappssmtp.com; s=20230601; t=1702376259; x=1702981059; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=33J8BP2HLxzySsghbviJPGk4M7islA6m5mnPAfoHhcc=; b=CiBren3RGg4mshnxl+1u9smFN9yfniEWAN979GHLAgbm9HrPsRRa71h0Si+dUpk+2p 8JKDcZ42lt2k8lv/0bHEXjLTCUJn8X5suiLT+E7R1v/Ow/1oIEEmr87eRm77TX9/L75P gqJwGtu1+6OQG2yB2fG6FVBFZHSJ6+z63lIZUUZpafKuCt3TTgkhz65YZkz7zyKeEJ64 S/tvOIjyuRZfvoHeK657/4hmjFBBW7TE/HShAJESsFBjMhtaQ9AFwAJoVhkoLOJ+/zx0 GQMf6DlpQ3pOulH/MkUt7XOW7/zOMzMkrMqTpvwBCrfn1zG84sMu157f8tkt/lC/T1Qq NC3w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1702376259; x=1702981059; 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=33J8BP2HLxzySsghbviJPGk4M7islA6m5mnPAfoHhcc=; b=f/kE8Ce1ksOX+k8THMEQ86k175x54WhFTAwPiWSCsAve1YfSbWVxYFUmlktngRcLep yeu2cCMHYAtosQlSAFkUtbfNZ4TjO6dZAR8cO5/9NfMbDtWiy9iv12gq6vdWvbUclSfe LTzANdW535B36gVFgzulGF+Rzq4x1BkcQt3B3cn6iXIR8geKMUWSwHKMA9vHm1x3iNce Ja0pG2cIjks6HUPdUkYItx4n63e938Dc6bi15Yauj9lsQjNzOtGGpxslAiNedz7DRZQy ZsUQcMpH1MQZ7KSMMqvIXXjTz2AdNAB+ck5OJmHB9VC6XQdJU5mHpjKMJreduaXpE6eP dVTA== X-Gm-Message-State: AOJu0Yzk/kQbF2m8UpPaUheh9wFE3Q0tPrj4jKP1LegClAhO7vtIf3cV xf2ZUsK6vO06eX4GCJr8r2wByT+W2LJl4OXaCLo= X-Google-Smtp-Source: AGHT+IHko3KlqmwsuEfbz7gKz1U457yfEDEvJ5nHQ7y1DP7dk6hDyL0y29sVOs4gCOkTfre5RzdYKg== X-Received: by 2002:a17:906:dfd0:b0:a19:a1ba:da41 with SMTP id jt16-20020a170906dfd000b00a19a1bada41mr3091607ejc.104.1702376259542; Tue, 12 Dec 2023 02:17:39 -0800 (PST) Received: from localhost (host-213-179-129-39.customer.m-online.net. [213.179.129.39]) by smtp.gmail.com with ESMTPSA id cw15-20020a170907160f00b00a1937153bddsm5976834ejd.20.2023.12.12.02.17.38 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 12 Dec 2023 02:17:39 -0800 (PST) From: Jiri Pirko To: netdev@vger.kernel.org Cc: kuba@kernel.org, pabeni@redhat.com, davem@davemloft.net, edumazet@google.com, jacob.e.keller@intel.com, jhs@mojatatu.com, johannes@sipsolutions.net, andriy.shevchenko@linux.intel.com, amritha.nambiar@intel.com, sdf@google.com, horms@kernel.org, przemyslaw.kitszel@intel.com Subject: [patch net-next v6 1/9] devlink: use devl_is_registered() helper instead xa_get_mark() Date: Tue, 12 Dec 2023 11:17:28 +0100 Message-ID: <20231212101736.1112671-2-jiri@resnulli.us> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20231212101736.1112671-1-jiri@resnulli.us> References: <20231212101736.1112671-1-jiri@resnulli.us> 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 From: Jiri Pirko Instead of checking the xarray mark directly using xa_get_mark() helper use devl_is_registered() helper which wraps it up. Note that there are couple more users of xa_get_mark() left which are going to be handled by the next patch. Signed-off-by: Jiri Pirko --- net/devlink/dev.c | 4 ++-- net/devlink/rate.c | 2 +- net/devlink/trap.c | 9 ++++++--- 3 files changed, 9 insertions(+), 6 deletions(-) diff --git a/net/devlink/dev.c b/net/devlink/dev.c index 918a0395b03e..3fe93c8a9fe2 100644 --- a/net/devlink/dev.c +++ b/net/devlink/dev.c @@ -202,7 +202,7 @@ static void devlink_notify(struct devlink *devlink, enum devlink_command cmd) int err; WARN_ON(cmd != DEVLINK_CMD_NEW && cmd != DEVLINK_CMD_DEL); - WARN_ON(!xa_get_mark(&devlinks, devlink->index, DEVLINK_REGISTERED)); + WARN_ON(!devl_is_registered(devlink)); msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); if (!msg) @@ -999,7 +999,7 @@ static void __devlink_flash_update_notify(struct devlink *devlink, cmd != DEVLINK_CMD_FLASH_UPDATE_END && cmd != DEVLINK_CMD_FLASH_UPDATE_STATUS); - if (!xa_get_mark(&devlinks, devlink->index, DEVLINK_REGISTERED)) + if (!devl_is_registered(devlink)) return; msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); diff --git a/net/devlink/rate.c b/net/devlink/rate.c index 94b289b93ff2..e2190cf22beb 100644 --- a/net/devlink/rate.c +++ b/net/devlink/rate.c @@ -146,7 +146,7 @@ static void devlink_rate_notify(struct devlink_rate *devlink_rate, WARN_ON(cmd != DEVLINK_CMD_RATE_NEW && cmd != DEVLINK_CMD_RATE_DEL); - if (!xa_get_mark(&devlinks, devlink->index, DEVLINK_REGISTERED)) + if (!devl_is_registered(devlink)) return; msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); diff --git a/net/devlink/trap.c b/net/devlink/trap.c index c26313e7ca08..908085e2c990 100644 --- a/net/devlink/trap.c +++ b/net/devlink/trap.c @@ -1173,7 +1173,8 @@ devlink_trap_group_notify(struct devlink *devlink, WARN_ON_ONCE(cmd != DEVLINK_CMD_TRAP_GROUP_NEW && cmd != DEVLINK_CMD_TRAP_GROUP_DEL); - if (!xa_get_mark(&devlinks, devlink->index, DEVLINK_REGISTERED)) + + if (!devl_is_registered(devlink)) return; msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); @@ -1234,7 +1235,8 @@ static void devlink_trap_notify(struct devlink *devlink, WARN_ON_ONCE(cmd != DEVLINK_CMD_TRAP_NEW && cmd != DEVLINK_CMD_TRAP_DEL); - if (!xa_get_mark(&devlinks, devlink->index, DEVLINK_REGISTERED)) + + if (!devl_is_registered(devlink)) return; msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); @@ -1710,7 +1712,8 @@ devlink_trap_policer_notify(struct devlink *devlink, WARN_ON_ONCE(cmd != DEVLINK_CMD_TRAP_POLICER_NEW && cmd != DEVLINK_CMD_TRAP_POLICER_DEL); - if (!xa_get_mark(&devlinks, devlink->index, DEVLINK_REGISTERED)) + + if (!devl_is_registered(devlink)) return; msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); From patchwork Tue Dec 12 10:17:29 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jiri Pirko X-Patchwork-Id: 13488874 X-Patchwork-Delegate: kuba@kernel.org Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=resnulli-us.20230601.gappssmtp.com header.i=@resnulli-us.20230601.gappssmtp.com header.b="QqRTRfQx" Received: from mail-ed1-x52a.google.com (mail-ed1-x52a.google.com [IPv6:2a00:1450:4864:20::52a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C713A9C for ; Tue, 12 Dec 2023 02:17:42 -0800 (PST) Received: by mail-ed1-x52a.google.com with SMTP id 4fb4d7f45d1cf-54cde11d0f4so7638802a12.2 for ; Tue, 12 Dec 2023 02:17:42 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=resnulli-us.20230601.gappssmtp.com; s=20230601; t=1702376261; x=1702981061; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=kPya/tj37w3+Pavkqq2uQ8tsfkZUvXPv2JD2RPZVhGQ=; b=QqRTRfQxRMjw3XMKqbTXmC5XaU6C6JoCsUa6x8mM6Aa8W+CSDyky2aQkAGraEFx04F q7Hkp10cQ0siCpurg21kHiwuVCPNrXNlgX1ivcivtREYugyMQ1ljyFaAT6oBmXcncAM4 28y5VxcC3GVcB+Gu6bpGCuoS3Oj1l915fOak4uq6alHtO0i7cEKEMckQnIHsse2ztSQy Ks1ZnJgPkw35j9nIajwDUCQp2S5GJvyHsaEBT9QoDWRxlQhCSuera0F/n3jxFtnOB9oj /SKZjT5dsvSRMqv0njt/I31LHmyNN/y56pFpKPQ9lh+JhLX5g5cO5NVG0LarHkzv9hzr fYbg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1702376261; x=1702981061; 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=kPya/tj37w3+Pavkqq2uQ8tsfkZUvXPv2JD2RPZVhGQ=; b=wyplI9dPa6TUg7Tg8Q7DUk8HHz4VFcQ3U/l74Csi8CFd9Et4VPjvgHB3shQ4ivXNKa TqHUcvkq0ng+nKrOpgCVF8fC7dlFYdCdywDiK8iiyxJ0GP6jB1CVjhXilpN5kOTHnvh8 miSSA4QWB7l1bp43HUpF44K5/F0578RXEbpu/LWasmkcU06SOJV4vIYcVhjt/HYQTWqc z+b4s+vOnRRRi7SStdLw7GfIWj3zSzYmoys9f2s50cAahSA8MRD+aO47Y9LN39Ca+Cq5 foeHt5Rl5YfBvP0+LAHQTVgULxYF4Za4kWm7ORdG8bC7+2dZB5ADV2TFWGTijk43/5an Dd0A== X-Gm-Message-State: AOJu0YzcGmTARPOhKAzJhetr7JNVttLgCoMaqBPkdTg/+n7Blku4SNWf S9nWErnvis8x9R8PKQmxbmR1npi++QLF9B4lQgk= X-Google-Smtp-Source: AGHT+IF63VD2bnYi8sMWPaOdqBrn2k/UHFLcq2LN+BlOdid8DEUi+hR5PUwN8wJ7R2AT0Th4yMUD0A== X-Received: by 2002:a50:ba86:0:b0:550:5182:8075 with SMTP id x6-20020a50ba86000000b0055051828075mr3053479ede.27.1702376261130; Tue, 12 Dec 2023 02:17:41 -0800 (PST) Received: from localhost (host-213-179-129-39.customer.m-online.net. [213.179.129.39]) by smtp.gmail.com with ESMTPSA id k13-20020a50cb8d000000b0054cc7a4dc4csm4447793edi.13.2023.12.12.02.17.40 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 12 Dec 2023 02:17:40 -0800 (PST) From: Jiri Pirko To: netdev@vger.kernel.org Cc: kuba@kernel.org, pabeni@redhat.com, davem@davemloft.net, edumazet@google.com, jacob.e.keller@intel.com, jhs@mojatatu.com, johannes@sipsolutions.net, andriy.shevchenko@linux.intel.com, amritha.nambiar@intel.com, sdf@google.com, horms@kernel.org, przemyslaw.kitszel@intel.com Subject: [patch net-next v6 2/9] devlink: introduce __devl_is_registered() helper and use it instead of xa_get_mark() Date: Tue, 12 Dec 2023 11:17:29 +0100 Message-ID: <20231212101736.1112671-3-jiri@resnulli.us> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20231212101736.1112671-1-jiri@resnulli.us> References: <20231212101736.1112671-1-jiri@resnulli.us> 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 From: Jiri Pirko Introduce __devl_is_registered() which does not assert on devlink instance lock and use it in notifications which may be called without devlink instance lock held. Signed-off-by: Jiri Pirko --- net/devlink/devl_internal.h | 7 ++++++- net/devlink/linecard.c | 2 +- net/devlink/port.c | 2 +- net/devlink/region.c | 3 ++- 4 files changed, 10 insertions(+), 4 deletions(-) diff --git a/net/devlink/devl_internal.h b/net/devlink/devl_internal.h index 5ea2e2012e93..59ae4761d10a 100644 --- a/net/devlink/devl_internal.h +++ b/net/devlink/devl_internal.h @@ -91,10 +91,15 @@ extern struct genl_family devlink_nl_family; struct devlink *devlinks_xa_find_get(struct net *net, unsigned long *indexp); +static inline bool __devl_is_registered(struct devlink *devlink) +{ + return xa_get_mark(&devlinks, devlink->index, DEVLINK_REGISTERED); +} + static inline bool devl_is_registered(struct devlink *devlink) { devl_assert_locked(devlink); - return xa_get_mark(&devlinks, devlink->index, DEVLINK_REGISTERED); + return __devl_is_registered(devlink); } static inline void devl_dev_lock(struct devlink *devlink, bool dev_lock) diff --git a/net/devlink/linecard.c b/net/devlink/linecard.c index 2f1c317b64cd..9d080ac1734b 100644 --- a/net/devlink/linecard.c +++ b/net/devlink/linecard.c @@ -136,7 +136,7 @@ static void devlink_linecard_notify(struct devlink_linecard *linecard, WARN_ON(cmd != DEVLINK_CMD_LINECARD_NEW && cmd != DEVLINK_CMD_LINECARD_DEL); - if (!xa_get_mark(&devlinks, devlink->index, DEVLINK_REGISTERED)) + if (!__devl_is_registered(devlink)) return; msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); diff --git a/net/devlink/port.c b/net/devlink/port.c index 7634f187fa50..f229a8699214 100644 --- a/net/devlink/port.c +++ b/net/devlink/port.c @@ -512,7 +512,7 @@ static void devlink_port_notify(struct devlink_port *devlink_port, WARN_ON(cmd != DEVLINK_CMD_PORT_NEW && cmd != DEVLINK_CMD_PORT_DEL); - if (!xa_get_mark(&devlinks, devlink->index, DEVLINK_REGISTERED)) + if (!__devl_is_registered(devlink)) return; msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); diff --git a/net/devlink/region.c b/net/devlink/region.c index e3bab458db94..b65181aa269a 100644 --- a/net/devlink/region.c +++ b/net/devlink/region.c @@ -234,7 +234,8 @@ static void devlink_nl_region_notify(struct devlink_region *region, struct sk_buff *msg; WARN_ON(cmd != DEVLINK_CMD_REGION_NEW && cmd != DEVLINK_CMD_REGION_DEL); - if (!xa_get_mark(&devlinks, devlink->index, DEVLINK_REGISTERED)) + + if (!__devl_is_registered(devlink)) return; msg = devlink_nl_region_notify_build(region, snapshot, cmd, 0, 0); From patchwork Tue Dec 12 10:17:30 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jiri Pirko X-Patchwork-Id: 13488875 X-Patchwork-Delegate: kuba@kernel.org Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=resnulli-us.20230601.gappssmtp.com header.i=@resnulli-us.20230601.gappssmtp.com header.b="PuuAeGLr" Received: from mail-ej1-x62a.google.com (mail-ej1-x62a.google.com [IPv6:2a00:1450:4864:20::62a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5F667A6 for ; Tue, 12 Dec 2023 02:17:44 -0800 (PST) Received: by mail-ej1-x62a.google.com with SMTP id a640c23a62f3a-a1f8510883aso386233466b.3 for ; Tue, 12 Dec 2023 02:17:44 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=resnulli-us.20230601.gappssmtp.com; s=20230601; t=1702376263; x=1702981063; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=bbWsBPpBXJN/mSeDiH7uhOyWgGTchnVsa58K3LTnlgY=; b=PuuAeGLrJNvZ/FT/QP6XH1it/KUW28fDCqlw7OGRPyyZeW2/KX2QGNcgrwfSal1AV1 FI2RFJ8JoPSUt1rrutzkXTVZBQ2A2UjR30WNgo3YB6HFp8gG/7lQohCSYLb+51skwFtF +0Ru8f2wk2QOj4eeJ/ht6rJcHWVYx3iwr+MndE5Da1SM3aitbUMGBBdyJHnNRKGrIVz8 V01AQplg+ae1e/9+oY8Ha1i5l7fZ4eqobdRzkwBkq950Nh6C8db9qhuiE2Z3MjGneH2N j6Oq6tF6xTMQlTcRIIY4BWo5yG3GgfW4/2vfppkWpm4KSaw6hGnjguflyHKzgpLnflJH On0w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1702376263; x=1702981063; 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=bbWsBPpBXJN/mSeDiH7uhOyWgGTchnVsa58K3LTnlgY=; b=ApQBMbJic1yeFvp2QorbMb9ezMZUrD7jSHJUiwgsxPym2MYWWQ0U3dLPrkZk8lJ1V+ XTx7xtDp6W9ARDrt1axCV26FOPw4E7IaJ7zdAMlpIDKMiek/lfbkJM/OMfU6ooqBxk1Z COutWHGxl9f4UlXbJKZa6IIxWKdrEpYvyCBIjKHSAnSJNT83K5mTVBD79t+xLJ7zJXE5 OfXTedI2R6Mn+gjsxjFPub2iNkSSw4Yr0bVRqtAKMk97SukvbszlsqIdlNmVpTkuGiSK r0kRBwjq/cqiesZs1nZsXmfsNuJBfWHqee3wVg5AtmGc3Uw/xsI4cqEIt1xeplqzQQVU Mh3w== X-Gm-Message-State: AOJu0Yy0XTsFb7ucNIuBFli1zAILKSMvUApfy0fKDdmBGduW6rLqsaJi Q4ZYk/SAsDYWxEeFxeqwyIx+J/Gf5hmZ6XOQx3E= X-Google-Smtp-Source: AGHT+IG9Tjh0NsdJjSd3ziOkEHQ5sAilLBVk2qZRb2oIxq4kcmW/OzJ7kRJ7o2YMSAEj7EG+7VHatw== X-Received: by 2002:a17:907:31cd:b0:a1c:9f65:a414 with SMTP id xf13-20020a17090731cd00b00a1c9f65a414mr1772737ejb.152.1702376262801; Tue, 12 Dec 2023 02:17:42 -0800 (PST) Received: from localhost (host-213-179-129-39.customer.m-online.net. [213.179.129.39]) by smtp.gmail.com with ESMTPSA id tn9-20020a170907c40900b00a1f7ab65d3fsm4949107ejc.131.2023.12.12.02.17.41 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 12 Dec 2023 02:17:42 -0800 (PST) From: Jiri Pirko To: netdev@vger.kernel.org Cc: kuba@kernel.org, pabeni@redhat.com, davem@davemloft.net, edumazet@google.com, jacob.e.keller@intel.com, jhs@mojatatu.com, johannes@sipsolutions.net, andriy.shevchenko@linux.intel.com, amritha.nambiar@intel.com, sdf@google.com, horms@kernel.org, przemyslaw.kitszel@intel.com Subject: [patch net-next v6 3/9] devlink: send notifications only if there are listeners Date: Tue, 12 Dec 2023 11:17:30 +0100 Message-ID: <20231212101736.1112671-4-jiri@resnulli.us> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20231212101736.1112671-1-jiri@resnulli.us> References: <20231212101736.1112671-1-jiri@resnulli.us> 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 From: Jiri Pirko Introduce devlink_nl_notify_need() helper and using it to check at the beginning of notification functions to avoid overhead of composing notification messages in case nobody listens. Signed-off-by: Jiri Pirko --- v4->v5: - swapped order in test in param.c --- net/devlink/dev.c | 5 ++++- net/devlink/devl_internal.h | 6 ++++++ net/devlink/health.c | 3 +++ net/devlink/linecard.c | 2 +- net/devlink/param.c | 2 +- net/devlink/port.c | 2 +- net/devlink/rate.c | 2 +- net/devlink/region.c | 2 +- net/devlink/trap.c | 6 +++--- 9 files changed, 21 insertions(+), 9 deletions(-) diff --git a/net/devlink/dev.c b/net/devlink/dev.c index 3fe93c8a9fe2..63fe3e02c928 100644 --- a/net/devlink/dev.c +++ b/net/devlink/dev.c @@ -204,6 +204,9 @@ static void devlink_notify(struct devlink *devlink, enum devlink_command cmd) WARN_ON(cmd != DEVLINK_CMD_NEW && cmd != DEVLINK_CMD_DEL); WARN_ON(!devl_is_registered(devlink)); + if (!devlink_nl_notify_need(devlink)) + return; + msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); if (!msg) return; @@ -999,7 +1002,7 @@ static void __devlink_flash_update_notify(struct devlink *devlink, cmd != DEVLINK_CMD_FLASH_UPDATE_END && cmd != DEVLINK_CMD_FLASH_UPDATE_STATUS); - if (!devl_is_registered(devlink)) + if (!devl_is_registered(devlink) || !devlink_nl_notify_need(devlink)) return; msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); diff --git a/net/devlink/devl_internal.h b/net/devlink/devl_internal.h index 59ae4761d10a..510990de094e 100644 --- a/net/devlink/devl_internal.h +++ b/net/devlink/devl_internal.h @@ -185,6 +185,12 @@ int devlink_nl_put_nested_handle(struct sk_buff *msg, struct net *net, struct devlink *devlink, int attrtype); int devlink_nl_msg_reply_and_new(struct sk_buff **msg, struct genl_info *info); +static inline bool devlink_nl_notify_need(struct devlink *devlink) +{ + return genl_has_listeners(&devlink_nl_family, devlink_net(devlink), + DEVLINK_MCGRP_CONFIG); +} + /* Notify */ void devlink_notify_register(struct devlink *devlink); void devlink_notify_unregister(struct devlink *devlink); diff --git a/net/devlink/health.c b/net/devlink/health.c index 71ae121dc739..0795dcf22ca8 100644 --- a/net/devlink/health.c +++ b/net/devlink/health.c @@ -496,6 +496,9 @@ static void devlink_recover_notify(struct devlink_health_reporter *reporter, WARN_ON(cmd != DEVLINK_CMD_HEALTH_REPORTER_RECOVER); ASSERT_DEVLINK_REGISTERED(devlink); + if (!devlink_nl_notify_need(devlink)) + return; + msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); if (!msg) return; diff --git a/net/devlink/linecard.c b/net/devlink/linecard.c index 9d080ac1734b..45b36975ee6f 100644 --- a/net/devlink/linecard.c +++ b/net/devlink/linecard.c @@ -136,7 +136,7 @@ static void devlink_linecard_notify(struct devlink_linecard *linecard, WARN_ON(cmd != DEVLINK_CMD_LINECARD_NEW && cmd != DEVLINK_CMD_LINECARD_DEL); - if (!__devl_is_registered(devlink)) + if (!__devl_is_registered(devlink) || !devlink_nl_notify_need(devlink)) return; msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); diff --git a/net/devlink/param.c b/net/devlink/param.c index d74df09311a9..7516b524ffb7 100644 --- a/net/devlink/param.c +++ b/net/devlink/param.c @@ -343,7 +343,7 @@ static void devlink_param_notify(struct devlink *devlink, * will replay the notifications if the params are added/removed * outside of the lifetime of the instance. */ - if (!devl_is_registered(devlink)) + if (!devl_is_registered(devlink) || !devlink_nl_notify_need(devlink)) return; msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); diff --git a/net/devlink/port.c b/net/devlink/port.c index f229a8699214..32f4d0331e63 100644 --- a/net/devlink/port.c +++ b/net/devlink/port.c @@ -512,7 +512,7 @@ static void devlink_port_notify(struct devlink_port *devlink_port, WARN_ON(cmd != DEVLINK_CMD_PORT_NEW && cmd != DEVLINK_CMD_PORT_DEL); - if (!__devl_is_registered(devlink)) + if (!__devl_is_registered(devlink) || !devlink_nl_notify_need(devlink)) return; msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); diff --git a/net/devlink/rate.c b/net/devlink/rate.c index e2190cf22beb..0371a2dd3e0a 100644 --- a/net/devlink/rate.c +++ b/net/devlink/rate.c @@ -146,7 +146,7 @@ static void devlink_rate_notify(struct devlink_rate *devlink_rate, WARN_ON(cmd != DEVLINK_CMD_RATE_NEW && cmd != DEVLINK_CMD_RATE_DEL); - if (!devl_is_registered(devlink)) + if (!devl_is_registered(devlink) || !devlink_nl_notify_need(devlink)) return; msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); diff --git a/net/devlink/region.c b/net/devlink/region.c index b65181aa269a..bf61312f64bd 100644 --- a/net/devlink/region.c +++ b/net/devlink/region.c @@ -235,7 +235,7 @@ static void devlink_nl_region_notify(struct devlink_region *region, WARN_ON(cmd != DEVLINK_CMD_REGION_NEW && cmd != DEVLINK_CMD_REGION_DEL); - if (!__devl_is_registered(devlink)) + if (!__devl_is_registered(devlink) || !devlink_nl_notify_need(devlink)) return; msg = devlink_nl_region_notify_build(region, snapshot, cmd, 0, 0); diff --git a/net/devlink/trap.c b/net/devlink/trap.c index 908085e2c990..3ca1ca7e2e64 100644 --- a/net/devlink/trap.c +++ b/net/devlink/trap.c @@ -1174,7 +1174,7 @@ devlink_trap_group_notify(struct devlink *devlink, WARN_ON_ONCE(cmd != DEVLINK_CMD_TRAP_GROUP_NEW && cmd != DEVLINK_CMD_TRAP_GROUP_DEL); - if (!devl_is_registered(devlink)) + if (!devl_is_registered(devlink) || !devlink_nl_notify_need(devlink)) return; msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); @@ -1236,7 +1236,7 @@ static void devlink_trap_notify(struct devlink *devlink, WARN_ON_ONCE(cmd != DEVLINK_CMD_TRAP_NEW && cmd != DEVLINK_CMD_TRAP_DEL); - if (!devl_is_registered(devlink)) + if (!devl_is_registered(devlink) || !devlink_nl_notify_need(devlink)) return; msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); @@ -1713,7 +1713,7 @@ devlink_trap_policer_notify(struct devlink *devlink, WARN_ON_ONCE(cmd != DEVLINK_CMD_TRAP_POLICER_NEW && cmd != DEVLINK_CMD_TRAP_POLICER_DEL); - if (!devl_is_registered(devlink)) + if (!devl_is_registered(devlink) || !devlink_nl_notify_need(devlink)) return; msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); From patchwork Tue Dec 12 10:17:31 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jiri Pirko X-Patchwork-Id: 13488876 X-Patchwork-Delegate: kuba@kernel.org Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=resnulli-us.20230601.gappssmtp.com header.i=@resnulli-us.20230601.gappssmtp.com header.b="hRvc9Loz" Received: from mail-ed1-x535.google.com (mail-ed1-x535.google.com [IPv6:2a00:1450:4864:20::535]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 27F5F92 for ; Tue, 12 Dec 2023 02:17:46 -0800 (PST) Received: by mail-ed1-x535.google.com with SMTP id 4fb4d7f45d1cf-54da61eb366so7269214a12.3 for ; Tue, 12 Dec 2023 02:17:46 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=resnulli-us.20230601.gappssmtp.com; s=20230601; t=1702376264; x=1702981064; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=SHh3MZJVlWMre4LtMHfrndshjQd5a9TSrM1m3pB1Etc=; b=hRvc9LozXJk/NQe4yClQTK09EQ2DFvYPzPrhna9G0x6D2wQztPMM0Dnej1NvM2qCaY Q729u7jTLQF/EOK+tx+uPdD2bzVEkx65jjvpZ1YL7DLVPXlANrxd5Y/Dm5NRdeti2zJd 4h+FbRJdMwklgesyq4mmB4KVdhaky9NQjCFrsgZoZZ1PcEtZ8hizGiHhahKl56El567P I/ULYN75Ez/ROMRdBmoS1vLpOCF3mtr2QV96nYE1J8Xe6LA3PDeU/5clhp76vTmnSn8X uCP9K+1CcuhQL8lfRXmguIdavcrOdHjS8fB7qWnSpNFxv8QfAriNoSdkJAsXd+Em7kH4 Ylbw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1702376264; x=1702981064; 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=SHh3MZJVlWMre4LtMHfrndshjQd5a9TSrM1m3pB1Etc=; b=PSWVEPG9uOCubFZLtP3QgzdH09MbM/oTMUWn9TQ1yKaxKh6w+YyC1nF+kjXXEHU3Kt P3/Tti3OkYOkApF0pz7JBWVbZ8k0V0roQ3NRQVziUgfICCPBK5aPIGnnlRJs425E/UCo pftbGTHzu1LJJr9DsPGmSzrzDavNREBq5Ea/VAqSfX9SibuYinK+/Vvzg9t67+Fcvg8m MBK4pzGipFuYVmGA4va/BBzVUEDpLB3pbFB5Qr4CC4eEF40WVZmbGu9rNkUppDxPbPqj xlTGU/xQjwfkVuUMX6Sd5AM2oWPR65IRjU9/VCMLKyXk9VO8ptC+umCMwcTtstfPqoj6 9xGw== X-Gm-Message-State: AOJu0Yzc9ePlcqQf8lUrmBG06gP52iXhf8cXO/Cbd8aRR7TWhvmXRE+6 ztRNugyDq9puWTtHCB8EK5CVmsSC6cyktfrpJPc= X-Google-Smtp-Source: AGHT+IHBlOKVZl4sAakvl/di+IFtvDxd52wM5kV9Oh0v3CLUbbW03krdIJwQob8pvMdaX4/Y9SMLXA== X-Received: by 2002:a50:c057:0:b0:54b:27e7:f965 with SMTP id u23-20020a50c057000000b0054b27e7f965mr3877363edd.19.1702376264677; Tue, 12 Dec 2023 02:17:44 -0800 (PST) Received: from localhost (host-213-179-129-39.customer.m-online.net. [213.179.129.39]) by smtp.gmail.com with ESMTPSA id f27-20020a50d55b000000b00551bef45cb0sm150938edj.24.2023.12.12.02.17.43 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 12 Dec 2023 02:17:44 -0800 (PST) From: Jiri Pirko To: netdev@vger.kernel.org Cc: kuba@kernel.org, pabeni@redhat.com, davem@davemloft.net, edumazet@google.com, jacob.e.keller@intel.com, jhs@mojatatu.com, johannes@sipsolutions.net, andriy.shevchenko@linux.intel.com, amritha.nambiar@intel.com, sdf@google.com, horms@kernel.org, przemyslaw.kitszel@intel.com Subject: [patch net-next v6 4/9] devlink: introduce a helper for netlink multicast send Date: Tue, 12 Dec 2023 11:17:31 +0100 Message-ID: <20231212101736.1112671-5-jiri@resnulli.us> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20231212101736.1112671-1-jiri@resnulli.us> References: <20231212101736.1112671-1-jiri@resnulli.us> 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 From: Jiri Pirko Introduce a helper devlink_nl_notify_send() so each object notification function does not have to call genlmsg_multicast_netns() with the same arguments. Signed-off-by: Jiri Pirko --- net/devlink/dev.c | 6 ++---- net/devlink/devl_internal.h | 7 +++++++ net/devlink/health.c | 3 +-- net/devlink/linecard.c | 3 +-- net/devlink/param.c | 3 +-- net/devlink/port.c | 3 +-- net/devlink/rate.c | 3 +-- net/devlink/region.c | 3 +-- net/devlink/trap.c | 9 +++------ 9 files changed, 18 insertions(+), 22 deletions(-) diff --git a/net/devlink/dev.c b/net/devlink/dev.c index 63fe3e02c928..19dbf540748a 100644 --- a/net/devlink/dev.c +++ b/net/devlink/dev.c @@ -217,8 +217,7 @@ static void devlink_notify(struct devlink *devlink, enum devlink_command cmd) return; } - genlmsg_multicast_netns(&devlink_nl_family, devlink_net(devlink), - msg, 0, DEVLINK_MCGRP_CONFIG, GFP_KERNEL); + devlink_nl_notify_send(devlink, msg); } int devlink_nl_get_doit(struct sk_buff *skb, struct genl_info *info) @@ -1013,8 +1012,7 @@ static void __devlink_flash_update_notify(struct devlink *devlink, if (err) goto out_free_msg; - genlmsg_multicast_netns(&devlink_nl_family, devlink_net(devlink), - msg, 0, DEVLINK_MCGRP_CONFIG, GFP_KERNEL); + devlink_nl_notify_send(devlink, msg); return; out_free_msg: diff --git a/net/devlink/devl_internal.h b/net/devlink/devl_internal.h index 510990de094e..84dc9628d3f2 100644 --- a/net/devlink/devl_internal.h +++ b/net/devlink/devl_internal.h @@ -191,6 +191,13 @@ static inline bool devlink_nl_notify_need(struct devlink *devlink) DEVLINK_MCGRP_CONFIG); } +static inline void devlink_nl_notify_send(struct devlink *devlink, + struct sk_buff *msg) +{ + genlmsg_multicast_netns(&devlink_nl_family, devlink_net(devlink), + msg, 0, DEVLINK_MCGRP_CONFIG, GFP_KERNEL); +} + /* Notify */ void devlink_notify_register(struct devlink *devlink); void devlink_notify_unregister(struct devlink *devlink); diff --git a/net/devlink/health.c b/net/devlink/health.c index 0795dcf22ca8..1d59ec0202f6 100644 --- a/net/devlink/health.c +++ b/net/devlink/health.c @@ -509,8 +509,7 @@ static void devlink_recover_notify(struct devlink_health_reporter *reporter, return; } - genlmsg_multicast_netns(&devlink_nl_family, devlink_net(devlink), msg, - 0, DEVLINK_MCGRP_CONFIG, GFP_KERNEL); + devlink_nl_notify_send(devlink, msg); } void diff --git a/net/devlink/linecard.c b/net/devlink/linecard.c index 45b36975ee6f..67f70a621d27 100644 --- a/net/devlink/linecard.c +++ b/net/devlink/linecard.c @@ -150,8 +150,7 @@ static void devlink_linecard_notify(struct devlink_linecard *linecard, return; } - genlmsg_multicast_netns(&devlink_nl_family, devlink_net(devlink), - msg, 0, DEVLINK_MCGRP_CONFIG, GFP_KERNEL); + devlink_nl_notify_send(devlink, msg); } void devlink_linecards_notify_register(struct devlink *devlink) diff --git a/net/devlink/param.c b/net/devlink/param.c index 7516b524ffb7..22bc3b500518 100644 --- a/net/devlink/param.c +++ b/net/devlink/param.c @@ -356,8 +356,7 @@ static void devlink_param_notify(struct devlink *devlink, return; } - genlmsg_multicast_netns(&devlink_nl_family, devlink_net(devlink), - msg, 0, DEVLINK_MCGRP_CONFIG, GFP_KERNEL); + devlink_nl_notify_send(devlink, msg); } static void devlink_params_notify(struct devlink *devlink, diff --git a/net/devlink/port.c b/net/devlink/port.c index 32f4d0331e63..758df3000a1b 100644 --- a/net/devlink/port.c +++ b/net/devlink/port.c @@ -525,8 +525,7 @@ static void devlink_port_notify(struct devlink_port *devlink_port, return; } - genlmsg_multicast_netns(&devlink_nl_family, devlink_net(devlink), msg, - 0, DEVLINK_MCGRP_CONFIG, GFP_KERNEL); + devlink_nl_notify_send(devlink, msg); } static void devlink_ports_notify(struct devlink *devlink, diff --git a/net/devlink/rate.c b/net/devlink/rate.c index 0371a2dd3e0a..7139e67e93ae 100644 --- a/net/devlink/rate.c +++ b/net/devlink/rate.c @@ -159,8 +159,7 @@ static void devlink_rate_notify(struct devlink_rate *devlink_rate, return; } - genlmsg_multicast_netns(&devlink_nl_family, devlink_net(devlink), msg, - 0, DEVLINK_MCGRP_CONFIG, GFP_KERNEL); + devlink_nl_notify_send(devlink, msg); } void devlink_rates_notify_register(struct devlink *devlink) diff --git a/net/devlink/region.c b/net/devlink/region.c index bf61312f64bd..7319127c5913 100644 --- a/net/devlink/region.c +++ b/net/devlink/region.c @@ -242,8 +242,7 @@ static void devlink_nl_region_notify(struct devlink_region *region, if (IS_ERR(msg)) return; - genlmsg_multicast_netns(&devlink_nl_family, devlink_net(devlink), msg, - 0, DEVLINK_MCGRP_CONFIG, GFP_KERNEL); + devlink_nl_notify_send(devlink, msg); } void devlink_regions_notify_register(struct devlink *devlink) diff --git a/net/devlink/trap.c b/net/devlink/trap.c index 3ca1ca7e2e64..5d18c7424df1 100644 --- a/net/devlink/trap.c +++ b/net/devlink/trap.c @@ -1188,8 +1188,7 @@ devlink_trap_group_notify(struct devlink *devlink, return; } - genlmsg_multicast_netns(&devlink_nl_family, devlink_net(devlink), - msg, 0, DEVLINK_MCGRP_CONFIG, GFP_KERNEL); + devlink_nl_notify_send(devlink, msg); } void devlink_trap_groups_notify_register(struct devlink *devlink) @@ -1249,8 +1248,7 @@ static void devlink_trap_notify(struct devlink *devlink, return; } - genlmsg_multicast_netns(&devlink_nl_family, devlink_net(devlink), - msg, 0, DEVLINK_MCGRP_CONFIG, GFP_KERNEL); + devlink_nl_notify_send(devlink, msg); } void devlink_traps_notify_register(struct devlink *devlink) @@ -1727,8 +1725,7 @@ devlink_trap_policer_notify(struct devlink *devlink, return; } - genlmsg_multicast_netns(&devlink_nl_family, devlink_net(devlink), - msg, 0, DEVLINK_MCGRP_CONFIG, GFP_KERNEL); + devlink_nl_notify_send(devlink, msg); } void devlink_trap_policers_notify_register(struct devlink *devlink) From patchwork Tue Dec 12 10:17:32 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jiri Pirko X-Patchwork-Id: 13488878 X-Patchwork-Delegate: kuba@kernel.org Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=resnulli-us.20230601.gappssmtp.com header.i=@resnulli-us.20230601.gappssmtp.com header.b="pRcIP+sp" Received: from mail-ed1-x52b.google.com (mail-ed1-x52b.google.com [IPv6:2a00:1450:4864:20::52b]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D9EE295 for ; Tue, 12 Dec 2023 02:17:47 -0800 (PST) Received: by mail-ed1-x52b.google.com with SMTP id 4fb4d7f45d1cf-5510479806dso3111987a12.0 for ; Tue, 12 Dec 2023 02:17:47 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=resnulli-us.20230601.gappssmtp.com; s=20230601; t=1702376266; x=1702981066; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=GvQLavHZhgBYjO34rYxoIaKhu/qHgcB5JUua6zhju5Q=; b=pRcIP+spTiuAckMce2w9M7mwtfuMA+UmoTGRsOufGLDhZ9mQEaTAvGHHdjRi5nR9w+ Y+jgog/zU8Rf95EX70MrXuAXBKwOqeHt086IDsYlDKQ5TNJ+dV1ATAib+RUl8rbXkk/o r/Hitn7QugHbP4qWB1CGFDopBDMtB+zqnEryZt+l/S+g0SMt2NxY9tQxeGCukz46svjm j7J1QVryDUNH9RenulYIYo/Hsjn+VekNO35qA5rrfstg0HW+bdxWHA9KMIkPnBm3q2eC hb0aiz6Cx818YQNy/WDmRFsKsKgpIGHiYUys2q0kmRaIkUCgFVLmOV8L+wjnfdZDs9im yVtw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1702376266; x=1702981066; 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=GvQLavHZhgBYjO34rYxoIaKhu/qHgcB5JUua6zhju5Q=; b=pcVbZ5cNgirmDtfBqNcTuwEU8cuHQbohmOjCfTNuaQfBXyDzj3R8euGYb2QW1u7hO8 EiXr0ar1aTvztiusc/eU4IxxrQvjVf+6BGMbKeyaS1z5zUC46hdL2Qn0Hpzofz+Y22wJ LYcJS/I6zerjyo94VliWXMBRA6zl6WPrsrW4MFowEfI6TqfjMfa/jbdVhqc0Rv8Jlhxj 4i0d4fArSWSqks3AjP9Tl5TUaZgimUFGhLDn+exq08AI+UIAj2Lg539rmcGlYFlYUrSX ocqMTW1DIEe17Raqid13O1pLn4SsT8Ukvp8ronGQc+tRMkVEgYajJ5fJ2CiIrXPvemUK xNCw== X-Gm-Message-State: AOJu0YyrwdOXXA8Dz++wOkQ50NpjcOQJhbnedxCOQ+iG7Q2pl0cWTtvF fPbeb7KuR6wm3HPpMtTQuNuIvh5tUOpzV6fmZZE= X-Google-Smtp-Source: AGHT+IGXTgnoJsXvzyCsuH0tVVl0HeuKZLCWtiOQsIsHGqxq1ZR/rRIwx0T4G/5UZaXZh8Pv6d3UFQ== X-Received: by 2002:a50:ab53:0:b0:551:bcc5:225 with SMTP id t19-20020a50ab53000000b00551bcc50225mr91888edc.96.1702376266379; Tue, 12 Dec 2023 02:17:46 -0800 (PST) Received: from localhost (host-213-179-129-39.customer.m-online.net. [213.179.129.39]) by smtp.gmail.com with ESMTPSA id c63-20020a509fc5000000b0054c738b6c31sm4789617edf.55.2023.12.12.02.17.45 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 12 Dec 2023 02:17:45 -0800 (PST) From: Jiri Pirko To: netdev@vger.kernel.org Cc: kuba@kernel.org, pabeni@redhat.com, davem@davemloft.net, edumazet@google.com, jacob.e.keller@intel.com, jhs@mojatatu.com, johannes@sipsolutions.net, andriy.shevchenko@linux.intel.com, amritha.nambiar@intel.com, sdf@google.com, horms@kernel.org, przemyslaw.kitszel@intel.com Subject: [patch net-next v6 5/9] genetlink: introduce per-sock family private storage Date: Tue, 12 Dec 2023 11:17:32 +0100 Message-ID: <20231212101736.1112671-6-jiri@resnulli.us> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20231212101736.1112671-1-jiri@resnulli.us> References: <20231212101736.1112671-1-jiri@resnulli.us> 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 From: Jiri Pirko Introduce a wrapper sock struct for Generic netlink and store a pointer to family privs xarray. This per socket xarray contains family->id indexed priv storage. Note I used xarray instead of suggested linked list as it is more convenient. Introduce genl_sk_priv_get() to get the family priv pointer and initialize it in case it does not exist. Introduce __genl_sk_priv_get() to obtain family the priv pointer under RCU read lock. Allow family to specify the priv size, init() and destroy() callbacks. Signed-off-by: Jiri Pirko --- v5->v6: - moved sock_priv* fields out of private section of struct genl_family - introduced NETLINK_SOCK_PROTO_SIZE define and use that in NETLINK_SOCK_SIZE computation - moved struct genl_sock into genetlink.c, added BUILD_BUG_ON check for its size - added missing priv free when xa_cmpxchg() fails - added per-family sock privs tracking in a list, init the list with lock on family register, free all related privs on family unregister - moved code up in above family register/unregister code - added documentation comment part for sock_priv* family struct fields - added WARN_ON_ONCE priv size check in genl_sk_priv_alloc() v4->v5: - s/Returns/Return/ in function comments - introduced wrapper genl sock struct and store xarray there - changed family helpers to genl_sk_priv_get() and __genl_sk_priv_get() - introduced sock_priv_size for family and use this to allocate the priv in generic netlink code - introduced init/destroy callbacks for family privs - moved genl_unlock() call a bit up in the unlikely case section - remove "again" label and return directly v3->v4: - new patch --- include/net/genetlink.h | 16 +++ net/netlink/af_netlink.c | 2 +- net/netlink/af_netlink.h | 7 ++ net/netlink/genetlink.c | 207 ++++++++++++++++++++++++++++++++++++++- 4 files changed, 230 insertions(+), 2 deletions(-) diff --git a/include/net/genetlink.h b/include/net/genetlink.h index c53244f20437..7df3ca11070a 100644 --- a/include/net/genetlink.h +++ b/include/net/genetlink.h @@ -20,6 +20,11 @@ struct genl_multicast_group { u8 cap_sys_admin:1; }; +struct genl_sk_priv_list { + struct list_head list; + spinlock_t lock; /* protects list */ +}; + struct genl_split_ops; struct genl_info; @@ -51,6 +56,9 @@ struct genl_info; * @split_ops: the split do/dump form of operation definition * @n_split_ops: number of entries in @split_ops, not that with split do/dump * ops the number of entries is not the same as number of commands + * @sock_priv_size: the size of per-socket private memory + * @sock_priv_init: the per-socket private memory initializer + * @sock_priv_destroy: the per-socket private memory destructor * * Attribute policies (the combination of @policy and @maxattr fields) * can be attached at the family level or at the operation level. @@ -84,11 +92,17 @@ struct genl_family { const struct genl_multicast_group *mcgrps; struct module *module; + size_t sock_priv_size; + void (*sock_priv_init)(void *priv); + void (*sock_priv_destroy)(void *priv); + /* private: internal use only */ /* protocol family identifier */ int id; /* starting number of multicast group IDs in this family */ unsigned int mcgrp_offset; + /* list of per-socket privs */ + struct genl_sk_priv_list *sock_priv_list; }; /** @@ -298,6 +312,8 @@ static inline bool genl_info_is_ntf(const struct genl_info *info) return !info->nlhdr; } +void *__genl_sk_priv_get(struct sock *sk, struct genl_family *family); +void *genl_sk_priv_get(struct sock *sk, struct genl_family *family); int genl_register_family(struct genl_family *family); int genl_unregister_family(const struct genl_family *family); void genl_notify(const struct genl_family *family, struct sk_buff *skb, diff --git a/net/netlink/af_netlink.c b/net/netlink/af_netlink.c index 177126fb0484..5683b0ca23b1 100644 --- a/net/netlink/af_netlink.c +++ b/net/netlink/af_netlink.c @@ -632,7 +632,7 @@ static void netlink_remove(struct sock *sk) static struct proto netlink_proto = { .name = "NETLINK", .owner = THIS_MODULE, - .obj_size = sizeof(struct netlink_sock), + .obj_size = NETLINK_SOCK_SIZE, }; static int __netlink_create(struct net *net, struct socket *sock, diff --git a/net/netlink/af_netlink.h b/net/netlink/af_netlink.h index 2145979b9986..c9d31e34269a 100644 --- a/net/netlink/af_netlink.h +++ b/net/netlink/af_netlink.h @@ -53,6 +53,13 @@ struct netlink_sock { struct work_struct work; }; +/* Size of netlink sock is size of the biggest user with priv, + * which is currently just Generic Netlink. + */ +#define NETLINK_SOCK_PROTO_SIZE 8 +#define NETLINK_SOCK_SIZE \ + (sizeof(struct netlink_sock) + NETLINK_SOCK_PROTO_SIZE) + static inline struct netlink_sock *nlk_sk(struct sock *sk) { return container_of(sk, struct netlink_sock, sk); diff --git a/net/netlink/genetlink.c b/net/netlink/genetlink.c index 9c7ffd10df2a..676dac168568 100644 --- a/net/netlink/genetlink.c +++ b/net/netlink/genetlink.c @@ -22,6 +22,8 @@ #include #include +#include "af_netlink.h" + static DEFINE_MUTEX(genl_mutex); /* serialization of message processing */ static DECLARE_RWSEM(cb_lock); @@ -631,6 +633,199 @@ static int genl_validate_ops(const struct genl_family *family) return 0; } +struct genl_sock { + struct netlink_sock nlk_sk; + struct xarray *family_privs; +}; + +static inline struct genl_sock *genl_sk(struct sock *sk) +{ + BUILD_BUG_ON(sizeof(struct genl_sock) > NETLINK_SOCK_SIZE); + return container_of(nlk_sk(sk), struct genl_sock, nlk_sk); +} + +struct genl_sk_priv { + struct list_head list; + struct genl_family *family; + void (*destructor)(void *priv); + long priv[]; +}; + +static struct genl_sk_priv *genl_sk_priv_alloc(struct genl_family *family) +{ + struct genl_sk_priv *priv; + + if (WARN_ON_ONCE(!family->sock_priv_size)) + return ERR_PTR(-EINVAL); + + priv = kzalloc(size_add(sizeof(*priv), family->sock_priv_size), + GFP_KERNEL); + if (!priv) + return ERR_PTR(-ENOMEM); + priv->family = family; + priv->destructor = family->sock_priv_destroy; + if (family->sock_priv_init) + family->sock_priv_init(priv->priv); + spin_lock(&family->sock_priv_list->lock); + list_add(&priv->list, &family->sock_priv_list->list); + spin_unlock(&family->sock_priv_list->lock); + return priv; +} + +static void genl_sk_priv_free(struct genl_sk_priv *priv) +{ + spin_lock(&priv->family->sock_priv_list->lock); + list_del(&priv->list); + spin_unlock(&priv->family->sock_priv_list->lock); + if (priv->destructor) + priv->destructor(priv->priv); + kfree(priv); +} + +static int genl_sk_priv_list_alloc(struct genl_family *family) +{ + if (!family->sock_priv_size) + return 0; + + family->sock_priv_list = kzalloc(sizeof(*family->sock_priv_list), + GFP_KERNEL); + if (!family->sock_priv_list) + return -ENOMEM; + INIT_LIST_HEAD(&family->sock_priv_list->list); + spin_lock_init(&family->sock_priv_list->lock); + return 0; +} + +static void genl_sk_priv_list_free(const struct genl_family *family) +{ + struct genl_sk_priv *priv, *tmp; + + if (!family->sock_priv_size) + return; + + list_for_each_entry_safe(priv, tmp, &family->sock_priv_list->list, list) + genl_sk_priv_free(priv); + kfree(family->sock_priv_list); +} + +static void genl_release(struct sock *sk, unsigned long *groups) +{ + struct genl_sock *gsk = genl_sk(sk); + struct genl_sk_priv *priv; + unsigned long family_id; + + if (!gsk->family_privs) + return; + xa_for_each(gsk->family_privs, family_id, priv) { + xa_erase(gsk->family_privs, family_id); + genl_sk_priv_free(priv); + } + xa_destroy(gsk->family_privs); + kfree(gsk->family_privs); +} + +static struct xarray *genl_family_privs_get(struct genl_sock *gsk) +{ + struct xarray *family_privs; + + family_privs = READ_ONCE(gsk->family_privs); + if (family_privs) + return family_privs; + + family_privs = kzalloc(sizeof(*family_privs), GFP_KERNEL); + if (!family_privs) + return ERR_PTR(-ENOMEM); + xa_init_flags(family_privs, XA_FLAGS_ALLOC); + + /* Use genl lock to protect family_privs to be + * initialized in parallel by different CPU. + */ + genl_lock(); + if (unlikely(gsk->family_privs)) { + genl_unlock(); + xa_destroy(family_privs); + kfree(family_privs); + return READ_ONCE(gsk->family_privs); + } + WRITE_ONCE(gsk->family_privs, family_privs); + genl_unlock(); + return family_privs; +} + +/** + * __genl_sk_priv_get - Get per-socket private pointer for family + * + * @sk: socket + * @family: family + * + * Lookup a private pointer stored per-socket by a specified + * Generic netlink family. + * + * Caller should make sure this is called in RCU read locked section. + * + * Return: valid pointer on success, otherwise NULL. + */ +void *__genl_sk_priv_get(struct sock *sk, struct genl_family *family) +{ + struct genl_sock *gsk = genl_sk(sk); + struct genl_sk_priv *priv; + struct xarray *family_privs; + + family_privs = READ_ONCE(gsk->family_privs); + if (!family_privs) + return NULL; + priv = xa_load(family_privs, family->id); + return priv ? priv->priv : NULL; +} + +/** + * genl_sk_priv_get - Get per-socket private pointer for family + * + * @sk: socket + * @family: family + * + * Store a private pointer per-socket for a specified + * Generic netlink family. + * + * Caller has to make sure this is not called in parallel multiple times + * for the same sock and also in parallel to genl_release() for the same sock. + * + * Return: previously stored private pointer for the family (could be NULL) + * on success, otherwise negative error value encoded by ERR_PTR(). + */ +void *genl_sk_priv_get(struct sock *sk, struct genl_family *family) +{ + struct genl_sk_priv *priv, *old_priv; + struct genl_sock *gsk = genl_sk(sk); + struct xarray *family_privs; + + family_privs = genl_family_privs_get(gsk); + if (IS_ERR(family_privs)) + return ERR_CAST(family_privs); + + priv = xa_load(family_privs, family->id); + if (priv) + return priv->priv; + + /* priv for the family does not exist so far, create it. */ + + priv = genl_sk_priv_alloc(family); + if (IS_ERR(priv)) + return ERR_CAST(priv); + + old_priv = xa_cmpxchg(family_privs, family->id, NULL, priv, GFP_KERNEL); + if (xa_is_err(old_priv)) { + genl_sk_priv_free(priv); + return ERR_PTR(xa_err(old_priv)); + } else if (!old_priv) { + return priv->priv; + } + + /* Race happened, priv was already inserted. */ + genl_sk_priv_free(priv); + return old_priv->priv; +} + /** * genl_register_family - register a generic netlink family * @family: generic netlink family @@ -659,6 +854,10 @@ int genl_register_family(struct genl_family *family) goto errout_locked; } + err = genl_sk_priv_list_alloc(family); + if (err) + goto errout_locked; + /* * Sadly, a few cases need to be special-cased * due to them having previously abused the API @@ -679,7 +878,7 @@ int genl_register_family(struct genl_family *family) start, end + 1, GFP_KERNEL); if (family->id < 0) { err = family->id; - goto errout_locked; + goto errout_priv_list_free; } err = genl_validate_assign_mc_groups(family); @@ -698,6 +897,8 @@ int genl_register_family(struct genl_family *family) errout_remove: idr_remove(&genl_fam_idr, family->id); +errout_priv_list_free: + genl_sk_priv_list_free(family); errout_locked: genl_unlock_all(); return err; @@ -728,6 +929,9 @@ int genl_unregister_family(const struct genl_family *family) up_write(&cb_lock); wait_event(genl_sk_destructing_waitq, atomic_read(&genl_sk_destructing_cnt) == 0); + + genl_sk_priv_list_free(family); + genl_unlock(); genl_ctrl_event(CTRL_CMD_DELFAMILY, family, NULL, 0); @@ -1708,6 +1912,7 @@ static int __net_init genl_pernet_init(struct net *net) .input = genl_rcv, .flags = NL_CFG_F_NONROOT_RECV, .bind = genl_bind, + .release = genl_release, }; /* we'll bump the group number right afterwards */ From patchwork Tue Dec 12 10:17:33 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jiri Pirko X-Patchwork-Id: 13488877 X-Patchwork-Delegate: kuba@kernel.org Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=resnulli-us.20230601.gappssmtp.com header.i=@resnulli-us.20230601.gappssmtp.com header.b="rvE+nqWp" Received: from mail-ed1-x52f.google.com (mail-ed1-x52f.google.com [IPv6:2a00:1450:4864:20::52f]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6DE9A9C for ; Tue, 12 Dec 2023 02:17:49 -0800 (PST) Received: by mail-ed1-x52f.google.com with SMTP id 4fb4d7f45d1cf-548ce39b101so7757041a12.2 for ; Tue, 12 Dec 2023 02:17:49 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=resnulli-us.20230601.gappssmtp.com; s=20230601; t=1702376268; x=1702981068; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=n6p6PhL6pJJB1VojYjKvy33GJM9rz1keLAcljTX9SbA=; b=rvE+nqWpwqTckTStifewI9yqKvZrpelscKj/wOQ5tpmHJxUPrtLkqpo40OzJFoDFhQ KuWth9hh9lCXlWOTFZL80h/HLZLgHL+7XRDaI2gIQ+/iJCxug97hgTchfTo6puOqq9/J C/AVK6+4Eq2+kDp51eLHZESVgsjC/fhH+kn2addWzg972ZuWe13KbupmYd9TsuB/unJH XU/DAIUM4mr44o7b3YMeFi60eeLGQ8glwBr49+rEGvlwSl7uO32WCHUGlyTgTyvDfnRS j4lTjGLxibmUga0juumf98yFivrM/spRjUUM2fbVhNOSch3je1yDW5wMRecyxlUjnugK ZDbw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1702376268; x=1702981068; 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=n6p6PhL6pJJB1VojYjKvy33GJM9rz1keLAcljTX9SbA=; b=YZpPX5uI31f9GeL4BqUCt3Z94/08Agev8fUDa006+Fx7dBfO5YDxMEqKRx42aHAD/3 QpEEG5YcoKYYGe7Yz6Q063qEIgZ/YyesZ+CsgTXt6FGqq6Mweqm5NMUqZL8U/6gJ530J AS9ElhDn223zJkRSN3mX1iOMSZX8nHFODyoz5amlxWKPh2ie/bmgpHQXzoRzwDkP2Caj YSpg/gcQUGgoEnJTkmrdUlFORkh2/16vx9oexzJwhWpUTpMcl2ULlng6rFPtrVsiEWIJ 2cbp0SMU00bBqbMwaxdpwN3Uq6JYob/J6djzdJmw01ExIwifVad6ti0WuH0T7XlkZbvl dKag== X-Gm-Message-State: AOJu0YzbBAfFQ9sLxa6hXeLwEO790gbpIE2eSFNslOvAT6YTH4G/VS7T z3boTHUdfmK2brYIsDhnhNAjVHKdeGHpsw05kv8= X-Google-Smtp-Source: AGHT+IEccQ3er4ntByT3BHLOUspHUHzCL8OAfed6JY7w6x7ObvgEIl/Rs0ZgwaeAe829VF5rhS07yA== X-Received: by 2002:a05:6402:2152:b0:54c:4837:7d0a with SMTP id bq18-20020a056402215200b0054c48377d0amr2083720edb.73.1702376267956; Tue, 12 Dec 2023 02:17:47 -0800 (PST) Received: from localhost (host-213-179-129-39.customer.m-online.net. [213.179.129.39]) by smtp.gmail.com with ESMTPSA id s28-20020a50ab1c000000b0054c6b50df3asm4740868edc.92.2023.12.12.02.17.47 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 12 Dec 2023 02:17:47 -0800 (PST) From: Jiri Pirko To: netdev@vger.kernel.org Cc: kuba@kernel.org, pabeni@redhat.com, davem@davemloft.net, edumazet@google.com, jacob.e.keller@intel.com, jhs@mojatatu.com, johannes@sipsolutions.net, andriy.shevchenko@linux.intel.com, amritha.nambiar@intel.com, sdf@google.com, horms@kernel.org, przemyslaw.kitszel@intel.com Subject: [patch net-next v6 6/9] netlink: introduce typedef for filter function Date: Tue, 12 Dec 2023 11:17:33 +0100 Message-ID: <20231212101736.1112671-7-jiri@resnulli.us> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20231212101736.1112671-1-jiri@resnulli.us> References: <20231212101736.1112671-1-jiri@resnulli.us> 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 From: Jiri Pirko Make the code using filter function a bit nicer by consolidating the filter function arguments using typedef. Suggested-by: Andy Shevchenko Signed-off-by: Jiri Pirko --- v2->v3: - left the original .c and .h arg names and types inconsistencies for cn_netlink_send_mult() and netlink_broadcast_filtered() v1->v2: - new patch --- drivers/connector/connector.c | 5 ++--- include/linux/connector.h | 3 +-- include/linux/netlink.h | 6 ++++-- net/netlink/af_netlink.c | 3 +-- 4 files changed, 8 insertions(+), 9 deletions(-) diff --git a/drivers/connector/connector.c b/drivers/connector/connector.c index 7f7b94f616a6..4028e8eeba82 100644 --- a/drivers/connector/connector.c +++ b/drivers/connector/connector.c @@ -59,9 +59,8 @@ static int cn_already_initialized; * both, or if both are zero then the group is looked up and sent there. */ int cn_netlink_send_mult(struct cn_msg *msg, u16 len, u32 portid, u32 __group, - gfp_t gfp_mask, - int (*filter)(struct sock *dsk, struct sk_buff *skb, void *data), - void *filter_data) + gfp_t gfp_mask, netlink_filter_fn filter, + void *filter_data) { struct cn_callback_entry *__cbq; unsigned int size; diff --git a/include/linux/connector.h b/include/linux/connector.h index cec2d99ae902..70bc1160f3d8 100644 --- a/include/linux/connector.h +++ b/include/linux/connector.h @@ -100,8 +100,7 @@ void cn_del_callback(const struct cb_id *id); */ int cn_netlink_send_mult(struct cn_msg *msg, u16 len, u32 portid, u32 group, gfp_t gfp_mask, - int (*filter)(struct sock *dsk, struct sk_buff *skb, - void *data), + netlink_filter_fn filter, void *filter_data); /** diff --git a/include/linux/netlink.h b/include/linux/netlink.h index abe91ed6b9aa..1a4445bf2ab9 100644 --- a/include/linux/netlink.h +++ b/include/linux/netlink.h @@ -228,10 +228,12 @@ bool netlink_strict_get_check(struct sk_buff *skb); int netlink_unicast(struct sock *ssk, struct sk_buff *skb, __u32 portid, int nonblock); int netlink_broadcast(struct sock *ssk, struct sk_buff *skb, __u32 portid, __u32 group, gfp_t allocation); + +typedef int (*netlink_filter_fn)(struct sock *dsk, struct sk_buff *skb, void *data); + int netlink_broadcast_filtered(struct sock *ssk, struct sk_buff *skb, __u32 portid, __u32 group, gfp_t allocation, - int (*filter)(struct sock *dsk, - struct sk_buff *skb, void *data), + netlink_filter_fn filter, void *filter_data); int netlink_set_err(struct sock *ssk, __u32 portid, __u32 group, int code); int netlink_register_notifier(struct notifier_block *nb); diff --git a/net/netlink/af_netlink.c b/net/netlink/af_netlink.c index 5683b0ca23b1..0efc1bd451fc 100644 --- a/net/netlink/af_netlink.c +++ b/net/netlink/af_netlink.c @@ -1519,8 +1519,7 @@ static void do_one_broadcast(struct sock *sk, int netlink_broadcast_filtered(struct sock *ssk, struct sk_buff *skb, u32 portid, u32 group, gfp_t allocation, - int (*filter)(struct sock *dsk, - struct sk_buff *skb, void *data), + netlink_filter_fn filter, void *filter_data) { struct net *net = sock_net(ssk); From patchwork Tue Dec 12 10:17:34 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jiri Pirko X-Patchwork-Id: 13488879 X-Patchwork-Delegate: kuba@kernel.org Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=resnulli-us.20230601.gappssmtp.com header.i=@resnulli-us.20230601.gappssmtp.com header.b="xCrqYk7f" Received: from mail-ej1-x633.google.com (mail-ej1-x633.google.com [IPv6:2a00:1450:4864:20::633]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4381F92 for ; Tue, 12 Dec 2023 02:17:51 -0800 (PST) Received: by mail-ej1-x633.google.com with SMTP id a640c23a62f3a-9fa2714e828so716895766b.1 for ; Tue, 12 Dec 2023 02:17:51 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=resnulli-us.20230601.gappssmtp.com; s=20230601; t=1702376269; x=1702981069; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=Gvq7X9IR33uLz+NOli+ik7mMtRRti1WP//ME+EWKFLc=; b=xCrqYk7fLNRCFvRTY1mn9WUWK9a8uLzwvbG9WGs4snOfgF+2KZb/A+RgIbuY5trNR5 qUOPAgCzuMMHZ9koW7MmtVskY7NS7HebFCREx/BMS0IBdfM5mFZdgcGRkl8PSijf/b1N W9yNFr1Et8bUyqdiRyXOAkCR8XXHyOLkSguJ67rZs5Asd1ytE8a3yePaL/IhDuYtI0mg gUjmJRw/QsQ6xRN3Ccp2awZsUCy/XCYe1ZnJjBWNAmFKn5R4X6yTsZpd9bXYgJ+a/Fgm Q51m3R/ZD1fUdkMnk0OlzWejaGC8+3y1Na6aolnjqKWK/Zf5fKzKw2h+LVDBQxPrfOQD ZvPw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1702376269; x=1702981069; 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=Gvq7X9IR33uLz+NOli+ik7mMtRRti1WP//ME+EWKFLc=; b=AQXw+u6eVnMYbQaGCDub+ebeuWZQ0U063bmE2KWZCpub4unJ4yRodImLJER7qkoKT3 M1ce9BHMbAAXDJlDOkkGmDETjl+vBUHO411Cirwy7dsdSw3Xnk6KQ0DUW899+uxWcPkE Ognc5t0XdnmAoPUHcizzcYJhnI0j6YkuLAZJ+0qsQ1I7Tg3AiI5F9ejs6/kQgF7atsmQ j4mcD+hws6XU4Unewe6QcXQJq27xyGp8k6ADdebZcFMAJUvM0cdrzHxlToBfDjSBtMvQ cDesjg/E7ZJMp0PIdpSu4kF4XIxGHRBYDmwOTQamEO5q5KNIVqzK2nbj3aZgenwbcenT miAA== X-Gm-Message-State: AOJu0YzjVaieTgG8a60HdKxQPriVRz4eiYZePILMOjg7wk9MBJL7IcS1 YG5MM3kMFMZLYGOL98XYVKueG9mqmWv/z/jU70U= X-Google-Smtp-Source: AGHT+IFLXzl33h/5IQ56s7iw09G7bVbx8QsiX4NhwJcfOKMBMwuvG7N4KyH0ECzwgIv1y7rgbfAKvA== X-Received: by 2002:a17:906:184:b0:a19:a19b:55ce with SMTP id 4-20020a170906018400b00a19a19b55cemr2655303ejb.94.1702376269749; Tue, 12 Dec 2023 02:17:49 -0800 (PST) Received: from localhost (host-213-179-129-39.customer.m-online.net. [213.179.129.39]) by smtp.gmail.com with ESMTPSA id ll9-20020a170907190900b00a1da2f7c1d8sm6026586ejc.77.2023.12.12.02.17.48 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 12 Dec 2023 02:17:49 -0800 (PST) From: Jiri Pirko To: netdev@vger.kernel.org Cc: kuba@kernel.org, pabeni@redhat.com, davem@davemloft.net, edumazet@google.com, jacob.e.keller@intel.com, jhs@mojatatu.com, johannes@sipsolutions.net, andriy.shevchenko@linux.intel.com, amritha.nambiar@intel.com, sdf@google.com, horms@kernel.org, przemyslaw.kitszel@intel.com Subject: [patch net-next v6 7/9] genetlink: introduce helpers to do filtered multicast Date: Tue, 12 Dec 2023 11:17:34 +0100 Message-ID: <20231212101736.1112671-8-jiri@resnulli.us> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20231212101736.1112671-1-jiri@resnulli.us> References: <20231212101736.1112671-1-jiri@resnulli.us> 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 From: Jiri Pirko Currently it is possible for netlink kernel user to pass custom filter function to broadcast send function netlink_broadcast_filtered(). However, this is not exposed to multicast send and to generic netlink users. Extend the api and introduce a netlink helper nlmsg_multicast_filtered() and a generic netlink helper genlmsg_multicast_netns_filtered() to allow generic netlink families to specify filter function while sending multicast messages. Signed-off-by: Jiri Pirko --- v1->v2: - used netlink_filter_fn introduce by the previous patch - added return comments to silence scripts/kernel-doc warnings --- include/net/genetlink.h | 35 +++++++++++++++++++++++++++++++---- include/net/netlink.h | 31 +++++++++++++++++++++++++++---- 2 files changed, 58 insertions(+), 8 deletions(-) diff --git a/include/net/genetlink.h b/include/net/genetlink.h index 7df3ca11070a..3dedb6268d5e 100644 --- a/include/net/genetlink.h +++ b/include/net/genetlink.h @@ -453,6 +453,35 @@ static inline void genlmsg_cancel(struct sk_buff *skb, void *hdr) nlmsg_cancel(skb, hdr - GENL_HDRLEN - NLMSG_HDRLEN); } +/** + * genlmsg_multicast_netns_filtered - multicast a netlink message + * to a specific netns with filter + * function + * @family: the generic netlink family + * @net: the net namespace + * @skb: netlink message as socket buffer + * @portid: own netlink portid to avoid sending to yourself + * @group: offset of multicast group in groups array + * @flags: allocation flags + * @filter: filter function + * @filter_data: filter function private data + * + * Return: 0 on success, negative error code for failure. + */ +static inline int +genlmsg_multicast_netns_filtered(const struct genl_family *family, + struct net *net, struct sk_buff *skb, + u32 portid, unsigned int group, gfp_t flags, + netlink_filter_fn filter, + void *filter_data) +{ + if (WARN_ON_ONCE(group >= family->n_mcgrps)) + return -EINVAL; + group = family->mcgrp_offset + group; + return nlmsg_multicast_filtered(net->genl_sock, skb, portid, group, + flags, filter, filter_data); +} + /** * genlmsg_multicast_netns - multicast a netlink message to a specific netns * @family: the generic netlink family @@ -466,10 +495,8 @@ static inline int genlmsg_multicast_netns(const struct genl_family *family, struct net *net, struct sk_buff *skb, u32 portid, unsigned int group, gfp_t flags) { - if (WARN_ON_ONCE(group >= family->n_mcgrps)) - return -EINVAL; - group = family->mcgrp_offset + group; - return nlmsg_multicast(net->genl_sock, skb, portid, group, flags); + return genlmsg_multicast_netns_filtered(family, net, skb, portid, + group, flags, NULL, NULL); } /** diff --git a/include/net/netlink.h b/include/net/netlink.h index 28039e57070a..c19ff921b661 100644 --- a/include/net/netlink.h +++ b/include/net/netlink.h @@ -1087,27 +1087,50 @@ static inline void nlmsg_free(struct sk_buff *skb) } /** - * nlmsg_multicast - multicast a netlink message + * nlmsg_multicast_filtered - multicast a netlink message with filter function * @sk: netlink socket to spread messages to * @skb: netlink message as socket buffer * @portid: own netlink portid to avoid sending to yourself * @group: multicast group id * @flags: allocation flags + * @filter: filter function + * @filter_data: filter function private data + * + * Return: 0 on success, negative error code for failure. */ -static inline int nlmsg_multicast(struct sock *sk, struct sk_buff *skb, - u32 portid, unsigned int group, gfp_t flags) +static inline int nlmsg_multicast_filtered(struct sock *sk, struct sk_buff *skb, + u32 portid, unsigned int group, + gfp_t flags, + netlink_filter_fn filter, + void *filter_data) { int err; NETLINK_CB(skb).dst_group = group; - err = netlink_broadcast(sk, skb, portid, group, flags); + err = netlink_broadcast_filtered(sk, skb, portid, group, flags, + filter, filter_data); if (err > 0) err = 0; return err; } +/** + * nlmsg_multicast - multicast a netlink message + * @sk: netlink socket to spread messages to + * @skb: netlink message as socket buffer + * @portid: own netlink portid to avoid sending to yourself + * @group: multicast group id + * @flags: allocation flags + */ +static inline int nlmsg_multicast(struct sock *sk, struct sk_buff *skb, + u32 portid, unsigned int group, gfp_t flags) +{ + return nlmsg_multicast_filtered(sk, skb, portid, group, flags, + NULL, NULL); +} + /** * nlmsg_unicast - unicast a netlink message * @sk: netlink socket to spread message to From patchwork Tue Dec 12 10:17:35 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jiri Pirko X-Patchwork-Id: 13488880 X-Patchwork-Delegate: kuba@kernel.org Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=resnulli-us.20230601.gappssmtp.com header.i=@resnulli-us.20230601.gappssmtp.com header.b="eiUICjW8" Received: from mail-ej1-x62d.google.com (mail-ej1-x62d.google.com [IPv6:2a00:1450:4864:20::62d]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 228819C for ; Tue, 12 Dec 2023 02:17:53 -0800 (PST) Received: by mail-ej1-x62d.google.com with SMTP id a640c23a62f3a-a1e116f2072so1118181966b.0 for ; Tue, 12 Dec 2023 02:17:53 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=resnulli-us.20230601.gappssmtp.com; s=20230601; t=1702376271; x=1702981071; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=CW+b4ooRLVZCpYEu8WPlvtXG500IAMPfBrByuKNM/JE=; b=eiUICjW8Xm08eiM+ydp1EftkRVFVh3tOaCjyc1rP6OWJBqlhtxryNhFB2KCntj4+X+ lDJVpkVuUcABWkX6dDxvkL0rG+74Lna6kL1ihGPOgzE9o9+XNAQ1abIT2RzJs8CbK2pg /1FTsY9z6vwtl2iq+BA6pX6kPnMYfqW/FsXC7Kvpw8ai2+uZcAIGluyw73vfn4fiGeYl Mlb2PJytP3zx6g3U+tNzUUr3UApzFtNJh36HhTWCNN5JU6Qfc7HIFiaE/lksg1l8vYGL JCNmdAkKmR4LYTESVYpGbNznVdo7ei9sJRxQ8uAIahASUnlVl2nz7NGT9Lv+hCctdWLI DeiQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1702376271; x=1702981071; 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=CW+b4ooRLVZCpYEu8WPlvtXG500IAMPfBrByuKNM/JE=; b=lzW0Lwczxbo6JsGfMTbFe7agrv+XpIC92EDBh7vnanQjCWZusr/zk3oAsk7e9rNCSx ny0g3eIs7cH3teUl8YYJwWNDhYaUEIGDmPecTN8ceuMBWhdJABoV13erMFsCjU/XADgF mv4V84BdNCr8oR00j5USy1lBf6cTakSXpQa0AJG7xh5loVOUz8G05c9/6bWaQSO3dts3 7CZ5KPrNW7BuoMTVSwMPmwd+IgEefYQyrc9EDIepIq367j7p7CVd9mqIZDIEkypap88B jZYCSqTgodmPfTpTeFzXsd7AViFPaVfT1/HH5aAdC9qGhUtU7N6XLbZ4bAY815MADRAO NVFw== X-Gm-Message-State: AOJu0YxXK26pr5FGPB7HqT1t3Qw2pSWXl8tfpFlLpAUVXWBiR6wzi1iK 4fkZY3QTaK+xsVOxWM7FgOfJ7GldKYBuvB9mno4= X-Google-Smtp-Source: AGHT+IGBGaHZ1Yb1OUp5/od6PT/eZyj0IvqyPUxQtz2lVCgCJhwDPpu8NAkrHlqUnmCejWo9KLy5/g== X-Received: by 2002:a17:907:e86:b0:a1d:6cbc:c22 with SMTP id ho6-20020a1709070e8600b00a1d6cbc0c22mr7212424ejc.41.1702376271388; Tue, 12 Dec 2023 02:17:51 -0800 (PST) Received: from localhost (host-213-179-129-39.customer.m-online.net. [213.179.129.39]) by smtp.gmail.com with ESMTPSA id v14-20020a17090651ce00b00a1f81a892b3sm4647600ejk.152.2023.12.12.02.17.50 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 12 Dec 2023 02:17:50 -0800 (PST) From: Jiri Pirko To: netdev@vger.kernel.org Cc: kuba@kernel.org, pabeni@redhat.com, davem@davemloft.net, edumazet@google.com, jacob.e.keller@intel.com, jhs@mojatatu.com, johannes@sipsolutions.net, andriy.shevchenko@linux.intel.com, amritha.nambiar@intel.com, sdf@google.com, horms@kernel.org, przemyslaw.kitszel@intel.com Subject: [patch net-next v6 8/9] devlink: add a command to set notification filter and use it for multicasts Date: Tue, 12 Dec 2023 11:17:35 +0100 Message-ID: <20231212101736.1112671-9-jiri@resnulli.us> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20231212101736.1112671-1-jiri@resnulli.us> References: <20231212101736.1112671-1-jiri@resnulli.us> 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 From: Jiri Pirko Currently the user listening on a socket for devlink notifications gets always all messages for all existing instances, even if he is interested only in one of those. That may cause unnecessary overhead on setups with thousands of instances present. User is currently able to narrow down the devlink objects replies to dump commands by specifying select attributes. Allow similar approach for notifications. Introduce a new devlink NOTIFY_FILTER_SET which the user passes the select attributes. Store these per-socket and use them for filtering messages during multicast send. Signed-off-by: Jiri Pirko --- v4->v5: - rebased on top of generic netlink per sock per family pointer allocation code - changed the flt to be stored in family priv rcu pointer, protected by spin lock - changed to use size_add() helper for kzalloc() size computation - removed generated userspace bits v3->v4: - rebased on top of genl_sk_priv_*() introduction --- Documentation/netlink/specs/devlink.yaml | 10 +++ include/uapi/linux/devlink.h | 2 + net/devlink/devl_internal.h | 34 ++++++- net/devlink/netlink.c | 108 +++++++++++++++++++++++ net/devlink/netlink_gen.c | 15 +++- net/devlink/netlink_gen.h | 4 +- 6 files changed, 169 insertions(+), 4 deletions(-) diff --git a/Documentation/netlink/specs/devlink.yaml b/Documentation/netlink/specs/devlink.yaml index c3a438197964..88bfcb3c3346 100644 --- a/Documentation/netlink/specs/devlink.yaml +++ b/Documentation/netlink/specs/devlink.yaml @@ -2254,3 +2254,13 @@ operations: - bus-name - dev-name - selftests + + - + name: notify-filter-set + doc: Set notification messages socket filter. + attribute-set: devlink + do: + request: + attributes: + - bus-name + - dev-name diff --git a/include/uapi/linux/devlink.h b/include/uapi/linux/devlink.h index b3c8383d342d..130cae0d3e20 100644 --- a/include/uapi/linux/devlink.h +++ b/include/uapi/linux/devlink.h @@ -139,6 +139,8 @@ enum devlink_command { DEVLINK_CMD_SELFTESTS_GET, /* can dump */ DEVLINK_CMD_SELFTESTS_RUN, + DEVLINK_CMD_NOTIFY_FILTER_SET, + /* add new commands above here */ __DEVLINK_CMD_MAX, DEVLINK_CMD_MAX = __DEVLINK_CMD_MAX - 1 diff --git a/net/devlink/devl_internal.h b/net/devlink/devl_internal.h index 84dc9628d3f2..82e0fb3bbebf 100644 --- a/net/devlink/devl_internal.h +++ b/net/devlink/devl_internal.h @@ -191,11 +191,41 @@ static inline bool devlink_nl_notify_need(struct devlink *devlink) DEVLINK_MCGRP_CONFIG); } +struct devlink_obj_desc { + struct rcu_head rcu; + const char *bus_name; + const char *dev_name; + long data[]; +}; + +static inline void devlink_nl_obj_desc_init(struct devlink_obj_desc *desc, + struct devlink *devlink) +{ + memset(desc, 0, sizeof(*desc)); + desc->bus_name = devlink->dev->bus->name; + desc->dev_name = dev_name(devlink->dev); +} + +int devlink_nl_notify_filter(struct sock *dsk, struct sk_buff *skb, void *data); + +static inline void devlink_nl_notify_send_desc(struct devlink *devlink, + struct sk_buff *msg, + struct devlink_obj_desc *desc) +{ + genlmsg_multicast_netns_filtered(&devlink_nl_family, + devlink_net(devlink), + msg, 0, DEVLINK_MCGRP_CONFIG, + GFP_KERNEL, + devlink_nl_notify_filter, desc); +} + static inline void devlink_nl_notify_send(struct devlink *devlink, struct sk_buff *msg) { - genlmsg_multicast_netns(&devlink_nl_family, devlink_net(devlink), - msg, 0, DEVLINK_MCGRP_CONFIG, GFP_KERNEL); + struct devlink_obj_desc desc; + + devlink_nl_obj_desc_init(&desc, devlink); + devlink_nl_notify_send_desc(devlink, msg, &desc); } /* Notify */ diff --git a/net/devlink/netlink.c b/net/devlink/netlink.c index fa9afe3e6d9b..06f294d34a04 100644 --- a/net/devlink/netlink.c +++ b/net/devlink/netlink.c @@ -17,6 +17,111 @@ static const struct genl_multicast_group devlink_nl_mcgrps[] = { [DEVLINK_MCGRP_CONFIG] = { .name = DEVLINK_GENL_MCGRP_CONFIG_NAME }, }; +struct devlink_nl_sock_priv { + struct devlink_obj_desc __rcu *flt; + spinlock_t flt_lock; /* Protects flt. */ +}; + +static void devlink_nl_sock_priv_init(void *priv) +{ + struct devlink_nl_sock_priv *sk_priv = priv; + + spin_lock_init(&sk_priv->flt_lock); +} + +static void devlink_nl_sock_priv_destroy(void *priv) +{ + struct devlink_nl_sock_priv *sk_priv = priv; + struct devlink_obj_desc *flt; + + flt = rcu_dereference_protected(sk_priv->flt, true); + kfree_rcu(flt, rcu); +} + +int devlink_nl_notify_filter_set_doit(struct sk_buff *skb, + struct genl_info *info) +{ + struct devlink_nl_sock_priv *sk_priv; + struct nlattr **attrs = info->attrs; + struct devlink_obj_desc *flt; + size_t data_offset = 0; + size_t data_size = 0; + char *pos; + + if (attrs[DEVLINK_ATTR_BUS_NAME]) + data_size = size_add(data_size, + nla_len(attrs[DEVLINK_ATTR_BUS_NAME]) + 1); + if (attrs[DEVLINK_ATTR_DEV_NAME]) + data_size = size_add(data_size, + nla_len(attrs[DEVLINK_ATTR_DEV_NAME]) + 1); + + flt = kzalloc(size_add(sizeof(*flt), data_size), GFP_KERNEL); + if (!flt) + return -ENOMEM; + + pos = (char *) flt->data; + if (attrs[DEVLINK_ATTR_BUS_NAME]) { + data_offset += nla_strscpy(pos, + attrs[DEVLINK_ATTR_BUS_NAME], + data_size) + 1; + flt->bus_name = pos; + pos += data_offset; + } + if (attrs[DEVLINK_ATTR_DEV_NAME]) { + nla_strscpy(pos, attrs[DEVLINK_ATTR_DEV_NAME], + data_size - data_offset); + flt->dev_name = pos; + } + + /* Don't attach empty filter. */ + if (!flt->bus_name && !flt->dev_name) { + kfree(flt); + flt = NULL; + } + + sk_priv = genl_sk_priv_get(NETLINK_CB(skb).sk, &devlink_nl_family); + if (IS_ERR(sk_priv)) { + kfree(flt); + return PTR_ERR(sk_priv); + } + spin_lock(&sk_priv->flt_lock); + flt = rcu_replace_pointer(sk_priv->flt, flt, + lockdep_is_held(&sk_priv->flt_lock)); + spin_unlock(&sk_priv->flt_lock); + kfree_rcu(flt, rcu); + return 0; +} + +static bool devlink_obj_desc_match(const struct devlink_obj_desc *desc, + const struct devlink_obj_desc *flt) +{ + if (desc->bus_name && flt->bus_name && + strcmp(desc->bus_name, flt->bus_name)) + return false; + if (desc->dev_name && flt->dev_name && + strcmp(desc->dev_name, flt->dev_name)) + return false; + return true; +} + +int devlink_nl_notify_filter(struct sock *dsk, struct sk_buff *skb, void *data) +{ + struct devlink_obj_desc *desc = data; + struct devlink_nl_sock_priv *sk_priv; + struct devlink_obj_desc *flt; + int ret = 0; + + rcu_read_lock(); + sk_priv = __genl_sk_priv_get(dsk, &devlink_nl_family); + if (sk_priv) { + flt = rcu_dereference(sk_priv->flt); + if (flt) + ret = !devlink_obj_desc_match(desc, flt); + } + rcu_read_unlock(); + return ret; +} + int devlink_nl_put_nested_handle(struct sk_buff *msg, struct net *net, struct devlink *devlink, int attrtype) { @@ -256,4 +361,7 @@ struct genl_family devlink_nl_family __ro_after_init = { .resv_start_op = DEVLINK_CMD_SELFTESTS_RUN + 1, .mcgrps = devlink_nl_mcgrps, .n_mcgrps = ARRAY_SIZE(devlink_nl_mcgrps), + .sock_priv_size = sizeof(struct devlink_nl_sock_priv), + .sock_priv_init = devlink_nl_sock_priv_init, + .sock_priv_destroy = devlink_nl_sock_priv_destroy, }; diff --git a/net/devlink/netlink_gen.c b/net/devlink/netlink_gen.c index 95f9b4350ab7..1cb0e05305d2 100644 --- a/net/devlink/netlink_gen.c +++ b/net/devlink/netlink_gen.c @@ -560,8 +560,14 @@ static const struct nla_policy devlink_selftests_run_nl_policy[DEVLINK_ATTR_SELF [DEVLINK_ATTR_SELFTESTS] = NLA_POLICY_NESTED(devlink_dl_selftest_id_nl_policy), }; +/* DEVLINK_CMD_NOTIFY_FILTER_SET - do */ +static const struct nla_policy devlink_notify_filter_set_nl_policy[DEVLINK_ATTR_DEV_NAME + 1] = { + [DEVLINK_ATTR_BUS_NAME] = { .type = NLA_NUL_STRING, }, + [DEVLINK_ATTR_DEV_NAME] = { .type = NLA_NUL_STRING, }, +}; + /* Ops table for devlink */ -const struct genl_split_ops devlink_nl_ops[73] = { +const struct genl_split_ops devlink_nl_ops[74] = { { .cmd = DEVLINK_CMD_GET, .validate = GENL_DONT_VALIDATE_STRICT, @@ -1233,4 +1239,11 @@ const struct genl_split_ops devlink_nl_ops[73] = { .maxattr = DEVLINK_ATTR_SELFTESTS, .flags = GENL_ADMIN_PERM | GENL_CMD_CAP_DO, }, + { + .cmd = DEVLINK_CMD_NOTIFY_FILTER_SET, + .doit = devlink_nl_notify_filter_set_doit, + .policy = devlink_notify_filter_set_nl_policy, + .maxattr = DEVLINK_ATTR_DEV_NAME, + .flags = GENL_CMD_CAP_DO, + }, }; diff --git a/net/devlink/netlink_gen.h b/net/devlink/netlink_gen.h index 02f3c0bfae0e..8f2bd50ddf5e 100644 --- a/net/devlink/netlink_gen.h +++ b/net/devlink/netlink_gen.h @@ -16,7 +16,7 @@ extern const struct nla_policy devlink_dl_port_function_nl_policy[DEVLINK_PORT_F extern const struct nla_policy devlink_dl_selftest_id_nl_policy[DEVLINK_ATTR_SELFTEST_ID_FLASH + 1]; /* Ops table for devlink */ -extern const struct genl_split_ops devlink_nl_ops[73]; +extern const struct genl_split_ops devlink_nl_ops[74]; int devlink_nl_pre_doit(const struct genl_split_ops *ops, struct sk_buff *skb, struct genl_info *info); @@ -142,5 +142,7 @@ int devlink_nl_selftests_get_doit(struct sk_buff *skb, struct genl_info *info); int devlink_nl_selftests_get_dumpit(struct sk_buff *skb, struct netlink_callback *cb); int devlink_nl_selftests_run_doit(struct sk_buff *skb, struct genl_info *info); +int devlink_nl_notify_filter_set_doit(struct sk_buff *skb, + struct genl_info *info); #endif /* _LINUX_DEVLINK_GEN_H */ From patchwork Tue Dec 12 10:17:36 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jiri Pirko X-Patchwork-Id: 13488881 X-Patchwork-Delegate: kuba@kernel.org Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=resnulli-us.20230601.gappssmtp.com header.i=@resnulli-us.20230601.gappssmtp.com header.b="d3zTKQXt" Received: from mail-ej1-x631.google.com (mail-ej1-x631.google.com [IPv6:2a00:1450:4864:20::631]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7AEA3A6 for ; Tue, 12 Dec 2023 02:17:54 -0800 (PST) Received: by mail-ej1-x631.google.com with SMTP id a640c23a62f3a-a1e116f2072so1118186766b.0 for ; Tue, 12 Dec 2023 02:17:54 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=resnulli-us.20230601.gappssmtp.com; s=20230601; t=1702376273; x=1702981073; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=b9Yp3x4mB8o6EVxoJQRF1QQfUP6GGPqcslyRHb22d6c=; b=d3zTKQXtiscKoDj+9qwFsfYuz2h0iIGK5C/tIC5CJEIShuK3Axva+XbJrJInDdh0xY wVwJOhOBIi8sVci5aX1B/t1LnCr+7eNKWmTgbsyh6EtlP+zniOv/AjAl03Tj1X9c0n7g lrY1ocgAoudYjDWJegbkikj750WteYsnYHMPeXvzH8xfMo1s42ZnsOqcMWEBgRUgyDWH qFrD+NpX+IdqHximrRcI3FIKZ8EFwMwEYdsqkkeoW8NAQ/9EHOPjPGsoB8WPGgAE61eK Nolb7xRoS1oXTH31XngrkfL6sY6rQtWkskDLXomiYeICVwg8Dvhq09Bu2ymjJQ/yvUT/ Cl7Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1702376273; x=1702981073; 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=b9Yp3x4mB8o6EVxoJQRF1QQfUP6GGPqcslyRHb22d6c=; b=u6R1o1dc5E/Ep73lrSglT6Mva6VMN0Y+WquD+rxWYf8u3/qj9TPJJUzvjW90cM2u2x m7m0QunqCro6lxd9RBfizNCOWw+70tQiLk8sizS54s6YKdXxgxDOMRgokL6MiKG/+ral efKHP+bEQMN2yX0QNiXPm5sLf2bc783CsBetmIsI8QFNG2PBPE0Nk1qzXG5rUfky8Ukm UENSL7tCn3+qG7hc7k7IEk0I+At/ijOqGPcOgjND/o7bHDF7bDcHBMtXRvXsRA+DWP06 0jC+TAMNf6uI5fTwdFSlasg9EQWABSTFqtfGaK1M9Q6ETPlNGdPYp7FktemA4n0WfJ+Q WS8w== X-Gm-Message-State: AOJu0YxADJGwqZWt98wIsXu50CvYIyBZrdSJBLnvyplZMZmNCnhTFnh+ t2lFDZNmgzXdrLCHOXExAf5g7sr4Rm5YE7cy+t0= X-Google-Smtp-Source: AGHT+IGAL4uZ30BQu9Wm1FDAYbCJXZW51xqdiferJHMra5zUX7DYKhL6idUAq+nqlLH2XWCHgnY8Rw== X-Received: by 2002:a17:907:28c1:b0:a19:5cb3:c52d with SMTP id en1-20020a17090728c100b00a195cb3c52dmr6451674ejc.11.1702376273031; Tue, 12 Dec 2023 02:17:53 -0800 (PST) Received: from localhost (host-213-179-129-39.customer.m-online.net. [213.179.129.39]) by smtp.gmail.com with ESMTPSA id vc11-20020a170907d08b00b00a1b6d503e7esm6007486ejc.157.2023.12.12.02.17.52 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 12 Dec 2023 02:17:52 -0800 (PST) From: Jiri Pirko To: netdev@vger.kernel.org Cc: kuba@kernel.org, pabeni@redhat.com, davem@davemloft.net, edumazet@google.com, jacob.e.keller@intel.com, jhs@mojatatu.com, johannes@sipsolutions.net, andriy.shevchenko@linux.intel.com, amritha.nambiar@intel.com, sdf@google.com, horms@kernel.org, przemyslaw.kitszel@intel.com Subject: [patch net-next v6 9/9] devlink: extend multicast filtering by port index Date: Tue, 12 Dec 2023 11:17:36 +0100 Message-ID: <20231212101736.1112671-10-jiri@resnulli.us> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20231212101736.1112671-1-jiri@resnulli.us> References: <20231212101736.1112671-1-jiri@resnulli.us> 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 From: Jiri Pirko Expose the previously introduced notification multicast messages filtering infrastructure and allow the user to select messages using port index. Signed-off-by: Jiri Pirko --- v4->v5: - removed generated userspace bits v3->v4: - rebased on top of genl_sk_priv_*() introduction --- Documentation/netlink/specs/devlink.yaml | 1 + net/devlink/devl_internal.h | 9 +++++++++ net/devlink/health.c | 6 +++++- net/devlink/netlink.c | 10 +++++++++- net/devlink/netlink_gen.c | 5 +++-- net/devlink/port.c | 5 ++++- 6 files changed, 31 insertions(+), 5 deletions(-) diff --git a/Documentation/netlink/specs/devlink.yaml b/Documentation/netlink/specs/devlink.yaml index 88bfcb3c3346..cf6eaa0da821 100644 --- a/Documentation/netlink/specs/devlink.yaml +++ b/Documentation/netlink/specs/devlink.yaml @@ -2264,3 +2264,4 @@ operations: attributes: - bus-name - dev-name + - port-index diff --git a/net/devlink/devl_internal.h b/net/devlink/devl_internal.h index 82e0fb3bbebf..c7a8e13f917c 100644 --- a/net/devlink/devl_internal.h +++ b/net/devlink/devl_internal.h @@ -195,6 +195,8 @@ struct devlink_obj_desc { struct rcu_head rcu; const char *bus_name; const char *dev_name; + unsigned int port_index; + bool port_index_valid; long data[]; }; @@ -206,6 +208,13 @@ static inline void devlink_nl_obj_desc_init(struct devlink_obj_desc *desc, desc->dev_name = dev_name(devlink->dev); } +static inline void devlink_nl_obj_desc_port_set(struct devlink_obj_desc *desc, + struct devlink_port *devlink_port) +{ + desc->port_index = devlink_port->index; + desc->port_index_valid = true; +} + int devlink_nl_notify_filter(struct sock *dsk, struct sk_buff *skb, void *data); static inline void devlink_nl_notify_send_desc(struct devlink *devlink, diff --git a/net/devlink/health.c b/net/devlink/health.c index 1d59ec0202f6..acb8c0e174bb 100644 --- a/net/devlink/health.c +++ b/net/devlink/health.c @@ -490,6 +490,7 @@ static void devlink_recover_notify(struct devlink_health_reporter *reporter, enum devlink_command cmd) { struct devlink *devlink = reporter->devlink; + struct devlink_obj_desc desc; struct sk_buff *msg; int err; @@ -509,7 +510,10 @@ static void devlink_recover_notify(struct devlink_health_reporter *reporter, return; } - devlink_nl_notify_send(devlink, msg); + devlink_nl_obj_desc_init(&desc, devlink); + if (reporter->devlink_port) + devlink_nl_obj_desc_port_set(&desc, reporter->devlink_port); + devlink_nl_notify_send_desc(devlink, msg, &desc); } void diff --git a/net/devlink/netlink.c b/net/devlink/netlink.c index 06f294d34a04..c6de311130fb 100644 --- a/net/devlink/netlink.c +++ b/net/devlink/netlink.c @@ -73,8 +73,13 @@ int devlink_nl_notify_filter_set_doit(struct sk_buff *skb, flt->dev_name = pos; } + if (attrs[DEVLINK_ATTR_PORT_INDEX]) { + flt->port_index = nla_get_u32(attrs[DEVLINK_ATTR_PORT_INDEX]); + flt->port_index_valid = true; + } + /* Don't attach empty filter. */ - if (!flt->bus_name && !flt->dev_name) { + if (!flt->bus_name && !flt->dev_name && !flt->port_index_valid) { kfree(flt); flt = NULL; } @@ -101,6 +106,9 @@ static bool devlink_obj_desc_match(const struct devlink_obj_desc *desc, if (desc->dev_name && flt->dev_name && strcmp(desc->dev_name, flt->dev_name)) return false; + if (desc->port_index_valid && flt->port_index_valid && + desc->port_index != flt->port_index) + return false; return true; } diff --git a/net/devlink/netlink_gen.c b/net/devlink/netlink_gen.c index 1cb0e05305d2..c81cf2dd154f 100644 --- a/net/devlink/netlink_gen.c +++ b/net/devlink/netlink_gen.c @@ -561,9 +561,10 @@ static const struct nla_policy devlink_selftests_run_nl_policy[DEVLINK_ATTR_SELF }; /* DEVLINK_CMD_NOTIFY_FILTER_SET - do */ -static const struct nla_policy devlink_notify_filter_set_nl_policy[DEVLINK_ATTR_DEV_NAME + 1] = { +static const struct nla_policy devlink_notify_filter_set_nl_policy[DEVLINK_ATTR_PORT_INDEX + 1] = { [DEVLINK_ATTR_BUS_NAME] = { .type = NLA_NUL_STRING, }, [DEVLINK_ATTR_DEV_NAME] = { .type = NLA_NUL_STRING, }, + [DEVLINK_ATTR_PORT_INDEX] = { .type = NLA_U32, }, }; /* Ops table for devlink */ @@ -1243,7 +1244,7 @@ const struct genl_split_ops devlink_nl_ops[74] = { .cmd = DEVLINK_CMD_NOTIFY_FILTER_SET, .doit = devlink_nl_notify_filter_set_doit, .policy = devlink_notify_filter_set_nl_policy, - .maxattr = DEVLINK_ATTR_DEV_NAME, + .maxattr = DEVLINK_ATTR_PORT_INDEX, .flags = GENL_CMD_CAP_DO, }, }; diff --git a/net/devlink/port.c b/net/devlink/port.c index 758df3000a1b..62e54e152ecf 100644 --- a/net/devlink/port.c +++ b/net/devlink/port.c @@ -507,6 +507,7 @@ static void devlink_port_notify(struct devlink_port *devlink_port, enum devlink_command cmd) { struct devlink *devlink = devlink_port->devlink; + struct devlink_obj_desc desc; struct sk_buff *msg; int err; @@ -525,7 +526,9 @@ static void devlink_port_notify(struct devlink_port *devlink_port, return; } - devlink_nl_notify_send(devlink, msg); + devlink_nl_obj_desc_init(&desc, devlink); + devlink_nl_obj_desc_port_set(&desc, devlink_port); + devlink_nl_notify_send_desc(devlink, msg, &desc); } static void devlink_ports_notify(struct devlink *devlink,