Received: by 2002:a05:6a10:9848:0:0:0:0 with SMTP id x8csp867079pxf; Wed, 7 Apr 2021 13:41:10 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxvRfnozdxJnFejrChMyQ5Bpeqs7NCYOMMV85BYeT1mQ5LcJi3bb5R5x383dBTPj93N743r X-Received: by 2002:a05:6402:9:: with SMTP id d9mr6740542edu.67.1617828070533; Wed, 07 Apr 2021 13:41:10 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1617828070; cv=none; d=google.com; s=arc-20160816; b=MKT/UUIAUWmNTfVBKxp5+0+6PZ9p+8AmDPTj5QJT+ryNpOxAsXzDPNDyYTTUg8LPYg jjiUdEH36pZ9yR7CnbqbT6n8OLht/EtqHLgFKXh5BK49BpoPDqJoJiewlJoyIQ2UbSQZ 9X+94WquZnKPyyu9CKjWgpOvpnTg+rZoP7iLGOozGI7OLcEANmEyNZUFd0rCVwsRqRMv B9CwYt8K0EBu9bqMusO2DnJ/6Vnvc787uWSY5j9WJ/bo48cg6ZC2s72PSIuPLroIg+QN 2/pufQ1++gkD46hJQv5ZYiXTNVsBCHtYCdhhtK6kmwlovUbrrP10Sn7pKiJxe3g/YtNp ZLhw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:content-language :in-reply-to:mime-version:user-agent:date:message-id:organization :from:references:cc:to:subject:dkim-signature; bh=ozcnQwHUu8p4zYE+iIFxAZOxz9lcMS0/tDJHr1riY2Q=; b=pVJAihVpk5x5Ko0j6EJHXAkeP+PMQhBx5dH6UC314qm50b9RLB/kndEfdKzj9UnfuZ b73aU4ukoh1OpSUKvGtHjn6YKLnpLmiOG/ee/9Y/cc4LtcVlXztX33Y2pRAphbsmDROr 25QuiXFw9Rn/7HO5JvCUFpiW8ITJ7PdCqxu+7iYpxappCz1Fch6ONMWke0cU3tLsa9nQ k2crux3SJcrfndkzfiFg+P0sSV7XLYV0cvlEJcMuKRQ6zxWx/tOQYTMkpQzEEEpbvK+J 2O7d47Kx9vNE8E2gLooIRAteZHGnOufdup4VNtmSVR+Luh0PL0uG9cJUj/5Uz2q9ZaR1 M6kA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=bgmDelG1; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id g17si3804258ejp.201.2021.04.07.13.40.47; Wed, 07 Apr 2021 13:41:10 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=bgmDelG1; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1350091AbhDGJ2r (ORCPT + 99 others); Wed, 7 Apr 2021 05:28:47 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]:49013 "EHLO us-smtp-delivery-124.mimecast.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S243648AbhDGJ2q (ORCPT ); Wed, 7 Apr 2021 05:28:46 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1617787716; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=ozcnQwHUu8p4zYE+iIFxAZOxz9lcMS0/tDJHr1riY2Q=; b=bgmDelG1FOABIXSF6dt3g8IW7/eBjOQ+tAH0OvHrBZhpVvAydqSlgoYp7J5gfS2TsvwDuO 5YAawuZgCQ1Ri90S+VK0VahaAb0AKyMuo98XAYDi/GwB2YDtvghAbVTnUBU3x3p+rDc5h2 Oy5JUsvCq5q1kM8p/m87TAyj/kxtjtM= Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-128-hVPpjPlvPA-97rcJg60t-A-1; Wed, 07 Apr 2021 05:28:33 -0400 X-MC-Unique: hVPpjPlvPA-97rcJg60t-A-1 Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.phx2.redhat.com [10.5.11.22]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id B99B010054F6; Wed, 7 Apr 2021 09:28:30 +0000 (UTC) Received: from [10.36.114.68] (ovpn-114-68.ams2.redhat.com [10.36.114.68]) by smtp.corp.redhat.com (Postfix) with ESMTP id F25E4100164A; Wed, 7 Apr 2021 09:28:18 +0000 (UTC) Subject: Re: [PATCH v4 3/8] hugetlb: add per-hstate mutex to synchronize user adjustments To: Mike Kravetz , linux-mm@kvack.org, linux-kernel@vger.kernel.org Cc: Roman Gushchin , Michal Hocko , Shakeel Butt , Oscar Salvador , Muchun Song , David Rientjes , Miaohe Lin , Peter Zijlstra , Matthew Wilcox , HORIGUCHI NAOYA , "Aneesh Kumar K . V" , Waiman Long , Peter Xu , Mina Almasry , Hillf Danton , Joonsoo Kim , Barry Song , Will Deacon , Andrew Morton References: <20210405230043.182734-1-mike.kravetz@oracle.com> <20210405230043.182734-4-mike.kravetz@oracle.com> From: David Hildenbrand Organization: Red Hat GmbH Message-ID: Date: Wed, 7 Apr 2021 11:28:17 +0200 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Thunderbird/78.8.1 MIME-Version: 1.0 In-Reply-To: <20210405230043.182734-4-mike.kravetz@oracle.com> Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 7bit X-Scanned-By: MIMEDefang 2.84 on 10.5.11.22 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 06.04.21 01:00, Mike Kravetz wrote: > The helper routine hstate_next_node_to_alloc accesses and modifies the > hstate variable next_nid_to_alloc. The helper is used by the routines > alloc_pool_huge_page and adjust_pool_surplus. adjust_pool_surplus is > called with hugetlb_lock held. However, alloc_pool_huge_page can not > be called with the hugetlb lock held as it will call the page allocator. > Two instances of alloc_pool_huge_page could be run in parallel or > alloc_pool_huge_page could run in parallel with adjust_pool_surplus > which may result in the variable next_nid_to_alloc becoming invalid > for the caller and pages being allocated on the wrong node. > > Both alloc_pool_huge_page and adjust_pool_surplus are only called from > the routine set_max_huge_pages after boot. set_max_huge_pages is only > called as the reusult of a user writing to the proc/sysfs nr_hugepages, > or nr_hugepages_mempolicy file to adjust the number of hugetlb pages. > > It makes little sense to allow multiple adjustment to the number of > hugetlb pages in parallel. Add a mutex to the hstate and use it to only > allow one hugetlb page adjustment at a time. This will synchronize > modifications to the next_nid_to_alloc variable. > > Signed-off-by: Mike Kravetz > Acked-by: Michal Hocko > Reviewed-by: Oscar Salvador > Reviewed-by: Miaohe Lin > Reviewed-by: Muchun Song > --- > include/linux/hugetlb.h | 1 + > mm/hugetlb.c | 8 ++++++++ > 2 files changed, 9 insertions(+) > > diff --git a/include/linux/hugetlb.h b/include/linux/hugetlb.h > index d9b78e82652f..b92f25ccef58 100644 > --- a/include/linux/hugetlb.h > +++ b/include/linux/hugetlb.h > @@ -566,6 +566,7 @@ HPAGEFLAG(Freed, freed) > #define HSTATE_NAME_LEN 32 > /* Defines one hugetlb page size */ > struct hstate { > + struct mutex resize_lock; > int next_nid_to_alloc; > int next_nid_to_free; > unsigned int order; > diff --git a/mm/hugetlb.c b/mm/hugetlb.c > index 1d62f0492e7b..8497a3598c86 100644 > --- a/mm/hugetlb.c > +++ b/mm/hugetlb.c > @@ -2730,6 +2730,11 @@ static int set_max_huge_pages(struct hstate *h, unsigned long count, int nid, > else > return -ENOMEM; > > + /* > + * resize_lock mutex prevents concurrent adjustments to number of > + * pages in hstate via the proc/sysfs interfaces. > + */ > + mutex_lock(&h->resize_lock); > spin_lock(&hugetlb_lock); > > /* > @@ -2762,6 +2767,7 @@ static int set_max_huge_pages(struct hstate *h, unsigned long count, int nid, > if (hstate_is_gigantic(h) && !IS_ENABLED(CONFIG_CONTIG_ALLOC)) { > if (count > persistent_huge_pages(h)) { > spin_unlock(&hugetlb_lock); > + mutex_unlock(&h->resize_lock); > NODEMASK_FREE(node_alloc_noretry); > return -EINVAL; > } > @@ -2836,6 +2842,7 @@ static int set_max_huge_pages(struct hstate *h, unsigned long count, int nid, > out: > h->max_huge_pages = persistent_huge_pages(h); > spin_unlock(&hugetlb_lock); > + mutex_unlock(&h->resize_lock); > > NODEMASK_FREE(node_alloc_noretry); > > @@ -3323,6 +3330,7 @@ void __init hugetlb_add_hstate(unsigned int order) > BUG_ON(hugetlb_max_hstate >= HUGE_MAX_HSTATE); > BUG_ON(order == 0); > h = &hstates[hugetlb_max_hstate++]; > + mutex_init(&h->resize_lock); > h->order = order; > h->mask = ~(huge_page_size(h) - 1); > for (i = 0; i < MAX_NUMNODES; ++i) > Reviewed-by: David Hildenbrand -- Thanks, David / dhildenb