Received: by 2002:ac0:8845:0:0:0:0:0 with SMTP id g63csp92367img; Mon, 25 Feb 2019 18:24:21 -0800 (PST) X-Google-Smtp-Source: AHgI3IbXEu67K7FuElZw3S5kHLZDCtRZye5eaivj5IRt4xpPSOZHNClGLNVpuQqstJugazG47Ppf X-Received: by 2002:a17:902:7b8c:: with SMTP id w12mr22006989pll.207.1551147861178; Mon, 25 Feb 2019 18:24:21 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1551147861; cv=none; d=google.com; s=arc-20160816; b=dL6cLHmMue40qY03UK2uTz/JGVEJQWuwkI/GkHGy1iaCQOoUEqzsFqHREAObRn1cxw QoNxmBlrbXXgn1++f6I8IyPHTvzfDp0CC7FUILjyWbCdhkAUxDnUn/SSHrJvNloXWRqA bD/qkhBIhO1ljU0JRpO7tU7PCxRO6fW9HorWykmvLffkeFEuxpZiUhW3yEyKG/L49omx 79fEFbFHzm94PKiTrjXfWVdW87D4W8UD6Z8McoZeB5y1JfzJoZmXSAFr6QoT9TnojDXi l/AzU1aFQy283T8ZU7vxziMWZXSLOAfrgNzSPbgdqCM3V/Poqz+ykGvg7pMol49aK7gq ru0w== 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:in-reply-to :mime-version:user-agent:date:message-id:from:cc:references:to :subject; bh=ECARpSwXnhvgAlRX5fisvhJrlkHQlIGmYnCbRxddYzs=; b=FI5TdNS8vBKOWgN1B4FpnfI6JbW2XvYPLfhYCNCNGqiDcETfdoX8WO8sO7XluNIVDP HU8HxRyXk0lLr2Dr13//JIjRNbXsf8Ge4v7KbAS1HDg4qFpY/6RVsxfrBL0EVOcv+jdr wEkFLT6XnAafGvZNyiW6IpaWK13inB1sRq9XhUnEHQxWNIwcjd8o8RjoSMQVQEQzdVvq 4bTSXnMdYhLgRrEKccHH8OKo9B/ymw9LP/tZOYFXlgZecM3QlaU89j466xIdW82FboMM tQL/XGB9MRGF2xFUkPoRiMZ40ei+BJ8FRnktoKhbhxYiQQFE5oX6Lgu+3bugH5N3G9Me ur3w== ARC-Authentication-Results: i=1; mx.google.com; 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 q15si11196808pgg.570.2019.02.25.18.23.33; Mon, 25 Feb 2019 18:24:21 -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; 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 S1726116AbfBZCWm (ORCPT + 99 others); Mon, 25 Feb 2019 21:22:42 -0500 Received: from szxga05-in.huawei.com ([45.249.212.191]:4735 "EHLO huawei.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1725929AbfBZCWm (ORCPT ); Mon, 25 Feb 2019 21:22:42 -0500 Received: from DGGEMS410-HUB.china.huawei.com (unknown [172.30.72.60]) by Forcepoint Email with ESMTP id 87861A41F452FCD66313; Tue, 26 Feb 2019 10:22:39 +0800 (CST) Received: from [127.0.0.1] (10.184.39.28) by DGGEMS410-HUB.china.huawei.com (10.3.19.210) with Microsoft SMTP Server id 14.3.408.0; Tue, 26 Feb 2019 10:22:36 +0800 Subject: Re: [PATCH v4] mm/hugetlb: Fix unsigned overflow in __nr_hugepages_store_common() To: David Rientjes , Mike Kravetz 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> CC: , , , , , , , From: Jing Xiangfeng Message-ID: <5C74A2DA.1030304@huawei.com> Date: Tue, 26 Feb 2019 10:22:18 +0800 User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:38.0) Gecko/20100101 Thunderbird/38.1.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset="windows-1252" Content-Transfer-Encoding: 7bit X-Originating-IP: [10.184.39.28] X-CFilter-Loop: Reflected Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 2019/2/26 3:17, David Rientjes wrote: > On Mon, 25 Feb 2019, Mike Kravetz wrote: > >> Ok, what about just moving the calculation/check inside the lock as in the >> untested patch below? >> >> Signed-off-by: Mike Kravetz >> --- >> mm/hugetlb.c | 34 ++++++++++++++++++++++++++-------- >> 1 file changed, 26 insertions(+), 8 deletions(-) >> >> diff --git a/mm/hugetlb.c b/mm/hugetlb.c >> index 1c5219193b9e..5afa77dc7bc8 100644 >> --- a/mm/hugetlb.c >> +++ b/mm/hugetlb.c >> @@ -2274,7 +2274,7 @@ static int adjust_pool_surplus(struct hstate *h, >> nodemask_t *nodes_allowed, >> } >> >> #define persistent_huge_pages(h) (h->nr_huge_pages - h->surplus_huge_pages) >> -static int set_max_huge_pages(struct hstate *h, unsigned long count, >> +static int set_max_huge_pages(struct hstate *h, unsigned long count, int nid, >> nodemask_t *nodes_allowed) >> { >> unsigned long min_count, ret; >> @@ -2289,6 +2289,23 @@ static int set_max_huge_pages(struct hstate *h, unsigned >> long count, >> goto decrease_pool; >> } >> >> + spin_lock(&hugetlb_lock); >> + >> + /* >> + * Check for a node specific request. Adjust global count, but >> + * 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. >> + */ >> + if (count < old_count) >> + count = ULONG_MAX; >> + } >> + >> /* >> * Increase the pool size >> * First take pages out of surplus state. Then make up the >> @@ -2300,7 +2317,6 @@ static int set_max_huge_pages(struct hstate *h, unsigned >> long count, >> * pool might be one hugepage larger than it needs to be, but >> * within all the constraints specified by the sysctls. >> */ >> - spin_lock(&hugetlb_lock); >> while (h->surplus_huge_pages && count > persistent_huge_pages(h)) { >> if (!adjust_pool_surplus(h, nodes_allowed, -1)) >> break; >> @@ -2421,16 +2437,18 @@ static ssize_t __nr_hugepages_store_common(bool >> obey_mempolicy, >> nodes_allowed = &node_states[N_MEMORY]; >> } >> } else if (nodes_allowed) { >> + /* Node specific request */ >> + init_nodemask_of_node(nodes_allowed, nid); >> + } 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. >> */ >> - count += h->nr_huge_pages - h->nr_huge_pages_node[nid]; >> - init_nodemask_of_node(nodes_allowed, nid); >> - } else >> + nid = NUMA_NO_NODE; >> nodes_allowed = &node_states[N_MEMORY]; >> + } >> >> - err = set_max_huge_pages(h, count, nodes_allowed); >> + err = set_max_huge_pages(h, count, nid, nodes_allowed); >> if (err) >> goto out; >> > > Looks good; Jing, could you test that this fixes your case? Yes, I have tested this patch, it can also fix my case. > > . >