From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=170.10.133.124 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796304051; b=4fEBfUHHwdZHjN5pD98lPt4/oFMjT8UEy00vIYXSdQ+Jg49oEhS0LaPPQDUATcR1YurT AY6JPUGbEv6zH+nvd/p29+ocnDNSniBaKPW7Atc3MmIpVPqZ5cMF4w9H3AFFFxcKkfDhC Qm0FfKAI4hFArlNXlggBGSV1LR/LxkrNotfXZZJrsxP3D/XqXlOO0WKr76vfDg8RGiegu SgvnmOjfEtnoyITQVJXLqi4TazgKsRAAz36p+fApaqUAJRpefHB2zIPxHsE/wdr+DhgaZ kS4jtaawIeduSvGYtjNFIPtn1DpIflMvv72sC6IfPAaAfGTKgV7mtq7B+lUSwPbTgUQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796304051; h=DMARC-Filter:DKIM-Signature:Received:X-MC-Unique: X-Mimecast-MFC-AGG-ID:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Gm-Message-State:X-Gm-Gg:X-Received:X-Received:Received:Date:From: To:Cc:Subject:Message-ID:References:MIME-Version:Content-Type: Content-Disposition:In-Reply-To; bh=33epWK39/45v24ue4C1CLn/ZqeHM7j6cNtpeRuZV5c0=; b=wEk35vXnHZxojHMWXUOKt6z1kupfTDaezoRCG5TMXpPSdZRiHa7P7VSmDT8Q0Kdeq2aP 8I3bJOvC5l3nf425R6nO9RHwuz8ZHBvyLZtFkFvw3ryLFF0mniGyBaL33GYKsll73+p/d En9DQOeJ1aMRhlYFJ45YZL8qWZjPT79mezGDmeQ09IYrniqKWS55ms4VrzVzDbwwv4TCp HhQzDyhGRsKzdRsDKbGoPCKBBEHObaB5XeLAkN2r5bl3s6hEz7i/imobLhP2eyNyVVaKX dVaE+ZMEr92aQhjoRb0k/Zjcyn1g6DQvey4RsjcEMdLkIaJUv41qkWfqLmnIQFf1kUA== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=Cyg7qdTC; dkim=pass header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=google header.b=FaWJdMTy; arc=none smtp.remote-ip=170.10.133.124 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 80278606A322 for ; Thu, 3 Dec 2026 13:20:50 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 80278606A322 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=Cyg7qdTC; dkim=pass (2048-bit key) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=google header.b=FaWJdMTy DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1796304049; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=33epWK39/45v24ue4C1CLn/ZqeHM7j6cNtpeRuZV5c0=; b=Cyg7qdTCP7FKPucOid0CY343Zonsbccr/i27+90pha8aQK//WwtNUrlMg2XuBQHKxhcxPp T2Ng1StZP2GzUp8PfZHhKyV1zPz1IgxwCek08IN0kG0WmeugmRBXokQT8g88e3Z3IRNGt6 OzV5csLzDHmHhmeElvNIe0N+/BqK1dQ= Received: from mail-wr1-f72.google.com (mail-wr1-f72.google.com [209.85.221.72]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-644-G5Ea9aMbOvq6LGHGWTgMGg-1; Thu, 3 Dec 2026 13:20:47 +0000 X-MC-Unique: G5Ea9aMbOvq6LGHGWTgMGg-1 X-Mimecast-MFC-AGG-ID: G5Ea9aMbOvq6LGHGWTgMGg_1796304046 Received: by mail-wr1-f72.google.com with SMTP id ffacd0b85a97d-4beee84c431so707793f8f.0 for ; Thu, 03 Dec 2026 05:20:47 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=google; t=1796304046; x=1796908846; darn=vger.kernel.org; h=in-reply-to:content-disposition:content-type:mime-version :references:message-id:subject:cc:to:from:date:from:to:cc:subject :date:message-id:reply-to:content-type; bh=33epWK39/45v24ue4C1CLn/ZqeHM7j6cNtpeRuZV5c0=; b=FaWJdMTyFBIEjhAITgowBt7xxIn1p2ES/Y/+fKVlPvk5m4Zd/g79gAC8Qyp0deP4M7 BifJDKYG8HD99+Dq/guk+6ugfwjEoI7E0Z3kcPH5GQAApzlEWFeu2zYmTgOwwCdSGKJ7 4EdRjO97pTH/GZw312mmjfkeD57oF017/VnN45XLffJXYw3kuCE7ZbglSdJgMfPiiteR p4oUKmM9Ao2r6RNBKAuTK7LlXEplYaC4eD4aebv30OwowFn8AW5bFNrP3/n4lpDot7+E vf6wSFmkAhyVNaGc5pe2QE/+8JICPrQ5MSmm4VmtysLhSAM4Up1dwB+ckcPhk2LqrLcw eZiA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796304046; x=1796908846; h=in-reply-to:content-disposition:content-type:mime-version :references:message-id:subject:cc:to:from:date:x-gm-gg :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to :content-type; bh=33epWK39/45v24ue4C1CLn/ZqeHM7j6cNtpeRuZV5c0=; b=wiHYWpkfuPMiVa0sRPff47Q52tcNQbYAnZBLdw1eS+yKgmceWUSXZ1oaSmREhVFRq3 ywVJs+4NoveNDq//s4EHJMOh2gPTeQEPsThJU7Te4bMW1QyPvHeqLxpBa2EaK9a/MwHl fiuAhWwt9JdVQ/VslxFo0wtWxWxy2tkFmLuFVHNFcRlB2TUvVDRrr09RIamrd+uiOpnE pxmIqW9mxdCBprZIss//WwaK9p1iC09ZH8X1aCWxjns+F4owz9zkmMVjXr2fa4aTLr0b hKPCvbJue+3AKbfbOC94JBexdllvOmXU1osq1Qn483lJ5rbO+8CZWECRUBjeSlrXafm+ A70Q== X-Gm-Message-State: APuU4Z/8XmTTOPiDu6QssrMcjoY3MKt7svXpgspwDwmIDzrXEFOPapoC dQ4u5a6x1WPJGFoOJ5BeaLLD0jm0V8EOYjQdRYciO+yAu8czJGMjyHzbI2EPGavbD0flZwwtYB3 3Rrgu3y4BbEeMa+vVRoAN6yXZL5VqFpv52LZ1IszjbS4f84SeIiZQPNGRfg== X-Gm-Gg: AZFdTZes/+DEIim5P1kNv1Q6KxXJY0KFjCly3nxzJF1l98E5Sez8PF8KsJwcnngfTt8 ncqFSx8PeTTTr9TiB1oBknGkdHozdf6BBsY6kU2hkNIv3TrQQtLBb9JuOTvR3eYNHryTawGlgFw 64ZigUByj5TuESf3q8qe/P+U12nYYUAwzYTbUqQ8xfH4QaFI2XVTTtv0qkhKqHL2712d/lEVij1 QnU4jXSCM5vFgsdJpolwtgk9t5fE3tPbXv+IDcA7iYM58lAKst2iyqA+8N179STkactwOGczuuC nAuyaGZGTdEoNjDQJqUZKWfzcb0ClrglgrQpweMF90XZoZ03g6Xcq/XAND8XTDG8ASvlkVX86JA ytewcGjZTb2KdPft0RFIk//CCQw+mjxgfHYqiGnaioQ== X-Received: by 2002:a5d:4b91:0:b0:4a3:c963:c1c3 with SMTP id ffacd0b85a97d-4cf460fb2dcmr3215280f8f.15.1796304045880; Thu, 03 Dec 2026 05:20:45 -0800 (PST) X-Received: by 2002:a5d:4b91:0:b0:4a3:c963:c1c3 with SMTP id ffacd0b85a97d-4cf460fb2dcmr3215189f8f.15.1796304045170; Thu, 03 Dec 2026 05:20:45 -0800 (PST) Received: from redhat.com ([2a0d:6fc0:153a:4c00:6468:7acf:febf:a046]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-4ab6ae2dd55sm81580978f8f.30.2026.12.03.05.20.43 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 03 Dec 2026 05:20:44 -0800 (PST) Date: Thu, 3 Dec 2026 08:20:41 -0500 From: "Michael S. Tsirkin" To: Shahar Shitrit Cc: netdev@vger.kernel.org, jasowang@redhat.com, pabeni@redhat.com, virtualization@lists.linux.dev, parav@nvidia.com, yohadt@nvidia.com, xuanzhuo@linux.alibaba.com, eperezma@redhat.com, jgg@ziepe.ca, kevin.tian@intel.com, kuba@kernel.org, andrew+netdev@lunn.ch, edumazet@google.com, danielj@nvidia.com Subject: Re: [PATCH net-next v24 10/14] virtio_net: Use existing classifier if possible Message-ID: <20261203081954-mutt-send-email-mst@kernel.org> References: <20261202150502.3350030-1-shshitrit@nvidia.com> <20261202150502.3350030-11-shshitrit@nvidia.com> X-Mailing-List: netdev@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20261202150502.3350030-11-shshitrit@nvidia.com> On Wed, Dec 02, 2026 at 05:04:58PM +0200, Shahar Shitrit wrote: > From: Daniel Jurgens > > 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 artificially 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: Xuan Zhuo > Signed-off-by: Shahar Shitrit > --- > drivers/net/virtio_net.c | 50 +++++++++++++++++++++++++++------------- > 1 file changed, 34 insertions(+), 16 deletions(-) > > diff --git a/drivers/net/virtio_net.c b/drivers/net/virtio_net.c > index e9caed7f0ef66..43c3e6750ad01 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); > @@ -5800,12 +5801,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"); > > @@ -5893,11 +5895,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) { So adding N classifiers is O(N^2). What kind of N can we expect? > + 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) > @@ -5905,30 +5920,31 @@ 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; > int err; > > c = xa_load(&ff->classifiers, classifier_id); > - if (c) { > + if (c && refcount_dec_and_test(&c->refcount)) { > err = virtio_admin_obj_destroy(ff->vdev, > VIRTIO_NET_RESOURCE_OBJ_FF_CLASSIFIER, > c->id, > @@ -5955,7 +5971,7 @@ static int 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); > > return err; > @@ -6138,7 +6154,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]; > > @@ -6148,14 +6164,16 @@ static int build_and_insert(struct virtnet_ff *ff, > if (err) > goto err_classifier; > > - 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.49.0 >