From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-11.8 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_PATCH, MAILING_LIST_MULTI,NICE_REPLY_A,SIGNED_OFF_BY,SPF_HELO_NONE,SPF_PASS, USER_AGENT_SANE_1 autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 6C647C433E1 for ; Mon, 17 Aug 2020 09:09:24 +0000 (UTC) Received: from merlin.infradead.org (merlin.infradead.org [205.233.59.134]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 2BEC22063A for ; Mon, 17 Aug 2020 09:09:24 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=lists.infradead.org header.i=@lists.infradead.org header.b="s3p08n93" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 2BEC22063A Authentication-Results: mail.kernel.org; dmarc=none (p=none dis=none) header.from=arm.com Authentication-Results: mail.kernel.org; spf=none smtp.mailfrom=linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=merlin.20170209; h=Sender:Content-Type: Content-Transfer-Encoding:Cc:List-Subscribe:List-Help:List-Post:List-Archive: List-Unsubscribe:List-Id:In-Reply-To:MIME-Version:Date:Message-ID:From: References:To:Subject:Reply-To:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=bNf6gKng4i2ADMq02MxsGs4LfwzHRng5a0e9jBDQ6xk=; b=s3p08n93mG0gjWgYOC6ZZehk6 UNKLo9SPaBuQzEE4LtpGeoDUTRxTLVkavKJK2B7A6wF8n/QXKvehQGh0H77kL2JRL1NYx6XTPwivX w2VP4xpVuUA+Q6KUES2OegXG9SVVNvQ+t80axuY/XX+Rr8gDvlcIlNPqe4zPMmE856mk2Amg/UEpG QU4PGav89pHYMP+7wyfK4Mqw+CQD5q15yb87fRazjo7+uK1oOhg4VQ9kyK+lwYTjJR9UtxCI/YBUo 1aiTQWD819Ol/F05sK6R6ipspsG+81f9vWhXGB8BEsTzwrOSYSX/bCTBRlTmaG9bZxa1Wu08gLllw mPwEPMkDg==; Received: from localhost ([::1] helo=merlin.infradead.org) by merlin.infradead.org with esmtp (Exim 4.92.3 #3 (Red Hat Linux)) id 1k7b6r-00078d-Ab; Mon, 17 Aug 2020 09:07:17 +0000 Received: from foss.arm.com ([217.140.110.172]) by merlin.infradead.org with esmtp (Exim 4.92.3 #3 (Red Hat Linux)) id 1k7afI-0000uT-SY; Mon, 17 Aug 2020 08:38:52 +0000 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 94C251045; Mon, 17 Aug 2020 01:38:47 -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 03EEC3F6CF; Mon, 17 Aug 2020 01:38:44 -0700 (PDT) Subject: Re: [PATCH v3 3/3] 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 , Daniel Kiss , Laurent Dufour References: <1597472419-32314-1-git-send-email-chinwen.chang@mediatek.com> <1597472419-32314-4-git-send-email-chinwen.chang@mediatek.com> From: Steven Price Message-ID: Date: Mon, 17 Aug 2020 09:38:43 +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: <1597472419-32314-4-git-send-email-chinwen.chang@mediatek.com> Content-Language: en-GB X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20200817_043849_635756_E96F430F X-CRM114-Status: GOOD ( 33.69 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: linux-fsdevel@vger.kernel.org, linux-mediatek@lists.infradead.org, linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, wsd_upstream@mediatek.com Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset="us-ascii"; Format="flowed" Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org On 15/08/2020 07:20, 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. > > Change since v1: > - If current VMA is freed after dropping the lock, it will return > - incomplete result. To fix this issue, refine the code flow as > - suggested by Steve. [1] > > Change since v2: > - When getting back the mmap lock, the address where you stopped last > - time could now be in the middle of a vma. Add one more check to handle > - this case as suggested by Michel. [2] > > [1] https://lore.kernel.org/lkml/bf40676e-b14b-44cd-75ce-419c70194783@arm.com/ > [2] https://lore.kernel.org/lkml/CANN689FtCsC71cjAjs0GPspOhgo_HRj+diWsoU1wr98YPktgWg@mail.gmail.com/ > > Signed-off-by: Chinwen Chang > CC: Steven Price > CC: Michel Lespinasse Reviewed-by: Steven Price > --- > fs/proc/task_mmu.c | 73 +++++++++++++++++++++++++++++++++++++++++++++++++++--- > 1 file changed, 70 insertions(+), 3 deletions(-) > > diff --git a/fs/proc/task_mmu.c b/fs/proc/task_mmu.c > index 76e623a..945904e 100644 > --- a/fs/proc/task_mmu.c > +++ b/fs/proc/task_mmu.c > @@ -846,7 +846,7 @@ static int show_smaps_rollup(struct seq_file *m, void *v) > struct mem_size_stats mss; > struct mm_struct *mm; > struct vm_area_struct *vma; > - unsigned long last_vma_end = 0; > + unsigned long last_vma_end = 0, last_stopped = 0; > int ret = 0; > > priv->task = get_proc_task(priv->inode); > @@ -867,9 +867,76 @@ static int show_smaps_rollup(struct seq_file *m, void *v) > > hold_task_mempolicy(priv); > > - for (vma = priv->mm->mmap; vma; vma = vma->vm_next) { > - smap_gather_stats(vma, &mss, 0); > + for (vma = priv->mm->mmap; vma;) { > + smap_gather_stats(vma, &mss, last_stopped); > + last_stopped = 0; > 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; > + } > + > + /* > + * After dropping the lock, there are four cases to > + * consider. See the following example for explanation. > + * > + * +------+------+-----------+ > + * | VMA1 | VMA2 | VMA3 | > + * +------+------+-----------+ > + * | | | | > + * 4k 8k 16k 400k > + * > + * Suppose we drop the lock after reading VMA2 due to > + * contention, then we get: > + * > + * last_vma_end = 16k > + * > + * 1) VMA2 is freed, but VMA3 exists: > + * > + * find_vma(mm, 16k - 1) will return VMA3. > + * In this case, just continue from VMA3. > + * > + * 2) VMA2 still exists: > + * > + * find_vma(mm, 16k - 1) will return VMA2. > + * Iterate the loop like the original one. > + * > + * 3) No more VMAs can be found: > + * > + * find_vma(mm, 16k - 1) will return NULL. > + * No more things to do, just break. > + * > + * 4) (last_vma_end - 1) is the middle of a vma (VMA'): > + * > + * find_vma(mm, 16k - 1) will return VMA' whose range > + * contains last_vma_end. > + * Iterate VMA' from last_vma_end. > + */ > + vma = find_vma(mm, last_vma_end - 1); > + /* Case 3 above */ > + if (!vma) > + break; > + > + /* Case 1 above */ > + if (vma->vm_start >= last_vma_end) > + continue; > + > + /* Case 4 above */ > + if (vma->vm_end > last_vma_end) { > + last_stopped = last_vma_end; > + continue; Note that instead of having last_stopped, you could replace the above with a direct call: smap_gather_stats(vma, &mss, last_vma_end); I'm not sure which is cleaner though. last_stopped is a bit messy (it's easily confused with last_vma_end), but having just the one call site for smap_gather_stats() is nice too. Steve > + } > + } > + /* Case 2 above */ > + vma = vma->vm_next; > } > > show_vma_header_prefix(m, priv->mm->mmap->vm_start, > _______________________________________________ linux-arm-kernel mailing list linux-arm-kernel@lists.infradead.org http://lists.infradead.org/mailman/listinfo/linux-arm-kernel