From patchwork Sat Dec 16 12:29:53 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jiri Pirko X-Patchwork-Id: 13495547 X-Patchwork-Delegate: kuba@kernel.org Received: from mail-wm1-f49.google.com (mail-wm1-f49.google.com [209.85.128.49]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 80CD819BA5 for ; Sat, 16 Dec 2023 12:30:06 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=resnulli.us Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=resnulli.us 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="jTgMcHq5" Received: by mail-wm1-f49.google.com with SMTP id 5b1f17b1804b1-40c3f68b69aso15135445e9.1 for ; Sat, 16 Dec 2023 04:30:06 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=resnulli-us.20230601.gappssmtp.com; s=20230601; t=1702729805; x=1703334605; 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=jTgMcHq5ab1oiMEOcRELLrJ9FnxoNNXhC8wKnkCa2nLuOZs+M9JVI+k95YXYkgWr4y 15LhoeVYWAyRZ8WwnMXMYYUQowWKE1cq7dDXOMJlXmX5Y/9+lX6bHgLooFQLmYM9ePG8 2VgiEBA0XhupFUAPtEff1twAHw+5euU2nMwe53XP9M44BtgsiYKqeSej4RHKGb5WKtiv jbrzfxmuBYvLqCQOT5jXN9ismTwbCOYY7/ZRlFMfClZduDk3/mf9vIqa4edaMeYlXMPl AgEfP8IfIJN64m/yIq/La1gIwJ1vjhKQmeCXQuAD2NaohLQpbBsgPAPg/IKUzNLWpak9 ulfQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1702729805; x=1703334605; 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=ocjvWUi8TbVP+QzARAes3gUc8yTR2ZW+bpq57hOj6AQ3JJhoagWJ6VwVAIVbi7ScBo +2i4WM4c1QiKTpK8jXAK5A5qfeQsxzx6ZbIo8/Mn65MS4yHAEGSZgGKYp3/SAsBrxz6l d/xAL7enLt2EBc5Y3+rAKTG02pOutb36WfOHU6j5YspvUmUwCKW6gyRcA0dP0tkjQ6MI wP/c0rom7B78YYhyqycRFcgBFCOXpDBi09hadux3ph9C3z4I1oVetaB/sMqJRPjSnxhs UPzDv+Q2+hKSjxHdgFiFgL0AhZib33w/VFI2UV8M2krz/6YYnCfpXcgYq0BGJ9RzPwD8 JEzQ== X-Gm-Message-State: AOJu0YzwyJOPvFSTZTnAsDxiL5lN+/602FhIzKcsItqgxhssYIPRXNb+ qAVa6nS1KU65q2sJTvxEp0q8k4gQBe8EoqOfXIw= X-Google-Smtp-Source: AGHT+IGYZTswU2HK06vCp3j/IB6r4PRrmxZ0FF+jhOPGKjOsCkhI72bT+70K2Wqg3v29Vh5g4X3uAA== X-Received: by 2002:a05:600c:3ca2:b0:40c:50d5:f7a6 with SMTP id bg34-20020a05600c3ca200b0040c50d5f7a6mr5245859wmb.121.1702729804401; Sat, 16 Dec 2023 04:30:04 -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 ts4-20020a170907c5c400b00a1cc1be1158sm11829412ejc.165.2023.12.16.04.30.03 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 16 Dec 2023 04:30:03 -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 v8 1/9] devlink: use devl_is_registered() helper instead xa_get_mark() Date: Sat, 16 Dec 2023 13:29:53 +0100 Message-ID: <20231216123001.1293639-2-jiri@resnulli.us> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20231216123001.1293639-1-jiri@resnulli.us> References: <20231216123001.1293639-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 Sat Dec 16 12:29:54 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jiri Pirko X-Patchwork-Id: 13495548 X-Patchwork-Delegate: kuba@kernel.org Received: from mail-ed1-f53.google.com (mail-ed1-f53.google.com [209.85.208.53]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 982B2250E9 for ; Sat, 16 Dec 2023 12:30:07 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=resnulli.us Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=resnulli.us 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="sDUadLNB" Received: by mail-ed1-f53.google.com with SMTP id 4fb4d7f45d1cf-553046ea641so294075a12.3 for ; Sat, 16 Dec 2023 04:30:07 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=resnulli-us.20230601.gappssmtp.com; s=20230601; t=1702729806; x=1703334606; 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=sDUadLNB+yPUqCinMzdrT5D3Ex9qO5dOFZNj7Lcic/1pp+3SJZy3HzHeVmTtQGLpMp aXAGwjF0uC2tLQ2Y0jcV4CNNMseNeoS2e4m5ixasHy1MoP3XS8R2520a762OBil7b0Wi Gi6jfgq9kp7l0NJtRcauhlEfi+XcV1jnaYn8xlLsh9204d53M8tQPVjf6noV183q9jbU ApPS4YfhJxCsKiQETkS0m3o6olUCYVnaXTckWwBJ8XvBNgD5HBkEbLm75jl46CPY2UnX Cp+A7rkXVKBqxKMHS4+XTQdOiVLW6HM/nCknrysxr1i6XHICw4XYGO4iSqr3/23+GSid RKHA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1702729806; x=1703334606; 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=xDb0uuq05pIyFoB6a2wTyc3PiDrAob5CN0gxM0lFuTCCEIxEfIiE82+/vDTo5sS6Le yXMkYNXIZ1kc4epiEMI0dqwFtGXlzYFCxnoA3LALFDhFv9RIwT1cYbBtqPMBT7lV+kmJ wGiLR9qKn95QSo0e/sEF7WCjONwjYoTdUZbW/NvBGwz/aANql5ER7/SXeqjTIUQP3qWF wsAQ9jlQZx2cW5FA9OssTqxrDJmhR4ap0r6Or5YlX+B1Tz363wuVvdb0pzxAcjORyyCO Q77Po3mFsFVeobbmNOctTlR32gTsBbL7BGofQ6/Z5kcOLHUUQVljmAKYyy6D+KY2UKTa zs4w== X-Gm-Message-State: AOJu0YyEd/zUoCR7BNqQxwvpUxXlL8MXi7CUvsCn2JbPD0127wydKrBO OaLGkkGGkIaYXEyIQCSnuBR9ooCPajLtYE/yJCg= X-Google-Smtp-Source: AGHT+IGO166Cj5w1r5+pdkWUTLPhlX1dfV8wXqdMruuoMn8uqddiEwY8w7IlVkqddgtVLvpWYOVJRA== X-Received: by 2002:a50:8a91:0:b0:553:fec:6d6c with SMTP id j17-20020a508a91000000b005530fec6d6cmr183932edj.79.1702729805960; Sat, 16 Dec 2023 04:30:05 -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 r17-20020aa7d591000000b0055315b9e7dbsm70480edq.74.2023.12.16.04.30.05 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 16 Dec 2023 04:30:05 -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 v8 2/9] devlink: introduce __devl_is_registered() helper and use it instead of xa_get_mark() Date: Sat, 16 Dec 2023 13:29:54 +0100 Message-ID: <20231216123001.1293639-3-jiri@resnulli.us> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20231216123001.1293639-1-jiri@resnulli.us> References: <20231216123001.1293639-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 Sat Dec 16 12:29:55 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jiri Pirko X-Patchwork-Id: 13495549 X-Patchwork-Delegate: kuba@kernel.org Received: from mail-wm1-f47.google.com (mail-wm1-f47.google.com [209.85.128.47]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id EFFB32D63C for ; Sat, 16 Dec 2023 12:30:09 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=resnulli.us Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=resnulli.us 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="qRYEUU5N" Received: by mail-wm1-f47.google.com with SMTP id 5b1f17b1804b1-40c31f18274so18855155e9.0 for ; Sat, 16 Dec 2023 04:30:09 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=resnulli-us.20230601.gappssmtp.com; s=20230601; t=1702729808; x=1703334608; 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=qRYEUU5N+vLXfZPzWxYdI07M9N5OBSQO3wzAw5yIonF3VuLTxr8MPLhL+rCv6YaRyP cGmh5QBMztzpyt9DKKBhF7OxY0LMscHVAOQZmb0PoqjUg7iub6xAgT37hUZjFSG/ivaj Mo7bv7LeurWoGkcp4l1JHv4JP5+/guOJy4UwUqevMkChdztzJOW5YIl8NFU8ESCMf6+5 IB7QYwumj5qLbEjrel8coHXxSvfNMxQoaqc/4lcxD3krG8ePnOWplW0vu5Htr4l1IWxa f5JevREymkBW4ty/0o0pYeiJEBUUBvg3g0Ci+Wqj6kn5YmChuBGfARdJX+j7OZVQtou1 GSZw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1702729808; x=1703334608; 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=q+oXeebNPxKKXHTTYJo6ovmhAC43zpuijBa+ZugucZ+8QaaTGVb9gh0i/cHEogt7F2 XicbUj4S2G9Psevn9PeRm8fFrwU+q/oNQxmyqkEGHAAT9OikO5ut+vZ0iJH323ReZu8m reXT/1rTDZR7lrJ359veFcW9+upaAkOpIShHXkyCm8IFxwNGYjo1SOYxAYWMTZa5CVjU NIHeR9a6yvv9RDJWU0xbCNg+flrgqqEbQfGQEEcFfMNG6plDP7W3NjIsEn6SOozF53Je kRdaQkyDGao1LQcZSg3ZbCho2K9hzR20fh/mjo9RxFU7uPKQiRxVxbJ1et+npkcH10ki egqA== X-Gm-Message-State: AOJu0Yx39GnS9oJYeV5HodXpSWIjBR1VhDFD0yRqzh5DUFhFwEzNUIMF 1jjXgUZe9fe1lefTi7AtJapFQoCvKkQa421MnUk= X-Google-Smtp-Source: AGHT+IF3fVUm6e8yFufg5pFTuJgTs9VZebew6UCS3E/u57lIVJUH1PVa8TFMCczUNZQC70/8qkq7ew== X-Received: by 2002:a05:600c:4f05:b0:40c:35b5:7020 with SMTP id l5-20020a05600c4f0500b0040c35b57020mr6411998wmq.134.1702729808053; Sat, 16 Dec 2023 04:30:08 -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 so7-20020a170907390700b00a1f747f762asm11153381ejc.112.2023.12.16.04.30.06 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 16 Dec 2023 04:30:06 -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 v8 3/9] devlink: send notifications only if there are listeners Date: Sat, 16 Dec 2023 13:29:55 +0100 Message-ID: <20231216123001.1293639-4-jiri@resnulli.us> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20231216123001.1293639-1-jiri@resnulli.us> References: <20231216123001.1293639-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 Sat Dec 16 12:29:56 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jiri Pirko X-Patchwork-Id: 13495550 X-Patchwork-Delegate: kuba@kernel.org Received: from mail-ej1-f52.google.com (mail-ej1-f52.google.com [209.85.218.52]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id ABB332D033 for ; Sat, 16 Dec 2023 12:30:11 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=resnulli.us Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=resnulli.us 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="p2ScuCsC" Received: by mail-ej1-f52.google.com with SMTP id a640c23a62f3a-a1db99cd1b2so189656266b.2 for ; Sat, 16 Dec 2023 04:30:11 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=resnulli-us.20230601.gappssmtp.com; s=20230601; t=1702729810; x=1703334610; 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=p2ScuCsCI2O8ogXBpYryXgnILlJD3o+1jAb7IPSplxrDWsbfW/90cGLuULgQDgUPtO zSnJxRd5LWtHRrhJ6p+v3i/WWtKz8F/xazvXUTThGM6Z3UyHyEbq+IRC7Buofm0HPFrw 762LgiAc/ZE3GGJZczfRqJaFwYK8Uuz8VNx5wF8P7fupxmWhmuFoGqLyDvo6IZ/NY1gw cJDwcIq8dDLFb6l8QGxIN3/lPexuha6qlDlCNqevEGz3iS8kafLeB1BJ63G3i/LbF5CL vt27xzbOCY38aal9n9qCmQAfhe+4LTgcwdW1xBSZ1kfXEpK0C7V9M2DUsZkJf2NKwznr 1NOw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1702729810; x=1703334610; 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=UXwJbVq0kZ840sZdS5fFJ4A7GLjTsTfq9HPswOeAauA5PP17zJFq3KEdmA9bIEUtTa F5gVoxxsuX98lI7PDu+AIg4Wgt+Lct6dWox48AG82F+sa0EMmOz5rnPpdAtExGqCpmUN r2AGOVIjlmOlsA64Tv3B9KWPmBhGd74btNjrAwwIGhFbWw760yu4d5Rd9AKDj/18k2SJ qb3Eo8Ieiv8zfb/AauOIUSlyUIDkseOUx/VF5QopaNMVErbpU+PLAQrNp52bDubMPaXy NLi116/Seswy9ejSRyjF2Of9hBxmuvUIbtuxgX7qPmTn0m6D0wIWUo0bbTP4mYqvsBs8 bjhg== X-Gm-Message-State: AOJu0Yz2FCy82PYOU2moz5SEJG+G4faj1xC6l6Rdhn1R6xOAv667KPJv BTIGFlz9iT4gOspagH6Z7rsW+eSOvOdeYakID6Y= X-Google-Smtp-Source: AGHT+IHmTMZlXuudtpk1Av+JApyVZeOb0/ryDXKeQ56dfLRQrVZaGIXf74AhgBGDOdHruN2G2fIepA== X-Received: by 2002:a17:906:3743:b0:a1a:8e58:9afa with SMTP id e3-20020a170906374300b00a1a8e589afamr4477811ejc.173.1702729809841; Sat, 16 Dec 2023 04:30:09 -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 ub27-20020a170907c81b00b00a1df4387f16sm11973174ejc.95.2023.12.16.04.30.09 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 16 Dec 2023 04:30:09 -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 v8 4/9] devlink: introduce a helper for netlink multicast send Date: Sat, 16 Dec 2023 13:29:56 +0100 Message-ID: <20231216123001.1293639-5-jiri@resnulli.us> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20231216123001.1293639-1-jiri@resnulli.us> References: <20231216123001.1293639-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 Sat Dec 16 12:29:57 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jiri Pirko X-Patchwork-Id: 13495551 X-Patchwork-Delegate: kuba@kernel.org Received: from mail-ed1-f50.google.com (mail-ed1-f50.google.com [209.85.208.50]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 869BD13B148 for ; Sat, 16 Dec 2023 12:30:13 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=resnulli.us Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=resnulli.us 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="btmQQ4/u" Received: by mail-ed1-f50.google.com with SMTP id 4fb4d7f45d1cf-54c5d041c23so1630839a12.2 for ; Sat, 16 Dec 2023 04:30:13 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=resnulli-us.20230601.gappssmtp.com; s=20230601; t=1702729812; x=1703334612; 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=MEaGRBLDuy8PazYC/YEpwSnUTDzBI+1qMZR7UgDTFjE=; b=btmQQ4/ukM/k7bCVYiQwWfhlwMgQF8pCGKEX+UcKoAL1TtEyFkQKtzPv8K16VaKtX1 BCsw4KyFDJllH44+AmRdIQ8jrLT7IWnFmxETYPG0tF0zDPvpXo56Swtrd/H7llwgP3Pd c9wt5zEhEmkSmC+HCZaQooDRptjivMM6FSL52Z0MBCDXBuBvFjhEfSGd9BQJ9Xjflnct P5pFT8PhOA1b+cWGPu72rtEla8JAon/PAyrOmcodfLQ16676ElVySNzzQfFwe/fdEwhU O1vdQPzcXNEjS3dGJicXAwcahlK7RiJYBz4vVEjDZV661dyplxT1VMxGr8VNZoOlFDQj lwCA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1702729812; x=1703334612; 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=MEaGRBLDuy8PazYC/YEpwSnUTDzBI+1qMZR7UgDTFjE=; b=tfTjX1Fijgug6EFrrktzECQT4TViHoT7w+jEF9Bo8Fr79IPMhw3bFEpkBD0KdxX8pj /LEEKznoBdwSeNCn97yzK+2tcjCRIAoVtY7wz2zeIjxSxpu5q2KER4iHQR26PqY7R3NB oq6Duprcj4k8mysfcOTwHF37xDLEmOvnPzed6uVMuYP0T5tNcH10Kb/f45qunxmkXNo7 Mxetjr25qXNOojZ0qtQA8WQVlIreWTIOWkJ1+9WHeYPzqCI6X1ksWgD7WCggGs831JGv 1TZG0Y1QwqIJ4ykg+DTP6hGyM4nCt/flJ8QqGstyzJq9asXho41jkS91M/RZ3HXgDZjF tqwQ== X-Gm-Message-State: AOJu0YyqWZPqmnpBurCo2Pfr/fsncYlfp8N05uQgIGOftpoaXa97pMTS tu2HNnbedJIeFvZLqjQQSb9POvlniIfe9awQ3YA= X-Google-Smtp-Source: AGHT+IEE2guXWrtaz3/TFwP0U5x5LgSannjZI/KSvxyA6b41EF5Vd/kcgkWUx9b4XsmEHM8JCtPz8w== X-Received: by 2002:a50:cc0b:0:b0:550:4c6f:7ba9 with SMTP id m11-20020a50cc0b000000b005504c6f7ba9mr3343982edi.102.1702729811663; Sat, 16 Dec 2023 04:30:11 -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 cu3-20020a170906ba8300b00a1d818ebcadsm12007427ejd.19.2023.12.16.04.30.10 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 16 Dec 2023 04:30:11 -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 v8 5/9] genetlink: introduce per-sock family private storage Date: Sat, 16 Dec 2023 13:29:57 +0100 Message-ID: <20231216123001.1293639-6-jiri@resnulli.us> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20231216123001.1293639-1-jiri@resnulli.us> References: <20231216123001.1293639-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 an xarray for Generic netlink family to store per-socket private. Initialize this xarray only if family uses per-socket privs. Introduce genl_sk_priv_get() to get the socket priv pointer for a family and initialize it in case it does not exist. Introduce __genl_sk_priv_get() to obtain socket priv pointer for a family under RCU read lock. Allow family to specify the priv size, init() and destroy() callbacks. Signed-off-by: Jiri Pirko --- v7->v8: - changed __genl_sk_priv_get() to return ERR_PTR() encoded pointer in case of a family bug v6->v7: - converted family->sock_priv_list to family->sock_privs xarray and use it to store the per-socket privs, use sock pointer as an xarrar index. This made the code much simpler - removed no longer needed struct genl_sock and related code as the priv is stored in family xarray only - removed sk_priv wrapper struct as destroy() is available through family pointer during priv_free() call, store void *priv directly into xarray - change the genl_release() to iterate over families to free privs - changed xa_cmpxchg() error flow in genl_sk_priv_get() - updated __genl_sk_priv_get() and genl_sk_priv_get() function comments accordingly - swapped __genl_sk_priv_get() and genl_sk_priv_get() args to better fit the changed lookup scheme - updated patch description 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 | 11 +++ net/netlink/genetlink.c | 144 +++++++++++++++++++++++++++++++++++++++- 2 files changed, 154 insertions(+), 1 deletion(-) diff --git a/include/net/genetlink.h b/include/net/genetlink.h index c53244f20437..6bc37f392a9a 100644 --- a/include/net/genetlink.h +++ b/include/net/genetlink.h @@ -51,6 +51,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 +87,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 xarray *sock_privs; }; /** @@ -298,6 +307,8 @@ static inline bool genl_info_is_ntf(const struct genl_info *info) return !info->nlhdr; } +void *__genl_sk_priv_get(struct genl_family *family, struct sock *sk); +void *genl_sk_priv_get(struct genl_family *family, struct sock *sk); 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/genetlink.c b/net/netlink/genetlink.c index 9c7ffd10df2a..c0d15470a10b 100644 --- a/net/netlink/genetlink.c +++ b/net/netlink/genetlink.c @@ -631,6 +631,138 @@ static int genl_validate_ops(const struct genl_family *family) return 0; } +static void *genl_sk_priv_alloc(struct genl_family *family) +{ + void *priv; + + priv = kzalloc(family->sock_priv_size, GFP_KERNEL); + if (!priv) + return ERR_PTR(-ENOMEM); + + if (family->sock_priv_init) + family->sock_priv_init(priv); + + return priv; +} + +static void genl_sk_priv_free(const struct genl_family *family, void *priv) +{ + if (family->sock_priv_destroy) + family->sock_priv_destroy(priv); + kfree(priv); +} + +static int genl_sk_privs_alloc(struct genl_family *family) +{ + if (!family->sock_priv_size) + return 0; + + family->sock_privs = kzalloc(sizeof(*family->sock_privs), GFP_KERNEL); + if (!family->sock_privs) + return -ENOMEM; + xa_init(family->sock_privs); + return 0; +} + +static void genl_sk_privs_free(const struct genl_family *family) +{ + unsigned long id; + void *priv; + + if (!family->sock_priv_size) + return; + + xa_for_each(family->sock_privs, id, priv) + genl_sk_priv_free(family, priv); + + xa_destroy(family->sock_privs); + kfree(family->sock_privs); +} + +static void genl_sk_priv_free_by_sock(struct genl_family *family, + struct sock *sk) +{ + void *priv; + + if (!family->sock_priv_size) + return; + priv = xa_erase(family->sock_privs, (unsigned long) sk); + if (!priv) + return; + genl_sk_priv_free(family, priv); +} + +static void genl_release(struct sock *sk, unsigned long *groups) +{ + struct genl_family *family; + unsigned int id; + + down_read(&cb_lock); + + idr_for_each_entry(&genl_fam_idr, family, id) + genl_sk_priv_free_by_sock(family, sk); + + up_read(&cb_lock); +} + +/** + * __genl_sk_priv_get - Get family private pointer for socket, if exists + * + * @family: family + * @sk: socket + * + * Lookup a private memory for a Generic netlink family and specified socket. + * + * Caller should make sure this is called in RCU read locked section. + * + * Return: valid pointer on success, otherwise negative error value + * encoded by ERR_PTR(), NULL in case priv does not exist. + */ +void *__genl_sk_priv_get(struct genl_family *family, struct sock *sk) +{ + if (WARN_ON_ONCE(!family->sock_privs)) + return ERR_PTR(-EINVAL); + return xa_load(family->sock_privs, (unsigned long) sk); +} + +/** + * genl_sk_priv_get - Get family private pointer for socket + * + * @family: family + * @sk: socket + * + * Lookup a private memory for a Generic netlink family and specified socket. + * Allocate the private memory in case it was not already done. + * + * Return: valid pointer on success, otherwise negative error value + * encoded by ERR_PTR(). + */ +void *genl_sk_priv_get(struct genl_family *family, struct sock *sk) +{ + void *priv, *old_priv; + + priv = __genl_sk_priv_get(family, sk); + if (priv) + return 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->sock_privs, (unsigned long) sk, NULL, + priv, GFP_KERNEL); + if (old_priv) { + genl_sk_priv_free(family, priv); + if (xa_is_err(old_priv)) + return ERR_PTR(xa_err(old_priv)); + /* Race happened, priv for the socket was already inserted. */ + return old_priv; + } + return priv; +} + /** * genl_register_family - register a generic netlink family * @family: generic netlink family @@ -659,6 +791,10 @@ int genl_register_family(struct genl_family *family) goto errout_locked; } + err = genl_sk_privs_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 +815,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_sk_privs_free; } err = genl_validate_assign_mc_groups(family); @@ -698,6 +834,8 @@ int genl_register_family(struct genl_family *family) errout_remove: idr_remove(&genl_fam_idr, family->id); +errout_sk_privs_free: + genl_sk_privs_free(family); errout_locked: genl_unlock_all(); return err; @@ -728,6 +866,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_privs_free(family); + genl_unlock(); genl_ctrl_event(CTRL_CMD_DELFAMILY, family, NULL, 0); @@ -1708,6 +1849,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 Sat Dec 16 12:29:58 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jiri Pirko X-Patchwork-Id: 13495552 X-Patchwork-Delegate: kuba@kernel.org Received: from mail-ej1-f51.google.com (mail-ej1-f51.google.com [209.85.218.51]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 3311B17729 for ; Sat, 16 Dec 2023 12:30:14 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=resnulli.us Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=resnulli.us 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="aKEFYbfk" Received: by mail-ej1-f51.google.com with SMTP id a640c23a62f3a-a1ca24776c3so500157066b.0 for ; Sat, 16 Dec 2023 04:30:14 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=resnulli-us.20230601.gappssmtp.com; s=20230601; t=1702729813; x=1703334613; 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=gKm4VYXW541UD9zLfrkWeMQGaEXyfaC2EWpyopTpM68=; b=aKEFYbfkSU8oCJo6+ls6BrnP3RTK2MZ9cRriGQUjIgrLV11dJl+8bafJYOiS7eGuCQ vSD5XNk58xdT/Lik9cw9R5TH74VtYXsBo4f/s5YFf1HQ7vou/9bJqHTnIV0beAcRms5N CcJARb5rI1SKMlgZ6/Md02FW6XfZhsuI4olXSEne6vJBqPVfAmw/6alCYYtsFPv3ZzRv AuHXR3efUBZtJFh4A7P+x1Et2acFqT+PqNzx9/RAkoZm4fnbk1PtpmYKesxA12BpL2EO KMYGvDzNJBH4vTzvfINdyx92+Wb+yB+CBrzCxdrtXvC3wNL4x2jvXIJc8OkbsVjJjEcr A/fQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1702729813; x=1703334613; 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=gKm4VYXW541UD9zLfrkWeMQGaEXyfaC2EWpyopTpM68=; b=uTLnspUK2Odmc3wLGLOBY23jEIwTm0nWe6ZNd+M8gHak9LHAZK+PL1Rcv8jLMQ1mmX hQx757o6+X8n8oIUNmCr5ZZlPZ3sfP+RZC9mqrZrbS8IZns1BMpeCN2cIsnEpRFoAGT4 UlXjfp49D6T+aZSweSCX2yFWEdWtdw8WTGZ+x98IbspDKgs2Y2vUY5EWarLs9FFjLo5o 5v7IZUyFoEWB0zyO0jSiSG2OqHk1Ee+VWazQRmx2TgtIUE33f87ekhwHZNfswjYyrb6w 68wNto0jGEMQ9EQrL9XBlBL70M8vxfwSE89PQvjcQSC/4JAewKWhbF80RRbHf5mWShFg tfPg== X-Gm-Message-State: AOJu0YwFm6UYePzJdZjr9XrGgwsneYG8e08K36zzKnKvf9rqIWrw2PNk sBpaSG4Oauz13XvUPGAs+5nU2NZ5QKKjv3P53w8= X-Google-Smtp-Source: AGHT+IG6whLFcdkgREjLq0CqpYHMF3jVuGmeC9kbxWoNnbbr4rbU45OOR/TywD4qlQod2wbjPBAS1Q== X-Received: by 2002:a17:907:8d8:b0:a1c:7c86:8b79 with SMTP id zu24-20020a17090708d800b00a1c7c868b79mr11485633ejb.26.1702729813522; Sat, 16 Dec 2023 04:30:13 -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 vw12-20020a170907a70c00b00a1c904675cfsm11755535ejc.29.2023.12.16.04.30.12 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 16 Dec 2023 04:30:12 -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 v8 6/9] netlink: introduce typedef for filter function Date: Sat, 16 Dec 2023 13:29:58 +0100 Message-ID: <20231216123001.1293639-7-jiri@resnulli.us> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20231216123001.1293639-1-jiri@resnulli.us> References: <20231216123001.1293639-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 177126fb0484..4ed8ffd58ff3 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 Sat Dec 16 12:29:59 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jiri Pirko X-Patchwork-Id: 13495553 X-Patchwork-Delegate: kuba@kernel.org Received: from mail-wr1-f52.google.com (mail-wr1-f52.google.com [209.85.221.52]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 072EB18EA9 for ; Sat, 16 Dec 2023 12:30:16 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=resnulli.us Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=resnulli.us 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="j7QU/s1F" Received: by mail-wr1-f52.google.com with SMTP id ffacd0b85a97d-334af3b3ddfso1189014f8f.3 for ; Sat, 16 Dec 2023 04:30:16 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=resnulli-us.20230601.gappssmtp.com; s=20230601; t=1702729815; x=1703334615; 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=dbTUWeD75Qq/q03fxzPY6vzlSe3qJgsO2ERBgJjGn4U=; b=j7QU/s1F13BYAQ/3tigXxdavbt4/Tli6QnfXK44R5LJ1n5GlrUv45WysxH3TcNFLcb KFLde47JdPHabT0xWVBdR8D6J5O/DRr/d10RM/wbEj0PYjgWL9HTVgvsz3fOwdBS5xTQ 7r68nrE6Xhw5KQBpUIQdRCan3oQ7+hisYsYO4Br2ypmxlfqEP/LvevbSgGqJPJHVQi9Q hIy4TrZ79fz8d64iSou7O3N4prB0C24jOIJ3Adz0xLhKQMMmxpN22gLQkUvdhtsnQFj7 4o9ilWx+r8oFc0zfI4aQmfH9Tkiv/O3e9Sz7F4r5qQx33kNiUoMf7eMHIzKWN9v0YpP9 0iHg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1702729815; x=1703334615; 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=dbTUWeD75Qq/q03fxzPY6vzlSe3qJgsO2ERBgJjGn4U=; b=KZwlPz8Y/yNMapdSNmtobVq44LwsnD7+849dN2H+cmZN1qd4efCkqt6lv60cADbXXP YtpdrAOpdrEUWnRtR34yvwxdKhwA9+TapIGz8V7kVJWoWZskdpRLrj1R1mvChQcGHjPX Q8Yb+Lsylsam8w6lowOHgfbnlYQgniPXZgiLPNDAeBFdDx1DcfQUxNDspQz04y5Go8i6 lcyAgmBM3OT7bCYZ8NglAg1YI/EEMiZezFWk8Z/wA4VXGWXlvPa5dnPuvnFj1yyhjRBy XYp5uXkgTp+9NaPDxW0A2mh74h2ZMwdzMbrGG3QRHwJ7u8/+llqQRB6aiE5TW0MKe+FV qk3g== X-Gm-Message-State: AOJu0Yy3SL4PoDmGRPjl0fquGjdpAlCx9qwoymXC9gGYNZFtiKcO+tad 71e1PVH+CPQu/SzdWNDq06orBt3geHeiUm4XoIQ= X-Google-Smtp-Source: AGHT+IFWTe45MqdZjukyoFjoaldo1SC5XH5Rl7j0Al77R+XVJjiGc7iMjEwRlngZ2v5hBIij0Bb47A== X-Received: by 2002:a5d:4603:0:b0:336:4b33:a919 with SMTP id t3-20020a5d4603000000b003364b33a919mr1841156wrq.50.1702729815278; Sat, 16 Dec 2023 04:30:15 -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 et14-20020a056402378e00b005527cfaa2dfsm2291644edb.49.2023.12.16.04.30.14 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 16 Dec 2023 04:30:14 -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 v8 7/9] genetlink: introduce helpers to do filtered multicast Date: Sat, 16 Dec 2023 13:29:59 +0100 Message-ID: <20231216123001.1293639-8-jiri@resnulli.us> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20231216123001.1293639-1-jiri@resnulli.us> References: <20231216123001.1293639-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 6bc37f392a9a..85c63d4f16dd 100644 --- a/include/net/genetlink.h +++ b/include/net/genetlink.h @@ -448,6 +448,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 @@ -461,10 +490,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 Sat Dec 16 12:30:00 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jiri Pirko X-Patchwork-Id: 13495554 X-Patchwork-Delegate: kuba@kernel.org Received: from mail-lj1-f181.google.com (mail-lj1-f181.google.com [209.85.208.181]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id E644F2C6A9 for ; Sat, 16 Dec 2023 12:30:18 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=resnulli.us Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=resnulli.us 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="lYilA93w" Received: by mail-lj1-f181.google.com with SMTP id 38308e7fff4ca-2cc5d9cf766so5769871fa.2 for ; Sat, 16 Dec 2023 04:30:18 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=resnulli-us.20230601.gappssmtp.com; s=20230601; t=1702729817; x=1703334617; 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=UINFUKQqwJpfrW26h1br3k+kqi4ue0hCECbqcTFenn0=; b=lYilA93wVYT3eihOWk/qz4XLLp7s1skez/jx0e8l3cCHeVPg3KtvbX/nMvGl5FqMyP VjA/HTSLloJFlV0mmH9T2tLD7ysMZ+8JZS1IYDCS7pgjBzR+qXh82ZL1StV/Ove6/2aO j5slCbhqB1cqTbUYvMqlmU2AEALnP+hq3xff96ArpV55nOqFOqAm6EmRAyPzM7cwiDYt nTvnNBsM+JoAQJ8WCjTwFRDhO19Mbn1cE/i5yPn4eLGK5Y4lbEYxzXqtIX6D7hvICQbc 5F59uC/NIf7umHO5lyC8Uh+M5cIGkq8K+oU3IHnKZoOErH40FOzzPbxW/ZfgaGWoNNQ4 gL8Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1702729817; x=1703334617; 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=UINFUKQqwJpfrW26h1br3k+kqi4ue0hCECbqcTFenn0=; b=W/1dcqYCXX6Ot4oHbFV1Ng1ja1WQnqFAS7mbw01bc4LGT0+MdgSDVodTHRh4U4tpqf iUBb8FfSMKNgD8pZYYjtev0m8CaozHCQCGtRbnZqmVcMd0TGjvqUK2W0xiCORmzKZiIZ cXXtfXkfyoEavS6ZbvLv5SLpGVQ1GKVf67uJqL+Kbtxu9lwk4szgyAc2sLYtbjYQk55/ eLqPJM48mk1umD7gpQEbJDtXYfjK+kIV6tLzVlyCCbHugWg9fBUoDd1TbMUhFJEMoweg Qhq/4jCIqVsXbYo0t5/93o8opCsgVgTiO79KWz7+GNJwxmFiB8eyBbs3Ecmjnw+Gm81W VDoQ== X-Gm-Message-State: AOJu0YyaLLvhSzNYCeswj7uszygv+WMkTTWchPY3qiu811Qqtf/ZPuiL QgNGVtUAp/dlfE56kYT0BbDJ458JpydE+GhlMJU= X-Google-Smtp-Source: AGHT+IEmQ7QWZ3tfnPtr0Lpkx20E/2KGHBsVRSK8DKXGQMiHsQPIPNowZ48F0dfEs/k8C5RUMWVhng== X-Received: by 2002:a05:6512:2242:b0:50d:1a17:b69c with SMTP id i2-20020a056512224200b0050d1a17b69cmr6895643lfu.52.1702729816901; Sat, 16 Dec 2023 04:30:16 -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 tp5-20020a170907c48500b00a2304386f4bsm4632349ejc.211.2023.12.16.04.30.16 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 16 Dec 2023 04:30:16 -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 v8 8/9] devlink: add a command to set notification filter and use it for multicasts Date: Sat, 16 Dec 2023 13:30:00 +0100 Message-ID: <20231216123001.1293639-9-jiri@resnulli.us> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20231216123001.1293639-1-jiri@resnulli.us> References: <20231216123001.1293639-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 --- v7->v8: - rebased on top of __genl_sk_priv_get() return value change v6->v7: - rebased on top of *genl_sk_priv_get() args swap 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..3176be2585cb 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(&devlink_nl_family, NETLINK_CB(skb).sk); + 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(&devlink_nl_family, dsk); + if (!IS_ERR_OR_NULL(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 Sat Dec 16 12:30:01 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jiri Pirko X-Patchwork-Id: 13495555 X-Patchwork-Delegate: kuba@kernel.org Received: from mail-ej1-f53.google.com (mail-ej1-f53.google.com [209.85.218.53]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 966A7D279 for ; Sat, 16 Dec 2023 12:30:20 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=resnulli.us Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=resnulli.us 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="ZmKCtsBv" Received: by mail-ej1-f53.google.com with SMTP id a640c23a62f3a-a22f59c6aeaso178599866b.2 for ; Sat, 16 Dec 2023 04:30:20 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=resnulli-us.20230601.gappssmtp.com; s=20230601; t=1702729819; x=1703334619; 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=SksabRRuzPhq1bHXhenDAYEiuuMN4zcmCaZ26jqamMQ=; b=ZmKCtsBvqkGuoTkkpnedf5kX4vvoCCmYtcLY8mBfpuM41t41pUO6thrzoaFEbrOoZI bYvzEH3Lf1z7xC/4BdCjAbu6pMsfVRDttvBNFyHRr7V6usOsJheHpw0HCaG1qchWTMGR mUpGPD5aTkRPZtnNJo8fzxvupwVSPq55+kpItib+NfLoRjmEPV0DLchGVx+m5+1Hoahg ErUhD8HAD4UrGA5sJkZSsF+xEsoKa5fqdFtBDkcyiykYm4WHByCwwuF3mWQL5uECPZm0 9jsJ+Y35rc++y1yMvn/iYhF8mjzBY/RIjxVVtAzGCQVXfLfE1VPDrTQwrdN8VDZGPbVQ V2Ug== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1702729819; x=1703334619; 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=SksabRRuzPhq1bHXhenDAYEiuuMN4zcmCaZ26jqamMQ=; b=q008FGPz9fQGihQVcOpIRB3bbYXL3dzoN3OhpTENnVIBf1YB6k98LOcwBLscp7Hhjp lfT1bGNtOXUk7Y9W97UHuqi7KI0lsP31n/ZbJzK2Su6KxcTYVdQhzpvpLzlS0lWgU6dn 1rNdvt1y3WvOEmUF9yXFpcnWZ9qapRdqVyDR0c2GO7b6ZWKlUAJ/ajVnd/nLsXkfdCT7 V77kPcQ/XdOM+N59z9oNeyCYqEkHmxkU7ovgi8AVM+DGpRoJKRW/Leu80RiVH/0zXvif 6NWvYhvxi1QUjSNGmyUdxrKSGh+SEHsw3tldps7HMPRSLMfEBQIqlx+NMuo9uWI/OTc7 eZQg== X-Gm-Message-State: AOJu0YyQn66SsmFaZKEdpreWo3S1RIWjQw1v3u7k803fKI8+5FyYB3vs 9TdGgt4dQSEmCVa2EnOHpDTf9kM+M7qjB+W3kAM= X-Google-Smtp-Source: AGHT+IERKe18EvAXbiO9PNmhpWCThaVa3jDPdPsj/V9V+jnF671Of9sKZQhft/IQpQIJ7GurFFC7Fw== X-Received: by 2002:a17:906:2097:b0:a1f:a0c8:750 with SMTP id 23-20020a170906209700b00a1fa0c80750mr5103548ejq.53.1702729818905; Sat, 16 Dec 2023 04:30:18 -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 vx6-20020a170907a78600b00a1e852ab3f0sm11985622ejc.15.2023.12.16.04.30.18 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 16 Dec 2023 04:30:18 -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 v8 9/9] devlink: extend multicast filtering by port index Date: Sat, 16 Dec 2023 13:30:01 +0100 Message-ID: <20231216123001.1293639-10-jiri@resnulli.us> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20231216123001.1293639-1-jiri@resnulli.us> References: <20231216123001.1293639-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 3176be2585cb..499885c8b9ca 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,