Use memdup_user_nul() helper instead of open-coding to simplify the code. Signed-off-by: Ruan Jinjie <ruanjinjie@xxxxxxxxxx> --- drivers/s390/char/hmcdrv_dev.c | 17 +++++------------ 1 file changed, 5 insertions(+), 12 deletions(-) diff --git a/drivers/s390/char/hmcdrv_dev.c b/drivers/s390/char/hmcdrv_dev.c index 8d50c894711f..504cdbf9998d 100644 --- a/drivers/s390/char/hmcdrv_dev.c +++ b/drivers/s390/char/hmcdrv_dev.c @@ -252,19 +252,12 @@ static ssize_t hmcdrv_dev_write(struct file *fp, const char __user *ubuf, fp, (long long) *pos, len); if (!fp->private_data) { /* first expect a cmd write */ - fp->private_data = kmalloc(len + 1, GFP_KERNEL); - - if (!fp->private_data) - return -ENOMEM; - - if (!copy_from_user(fp->private_data, ubuf, len)) { - ((char *)fp->private_data)[len] = '\0'; - return len; + fp->private_data = memdup_user_nul(ubuf, len); + if (IS_ERR(fp->private_data)) { + fp->private_data = NULL; + return PTR_ERR(fp->private_data); } - - kfree(fp->private_data); - fp->private_data = NULL; - return -EFAULT; + return len; } retlen = hmcdrv_dev_transfer((char *) fp->private_data, -- 2.34.1