From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from BN1PR04CU002.outbound.protection.outlook.com (mail-eastus2azon11010031.outbound.protection.outlook.com [52.101.56.31]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 7E690361DB2 for ; Wed, 7 Jan 2026 17:05:16 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=52.101.56.31 ARC-Seal:i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1767805518; cv=fail; b=TBVtrGqs2GNPyBkroMSYWY69Yd7GuM9Wxj681R3BvF6R2GPDa8d5XrUSsoPOxBNOUwg9+tQFy0EnbCpF9kMkAlLgimPtipC1rcXYP01kE2OdCS0xj6bpz9hJoKce2s+ZYoONXKn2pZ7QZciBLXecXxhuzyHlOIwVEf4L5N0zR1Q= ARC-Message-Signature:i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1767805518; c=relaxed/simple; bh=Mk4EvueWGirVruhunaVE6kFRDMU+hPaPpB5rNfhm7Rw=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=Ew+NlnK7ke0ob1xM47lc2Jpx9wCNug6UWasQ3JmVWoLQRFjzSNP9/KToTX29AiWzWfEADqMk1wZwdTGCeVzrja5vAnUa6/w4sySsWL1M3tNf0eE3MJmGFxWWSh9iT+donaLbWj9+FcPojRrVrRhXlnpMUivhgVwK3WkptYIkOYk= ARC-Authentication-Results:i=2; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=nvidia.com; spf=fail smtp.mailfrom=nvidia.com; dkim=pass (2048-bit key) header.d=Nvidia.com header.i=@Nvidia.com header.b=RDTE+2wH; arc=fail smtp.client-ip=52.101.56.31 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=nvidia.com Authentication-Results: smtp.subspace.kernel.org; spf=fail smtp.mailfrom=nvidia.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=Nvidia.com header.i=@Nvidia.com header.b="RDTE+2wH" ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=Dmw2xXTZtokRi49iEUO6+WbRh68jKgQJMHceiCSxue2MI0kmP8Ve9/qCm/6yWbu8JlNwwT9Wm8UNzJ9ZmGRtYalXWEfOzWhYsbWU/WCfba3xHKDAjXfSR6JxOOyc02eJP6tqNS13rASlEFhSHKBg7wZz6MT+GjWEUH5NthjUy6A1/EBCelJJ5GUW8CYyLW9i/g9gk572kngWGcz+pVMkzwVWr4qQ1xvDDkPJMOre7TwwWAGC4lD8ZYZKhtxvWGUSKyGzN0xpacNiiIDYm92hSN+C/EnvqN5r+TvjGdeUc6zjJLA0e2B1rRcHY4V4xXhMs+zZu+twFFnaWZZJwr6ckw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector10001; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=3ZQmW6Ii/wBMkYK2JLjcwW3ap6UUzqClh2wMfgA9LHM=; b=nxcvtdmoVPEF+VOMJ8PFnh1YbUojZdFTUP6dHu2u3LWFm/EXqHVyA84M9ku+yvvvTrJ2p9E9KnCDA2qBi+DlI6jUXslYSzvUI2mx7sIvULNf9n2vY3UlLBCK8iuZsPAq0HLvfjwC+VkJxoE/glHcPjrpO7g+uzfhjszc+c6MCjuuIy6touwfjHrAcyXs13jvgazb8HZrO+TPyEoMnIbpOpJvpiE+bjB7k8+Ki8ZsNq5V9Lrhnta1dY4FLz0/77wHFx38PjvVKKoBQ4H/v8YckTQ0oi6hpXW8/i2vYQmFk4XAWAK9c8YSGJHjyX8IaUI239FCUg7ZpPLk+dOxrbV7PA== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 216.228.118.233) smtp.rcpttodomain=vger.kernel.org smtp.mailfrom=nvidia.com; dmarc=pass (p=reject sp=reject pct=100) action=none header.from=nvidia.com; dkim=none (message not signed); arc=none (0) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=Nvidia.com; s=selector2; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=3ZQmW6Ii/wBMkYK2JLjcwW3ap6UUzqClh2wMfgA9LHM=; b=RDTE+2wHIcXzcCysNUaseqxZQ/c/Ofbutg3G8ZES02fFnGEGncvYshon+lTaPmbVzIRu57f3XsG7ilHzA70jTqkmBcRup8astPATsIw0bjuYKXe9oOpoh3uA8ZXMvVg0aulwAZBF5CdwScXOacwL7/MVZp/1k6CwEUJDb7HV95XqpLzU5qy1d0i4wiZ+ytaIjanZ9/r1BQPvRu2uyp/48S5LUUtrpjfFoblJrUKWSGh2CgwN6B121tZ+WUmDdZh6q/Qqfn7pZy1L2aYD6xuRvc+SZPoeW7acq89nvaJSnttZZVBla/hoyY+KD17sCKk9D4HSxje/lygZSlm49Ko+PA== Received: from MW4PR04CA0253.namprd04.prod.outlook.com (2603:10b6:303:88::18) by CH2PR12MB9457.namprd12.prod.outlook.com (2603:10b6:610:27c::7) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.9478.4; Wed, 7 Jan 2026 17:05:05 +0000 Received: from CO1PEPF000044F7.namprd21.prod.outlook.com (2603:10b6:303:88:cafe::17) by MW4PR04CA0253.outlook.office365.com (2603:10b6:303:88::18) with Microsoft SMTP Server (version=TLS1_3, cipher=TLS_AES_256_GCM_SHA384) id 15.20.9478.5 via Frontend Transport; Wed, 7 Jan 2026 17:05:04 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 216.228.118.233) smtp.mailfrom=nvidia.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=nvidia.com; Received-SPF: Pass (protection.outlook.com: domain of nvidia.com designates 216.228.118.233 as permitted sender) receiver=protection.outlook.com; client-ip=216.228.118.233; helo=mail.nvidia.com; pr=C Received: from mail.nvidia.com (216.228.118.233) by CO1PEPF000044F7.mail.protection.outlook.com (10.167.241.197) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.9520.0 via Frontend Transport; Wed, 7 Jan 2026 17:05:04 +0000 Received: from drhqmail202.nvidia.com (10.126.190.181) by mail.nvidia.com (10.127.129.6) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.2562.20; Wed, 7 Jan 2026 09:04:40 -0800 Received: from drhqmail202.nvidia.com (10.126.190.181) by drhqmail202.nvidia.com (10.126.190.181) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.2562.20; Wed, 7 Jan 2026 09:04:40 -0800 Received: from vdi.nvidia.com (10.127.8.14) by mail.nvidia.com (10.126.190.181) with Microsoft SMTP Server id 15.2.2562.20 via Frontend Transport; Wed, 7 Jan 2026 09:04:39 -0800 From: Daniel Jurgens To: , , , CC: , , , , , , , , , , , "Daniel Jurgens" Subject: [PATCH net-next v15 08/12] virtio_net: Use existing classifier if possible Date: Wed, 7 Jan 2026 11:04:18 -0600 Message-ID: <20260107170422.407591-9-danielj@nvidia.com> X-Mailer: git-send-email 2.50.1 In-Reply-To: <20260107170422.407591-1-danielj@nvidia.com> References: <20260107170422.407591-1-danielj@nvidia.com> Precedence: bulk X-Mailing-List: virtualization@lists.linux.dev List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Content-Type: text/plain X-NV-OnPremToCloud: ExternallySecured X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: CO1PEPF000044F7:EE_|CH2PR12MB9457:EE_ X-MS-Office365-Filtering-Correlation-Id: 987c5608-8255-43be-ace7-08de4e0ee6e2 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|1800799024|82310400026|7416014|376014|36860700013; X-Microsoft-Antispam-Message-Info: =?us-ascii?Q?7PfU663CvM07uEnx28KXcnjnLKTXXBs6wLZhRWolxrTWR2jwOipxD+UvWGDJ?= =?us-ascii?Q?IHvCkWpfxVdPY4ueIkuqQBFuY4Yn1fZXErhpR9WjcczdphOuoDeFcFn7NPc8?= =?us-ascii?Q?zupGAnPQpTuEciL7BuOmUvS4ZFHMm+a2TxmhyaUYbdRL8oe9So4C3Xvh3Pa7?= =?us-ascii?Q?OThMabR2Z3ZWNPqrgQeAo3kobKnHD239WvECyp3DDRr9JNd3hXJlyNW5LXR1?= =?us-ascii?Q?2gkZRxT04kU2DQom5ur5pr//iYD7baWtd/Nxrc5mAYLdoAgJBUzk6gjGSvAE?= =?us-ascii?Q?Y39o2v5AmsPekIh9NcXF5XAW7hJqweqXPMXwqFtRdgvSUnwoGg1GIHdSBEzI?= =?us-ascii?Q?mT1xADm+b1Xgwkd8j+Bhht5E7JGc82NkPWgU+erGSfalUwplwRa8EqVVoIh0?= =?us-ascii?Q?sYVxvb5HBUtBSerOfyclUe+oLPkNk1epa5ncC79P5/wkFgdtshPwgxNA/tEW?= =?us-ascii?Q?AAteMQu7w8T29dOOs2bbw2nzQgJDd2HwT4SNIZPApaq3kTzZ1MuAk9QaPGDh?= =?us-ascii?Q?Z/PlKbh6aKnuVWvLg1iemI/EtGiEmbhKQj8wnUN4wkFVTBAJUQFThUCD9MKQ?= =?us-ascii?Q?03NNOsHYQxWDqUwzjbngcEtXevmUfesBIu6yX/hm/bw36ZK5ZEuzDzx8EtTb?= =?us-ascii?Q?9IyN9GqvzBTffEeGIAdyA0B2RGSEz05QHj22eV48ArT5Nqs5Bb5myknKGpRg?= =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-Forefront-Antispam-Report: CIP:216.228.118.233;CTRY:US;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:mail.nvidia.com;PTR:dc7edge2.nvidia.com;CAT:NONE;SFS:(13230040)(1800799024)(82310400026)(7416014)(376014)(36860700013);DIR:OUT;SFP:1101; X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 07 Jan 2026 17:05:04.2386 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 987c5608-8255-43be-ace7-08de4e0ee6e2 X-MS-Exchange-CrossTenant-Id: 43083d15-7273-40c1-b7db-39efd9ccc17a X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=43083d15-7273-40c1-b7db-39efd9ccc17a;Ip=[216.228.118.233];Helo=[mail.nvidia.com] X-MS-Exchange-CrossTenant-AuthSource: CO1PEPF000044F7.namprd21.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: CH2PR12MB9457 Classifiers can be used by more than one rule. If there is an existing classifier, use it instead of creating a new one. If duplicate classifiers are created it would artifically limit the number of rules to the classifier limit, which is likely less than the rules limit. Signed-off-by: Daniel Jurgens Reviewed-by: Parav Pandit Reviewed-by: Shahar Shitrit Reviewed-by: Xuan Zhuo --- v4: - Fixed typo in commit message - for (int -> for ( v8: - Removed unused num_classifiers. Jason Wang v12: - Clarified comment about destroy_classifier freeing. MST - Renamed the classifier field of virtnet_classifier to obj. MST - Explained why in commit message. MST v13: - Fixed comment about try_destroy_classifier. MST --- --- drivers/net/virtio_net.c | 51 ++++++++++++++++++++++++++-------------- 1 file changed, 34 insertions(+), 17 deletions(-) diff --git a/drivers/net/virtio_net.c b/drivers/net/virtio_net.c index a34bd98a5a6c..cc7de2a3989f 100644 --- a/drivers/net/virtio_net.c +++ b/drivers/net/virtio_net.c @@ -32,6 +32,7 @@ #include #include #include +#include static int napi_weight = NAPI_POLL_WEIGHT; module_param(napi_weight, int, 0444); @@ -302,7 +303,6 @@ struct virtnet_ff { struct virtio_net_ff_cap_mask_data *ff_mask; struct virtio_net_ff_actions *ff_actions; struct xarray classifiers; - int num_classifiers; struct virtnet_ethtool_ff ethtool; }; @@ -5826,12 +5826,13 @@ struct virtnet_ethtool_rule { /* The classifier struct must be the last field in this struct */ struct virtnet_classifier { size_t size; + refcount_t refcount; u32 id; - struct virtio_net_resource_obj_ff_classifier classifier; + struct virtio_net_resource_obj_ff_classifier obj; }; static_assert(sizeof(struct virtnet_classifier) == - ALIGN(offsetofend(struct virtnet_classifier, classifier), + ALIGN(offsetofend(struct virtnet_classifier, obj), __alignof__(struct virtnet_classifier)), "virtnet_classifier: classifier must be the last member"); @@ -5919,11 +5920,24 @@ static bool validate_mask(const struct virtnet_ff *ff, return false; } -static int setup_classifier(struct virtnet_ff *ff, struct virtnet_classifier *c) +static int setup_classifier(struct virtnet_ff *ff, + struct virtnet_classifier **c) { + struct virtnet_classifier *tmp; + unsigned long i; int err; - err = xa_alloc(&ff->classifiers, &c->id, c, + xa_for_each(&ff->classifiers, i, tmp) { + if ((*c)->size == tmp->size && + !memcmp(&tmp->obj, &(*c)->obj, tmp->size)) { + refcount_inc(&tmp->refcount); + kfree(*c); + *c = tmp; + goto out; + } + } + + err = xa_alloc(&ff->classifiers, &(*c)->id, *c, XA_LIMIT(0, le32_to_cpu(ff->ff_caps->classifiers_limit) - 1), GFP_KERNEL); if (err) @@ -5931,29 +5945,30 @@ static int setup_classifier(struct virtnet_ff *ff, struct virtnet_classifier *c) err = virtio_admin_obj_create(ff->vdev, VIRTIO_NET_RESOURCE_OBJ_FF_CLASSIFIER, - c->id, + (*c)->id, VIRTIO_ADMIN_GROUP_TYPE_SELF, 0, - &c->classifier, - c->size); + &(*c)->obj, + (*c)->size); if (err) goto err_xarray; + refcount_set(&(*c)->refcount, 1); +out: return 0; err_xarray: - xa_erase(&ff->classifiers, c->id); + xa_erase(&ff->classifiers, (*c)->id); return err; } -static void destroy_classifier(struct virtnet_ff *ff, - u32 classifier_id) +static void try_destroy_classifier(struct virtnet_ff *ff, u32 classifier_id) { struct virtnet_classifier *c; c = xa_load(&ff->classifiers, classifier_id); - if (c) { + if (c && refcount_dec_and_test(&c->refcount)) { virtio_admin_obj_destroy(ff->vdev, VIRTIO_NET_RESOURCE_OBJ_FF_CLASSIFIER, c->id, @@ -5977,7 +5992,7 @@ static void destroy_ethtool_rule(struct virtnet_ff *ff, 0); xa_erase(&ff->ethtool.rules, eth_rule->flow_spec.location); - destroy_classifier(ff, eth_rule->classifier_id); + try_destroy_classifier(ff, eth_rule->classifier_id); kfree(eth_rule); } @@ -6149,7 +6164,7 @@ static int build_and_insert(struct virtnet_ff *ff, } c->size = classifier_size; - classifier = &c->classifier; + classifier = &c->obj; classifier->count = num_hdrs; selector = (void *)&classifier->selectors[0]; @@ -6159,14 +6174,16 @@ static int build_and_insert(struct virtnet_ff *ff, if (err) goto err_key; - err = setup_classifier(ff, c); + err = setup_classifier(ff, &c); if (err) goto err_classifier; err = insert_rule(ff, eth_rule, c->id, key, key_size); if (err) { - /* destroy_classifier will free the classifier */ - destroy_classifier(ff, c->id); + /* try_destroy_classifier will decrement the refcount on the + * classifier and free it if needed. + */ + try_destroy_classifier(ff, c->id); goto err_key; } -- 2.50.1