From: David Rientjes <rientjes@google.com>
To: Mike Kravetz <mike.kravetz@oracle.com>
Cc: Jing Xiangfeng <jingxiangfeng@huawei.com>,
mhocko@kernel.org, akpm@linux-foundation.org, hughd@google.com,
linux-mm@kvack.org, n-horiguchi@ah.jp.nec.com,
aarcange@redhat.com, kirill.shutemov@linux.intel.com,
linux-kernel@vger.kernel.org
Subject: Re: [PATCH v4] mm/hugetlb: Fix unsigned overflow in __nr_hugepages_store_common()
Date: Mon, 25 Feb 2019 11:17:14 -0800 (PST) [thread overview]
Message-ID: <alpine.DEB.2.21.1902251116180.167839@chino.kir.corp.google.com> (raw)
In-Reply-To: <13400ee2-3d3b-e5d6-2d78-a770820417de@oracle.com>
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 <mike.kravetz@oracle.com>
> ---
> 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?
next prev parent reply other threads:[~2019-02-25 19:17 UTC|newest]
Thread overview: 20+ messages / expand[flat|nested] mbox.gz Atom feed top
2019-02-23 1:32 [PATCH v4] mm/hugetlb: Fix unsigned overflow in __nr_hugepages_store_common() Jing Xiangfeng
2019-02-25 0:45 ` Mike Kravetz
2019-02-25 3:17 ` David Rientjes
2019-02-25 16:49 ` Mike Kravetz
2019-02-25 18:19 ` Mike Kravetz
2019-02-25 19:17 ` David Rientjes [this message]
2019-02-26 2:22 ` Jing Xiangfeng
2019-02-26 6:21 ` David Rientjes
2019-02-26 19:32 ` Mike Kravetz
2019-02-26 22:36 ` Andrew Morton
2019-02-27 0:03 ` Mike Kravetz
2019-03-04 13:48 ` Oscar Salvador
2019-03-05 0:03 ` Naoya Horiguchi
2019-03-05 4:15 ` Mike Kravetz
2019-03-05 21:16 ` Andrew Morton
2019-03-05 21:35 ` Mike Kravetz
2019-03-05 21:41 ` Alex Ghiti
2019-03-06 9:41 ` Oscar Salvador
2019-03-07 0:17 ` Mike Kravetz
2019-03-04 6:00 ` Naoya Horiguchi
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=alpine.DEB.2.21.1902251116180.167839@chino.kir.corp.google.com \
--to=rientjes@google.com \
--cc=aarcange@redhat.com \
--cc=akpm@linux-foundation.org \
--cc=hughd@google.com \
--cc=jingxiangfeng@huawei.com \
--cc=kirill.shutemov@linux.intel.com \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=mhocko@kernel.org \
--cc=mike.kravetz@oracle.com \
--cc=n-horiguchi@ah.jp.nec.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).