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 00FB0C4332F for ; Tue, 18 Oct 2022 06:25:41 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 5DCD16B0072; Tue, 18 Oct 2022 02:25:41 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 565636B0075; Tue, 18 Oct 2022 02:25:41 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 406676B0078; Tue, 18 Oct 2022 02:25:41 -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 2A3096B0072 for ; Tue, 18 Oct 2022 02:25:41 -0400 (EDT) Received: from smtpin21.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id F1743120E73 for ; Tue, 18 Oct 2022 06:25:40 +0000 (UTC) X-FDA: 80033084040.21.64CD047 Received: from szxga01-in.huawei.com (szxga01-in.huawei.com [45.249.212.187]) by imf15.hostedemail.com (Postfix) with ESMTP id DCEB4A0031 for ; Tue, 18 Oct 2022 06:25:38 +0000 (UTC) Received: from canpemm500002.china.huawei.com (unknown [172.30.72.54]) by szxga01-in.huawei.com (SkyGuard) with ESMTP id 4Ms3gC1T7dzpVgf; Tue, 18 Oct 2022 14:22:15 +0800 (CST) Received: from [10.174.151.185] (10.174.151.185) by canpemm500002.china.huawei.com (7.192.104.244) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.31; Tue, 18 Oct 2022 14:25:31 +0800 Subject: Re: [PATCH 1/3] hugetlb: fix vma lock handling during split vma and range unmapping To: Mike Kravetz CC: , , Muchun Song , David Hildenbrand , Sven Schnelle , Michal Hocko , Peter Xu , Naoya Horiguchi , "Aneesh Kumar K . V" , Andrea Arcangeli , "Kirill A . Shutemov" , Davidlohr Bueso , Prakash Sangappa , James Houghton , Mina Almasry , Pasha Tatashin , Axel Rasmussen , Ray Fucillo , Andrew Morton References: <20221005011707.514612-1-mike.kravetz@oracle.com> <20221005011707.514612-2-mike.kravetz@oracle.com> <5154292a-4c55-28cd-0935-82441e512fc3@huawei.com> From: Miaohe Lin Message-ID: Date: Tue, 18 Oct 2022 14:25:31 +0800 User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:78.0) Gecko/20100101 Thunderbird/78.6.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset="utf-8" Content-Language: en-US Content-Transfer-Encoding: 7bit X-Originating-IP: [10.174.151.185] X-ClientProxiedBy: dggems704-chm.china.huawei.com (10.3.19.181) To canpemm500002.china.huawei.com (7.192.104.244) X-CFilter-Loop: Reflected ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1666074340; a=rsa-sha256; cv=none; b=nAD2iLhVTkAhWPKMFyYP/HpONmJggKEEfUmcexGicafDJgT9sB0nWpVF4hPtov+R3SQctW YjEIFNud8PHWzYSoPaMIdQhd7lPnNPciWvDEbSAXpS5uWuwFcwKE6Xoodbg2YVHyATrEoX jZuasknjqihpv/QAwCZTL/XuF0eMD6M= ARC-Authentication-Results: i=1; imf15.hostedemail.com; dkim=none; dmarc=pass (policy=quarantine) header.from=huawei.com; spf=pass (imf15.hostedemail.com: domain of linmiaohe@huawei.com designates 45.249.212.187 as permitted sender) smtp.mailfrom=linmiaohe@huawei.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1666074340; 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=3f40zJIDTnnku8Q0Zs0YHIT4NBBTWfAtewXQcy8SRaQ=; b=d622rv+b/4YciDxTAq9Bi5pOSd3Y1kRposKou8P7n+GCNCyJhh0Q13jUq6AQRcDCaHUnOQ 7q2VfHqCrBFTlJIXua7+Qoe3cMifVE1hebO4E9JZeZC2G65VZwQv3JX959HkumXny2nq8M x8OHDFMtb3/zffczzF7Q8UdFKvG+Iv0= X-Rspam-User: Authentication-Results: imf15.hostedemail.com; dkim=none; dmarc=pass (policy=quarantine) header.from=huawei.com; spf=pass (imf15.hostedemail.com: domain of linmiaohe@huawei.com designates 45.249.212.187 as permitted sender) smtp.mailfrom=linmiaohe@huawei.com X-Stat-Signature: 7d1iy7yi178dqrmpiqtgmg1a716pj1po X-Rspamd-Queue-Id: DCEB4A0031 X-Rspamd-Server: rspam10 X-HE-Tag: 1666074338-77393 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/10/18 10:56, Mike Kravetz wrote: > On 10/15/22 09:25, Miaohe Lin wrote: >> Sorry for late respond. It's a really busy week. :) >> >> On 2022/10/5 9:17, Mike Kravetz wrote: >>> The hugetlb vma lock hangs off the vm_private_data field and is specific >>> to the vma. When vm_area_dup() is called as part of vma splitting, the >> >> Oh, I checked vm_area_dup() from callsite of copy_vma and dup_mmap but split_vma >> is missed... And yes, vma splitting can occur but vma merging won't for hugetlb >> vma. Thanks for catching this, Mike. >> >>> vma lock pointer is copied to the new vma. This will result in issues >>> such as double freeing of the structure. Update the hugetlb open vm_ops >>> to allocate a new vma lock for the new vma. >>> >>> The routine __unmap_hugepage_range_final unconditionally unset >>> VM_MAYSHARE to prevent subsequent pmd sharing. hugetlb_vma_lock_free >>> attempted to anticipate this by checking both VM_MAYSHARE and VM_SHARED. >>> However, if only VM_MAYSHARE was set we would miss the free. With the >>> introduction of the vma lock, a vma can not participate in pmd sharing >>> if vm_private_data is NULL. Instead of clearing VM_MAYSHARE in >>> __unmap_hugepage_range_final, free the vma lock to prevent sharing. Also, >>> update the sharing code to make sure vma lock is indeed a condition for >>> pmd sharing. hugetlb_vma_lock_free can then key off VM_MAYSHARE and not >>> miss any vmas. >>> >>> Fixes: "hugetlb: add vma based lock for pmd sharing" >>> Signed-off-by: Mike Kravetz >>> --- >>> mm/hugetlb.c | 43 +++++++++++++++++++++++++++---------------- >>> mm/memory.c | 4 ---- >>> 2 files changed, 27 insertions(+), 20 deletions(-) >>> >>> diff --git a/mm/hugetlb.c b/mm/hugetlb.c >>> index 4443e87e814b..0129d371800c 100644 >>> --- a/mm/hugetlb.c >>> +++ b/mm/hugetlb.c >>> @@ -4612,7 +4612,14 @@ static void hugetlb_vm_op_open(struct vm_area_struct *vma) >>> kref_get(&resv->refs); >>> } >>> >>> - hugetlb_vma_lock_alloc(vma); >>> + /* >>> + * vma_lock structure for sharable mappings is vma specific. >>> + * Clear old pointer (if copied via vm_area_dup) and create new. >>> + */ >>> + if (vma->vm_flags & VM_MAYSHARE) { >>> + vma->vm_private_data = NULL; >>> + hugetlb_vma_lock_alloc(vma); >>> + } >> >> IMHO this would lead to memoryleak. Think about the below move_vma() flow: >> move_vma >> copy_vma >> new_vma = vm_area_dup(vma); >> new_vma->vm_ops->open(new_vma); --> new_vma has its own vma lock. >> is_vm_hugetlb_page(vma) >> clear_vma_resv_huge_pages >> hugetlb_dup_vma_private --> vma->vm_private_data is set to NULL >> without put ref. So vma lock is *leaked*? > > You are right, that could lead to a leak. > > I have an idea about setting vma->vm_private_data to NULL for VM_MAYSHARE > vmas in routines like hugetlb_dup_vma_private(). We can check > hugetlb_vma_lock->vma and only set to NULL if, > > vma->(hugetlb_vma_lock)vma->vm_private_data->vma != vma Looks feasible. Thanks for your work, Mike. Thanks, Miaohe Lin > > Got sidetracked chasing down another leak today. Will send a patch > implementing this idea soon. > > Thanks for looking at this! >