From patchwork Tue Jan 17 08:55:40 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Steen Hegelund X-Patchwork-Id: 44565 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:eb09:0:0:0:0:0 with SMTP id s9csp1643651wrn; Tue, 17 Jan 2023 01:00:50 -0800 (PST) X-Google-Smtp-Source: AMrXdXuHiOQofg/LIjPmlRvTp2ctfhY6rxnwLhr9hMB33nnU3ehxuJHcqLkoKAIGw7ynbun5I2nJ X-Received: by 2002:a17:90b:3755:b0:226:eec9:6b62 with SMTP id ne21-20020a17090b375500b00226eec96b62mr2428017pjb.22.1673946049697; Tue, 17 Jan 2023 01:00:49 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1673946049; cv=none; d=google.com; s=arc-20160816; b=Syi9gksw5mSWWdXKwILuxPztR94nxRsaEC0V23Rtrs6iIFK0getx9WEhljOD95G3Iq fJRy0mHnPwGZwwVAPMbsk+xUAOZAnaIcH1mbo/fQ1MFtrqJ6uo3HkFu/WUcUCVl0603s tNk4bJ8vD3jTpBd/9p5MJAukW3X8r+8nP65DtQcO7XRXhdCfWCyyNHi2INykDq+zDHgB X8/4bjBYHwyY5RtxfqoGlqlOucCB5DK/18iuSQSJWupGmUPKklASb0uzov6lcdu/iWrB JVM9ND0zF6A/YgmUZ8YQx8Isd9HPDxIqvdV+ndnWG3s7Ha3Rx0QfueetN8ruSC5VyV84 UPPQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=51+D+4B3SOE4prNmetydtTrc1tkxh+R2piktXF4Qa5c=; b=DrHn5KbulviZtOIm1ZdHaWE9jh0Xi2BIH5FU/b2oSTPIgV4DPVqAnoD5pmrv3fS0vX M8UnbDU+HBnmDICrL+OlmGF1wKFP9OxtzQWNX/Fh0nXU3vdNAME4VVpq9vii1iWos0UE A37nXnkoFLYQ1QROuB+gpucsmLrrGkFb9LJADi8jizMSQ0zD6pJdeYWaI8/zLH9YhInZ rKQBu+Wzrv4dL9TMwp/OeSlEAHN3LN/D7xFS3pWWL5Zz308H7NcXAT9T2B9V9cVZc1Mv Jw8fB3GYAHKCdC8NCVTd8AKR7l5Q3CuwS/Pmdj4raN3UG4QgjMXaQRfOFtAjYtIvQ6pW aH2Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@microchip.com header.s=mchp header.b=AH1tE+Ai; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=microchip.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id f15-20020a65590f000000b00473c36e3b75si12387453pgu.608.2023.01.17.01.00.37; Tue, 17 Jan 2023 01:00:49 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@microchip.com header.s=mchp header.b=AH1tE+Ai; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=microchip.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235720AbjAQI4G (ORCPT + 99 others); Tue, 17 Jan 2023 03:56:06 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57834 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235853AbjAQIz7 (ORCPT ); Tue, 17 Jan 2023 03:55:59 -0500 Received: from esa.microchip.iphmx.com (esa.microchip.iphmx.com [68.232.153.233]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B64753C1D; Tue, 17 Jan 2023 00:55:55 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=microchip.com; i=@microchip.com; q=dns/txt; s=mchp; t=1673945755; x=1705481755; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=95uKSDc/UujjSLEnC0nCOkmJuk+JOEIElumqUJ28/IM=; b=AH1tE+AiAEUF384uwakdvrSERrlHgG6Pd4kr9HeiIU6qITILe1p9aSd4 aI+q9/QUm1sZJEbBa3hQngfjcbxgOMTgc6UTGR7JtpMBOed5w4aLe4EYH XUCvmynQrNcNbaMYS7cjvECHtgNDUOlA0CV0Qm/moDdxDoyNYXwGPWH+0 THTzuOqPX6mOgD8T4fShNGolOuKhX2c6ZAtcEl2UMex5I50vkoC695P0L CAWHsgIfkGMpuvEn+UbvEkndBnEzkUuOKqrbXVE3KOZPg535UBSBlk29p ZQ7YuOD8Ro5p2IgfzPgoWMCQWn/YqanRMUsvraYYQ6zenpZZGUr+bklXA w==; X-IronPort-AV: E=Sophos;i="5.97,222,1669100400"; d="scan'208";a="208096662" Received: from unknown (HELO email.microchip.com) ([170.129.1.10]) by esa1.microchip.iphmx.com with ESMTP/TLS/AES256-SHA256; 17 Jan 2023 01:55:55 -0700 Received: from chn-vm-ex04.mchp-main.com (10.10.85.152) by chn-vm-ex04.mchp-main.com (10.10.85.152) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.16; Tue, 17 Jan 2023 01:55:54 -0700 Received: from den-dk-m31857.microchip.com (10.10.115.15) by chn-vm-ex04.mchp-main.com (10.10.85.152) with Microsoft SMTP Server id 15.1.2507.16 via Frontend Transport; Tue, 17 Jan 2023 01:55:51 -0700 From: Steen Hegelund To: "David S . Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni CC: Steen Hegelund , , Randy Dunlap , "Casper Andersson" , Russell King , Wan Jiabing , "Nathan Huckleberry" , , , , "Steen Hegelund" , Daniel Machon , Horatiu Vultur , Lars Povlsen , Dan Carpenter , Michael Walle Subject: [PATCH net-next 1/5] net: microchip: sparx5: Add support for rule count by cookie Date: Tue, 17 Jan 2023 09:55:40 +0100 Message-ID: <20230117085544.591523-2-steen.hegelund@microchip.com> X-Mailer: git-send-email 2.39.0 In-Reply-To: <20230117085544.591523-1-steen.hegelund@microchip.com> References: <20230117085544.591523-1-steen.hegelund@microchip.com> MIME-Version: 1.0 X-Spam-Status: No, score=-4.4 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED, RCVD_IN_MSPIKE_H2,SPF_HELO_PASS,SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1755259653149185369?= X-GMAIL-MSGID: =?utf-8?q?1755259653149185369?= This adds support for TC clients to get the packet count for a TC filter identified by its cookie. Signed-off-by: Steen Hegelund --- .../microchip/sparx5/sparx5_tc_flower.c | 34 +--------- .../net/ethernet/microchip/vcap/vcap_api.c | 67 ++++++++++++------- .../ethernet/microchip/vcap/vcap_api_client.h | 2 + 3 files changed, 47 insertions(+), 56 deletions(-) diff --git a/drivers/net/ethernet/microchip/sparx5/sparx5_tc_flower.c b/drivers/net/ethernet/microchip/sparx5/sparx5_tc_flower.c index 986e41d3bb28..affaa1656710 100644 --- a/drivers/net/ethernet/microchip/sparx5/sparx5_tc_flower.c +++ b/drivers/net/ethernet/microchip/sparx5/sparx5_tc_flower.c @@ -35,11 +35,6 @@ struct sparx5_tc_flower_parse_usage { unsigned int used_keys; }; -struct sparx5_tc_rule_pkt_cnt { - u64 cookie; - u32 pkts; -}; - /* These protocols have dedicated keysets in IS2 and a TC dissector * ETH_P_ARP does not have a TC dissector */ @@ -947,44 +942,21 @@ static int sparx5_tc_flower_destroy(struct net_device *ndev, return err; } -/* Collect packet counts from all rules with the same cookie */ -static int sparx5_tc_rule_counter_cb(void *arg, struct vcap_rule *rule) -{ - struct sparx5_tc_rule_pkt_cnt *rinfo = arg; - struct vcap_counter counter; - int err = 0; - - if (rule->cookie == rinfo->cookie) { - err = vcap_rule_get_counter(rule, &counter); - if (err) - return err; - rinfo->pkts += counter.value; - /* Reset the rule counter */ - counter.value = 0; - vcap_rule_set_counter(rule, &counter); - } - return err; -} - static int sparx5_tc_flower_stats(struct net_device *ndev, struct flow_cls_offload *fco, struct vcap_admin *admin) { struct sparx5_port *port = netdev_priv(ndev); - struct sparx5_tc_rule_pkt_cnt rinfo = {}; + struct vcap_counter ctr = {}; struct vcap_control *vctrl; ulong lastused = 0; - u64 drops = 0; - u32 pkts = 0; int err; - rinfo.cookie = fco->cookie; vctrl = port->sparx5->vcap_ctrl; - err = vcap_rule_iter(vctrl, sparx5_tc_rule_counter_cb, &rinfo); + err = vcap_get_rule_count_by_cookie(vctrl, &ctr, fco->cookie); if (err) return err; - pkts = rinfo.pkts; - flow_stats_update(&fco->stats, 0x0, pkts, drops, lastused, + flow_stats_update(&fco->stats, 0x0, ctr.value, 0, lastused, FLOW_ACTION_HW_STATS_IMMEDIATE); return err; } diff --git a/drivers/net/ethernet/microchip/vcap/vcap_api.c b/drivers/net/ethernet/microchip/vcap/vcap_api.c index 0f9e0d735ae3..4e038f96a131 100644 --- a/drivers/net/ethernet/microchip/vcap/vcap_api.c +++ b/drivers/net/ethernet/microchip/vcap/vcap_api.c @@ -2989,31 +2989,6 @@ void vcap_rule_set_counter_id(struct vcap_rule *rule, u32 counter_id) } EXPORT_SYMBOL_GPL(vcap_rule_set_counter_id); -/* Provide all rules via a callback interface */ -int vcap_rule_iter(struct vcap_control *vctrl, - int (*callback)(void *, struct vcap_rule *), void *arg) -{ - struct vcap_rule_internal *ri; - struct vcap_admin *admin; - int ret; - - ret = vcap_api_check(vctrl); - if (ret) - return ret; - - /* Iterate all rules in each VCAP instance */ - list_for_each_entry(admin, &vctrl->list, list) { - list_for_each_entry(ri, &admin->rules, list) { - ret = callback(arg, &ri->data); - if (ret) - return ret; - } - } - - return 0; -} -EXPORT_SYMBOL_GPL(vcap_rule_iter); - int vcap_rule_set_counter(struct vcap_rule *rule, struct vcap_counter *ctr) { struct vcap_rule_internal *ri = to_intrule(rule); @@ -3105,6 +3080,48 @@ int vcap_rule_get_keysets(struct vcap_rule_internal *ri, return -EINVAL; } +/* Collect packet counts from all rules with the same cookie */ +int vcap_get_rule_count_by_cookie(struct vcap_control *vctrl, + struct vcap_counter *ctr, u64 cookie) +{ + struct vcap_rule_internal *ri; + struct vcap_counter temp = {}; + struct vcap_admin *admin; + int err; + + err = vcap_api_check(vctrl); + if (err) + return err; + + /* Iterate all rules in each VCAP instance */ + list_for_each_entry(admin, &vctrl->list, list) { + mutex_lock(&admin->lock); + list_for_each_entry(ri, &admin->rules, list) { + if (ri->data.cookie != cookie) + continue; + + err = vcap_read_counter(ri, &temp); + if (err) + goto unlock; + ctr->value += temp.value; + + /* Reset the rule counter */ + temp.value = 0; + temp.sticky = 0; + err = vcap_write_counter(ri, &temp); + if (err) + goto unlock; + } + mutex_unlock(&admin->lock); + } + return err; + +unlock: + mutex_unlock(&admin->lock); + return err; +} +EXPORT_SYMBOL_GPL(vcap_get_rule_count_by_cookie); + static int vcap_rule_mod_key(struct vcap_rule *rule, enum vcap_key_field key, enum vcap_field_type ftype, diff --git a/drivers/net/ethernet/microchip/vcap/vcap_api_client.h b/drivers/net/ethernet/microchip/vcap/vcap_api_client.h index b8980b22352f..2cdcd3b56b30 100644 --- a/drivers/net/ethernet/microchip/vcap/vcap_api_client.h +++ b/drivers/net/ethernet/microchip/vcap/vcap_api_client.h @@ -202,6 +202,8 @@ int vcap_rule_add_action_u32(struct vcap_rule *rule, enum vcap_action_field action, u32 value); /* VCAP rule counter operations */ +int vcap_get_rule_count_by_cookie(struct vcap_control *vctrl, + struct vcap_counter *ctr, u64 cookie); int vcap_rule_set_counter(struct vcap_rule *rule, struct vcap_counter *ctr); int vcap_rule_get_counter(struct vcap_rule *rule, struct vcap_counter *ctr); From patchwork Tue Jan 17 08:55:41 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Steen Hegelund X-Patchwork-Id: 44563 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:eb09:0:0:0:0:0 with SMTP id s9csp1643404wrn; Tue, 17 Jan 2023 01:00:16 -0800 (PST) X-Google-Smtp-Source: AMrXdXtY4UZeAh42BxGtG5ah5xH24m7x/2w5bOljDOW1EgxWzSCXDEo+bXgflpx8VIz4ZUfaZRw2 X-Received: by 2002:a05:6a20:958a:b0:b8:6edc:7eac with SMTP id iu10-20020a056a20958a00b000b86edc7eacmr2311376pzb.39.1673946015890; Tue, 17 Jan 2023 01:00:15 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1673946015; cv=none; d=google.com; s=arc-20160816; b=yQLKWWCxyItZ8bs+xw2nqKZVob3w21OV4pP5YXt+hMbmXlwN0DENaUaiSmSSSS+8de Kmt7q/W+nNerXwB5mSQWvwyVJhW+azTxUrlOPU0PMH/68R1floiCzr9rQOyeY2+gxMLV V6rqMHRSmMgvGS2OEGLxQ67tAFW59/aH3K5k18BZ+bGC4UcXtdgbxGzoFDfYNSdXQLuK MVanpT2X2CBXtHXS0TyDGeB6uZHcscp0DNqyg98ZInPq0oOi4LM5ccfscldEyDhlQ4y3 d9SfZ5hX2Lqc+36wlP7JMfOtmzVG8lSIJlYlzG38+hChVhznJmBa6B3AUVPOtk8SfZti R8Pg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=y2r/Ad7ZrxV9pZhwzHT8SDIeV9b9Cqe374h129+Hn5I=; b=yGaKnKSZJvbLCPUrOlGTO9+yPu4O4F/lDRcuvIFRK0wnAOna338Ry/v8zOfYCfSM3H H2nLNOG0Fsov34F5yX2vaIIvXtwLWIJce/o3Kn5C7iIKgtc//WwsLc/G5QJf1AzFFISx q4VIsRmvhZ5LqFCw44Fw7E4nyIow2MSaG4vXT191uuPV0MIhfvSmrt93ELczn9ur82Pu VSXW27dLmz3t+LTPMP/5kFdL0kTqsPk0W4qnz6d31/cRK/1SqfgCduWFy5Rd2fiwcJld qivFx166eZOZm6sbZgpVAmAnfkAroLJornBqiljRY/qqPRLRIJ7VdKfzYbXACDbT58v8 fpDg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@microchip.com header.s=mchp header.b=TMUOmg1r; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=microchip.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id p28-20020a056a000a1c00b005768619bd8esi21829802pfh.177.2023.01.17.01.00.03; Tue, 17 Jan 2023 01:00:15 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@microchip.com header.s=mchp header.b=TMUOmg1r; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=microchip.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235791AbjAQI4M (ORCPT + 99 others); Tue, 17 Jan 2023 03:56:12 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57886 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236103AbjAQI4B (ORCPT ); Tue, 17 Jan 2023 03:56:01 -0500 Received: from esa.microchip.iphmx.com (esa.microchip.iphmx.com [68.232.153.233]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 958213C10; Tue, 17 Jan 2023 00:55:59 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=microchip.com; i=@microchip.com; q=dns/txt; s=mchp; t=1673945759; x=1705481759; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=snZiNtmGLv9PnppTNGgB75z+Rsn5YVgjz5NEqqogGM4=; b=TMUOmg1rwgCBOzYQULr5aJB7tseJwtL5+yQrfYUfV6vDh4p4GJIVRugG w0M8SsHpCp/x4PjsFDZL1y1sXcpSEeMOcwlioLYkAZE2+C7X0m+C1xz97 uURIubqIqECCgLS0cZP6STmEm9xAout1ZogMTi5VqeHdqhwviYzfVqXZR WqMxuHQUcXds8r1ld61Y1PuyL4ilhH/uvvMRNHawToOC9caGsyq2b5yCK PjqsFGQ8IVEm1n8I/FY6tMfF4Mp+f/nOJbg/N4FZdF7yhYFcFhehLHVQu SIe57LMhz0vGV6q1Eoe04SFDE3c1uaLcHyp9zz9gzTy/NkL3IjAnovBWJ w==; X-IronPort-AV: E=Sophos;i="5.97,222,1669100400"; d="scan'208";a="208096687" Received: from unknown (HELO email.microchip.com) ([170.129.1.10]) by esa1.microchip.iphmx.com with ESMTP/TLS/AES256-SHA256; 17 Jan 2023 01:55:59 -0700 Received: from chn-vm-ex04.mchp-main.com (10.10.85.152) by chn-vm-ex04.mchp-main.com (10.10.85.152) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.16; Tue, 17 Jan 2023 01:55:58 -0700 Received: from den-dk-m31857.microchip.com (10.10.115.15) by chn-vm-ex04.mchp-main.com (10.10.85.152) with Microsoft SMTP Server id 15.1.2507.16 via Frontend Transport; Tue, 17 Jan 2023 01:55:55 -0700 From: Steen Hegelund To: "David S . Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni CC: Steen Hegelund , , Randy Dunlap , "Casper Andersson" , Russell King , Wan Jiabing , "Nathan Huckleberry" , , , , "Steen Hegelund" , Daniel Machon , Horatiu Vultur , Lars Povlsen , Dan Carpenter , Michael Walle Subject: [PATCH net-next 2/5] net: microchip: sparx5: Add support to check for existing VCAP rule id Date: Tue, 17 Jan 2023 09:55:41 +0100 Message-ID: <20230117085544.591523-3-steen.hegelund@microchip.com> X-Mailer: git-send-email 2.39.0 In-Reply-To: <20230117085544.591523-1-steen.hegelund@microchip.com> References: <20230117085544.591523-1-steen.hegelund@microchip.com> MIME-Version: 1.0 X-Spam-Status: No, score=-4.4 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED, RCVD_IN_MSPIKE_H2,SPF_HELO_PASS,SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1755259617572399078?= X-GMAIL-MSGID: =?utf-8?q?1755259617572399078?= Add a new function that just checks if the VCAP rule id is already used by an existing rule. Signed-off-by: Steen Hegelund --- drivers/net/ethernet/microchip/vcap/vcap_api.c | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/drivers/net/ethernet/microchip/vcap/vcap_api.c b/drivers/net/ethernet/microchip/vcap/vcap_api.c index 4e038f96a131..f1dc4fd6bb96 100644 --- a/drivers/net/ethernet/microchip/vcap/vcap_api.c +++ b/drivers/net/ethernet/microchip/vcap/vcap_api.c @@ -920,6 +920,20 @@ int vcap_set_rule_set_actionset(struct vcap_rule *rule, } EXPORT_SYMBOL_GPL(vcap_set_rule_set_actionset); +/* Check if a rule with this id exists */ +static bool vcap_rule_exists(struct vcap_control *vctrl, u32 id) +{ + struct vcap_rule_internal *ri; + struct vcap_admin *admin; + + /* Look for the rule id in all vcaps */ + list_for_each_entry(admin, &vctrl->list, list) + list_for_each_entry(ri, &admin->rules, list) + if (ri->data.id == id) + return true; + return false; +} + /* Find a rule with a provided rule id */ static struct vcap_rule_internal *vcap_lookup_rule(struct vcap_control *vctrl, u32 id) @@ -1866,7 +1880,7 @@ static u32 vcap_set_rule_id(struct vcap_rule_internal *ri) return ri->data.id; for (u32 next_id = 1; next_id < ~0; ++next_id) { - if (!vcap_lookup_rule(ri->vctrl, next_id)) { + if (!vcap_rule_exists(ri->vctrl, next_id)) { ri->data.id = next_id; break; } @@ -2103,7 +2117,7 @@ struct vcap_rule *vcap_alloc_rule(struct vcap_control *vctrl, if (vctrl->vcaps[admin->vtype].rows == 0) return ERR_PTR(-EINVAL); /* Check if a rule with this id already exists */ - if (vcap_lookup_rule(vctrl, id)) + if (vcap_rule_exists(vctrl, id)) return ERR_PTR(-EEXIST); /* Check if there is room for the rule in the block(s) of the VCAP */ maxsize = vctrl->vcaps[admin->vtype].sw_count; /* worst case rule size */ From patchwork Tue Jan 17 08:55:42 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Steen Hegelund X-Patchwork-Id: 44564 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:eb09:0:0:0:0:0 with SMTP id s9csp1643469wrn; Tue, 17 Jan 2023 01:00:25 -0800 (PST) X-Google-Smtp-Source: AMrXdXurTsCg+Z47p1CRr1ti6x4+DxfcAXhZGwBleI/rVr8YFZTUZ0VLh2dKkyFjPEYMXs9UZM4h X-Received: by 2002:a17:902:eb44:b0:189:377c:9ab with SMTP id i4-20020a170902eb4400b00189377c09abmr2746007pli.51.1673946024787; Tue, 17 Jan 2023 01:00:24 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1673946024; cv=none; d=google.com; s=arc-20160816; b=uyp/tsKKhDThZzPLyuunkX2mzLvbuwwUWE8REvq86Zqr38caQo+7/NdSr82cNFKTR8 flPz9wov5FDE/posbCAZgJznF3e7IVgBXe8oPtG/XbFE2GXlaybTADJFocFWV83bC4wi ABKPPQgj1V0Bd374PGwoguAvlLneMG6E9ckaWCAy713ANSLEhdDjkDaYtxm5/oOs3I5F XYEubHXEy81VthY82iDcrBV+kq+I6ybu2AnrLaKdfCaBVimRcDagNYA4YU10zRnC8ksY q8cyyYypH3NEYLhN73SKhJL2Ej4V4lEW4jG6QP1ajM+if2O0/x/yf5aD07+z4uU51U4v yA9g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=1B7WHSHiSiQu2z20iXvMEZXyiIj0n8PDf7AfsMKsb8o=; b=YA/QyIvs5G5Se17azUn84EkDBGmVDj7KVGCajUGJRiMYLKEv7jpcMICQasTfwKRpdQ UK42WltcxtUY9pWTYtSdmgyNCGHpD1qA2jnXlcvyve8x2O9zMzM5yznNrAPcHc+ZHvyw GMHQsT6b7/4fs8CofvUA6NtowznMQfgpooRhgQ4wcOxSufijuyh9Zulyzn0vNI0ecNRn lGebD010SPEOLoPUzgodfn5MtMyWRKXjuqEfzBcf1Z1TYUOZmRLx/6A8f+uFDlL3yRz6 wfENq6dvGn/cVSz6Njc8wQPorZpd/eDevhTt3PEhhjy/LziKwycx7xCjoorX6Gj5djQD eQQQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@microchip.com header.s=mchp header.b=hRBUmQ8H; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=microchip.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id o11-20020a170902778b00b0017123d2abd5si31201690pll.457.2023.01.17.01.00.12; Tue, 17 Jan 2023 01:00:24 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@microchip.com header.s=mchp header.b=hRBUmQ8H; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=microchip.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236237AbjAQI4g (ORCPT + 99 others); Tue, 17 Jan 2023 03:56:36 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57990 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236158AbjAQI4N (ORCPT ); Tue, 17 Jan 2023 03:56:13 -0500 Received: from esa.microchip.iphmx.com (esa.microchip.iphmx.com [68.232.153.233]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 21D931816D; Tue, 17 Jan 2023 00:56:08 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=microchip.com; i=@microchip.com; q=dns/txt; s=mchp; t=1673945768; x=1705481768; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=GZcHBZbu5HtMonOR5Xd9wzrrmhkAZmUisdDYlTfIN/Q=; b=hRBUmQ8H6LpXSLw4Kv+zv/Z8cn0k4RKLXhcqWQHbMDxMMdhYiAty+nF6 8ahAHP9LGW03PtFeAbEvd/zrYUvITdyFXOnAWSQwWdOyr78veL+6eEKZg GbzLJ40xJ+w0lgp4Djy3ERlp/Avg0g3nFCNxE9hE3oe72CCUgum2Q9N7E vGNnH6ia+wyFd20Rep0idxdE86EEPWNgIoemCnW534o3DQWhoXF/k2JWN +QHZqsghh+gdCOmLEV987uTKW3Cdd3LRNjlXpF6nljlXPf/k+CWEmSUU/ ZzybWbrfj2N+7lBbvDAN3P0LCHDaUnc7iUppDdK4YuL5ZynFNDuu5asTb g==; X-IronPort-AV: E=Sophos;i="5.97,222,1669100400"; d="scan'208";a="197113715" Received: from unknown (HELO email.microchip.com) ([170.129.1.10]) by esa5.microchip.iphmx.com with ESMTP/TLS/AES256-SHA256; 17 Jan 2023 01:56:08 -0700 Received: from chn-vm-ex04.mchp-main.com (10.10.85.152) by chn-vm-ex01.mchp-main.com (10.10.85.143) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.16; Tue, 17 Jan 2023 01:56:02 -0700 Received: from den-dk-m31857.microchip.com (10.10.115.15) by chn-vm-ex04.mchp-main.com (10.10.85.152) with Microsoft SMTP Server id 15.1.2507.16 via Frontend Transport; Tue, 17 Jan 2023 01:55:58 -0700 From: Steen Hegelund To: "David S . Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni CC: Steen Hegelund , , Randy Dunlap , "Casper Andersson" , Russell King , Wan Jiabing , "Nathan Huckleberry" , , , , "Steen Hegelund" , Daniel Machon , Horatiu Vultur , Lars Povlsen , Dan Carpenter , Michael Walle Subject: [PATCH net-next 3/5] net: microchip: sparx5: Add VCAP admin locking in debugFS Date: Tue, 17 Jan 2023 09:55:42 +0100 Message-ID: <20230117085544.591523-4-steen.hegelund@microchip.com> X-Mailer: git-send-email 2.39.0 In-Reply-To: <20230117085544.591523-1-steen.hegelund@microchip.com> References: <20230117085544.591523-1-steen.hegelund@microchip.com> MIME-Version: 1.0 X-Spam-Status: No, score=-4.4 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED, RCVD_IN_MSPIKE_H2,SPF_HELO_PASS,SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1755259626934671461?= X-GMAIL-MSGID: =?utf-8?q?1755259626934671461?= This ensures that the admin lock is taken before the debugFS functions starts iterating the VCAP rules. It also adds a separate function to decode a rule, which expects the lock to have been taken before it is called. Signed-off-by: Steen Hegelund --- .../net/ethernet/microchip/vcap/vcap_api.c | 60 ++++++++++--------- .../microchip/vcap/vcap_api_debugfs.c | 14 ++++- .../microchip/vcap/vcap_api_private.h | 3 + 3 files changed, 47 insertions(+), 30 deletions(-) diff --git a/drivers/net/ethernet/microchip/vcap/vcap_api.c b/drivers/net/ethernet/microchip/vcap/vcap_api.c index f1dc4fd6bb96..198c36627ba1 100644 --- a/drivers/net/ethernet/microchip/vcap/vcap_api.c +++ b/drivers/net/ethernet/microchip/vcap/vcap_api.c @@ -2170,47 +2170,53 @@ void vcap_free_rule(struct vcap_rule *rule) } EXPORT_SYMBOL_GPL(vcap_free_rule); -struct vcap_rule *vcap_get_rule(struct vcap_control *vctrl, u32 id) +/* Decode a rule from the VCAP cache and return a copy */ +struct vcap_rule *vcap_decode_rule(struct vcap_rule_internal *elem) { - struct vcap_rule_internal *elem; struct vcap_rule_internal *ri; int err; - ri = NULL; - - err = vcap_api_check(vctrl); - if (err) - return ERR_PTR(err); - elem = vcap_lookup_rule(vctrl, id); - if (!elem) - return NULL; - mutex_lock(&elem->admin->lock); ri = vcap_dup_rule(elem, elem->state == VCAP_RS_DISABLED); if (IS_ERR(ri)) - goto unlock; + return ERR_PTR(PTR_ERR(ri)); if (ri->state == VCAP_RS_DISABLED) - goto unlock; + goto out; err = vcap_read_rule(ri); - if (err) { - ri = ERR_PTR(err); - goto unlock; - } + if (err) + return ERR_PTR(err); + err = vcap_decode_keyset(ri); - if (err) { - ri = ERR_PTR(err); - goto unlock; - } + if (err) + return ERR_PTR(err); + err = vcap_decode_actionset(ri); - if (err) { - ri = ERR_PTR(err); - goto unlock; - } + if (err) + return ERR_PTR(err); -unlock: +out: + return &ri->data; +} + +struct vcap_rule *vcap_get_rule(struct vcap_control *vctrl, u32 id) +{ + struct vcap_rule_internal *elem; + struct vcap_rule *rule; + int err; + + err = vcap_api_check(vctrl); + if (err) + return ERR_PTR(err); + + elem = vcap_lookup_rule(vctrl, id); + if (!elem) + return NULL; + + mutex_lock(&elem->admin->lock); + rule = vcap_decode_rule(elem); mutex_unlock(&elem->admin->lock); - return (struct vcap_rule *)ri; + return rule; } EXPORT_SYMBOL_GPL(vcap_get_rule); diff --git a/drivers/net/ethernet/microchip/vcap/vcap_api_debugfs.c b/drivers/net/ethernet/microchip/vcap/vcap_api_debugfs.c index dc06f6d4f513..d49b1cf7712f 100644 --- a/drivers/net/ethernet/microchip/vcap/vcap_api_debugfs.c +++ b/drivers/net/ethernet/microchip/vcap/vcap_api_debugfs.c @@ -295,7 +295,7 @@ static int vcap_show_admin(struct vcap_control *vctrl, vcap_show_admin_info(vctrl, admin, out); list_for_each_entry(elem, &admin->rules, list) { - vrule = vcap_get_rule(vctrl, elem->data.id); + vrule = vcap_decode_rule(elem); if (IS_ERR_OR_NULL(vrule)) { ret = PTR_ERR(vrule); break; @@ -404,8 +404,12 @@ static int vcap_debugfs_show(struct seq_file *m, void *unused) .prf = (void *)seq_printf, .dst = m, }; + int ret; - return vcap_show_admin(info->vctrl, info->admin, &out); + mutex_lock(&info->admin->lock); + ret = vcap_show_admin(info->vctrl, info->admin, &out); + mutex_unlock(&info->admin->lock); + return ret; } DEFINE_SHOW_ATTRIBUTE(vcap_debugfs); @@ -417,8 +421,12 @@ static int vcap_raw_debugfs_show(struct seq_file *m, void *unused) .prf = (void *)seq_printf, .dst = m, }; + int ret; - return vcap_show_admin_raw(info->vctrl, info->admin, &out); + mutex_lock(&info->admin->lock); + ret = vcap_show_admin_raw(info->vctrl, info->admin, &out); + mutex_unlock(&info->admin->lock); + return ret; } DEFINE_SHOW_ATTRIBUTE(vcap_raw_debugfs); diff --git a/drivers/net/ethernet/microchip/vcap/vcap_api_private.h b/drivers/net/ethernet/microchip/vcap/vcap_api_private.h index 86542accffe6..df81d9ff502b 100644 --- a/drivers/net/ethernet/microchip/vcap/vcap_api_private.h +++ b/drivers/net/ethernet/microchip/vcap/vcap_api_private.h @@ -118,4 +118,7 @@ int vcap_find_keystream_keysets(struct vcap_control *vctrl, enum vcap_type vt, /* Get the keysets that matches the rule key type/mask */ int vcap_rule_get_keysets(struct vcap_rule_internal *ri, struct vcap_keyset_list *matches); +/* Decode a rule from the VCAP cache and return a copy */ +struct vcap_rule *vcap_decode_rule(struct vcap_rule_internal *elem); + #endif /* __VCAP_API_PRIVATE__ */ From patchwork Tue Jan 17 08:55:43 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Steen Hegelund X-Patchwork-Id: 44562 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:eb09:0:0:0:0:0 with SMTP id s9csp1643301wrn; Tue, 17 Jan 2023 01:00:03 -0800 (PST) X-Google-Smtp-Source: AMrXdXvdrDmIWJnOLpnsQcOBHbUGopwI8w+w630MIuR3/VePHlI8oX8qnMe9R6mShf/YAwibdhas X-Received: by 2002:a17:902:6904:b0:193:3354:1c22 with SMTP id j4-20020a170902690400b0019333541c22mr1522952plk.39.1673946003381; Tue, 17 Jan 2023 01:00:03 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1673946003; cv=none; d=google.com; s=arc-20160816; b=JiijQNi5aEYumYdY22Z957ahbnx+eHfVFOznJC0YWwEr1XnuKmDTXniQM0Kyk4tjtA WugJMgIEhMtKQipnjXfj/Q8TuTnChjcTuoUJ3NvkDNr3lxlIXKRxE+USduOSUTgDW9KJ M0bfYl0jCgYJ3QigQV4nxq46Swi6AixqazOT5Lfjf8LjuvVX6RBAiuzf1UHEAybGyOwR 9Fs2QAkF91Aj94n3vgwxsOHP8aCy/CVhY86hqJPyInM8VMzJWqlOuqdWF+7KD+q+TroL s8PtoLGv51/iCc0GevLb4ZepkJkYQ7iu0FSuuuULyioFfdsPE6gqaZ35NenJ7FAN3f+z KtZA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=m8dBupZlLePFeokKFmHnlh3DyyotY3Ms4o5VWwtX0ys=; b=UH4l4M2J8ne6knYqs8JZ0XWMbqOaFjdY/dTy0/qfZkhTUfnTfarc5wprmqh8MgBf3H HMU39wiE2LhGgqPEWObHXIoPoTnDeR+y2LwTvzmasfOmaqD6WrmpRPAhYHY4DailDHAm nPMC5uSAc9jO2f0CN2JkyxMQTaY86F8pxokcl5TK6DzdcFrwYhyYO3h6BY8qLNI0kw/0 QkujztEn/ltmvSzs2m9PHIZ1Pe9U972AQvIsyROway5KYWyybbtctLlpc75ZZxQZhL8e NuzRRnVgtcQ0aEgZC+MWFfCIUzRKa1J+1V7+itpVhZXIq3MNualoyTa9uPUWUO4q/pBu 94Hw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@microchip.com header.s=mchp header.b=na7vQqEv; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=microchip.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id f10-20020a170902ff0a00b00194a944fec7si1458342plj.282.2023.01.17.00.59.51; Tue, 17 Jan 2023 01:00:03 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@microchip.com header.s=mchp header.b=na7vQqEv; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=microchip.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235943AbjAQI4o (ORCPT + 99 others); Tue, 17 Jan 2023 03:56:44 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58022 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235779AbjAQI4Z (ORCPT ); Tue, 17 Jan 2023 03:56:25 -0500 Received: from esa.microchip.iphmx.com (esa.microchip.iphmx.com [68.232.153.233]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7D0541A96A; Tue, 17 Jan 2023 00:56:10 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=microchip.com; i=@microchip.com; q=dns/txt; s=mchp; t=1673945771; x=1705481771; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=W98pjnf4ANO3DD+DNkbIvqiBKdIWDg5rmKA7LNbwvO0=; b=na7vQqEvnO+M6BSeDrDdNeB0bRnRqvXfT1saZb/3Tgyz+XtWl47hpvF5 gijl0/36rMnqDlrVGyCxg76E2MG0iiLLFUIwqagwCgEEy61QuPeKnOwbe z0XzivQgEeMxuNgb5YgoBZImbR4HY746bRbiP2GndJVr3+WTSuZUXEqk8 j7iN5AMJPsFaTrX4XTZeoy9OraZeb1LAlUqNTHbHK9ZlDk6Sb4MTIwlMA 1wBALUNNQYYJnM+Us/724ud0k0qI6RRXg9efMQk10Xvl0E8g+QP26tffv dxcQ8dWjzJEOlEa3Z4Yc7g3SVu/ilc/iHiOjD8DDuS8h0124q5FZmPdnr A==; X-IronPort-AV: E=Sophos;i="5.97,222,1669100400"; d="scan'208";a="197113729" Received: from unknown (HELO email.microchip.com) ([170.129.1.10]) by esa5.microchip.iphmx.com with ESMTP/TLS/AES256-SHA256; 17 Jan 2023 01:56:10 -0700 Received: from chn-vm-ex04.mchp-main.com (10.10.85.152) by chn-vm-ex01.mchp-main.com (10.10.85.143) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.16; Tue, 17 Jan 2023 01:56:06 -0700 Received: from den-dk-m31857.microchip.com (10.10.115.15) by chn-vm-ex04.mchp-main.com (10.10.85.152) with Microsoft SMTP Server id 15.1.2507.16 via Frontend Transport; Tue, 17 Jan 2023 01:56:02 -0700 From: Steen Hegelund To: "David S . Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni CC: Steen Hegelund , , Randy Dunlap , "Casper Andersson" , Russell King , Wan Jiabing , "Nathan Huckleberry" , , , , "Steen Hegelund" , Daniel Machon , Horatiu Vultur , Lars Povlsen , Dan Carpenter , Michael Walle Subject: [PATCH net-next 4/5] net: microchip: sparx5: Improve VCAP admin locking in the VCAP API Date: Tue, 17 Jan 2023 09:55:43 +0100 Message-ID: <20230117085544.591523-5-steen.hegelund@microchip.com> X-Mailer: git-send-email 2.39.0 In-Reply-To: <20230117085544.591523-1-steen.hegelund@microchip.com> References: <20230117085544.591523-1-steen.hegelund@microchip.com> MIME-Version: 1.0 X-Spam-Status: No, score=-4.4 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED, RCVD_IN_MSPIKE_H2,SPF_HELO_PASS,SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1755259603984803368?= X-GMAIL-MSGID: =?utf-8?q?1755259603984803368?= This improves the VCAP cache and the VCAP rule list protection against access from different sources. Signed-off-by: Steen Hegelund --- .../net/ethernet/microchip/vcap/vcap_api.c | 97 +++++++++++++------ 1 file changed, 67 insertions(+), 30 deletions(-) diff --git a/drivers/net/ethernet/microchip/vcap/vcap_api.c b/drivers/net/ethernet/microchip/vcap/vcap_api.c index 198c36627ba1..71f787a78295 100644 --- a/drivers/net/ethernet/microchip/vcap/vcap_api.c +++ b/drivers/net/ethernet/microchip/vcap/vcap_api.c @@ -934,18 +934,21 @@ static bool vcap_rule_exists(struct vcap_control *vctrl, u32 id) return false; } -/* Find a rule with a provided rule id */ -static struct vcap_rule_internal *vcap_lookup_rule(struct vcap_control *vctrl, - u32 id) +/* Find a rule with a provided rule id return a locked vcap */ +static struct vcap_rule_internal * +vcap_get_locked_rule(struct vcap_control *vctrl, u32 id) { struct vcap_rule_internal *ri; struct vcap_admin *admin; /* Look for the rule id in all vcaps */ - list_for_each_entry(admin, &vctrl->list, list) + list_for_each_entry(admin, &vctrl->list, list) { + mutex_lock(&admin->lock); list_for_each_entry(ri, &admin->rules, list) if (ri->data.id == id) return ri; + mutex_unlock(&admin->lock); + } return NULL; } @@ -954,12 +957,21 @@ int vcap_lookup_rule_by_cookie(struct vcap_control *vctrl, u64 cookie) { struct vcap_rule_internal *ri; struct vcap_admin *admin; + int id = 0; /* Look for the rule id in all vcaps */ - list_for_each_entry(admin, &vctrl->list, list) - list_for_each_entry(ri, &admin->rules, list) - if (ri->data.cookie == cookie) - return ri->data.id; + list_for_each_entry(admin, &vctrl->list, list) { + mutex_lock(&admin->lock); + list_for_each_entry(ri, &admin->rules, list) { + if (ri->data.cookie == cookie) { + id = ri->data.id; + break; + } + } + mutex_unlock(&admin->lock); + if (id) + return id; + } return -ENOENT; } EXPORT_SYMBOL_GPL(vcap_lookup_rule_by_cookie); @@ -2116,17 +2128,28 @@ struct vcap_rule *vcap_alloc_rule(struct vcap_control *vctrl, /* Sanity check that this VCAP is supported on this platform */ if (vctrl->vcaps[admin->vtype].rows == 0) return ERR_PTR(-EINVAL); + + mutex_lock(&admin->lock); /* Check if a rule with this id already exists */ - if (vcap_rule_exists(vctrl, id)) - return ERR_PTR(-EEXIST); + if (vcap_rule_exists(vctrl, id)) { + err = -EINVAL; + goto out_unlock; + } + /* Check if there is room for the rule in the block(s) of the VCAP */ maxsize = vctrl->vcaps[admin->vtype].sw_count; /* worst case rule size */ - if (vcap_rule_space(admin, maxsize)) - return ERR_PTR(-ENOSPC); + if (vcap_rule_space(admin, maxsize)) { + err = -ENOSPC; + goto out_unlock; + } + /* Create a container for the rule and return it */ ri = kzalloc(sizeof(*ri), GFP_KERNEL); - if (!ri) - return ERR_PTR(-ENOMEM); + if (!ri) { + err = -ENOMEM; + goto out_unlock; + } + ri->data.vcap_chain_id = vcap_chain_id; ri->data.user = user; ri->data.priority = priority; @@ -2139,13 +2162,21 @@ struct vcap_rule *vcap_alloc_rule(struct vcap_control *vctrl, ri->ndev = ndev; ri->admin = admin; /* refer to the vcap instance */ ri->vctrl = vctrl; /* refer to the client */ - if (vcap_set_rule_id(ri) == 0) + + if (vcap_set_rule_id(ri) == 0) { + err = -EINVAL; goto out_free; + } + + mutex_unlock(&admin->lock); return (struct vcap_rule *)ri; out_free: kfree(ri); - return ERR_PTR(-EINVAL); +out_unlock: + mutex_unlock(&admin->lock); + return ERR_PTR(err); + } EXPORT_SYMBOL_GPL(vcap_alloc_rule); @@ -2209,11 +2240,10 @@ struct vcap_rule *vcap_get_rule(struct vcap_control *vctrl, u32 id) if (err) return ERR_PTR(err); - elem = vcap_lookup_rule(vctrl, id); + elem = vcap_get_locked_rule(vctrl, id); if (!elem) return NULL; - mutex_lock(&elem->admin->lock); rule = vcap_decode_rule(elem); mutex_unlock(&elem->admin->lock); return rule; @@ -2231,11 +2261,9 @@ int vcap_mod_rule(struct vcap_rule *rule) if (err) return err; - if (!vcap_lookup_rule(ri->vctrl, ri->data.id)) + if (!vcap_get_locked_rule(ri->vctrl, ri->data.id)) return -ENOENT; - mutex_lock(&ri->admin->lock); - vcap_rule_set_state(ri); if (ri->state == VCAP_RS_DISABLED) goto out; @@ -2252,8 +2280,6 @@ int vcap_mod_rule(struct vcap_rule *rule) memset(&ctr, 0, sizeof(ctr)); err = vcap_write_counter(ri, &ctr); - if (err) - goto out; out: mutex_unlock(&ri->admin->lock); @@ -2320,20 +2346,19 @@ int vcap_del_rule(struct vcap_control *vctrl, struct net_device *ndev, u32 id) if (err) return err; /* Look for the rule id in all vcaps */ - ri = vcap_lookup_rule(vctrl, id); + ri = vcap_get_locked_rule(vctrl, id); if (!ri) - return -EINVAL; + return -ENOENT; + admin = ri->admin; if (ri->addr > admin->last_used_addr) gap = vcap_fill_rule_gap(ri); /* Delete the rule from the list of rules and the cache */ - mutex_lock(&admin->lock); list_del(&ri->list); vctrl->ops->init(ndev, admin, admin->last_used_addr, ri->size + gap); vcap_free_rule(&ri->data); - mutex_unlock(&admin->lock); /* Update the last used address, set to default when no rules */ if (list_empty(&admin->rules)) { @@ -2343,7 +2368,9 @@ int vcap_del_rule(struct vcap_control *vctrl, struct net_device *ndev, u32 id) list); admin->last_used_addr = elem->addr; } - return 0; + + mutex_unlock(&admin->lock); + return err; } EXPORT_SYMBOL_GPL(vcap_del_rule); @@ -3021,7 +3048,12 @@ int vcap_rule_set_counter(struct vcap_rule *rule, struct vcap_counter *ctr) pr_err("%s:%d: counter is missing\n", __func__, __LINE__); return -EINVAL; } - return vcap_write_counter(ri, ctr); + + mutex_lock(&ri->admin->lock); + err = vcap_write_counter(ri, ctr); + mutex_unlock(&ri->admin->lock); + + return err; } EXPORT_SYMBOL_GPL(vcap_rule_set_counter); @@ -3037,7 +3069,12 @@ int vcap_rule_get_counter(struct vcap_rule *rule, struct vcap_counter *ctr) pr_err("%s:%d: counter is missing\n", __func__, __LINE__); return -EINVAL; } - return vcap_read_counter(ri, ctr); + + mutex_lock(&ri->admin->lock); + err = vcap_read_counter(ri, ctr); + mutex_unlock(&ri->admin->lock); + + return err; } EXPORT_SYMBOL_GPL(vcap_rule_get_counter); From patchwork Tue Jan 17 08:55:44 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Steen Hegelund X-Patchwork-Id: 44566 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:eb09:0:0:0:0:0 with SMTP id s9csp1643837wrn; Tue, 17 Jan 2023 01:01:08 -0800 (PST) X-Google-Smtp-Source: AMrXdXviUtXTOO9PlPPf3cuxWVMvRuedcm/BMs3qaztFYmB5adMohC8yb190stQkzvgENaxcnKkE X-Received: by 2002:a17:902:a714:b0:189:ba1f:b168 with SMTP id w20-20020a170902a71400b00189ba1fb168mr2848125plq.1.1673946068518; Tue, 17 Jan 2023 01:01:08 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1673946068; cv=none; d=google.com; s=arc-20160816; b=jvPFJhSULqQQIqgpfA/2SA4ZvQwKH+2qvTySvMMvwhwjQj9QiqvcZMPPMynQ1tuNDZ hm4QqzX1BaB0c/nCs2YjtqgA2BGC7cSIMOLnTwp+Yt9Zv17wqgBvC/vbKak8fMoJ8/Kf CHUl8Hvu7mhNpwNAhnD5Wn65NLWpWQosHVmerzLAUlSPmVhrKTmhchiHqRQg6QUHiUua YWBC/LXLNxF+LSk131C9Jz6V4E+T/qK/wtaX07iNZRoc6bh38MSL6cRo0fFtSZqI1rKm cezkZCpGyqoGBFdS+3Qxj5yL0ho40muz2kna+0RasGLR60tAl61gkIxLtCLYE7cWZGLR ITBg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=7SNRLQ28Mtiyl7/4saiYiCkiDayp+Xg11zc6TxSxsGg=; b=VawQF+8k300U7wCal/g238Kx6PXxkYRiz0rChjVk02P90mZ5pVwCDMjMxevmj2puhQ 80q4/OxoBe9zaFC4CWTX9s6e/Us3xNEhYsgOS8QlzZi5Sos8UkrZMCJZd1PATI0wrs0q p4E2IVE6c8nJ4OsQhrNlQ8VIZZPwS5/HL6oi7JkJESoPyV/SI8xAtcvxawbhql5DufYN XTkNg6ufS0pE6nhinWAIGO36/K8QZh4tsPwqos2xv7/1iWrWwTq8xUWMdzBPWzNqG/VV 82xjUpgtsKKWPgrEEc207izb1lExne0igTJNkfPHOfp6t7rDRvfggIoP8NeR/fCQKJnX St2g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@microchip.com header.s=mchp header.b=JD1sTN5y; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=microchip.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id a4-20020a170902ecc400b00189aab9cdbcsi34082700plh.605.2023.01.17.01.00.55; Tue, 17 Jan 2023 01:01:08 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@microchip.com header.s=mchp header.b=JD1sTN5y; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=microchip.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235796AbjAQI5L (ORCPT + 99 others); Tue, 17 Jan 2023 03:57:11 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58740 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234517AbjAQI4c (ORCPT ); Tue, 17 Jan 2023 03:56:32 -0500 Received: from esa.microchip.iphmx.com (esa.microchip.iphmx.com [68.232.153.233]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id DFB4410ABE; Tue, 17 Jan 2023 00:56:12 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=microchip.com; i=@microchip.com; q=dns/txt; s=mchp; t=1673945772; x=1705481772; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=qQNh55ZgYhBc07wztLVkY1eS2Pi1LYlQR49lenb2Z8M=; b=JD1sTN5yeQN/UWlraeVKKZpD/TBSnMbmXVrXhlXo4tI+jGy5H0R3rnHt 4njkp4u9GitCsWZ82Do+S5t3cLteAZoOb8mOQm+HUx2bjc0HFxaypOBvq w67rKW+kWN2UT1tG6kr4Oct1XXs6/Uwx5QqEMMJWMWleA3YS95GdexDuH sivZixqCowzTy/dSsUfRRwcYhUyHudOrx7QCv+d9P9mem/8OI6dDgo/w8 Oc2K6Q4CxM3QVdHkq0hGL7GNgJouz2U/0OS7cFkdtz7KT5gZe50wYfcsQ gWbLkAgxggtbTfFisGSNfQXrWlSC2/D8q6hvotIURchyxpE8cwb4kSxn1 g==; X-IronPort-AV: E=Sophos;i="5.97,222,1669100400"; d="scan'208";a="208096731" Received: from unknown (HELO email.microchip.com) ([170.129.1.10]) by esa1.microchip.iphmx.com with ESMTP/TLS/AES256-SHA256; 17 Jan 2023 01:56:10 -0700 Received: from chn-vm-ex04.mchp-main.com (10.10.85.152) by chn-vm-ex04.mchp-main.com (10.10.85.152) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.16; Tue, 17 Jan 2023 01:56:09 -0700 Received: from den-dk-m31857.microchip.com (10.10.115.15) by chn-vm-ex04.mchp-main.com (10.10.85.152) with Microsoft SMTP Server id 15.1.2507.16 via Frontend Transport; Tue, 17 Jan 2023 01:56:06 -0700 From: Steen Hegelund To: "David S . Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni CC: Steen Hegelund , , Randy Dunlap , "Casper Andersson" , Russell King , Wan Jiabing , "Nathan Huckleberry" , , , , "Steen Hegelund" , Daniel Machon , Horatiu Vultur , Lars Povlsen , Dan Carpenter , Michael Walle Subject: [PATCH net-next 5/5] net: microchip: sparx5: Add lock initialization to the KUNIT tests Date: Tue, 17 Jan 2023 09:55:44 +0100 Message-ID: <20230117085544.591523-6-steen.hegelund@microchip.com> X-Mailer: git-send-email 2.39.0 In-Reply-To: <20230117085544.591523-1-steen.hegelund@microchip.com> References: <20230117085544.591523-1-steen.hegelund@microchip.com> MIME-Version: 1.0 X-Spam-Status: No, score=-4.4 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED, RCVD_IN_MSPIKE_H2,SPF_HELO_PASS,SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1755259672816888502?= X-GMAIL-MSGID: =?utf-8?q?1755259672816888502?= Ensure that the KUNIT tests lock instance is initialized before the test is executed. Signed-off-by: Steen Hegelund --- drivers/net/ethernet/microchip/vcap/vcap_api_debugfs_kunit.c | 1 + drivers/net/ethernet/microchip/vcap/vcap_api_kunit.c | 1 + 2 files changed, 2 insertions(+) diff --git a/drivers/net/ethernet/microchip/vcap/vcap_api_debugfs_kunit.c b/drivers/net/ethernet/microchip/vcap/vcap_api_debugfs_kunit.c index 9211cb453a3d..cbf7e0f110b8 100644 --- a/drivers/net/ethernet/microchip/vcap/vcap_api_debugfs_kunit.c +++ b/drivers/net/ethernet/microchip/vcap/vcap_api_debugfs_kunit.c @@ -246,6 +246,7 @@ static void vcap_test_api_init(struct vcap_admin *admin) INIT_LIST_HEAD(&admin->list); INIT_LIST_HEAD(&admin->rules); INIT_LIST_HEAD(&admin->enabled); + mutex_init(&admin->lock); list_add_tail(&admin->list, &test_vctrl.list); memset(test_updateaddr, 0, sizeof(test_updateaddr)); test_updateaddridx = 0; diff --git a/drivers/net/ethernet/microchip/vcap/vcap_api_kunit.c b/drivers/net/ethernet/microchip/vcap/vcap_api_kunit.c index 22690c669028..82981176218c 100644 --- a/drivers/net/ethernet/microchip/vcap/vcap_api_kunit.c +++ b/drivers/net/ethernet/microchip/vcap/vcap_api_kunit.c @@ -236,6 +236,7 @@ static void vcap_test_api_init(struct vcap_admin *admin) INIT_LIST_HEAD(&admin->list); INIT_LIST_HEAD(&admin->rules); INIT_LIST_HEAD(&admin->enabled); + mutex_init(&admin->lock); list_add_tail(&admin->list, &test_vctrl.list); memset(test_updateaddr, 0, sizeof(test_updateaddr)); test_updateaddridx = 0;