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=1796304057; b=DMAmjWUBrOnw+tXiz8VsfxcQq18nMw+emem5h8CGieJ8y8jucv4hbdq2XqqZ98jhaWaM AwQopU8miws7ur319hSOU5RXRznnpl8VZdTqXnQGLy8omMfZZuvovGm1c18c1oPRL/il5 s5OhZrH92jeHpQu+eztU9PjsK/UOhNpZHgsdWXxLe6BqiRvIxjQRlFATg/mRkQkWSAjnS Z08p/3HTOulV38X+jEtyM+fvvP+W4oFi2RrVcUs4VgC6YbxuHdffr0gkDTv2ZeoFKHAMI KI31Wt3DAPd8mRhIFpj9yXQ4jEiVAX1oXZHGmp43ln8ZvuM8hrFH/3zG0dgrPnKChIA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796304057; h=DMARC-Filter:DKIM-Signature:X-MC-Rep:Received:X-MC-Unique: X-Mimecast-MFC-AGG-ID:Received:X-Google-DKIM-Signature: X-Forwarded-Encrypted:X-Gm-Message-State:X-Gm-Gg:X-Received: X-Received:Received:Date:From:To:Cc:Subject:Message-ID:References: MIME-Version:In-Reply-To:X-Mimecast-Spam-Score:X-Mimecast-MFC-PROC-ID: X-Mimecast-Originator:Content-Type:Content-Disposition; bh=33epWK39/45v24ue4C1CLn/ZqeHM7j6cNtpeRuZV5c0=; b=Amjp5F6PPQyRBVAPfu2mIitlkuZ1kEXPjI1IFv4RjG0Z4Ms1V9xOpbwici7v002aV7Rb jtFsnNsziMtWmzrUFCvcCyKK0l4y/TfJuU89A7ED+nx3XC0zQWvAtdR5b+53DAiT4NUcJ DLTrYQkRoSmBvN3i+sWQNcNuMz5AZ5xqz3cO2ivZoRL+dc1e+UgRcRR/3GnUPB9M06JNG sj8rX/Z8fhY2f6jxsNq+sWMdXTFP4JhA6cxIy/eHzYDXkpjzCgXTjMsAqeS7kaLLDG/lb o0d7DiIExz2Emg2m1oCmodVR54GgQ2SoNtHxW6OmXXJ84H6XfSNDuqd5u1dE2B5wgaA== 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=Y7wClU8S; 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 0B6F8606A32C for ; Thu, 3 Dec 2026 13:20:56 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 0B6F8606A32C 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=Y7wClU8S DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1796304056; 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=Y7wClU8SL05vJ1B9m1XzFfe7/rK0b9EDnY2CyvXVb2oWJgQCGq0lSGA8rydyYglzw4M3VI eTuOD7Xf4j3pvkTcmt5dwz0kHQ+syMI5pJ6zSjJMkfHXHkguBAxeeHcHfQgs5PC4tppBUW E88IQ3j1DJJFIKLBM4D9fdvlPK3frug= X-MC-Rep: binary re single 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-58-4vWXNvNwOSi1F6nGuS6xHw-1; Thu, 3 Dec 2026 13:20:47 +0000 X-MC-Unique: 4vWXNvNwOSi1F6nGuS6xHw-1 X-Mimecast-MFC-AGG-ID: 4vWXNvNwOSi1F6nGuS6xHw_1796304046 Received: by mail-wr1-f72.google.com with SMTP id ffacd0b85a97d-499ca65ae87so541691f8f.1 for ; Thu, 03 Dec 2026 05:20:47 -0800 (PST) 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=mjtHIuslD98YnO3EiUyBYangs/QRo+iyTlKqhE9vm+08FboWoSXPPXfjp+ajLA5VeZ syHZhWful+jJw49igOY0cQz2uNvujTfnRyf5O7pJ+HAsVK/bJXQl18BU/lERCg4yHgst ofWNv6AEhot+1XBv22NFyne1tF2+frrI+lcX+WRNycfSx2zEdwBAdM9iKCSPwIZqgboA fhBFpsjuZxtlQn8cP6jYEAgXKQH2XQN191WGqJFW6T5z+PqkJMNyHcUvkw5rLfXUpIRf 5TrpmZJdljCwro9VEhfJ3tj0WsSrSRSDpIpzsnbgkoPF83XbI6NQiNUZ5m1O/1z8Qph7 i2Jg== X-Forwarded-Encrypted: i=1; AL+hErHjJ4GtpPHkLBUzHs+eRpAb4K0aYl28gR3H2VUtyyaCNAmh6YSRCuR5O47jEGY84sl9A0OfaeH603Tx10cfFw==@lists.linux.dev X-Gm-Message-State: APuU4Z/lLkxzRCW9reEkkn11iN9i/XklWdhEE+3OnqsugKF24viXZHL4 JFpTFA1H3J495DOmriJ681J4w/kxk7gOIDy+uRfnVezgwBrcSVm1mESgn0J00nBb98GWicEWC08 wLsSB6YQdgKQ0G3kHNVk7zPhrJEy6ItnEiRHSO4pM348o1r4r0FG/yZic8KwhgnlqKrXA X-Gm-Gg: AZFdTZf9QYPMXRkaWMUe5LM8Pz1mINqtjXBKRcIduwnOpB2DnFzeeK96Q+hCHexuWLx f0shG+rd9DdjqoshYnYFnREJVGbxKf5J5IAkZA+jZFiD580pX5G9qlRk+GA25ucSSlOsCt038bS cS3FYckRmzbK3DGY2OaJy9CcNjU3Fn/KJMOFXISZgKzd3PPNisdbwy/AB7v9yw4F3NllcgFtJDA /wHa4rwJalU81UGXAhA9mttFMTuLb8/conugSkxnviVh+CTt2eEOOBs70gu5rRkDq06N9rX4pjx ffzPAwXhoK/0bKqJURKX9B5WCuqe76VCL4j6P4tOfj49ELIKTroFWwZYSujVcDbwjS/3ZMOtRBJ an8zozqEDlAdAY2l+hCQGcUt0WyQIo087cNZkR6mZeg== X-Received: by 2002:a5d:4b91:0:b0:4a3:c963:c1c3 with SMTP id ffacd0b85a97d-4cf460fb2dcmr3215274f8f.15.1796304045868; 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: virtualization@lists.linux.dev List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 In-Reply-To: <20261202150502.3350030-11-shshitrit@nvidia.com> X-Mimecast-Spam-Score: 0 X-Mimecast-MFC-PROC-ID: WvL1mmFDUwJMNzTc09SGv8ovcuM-8tr2RFClGEp4cHg_1796304046 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset=us-ascii Content-Disposition: inline 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 >