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 1B437CE79A1 for ; Mon, 25 Sep 2023 20:31:35 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id A2E348D0037; Mon, 25 Sep 2023 16:31:34 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 9DDF28D0001; Mon, 25 Sep 2023 16:31:34 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 8A5D78D0037; Mon, 25 Sep 2023 16:31:34 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id 79D058D0001 for ; Mon, 25 Sep 2023 16:31:34 -0400 (EDT) Received: from smtpin29.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id 4CDE41A0D2C for ; Mon, 25 Sep 2023 20:31:34 +0000 (UTC) X-FDA: 81276265308.29.5F8FBE2 Received: from shelob.surriel.com (shelob.surriel.com [96.67.55.147]) by imf06.hostedemail.com (Postfix) with ESMTP id A5C9018002D for ; Mon, 25 Sep 2023 20:31:32 +0000 (UTC) Authentication-Results: imf06.hostedemail.com; dkim=none; dmarc=none; spf=none (imf06.hostedemail.com: domain of riel@shelob.surriel.com has no SPF policy when checking 96.67.55.147) smtp.mailfrom=riel@shelob.surriel.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1695673892; h=from:from:sender:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=XaxFx04wusLir1Joa7skEBW7U6YyXkbQvM7BpW0L4Rw=; b=OP7p3ixkkubKJox05381k7UuDjKHw7VZQW8yTPOa5QXepjzQXyMij7URRPVGh8fMCWepkH CE/ma5C30OVmZQArPFLixuyy4zE3zqYKaO/sgD6XSvyx1u4JYq8E3aXul9DYPJgxfntwgM /HbarpeIAstrBU+zg7avzxSLbJbtq9o= ARC-Authentication-Results: i=1; imf06.hostedemail.com; dkim=none; dmarc=none; spf=none (imf06.hostedemail.com: domain of riel@shelob.surriel.com has no SPF policy when checking 96.67.55.147) smtp.mailfrom=riel@shelob.surriel.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1695673892; a=rsa-sha256; cv=none; b=Y1yjn9+UX1SToEKZIH8fYRSQD2TPdMDkCxRXfQ/QyhbIdmbXaZerorIqfaF+sW9e+baWt9 RALX53rdvl2TLM9xEABHKixtGy5a36WMUzT+HRGrIChEiXQet+A9o7arpr68wDnZ7QKZHm LSv0u/F8NufG8G+ag5OyYcESfUyc4o0= Received: from imladris.home.surriel.com ([10.0.13.28] helo=imladris.surriel.com) by shelob.surriel.com with esmtpsa (TLS1.2) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1qksDy-0006Gb-0J; Mon, 25 Sep 2023 16:30:34 -0400 From: riel@surriel.com To: linux-kernel@vger.kernel.org Cc: kernel-team@meta.com, linux-mm@kvack.org, akpm@linux-foundation.org, muchun.song@linux.dev, mike.kravetz@oracle.com, leit@meta.com, willy@infradead.org, Rik van Riel Subject: [PATCH 1/3] hugetlbfs: extend hugetlb_vma_lock to private VMAs Date: Mon, 25 Sep 2023 16:28:50 -0400 Message-ID: <20230925203030.703439-2-riel@surriel.com> X-Mailer: git-send-email 2.41.0 In-Reply-To: <20230925203030.703439-1-riel@surriel.com> References: <20230925203030.703439-1-riel@surriel.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Rspam-User: X-Stat-Signature: 8qi3no5r3ctnfidbk1maahhqz3djiqju X-Rspamd-Server: rspam07 X-Rspamd-Queue-Id: A5C9018002D X-HE-Tag: 1695673892-538569 X-HE-Meta: U2FsdGVkX19mijv//skpqXNV2IH3E0Xu/4b3M2+GdXiIPZ2m3WtIlpeR+Sjzz0mMmXJJSLj4RALI5NHjHx1SKgGM2AckKXWzagOpDuP2mSLX1aABoi7Q/4M3/+9YL81CQWX0N/so221tzEBEJcTWopB5inN7ELvBjwhAppG8sT2yGTcrdERSLn8qIhBuo32uIQn6iR1pRufRWpwU0OdQzLfTxLJ7fLXlXq76I44WfcI7MLtkFvJUXKfmTngXR8RrVDokVKAxorTFs2m75tbiQwfVTnYcHGJSnB6jiJVs384C1xorWlfk0q7zIioFYgysHbyKHC3PHsBbzUzZwYTAsPsag7Y/QpwCisqY+VlzzPueyR0v1apz2Zyail7edMBYwvB1whNfrrnNpPLVkGNKPNFR5W2+WBxi45K3KjgUQ/Hv3jXHOEOICan0y33bDI+9aNIPu8JUFJbLjucsBFhGsGXEtFl/AkXwt548qNlaKK+sgch/PPO+83VUKP3fSCNI+HW+jPFK2xb+q/+cQgPymUzghQolXln1ryfFJDIm/xHRyK6wgSZ39JqZIfVSyEsin51RJm6tLRbjh3It2hO98DF/AcFVxYMlor5DhlduHYGrfMIfdxdbGe7zJHsRHToFlx/2lgRCyEiTqqU9Fj6ZviSACCdtaQNa3GKnM7fylepeO0FeElbOul/C4BSH6BBaWhL8wyDQKRD2I8QCQwkcwcMWfnIiDQxhnoSAoCpRV5qtWyMZbrV3QOlSirsR7ANVJbGnnpxKgi/Q0tNpoKfIJjOuEd5nq85KwfwXpPmmMOhcPlSoWQ7oV8kEmCQDM7SxqniTNDklOEr1gUnFqgagdjo0y4bEuV/AizRKcpQrsFhqTewuw0MWurp0y4f01IXyBQlHO9fqxZxKiygu8BFeYWX3GnWvmb453zJOBF3dztwl9uNa7ROUPWCwmzuAhwXy7cXoW4CQ7fH1eRciADF DZefhBeC CoK2tyjK6PLv/6gQEGcBqERuTHNiJWvunatzcaf0/4J6ZKzyRtqoZszQz3dPmMgzIdgo/6GkLYIVlplC/VaAvoBxd0hnI1NX44rqtnvu5dF/rWoj3EzAfLRkUdhNOcKhULMA0BTRe/FHjnlUX45NT+gEfD1hSi04AFDbKH08fHrlL920Ehz3kD27qx+y/TWPldYQB4rkWfsqkXSBsxiLA2dERs7gKauAi0jmmXyv3xWQJNp38UhQAJpxAknAuyO3MV1AwCGMecr0/onZ0n6+PQwDP4afAp6HUfmzUPia78ypkCvi9puDZoVz/PeODZ6HViEq43H5T1tmuOvi+xqtcMTGfNg== 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: From: Rik van Riel Extend the locking scheme used to protect shared hugetlb mappings from truncate vs page fault races, in order to protect private hugetlb mappings (with resv_map) against MADV_DONTNEED. Add a read-write semaphore to the resv_map data structure, and use that from the hugetlb_vma_(un)lock_* functions, in preparation for closing the race between MADV_DONTNEED and page faults. Signed-off-by: Rik van Riel --- include/linux/hugetlb.h | 6 ++++++ mm/hugetlb.c | 41 +++++++++++++++++++++++++++++++++++++---- 2 files changed, 43 insertions(+), 4 deletions(-) diff --git a/include/linux/hugetlb.h b/include/linux/hugetlb.h index 5b2626063f4f..694928fa06a3 100644 --- a/include/linux/hugetlb.h +++ b/include/linux/hugetlb.h @@ -60,6 +60,7 @@ struct resv_map { long adds_in_progress; struct list_head region_cache; long region_cache_count; + struct rw_semaphore rw_sema; #ifdef CONFIG_CGROUP_HUGETLB /* * On private mappings, the counter to uncharge reservations is stored @@ -1231,6 +1232,11 @@ static inline bool __vma_shareable_lock(struct vm_area_struct *vma) return (vma->vm_flags & VM_MAYSHARE) && vma->vm_private_data; } +static inline bool __vma_private_lock(struct vm_area_struct *vma) +{ + return (!(vma->vm_flags & VM_MAYSHARE)) && vma->vm_private_data; +} + /* * Safe version of huge_pte_offset() to check the locks. See comments * above huge_pte_offset(). diff --git a/mm/hugetlb.c b/mm/hugetlb.c index ba6d39b71cb1..e859fba5bc7d 100644 --- a/mm/hugetlb.c +++ b/mm/hugetlb.c @@ -97,6 +97,7 @@ static void hugetlb_vma_lock_alloc(struct vm_area_struct *vma); static void __hugetlb_vma_unlock_write_free(struct vm_area_struct *vma); static void hugetlb_unshare_pmds(struct vm_area_struct *vma, unsigned long start, unsigned long end); +static struct resv_map *vma_resv_map(struct vm_area_struct *vma); static inline bool subpool_is_free(struct hugepage_subpool *spool) { @@ -267,6 +268,10 @@ void hugetlb_vma_lock_read(struct vm_area_struct *vma) struct hugetlb_vma_lock *vma_lock = vma->vm_private_data; down_read(&vma_lock->rw_sema); + } else if (__vma_private_lock(vma)) { + struct resv_map *resv_map = vma_resv_map(vma); + + down_read(&resv_map->rw_sema); } } @@ -276,6 +281,10 @@ void hugetlb_vma_unlock_read(struct vm_area_struct *vma) struct hugetlb_vma_lock *vma_lock = vma->vm_private_data; up_read(&vma_lock->rw_sema); + } else if (__vma_private_lock(vma)) { + struct resv_map *resv_map = vma_resv_map(vma); + + up_read(&resv_map->rw_sema); } } @@ -285,6 +294,10 @@ void hugetlb_vma_lock_write(struct vm_area_struct *vma) struct hugetlb_vma_lock *vma_lock = vma->vm_private_data; down_write(&vma_lock->rw_sema); + } else if (__vma_private_lock(vma)) { + struct resv_map *resv_map = vma_resv_map(vma); + + down_write(&resv_map->rw_sema); } } @@ -294,17 +307,27 @@ void hugetlb_vma_unlock_write(struct vm_area_struct *vma) struct hugetlb_vma_lock *vma_lock = vma->vm_private_data; up_write(&vma_lock->rw_sema); + } else if (__vma_private_lock(vma)) { + struct resv_map *resv_map = vma_resv_map(vma); + + up_write(&resv_map->rw_sema); } } int hugetlb_vma_trylock_write(struct vm_area_struct *vma) { - struct hugetlb_vma_lock *vma_lock = vma->vm_private_data; - if (!__vma_shareable_lock(vma)) - return 1; + if (__vma_shareable_lock(vma)) { + struct hugetlb_vma_lock *vma_lock = vma->vm_private_data; - return down_write_trylock(&vma_lock->rw_sema); + return down_write_trylock(&vma_lock->rw_sema); + } else if (__vma_private_lock(vma)) { + struct resv_map *resv_map = vma_resv_map(vma); + + return down_write_trylock(&resv_map->rw_sema); + } + + return 1; } void hugetlb_vma_assert_locked(struct vm_area_struct *vma) @@ -313,6 +336,10 @@ void hugetlb_vma_assert_locked(struct vm_area_struct *vma) struct hugetlb_vma_lock *vma_lock = vma->vm_private_data; lockdep_assert_held(&vma_lock->rw_sema); + } else if (__vma_private_lock(vma)) { + struct resv_map *resv_map = vma_resv_map(vma); + + lockdep_assert_held(&resv_map->rw_sema); } } @@ -345,6 +372,11 @@ static void __hugetlb_vma_unlock_write_free(struct vm_area_struct *vma) struct hugetlb_vma_lock *vma_lock = vma->vm_private_data; __hugetlb_vma_unlock_write_put(vma_lock); + } else { + struct resv_map *resv_map = vma_resv_map(vma); + + /* no free for anon vmas, but still need to unlock */ + up_write(&resv_map->rw_sema); } } @@ -1068,6 +1100,7 @@ struct resv_map *resv_map_alloc(void) kref_init(&resv_map->refs); spin_lock_init(&resv_map->lock); INIT_LIST_HEAD(&resv_map->regions); + init_rwsem(&resv_map->rw_sema); resv_map->adds_in_progress = 0; /* -- 2.41.0