From patchwork Sun Oct 1 00:55:48 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: 13405271 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 A39D6E748F0 for ; Sun, 1 Oct 2023 00:58:06 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 03E6F6B01F5; Sat, 30 Sep 2023 20:58:06 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id F31326B0249; Sat, 30 Sep 2023 20:58:05 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id E1F1E6B024A; Sat, 30 Sep 2023 20:58:05 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id CDEB16B01F5 for ; Sat, 30 Sep 2023 20:58:05 -0400 (EDT) Received: from smtpin29.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id A1BDF140238 for ; Sun, 1 Oct 2023 00:58:05 +0000 (UTC) X-FDA: 81295080930.29.C1101A4 Received: from shelob.surriel.com (shelob.surriel.com [96.67.55.147]) by imf20.hostedemail.com (Postfix) with ESMTP id 11DE81C0002 for ; Sun, 1 Oct 2023 00:58:03 +0000 (UTC) Authentication-Results: imf20.hostedemail.com; dkim=none; dmarc=none; spf=none (imf20.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=1696121884; 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=nSZmvMhoI0pEeAkjjR9SHRNaCkVt5+nPSYRrCozDXx4=; b=lAK5LZ5THlNIHq9QdDUxx4xqtnnxg2EkeJdlxb4hQ2XM/6bwr2KPWAv4B0qJdjg9+/Rgcn OdzrLPyOvcTxUL/r0HugvpQygOaUiNAUATpBn+2M4n7S+/KtWG/Z2E+guksn7LSugkozue rghbo1qM92YQzM/b1V49MXhON16gUv0= ARC-Authentication-Results: i=1; imf20.hostedemail.com; dkim=none; dmarc=none; spf=none (imf20.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=1696121884; a=rsa-sha256; cv=none; b=lysPVlHb5LH9QVDY0eMhMbbhY2YCn6+A2Pv9I0qKCS0n9V6fibUVSgbBs1qebi4D8Qw94/ w9wWdd9Ryq/MliI74fr4i7s5LgM91rIedddurTqVJT02tuclryabdaUL52YArTANBCrgYN BaYAxAi4b69bEdxBCRqe+alTKlx/hRo= 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 1qmklZ-0008G8-0N; Sat, 30 Sep 2023 20:57:01 -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: Sat, 30 Sep 2023 20:55:48 -0400 Message-ID: <20231001005659.2185316-2-riel@surriel.com> X-Mailer: git-send-email 2.41.0 In-Reply-To: <20231001005659.2185316-1-riel@surriel.com> References: <20231001005659.2185316-1-riel@surriel.com> MIME-Version: 1.0 X-Rspamd-Queue-Id: 11DE81C0002 X-Rspam-User: X-Rspamd-Server: rspam05 X-Stat-Signature: wtjgiua1boxqwgn8g8pbtoqo84ngzcka X-HE-Tag: 1696121883-841836 X-HE-Meta: U2FsdGVkX19yxZ7QinHGnqjbct0FTj6cgle30I6n8HnQqdqa7N6qn6H4yQnrj/8nboSa10RuHRTmXqNw4a7Q+l718fBBD21THbdMw4cLc1dsYJzU1AcBLB4MhjC/C5YGjipICj+njqdc+OaRMsivi40Jxtjpf6jeXDsX9FFDRAMxpGOK5uXfw3mGiExiCInO6oNeqkVuqOKd/yL53WwvQSk7HIVJzSIng3130VAjv6SEGq+GGzRKS1ofLP1Fnl1zbrVt1qFevB4JHQLRH/I7yB7/V6xZrxVsQcMrkVY+xwL33yrYT+qJF/U9Zd1cP05QZuWKI8T9o++PUF2442U5PXXFgl6fFSl9WUJ8UfwFTXW1nWCFxPWn63ScZh8HXRyrHAXdbRi1mcEvLB04aQ9WPrUT63Ss7ziPfyZAiBVaBKaTVAIsOgmOANyx2n9WDoUcqY5J95SkhPNqTI/xlYIcF3AtNNkzmAYkALraPHrdBL1GoPCzSPN2YpG+bnvJNwibd0oX0IsLSkbpdCk9z6X5HS80ctskaE7RUu7bhMYlBzgGIQukWojkf80U+IBodWqINKjOSYHw9PGOtQXmVibQzfZX4OURGYVeFW+UN18S0b2bVR7miPpewlDXtI1xLFMendtLmNO7M65amnrQ3qrI6ALDtJ2jNvARxwBzHrJUHhbERs6njDlrpBJIjSktH16L2PEFlqsGZ2obn/AhEuMdNSohUEsVSSgMiWc3dpNf1tlguqug1FpAooKX6kd9QQi56B49AWbGgNmwm+xP9hNjCjnhLdiwaWXDJhNJz7NUjiGbbYaoUMhFda+3UA+tqyN1c9oKwORSGuXaezFcv2eE3FRfycA9mRqkMB1ymaVI5aV5cDLsW0ES3eDlqBGzbcV/g3n1uIiVfPREX9kNXFoX8xO5tz7La1RpWZBCY/skqEPqKhhhc3Imcg8oA1eRhb+Zy2T7uRlUw9GGpqsFas9 DZGnR/yM xD+DkkQpMqtvPY6DjZP4H6rXRVTTHjGDjftUvWzdVj9mlFTPqANv4gsprLlPYjBxa9kqVepXvd3c+s77BirFLaAKzWq6teAg35e8yMCPcqGUogIeynVSdDkAJEHURENaRq87o/r580Et0+Hlw0uoZO/oVW/Y3zFGkuCzr/S3PBXulABDMnNpfglw+fUKk+O3L7tzKJ5AJlHaryfJL7GJ1lQ+izFj4vm47/6Iq/eJkC+oG5eCyemrJsWh4+SDjnS2hEuN6yyoSoUhwlrma2kfZFTVSmbnDh5l/ATwbgPBpgbnq5TJZukG7XvmGFzmZLQrX17pySnDv+epgMGPKs2bGtJPPkp9IuecLaL2Vo0PibecUKJQ= 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..ee7497f37098 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 if (__vma_private_lock(vma)) { + 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; /*