From patchwork Tue Sep 26 03:10:50 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Rik van Riel X-Patchwork-Id: 13398617 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 4C10CE81815 for ; Tue, 26 Sep 2023 03:13:44 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id C522B6B011F; Mon, 25 Sep 2023 23:13:43 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id C006C6B0120; Mon, 25 Sep 2023 23:13:43 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id A54486B0123; Mon, 25 Sep 2023 23:13:43 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id 984396B011F for ; Mon, 25 Sep 2023 23:13:43 -0400 (EDT) Received: from smtpin22.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id 52D281C87BF for ; Tue, 26 Sep 2023 03:13:43 +0000 (UTC) X-FDA: 81277278726.22.EC8480C Received: from shelob.surriel.com (shelob.surriel.com [96.67.55.147]) by imf23.hostedemail.com (Postfix) with ESMTP id 99481140002 for ; Tue, 26 Sep 2023 03:13:41 +0000 (UTC) Authentication-Results: imf23.hostedemail.com; dkim=none; spf=none (imf23.hostedemail.com: domain of riel@shelob.surriel.com has no SPF policy when checking 96.67.55.147) smtp.mailfrom=riel@shelob.surriel.com; dmarc=none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1695698021; 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=Lj4Z6Ul00eCWNgyGLnZw82tbqm4QntGZxAS4UjBFJEM=; b=yDZo1zkpWqt4hE/ZDrzp0YCYJa1T+IpOPoeQEBf20HSbbYGvBKgJ9EElXNO0EJu5GX9/qj 2b3mqDGBfifAns/NvsnbCMS9bC2dogm8wkv/Em4Tm4didbVGWnd5pW9ym5iIN0EFIXixrK hrrA1wtXHTSYZ8/47IPuDR66aHZKjuw= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1695698021; a=rsa-sha256; cv=none; b=ixG0ESacKT/VNepK+qTwjcAbu/Y/Ojjxu/ZAV154Zr4Hk+QWWnjAZyLdm9ge9Mnc6ZxL1r 6IIqjAIPsuG3ThSvpsiXPNJJHqp5T+l7kiMBySH8FzkITqwrg3fHMghx0sohZkyq2tCoiE QLC+RSHxqdyqtUe6KQjwHrA/duTi2F4= ARC-Authentication-Results: i=1; imf23.hostedemail.com; dkim=none; spf=none (imf23.hostedemail.com: domain of riel@shelob.surriel.com has no SPF policy when checking 96.67.55.147) smtp.mailfrom=riel@shelob.surriel.com; dmarc=none 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 1qkyVD-00025y-0w; Mon, 25 Sep 2023 23:12:47 -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 23:10:50 -0400 Message-ID: <20230926031245.795759-2-riel@surriel.com> X-Mailer: git-send-email 2.41.0 In-Reply-To: <20230926031245.795759-1-riel@surriel.com> References: <20230926031245.795759-1-riel@surriel.com> MIME-Version: 1.0 X-Stat-Signature: mbfi3excgizig6r1p6p74x53zo51ceqh X-Rspamd-Server: rspam10 X-Rspamd-Queue-Id: 99481140002 X-Rspam-User: X-HE-Tag: 1695698021-330114 X-HE-Meta: U2FsdGVkX18uCEE6mPsL7yxnBRIDRPc3ImIlBVPukGmgo1GJnocodGoDKcQPPtMimz0AhSEVVQOuT2XY3cskF1c++Jle8xV0+RcHzrrQRYWO9LGnbhgHDtumOOiaXMaE62NyhLDSwZynI8mHOjlYFx8XDp/ON2AF/K25l9UoD2k9FQIIqpebqO6mZNKHwricdc5O6bxxVkN7w3S0PB/KdjHimDxcZ7k703YAbGtBVstz/H28DqlaQvBj6PTbcI+xbBuNabD6/1dCezT9PJe6EFd8Bwt4amNN/iDJtT+slyXZwRfbC4hOeJY+M2j6Vuj3rbKlIsFarH9MXzisOBzTm9o9EwbrgYNnI3lwhqOT6JWJw1lOQ+uW4xvKYd7lNh5t0WaV5LkDSnHNwpsOXVvfcTZTjq95YNyWRMODyD2L/WX1KbVrYlgiX45iWOYA9zYcpdhT3crJgEH7VVbIDFxO9mxydOqDGYNVnyC+cmP+yXwjkoespYJ6TP5wUFF4gEGaq4URXK1gAq/54mJE9CtD23k2UywPo0bb902gmX3az+HuAyUkY1rV+XfGek2gLzjq4eAlI3QpG6b8sJnP0+meASNBtSUFPt9IAZ9zEHSOJhTXY11ebkZD/PHOLpJQvbjYcbRUc4DoLpoAPxPdDuG5pIBnQFJLy9m5iF6Tu4K9Biq953x/yVpyWXFmQDqfiF5SQJIJxK0DKh/x85sKWhqWgG0fd9EdXk0qs/o/rCF3tr6oKgDHbcxL2Je1azVFHLD1ZSJ5QFLLWwh3t7qaNV/SO+5DT6eT4vgjINfyD2lJP7nWhGiTqL7LC+FAbvukuyMDSH257MOKdIetSo7on4pRHdaFYN1R97v048WtYy9PT9/JNH9hq9vvihjsdpOCJlQXey8z1NNjfth9JUHMvKO8uvo23Jubq77tzxPplRea6JWH5sEkGgOux+V/A5UCoYCFVYk7DqyHmYHepGDqvM1 YGF0IbGo nayUtP9SFTx0Z4C2s6IvJrzwEANwyzzrMEgRviJVh/XFlo7jYpOUz6DOBqSr9cmxQLnEuCav54yV5nyOmDvZS59lFCUUf8RCg1YgWQXqHp0rGSd4NuMFHUdTYhZey8IRvk+I1oJEDZE8L57ShV+gd9tuUl+I+/ZE5qaMJ92YDiW7nAek3j+vJ0PfcyqI2Vzd9AWvKe8waHHX19F0EIlgNNAfNsjrl0xsTZK4pQ/3kAJK1e8l/fYe+XoTePV5BkD0I8utKEoWjPuNmBGCInuqMtZrkfFlTAD5dyiGYTu6Jl+d9scV9FurrpOa7m8UtZo9Ezk+TQ/Tie8yufLsHHJP07WyObysqfufxIr7vIiIm0ZOk3Lo= 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 Reviewed-by: Mike Kravetz --- 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; /*