From patchwork Mon May 11 18:37:04 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Dan Carpenter X-Patchwork-Id: 11541419 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 6CD3615E6 for ; Mon, 11 May 2020 18:37:20 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 3933D20720 for ; Mon, 11 May 2020 18:37:20 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=oracle.com header.i=@oracle.com header.b="QIsfYpGw" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 3933D20720 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=oracle.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id 50402900077; Mon, 11 May 2020 14:37:19 -0400 (EDT) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id 4B468900036; Mon, 11 May 2020 14:37:19 -0400 (EDT) X-Original-To: int-list-linux-mm@kvack.org X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 3A256900077; Mon, 11 May 2020 14:37:19 -0400 (EDT) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0065.hostedemail.com [216.40.44.65]) by kanga.kvack.org (Postfix) with ESMTP id 2029A900036 for ; Mon, 11 May 2020 14:37:19 -0400 (EDT) Received: from smtpin19.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay02.hostedemail.com (Postfix) with ESMTP id E18D62C04 for ; Mon, 11 May 2020 18:37:18 +0000 (UTC) X-FDA: 76805295756.19.crime18_22ad80f8f4550 X-Spam-Summary: 1,0,0,,d41d8cd98f00b204,dan.carpenter@oracle.com,,RULES_HIT:30012:30034:30054:30064,0,RBL:141.146.126.78:@oracle.com:.lbl8.mailshell.net-64.10.201.10 62.18.0.100,CacheIP:none,Bayesian:0.5,0.5,0.5,Netcheck:none,DomainCache:0,MSF:not bulk,SPF:ft,MSBL:0,DNSBL:neutral,Custom_rules:0:0:0,LFtime:23,LUA_SUMMARY:none X-HE-Tag: crime18_22ad80f8f4550 X-Filterd-Recvd-Size: 6458 Received: from aserp2120.oracle.com (aserp2120.oracle.com [141.146.126.78]) by imf47.hostedemail.com (Postfix) with ESMTP for ; Mon, 11 May 2020 18:37:18 +0000 (UTC) Received: from pps.filterd (aserp2120.oracle.com [127.0.0.1]) by aserp2120.oracle.com (8.16.0.42/8.16.0.42) with SMTP id 04BIU5av047473; Mon, 11 May 2020 18:37:17 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.com; h=date : from : to : cc : subject : message-id : mime-version : content-type; s=corp-2020-01-29; bh=CZOwzpemRnd78zgwoxIdVl8m13SuOTE/kD6NJ7g+w6k=; b=QIsfYpGwul6BKrwim6pMahlhi6Xx7ZzI8GpTf3qm3TPzrCxr5R490SR/0jtf8PsPtJAH 3q0H6N6tWSwg0TKoM2Wz0UpK/QJkGvqAt9EZ67Cj3Zqzu6JH4kjTQDPqIlPwf/R+eJIw 8DaylgaLP6DCevWUHZ+oZyDkTFLMKZXGSbc50yFG4c78I2S+C+1+nFT5uUTgphxG8n9I YJMbc//tkH5V8tLPK6bHAYXLcNcZvfMOXhXVitKYGVf0rPSNj5w9jjjAQhW7WZnkaP5L B3quaDW2CMXmdqZZSM4d7bOv2iYA11u3oBhPCv644JhgFfBzdl87PYuPetwPWYSzuy87 CQ== Received: from userp3020.oracle.com (userp3020.oracle.com [156.151.31.79]) by aserp2120.oracle.com with ESMTP id 30x3gsetud-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=FAIL); Mon, 11 May 2020 18:37:16 +0000 Received: from pps.filterd (userp3020.oracle.com [127.0.0.1]) by userp3020.oracle.com (8.16.0.42/8.16.0.42) with SMTP id 04BITgeQ174117; Mon, 11 May 2020 18:37:16 GMT Received: from userv0121.oracle.com (userv0121.oracle.com [156.151.31.72]) by userp3020.oracle.com with ESMTP id 30x69rht64-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Mon, 11 May 2020 18:37:16 +0000 Received: from abhmp0009.oracle.com (abhmp0009.oracle.com [141.146.116.15]) by userv0121.oracle.com (8.14.4/8.13.8) with ESMTP id 04BIbEuJ010078; Mon, 11 May 2020 18:37:15 GMT Received: from mwanda (/10.175.212.140) by default (Oracle Beehive Gateway v4.0) with ESMTP ; Mon, 11 May 2020 11:37:10 -0700 Date: Mon, 11 May 2020 21:37:04 +0300 From: Dan Carpenter To: =?iso-8859-1?q?J=E9r=F4me?= Glisse , Ralph Campbell Cc: Jason Gunthorpe , linux-mm@kvack.org, kernel-janitors@vger.kernel.org Subject: [PATCH] mm/hmm/test: Fix some copy_to_user() error handling Message-ID: <20200511183704.GA225608@mwanda> MIME-Version: 1.0 Content-Disposition: inline X-Mailer: git-send-email haha only kidding X-Proofpoint-Virus-Version: vendor=nai engine=6000 definitions=9618 signatures=668687 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 bulkscore=0 malwarescore=0 adultscore=0 spamscore=0 suspectscore=0 mlxscore=0 mlxlogscore=999 phishscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2003020000 definitions=main-2005110141 X-Proofpoint-Virus-Version: vendor=nai engine=6000 definitions=9618 signatures=668687 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 spamscore=0 mlxlogscore=999 malwarescore=0 adultscore=0 mlxscore=0 priorityscore=1501 lowpriorityscore=0 impostorscore=0 clxscore=1011 bulkscore=0 phishscore=0 suspectscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2003020000 definitions=main-2005110141 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: The copy_to_user() function returns the number of bytes which weren't copied but we want to return negative error codes. Also in dmirror_write() if the copy_from_user() fails then there is some cleanup needed before we can return so I fixed that as well. Fixes: 5d5e54be8a1e3 ("mm/hmm/test: add selftest driver for HMM") Signed-off-by: Dan Carpenter Reviewed-by: Ralph Campbell --- lib/test_hmm.c | 41 +++++++++++++++++++++++++---------------- 1 file changed, 25 insertions(+), 16 deletions(-) diff --git a/lib/test_hmm.c b/lib/test_hmm.c index 00bca6116f930..fd4889f7b3d90 100644 --- a/lib/test_hmm.c +++ b/lib/test_hmm.c @@ -360,9 +360,11 @@ static int dmirror_read(struct dmirror *dmirror, struct hmm_dmirror_cmd *cmd) cmd->faults++; } - if (ret == 0) - ret = copy_to_user(u64_to_user_ptr(cmd->ptr), bounce.ptr, - bounce.size); + if (ret == 0) { + if (copy_to_user(u64_to_user_ptr(cmd->ptr), bounce.ptr, + bounce.size)) + ret = -EFAULT; + } cmd->cpages = bounce.cpages; dmirror_bounce_fini(&bounce); return ret; @@ -412,10 +414,11 @@ static int dmirror_write(struct dmirror *dmirror, struct hmm_dmirror_cmd *cmd) ret = dmirror_bounce_init(&bounce, start, size); if (ret) return ret; - ret = copy_from_user(bounce.ptr, u64_to_user_ptr(cmd->ptr), - bounce.size); - if (ret) - return ret; + if (copy_from_user(bounce.ptr, u64_to_user_ptr(cmd->ptr), + bounce.size)) { + ret = -EFAULT; + goto fini; + } while (1) { mutex_lock(&dmirror->mutex); @@ -431,6 +434,7 @@ static int dmirror_write(struct dmirror *dmirror, struct hmm_dmirror_cmd *cmd) cmd->faults++; } +fini: cmd->cpages = bounce.cpages; dmirror_bounce_fini(&bounce); return ret; @@ -715,9 +719,11 @@ static int dmirror_migrate(struct dmirror *dmirror, mutex_lock(&dmirror->mutex); ret = dmirror_do_read(dmirror, start, end, &bounce); mutex_unlock(&dmirror->mutex); - if (ret == 0) - ret = copy_to_user(u64_to_user_ptr(cmd->ptr), bounce.ptr, - bounce.size); + if (ret == 0) { + if (copy_to_user(u64_to_user_ptr(cmd->ptr), bounce.ptr, + bounce.size)) + ret = -EFAULT; + } cmd->cpages = bounce.cpages; dmirror_bounce_fini(&bounce); return ret; @@ -886,9 +892,10 @@ static int dmirror_snapshot(struct dmirror *dmirror, break; n = (range.end - range.start) >> PAGE_SHIFT; - ret = copy_to_user(uptr, perm, n); - if (ret) + if (copy_to_user(uptr, perm, n)) { + ret = -EFAULT; break; + } cmd->cpages += n; uptr += n; @@ -911,9 +918,8 @@ static long dmirror_fops_unlocked_ioctl(struct file *filp, if (!dmirror) return -EINVAL; - ret = copy_from_user(&cmd, uarg, sizeof(cmd)); - if (ret) - return ret; + if (copy_from_user(&cmd, uarg, sizeof(cmd))) + return -EFAULT; if (cmd.addr & ~PAGE_MASK) return -EINVAL; @@ -946,7 +952,10 @@ static long dmirror_fops_unlocked_ioctl(struct file *filp, if (ret) return ret; - return copy_to_user(uarg, &cmd, sizeof(cmd)); + if (copy_to_user(uarg, &cmd, sizeof(cmd))) + return -EFAULT; + + return 0; } static const struct file_operations dmirror_fops = {