2005-04-16 15:20:36 -07:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2004 Topspin Communications. All rights reserved.
|
|
|
|
*
|
|
|
|
* This software is available to you under a choice of one of two
|
|
|
|
* licenses. You may choose to be licensed under the terms of the GNU
|
|
|
|
* General Public License (GPL) Version 2, available from the file
|
|
|
|
* COPYING in the main directory of this source tree, or the
|
|
|
|
* OpenIB.org BSD license below:
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or
|
|
|
|
* without modification, are permitted provided that the following
|
|
|
|
* conditions are met:
|
|
|
|
*
|
|
|
|
* - Redistributions of source code must retain the above
|
|
|
|
* copyright notice, this list of conditions and the following
|
|
|
|
* disclaimer.
|
|
|
|
*
|
|
|
|
* - Redistributions in binary form must reproduce the above
|
|
|
|
* copyright notice, this list of conditions and the following
|
|
|
|
* disclaimer in the documentation and/or other materials
|
|
|
|
* provided with the distribution.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
|
|
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
|
|
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
|
|
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
|
|
|
|
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
|
|
|
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
|
|
|
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
|
|
* SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
2017-02-02 19:15:33 +01:00
|
|
|
#include <linux/sched/signal.h>
|
2005-04-16 15:20:36 -07:00
|
|
|
|
|
|
|
#include <linux/init.h>
|
|
|
|
#include <linux/seq_file.h>
|
|
|
|
|
2016-12-24 11:46:01 -08:00
|
|
|
#include <linux/uaccess.h>
|
2005-04-16 15:20:36 -07:00
|
|
|
|
|
|
|
#include "ipoib.h"
|
|
|
|
|
2021-05-26 21:27:53 +08:00
|
|
|
static ssize_t parent_show(struct device *d, struct device_attribute *attr,
|
2002-04-09 12:14:34 -07:00
|
|
|
char *buf)
|
2005-04-16 15:20:36 -07:00
|
|
|
{
|
2002-04-09 12:14:34 -07:00
|
|
|
struct net_device *dev = to_net_dev(d);
|
2017-04-10 11:22:29 +03:00
|
|
|
struct ipoib_dev_priv *priv = ipoib_priv(dev);
|
2005-04-16 15:20:36 -07:00
|
|
|
|
RDMA: Convert sysfs device * show functions to use sysfs_emit()
Done with cocci script:
@@
identifier d_show;
identifier dev, attr, buf;
@@
ssize_t d_show(struct device *dev, struct device_attribute *attr, char *buf)
{
<...
return
- sprintf(buf,
+ sysfs_emit(buf,
...);
...>
}
@@
identifier d_show;
identifier dev, attr, buf;
@@
ssize_t d_show(struct device *dev, struct device_attribute *attr, char *buf)
{
<...
return
- snprintf(buf, PAGE_SIZE,
+ sysfs_emit(buf,
...);
...>
}
@@
identifier d_show;
identifier dev, attr, buf;
@@
ssize_t d_show(struct device *dev, struct device_attribute *attr, char *buf)
{
<...
return
- scnprintf(buf, PAGE_SIZE,
+ sysfs_emit(buf,
...);
...>
}
@@
identifier d_show;
identifier dev, attr, buf;
expression chr;
@@
ssize_t d_show(struct device *dev, struct device_attribute *attr, char *buf)
{
<...
return
- strcpy(buf, chr);
+ sysfs_emit(buf, chr);
...>
}
@@
identifier d_show;
identifier dev, attr, buf;
identifier len;
@@
ssize_t d_show(struct device *dev, struct device_attribute *attr, char *buf)
{
<...
len =
- sprintf(buf,
+ sysfs_emit(buf,
...);
...>
return len;
}
@@
identifier d_show;
identifier dev, attr, buf;
identifier len;
@@
ssize_t d_show(struct device *dev, struct device_attribute *attr, char *buf)
{
<...
len =
- snprintf(buf, PAGE_SIZE,
+ sysfs_emit(buf,
...);
...>
return len;
}
@@
identifier d_show;
identifier dev, attr, buf;
identifier len;
@@
ssize_t d_show(struct device *dev, struct device_attribute *attr, char *buf)
{
<...
len =
- scnprintf(buf, PAGE_SIZE,
+ sysfs_emit(buf,
...);
...>
return len;
}
@@
identifier d_show;
identifier dev, attr, buf;
identifier len;
@@
ssize_t d_show(struct device *dev, struct device_attribute *attr, char *buf)
{
<...
- len += scnprintf(buf + len, PAGE_SIZE - len,
+ len += sysfs_emit_at(buf, len,
...);
...>
return len;
}
@@
identifier d_show;
identifier dev, attr, buf;
expression chr;
@@
ssize_t d_show(struct device *dev, struct device_attribute *attr, char *buf)
{
...
- strcpy(buf, chr);
- return strlen(buf);
+ return sysfs_emit(buf, chr);
}
Link: https://lore.kernel.org/r/7f406fa8e3aa2552c022bec680f621e38d1fe414.1602122879.git.joe@perches.com
Signed-off-by: Joe Perches <joe@perches.com>
Reviewed-by: Jason Gunthorpe <jgg@nvidia.com>
Signed-off-by: Jason Gunthorpe <jgg@nvidia.com>
2020-10-07 19:36:24 -07:00
|
|
|
return sysfs_emit(buf, "%s\n", priv->parent->name);
|
2005-04-16 15:20:36 -07:00
|
|
|
}
|
2021-05-26 21:27:53 +08:00
|
|
|
static DEVICE_ATTR_RO(parent);
|
2005-04-16 15:20:36 -07:00
|
|
|
|
2018-07-29 11:35:00 +03:00
|
|
|
static bool is_child_unique(struct ipoib_dev_priv *ppriv,
|
|
|
|
struct ipoib_dev_priv *priv)
|
|
|
|
{
|
|
|
|
struct ipoib_dev_priv *tpriv;
|
2025-05-21 15:08:59 +03:00
|
|
|
bool result = true;
|
2018-07-29 11:35:00 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Since the legacy sysfs interface uses pkey for deletion it cannot
|
|
|
|
* support more than one interface with the same pkey, it creates
|
|
|
|
* ambiguity. The RTNL interface deletes using the netdev so it does
|
|
|
|
* not have a problem to support duplicated pkeys.
|
|
|
|
*/
|
|
|
|
if (priv->child_type != IPOIB_LEGACY_CHILD)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* First ensure this isn't a duplicate. We check the parent device and
|
|
|
|
* then all of the legacy child interfaces to make sure the Pkey
|
|
|
|
* doesn't match.
|
|
|
|
*/
|
|
|
|
if (ppriv->pkey == priv->pkey)
|
|
|
|
return false;
|
|
|
|
|
2025-05-21 15:08:59 +03:00
|
|
|
netdev_lock(ppriv->dev);
|
2018-07-29 11:35:00 +03:00
|
|
|
list_for_each_entry(tpriv, &ppriv->child_intfs, list) {
|
|
|
|
if (tpriv->pkey == priv->pkey &&
|
2025-05-21 15:08:59 +03:00
|
|
|
tpriv->child_type == IPOIB_LEGACY_CHILD) {
|
|
|
|
result = false;
|
|
|
|
break;
|
|
|
|
}
|
2018-07-29 11:35:00 +03:00
|
|
|
}
|
2025-05-21 15:08:59 +03:00
|
|
|
netdev_unlock(ppriv->dev);
|
2018-07-29 11:35:00 +03:00
|
|
|
|
2025-05-21 15:08:59 +03:00
|
|
|
return result;
|
2018-07-29 11:35:00 +03:00
|
|
|
}
|
|
|
|
|
2018-07-29 11:34:56 +03:00
|
|
|
/*
|
|
|
|
* NOTE: If this function fails then the priv->dev will remain valid, however
|
2018-08-14 14:22:35 +03:00
|
|
|
* priv will have been freed and must not be touched by caller in the error
|
2018-07-29 11:34:56 +03:00
|
|
|
* case.
|
|
|
|
*
|
|
|
|
* If (ndev->reg_state == NETREG_UNINITIALIZED) then it is up to the caller to
|
|
|
|
* free the net_device (just as rtnl_newlink does) otherwise the net_device
|
|
|
|
* will be freed when the rtnl is unlocked.
|
|
|
|
*/
|
2012-09-13 05:56:36 +00:00
|
|
|
int __ipoib_vlan_add(struct ipoib_dev_priv *ppriv, struct ipoib_dev_priv *priv,
|
|
|
|
u16 pkey, int type)
|
2005-04-16 15:20:36 -07:00
|
|
|
{
|
2018-07-29 11:34:56 +03:00
|
|
|
struct net_device *ndev = priv->dev;
|
2005-04-16 15:20:36 -07:00
|
|
|
int result;
|
2020-05-11 12:06:55 -04:00
|
|
|
struct rdma_netdev *rn = netdev_priv(ndev);
|
2005-04-16 15:20:36 -07:00
|
|
|
|
2018-08-14 14:22:35 +03:00
|
|
|
/*
|
|
|
|
* We do not need to touch priv if register_netdevice fails, so just
|
|
|
|
* always use this flow.
|
|
|
|
*/
|
|
|
|
ndev->priv_destructor = ipoib_intf_free;
|
|
|
|
|
2018-07-29 11:34:58 +03:00
|
|
|
/*
|
|
|
|
* Racing with unregister of the parent must be prevented by the
|
|
|
|
* caller.
|
|
|
|
*/
|
|
|
|
WARN_ON(ppriv->dev->reg_state != NETREG_REGISTERED);
|
|
|
|
|
2018-07-29 11:35:00 +03:00
|
|
|
if (pkey == 0 || pkey == 0x8000) {
|
|
|
|
result = -EINVAL;
|
|
|
|
goto out_early;
|
|
|
|
}
|
|
|
|
|
2020-05-11 12:06:55 -04:00
|
|
|
rn->mtu = priv->mcast_mtu;
|
|
|
|
|
2015-04-16 16:34:34 +03:00
|
|
|
priv->parent = ppriv->dev;
|
2005-04-16 15:20:36 -07:00
|
|
|
priv->pkey = pkey;
|
2018-07-29 11:34:55 +03:00
|
|
|
priv->child_type = type;
|
2005-04-16 15:20:36 -07:00
|
|
|
|
2018-07-29 11:35:00 +03:00
|
|
|
if (!is_child_unique(ppriv, priv)) {
|
|
|
|
result = -ENOTUNIQ;
|
|
|
|
goto out_early;
|
|
|
|
}
|
|
|
|
|
2018-07-29 11:34:56 +03:00
|
|
|
result = register_netdevice(ndev);
|
2005-04-16 15:20:36 -07:00
|
|
|
if (result) {
|
|
|
|
ipoib_warn(priv, "failed to initialize; error %i", result);
|
2018-07-29 11:34:56 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* register_netdevice sometimes calls priv_destructor,
|
|
|
|
* sometimes not. Make sure it was done.
|
|
|
|
*/
|
2018-07-29 11:35:00 +03:00
|
|
|
goto out_early;
|
2005-04-16 15:20:36 -07:00
|
|
|
}
|
|
|
|
|
2012-09-27 12:06:02 +00:00
|
|
|
/* RTNL childs don't need proprietary sysfs entries */
|
|
|
|
if (type == IPOIB_LEGACY_CHILD) {
|
2018-07-29 11:34:56 +03:00
|
|
|
if (ipoib_cm_add_mode_attr(ndev))
|
2012-09-27 12:06:02 +00:00
|
|
|
goto sysfs_failed;
|
2018-07-29 11:34:56 +03:00
|
|
|
if (ipoib_add_pkey_attr(ndev))
|
2012-09-27 12:06:02 +00:00
|
|
|
goto sysfs_failed;
|
2018-07-29 11:34:56 +03:00
|
|
|
if (ipoib_add_umcast_attr(ndev))
|
2012-09-27 12:06:02 +00:00
|
|
|
goto sysfs_failed;
|
|
|
|
|
2018-07-29 11:34:56 +03:00
|
|
|
if (device_create_file(&ndev->dev, &dev_attr_parent))
|
2012-09-27 12:06:02 +00:00
|
|
|
goto sysfs_failed;
|
|
|
|
}
|
2005-04-16 15:20:36 -07:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
sysfs_failed:
|
2009-01-14 21:44:39 -08:00
|
|
|
unregister_netdevice(priv->dev);
|
2018-07-29 11:34:55 +03:00
|
|
|
return -ENOMEM;
|
2018-07-29 11:35:00 +03:00
|
|
|
|
|
|
|
out_early:
|
|
|
|
if (ndev->priv_destructor)
|
|
|
|
ndev->priv_destructor(ndev);
|
|
|
|
return result;
|
2012-09-13 05:56:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int ipoib_vlan_add(struct net_device *pdev, unsigned short pkey)
|
|
|
|
{
|
|
|
|
struct ipoib_dev_priv *ppriv, *priv;
|
|
|
|
char intf_name[IFNAMSIZ];
|
2018-07-29 11:34:56 +03:00
|
|
|
struct net_device *ndev;
|
2012-09-13 05:56:36 +00:00
|
|
|
int result;
|
|
|
|
|
|
|
|
if (!capable(CAP_NET_ADMIN))
|
|
|
|
return -EPERM;
|
|
|
|
|
2018-07-29 11:34:57 +03:00
|
|
|
if (!rtnl_trylock())
|
2017-06-14 09:59:09 +03:00
|
|
|
return restart_syscall();
|
|
|
|
|
2018-07-29 11:34:51 +03:00
|
|
|
if (pdev->reg_state != NETREG_REGISTERED) {
|
|
|
|
rtnl_unlock();
|
|
|
|
return -EPERM;
|
|
|
|
}
|
|
|
|
|
2018-07-29 11:35:00 +03:00
|
|
|
ppriv = ipoib_priv(pdev);
|
2012-09-13 05:56:36 +00:00
|
|
|
|
RDMA/IPoIB: Fix format truncation compilation errors
Truncate the device name to store IPoIB VLAN name.
[leonro@5b4e8fba4ddd kernel]$ make -s -j 20 allmodconfig
[leonro@5b4e8fba4ddd kernel]$ make -s -j 20 W=1 drivers/infiniband/ulp/ipoib/
drivers/infiniband/ulp/ipoib/ipoib_vlan.c: In function ‘ipoib_vlan_add’:
drivers/infiniband/ulp/ipoib/ipoib_vlan.c:187:52: error: ‘%04x’
directive output may be truncated writing 4 bytes into a region of size
between 0 and 15 [-Werror=format-truncation=]
187 | snprintf(intf_name, sizeof(intf_name), "%s.%04x",
| ^~~~
drivers/infiniband/ulp/ipoib/ipoib_vlan.c:187:48: note: directive
argument in the range [0, 65535]
187 | snprintf(intf_name, sizeof(intf_name), "%s.%04x",
| ^~~~~~~~~
drivers/infiniband/ulp/ipoib/ipoib_vlan.c:187:9: note: ‘snprintf’ output
between 6 and 21 bytes into a destination of size 16
187 | snprintf(intf_name, sizeof(intf_name), "%s.%04x",
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
188 | ppriv->dev->name, pkey);
| ~~~~~~~~~~~~~~~~~~~~~~~
cc1: all warnings being treated as errors
make[6]: *** [scripts/Makefile.build:244: drivers/infiniband/ulp/ipoib/ipoib_vlan.o] Error 1
make[6]: *** Waiting for unfinished jobs....
Fixes: 9baa0b036410 ("IB/ipoib: Add rtnl_link_ops support")
Link: https://lore.kernel.org/r/e9d3e1fef69df4c9beaf402cc3ac342bad680791.1715240029.git.leon@kernel.org
Signed-off-by: Leon Romanovsky <leonro@nvidia.com>
2024-05-09 10:39:33 +03:00
|
|
|
/* If you increase IFNAMSIZ, update snprintf below
|
|
|
|
* to allow longer names.
|
|
|
|
*/
|
|
|
|
BUILD_BUG_ON(IFNAMSIZ != 16);
|
|
|
|
snprintf(intf_name, sizeof(intf_name), "%.10s.%04x", ppriv->dev->name,
|
|
|
|
pkey);
|
2012-09-13 05:56:36 +00:00
|
|
|
|
2018-08-14 14:22:35 +03:00
|
|
|
ndev = ipoib_intf_alloc(ppriv->ca, ppriv->port, intf_name);
|
|
|
|
if (IS_ERR(ndev)) {
|
|
|
|
result = PTR_ERR(ndev);
|
2018-07-29 11:34:56 +03:00
|
|
|
goto out;
|
|
|
|
}
|
2018-08-14 14:22:35 +03:00
|
|
|
priv = ipoib_priv(ndev);
|
2018-07-29 11:34:56 +03:00
|
|
|
|
2020-10-04 16:29:48 +03:00
|
|
|
ndev->rtnl_link_ops = ipoib_get_link_ops();
|
|
|
|
|
2012-09-13 05:56:36 +00:00
|
|
|
result = __ipoib_vlan_add(ppriv, priv, pkey, IPOIB_LEGACY_CHILD);
|
|
|
|
|
2018-07-29 11:34:56 +03:00
|
|
|
if (result && ndev->reg_state == NETREG_UNINITIALIZED)
|
|
|
|
free_netdev(ndev);
|
|
|
|
|
2012-09-13 05:56:36 +00:00
|
|
|
out:
|
2016-12-28 14:47:24 +02:00
|
|
|
rtnl_unlock();
|
|
|
|
|
2005-04-16 15:20:36 -07:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2018-07-29 11:34:57 +03:00
|
|
|
struct ipoib_vlan_delete_work {
|
|
|
|
struct work_struct work;
|
|
|
|
struct net_device *dev;
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* sysfs callbacks of a netdevice cannot obtain the rtnl lock as
|
|
|
|
* unregister_netdev ultimately deletes the sysfs files while holding the rtnl
|
|
|
|
* lock. This deadlocks the system.
|
|
|
|
*
|
|
|
|
* A callback can use rtnl_trylock to avoid the deadlock but it cannot call
|
|
|
|
* unregister_netdev as that internally takes and releases the rtnl_lock. So
|
|
|
|
* instead we find the netdev to unregister and then do the actual unregister
|
|
|
|
* from the global work queue where we can obtain the rtnl_lock safely.
|
|
|
|
*/
|
|
|
|
static void ipoib_vlan_delete_task(struct work_struct *work)
|
|
|
|
{
|
|
|
|
struct ipoib_vlan_delete_work *pwork =
|
|
|
|
container_of(work, struct ipoib_vlan_delete_work, work);
|
|
|
|
struct net_device *dev = pwork->dev;
|
|
|
|
|
|
|
|
rtnl_lock();
|
|
|
|
|
|
|
|
/* Unregistering tasks can race with another task or parent removal */
|
|
|
|
if (dev->reg_state == NETREG_REGISTERED) {
|
|
|
|
struct ipoib_dev_priv *priv = ipoib_priv(dev);
|
|
|
|
struct ipoib_dev_priv *ppriv = ipoib_priv(priv->parent);
|
|
|
|
|
|
|
|
ipoib_dbg(ppriv, "delete child vlan %s\n", dev->name);
|
|
|
|
unregister_netdevice(dev);
|
|
|
|
}
|
|
|
|
|
|
|
|
rtnl_unlock();
|
|
|
|
|
|
|
|
kfree(pwork);
|
|
|
|
}
|
|
|
|
|
2005-04-16 15:20:36 -07:00
|
|
|
int ipoib_vlan_delete(struct net_device *pdev, unsigned short pkey)
|
|
|
|
{
|
|
|
|
struct ipoib_dev_priv *ppriv, *priv, *tpriv;
|
2018-07-29 11:34:57 +03:00
|
|
|
int rc;
|
2005-04-16 15:20:36 -07:00
|
|
|
|
|
|
|
if (!capable(CAP_NET_ADMIN))
|
|
|
|
return -EPERM;
|
|
|
|
|
2018-07-29 11:34:57 +03:00
|
|
|
if (!rtnl_trylock())
|
2017-08-17 15:50:50 +03:00
|
|
|
return restart_syscall();
|
|
|
|
|
2018-07-29 11:34:51 +03:00
|
|
|
if (pdev->reg_state != NETREG_REGISTERED) {
|
|
|
|
rtnl_unlock();
|
|
|
|
return -EPERM;
|
|
|
|
}
|
|
|
|
|
2018-07-29 11:34:57 +03:00
|
|
|
ppriv = ipoib_priv(pdev);
|
2017-09-24 21:46:32 +03:00
|
|
|
|
2018-07-29 11:34:57 +03:00
|
|
|
rc = -ENODEV;
|
2025-05-21 15:08:59 +03:00
|
|
|
netdev_lock(ppriv->dev);
|
2005-04-16 15:20:36 -07:00
|
|
|
list_for_each_entry_safe(priv, tpriv, &ppriv->child_intfs, list) {
|
2012-09-13 05:56:36 +00:00
|
|
|
if (priv->pkey == pkey &&
|
|
|
|
priv->child_type == IPOIB_LEGACY_CHILD) {
|
2018-07-29 11:34:57 +03:00
|
|
|
struct ipoib_vlan_delete_work *work;
|
|
|
|
|
|
|
|
work = kmalloc(sizeof(*work), GFP_KERNEL);
|
|
|
|
if (!work) {
|
|
|
|
rc = -ENOMEM;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
list_del_init(&priv->list);
|
|
|
|
work->dev = priv->dev;
|
|
|
|
INIT_WORK(&work->work, ipoib_vlan_delete_task);
|
|
|
|
queue_work(ipoib_workqueue, &work->work);
|
|
|
|
|
|
|
|
rc = 0;
|
2005-04-16 15:20:36 -07:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2016-12-28 14:47:22 +02:00
|
|
|
|
2018-07-29 11:34:57 +03:00
|
|
|
out:
|
2025-05-21 15:08:59 +03:00
|
|
|
netdev_unlock(ppriv->dev);
|
2009-01-14 21:44:39 -08:00
|
|
|
rtnl_unlock();
|
2005-04-16 15:20:36 -07:00
|
|
|
|
2018-07-29 11:34:57 +03:00
|
|
|
return rc;
|
2005-04-16 15:20:36 -07:00
|
|
|
}
|