Received: by 10.223.164.221 with SMTP id h29csp356911wrb; Fri, 3 Nov 2017 16:10:12 -0700 (PDT) X-Google-Smtp-Source: ABhQp+To79n/uWUvDClSpAZoohoE0o1H9qCX27uTnkk7axe/aniQq6z6MbxqpaZ3EpMzY5LhKDBm X-Received: by 10.98.58.92 with SMTP id h89mr9297917pfa.106.1509750612349; Fri, 03 Nov 2017 16:10:12 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1509750612; cv=none; d=google.com; s=arc-20160816; b=DutIW7rG5JwVlnyxBEEx770ygeshx4kNILVi696ba/ufM6TeIxvgtWL6y+PhWWxd3U fGIIHCRef4CfdcXYlaUn/PteuKbBfSjVnvIX0clVOGPqWz6Z03tSEwRIuTuWsauF10rt FgA6niSWTGsdNGJyxySShh8iFdPKU/XCh1p0ssKuQoTWpgwy/ynoxShNe75BMPD6FR33 hUNt5igIAZdjN/qW2iDHLPsg3Ud58Y1gtcS61LXXYdEsLxQygRAwVDK9IEAcPwpwCVzb ZE8BQcoYnCAHdL0ImCf78kFgguMvUm965MhdAvWyTk8x23/2dIqrc9MCswkZMrxO0G1V KmAA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:references:in-reply-to:message-id:date :subject:cc:to:from:arc-authentication-results; bh=GBn2/HdFuPmzqDAqbaD3WpFF8Z7u27tOtLqYZKOF80c=; b=o+rOZhbEkAbtZP5NcB3+qwv/bM/3pbpL1NHzEZ4DZi/S2kWeTkILsA29Pts9zZV2wZ ISqB/DwrJBVBMpsDh2U763p5LRNunS8eP1g6PDRPjK0KqmAs8mwJWcOAHsZ7w1YsoRHr h3CT3ffsQ0khizSKUpqVOmNFNHPjL2fiL1GUVkHBH1G3UmFD+ngjdnPSj86IW1R4sfry +4pQXsgQcyfW3QIJeP54phupM7CbHV4ezDNvb3BqIHANNzH8hiG8v1XAlFjpUfaftDST NtQzmAN3xrx2ROS0qN92ZDjbZ6N0ZjyfKyhFENhaw3bClqy91zdgF8bhpVE5P9qWPL4r 2Z4A== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id x24si5573988pln.821.2017.11.03.16.09.59; Fri, 03 Nov 2017 16:10:12 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1756613AbdKCXIh (ORCPT + 92 others); Fri, 3 Nov 2017 19:08:37 -0400 Received: from mail.savoirfairelinux.com ([208.88.110.44]:33340 "EHLO mail.savoirfairelinux.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1756398AbdKCXF7 (ORCPT ); Fri, 3 Nov 2017 19:05:59 -0400 Received: from localhost (localhost [127.0.0.1]) by mail.savoirfairelinux.com (Postfix) with ESMTP id EB4AE9C2F53; Fri, 3 Nov 2017 19:05:58 -0400 (EDT) Received: from mail.savoirfairelinux.com ([127.0.0.1]) by localhost (mail.savoirfairelinux.com [127.0.0.1]) (amavisd-new, port 10032) with ESMTP id MvGNPcduGLr2; Fri, 3 Nov 2017 19:05:46 -0400 (EDT) Received: from localhost (localhost [127.0.0.1]) by mail.savoirfairelinux.com (Postfix) with ESMTP id C4FA29C2F47; Fri, 3 Nov 2017 19:05:46 -0400 (EDT) X-Virus-Scanned: amavisd-new at mail.savoirfairelinux.com Received: from mail.savoirfairelinux.com ([127.0.0.1]) by localhost (mail.savoirfairelinux.com [127.0.0.1]) (amavisd-new, port 10026) with ESMTP id dMSYDl-tGN2S; Fri, 3 Nov 2017 19:05:46 -0400 (EDT) Received: from weeman.mtl.sfl (unknown [192.168.49.192]) by mail.savoirfairelinux.com (Postfix) with ESMTPSA id 8C16C9C2F4D; Fri, 3 Nov 2017 19:05:46 -0400 (EDT) From: Vivien Didelot To: netdev@vger.kernel.org Cc: linux-kernel@vger.kernel.org, kernel@savoirfairelinux.com, "David S. Miller" , Florian Fainelli , Andrew Lunn , Vivien Didelot Subject: [PATCH net-next 03/11] net: dsa: simplify tree reference counting Date: Fri, 3 Nov 2017 19:05:22 -0400 Message-Id: <20171103230530.996-4-vivien.didelot@savoirfairelinux.com> X-Mailer: git-send-email 2.14.3 In-Reply-To: <20171103230530.996-1-vivien.didelot@savoirfairelinux.com> References: <20171103230530.996-1-vivien.didelot@savoirfairelinux.com> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org DSA trees have a refcount used to automatically free the dsa_switch_tree structure once there is no switch devices inside of it. The refcount is incremented when a switch is added to the tree, and decremented when it is removed from it. But because of kref_init, the refcount is also incremented at initialization, and when looking up the tree from the list for symmetry. Thus the current code stores the number of switches plus one, and makes the switch registration more complex. To simplify the switch registration function, we reset the refcount to zero after initialization and don't increment it when looking up a tree. Signed-off-by: Vivien Didelot --- net/dsa/dsa2.c | 30 ++++++++++-------------------- 1 file changed, 10 insertions(+), 20 deletions(-) diff --git a/net/dsa/dsa2.c b/net/dsa/dsa2.c index 8b68dc2f5707..d3f1a7607463 100644 --- a/net/dsa/dsa2.c +++ b/net/dsa/dsa2.c @@ -32,10 +32,9 @@ static struct dsa_switch_tree *dsa_get_dst(unsigned int index) struct dsa_switch_tree *dst; list_for_each_entry(dst, &dsa_switch_trees, list) - if (dst->index == index) { - kref_get(&dst->refcount); + if (dst->index == index) return dst; - } + return NULL; } @@ -48,11 +47,6 @@ static void dsa_free_dst(struct kref *ref) kfree(dst); } -static void dsa_put_dst(struct dsa_switch_tree *dst) -{ - kref_put(&dst->refcount, dsa_free_dst); -} - static struct dsa_switch_tree *dsa_add_dst(unsigned int index) { struct dsa_switch_tree *dst; @@ -63,7 +57,10 @@ static struct dsa_switch_tree *dsa_add_dst(unsigned int index) dst->index = index; INIT_LIST_HEAD(&dst->list); list_add_tail(&dsa_switch_trees, &dst->list); + + /* Initialize the reference counter to the number of switches, not 1 */ kref_init(&dst->refcount); + refcount_set(&dst->refcount.refcount, 0); return dst; } @@ -739,10 +736,8 @@ static int _dsa_register_switch(struct dsa_switch *ds) return -ENOMEM; } - if (dst->ds[index]) { - err = -EBUSY; - goto out; - } + if (dst->ds[index]) + return -EBUSY; ds->dst = dst; ds->index = index; @@ -758,11 +753,9 @@ static int _dsa_register_switch(struct dsa_switch *ds) if (err < 0) goto out_del_dst; - if (err == 1) { - /* Not all switches registered yet */ - err = 0; - goto out; - } + /* Not all switches registered yet */ + if (err == 1) + return 0; if (dst->applied) { pr_info("DSA: Disjoint trees?\n"); @@ -779,13 +772,10 @@ static int _dsa_register_switch(struct dsa_switch *ds) goto out_del_dst; } - dsa_put_dst(dst); return 0; out_del_dst: dsa_dst_del_ds(dst, ds, ds->index); -out: - dsa_put_dst(dst); return err; } -- 2.14.3 From 1583074286908593564@xxx Fri Nov 03 19:28:08 +0000 2017 X-GM-THRID: 1583074286908593564 X-Gmail-Labels: Inbox,Category Forums,HistoricalUnread