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.2 required=3.0 tests=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 1DE8ACA9EAF for ; Mon, 21 Oct 2019 13:47:49 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id DE65E2053B for ; Mon, 21 Oct 2019 13:47:48 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org DE65E2053B Authentication-Results: mail.kernel.org; dmarc=none (p=none dis=none) header.from=huawei.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id 8895E6B0003; Mon, 21 Oct 2019 09:47:48 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 83A7F6B0005; Mon, 21 Oct 2019 09:47:48 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 74F2E6B0006; Mon, 21 Oct 2019 09:47:48 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id 52E3F6B0003 for ; Mon, 21 Oct 2019 09:47:48 -0400 (EDT) Received: from smtpin04.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay02.hostedemail.com (Postfix) with SMTP id D62465DF3 for ; Mon, 21 Oct 2019 13:47:47 +0000 (UTC) X-FDA: 76067919774.04.shame62_60edf28edb435 X-HE-Tag: shame62_60edf28edb435 X-Filterd-Recvd-Size: 6053 Received: from huawei.com (szxga06-in.huawei.com [45.249.212.32]) by imf34.hostedemail.com (Postfix) with ESMTP for ; Mon, 21 Oct 2019 13:47:45 +0000 (UTC) Received: from DGGEMS412-HUB.china.huawei.com (unknown [172.30.72.60]) by Forcepoint Email with ESMTP id 2ABE3E2B6FC278AF8C3C; Mon, 21 Oct 2019 21:47:37 +0800 (CST) Received: from [127.0.0.1] (10.133.219.218) by DGGEMS412-HUB.china.huawei.com (10.3.19.212) with Microsoft SMTP Server id 14.3.439.0; Mon, 21 Oct 2019 21:47:32 +0800 Message-ID: <5DADB6F3.2010504@huawei.com> Date: Mon, 21 Oct 2019 21:47:31 +0800 From: zhong jiang User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:12.0) Gecko/20120428 Thunderbird/12.0.1 MIME-Version: 1.0 To: Vlastimil Babka CC: John Hubbard , , Subject: Re: [PATCH] mm: Unsigned 'nr_pages' always larger than zero References: <1571282386-65076-1-git-send-email-zhongjiang@huawei.com> <60492b3f-c933-b54c-8551-18ebb813d7d1@nvidia.com> In-Reply-To: Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: 7bit X-Originating-IP: [10.133.219.218] X-CFilter-Loop: Reflected 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 2019/10/21 20:57, Vlastimil Babka wrote: > On 10/17/19 10:42 PM, John Hubbard wrote: >> 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. > Agreed. > >>> 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: > Also agreed. Zhong, can you resend it like that? Ok, I will resend it right now. Thanks Sincerely, zhong jiang >> 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 >> > > . >