Received: by 2002:a05:6a10:a0d1:0:0:0:0 with SMTP id j17csp282917pxa; Wed, 12 Aug 2020 01:40:16 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyjCxzg2+p4Fg0esjRnq8jai9DR53WDWHoKV/OlyMIr6lrL2kY3m1b6tmPiQDI7PvA9EoCv X-Received: by 2002:a50:d75e:: with SMTP id i30mr31007077edj.246.1597221615911; Wed, 12 Aug 2020 01:40:15 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1597221615; cv=none; d=google.com; s=arc-20160816; b=1Br2MYVgBu9CFYFuBY+Cp770jmCcvRXziIuTQvwHO7Bbp7SZ0a0j5hiWNShMzPDFQ9 YGrj+yUadMUkxL9p0ZrrKX/V6KGkJt3ZboLqjY7je66qgE+LdjZyAI1c/bhIhoeem/bw pfupOB5asuRVxD7KM2PGsXUeC+c62mNZElMrnCYVVaKovaZ50oLdlWha0Je0NQlyRZhm ga8oTcokcIedOX1qoSF/8qFjiqziZv77hjAaKwyjsxHflsi22semtiTipCUq8sk1RD+A StL4zzPSlEuGBUMUKdxhyHU06VHiopYt2Q4R5gNfpgNqp+ukbm+Ro5UEyV97A8jSn6TD 9hmQ== 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; bh=e2gxI+910X3Lte6nj7zeYLb2LEGuh62q+Xo5691oQF0=; b=zD01TTLfNqXAmVXbmZwJ/fHgEqhFADWyhbAUohuEav/WmjoRP417IDOhc6gVo6dpCX 5BozTgECNR/3KtW6v2uOnlcYGULPjTEzboV8Eo6ggVsTeXzZZat7dOYMBoSxnRpxAXEf qNexIXC5UlvobP86S/VdV3xf7wsYIf1zHLXiOa+6h7+Nj5KCmu73tB6BQcXPoyxY/bas fZrj8u6L08ViYr3mRPbO7tCY7e1IMo/Tu3tHf1/cxfCJhu92ZhtN+Q8T0ihHCDAevftU n/TW08bRMDEYf4GaWilnzhC+Ui7SXEUTL1RxaKdKty00mCpamJMfjbNjLxcBvwk44La9 N8Qw== ARC-Authentication-Results: i=1; mx.google.com; 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 Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id a1si842914edy.514.2020.08.12.01.39.52; Wed, 12 Aug 2020 01:40:15 -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; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727001AbgHLIjO (ORCPT + 99 others); Wed, 12 Aug 2020 04:39:14 -0400 Received: from foss.arm.com ([217.140.110.172]:42788 "EHLO foss.arm.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726712AbgHLIjN (ORCPT ); Wed, 12 Aug 2020 04:39:13 -0400 Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id AACB4D6E; Wed, 12 Aug 2020 01:39:12 -0700 (PDT) Received: from [192.168.1.179] (unknown [172.31.20.19]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id 6792E3F22E; Wed, 12 Aug 2020 01:39:10 -0700 (PDT) Subject: Re: [PATCH 2/2] mm: proc: smaps_rollup: do not stall write attempts on mmap_lock To: Chinwen Chang , Matthias Brugger , Michel Lespinasse , Andrew Morton , Vlastimil Babka , Daniel Jordan , Davidlohr Bueso , Alexey Dobriyan , "Matthew Wilcox (Oracle)" , Jason Gunthorpe , Song Liu , Jimmy Assarsson , Huang Ying Cc: linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-mediatek@lists.infradead.org, linux-fsdevel@vger.kernel.org, wsd_upstream@mediatek.com References: <1597120955-16495-1-git-send-email-chinwen.chang@mediatek.com> <1597120955-16495-3-git-send-email-chinwen.chang@mediatek.com> From: Steven Price Message-ID: Date: Wed, 12 Aug 2020 09:39:09 +0100 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Thunderbird/68.10.0 MIME-Version: 1.0 In-Reply-To: <1597120955-16495-3-git-send-email-chinwen.chang@mediatek.com> Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-GB Content-Transfer-Encoding: 7bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 11/08/2020 05:42, Chinwen Chang wrote: > smaps_rollup will try to grab mmap_lock and go through the whole vma > list until it finishes the iterating. When encountering large processes, > the mmap_lock will be held for a longer time, which may block other > write requests like mmap and munmap from progressing smoothly. > > There are upcoming mmap_lock optimizations like range-based locks, but > the lock applied to smaps_rollup would be the coarse type, which doesn't > avoid the occurrence of unpleasant contention. > > To solve aforementioned issue, we add a check which detects whether > anyone wants to grab mmap_lock for write attempts. > > Signed-off-by: Chinwen Chang > --- > fs/proc/task_mmu.c | 21 +++++++++++++++++++++ > 1 file changed, 21 insertions(+) > > diff --git a/fs/proc/task_mmu.c b/fs/proc/task_mmu.c > index dbda449..4b51f25 100644 > --- a/fs/proc/task_mmu.c > +++ b/fs/proc/task_mmu.c > @@ -856,6 +856,27 @@ static int show_smaps_rollup(struct seq_file *m, void *v) > for (vma = priv->mm->mmap; vma; vma = vma->vm_next) { > smap_gather_stats(vma, &mss); > last_vma_end = vma->vm_end; > + > + /* > + * Release mmap_lock temporarily if someone wants to > + * access it for write request. > + */ > + if (mmap_lock_is_contended(mm)) { > + mmap_read_unlock(mm); > + ret = mmap_read_lock_killable(mm); > + if (ret) { > + release_task_mempolicy(priv); > + goto out_put_mm; > + } > + > + /* Check whether current vma is available */ > + vma = find_vma(mm, last_vma_end - 1); > + if (vma && vma->vm_start < last_vma_end) I may be wrong, but this looks like it could return incorrect results. For example if we start reading with the following VMAs: +------+------+-----------+ | VMA1 | VMA2 | VMA3 | +------+------+-----------+ | | | | 4k 8k 16k 400k Then after reading VMA2 we drop the lock due to contention. So: last_vma_end = 16k Then if VMA2 is freed while the lock is dropped, so we have: +------+ +-----------+ | VMA1 | | VMA3 | +------+ +-----------+ | | | | 4k 8k 16k 400k find_vma(mm, 16k-1) will then return VMA3 and the condition vm_start < last_vma_end will be false. > + continue; > + > + /* Current vma is not available, just break */ > + break; Which means we break out here and report an incomplete output (the numbers will be much smaller than reality). Would it be better to have a loop like: for (vma = priv->mm->mmap; vma;) { smap_gather_stats(vma, &mss); last_vma_end = vma->vm_end; if (contended) { /* drop/acquire lock */ vma = find_vma(mm, last_vma_end - 1); if (!vma) break; if (vma->vm_start >= last_vma_end) continue; } vma = vma->vm_next; } that way if the VMA is removed while the lock is dropped the loop can just continue from the next VMA. Or perhaps I missed something obvious? I haven't actually tested anything above. Steve > + } > } > > show_vma_header_prefix(m, priv->mm->mmap->vm_start, >