Received: by 2002:a25:c593:0:0:0:0:0 with SMTP id v141csp1054184ybe; Wed, 4 Sep 2019 11:49:29 -0700 (PDT) X-Google-Smtp-Source: APXvYqyI7Yl2q0W33TkYlllhtWFI2U7hDArbJY9R2SXPDXQXsqQOH104XGYZLLtNinmU7Q5A1Cb3 X-Received: by 2002:a63:8141:: with SMTP id t62mr33535765pgd.95.1567622969598; Wed, 04 Sep 2019 11:49:29 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1567622969; cv=none; d=google.com; s=arc-20160816; b=tmrEs2IjUiXcg+fYb2RMLFamo2/no36SGbnhN5yVCaH25/PPQx0PStSy5aM2v0PeO2 0EXO/2wjA8LFIcpCuFlNy2Nh9RERZMZ8cQ9e1KGF5QJ5YeSH5DACuPkUBqt/ssnxVajt y4GAuK51CmNIDxUHNXz2Fl3ZBENra+ok9CyVUfNklD6Go5UmkeLJw8uyMU1x4LJuoHiX ApGCENC0srtUMij4jjWar7282oBTIaWLshMuhOu2+cFZBCe0bGkQuwnQ/hwsbLvWckBV AQj72Cmwe3HGfe9nfZYs+FqfjO9yicxqjNQMCZfAuQZwD5W/yQHUwEYg76CXGmv06VqW H8mA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :references:in-reply-to:message-id:subject:cc:to:from:date :dkim-signature; bh=dv8i4SOdasDYgYwkmqU9x9edmZLdP38IXp8Z9bpa9XI=; b=Gv5Fw1HsZ6BE8SVLF+lN5qL/zQV2PXRB9wZ0ftzgMuzEyy9Uyzz4PZ+4T8zZPIyI+2 XIuWCFmqWqlmUhRpSYKUOQ5iQ6YhvtWY/2TMTavv31NJ90TJWzRu0yocZXMddORJbdbo QITXL/REarCzMWi8Mnq1Gbxg190wOjTEz2TkRnQi6NpkCN8e272DRNvSAcAcQ6HusLM+ 2yInd74a7/x6xN9s699ZaopNFksUDJkrE3sduFl0LdmgHNsIl4PkSZh9rFdADBbDUHxQ EzSc7427JN7mXF3kafwox7VcNsTfyEYLirIFtHssaZFaEeZ+dPKKplGq6rTX9g1j5xZZ 6X3A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=l48eplRt; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id z28si9809827pff.144.2019.09.04.11.49.13; Wed, 04 Sep 2019 11:49:29 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=l48eplRt; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1732885AbfIDSsW (ORCPT + 99 others); Wed, 4 Sep 2019 14:48:22 -0400 Received: from mail.kernel.org ([198.145.29.99]:46940 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1730410AbfIDSsV (ORCPT ); Wed, 4 Sep 2019 14:48:21 -0400 Received: from localhost.localdomain (c-73-231-172-41.hsd1.ca.comcast.net [73.231.172.41]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 03D9C2077B; Wed, 4 Sep 2019 18:48:20 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1567622901; bh=y/U9awkp5NHEKyToOdNfhASID49ak7lJitv2XnwM8ig=; h=Date:From:To:Cc:Subject:In-Reply-To:References:From; b=l48eplRtfDQ9dCB0z0/vjJSBpp4+nu72sQ5JB/xFiwEi+5YkL02O902322DNOoXq0 2o6b81X9JJkXjkuTU3+Rlwzma+WoPy3wed9pWDdYoHBGHF78R5LfS/3ZeheTMIWMgh bKmluPnWE7bN+EqOrCD1L/bEcjgC3ET+et+hF2Yw= Date: Wed, 4 Sep 2019 11:48:20 -0700 From: Andrew Morton To: Vlastimil Babka Cc: zhong jiang , mhocko@kernel.org, anshuman.khandual@arm.com, linux-mm@kvack.org, linux-kernel@vger.kernel.org, Ira Weiny , "Aneesh Kumar K.V" Subject: Re: [PATCH] mm: Unsigned 'nr_pages' always larger than zero Message-Id: <20190904114820.42d9c4daf445ded3d0da52ab@linux-foundation.org> In-Reply-To: <5505fa16-117e-8890-0f48-38555a61a036@suse.cz> References: <1567592763-25282-1-git-send-email-zhongjiang@huawei.com> <5505fa16-117e-8890-0f48-38555a61a036@suse.cz> X-Mailer: Sylpheed 3.5.1 (GTK+ 2.24.31; x86_64-pc-linux-gnu) Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Wed, 4 Sep 2019 13:24:58 +0200 Vlastimil Babka wrote: > On 9/4/19 12:26 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. > > Hence, Convert the long to compare with zero is feasible. > > It would be nicer if the parameter nr_pages was long again instead of unsigned > long (note there are two variants of the function, so both should be changed). nr_pages should be unsigned - it's a count of pages! The bug is that __get_user_pages_locked() returns a signed long which can be a -ve errno. I think it's best if __get_user_pages_locked() is to get itself a new local with the same type as its return value. Something like: --- a/mm/gup.c~a +++ a/mm/gup.c @@ -1450,6 +1450,7 @@ static long check_and_migrate_cma_pages( bool drain_allow = true; bool migrate_allow = true; LIST_HEAD(cma_page_list); + long ret; check_again: for (i = 0; i < nr_pages;) { @@ -1511,17 +1512,18 @@ check_again: * 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; + 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,