From patchwork Wed Oct 4 03:25:03 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: 13408258 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 EE91CE8FDC3 for ; Wed, 4 Oct 2023 03:28:36 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 909448D0093; Tue, 3 Oct 2023 23:28:36 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 8B8FE8D0002; Tue, 3 Oct 2023 23:28:36 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 781DB8D0093; Tue, 3 Oct 2023 23:28:36 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id 682818D0002 for ; Tue, 3 Oct 2023 23:28:36 -0400 (EDT) Received: from smtpin19.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id 3A315C0535 for ; Wed, 4 Oct 2023 03:28:36 +0000 (UTC) X-FDA: 81306346632.19.3EFEB41 Received: from shelob.surriel.com (shelob.surriel.com [96.67.55.147]) by imf12.hostedemail.com (Postfix) with ESMTP id A39094000D for ; Wed, 4 Oct 2023 03:28:34 +0000 (UTC) Authentication-Results: imf12.hostedemail.com; dkim=none; dmarc=none; spf=none (imf12.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=1696390114; 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=oxlcO+xmMcVZKmyrYDcr4XxhuTmockHQJZop0nLEEQk=; b=R7BUKFKTYLw2Rdwe8MB2LxXwaWTK0yaqh/eezxKsq2mMIDcc49eyPoDGaxuDdAjeAATHw4 HHrxt7uSJGGDiwDvGp02FZNM1jJY8uEvLBHxpMKLqECdaYyqrqkvE3k/3GDu+M0PAO0cJx fAqOXABgZ6Sh4F08R8v8gLWhvVWzZpw= ARC-Authentication-Results: i=1; imf12.hostedemail.com; dkim=none; dmarc=none; spf=none (imf12.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=1696390114; a=rsa-sha256; cv=none; b=bquoKyle4U0C8uMz6N49I625ZOU7h/aIJl/JKaoGlT0WWf0jwwg6JhR8yyfxPKeKSXYiIj kHURCb/2da0JYZt3AnNk17ee0006Vem5eAc+84mYMGRqJo+ScsT7nwagmE2R5hNzCUa8x9 8ZCenqJw6Rf92kFYd9Azm7pnjYOpqLM= 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 1qnsYb-0005Bb-0R; Tue, 03 Oct 2023 23:28:17 -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 , stable@kernel.org Subject: [PATCH 1/3] hugetlbfs: extend hugetlb_vma_lock to private VMAs Date: Tue, 3 Oct 2023 23:25:03 -0400 Message-ID: <20231004032814.3108383-2-riel@surriel.com> X-Mailer: git-send-email 2.41.0 In-Reply-To: <20231004032814.3108383-1-riel@surriel.com> References: <20231004032814.3108383-1-riel@surriel.com> MIME-Version: 1.0 X-Rspam-User: X-Stat-Signature: oitp7x9i7koj34u876nxqiwdane8uwga X-Rspamd-Server: rspam07 X-Rspamd-Queue-Id: A39094000D X-HE-Tag: 1696390114-6722 X-HE-Meta: U2FsdGVkX19HlneVdMHyuByq4mb5xzSi2FZ6jqSDVH8+hpxdmFVsWXAPyASFY9r7L0mU+OAYQY3htiO142DwEYD7yszXGp4ICmUgh7hRw7t6NlZ1d9PBSqhVLgPkLMsrLinwutcQM3pQRSb6gzH3NgWIY3m6aO12tG/oPDRDs4aKLHdtl3X3ncAogf84qkNbSgUVmr156UujIOZRvc6qwZ7GtlqHUBmc+JvCBgXToqIB8QI1LYMFGnweaZBzGR0hHUVFdwIZImzvEZPIM5q0PYaj1WSEmBtgVcYzEjL2DMojVvWcCFlrQgr0d3n6p39ZIl49+P9alGdC/Cs3SFXkaGgpFhO0OCsf1PyDjkU7GiQXadgEVx67Q41zNYZN+H5EAelAuQllRRMfegDkymS2zKxlRZSmR+8txRyQL+o+tMo+FkIXz7V8MhyGZKTUSfUS8YURCE6GB/ezQagRO1krDd4NvF6bdioak1Q4y4M/JFqHRTQh/WGPkGdeZn9AGwqb2eyJu22kZRbFzISWDDaCiwiB8Zz3UB677Vmdue2ypS/yauilwF4GHmnkaHfhnbd8DxAK1il1w67n4/bFcX3EMiRJhELwOEkSONRCVAnKWSdZ11W1ve/gCatbdm73IVnYQN4Ftn2qojZZrbgvLXBi5n9FJttZA/RZwwd/ibdSe4aWQedLbj8pA/HXhREO3cD9Dbk7ZY2Hs73X+pxpFMZAA7glzi+Dr+8z0QjxNAVJuloCP7tWI6PODTDmkOmg/Nkn0q6HyAz4XvaQv80xYhZBiJLhLtXctv5mV0eSBNjwZPUzA20rbh2VVkB0xLtFZAI+As87phRCwIXvhI7wWzpWP+sHCvtJDFXqr+UTfyjGxLLBUw9q1+J82vPbmpRym+iG9ptmX8My9PjMha36YlTPgZt9fzfB8dSBuj0godckjhQQLwiFeX8eV9oFeVHcLi/CCjzDqnciUWVAkUWuR4K fxwzn7zL 0UBXbGSF0LBHmDFnD2dVMZvlQMTp6KewTrDYpve/71uIHTnKBfv4VlszpS8G2RAW5w0JF1UYXsXFOrkjlEIc6Oh0Z/qsEATqxNtRYTz3Bq+VQkxU+rdKFjNWPQCP9sgTPgg6dUk6qceHEdUWgio3PSz+xpDf3qVZ0nWuRAlB2SyH84ajNw8hiaWKgpUKP0S9aWGVfY4x0nA6QsZ2aJNE1hgHgoHnYQ0KkJYZqAX11jD2WkELstIeJx44/0Wht7jVccHG8gs4hoHGCSKAjCiEHAUShindkLGH73+6FS6DPY2g0KIqnMv/kyf1BZtBwrFATpnCcook3mj8LpYZRb/Efp8FIoAXW4D4cn4X9Q3v947CcCZHjHGh8VE91sBnELxzCv8wfiR3J9g1GCFCdHRikHp+OgCvkKHbcMMH3wG7xql8iTUc= 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 Cc: stable@kernel.org Fixes: 04ada095dcfc ("hugetlb: don't delete vma_lock in hugetlb MADV_DONTNEED processing") --- 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; /*