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 Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id B3737C433EF for ; Wed, 27 Apr 2022 06:34:30 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 145236B0073; Wed, 27 Apr 2022 02:34:30 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 0F7186B0075; Wed, 27 Apr 2022 02:34:30 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id F26086B0078; Wed, 27 Apr 2022 02:34:29 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (relay.a.hostedemail.com [64.99.140.24]) by kanga.kvack.org (Postfix) with ESMTP id E4BFC6B0073 for ; Wed, 27 Apr 2022 02:34:29 -0400 (EDT) Received: from smtpin11.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay12.hostedemail.com (Postfix) with ESMTP id B0BA8121A9F for ; Wed, 27 Apr 2022 06:34:29 +0000 (UTC) X-FDA: 79401695058.11.02EE2AA Received: from szxga03-in.huawei.com (szxga03-in.huawei.com [45.249.212.189]) by imf02.hostedemail.com (Postfix) with ESMTP id D1F6C8005E for ; Wed, 27 Apr 2022 06:34:25 +0000 (UTC) Received: from dggpemm500022.china.huawei.com (unknown [172.30.72.53]) by szxga03-in.huawei.com (SkyGuard) with ESMTP id 4Kp84H4ttkzCsPr; Wed, 27 Apr 2022 14:29:51 +0800 (CST) Received: from [10.67.110.243] (10.67.110.243) by dggpemm500022.china.huawei.com (7.185.36.162) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.24; Wed, 27 Apr 2022 14:34:24 +0800 Message-ID: <1f5966cf-d40f-4a49-c2d0-e2c90eb5a124@huawei.com> Date: Wed, 27 Apr 2022 14:34:23 +0800 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:91.0) Gecko/20100101 Thunderbird/91.8.1 Subject: Re: [PATCH] mm/mprotect: reduce Committed_AS if memory protection is changed to PROT_NONE Content-Language: en-US To: Andrew Morton CC: , , , , References: <20220426112705.3323-1-liusongtang@huawei.com> <20220426133401.9404190c5a5c6ff58b694c59@linux-foundation.org> From: liusongtang In-Reply-To: <20220426133401.9404190c5a5c6ff58b694c59@linux-foundation.org> Content-Type: text/plain; charset="UTF-8"; format=flowed Content-Transfer-Encoding: 7bit X-Originating-IP: [10.67.110.243] X-ClientProxiedBy: dggems704-chm.china.huawei.com (10.3.19.181) To dggpemm500022.china.huawei.com (7.185.36.162) X-CFilter-Loop: Reflected X-Stat-Signature: aerkge313c9xtsthc3dbuhpfmzk75pge X-Rspamd-Server: rspam12 X-Rspamd-Queue-Id: D1F6C8005E Authentication-Results: imf02.hostedemail.com; dkim=none; spf=pass (imf02.hostedemail.com: domain of liusongtang@huawei.com designates 45.249.212.189 as permitted sender) smtp.mailfrom=liusongtang@huawei.com; dmarc=pass (policy=quarantine) header.from=huawei.com X-Rspam-User: X-HE-Tag: 1651041265-750567 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: On 2022/4/27 4:34, Andrew Morton wrote: > On Tue, 26 Apr 2022 19:27:05 +0800 liusongtang wrote: > >> If PROT_WRITE is set, the size of vm area will be added to Committed_AS. >> However, if memory protection is changed to PROT_NONE, >> the corresponding physical memory will not be used, but Committed_AS still >> count the size of the PROT_NONE memory. >> >> This patch reduce Committed_AS and free the corresponding memory if >> memory protection is changed to PROT_NONE. >> >> ... >> >> --- a/mm/mprotect.c >> +++ b/mm/mprotect.c >> @@ -497,6 +497,12 @@ mprotect_fixup(struct vm_area_struct *vma, struct vm_area_struct **pprev, >> } >> >> success: >> + if ((newflags & (VM_READ | VM_WRITE | VM_EXEC | VM_LOCKED | VM_ACCOUNT)) == VM_ACCOUNT) { >> + zap_page_range(vma, start, end - start); >> + newflags &= ~VM_ACCOUNT; >> + vm_unacct_memory((end - start) >> PAGE_SHIFT); >> + } >> + >> /* >> * vm_flags and vm_page_prot are protected by the mmap_lock >> * held in write mode. > Surprised. If userspace does mprotect(addr, len. PROT_NONE) then > mprotect(addr, len. PROT_READ), what is now at *addr? Zeroes? > . 1. In the case mentioned above, I think data in *addr is invalid after mprotect(addr, len. PROT_NONE), so clear it will not cause a problem. 2. Another idea is we can check if this vm area is populated before reduce Committed_AS.