From patchwork Tue Aug 20 13:55:46 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mike Pattrick X-Patchwork-Id: 1974441 X-Patchwork-Delegate: i.maximets@samsung.com Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=fail reason="signature verification failed" (1024-bit key; unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=SHN8Eur8; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=openvswitch.org (client-ip=2605:bc80:3010::138; helo=smtp1.osuosl.org; envelope-from=ovs-dev-bounces@openvswitch.org; receiver=patchwork.ozlabs.org) Received: from smtp1.osuosl.org (smtp1.osuosl.org [IPv6:2605:bc80:3010::138]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4Wp9x16fZXz1yYZ for ; Tue, 20 Aug 2024 23:56:21 +1000 (AEST) Received: from localhost (localhost [127.0.0.1]) by smtp1.osuosl.org (Postfix) with ESMTP id 2774381220; Tue, 20 Aug 2024 13:56:20 +0000 (UTC) X-Virus-Scanned: amavis at osuosl.org Received: from smtp1.osuosl.org ([127.0.0.1]) by localhost (smtp1.osuosl.org [127.0.0.1]) (amavis, port 10024) with ESMTP id 5lMFmBkabQDl; Tue, 20 Aug 2024 13:56:18 +0000 (UTC) X-Comment: SPF check N/A for local connections - client-ip=140.211.9.56; helo=lists.linuxfoundation.org; envelope-from=ovs-dev-bounces@openvswitch.org; receiver= DKIM-Filter: OpenDKIM Filter v2.11.0 smtp1.osuosl.org 0573880EAA Authentication-Results: smtp1.osuosl.org; dkim=fail reason="signature verification failed" (1024-bit key) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=SHN8Eur8 Received: from lists.linuxfoundation.org (lf-lists.osuosl.org [140.211.9.56]) by smtp1.osuosl.org (Postfix) with ESMTPS id 0573880EAA; Tue, 20 Aug 2024 13:56:17 +0000 (UTC) Received: from lf-lists.osuosl.org (localhost [127.0.0.1]) by lists.linuxfoundation.org (Postfix) with ESMTP id 8D277C1029; Tue, 20 Aug 2024 13:56:17 +0000 (UTC) X-Original-To: dev@openvswitch.org Delivered-To: ovs-dev@lists.linuxfoundation.org Received: from smtp4.osuosl.org (smtp4.osuosl.org [140.211.166.137]) by lists.linuxfoundation.org (Postfix) with ESMTP id 94B41C087F for ; Tue, 20 Aug 2024 13:56:11 +0000 (UTC) Received: from localhost (localhost [127.0.0.1]) by smtp4.osuosl.org (Postfix) with ESMTP id B7228405E6 for ; Tue, 20 Aug 2024 13:56:10 +0000 (UTC) X-Virus-Scanned: amavis at osuosl.org Received: from smtp4.osuosl.org ([127.0.0.1]) by localhost (smtp4.osuosl.org [127.0.0.1]) (amavis, port 10024) with ESMTP id T_yLU_TOB19C for ; Tue, 20 Aug 2024 13:56:10 +0000 (UTC) Received-SPF: Pass (mailfrom) identity=mailfrom; client-ip=170.10.129.124; helo=us-smtp-delivery-124.mimecast.com; envelope-from=mkp@redhat.com; receiver= DMARC-Filter: OpenDMARC Filter v1.4.2 smtp4.osuosl.org C6368405FB Authentication-Results: smtp4.osuosl.org; dmarc=pass (p=none dis=none) header.from=redhat.com DKIM-Filter: OpenDKIM Filter v2.11.0 smtp4.osuosl.org C6368405FB Authentication-Results: smtp4.osuosl.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=SHN8Eur8 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by smtp4.osuosl.org (Postfix) with ESMTPS id C6368405FB for ; Tue, 20 Aug 2024 13:56:09 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1724162168; 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: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=NV0jyKAgQHYmrnqJJ7owHnz0/cSxEsBJwBk9RTsrdPc=; b=SHN8Eur8hls/HWEg97YHegX+wur+V4Zha9pZLSMhq+QV79kA2KKo8CBqzEKG0/9RcN0/ni DWzIwHgUAtVdBe7o8Sll9jKCtCLv3CKMT5o6HY7Za15L+zQ7W65k/N8VzeMGjXjDTjG8b8 aQrTIgwWwIDRTIfiVLwtgg4vUBci6ac= Received: from mx-prod-mc-05.mail-002.prod.us-west-2.aws.redhat.com (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-645-IwzPK73sO96lUPngbs-UhQ-1; Tue, 20 Aug 2024 09:56:07 -0400 X-MC-Unique: IwzPK73sO96lUPngbs-UhQ-1 Received: from mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.17]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-05.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 994E61955F08 for ; Tue, 20 Aug 2024 13:56:06 +0000 (UTC) Received: from mpattric.remote.csb (unknown [10.22.33.24]) by mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id D6B091956053; Tue, 20 Aug 2024 13:56:04 +0000 (UTC) From: Mike Pattrick To: dev@openvswitch.org Date: Tue, 20 Aug 2024 09:55:46 -0400 Message-ID: <20240820135549.1726748-6-mkp@redhat.com> In-Reply-To: <20240820135549.1726748-1-mkp@redhat.com> References: <20240820135549.1726748-1-mkp@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.0 on 10.30.177.17 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Subject: [ovs-dev] [PATCH v2 5/8] classifier: Store n_indices between usage. X-BeenThere: ovs-dev@openvswitch.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: ovs-dev-bounces@openvswitch.org Sender: "dev" Currently the Clang analyzer will complain about usage of an uninitialized variable in the classifier. This is a false positive, but not for a reason that could easily be detectable by clang. The classifier is not safe for multiple writer threads to use simultaneously so all callers protect these functions from simultaneous writes. However, this is not so clear from the code's static analysis alone. To help Clang out here, the n_indicies count is saved onto the stack instead of accessed from the subtables struct repeatedly. Signed-off-by: Mike Pattrick Acked-by: Eelco Chaudron --- lib/classifier.c | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/lib/classifier.c b/lib/classifier.c index 0729bd190..55f23b976 100644 --- a/lib/classifier.c +++ b/lib/classifier.c @@ -527,6 +527,7 @@ classifier_replace(struct classifier *cls, const struct cls_rule *rule, struct cls_match *head; unsigned int mask_offset; size_t n_rules = 0; + uint8_t n_indices; uint32_t basis; uint32_t hash; unsigned int i; @@ -543,7 +544,8 @@ classifier_replace(struct classifier *cls, const struct cls_rule *rule, /* Compute hashes in segments. */ basis = 0; mask_offset = 0; - for (i = 0; i < subtable->n_indices; i++) { + n_indices = subtable->n_indices; + for (i = 0; i < n_indices; i++) { ihash[i] = minimatch_hash_range(&rule->match, subtable->index_maps[i], &mask_offset, &basis); } @@ -575,7 +577,7 @@ classifier_replace(struct classifier *cls, const struct cls_rule *rule, } /* Add new node to segment indices. */ - for (i = 0; i < subtable->n_indices; i++) { + for (i = 0; i < n_indices; i++) { ccmap_inc(&subtable->indices[i], ihash[i]); } n_rules = cmap_insert(&subtable->rules, &new->cmap_node, hash); @@ -713,6 +715,7 @@ classifier_remove(struct classifier *cls, const struct cls_rule *cls_rule) struct cls_subtable *subtable; uint32_t basis = 0, hash, ihash[CLS_MAX_INDICES]; unsigned int mask_offset; + uint8_t n_indices; size_t n_rules; unsigned int i; @@ -730,7 +733,8 @@ classifier_remove(struct classifier *cls, const struct cls_rule *cls_rule) ovs_assert(subtable); mask_offset = 0; - for (i = 0; i < subtable->n_indices; i++) { + n_indices = subtable->n_indices; + for (i = 0; i < n_indices; i++) { ihash[i] = minimatch_hash_range(&cls_rule->match, subtable->index_maps[i], &mask_offset, &basis); @@ -783,7 +787,7 @@ classifier_remove(struct classifier *cls, const struct cls_rule *cls_rule) } /* Remove rule node from indices. */ - for (i = 0; i < subtable->n_indices; i++) { + for (i = 0; i < n_indices; i++) { ccmap_dec(&subtable->indices[i], ihash[i]); } n_rules = cmap_remove(&subtable->rules, &rule->cmap_node, hash);