Skip to content

Commit ccde4de

Browse files
Hao Lanjfvogel
authored andcommitted
net: hns3: Resolved the issue that the debugfs query result is inconsistent.
[ Upstream commit 5191a8d ] This patch modifies the implementation of debugfs: When the user process stops unexpectedly, not all data of the file system is read. In this case, the save_buf pointer is not released. When the user process is called next time, save_buf is used to copy the cached data to the user space. As a result, the queried data is stale. To solve this problem, this patch implements .open() and .release() handler for debugfs file_operations. moving allocation buffer and execution of the cmd to the .open() handler and freeing in to the .release() handler. Allocate separate buffer for each reader and associate the buffer with the file pointer. When different user read processes no longer share the buffer, the stale data problem is fixed. Fixes: 5e69ea7 ("net: hns3: refactor the debugfs process") Signed-off-by: Hao Lan <[email protected]> Signed-off-by: Guangwei Zhang <[email protected]> Signed-off-by: Jijie Shao <[email protected]> Reviewed-by: Michal Swiatkowski <[email protected]> Link: https://patch.msgid.link/[email protected] Signed-off-by: Jakub Kicinski <[email protected]> Signed-off-by: Sasha Levin <[email protected]> (cherry picked from commit 864c1df2e37919df051f310d27176f9bb49ed99e) Signed-off-by: Jack Vogel <[email protected]>
1 parent ffd1bfa commit ccde4de

File tree

2 files changed

+31
-68
lines changed

2 files changed

+31
-68
lines changed

drivers/net/ethernet/hisilicon/hns3/hnae3.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -916,9 +916,6 @@ struct hnae3_handle {
916916

917917
u8 netdev_flags;
918918
struct dentry *hnae3_dbgfs;
919-
/* protects concurrent contention between debugfs commands */
920-
struct mutex dbgfs_lock;
921-
char **dbgfs_buf;
922919

923920
/* Network interface message level enabled bits */
924921
u32 msg_enable;

drivers/net/ethernet/hisilicon/hns3/hns3_debugfs.c

Lines changed: 31 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -1260,69 +1260,55 @@ static int hns3_dbg_read_cmd(struct hns3_dbg_data *dbg_data,
12601260
static ssize_t hns3_dbg_read(struct file *filp, char __user *buffer,
12611261
size_t count, loff_t *ppos)
12621262
{
1263-
struct hns3_dbg_data *dbg_data = filp->private_data;
1263+
char *buf = filp->private_data;
1264+
1265+
return simple_read_from_buffer(buffer, count, ppos, buf, strlen(buf));
1266+
}
1267+
1268+
static int hns3_dbg_open(struct inode *inode, struct file *filp)
1269+
{
1270+
struct hns3_dbg_data *dbg_data = inode->i_private;
12641271
struct hnae3_handle *handle = dbg_data->handle;
12651272
struct hns3_nic_priv *priv = handle->priv;
1266-
ssize_t size = 0;
1267-
char **save_buf;
1268-
char *read_buf;
12691273
u32 index;
1274+
char *buf;
12701275
int ret;
12711276

1277+
if (!test_bit(HNS3_NIC_STATE_INITED, &priv->state) ||
1278+
test_bit(HNS3_NIC_STATE_RESETTING, &priv->state))
1279+
return -EBUSY;
1280+
12721281
ret = hns3_dbg_get_cmd_index(dbg_data, &index);
12731282
if (ret)
12741283
return ret;
12751284

1276-
mutex_lock(&handle->dbgfs_lock);
1277-
save_buf = &handle->dbgfs_buf[index];
1278-
1279-
if (!test_bit(HNS3_NIC_STATE_INITED, &priv->state) ||
1280-
test_bit(HNS3_NIC_STATE_RESETTING, &priv->state)) {
1281-
ret = -EBUSY;
1282-
goto out;
1283-
}
1284-
1285-
if (*save_buf) {
1286-
read_buf = *save_buf;
1287-
} else {
1288-
read_buf = kvzalloc(hns3_dbg_cmd[index].buf_len, GFP_KERNEL);
1289-
if (!read_buf) {
1290-
ret = -ENOMEM;
1291-
goto out;
1292-
}
1293-
1294-
/* save the buffer addr until the last read operation */
1295-
*save_buf = read_buf;
1296-
1297-
/* get data ready for the first time to read */
1298-
ret = hns3_dbg_read_cmd(dbg_data, hns3_dbg_cmd[index].cmd,
1299-
read_buf, hns3_dbg_cmd[index].buf_len);
1300-
if (ret)
1301-
goto out;
1302-
}
1285+
buf = kvzalloc(hns3_dbg_cmd[index].buf_len, GFP_KERNEL);
1286+
if (!buf)
1287+
return -ENOMEM;
13031288

1304-
size = simple_read_from_buffer(buffer, count, ppos, read_buf,
1305-
strlen(read_buf));
1306-
if (size > 0) {
1307-
mutex_unlock(&handle->dbgfs_lock);
1308-
return size;
1289+
ret = hns3_dbg_read_cmd(dbg_data, hns3_dbg_cmd[index].cmd,
1290+
buf, hns3_dbg_cmd[index].buf_len);
1291+
if (ret) {
1292+
kvfree(buf);
1293+
return ret;
13091294
}
13101295

1311-
out:
1312-
/* free the buffer for the last read operation */
1313-
if (*save_buf) {
1314-
kvfree(*save_buf);
1315-
*save_buf = NULL;
1316-
}
1296+
filp->private_data = buf;
1297+
return 0;
1298+
}
13171299

1318-
mutex_unlock(&handle->dbgfs_lock);
1319-
return ret;
1300+
static int hns3_dbg_release(struct inode *inode, struct file *filp)
1301+
{
1302+
kvfree(filp->private_data);
1303+
filp->private_data = NULL;
1304+
return 0;
13201305
}
13211306

13221307
static const struct file_operations hns3_dbg_fops = {
13231308
.owner = THIS_MODULE,
1324-
.open = simple_open,
1309+
.open = hns3_dbg_open,
13251310
.read = hns3_dbg_read,
1311+
.release = hns3_dbg_release,
13261312
};
13271313

13281314
static int hns3_dbg_bd_file_init(struct hnae3_handle *handle, u32 cmd)
@@ -1379,13 +1365,6 @@ int hns3_dbg_init(struct hnae3_handle *handle)
13791365
int ret;
13801366
u32 i;
13811367

1382-
handle->dbgfs_buf = devm_kcalloc(&handle->pdev->dev,
1383-
ARRAY_SIZE(hns3_dbg_cmd),
1384-
sizeof(*handle->dbgfs_buf),
1385-
GFP_KERNEL);
1386-
if (!handle->dbgfs_buf)
1387-
return -ENOMEM;
1388-
13891368
hns3_dbg_dentry[HNS3_DBG_DENTRY_COMMON].dentry =
13901369
debugfs_create_dir(name, hns3_dbgfs_root);
13911370
handle->hnae3_dbgfs = hns3_dbg_dentry[HNS3_DBG_DENTRY_COMMON].dentry;
@@ -1395,8 +1374,6 @@ int hns3_dbg_init(struct hnae3_handle *handle)
13951374
debugfs_create_dir(hns3_dbg_dentry[i].name,
13961375
handle->hnae3_dbgfs);
13971376

1398-
mutex_init(&handle->dbgfs_lock);
1399-
14001377
for (i = 0; i < ARRAY_SIZE(hns3_dbg_cmd); i++) {
14011378
if ((hns3_dbg_cmd[i].cmd == HNAE3_DBG_CMD_TM_NODES &&
14021379
ae_dev->dev_version <= HNAE3_DEVICE_VERSION_V2) ||
@@ -1425,24 +1402,13 @@ int hns3_dbg_init(struct hnae3_handle *handle)
14251402
out:
14261403
debugfs_remove_recursive(handle->hnae3_dbgfs);
14271404
handle->hnae3_dbgfs = NULL;
1428-
mutex_destroy(&handle->dbgfs_lock);
14291405
return ret;
14301406
}
14311407

14321408
void hns3_dbg_uninit(struct hnae3_handle *handle)
14331409
{
1434-
u32 i;
1435-
14361410
debugfs_remove_recursive(handle->hnae3_dbgfs);
14371411
handle->hnae3_dbgfs = NULL;
1438-
1439-
for (i = 0; i < ARRAY_SIZE(hns3_dbg_cmd); i++)
1440-
if (handle->dbgfs_buf[i]) {
1441-
kvfree(handle->dbgfs_buf[i]);
1442-
handle->dbgfs_buf[i] = NULL;
1443-
}
1444-
1445-
mutex_destroy(&handle->dbgfs_lock);
14461412
}
14471413

14481414
void hns3_dbg_register_debugfs(const char *debugfs_dir_name)

0 commit comments

Comments
 (0)