From patchwork Wed Jul 19 07:51:11 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Kefeng Wang X-Patchwork-Id: 13318465 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id ECA06C04E69 for ; Wed, 19 Jul 2023 07:40:14 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 70823280035; Wed, 19 Jul 2023 03:40:12 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 691358D004B; Wed, 19 Jul 2023 03:40:12 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 494C9280035; Wed, 19 Jul 2023 03:40:12 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id 379458D004B for ; Wed, 19 Jul 2023 03:40:12 -0400 (EDT) Received: from smtpin24.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 0A53DA0002 for ; Wed, 19 Jul 2023 07:40:12 +0000 (UTC) X-FDA: 81027563064.24.270BAAB Received: from szxga08-in.huawei.com (szxga08-in.huawei.com [45.249.212.255]) by imf12.hostedemail.com (Postfix) with ESMTP id 4A4E440013 for ; Wed, 19 Jul 2023 07:40:07 +0000 (UTC) Authentication-Results: imf12.hostedemail.com; dkim=none; spf=pass (imf12.hostedemail.com: domain of wangkefeng.wang@huawei.com designates 45.249.212.255 as permitted sender) smtp.mailfrom=wangkefeng.wang@huawei.com; dmarc=pass (policy=quarantine) header.from=huawei.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1689752409; a=rsa-sha256; cv=none; b=dvnSPsMCK2MvHhYX1DUDWk1WdDOReyleQbwXsZdZCq1sueEv/uvHTb0v8FyhHj70e4IIMb GMTeZa3Y3KUmFkXgBtUBcFN/6UGOvX596THlqTYlAHiRwoJPYfVsnCxutpwace9BP4Rrl2 AtS9m+F7X9AbpFTHJvbeEId+8ZgNObw= ARC-Authentication-Results: i=1; imf12.hostedemail.com; dkim=none; spf=pass (imf12.hostedemail.com: domain of wangkefeng.wang@huawei.com designates 45.249.212.255 as permitted sender) smtp.mailfrom=wangkefeng.wang@huawei.com; dmarc=pass (policy=quarantine) header.from=huawei.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1689752409; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=MPN5dJ7jQjJof++e0GOGPjqr9j7WBPBqmO8MjvBjRlI=; b=XGOQQ3AUlN5nPOhm1u2bjspqk/qpmt+jfQ+UiqgCHdUc0fgdM/GEHXjC12Ak+VGZoLiTht HQkFFeeAJb1pjJ4Xn/6LivT322w6tRfL2NU5946auWuYC4/Q4fpkNVtXUWW4JUWbWvyyik NOVMFEPH0/aaTkcMnfKFYPlQLF/PfXo= Received: from dggpemm500001.china.huawei.com (unknown [172.30.72.57]) by szxga08-in.huawei.com (SkyGuard) with ESMTP id 4R5SPg5zCWz18Ljp; Wed, 19 Jul 2023 15:39:19 +0800 (CST) Received: from localhost.localdomain.localdomain (10.175.113.25) by dggpemm500001.china.huawei.com (7.185.36.107) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.27; Wed, 19 Jul 2023 15:40:02 +0800 From: Kefeng Wang To: Andrew Morton CC: , , , , , , , Kefeng Wang , =?utf-8?q?Christian_G=C3=B6ttsche?= , David Hildenbrand Subject: [PATCH v2 1/4] mm: factor out VMA stack and heap checks Date: Wed, 19 Jul 2023 15:51:11 +0800 Message-ID: <20230719075127.47736-2-wangkefeng.wang@huawei.com> X-Mailer: git-send-email 2.41.0 In-Reply-To: <20230719075127.47736-1-wangkefeng.wang@huawei.com> References: <20230719075127.47736-1-wangkefeng.wang@huawei.com> MIME-Version: 1.0 X-Originating-IP: [10.175.113.25] X-ClientProxiedBy: dggems706-chm.china.huawei.com (10.3.19.183) To dggpemm500001.china.huawei.com (7.185.36.107) X-CFilter-Loop: Reflected X-Rspam-User: X-Rspamd-Server: rspam06 X-Rspamd-Queue-Id: 4A4E440013 X-Stat-Signature: m9kk7zbpm36fqnypx914r3hgxzd1uj1q X-HE-Tag: 1689752407-288989 X-HE-Meta: U2FsdGVkX1/VCpxbu0A/orwIb4gT6cqQlqi4iBsb6hRZtk3UNZINCA800tKDk2Zb17b1KMhF0QctjTsagzOT+D/mtA2l5jxRFrN4z9jo64RW4TudsOqvdrMgUuxv/SYDk4ezTPQEaaX7plrWMKHlX3lDVZEgA/dgEGIEw1MtGxhE2K7cIJM4AdUOmr/bc+w46ILcDBt7KG2dUMSuvqaAn2JMYgYcELYHkgtJNdzIYhRlsssQTvq2wud/Mj3lGd6u/aJM2WFOVf66ClYjS9O76wR4XqEojulEqNy5O9DEKc4X8E7vO4h8XZnXjSiH8F4SW2mhL55c0l+FsFvTt74+qFx3hK+IJnbVrl2GDriumvt2Q5PflmEXrDXu0CCUB4nwWbEjIPNPGYpvH9ygG1jqKwrFWPqbul4BgBVpM+HSK55xeUbb9mhR1b7jwSfRfN9EyD716yS0xCPL/nFw5J0P+NqoGp1iATyinHEEvxFkDGXB0YzJdITrtwBJMj6bssToR1BSTEbM2P/T6DIeSQGb9HBzY7iKgSoktxvnG0jIa6owue9kLaWajf3t0OmUqYYHQcFa5XcXg/h8hniibdQW5b0a5mE/P3yIiFOxleBeHwaPno2xmlv50r/autCvnxvrNoEocJfcX7PuybcwA2YojoUw1vr9RP4hIdaL7LLN/sVJdjBRDx/Kvwd9lr6amIpfNC9z+YWzR9X6pcrvaH7vTUyRw5mXRDI0gT65FctPdkuBBUl/BZhThkpGC5dIk1NwLLrQ1gLhzYV28r5gnhyANWY9TDG/Nhjeyiq0tpx20C+aC+rxDpK9bk+bXjLGaLUxPbX9I26YHwXsaCOT3v1QXeLP4sefL6jvyA/9CWa5/JJyLkI11CPFKNXS258DfhBO3W8Y95y3gqIoyBKaxSnQyP7dFTvtQsFom9iNTBhoK8UlZXxXGxw27I7QWfYkCSP8tq+TdClLGMPQYKiE7yy GH2TqiUh hVz7aMhjJqtrW8wIsVCEYJX4cbbi0QWH9mU5BSIGl5mgCneECfXwMv2CkrpZLFPWm3dklKNw+1N4V18BWvBD1mxVbil3LNtB9RkFgAsM7zE8NLsN27VGYTVu/vni+ox9Z6/jtVDXgvEkOqAwfgVBkXBMAT5HpM73NvymWwbhmilcohL8cUV2iC906KZOJ85AzE04OkNx6qMZgsUVovDsx3IeB/uQtqly5VFlIrhOU47nC1Z8= 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: Factor out VMA stack and heap checks and name them vma_is_initial_stack() and vma_is_initial_heap() for general use. Cc: Christian Göttsche Cc: David Hildenbrand Signed-off-by: Kefeng Wang Reviewed-by: David Hildenbrand --- fs/proc/task_mmu.c | 24 ++++-------------------- fs/proc/task_nommu.c | 15 +-------------- include/linux/mm.h | 21 +++++++++++++++++++++ 3 files changed, 26 insertions(+), 34 deletions(-) diff --git a/fs/proc/task_mmu.c b/fs/proc/task_mmu.c index 507cd4e59d07..bf25178ae66a 100644 --- a/fs/proc/task_mmu.c +++ b/fs/proc/task_mmu.c @@ -236,21 +236,6 @@ static int do_maps_open(struct inode *inode, struct file *file, sizeof(struct proc_maps_private)); } -/* - * Indicate if the VMA is a stack for the given task; for - * /proc/PID/maps that is the stack of the main task. - */ -static int is_stack(struct vm_area_struct *vma) -{ - /* - * We make no effort to guess what a given thread considers to be - * its "stack". It's not even well-defined for programs written - * languages like Go. - */ - return vma->vm_start <= vma->vm_mm->start_stack && - vma->vm_end >= vma->vm_mm->start_stack; -} - static void show_vma_header_prefix(struct seq_file *m, unsigned long start, unsigned long end, vm_flags_t flags, unsigned long long pgoff, @@ -327,13 +312,12 @@ show_map_vma(struct seq_file *m, struct vm_area_struct *vma) goto done; } - if (vma->vm_start <= mm->brk && - vma->vm_end >= mm->start_brk) { + if (vma_is_initial_heap(vma)) { name = "[heap]"; goto done; } - if (is_stack(vma)) { + if (vma_is_initial_stack(vma)) { name = "[stack]"; goto done; } @@ -1971,9 +1955,9 @@ static int show_numa_map(struct seq_file *m, void *v) if (file) { seq_puts(m, " file="); seq_file_path(m, file, "\n\t= "); - } else if (vma->vm_start <= mm->brk && vma->vm_end >= mm->start_brk) { + } else if (vma_is_initial_heap(vma)) { seq_puts(m, " heap"); - } else if (is_stack(vma)) { + } else if (vma_is_initial_stack(vma)) { seq_puts(m, " stack"); } diff --git a/fs/proc/task_nommu.c b/fs/proc/task_nommu.c index 2c8b62265981..a8ac0dd8041e 100644 --- a/fs/proc/task_nommu.c +++ b/fs/proc/task_nommu.c @@ -121,19 +121,6 @@ unsigned long task_statm(struct mm_struct *mm, return size; } -static int is_stack(struct vm_area_struct *vma) -{ - struct mm_struct *mm = vma->vm_mm; - - /* - * We make no effort to guess what a given thread considers to be - * its "stack". It's not even well-defined for programs written - * languages like Go. - */ - return vma->vm_start <= mm->start_stack && - vma->vm_end >= mm->start_stack; -} - /* * display a single VMA to a sequenced file */ @@ -171,7 +158,7 @@ static int nommu_vma_show(struct seq_file *m, struct vm_area_struct *vma) if (file) { seq_pad(m, ' '); seq_file_path(m, file, ""); - } else if (mm && is_stack(vma)) { + } else if (mm && vma_is_initial_stack(vma)) { seq_pad(m, ' '); seq_puts(m, "[stack]"); } diff --git a/include/linux/mm.h b/include/linux/mm.h index 2dd73e4f3d8e..51f8c573db74 100644 --- a/include/linux/mm.h +++ b/include/linux/mm.h @@ -822,6 +822,27 @@ static inline bool vma_is_anonymous(struct vm_area_struct *vma) return !vma->vm_ops; } +static inline bool vma_is_initial_heap(const struct vm_area_struct *vma) +{ + return vma->vm_start <= vma->vm_mm->brk && + vma->vm_end >= vma->vm_mm->start_brk; +} + +/* + * Indicate if the VMA is a stack for the given task; for + * /proc/PID/maps that is the stack of the main task. + */ +static inline bool vma_is_initial_stack(const struct vm_area_struct *vma) +{ + /* + * We make no effort to guess what a given thread considers to be + * its "stack". It's not even well-defined for programs written + * languages like Go. + */ + return vma->vm_start <= vma->vm_mm->start_stack && + vma->vm_end >= vma->vm_mm->start_stack; +} + static inline bool vma_is_temporary_stack(struct vm_area_struct *vma) { int maybe_stack = vma->vm_flags & (VM_GROWSDOWN | VM_GROWSUP);