Commit 1bb0d7b7 authored by Michael J. Ruhl's avatar Michael J. Ruhl Committed by Doug Ledford

IB/hfi1: Code reuse with memdup_copy

Update several usages of kmalloc/user_copy to memdup_copy and
memdup_copy_nul.
Reviewed-by: default avatarDennis Dalessandro <dennis.dalessandro@intel.com>
Reviewed-by: default avatarMike Marciniszyn <mike.marciniszyn@intel.com>
Signed-off-by: default avatarMichael J. Ruhl <michael.j.ruhl@intel.com>
Signed-off-by: default avatarDennis Dalessandro <dennis.dalessandro@intel.com>
Signed-off-by: default avatarDoug Ledford <dledford@redhat.com>
parent 832666c1
...@@ -50,6 +50,7 @@ ...@@ -50,6 +50,7 @@
#include <linux/kernel.h> #include <linux/kernel.h>
#include <linux/export.h> #include <linux/export.h>
#include <linux/module.h> #include <linux/module.h>
#include <linux/string.h>
#include "hfi.h" #include "hfi.h"
#include "debugfs.h" #include "debugfs.h"
...@@ -503,18 +504,11 @@ static ssize_t asic_flags_write(struct file *file, const char __user *buf, ...@@ -503,18 +504,11 @@ static ssize_t asic_flags_write(struct file *file, const char __user *buf,
ppd = private2ppd(file); ppd = private2ppd(file);
dd = ppd->dd; dd = ppd->dd;
buff = kmalloc(count + 1, GFP_KERNEL);
if (!buff)
return -ENOMEM;
ret = copy_from_user(buff, buf, count);
if (ret > 0) {
ret = -EFAULT;
goto do_free;
}
/* zero terminate and read the expected integer */ /* zero terminate and read the expected integer */
buff[count] = 0; buff = memdup_user_nul(buf, count);
if (IS_ERR(buff))
return PTR_ERR(buff);
ret = kstrtoull(buff, 0, &value); ret = kstrtoull(buff, 0, &value);
if (ret) if (ret)
goto do_free; goto do_free;
...@@ -692,15 +686,9 @@ static ssize_t __i2c_debugfs_write(struct file *file, const char __user *buf, ...@@ -692,15 +686,9 @@ static ssize_t __i2c_debugfs_write(struct file *file, const char __user *buf,
if (i2c_addr == 0) if (i2c_addr == 0)
return -EINVAL; return -EINVAL;
buff = kmalloc(count, GFP_KERNEL); buff = memdup_user(buf, count);
if (!buff) if (IS_ERR(buff))
return -ENOMEM; return PTR_ERR(buff);
ret = copy_from_user(buff, buf, count);
if (ret > 0) {
ret = -EFAULT;
goto _free;
}
total_written = i2c_write(ppd, target, i2c_addr, offset, buff, count); total_written = i2c_write(ppd, target, i2c_addr, offset, buff, count);
if (total_written < 0) { if (total_written < 0) {
...@@ -805,15 +793,10 @@ static ssize_t __qsfp_debugfs_write(struct file *file, const char __user *buf, ...@@ -805,15 +793,10 @@ static ssize_t __qsfp_debugfs_write(struct file *file, const char __user *buf,
ppd = private2ppd(file); ppd = private2ppd(file);
buff = kmalloc(count, GFP_KERNEL); buff = memdup_user(buf, count);
if (!buff) if (IS_ERR(buff))
return -ENOMEM; return PTR_ERR(buff);
ret = copy_from_user(buff, buf, count);
if (ret > 0) {
ret = -EFAULT;
goto _free;
}
total_written = qsfp_write(ppd, target, *ppos, buff, count); total_written = qsfp_write(ppd, target, *ppos, buff, count);
if (total_written < 0) { if (total_written < 0) {
ret = total_written; ret = total_written;
......
...@@ -45,6 +45,7 @@ ...@@ -45,6 +45,7 @@
* *
*/ */
#include <asm/page.h> #include <asm/page.h>
#include <linux/string.h>
#include "user_exp_rcv.h" #include "user_exp_rcv.h"
#include "trace.h" #include "trace.h"
...@@ -577,16 +578,10 @@ int hfi1_user_exp_rcv_clear(struct file *fp, struct hfi1_tid_info *tinfo) ...@@ -577,16 +578,10 @@ int hfi1_user_exp_rcv_clear(struct file *fp, struct hfi1_tid_info *tinfo)
u32 *tidinfo; u32 *tidinfo;
unsigned tididx; unsigned tididx;
tidinfo = kcalloc(tinfo->tidcnt, sizeof(*tidinfo), GFP_KERNEL); tidinfo = memdup_user((void __user *)(unsigned long)tinfo->tidlist,
if (!tidinfo) sizeof(tidinfo[0]) * tinfo->tidcnt);
return -ENOMEM; if (IS_ERR(tidinfo))
return PTR_ERR(tidinfo);
if (copy_from_user(tidinfo, (void __user *)(unsigned long)
tinfo->tidlist, sizeof(tidinfo[0]) *
tinfo->tidcnt)) {
ret = -EFAULT;
goto done;
}
mutex_lock(&uctxt->exp_lock); mutex_lock(&uctxt->exp_lock);
for (tididx = 0; tididx < tinfo->tidcnt; tididx++) { for (tididx = 0; tididx < tinfo->tidcnt; tididx++) {
...@@ -602,7 +597,7 @@ int hfi1_user_exp_rcv_clear(struct file *fp, struct hfi1_tid_info *tinfo) ...@@ -602,7 +597,7 @@ int hfi1_user_exp_rcv_clear(struct file *fp, struct hfi1_tid_info *tinfo)
spin_unlock(&fd->tid_lock); spin_unlock(&fd->tid_lock);
tinfo->tidcnt = tididx; tinfo->tidcnt = tididx;
mutex_unlock(&uctxt->exp_lock); mutex_unlock(&uctxt->exp_lock);
done:
kfree(tidinfo); kfree(tidinfo);
return ret; return ret;
} }
......
...@@ -60,6 +60,7 @@ ...@@ -60,6 +60,7 @@
#include <linux/mmu_context.h> #include <linux/mmu_context.h>
#include <linux/module.h> #include <linux/module.h>
#include <linux/vmalloc.h> #include <linux/vmalloc.h>
#include <linux/string.h>
#include "hfi.h" #include "hfi.h"
#include "sdma.h" #include "sdma.h"
...@@ -725,30 +726,28 @@ int hfi1_user_sdma_process_request(struct file *fp, struct iovec *iovec, ...@@ -725,30 +726,28 @@ int hfi1_user_sdma_process_request(struct file *fp, struct iovec *iovec,
*/ */
if (req_opcode(req->info.ctrl) == EXPECTED) { if (req_opcode(req->info.ctrl) == EXPECTED) {
u16 ntids = iovec[idx].iov_len / sizeof(*req->tids); u16 ntids = iovec[idx].iov_len / sizeof(*req->tids);
u32 *tmp;
if (!ntids || ntids > MAX_TID_PAIR_ENTRIES) { if (!ntids || ntids > MAX_TID_PAIR_ENTRIES) {
ret = -EINVAL; ret = -EINVAL;
goto free_req; goto free_req;
} }
req->tids = kcalloc(ntids, sizeof(*req->tids), GFP_KERNEL);
if (!req->tids) {
ret = -ENOMEM;
goto free_req;
}
/* /*
* We have to copy all of the tids because they may vary * We have to copy all of the tids because they may vary
* in size and, therefore, the TID count might not be * in size and, therefore, the TID count might not be
* equal to the pkt count. However, there is no way to * equal to the pkt count. However, there is no way to
* tell at this point. * tell at this point.
*/ */
ret = copy_from_user(req->tids, iovec[idx].iov_base, tmp = memdup_user(iovec[idx].iov_base,
ntids * sizeof(*req->tids)); ntids * sizeof(*req->tids));
if (ret) { if (IS_ERR(tmp)) {
ret = PTR_ERR(tmp);
SDMA_DBG(req, "Failed to copy %d TIDs (%d)", SDMA_DBG(req, "Failed to copy %d TIDs (%d)",
ntids, ret); ntids, ret);
ret = -EFAULT;
goto free_req; goto free_req;
} }
req->tids = tmp;
req->n_tids = ntids; req->n_tids = ntids;
idx++; idx++;
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment