Received: by 2002:a05:6a10:a0d1:0:0:0:0 with SMTP id j17csp280858pxa; Wed, 19 Aug 2020 00:41:00 -0700 (PDT) X-Google-Smtp-Source: ABdhPJx1dqpwKNNImQEZB4XnxoiV7fgCa7u+JGpSTMiAYCEsM5W/19XGTxN+uoHZsqwKcc3xGbpd X-Received: by 2002:a17:906:fa0b:: with SMTP id lo11mr24827431ejb.235.1597822860607; Wed, 19 Aug 2020 00:41:00 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1597822860; cv=none; d=google.com; s=arc-20160816; b=SozvsBbwwD4XcmBc9AUPdSNQ/asLx6rbyb3wAPMk4BJCrCIMXht81vbnbBgRD3jO6g qJur4V9/40ffJ61rBODQ3EJ0Y7xvcu85/SnovCmb2lopsAMNHvd5sZz3K7r4dKaTb1uS DrELL9dYbr04psGG5C/3lH8cp/yRzNVL2UhJ4ePr4YZeCkHV3ugo+PVpKAlXaolE5P// Skbm2nwkk7XH9O+I/x4Fc5kRdFd6FhAD/cxjAmQOWFj4e8+m31UEjq7Yz6v4TqFm2bfj TGmFNBXytV/nOa6J7FyPo6q+XQC/wKbfWWY6E1/cSnnvFbt7dKbkpLYymuXAJza9MkI2 No0Q== 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=P11s+85cD7vhLXmm9baf40EVeXddJGmo/kS+x9CIfbI=; b=mfjnjFyYDAPoQxgLFnB6C2VDr6r7/qE8Tk/GOVETbuxsGBU5CvleUmbNhkeOJXV4r6 2zWNg/ZLcSBeorYP7itSNqJpbU2bARC7LSi+cmm1fCf3QeMGQgiyTfJeydaXDLroUVDI iN19zfVFpddAvWPxR2PmOgqUH9SPverOasmpJwwKVooLGYdF68+PGf4H1IRYl6yb5Qtb FD/u+zvVzxkc8Tcd2VuNJTwpgODnO8IS1JVEYCnFqEvOJJreOM/6UXbTLap9xd5uHX+7 sDTxcEN7dgDSgjTexPbA1Wf5po2CPydprro6Op2GICU/DcanegJk2W3NI6Eox6rRJQq9 9wcw== 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 o29si15734877edz.62.2020.08.19.00.40.37; Wed, 19 Aug 2020 00:41:00 -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 S1726980AbgHSHhh (ORCPT + 99 others); Wed, 19 Aug 2020 03:37:37 -0400 Received: from foss.arm.com ([217.140.110.172]:57246 "EHLO foss.arm.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726773AbgHSHhh (ORCPT ); Wed, 19 Aug 2020 03:37:37 -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 E98591FB; Wed, 19 Aug 2020 00:37:35 -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 676763F6CF; Wed, 19 Aug 2020 00:37:33 -0700 (PDT) Subject: Re: [PATCH v4 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 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: <1597715898-3854-1-git-send-email-chinwen.chang@mediatek.com> <1597715898-3854-4-git-send-email-chinwen.chang@mediatek.com> From: Steven Price Message-ID: <66d5de29-933d-347b-8922-3238b62bc7f5@arm.com> Date: Wed, 19 Aug 2020 08:37:28 +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: <1597715898-3854-4-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 18/08/2020 02:58, 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] > > Change since v3: > - last_stopped is easily confused with last_vma_end. Replace it with > - a direct call to smap_gather_stats(vma, &mss, last_vma_end) as > - suggested by Steve. [3] > > [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/ > [3] https://lore.kernel.org/lkml/db0d40e2-72f3-09d5-c162-9c49218f128f@arm.com/ > > Signed-off-by: Chinwen Chang > CC: Steven Price > CC: Michel Lespinasse Reviewed-by: Steven Price > --- > fs/proc/task_mmu.c | 66 +++++++++++++++++++++++++++++++++++++++++++++++++++++- > 1 file changed, 65 insertions(+), 1 deletion(-) > > diff --git a/fs/proc/task_mmu.c b/fs/proc/task_mmu.c > index 76e623a..1a80624 100644 > --- a/fs/proc/task_mmu.c > +++ b/fs/proc/task_mmu.c > @@ -867,9 +867,73 @@ 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) { > + for (vma = priv->mm->mmap; vma;) { > smap_gather_stats(vma, &mss, 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) > + smap_gather_stats(vma, &mss, last_vma_end); > + } > + /* Case 2 above */ > + vma = vma->vm_next; > } > > show_vma_header_prefix(m, priv->mm->mmap->vm_start, >