Received: by 2002:ac0:8845:0:0:0:0:0 with SMTP id g63csp1183963img; Tue, 26 Feb 2019 16:05:38 -0800 (PST) X-Google-Smtp-Source: AHgI3Iap3zYu4JXsUiVD4d0GB35pL29lky0uWIfE63I9MYXl/VTRhk/ce7Nv8KlJ+mawtz73qpy5 X-Received: by 2002:a62:be02:: with SMTP id l2mr29025042pff.55.1551225938739; Tue, 26 Feb 2019 16:05:38 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1551225938; cv=none; d=google.com; s=arc-20160816; b=vIZLWv1Q3dMdXRiug1wExSYsj4XMTMBkgb6EJ6XGR5Z9/Pi7ZvDEf8wPaJthPW5Ok8 22qV1WgdA/yG90MB0jSRUqlvUYK08H/co/qcxiXMsxcoQozIW9vs6YIdOKMcbWmlugmY SnOCCcU6Pp8MQDKzIrnuOrSzFF2MkkV+p5voMkQt88okXfdK9Co2WM/DgLErzkoa8X3P QVM4fq7TYJtWizQvJddUEOsuq2oRpRqMyfGkwzrKNA+KXYQirAkQNzodqq9B5nLzVYCc cBz8DPIOhzxllzNJc1jjiYOLaUTmYDZssjZux7xB5pghVH7gqDdOP/tqM4bOCRHOVSLn +Pug== 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 :content-language:in-reply-to:mime-version:user-agent:date :message-id:from:references:cc:to:subject:dkim-signature; bh=VyO4ZrBMdrud25lLhZHSrL2iH8LB0S9KEvRat9Q0vfE=; b=Uat2pYQYDmw7b4C3Iwi8QWUImn6xGmBfTPntzU827N64PAP8h+rU8sDdxzbvJW1D5I i8OSi4Aeu71rP23WOuOwoht1ambQCFT1bR7v0GRW175R71nMMAEiQ0GcvfRRYxYlXqMG RnEPm/OJWzDzjQGluaNa+JGWrpXvbu+rx/6v3tCO8B1f5Ng4Cko8z8N4VOIhtGi4r8uy detdVk9VTzGh0b58mgq+/fCZTIcMTwSeJ1a6uCJ/vb7Mn8PyQX6Y+JocCtTpJS0RNyvO 0aSuAgYII5NDtXlEEaSioWJNMMfNHC4f3ipjUxwCwxRLsGn9e/aRWU73EV8K5yYA6ft9 7G4A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@oracle.com header.s=corp-2018-07-02 header.b=m1hW66jq; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=oracle.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id v14si13814399pgi.249.2019.02.26.16.05.23; Tue, 26 Feb 2019 16:05:38 -0800 (PST) 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=@oracle.com header.s=corp-2018-07-02 header.b=m1hW66jq; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=oracle.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1729035AbfB0ADp (ORCPT + 99 others); Tue, 26 Feb 2019 19:03:45 -0500 Received: from aserp2130.oracle.com ([141.146.126.79]:35528 "EHLO aserp2130.oracle.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727998AbfB0ADo (ORCPT ); Tue, 26 Feb 2019 19:03:44 -0500 Received: from pps.filterd (aserp2130.oracle.com [127.0.0.1]) by aserp2130.oracle.com (8.16.0.27/8.16.0.27) with SMTP id x1QNxH1f033438; Wed, 27 Feb 2019 00:03:27 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.com; h=subject : to : cc : references : from : message-id : date : mime-version : in-reply-to : content-type : content-transfer-encoding; s=corp-2018-07-02; bh=VyO4ZrBMdrud25lLhZHSrL2iH8LB0S9KEvRat9Q0vfE=; b=m1hW66jqcPB1r6qR7HDIFFGoFpEGxlCnx8ykYAwqwsZbJ2e4r5C9xsAZONtel47j9BEG j6gQrLZ+AEzsbkhhlXSEJlADG2a2DPP8DyOHxAkZobIiDu/8jcK8dEp2Mmawp52xdsSo CO6J8++gNHQ0ZjEpZPfc6y5WNn4GcqFWzOVMw5/W1GssG9OsF49JyayOZlQeAsGeJ8kd iGwD8nsyWJaAZDHvIR3G1w448ljXEW1JYF+qNqq6kSg6EBBDEnMiBKvnuMEWZPH12kwi Hj+KnDsOxW90jpV7/YUbRc4lY/PHEZMRs6D9Rrd/x4iQBb0tIH/4tM+fjoSguSzymlws hA== Received: from userv0022.oracle.com (userv0022.oracle.com [156.151.31.74]) by aserp2130.oracle.com with ESMTP id 2qtupe81m0-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Wed, 27 Feb 2019 00:03:27 +0000 Received: from userv0121.oracle.com (userv0121.oracle.com [156.151.31.72]) by userv0022.oracle.com (8.14.4/8.14.4) with ESMTP id x1R03Qcd008984 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Wed, 27 Feb 2019 00:03:26 GMT Received: from abhmp0007.oracle.com (abhmp0007.oracle.com [141.146.116.13]) by userv0121.oracle.com (8.14.4/8.13.8) with ESMTP id x1R03PHk020730; Wed, 27 Feb 2019 00:03:25 GMT Received: from [192.168.1.164] (/50.38.38.67) by default (Oracle Beehive Gateway v4.0) with ESMTP ; Tue, 26 Feb 2019 16:03:24 -0800 Subject: Re: [PATCH v4] mm/hugetlb: Fix unsigned overflow in __nr_hugepages_store_common() To: Andrew Morton , David Rientjes Cc: Jing Xiangfeng , mhocko@kernel.org, hughd@google.com, linux-mm@kvack.org, n-horiguchi@ah.jp.nec.com, Andrea Arcangeli , kirill.shutemov@linux.intel.com, linux-kernel@vger.kernel.org References: <1550885529-125561-1-git-send-email-jingxiangfeng@huawei.com> <388cbbf5-7086-1d04-4c49-049021504b9d@oracle.com> <8c167be7-06fa-a8c0-8ee7-0bfad41eaba2@oracle.com> <13400ee2-3d3b-e5d6-2d78-a770820417de@oracle.com> <5C74A2DA.1030304@huawei.com> <20190226143620.c6af15c7c897d3362b191e36@linux-foundation.org> From: Mike Kravetz Message-ID: <086c4a4b-a37d-f144-00c0-d9a4062cc5fe@oracle.com> Date: Tue, 26 Feb 2019 16:03:23 -0800 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Thunderbird/60.4.0 MIME-Version: 1.0 In-Reply-To: <20190226143620.c6af15c7c897d3362b191e36@linux-foundation.org> Content-Type: text/plain; charset=utf-8 Content-Language: en-US Content-Transfer-Encoding: 7bit X-Proofpoint-Virus-Version: vendor=nai engine=5900 definitions=9179 signatures=668685 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 priorityscore=1501 malwarescore=0 suspectscore=2 phishscore=0 bulkscore=0 spamscore=0 clxscore=1015 lowpriorityscore=0 mlxscore=0 impostorscore=0 mlxlogscore=999 adultscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.0.1-1810050000 definitions=main-1902260162 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 2/26/19 2:36 PM, Andrew Morton wrote: >> ... >> >> --- a/mm/hugetlb.c >> +++ b/mm/hugetlb.c >> @@ -2274,7 +2274,7 @@ static int adjust_pool_surplus(struct hstate *h, >> nodemask_t *nodes_allowed, > > Please tweak that email client to prevent the wordwraps. Sorry about that. >> + /* >> + * Check for a node specific request. Adjust global count, but >> + * restrict alloc/free to the specified node. >> + */ Better comment might be: /* * Check for a node specific request. * Changing node specific huge page count may require a corresponding * change to the global count. In any case, the passed node mask * (nodes_allowed) will restrict alloc/free to the specified node. */ >> + if (nid != NUMA_NO_NODE) { >> + unsigned long old_count = count; >> + count += h->nr_huge_pages - h->nr_huge_pages_node[nid]; >> + /* >> + * If user specified count causes overflow, set to >> + * largest possible value. >> + */ Updated comment: /* * User may have specified a large count value which caused the * above calculation to overflow. In this case, they wanted * to allocate as many huge pages as possible. Set count to * largest possible value to align with their intention. */ >> + if (count < old_count) >> + count = ULONG_MAX; >> + } > > The above two comments explain the code, but do not reveal the > reasoning behind the policy decisions which that code implements. > >> ... >> >> + } else { >> /* >> - * per node hstate attribute: adjust count to global, >> - * but restrict alloc/free to the specified node. >> + * Node specific request, but we could not allocate >> + * node mask. Pass in ALL nodes, and clear nid. >> */ > > Ditto here, somewhat. I was just going to update the comments and send you a new patch, but but your comment got me thinking about this situation. I did not really change the way this code operates. As a reminder, the original code is like: NODEMASK_ALLOC(nodemask_t, nodes_allowed, GFP_KERNEL | __GFP_NORETRY); if (nid == NUMA_NO_NODE) { /* do something */ } else if (nodes_allowed) { /* do something else */ } else { nodes_allowed = &node_states[N_MEMORY]; } So, the only way we get to that final else if if we can not allocate a node mask (kmalloc a few words). Right? I wonder why we should even try to continue in this case. Why not just return right there? The specified count value is either a request to increase number of huge pages or decrease. If we can't allocate a few words, we certainly are not going to find memory to create huge pages. There 'might' be surplus pages which can be converted to permanent pages. But remember this is a 'node specific' request and we can't allocate a mask to pass down to the conversion routines. So, chances are good we would operate on the wrong node. The same goes for a request to 'free' huge pages. Since, we can't allocate a node mask we are likely to free them from the wrong node. Unless my reasoning above is incorrect, I think that final else block in __nr_hugepages_store_common() is wrong. Any additional thoughts? -- Mike Kravetz