From patchwork Mon Oct 5 02:40:14 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Chinwen Chang X-Patchwork-Id: 11816041 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id DEDA992C for ; Mon, 5 Oct 2020 02:40:57 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 8EFCD2078E for ; Mon, 5 Oct 2020 02:40:57 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (1024-bit key) header.d=mediatek.com header.i=@mediatek.com header.b="YD4ULh4j" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 8EFCD2078E Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=mediatek.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id C10796B0068; Sun, 4 Oct 2020 22:40:56 -0400 (EDT) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id BC0436B006C; Sun, 4 Oct 2020 22:40:56 -0400 (EDT) X-Original-To: int-list-linux-mm@kvack.org X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id AB0AE6B006E; Sun, 4 Oct 2020 22:40:56 -0400 (EDT) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0092.hostedemail.com [216.40.44.92]) by kanga.kvack.org (Postfix) with ESMTP id 760016B0068 for ; Sun, 4 Oct 2020 22:40:56 -0400 (EDT) Received: from smtpin26.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay04.hostedemail.com (Postfix) with ESMTP id F33941EE6 for ; Mon, 5 Oct 2020 02:40:55 +0000 (UTC) X-FDA: 77336319312.26.pull28_3306f48271ba Received: from filter.hostedemail.com (10.5.16.251.rfc1918.com [10.5.16.251]) by smtpin26.hostedemail.com (Postfix) with ESMTP id CA3E11804B647 for ; Mon, 5 Oct 2020 02:40:55 +0000 (UTC) X-Spam-Summary: 1,0,0,f0e0b1fb937cf2ad,d41d8cd98f00b204,chinwen.chang@mediatek.com,,RULES_HIT:41:69:355:379:541:800:960:966:967:973:988:989:1260:1277:1311:1313:1314:1345:1437:1515:1516:1518:1535:1544:1605:1711:1730:1747:1777:1792:2196:2198:2199:2200:2393:2525:2553:2559:2565:2570:2682:2685:2693:2703:2730:2859:2898:2901:2933:2937:2939:2942:2945:2947:2951:2954:3022:3865:3866:3867:3868:3870:3871:3872:3873:3934:3936:3938:3941:3944:3947:3950:3953:3956:3959:4118:4250:4321:4385:4605:5007:6119:6261:7875:7903:8531:8660:8957:9010:9025:10004:11658:13148:13161:13172:13229:13230,0,RBL:210.61.82.183:@mediatek.com:.lbl8.mailshell.net-64.100.201.201 62.2.12.100;04ygpy1b3i4yuxsitdsnwqsf736xdyp5fc5rexe3osyp5w7h8jegcq5pj8umek9.farnm54d3t5o1fn94fwe9ydtijmecg7ea3h4et5hf6i5gq6x9brmjh8mfoddrdb.r-lbl8.mailshell.net-223.238.255.100,CacheIP:none,Bayesian:0.5,0.5,0.5,Netcheck:none,DomainCache:0,MSF:not bulk,SPF:fp,MSBL:0,DNSBL:neutral,Custom_rules:0:0:0,LFtime:23,LUA_SUMMARY:none X-HE-Tag: pull28_3306f48271ba X-Filterd-Recvd-Size: 7691 Received: from mailgw01.mediatek.com (unknown [210.61.82.183]) by imf13.hostedemail.com (Postfix) with ESMTP for ; Mon, 5 Oct 2020 02:40:54 +0000 (UTC) X-UUID: 1ac8d4c9d4a4466c9d72dde7ac8ccaf5-20201005 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=mediatek.com; s=dk; h=Content-Transfer-Encoding:Content-Type:MIME-Version:References:In-Reply-To:Message-ID:Date:Subject:CC:To:From; bh=EUI7uOkc6wflbcN4dLdDFLe1TAyO6/KD9JziaxsXixA=; b=YD4ULh4jHW2Np/msBKmiLJBTH2iiTAfYPXFdSQ8FQloyGk6XrupFx1KDuLTr1AGwkQZvJSPMTNEnSscLIhAspzCMXYqb8RuuegRlHeHAI70i2pEpdeVxJ/1+XtW7vm6tlame/a3iBrbRz7P/kltJxZwlXw3L9iK1yaYnj09k2Ko=; X-UUID: 1ac8d4c9d4a4466c9d72dde7ac8ccaf5-20201005 Received: from mtkcas08.mediatek.inc [(172.21.101.126)] by mailgw01.mediatek.com (envelope-from ) (Cellopoint E-mail Firewall v4.1.14 Build 0819 with TLSv1.2 ECDHE-RSA-AES256-SHA384 256/256) with ESMTP id 46171417; Mon, 05 Oct 2020 10:40:50 +0800 Received: from MTKCAS06.mediatek.inc (172.21.101.30) by mtkmbs02n1.mediatek.inc (172.21.101.77) with Microsoft SMTP Server (TLS) id 15.0.1497.2; Mon, 5 Oct 2020 10:40:44 +0800 Received: from mtkswgap22.mediatek.inc (172.21.77.33) by MTKCAS06.mediatek.inc (172.21.101.73) with Microsoft SMTP Server id 15.0.1497.2 via Frontend Transport; Mon, 5 Oct 2020 10:40:44 +0800 From: Chinwen Chang To: Andrew Morton CC: , , Chinwen Chang , Michel Lespinasse Subject: [RESEND, PATCH v4 3/3] mm: proc: smaps_rollup: do not stall write attempts on mmap_lock Date: Mon, 5 Oct 2020 10:40:14 +0800 Message-ID: <1601865614-4918-4-git-send-email-chinwen.chang@mediatek.com> X-Mailer: git-send-email 1.9.1 In-Reply-To: <1601865614-4918-1-git-send-email-chinwen.chang@mediatek.com> References: <1601865614-4918-1-git-send-email-chinwen.chang@mediatek.com> MIME-Version: 1.0 X-MTK: N X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: 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/ Change-Id: Idcdb6478ccd06a9e5edd4eda9285378e961a6b94 Signed-off-by: Chinwen Chang Reviewed-by: Steven Price CC: Michel Lespinasse --- 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,