Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.2 required=3.0 tests=DKIMWL_WL_MED,DKIM_SIGNED, DKIM_VALID,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_PATCH,MAILING_LIST_MULTI, SIGNED_OFF_BY,SPF_PASS,UNWANTED_LANGUAGE_BODY,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 70E21C43381 for ; Sun, 24 Mar 2019 10:14:58 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 3D921222A7 for ; Sun, 24 Mar 2019 10:14:58 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=resnulli-us.20150623.gappssmtp.com header.i=@resnulli-us.20150623.gappssmtp.com header.b="G3/Jz+in" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728775AbfCXKO4 (ORCPT ); Sun, 24 Mar 2019 06:14:56 -0400 Received: from mail-wm1-f67.google.com ([209.85.128.67]:35698 "EHLO mail-wm1-f67.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728746AbfCXKOy (ORCPT ); Sun, 24 Mar 2019 06:14:54 -0400 Received: by mail-wm1-f67.google.com with SMTP id y197so6100049wmd.0 for ; Sun, 24 Mar 2019 03:14:53 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=resnulli-us.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=sQzKubsoG3ZvLWm8bdo496MWcHVn9y222UqebVQu6fc=; b=G3/Jz+inkLt7ujLdnl9roYPTChawGi5sizoTzwLnHFlRPmFg329wvAr1qcxuMVOCpQ 9KbPzU7WRjKaB5b62z1br94oPI9wybhbv7gUomXhfcu19VJ/dcB/n+O1djTEP1w+vPtP wMrdarv+xeUybGem2hoNxNR0R/a94PUE1eSBTSBrRHVHfNdzZiFsGex4kjXM2nu3M2dL wlG1guvn9gQyIzVVm/MCtiJuNajqY1kBb1QRc+rbNnUagNnt0haclJ5OPkyxrDivDiR+ 4yuuBpGvEIE/2c5p9BNZ3/WDfgpwXPnxrHZTF2yJ1TndjsdsWsRH82o9ckP08uJiSAHN z+Ng== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=sQzKubsoG3ZvLWm8bdo496MWcHVn9y222UqebVQu6fc=; b=fPiSmWfR6zxlt+oDnJFwDKWcgjvBsr2IzwZw1PeOAhffmp5wbOFa7Ir6YznRW0kKw3 zGC9W+vipEr/DK0iLlInkRSx1p3ncocT8c8O03fBzOGnyLtkceL02ahHrLcn2a4kouuQ Fb/g8RNtAQt8mqzml0uhpIA2fk2aeRXnLO+A4w5TtIFYJCzgqREqxLyj1B8sA18KYfVk gQUi016rhcfCvnbYvWeY9AMYFZo+IqTa1NR2jyns/Rzn8UJsrB5HuZQcnG+qqwFio+ZQ jrGYIf7Jbhm+4yeuyAFLWjNGqEgcB96GROZ9XZZ6uOCEvIJ/p8V1n222VtpOK7q9Nqdv e6sA== X-Gm-Message-State: APjAAAVlUjwzb69FzLlHpcmsTCmJ63iui2Gss1EBq9FviKE/0M/ldEw0 CV47bQGbEqaiw8YrKR8sFoiyGgYOkCI= X-Google-Smtp-Source: APXvYqynzPMU/HShTgGq/H6H8HP2yA+Bg8sjBmIouK1ewxTS4AOH3luUUbN9Aevn3yJBGV/daJ0/sg== X-Received: by 2002:a1c:230e:: with SMTP id j14mr8331160wmj.120.1553422492761; Sun, 24 Mar 2019 03:14:52 -0700 (PDT) Received: from localhost (ip-94-113-125-71.net.upcbroadband.cz. [94.113.125.71]) by smtp.gmail.com with ESMTPSA id x192sm8336319wmf.48.2019.03.24.03.14.52 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Sun, 24 Mar 2019 03:14:52 -0700 (PDT) From: Jiri Pirko To: netdev@vger.kernel.org Cc: davem@davemloft.net, mlxsw@mellanox.com, idosch@mellanox.com, jakub.kicinski@netronome.com, f.fainelli@gmail.com, andrew@lunn.ch, vivien.didelot@gmail.com Subject: [patch net-next v3 14/15] net: devlink: add port type spinlock Date: Sun, 24 Mar 2019 11:14:37 +0100 Message-Id: <20190324101438.6376-15-jiri@resnulli.us> X-Mailer: git-send-email 2.17.2 In-Reply-To: <20190324101438.6376-1-jiri@resnulli.us> References: <20190324101438.6376-1-jiri@resnulli.us> Sender: netdev-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org From: Jiri Pirko Add spinlock to protect port type and type_dev pointer consistency. Without that, userspace may see inconsistent type and type_dev combinations. Signed-off-by: Jiri Pirko v1->v2: - rebased --- include/net/devlink.h | 4 ++++ net/core/devlink.c | 17 +++++++++++++---- 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/include/net/devlink.h b/include/net/devlink.h index 63de99e09f04..cb9b060033e1 100644 --- a/include/net/devlink.h +++ b/include/net/devlink.h @@ -16,6 +16,7 @@ #include #include #include +#include #include #include @@ -53,6 +54,9 @@ struct devlink_port { struct devlink *devlink; unsigned index; bool registered; + spinlock_t type_lock; /* Protects type and type_dev + * pointer consistency. + */ enum devlink_port_type type; enum devlink_port_type desired_type; void *type_dev; diff --git a/net/core/devlink.c b/net/core/devlink.c index b771b00a52a0..4de4197243a9 100644 --- a/net/core/devlink.c +++ b/net/core/devlink.c @@ -19,6 +19,7 @@ #include #include #include +#include #include #include #include @@ -543,12 +544,14 @@ static int devlink_nl_port_fill(struct sk_buff *msg, struct devlink *devlink, goto nla_put_failure; if (nla_put_u32(msg, DEVLINK_ATTR_PORT_INDEX, devlink_port->index)) goto nla_put_failure; + + spin_lock(&devlink_port->type_lock); if (nla_put_u16(msg, DEVLINK_ATTR_PORT_TYPE, devlink_port->type)) - goto nla_put_failure; + goto nla_put_failure_type_locked; if (devlink_port->desired_type != DEVLINK_PORT_TYPE_NOTSET && nla_put_u16(msg, DEVLINK_ATTR_PORT_DESIRED_TYPE, devlink_port->desired_type)) - goto nla_put_failure; + goto nla_put_failure_type_locked; if (devlink_port->type == DEVLINK_PORT_TYPE_ETH) { struct net_device *netdev = devlink_port->type_dev; @@ -557,7 +560,7 @@ static int devlink_nl_port_fill(struct sk_buff *msg, struct devlink *devlink, netdev->ifindex) || nla_put_string(msg, DEVLINK_ATTR_PORT_NETDEV_NAME, netdev->name))) - goto nla_put_failure; + goto nla_put_failure_type_locked; } if (devlink_port->type == DEVLINK_PORT_TYPE_IB) { struct ib_device *ibdev = devlink_port->type_dev; @@ -565,14 +568,17 @@ static int devlink_nl_port_fill(struct sk_buff *msg, struct devlink *devlink, if (ibdev && nla_put_string(msg, DEVLINK_ATTR_PORT_IBDEV_NAME, ibdev->name)) - goto nla_put_failure; + goto nla_put_failure_type_locked; } + spin_unlock(&devlink_port->type_lock); if (devlink_nl_port_attrs_put(msg, devlink_port)) goto nla_put_failure; genlmsg_end(msg, hdr); return 0; +nla_put_failure_type_locked: + spin_unlock(&devlink_port->type_lock); nla_put_failure: genlmsg_cancel(msg, hdr); return -EMSGSIZE; @@ -5342,6 +5348,7 @@ int devlink_port_register(struct devlink *devlink, devlink_port->devlink = devlink; devlink_port->index = port_index; devlink_port->registered = true; + spin_lock_init(&devlink_port->type_lock); list_add_tail(&devlink_port->list, &devlink->port_list); INIT_LIST_HEAD(&devlink_port->param_list); mutex_unlock(&devlink->lock); @@ -5372,8 +5379,10 @@ static void __devlink_port_type_set(struct devlink_port *devlink_port, { if (WARN_ON(!devlink_port->registered)) return; + spin_lock(&devlink_port->type_lock); devlink_port->type = type; devlink_port->type_dev = type_dev; + spin_unlock(&devlink_port->type_lock); devlink_port_notify(devlink_port, DEVLINK_CMD_PORT_NEW); } -- 2.17.2