From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-8.3 required=3.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_PATCH, MAILING_LIST_MULTI,SIGNED_OFF_BY,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED, USER_AGENT_SANE_1 autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id BD7DBECE58D for ; Thu, 17 Oct 2019 20:42:25 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 5B2A321835 for ; Thu, 17 Oct 2019 20:42:25 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=nvidia.com header.i=@nvidia.com header.b="WW6YjiuO" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 5B2A321835 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=nvidia.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id B3A418E0005; Thu, 17 Oct 2019 16:42:24 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id AEA958E0003; Thu, 17 Oct 2019 16:42:24 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 9D9378E0005; Thu, 17 Oct 2019 16:42:24 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0019.hostedemail.com [216.40.44.19]) by kanga.kvack.org (Postfix) with ESMTP id 767E08E0003 for ; Thu, 17 Oct 2019 16:42:24 -0400 (EDT) Received: from smtpin19.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay05.hostedemail.com (Postfix) with SMTP id 18D24181AC9C6 for ; Thu, 17 Oct 2019 20:42:24 +0000 (UTC) X-FDA: 76054449408.19.iron92_894a68efa8201 X-HE-Tag: iron92_894a68efa8201 X-Filterd-Recvd-Size: 6770 Received: from hqemgate14.nvidia.com (hqemgate14.nvidia.com [216.228.121.143]) by imf38.hostedemail.com (Postfix) with ESMTP for ; Thu, 17 Oct 2019 20:42:23 +0000 (UTC) Received: from hqpgpgate102.nvidia.com (Not Verified[216.228.121.13]) by hqemgate14.nvidia.com (using TLS: TLSv1.2, DES-CBC3-SHA) id ; Thu, 17 Oct 2019 13:42:26 -0700 Received: from hqmail.nvidia.com ([172.20.161.6]) by hqpgpgate102.nvidia.com (PGP Universal service); Thu, 17 Oct 2019 13:42:21 -0700 X-PGP-Universal: processed; by hqpgpgate102.nvidia.com on Thu, 17 Oct 2019 13:42:21 -0700 Received: from DRHQMAIL107.nvidia.com (10.27.9.16) by HQMAIL107.nvidia.com (172.20.187.13) with Microsoft SMTP Server (TLS) id 15.0.1473.3; Thu, 17 Oct 2019 20:42:21 +0000 Received: from [10.110.48.28] (10.124.1.5) by DRHQMAIL107.nvidia.com (10.27.9.16) with Microsoft SMTP Server (TLS) id 15.0.1473.3; Thu, 17 Oct 2019 20:42:21 +0000 Subject: Re: [PATCH] mm: Unsigned 'nr_pages' always larger than zero To: zhong jiang , CC: , References: <1571282386-65076-1-git-send-email-zhongjiang@huawei.com> X-Nvconfidentiality: public From: John Hubbard Message-ID: <60492b3f-c933-b54c-8551-18ebb813d7d1@nvidia.com> Date: Thu, 17 Oct 2019 13:42:20 -0700 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Thunderbird/60.8.0 MIME-Version: 1.0 In-Reply-To: <1571282386-65076-1-git-send-email-zhongjiang@huawei.com> X-Originating-IP: [10.124.1.5] X-ClientProxiedBy: HQMAIL105.nvidia.com (172.20.187.12) To DRHQMAIL107.nvidia.com (10.27.9.16) Content-Type: text/plain; charset="utf-8" Content-Language: en-US Content-Transfer-Encoding: 7bit DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=nvidia.com; s=n1; t=1571344946; bh=gNFGiXTghfxUTFXj9XoWpXatcR0YEVeKAT3rgtgi4uM=; h=X-PGP-Universal:Subject:To:CC:References:X-Nvconfidentiality:From: Message-ID:Date:User-Agent:MIME-Version:In-Reply-To: X-Originating-IP:X-ClientProxiedBy:Content-Type:Content-Language: Content-Transfer-Encoding; b=WW6YjiuOeiXQYc8rp/ALqPkFTKtkVJ1/0mwbTq+ajwBzmJ6AKrnd5P9tF5Fi+xpvQ M+Uw37AR4IRa7IXb0Vsvpo5Hqj3AiU9VUIumHO29ol5z0ET8n1OxCjP2HAUMVL998J M4O0g6el2p5o4ve0wZL/gAb7CYHlWztN6GkhTYMvcPedjKJ/SOxmRWu6chwYvSmr0A ap/xtWXhBMjioCx9My8DmoIbRGQCoNGsa8bEKqjZ28EigzXYOe2cMjMo9MdCImopg1 gexOOe2UOuONwZXAXoQkFEoxljWkHqF8SSoV/UD6N/GzRjMNB9sH14IxwpRXV3wEpx D9at1Lxh478bw== 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: On 10/16/19 8:19 PM, zhong jiang wrote: > With the help of unsigned_lesser_than_zero.cocci. Unsigned 'nr_pages"' > compare with zero. And __get_user_pages_locked will return an long value. > > The patch use a new local variable to store the return value of > __get_user_pages_locked(). Then use it to compare with zero. Hi Zhong, The above are actually more like notes to yourself, and while those are good to have, but it's not yet a perfect commit description: it talks about how you got here, which is only part of the story. A higher level summary is better, and let the code itself cover the details. Like this: First line (subject): mm/gup: allow CMA migration to propagate errors back to caller Commit description: check_and_migrate_cma_pages() was recording the result of __get_user_pages_locked() in an unsigned "nr_pages" variable. Because __get_user_pages_locked() returns a signed value that can include negative errno values, this had the effect of hiding errors. Change check_and_migrate_cma_pages() implementation so that it uses a signed variable instead, and propagates the results back to the caller just as other gup internal functions do. This was discovered with the help of unsigned_lesser_than_zero.cocci. > > Suggested-by: Andrew Morton > Signed-off-by: zhong jiang > --- > mm/gup.c | 8 +++++--- > 1 file changed, 5 insertions(+), 3 deletions(-) > > diff --git a/mm/gup.c b/mm/gup.c > index 8f236a3..1fe0ceb 100644 > --- a/mm/gup.c > +++ b/mm/gup.c > @@ -1443,6 +1443,7 @@ static long check_and_migrate_cma_pages(struct task_struct *tsk, > bool drain_allow = true; > bool migrate_allow = true; > LIST_HEAD(cma_page_list); > + long ret; Ira pointed out that this needs initialization, see below. > > check_again: > for (i = 0; i < nr_pages;) { > @@ -1504,17 +1505,18 @@ static long check_and_migrate_cma_pages(struct task_struct *tsk, > * again migrating any new CMA pages which we failed to isolate > * earlier. > */ > - nr_pages = __get_user_pages_locked(tsk, mm, start, nr_pages, > + ret = __get_user_pages_locked(tsk, mm, start, nr_pages, > pages, vmas, NULL, > gup_flags); > > - if ((nr_pages > 0) && migrate_allow) { > + nr_pages = ret; Although technically correct, it makes me feel odd to see the assignment done from signed to unsigned, *before* checking for >0. And Ira is hinting at the same thing, when he asks if we can return early here. See below... > + if ((ret > 0) && migrate_allow) { > drain_allow = true; > goto check_again; > } > } > > - return nr_pages; > + return ret; > } > #else > static long check_and_migrate_cma_pages(struct task_struct *tsk, > So, overall, I'd recommend this, instead: diff --git a/mm/gup.c b/mm/gup.c index 23a9f9c9d377..72bc027037fa 100644 --- a/mm/gup.c +++ b/mm/gup.c @@ -1443,6 +1443,7 @@ static long check_and_migrate_cma_pages(struct task_struct *tsk, bool drain_allow = true; bool migrate_allow = true; LIST_HEAD(cma_page_list); + long ret = nr_pages; check_again: for (i = 0; i < nr_pages;) { @@ -1504,17 +1505,18 @@ static long check_and_migrate_cma_pages(struct task_struct *tsk, * again migrating any new CMA pages which we failed to isolate * earlier. */ - nr_pages = __get_user_pages_locked(tsk, mm, start, nr_pages, + ret = __get_user_pages_locked(tsk, mm, start, nr_pages, pages, vmas, NULL, gup_flags); - if ((nr_pages > 0) && migrate_allow) { + if ((ret > 0) && migrate_allow) { + nr_pages = ret; drain_allow = true; goto check_again; } } - return nr_pages; + return ret; } #else static long check_and_migrate_cma_pages(struct task_struct *tsk, thanks, John Hubbard NVIDIA