Received: by 2002:a25:c593:0:0:0:0:0 with SMTP id v141csp1956624ybe; Thu, 12 Sep 2019 02:10:37 -0700 (PDT) X-Google-Smtp-Source: APXvYqzVwMr4/1J6pH5XGOgORO5PL+v+9r1f/O0p5T3NrQtk5e8m4dDYdfuzsEg1/bOFGWh6cJCL X-Received: by 2002:a17:906:349b:: with SMTP id g27mr33538430ejb.130.1568279436891; Thu, 12 Sep 2019 02:10:36 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1568279436; cv=none; d=google.com; s=arc-20160816; b=kKkBWnDomSmZynqJ5sc+EPeNdRIas2Qtj8o0KFwLNIIrBz8q0+wXyvKBvCzlOXwM2v kBneufKQ3vCDKcZoZAOt+FKS8d9Q/q764+PskXOosefmfN+2/AXU1/ttEPJe2YdhmJaM dn5Iqv3IzKSi/8l1NNtwJ5vAXSbatgYNRoj6tdMnAIu4a/dEfkchQWlISJdlmAeKEdYl ByareJMV3MBd2/+egTc8MpfjHQp21DYOH90U4069gt9Sxs2YF9xC4/olvy64Fnr0e49w gh2Z/LgDw435Zl+H/lZCkUT2bTTTtM8g9VfrX9uUQpYz7Au6EY66Uyw4URlIn7WsezqK tERA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-language :content-transfer-encoding:in-reply-to:mime-version:user-agent:date :message-id:organization:from:references:cc:to:subject; bh=8o7y+K2WcTloTFYJUMQD8UvMHEbNxVaRrbkevQ5k9cM=; b=rjTQFIuTMREQEZU1UulGfR10gah8yUg5Ls5ctYmKjOyBjBuqHaj9lzqCH9x3Z4nFFM B6L29WE465/3/fLzJHxDErJ8Kzzcc5xKWgh+irPUyAMZQZ5CiiSx/Un1hta1m/qCDJKN rnPqAXHbAnbPc4LcL+N4CL74hFMr21epf6Ui0Of8XtVG/WKN39Rps3huAJKSpW6YMlb4 pzE+Y4lHYnOg35UQnGh/ne/ls7YoYoRCyHRkg1ls2gxAfDnp2d8T9xyaT3XHQtXR5Hce LVoD0627cALhtOu9S8bXDV1kodIRlE6S7vr1QnWPxl9n+1xnqZI43ef4e83U1UUl8sVS pagg== 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; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id e20si12725938ejl.225.2019.09.12.02.10.13; Thu, 12 Sep 2019 02:10:36 -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; 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=fail (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1730526AbfILJGY (ORCPT + 99 others); Thu, 12 Sep 2019 05:06:24 -0400 Received: from mx1.redhat.com ([209.132.183.28]:40360 "EHLO mx1.redhat.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1730327AbfILJGY (ORCPT ); Thu, 12 Sep 2019 05:06:24 -0400 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 882FB18C4272; Thu, 12 Sep 2019 09:06:23 +0000 (UTC) Received: from llong.remote.csb (ovpn-120-238.rdu2.redhat.com [10.10.120.238]) by smtp.corp.redhat.com (Postfix) with ESMTP id B31CA600C4; Thu, 12 Sep 2019 09:06:18 +0000 (UTC) Subject: Re: [PATCH 5/5] hugetlbfs: Limit wait time when trying to share huge PMD To: Mike Kravetz , Peter Zijlstra , Ingo Molnar , Will Deacon , Alexander Viro Cc: linux-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, Davidlohr Bueso References: <20190911150537.19527-1-longman@redhat.com> <20190911150537.19527-6-longman@redhat.com> From: Waiman Long Organization: Red Hat Message-ID: Date: Thu, 12 Sep 2019 10:06:14 +0100 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Thunderbird/60.7.2 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 7bit Content-Language: en-US X-Scanned-By: MIMEDefang 2.79 on 10.5.11.11 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.6.2 (mx1.redhat.com [10.5.110.62]); Thu, 12 Sep 2019 09:06:23 +0000 (UTC) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 9/12/19 4:26 AM, Mike Kravetz wrote: > On 9/11/19 8:05 AM, Waiman Long wrote: >> When allocating a large amount of static hugepages (~500-1500GB) on a >> system with large number of CPUs (4, 8 or even 16 sockets), performance >> degradation (random multi-second delays) was observed when thousands >> of processes are trying to fault in the data into the huge pages. The >> likelihood of the delay increases with the number of sockets and hence >> the CPUs a system has. This only happens in the initial setup phase >> and will be gone after all the necessary data are faulted in. >> >> These random delays, however, are deemed unacceptable. The cause of >> that delay is the long wait time in acquiring the mmap_sem when trying >> to share the huge PMDs. >> >> To remove the unacceptable delays, we have to limit the amount of wait >> time on the mmap_sem. So the new down_write_timedlock() function is >> used to acquire the write lock on the mmap_sem with a timeout value of >> 10ms which should not cause a perceivable delay. If timeout happens, >> the task will abandon its effort to share the PMD and allocate its own >> copy instead. >> > >> diff --git a/mm/hugetlb.c b/mm/hugetlb.c >> index 6d7296dd11b8..445af661ae29 100644 >> --- a/mm/hugetlb.c >> +++ b/mm/hugetlb.c >> @@ -4750,6 +4750,8 @@ void adjust_range_if_pmd_sharing_possible(struct vm_area_struct *vma, >> } >> } >> >> +#define PMD_SHARE_DISABLE_THRESHOLD (1 << 8) >> + >> /* >> * Search for a shareable pmd page for hugetlb. In any case calls pmd_alloc() >> * and returns the corresponding pte. While this is not necessary for the >> @@ -4770,11 +4772,24 @@ pte_t *huge_pmd_share(struct mm_struct *mm, unsigned long addr, pud_t *pud) >> pte_t *spte = NULL; >> pte_t *pte; >> spinlock_t *ptl; >> + static atomic_t timeout_cnt; >> >> - if (!vma_shareable(vma, addr)) >> - return (pte_t *)pmd_alloc(mm, pud, addr); >> + /* >> + * Don't share if it is not sharable or locking attempt timed out >> + * after 10ms. After 256 timeouts, PMD sharing will be permanently >> + * disabled as it is just too slow. >> + */ >> + if (!vma_shareable(vma, addr) || >> + (atomic_read(&timeout_cnt) >= PMD_SHARE_DISABLE_THRESHOLD)) >> + goto out_no_share; >> + >> + if (!i_mmap_timedlock_write(mapping, ms_to_ktime(10))) { >> + if (atomic_inc_return(&timeout_cnt) == >> + PMD_SHARE_DISABLE_THRESHOLD) >> + pr_info("Hugetlbfs PMD sharing disabled because of timeouts!\n"); >> + goto out_no_share; >> + } >> >> - i_mmap_lock_write(mapping); > All this got me wondering if we really need to take i_mmap_rwsem in write > mode here. We are not changing the tree, only traversing it looking for > a suitable vma. > > Unless I am missing something, the hugetlb code only ever takes the semaphore > in write mode; never read. Could this have been the result of changing the > tree semaphore to read/write? Instead of analyzing all the code, the easiest > and safest thing would have been to take all accesses in write mode. > > I can investigate more, but wanted to ask the question in case someone already > knows. > > At one time, I thought it was safe to acquire the semaphore in read mode for > huge_pmd_share, but write mode for huge_pmd_unshare. See commit b43a99900559. > This was reverted along with another patch for other reasons. > > If we change change from write to read mode, this may have significant impact > on the stalls. If we can take the rwsem in read mode, that should solve the problem AFAICS. As I don't have a full understanding of the history of that code, I didn't try to do that in my patch. Cheers, Longman