Received: by 2002:a25:8b91:0:0:0:0:0 with SMTP id j17csp120290ybl; Tue, 10 Dec 2019 19:00:39 -0800 (PST) X-Google-Smtp-Source: APXvYqx8OW4ui+FyyEHqoZM8X6z+wTfBmT41jNTfaoY/3gILwquzM+mfJf2FXf/B6h0krGfNjW/b X-Received: by 2002:aca:f305:: with SMTP id r5mr1086823oih.174.1576033239247; Tue, 10 Dec 2019 19:00:39 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1576033239; cv=none; d=google.com; s=arc-20160816; b=A8sdNxWLtpF0T6hBPhw/dXMgY2gVyuHgNBheA9niZU/LKryHvWSDGqhtbDRCYD1cSK EemSZ7JJkzF1JUWQRCu00Ilazel04D1KF2ysItoD4rRpQ0qLIxo1NDsZJgYQ/jHfOxRb bkLvqtT7daSGp5WoaY5v1tbFCtLkb3EkHhKlNwJxpzZCWi7tHXXneyMg1Hm/SFoly9ED UzckTRzpeCf7Ssv6AbYUo/bVP/fbpTbnNfDe8TWiMaHEkNg8JgHl4sSSdky4Ya/xgW2Y /XirN7nc++ulMAuNT90oY0IpRvNenWEolUTAEC3JURlrXMtMhIjDT2FGrCo4DFhbl6N9 3JDg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:to:message-id:date:subject:cc:from; bh=qLDvbe3/jBTMz5Z9bIkrqDILgbG3wLRm5cW4d8eG6q0=; b=mk5blpmT42GP7S788RR70J1FEbFGflEBNCmHi/N//UHO4yQCao+RAlaV3OsiuewA79 hdxF6zgj0o7ns1XYUgc+wIh2VVyC3vRfdY+AMXDfjlGTM+S8ovwss97ZDwfHdHjt0W6h ArL3T3IacLXX+pjwW8opD3aELddMt/VKMJo+dqbpTcZVMKKDAgCD6vdkR6iKEeVm/QyA qT5g5hCM8t8EqSB5rzmXhJDVvan0o3uqVSF3FnUU8U4UayQ3Mqk/Qkp8DMqzNekuauHG vnD+n3caYNog5cxBvJiDtOHzcsYXFYO4MhqMtPr/Mwn3TfgnLKwWeMnklFezaoCyXp1N i9cg== 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; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=mellanox.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id i15si241956otk.120.2019.12.10.19.00.26; Tue, 10 Dec 2019 19:00:39 -0800 (PST) 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; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=mellanox.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727217AbfLKC7A (ORCPT + 99 others); Tue, 10 Dec 2019 21:59:00 -0500 Received: from mail-il-dmz.mellanox.com ([193.47.165.129]:40273 "EHLO mellanox.co.il" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1727197AbfLKC7A (ORCPT ); Tue, 10 Dec 2019 21:59:00 -0500 Received: from Internal Mail-Server by MTLPINE1 (envelope-from yuvalav@mellanox.com) with ESMTPS (AES256-SHA encrypted); 11 Dec 2019 04:58:58 +0200 Received: from sw-mtx-008.mtx.labs.mlnx (sw-mtx-008.mtx.labs.mlnx [10.9.150.35]) by labmailer.mlnx (8.13.8/8.13.8) with ESMTP id xBB2wv6U003700; Wed, 11 Dec 2019 04:58:58 +0200 Received: from sw-mtx-008.mtx.labs.mlnx (localhost [127.0.0.1]) by sw-mtx-008.mtx.labs.mlnx (8.14.7/8.14.7) with ESMTP id xBB2wuAl018882; Wed, 11 Dec 2019 04:58:56 +0200 Received: (from yuvalav@localhost) by sw-mtx-008.mtx.labs.mlnx (8.14.7/8.14.7/Submit) id xBB2wt3s018881; Wed, 11 Dec 2019 04:58:55 +0200 From: Yuval Avnery Cc: jiri@mellanox.com, jakub.kicinski@netronome.com, davem@davemloft.net, netdev@vger.kernel.org, linux-kernel@vger.kernel.org, Yuval Avnery Subject: [PATCH net-next] netdevsim: Add max_vfs to bus_dev Date: Wed, 11 Dec 2019 04:58:53 +0200 Message-Id: <1576033133-18845-1-git-send-email-yuvalav@mellanox.com> X-Mailer: git-send-email 1.8.3.1 To: unlisted-recipients:; (no To-header on input) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Currently there is no limit to the number of VFs netdevsim can enable. In a real systems this value exist and used by driver. Fore example, Some features might need to consider this value when allocating memory. Signed-off-by: Yuval Avnery Acked-by: Jiri Pirko --- drivers/net/netdevsim/bus.c | 39 +++++++++++++++++++++++-------- drivers/net/netdevsim/netdevsim.h | 1 + 2 files changed, 30 insertions(+), 10 deletions(-) diff --git a/drivers/net/netdevsim/bus.c b/drivers/net/netdevsim/bus.c index 6aeed0c600f8..f1a0171080cb 100644 --- a/drivers/net/netdevsim/bus.c +++ b/drivers/net/netdevsim/bus.c @@ -26,9 +26,9 @@ static struct nsim_bus_dev *to_nsim_bus_dev(struct device *dev) static int nsim_bus_dev_vfs_enable(struct nsim_bus_dev *nsim_bus_dev, unsigned int num_vfs) { - nsim_bus_dev->vfconfigs = kcalloc(num_vfs, - sizeof(struct nsim_vf_config), - GFP_KERNEL); + if (nsim_bus_dev->max_vfs < num_vfs) + return -ENOMEM; + if (!nsim_bus_dev->vfconfigs) return -ENOMEM; nsim_bus_dev->num_vfs = num_vfs; @@ -38,8 +38,6 @@ static int nsim_bus_dev_vfs_enable(struct nsim_bus_dev *nsim_bus_dev, static void nsim_bus_dev_vfs_disable(struct nsim_bus_dev *nsim_bus_dev) { - kfree(nsim_bus_dev->vfconfigs); - nsim_bus_dev->vfconfigs = NULL; nsim_bus_dev->num_vfs = 0; } @@ -154,22 +152,29 @@ static struct device_type nsim_bus_dev_type = { }; static struct nsim_bus_dev * -nsim_bus_dev_new(unsigned int id, unsigned int port_count); +nsim_bus_dev_new(unsigned int id, unsigned int port_count, + unsigned int max_vfs); + +#define NSIM_BUS_DEV_MAX_VFS 4 static ssize_t new_device_store(struct bus_type *bus, const char *buf, size_t count) { struct nsim_bus_dev *nsim_bus_dev; unsigned int port_count; + unsigned int max_vfs; unsigned int id; int err; - err = sscanf(buf, "%u %u", &id, &port_count); + err = sscanf(buf, "%u %u %u", &id, &port_count, &max_vfs); switch (err) { case 1: port_count = 1; /* fall through */ case 2: + max_vfs = NSIM_BUS_DEV_MAX_VFS; + /* fall through */ + case 3: if (id > INT_MAX) { pr_err("Value of \"id\" is too big.\n"); return -EINVAL; @@ -179,7 +184,7 @@ new_device_store(struct bus_type *bus, const char *buf, size_t count) pr_err("Format for adding new device is \"id port_count\" (uint uint).\n"); return -EINVAL; } - nsim_bus_dev = nsim_bus_dev_new(id, port_count); + nsim_bus_dev = nsim_bus_dev_new(id, port_count, max_vfs); if (IS_ERR(nsim_bus_dev)) return PTR_ERR(nsim_bus_dev); @@ -267,7 +272,8 @@ static struct bus_type nsim_bus = { }; static struct nsim_bus_dev * -nsim_bus_dev_new(unsigned int id, unsigned int port_count) +nsim_bus_dev_new(unsigned int id, unsigned int port_count, + unsigned int max_vfs) { struct nsim_bus_dev *nsim_bus_dev; int err; @@ -284,12 +290,24 @@ nsim_bus_dev_new(unsigned int id, unsigned int port_count) nsim_bus_dev->dev.type = &nsim_bus_dev_type; nsim_bus_dev->port_count = port_count; nsim_bus_dev->initial_net = current->nsproxy->net_ns; + nsim_bus_dev->max_vfs = max_vfs; + + nsim_bus_dev->vfconfigs = kcalloc(nsim_bus_dev->max_vfs, + sizeof(struct nsim_vf_config), + GFP_KERNEL); + if (!nsim_bus_dev->vfconfigs) { + err = -ENOMEM; + goto err_nsim_bus_dev_id_free; + } err = device_register(&nsim_bus_dev->dev); if (err) - goto err_nsim_bus_dev_id_free; + goto err_nsim_vfconfigs_free; + return nsim_bus_dev; +err_nsim_vfconfigs_free: + kfree(nsim_bus_dev->vfconfigs); err_nsim_bus_dev_id_free: ida_free(&nsim_bus_dev_ids, nsim_bus_dev->dev.id); err_nsim_bus_dev_free: @@ -301,6 +319,7 @@ static void nsim_bus_dev_del(struct nsim_bus_dev *nsim_bus_dev) { device_unregister(&nsim_bus_dev->dev); ida_free(&nsim_bus_dev_ids, nsim_bus_dev->dev.id); + kfree(nsim_bus_dev->vfconfigs); kfree(nsim_bus_dev); } diff --git a/drivers/net/netdevsim/netdevsim.h b/drivers/net/netdevsim/netdevsim.h index 94df795ef4d3..e2049856add8 100644 --- a/drivers/net/netdevsim/netdevsim.h +++ b/drivers/net/netdevsim/netdevsim.h @@ -238,6 +238,7 @@ struct nsim_bus_dev { struct net *initial_net; /* Purpose of this is to carry net pointer * during the probe time only. */ + unsigned int max_vfs; unsigned int num_vfs; struct nsim_vf_config *vfconfigs; }; -- 2.17.1