Message ID | 20221129193526.3588187-1-peterx@redhat.com (mailing list archive) |
---|---|
State | New |
Headers | show |
On Tue, 29 Nov 2022 14:35:16 -0500 Peter Xu <peterx@redhat.com> wrote: > Based on latest mm-unstable (9ed079378408). > > This can be seen as a follow-up series to Mike's recent hugetlb vma lock > series for pmd unsharing, but majorly covering safe use of huge_pte_offset. We're at -rc7 (a -rc8 appears probable this time) and I'm looking to settle down and stabilize things... > > ... > > huge_pte_offset() is always called with mmap lock held with either read or > write. It was assumed to be safe but it's actually not. One race > condition can easily trigger by: (1) firstly trigger pmd share on a memory > range, (2) do huge_pte_offset() on the range, then at the meantime, (3) > another thread unshare the pmd range, and the pgtable page is prone to lost > if the other shared process wants to free it completely (by either munmap > or exit mm). That sounds like a hard-to-hit memory leak, but what we have here is a user-triggerable use-after-free and an oops. Ugh. Could people please prioritize the review and testing of this patchset?
On Tue, 29 Nov 2022 14:35:16 -0500 Peter Xu <peterx@redhat.com> wrote:
> [ 17.975943] Oops: 0000 [#1] PREEMPT SMP NOPTI
Do we know which kernel versions are affected here?
Hi, Andrew, On Tue, Nov 29, 2022 at 12:49:44PM -0800, Andrew Morton wrote: > On Tue, 29 Nov 2022 14:35:16 -0500 Peter Xu <peterx@redhat.com> wrote: > > > Based on latest mm-unstable (9ed079378408). > > > > This can be seen as a follow-up series to Mike's recent hugetlb vma lock > > series for pmd unsharing, but majorly covering safe use of huge_pte_offset. > > We're at -rc7 (a -rc8 appears probable this time) and I'm looking to > settle down and stabilize things... I targeted this series for the next release not current, because there's no known report for it per my knowledge. The reproducer needs explicit kernel delays to trigger as mentioned in the cover letter. So far I didn't try to reproduce with a generic kernel yet but just to verify the existance of the problem. > > > > > ... > > > > huge_pte_offset() is always called with mmap lock held with either read or > > write. It was assumed to be safe but it's actually not. One race > > condition can easily trigger by: (1) firstly trigger pmd share on a memory > > range, (2) do huge_pte_offset() on the range, then at the meantime, (3) > > another thread unshare the pmd range, and the pgtable page is prone to lost > > if the other shared process wants to free it completely (by either munmap > > or exit mm). > > That sounds like a hard-to-hit memory leak, but what we have here is a > user-triggerable use-after-free and an oops. Ugh. IIUC it's not a leak, but it's just that huge_pte_offset() can walk the (pmd-shared) pgtable page and also trying to take the pgtable lock even though the page can already be freed in parallel, hence accessing either the page or the pgtable lock after the pgtable page got freed. E.g., the 1st warning was trigger by: static inline struct lock_class *hlock_class(struct held_lock *hlock) { unsigned int class_idx = hlock->class_idx; /* Don't re-read hlock->class_idx, can't use READ_ONCE() on bitfield */ barrier(); if (!test_bit(class_idx, lock_classes_in_use)) { /* * Someone passed in garbage, we give up. */ DEBUG_LOCKS_WARN_ON(1); <---------------------------- return NULL; } ... } I think it's because the spin lock got freed along with the pgtable page, so when we want to lock the pgtable lock we see weird lock state in dep_map, as the lock pointer is not valid at all.
On Tue, 29 Nov 2022 16:19:52 -0500 Peter Xu <peterx@redhat.com> wrote: > On Tue, Nov 29, 2022 at 12:49:44PM -0800, Andrew Morton wrote: > > On Tue, 29 Nov 2022 14:35:16 -0500 Peter Xu <peterx@redhat.com> wrote: > > > > > Based on latest mm-unstable (9ed079378408). > > > > > > This can be seen as a follow-up series to Mike's recent hugetlb vma lock > > > series for pmd unsharing, but majorly covering safe use of huge_pte_offset. > > > > We're at -rc7 (a -rc8 appears probable this time) and I'm looking to > > settle down and stabilize things... > > I targeted this series for the next release not current, because there's no > known report for it per my knowledge. > > The reproducer needs explicit kernel delays to trigger as mentioned in the > cover letter. So far I didn't try to reproduce with a generic kernel yet > but just to verify the existance of the problem. OK, thanks, I missed that. I'll give the series a run in -next for a couple of days then I'll pull it out until after the next Linus merge window, so it can't invalidate testing heading into that merge window.
On Tue, Nov 29, 2022 at 12:51:17PM -0800, Andrew Morton wrote: > On Tue, 29 Nov 2022 14:35:16 -0500 Peter Xu <peterx@redhat.com> wrote: > > > [ 17.975943] Oops: 0000 [#1] PREEMPT SMP NOPTI > > Do we know which kernel versions are affected here? Since lockless walk of huge_pte_offset() existed since the initial git commit, it should be the time when we introduced pmd sharing for hugetlb, which means any kernel after commit 39dde65c9940 ("[PATCH] shared page table for hugetlb page", 2006-12-07) should be prone to the issue at least for x86 (as pmd sharing was proposed initially only for x86).
On 29.11.22 20:35, Peter Xu wrote: > Based on latest mm-unstable (9ed079378408). > > This can be seen as a follow-up series to Mike's recent hugetlb vma lock > series for pmd unsharing, but majorly covering safe use of huge_pte_offset. > > Comparing to previous rfcv2, the major change is I dropped the new pgtable > lock but only use vma lock for locking. The major reason is I overlooked > that the pgtable lock was not protected by RCU: __pmd_free_tlb() frees the > pgtable lock before e.g. call_rcu() for RCU_TABLE_FREE archs. OTOH many of > the huge_pte_offset() call sites do need to take pgtable lock. It means > the valid users for the new RCU lock will be very limited. Thanks. > > It's possible that in the future we can rework the pgtable free to only > free the pgtable lock after RCU grace period (move pgtable_pmd_page_dtor() > to be within tlb_remove_table_rcu()), then the RCU lock will make more > sense. For now, make it simple by fixing the races first. Good. > > Since this version attached a reproducer (below) and also removed the RCU > (especially, the fallback irqoff) solution, removing RFC tag. Very nice, thanks. > > Old versions: > > rfcv1: https://lore.kernel.org/r/20221030212929.335473-1-peterx@redhat.com > rfcv2: https://lore.kernel.org/r/20221118011025.2178986-1-peterx@redhat.com > > Problem > ======= > > huge_pte_offset() is a major helper used by hugetlb code paths to walk a > hugetlb pgtable. It's used mostly everywhere since that's needed even > before taking the pgtable lock. > > huge_pte_offset() is always called with mmap lock held with either read or > write. It was assumed to be safe but it's actually not. One race > condition can easily trigger by: (1) firstly trigger pmd share on a memory > range, (2) do huge_pte_offset() on the range, then at the meantime, (3) > another thread unshare the pmd range, and the pgtable page is prone to lost > if the other shared process wants to free it completely (by either munmap > or exit mm). So just that I understand correctly: Two processes, #A and #B, share a page table. Process #A runs two threads, #A1 and #A2. #A1 walks that shared page table (using huge_pte_offset()), for example, to resolve a page fault. Concurrently, #A2 triggers unsharing of that page table (replacing it by a private page table), for example, using munmap(). So #A1 will eventually read/write the shared page table while we're placing a private page table. Which would be fine (assuming no unsharing would be required by #A1), however, if #B also concurrently drops the reference to the shared page table (), the shared page table could essentially get freed while #A1 is still walking it. I suspect, looking at the reproducer, that the page table deconstructor was called. Will the page table also actually get freed already? IOW, could #A1 be reading/writing a freed page? > > The recent work from Mike on vma lock can resolve most of this already. > It's achieved by forbidden pmd unsharing during the lock being taken, so no > further risk of the pgtable page being freed. It means if we can take the > vma lock around all huge_pte_offset() callers it'll be safe. Agreed. > > There're already a bunch of them that we did as per the latest mm-unstable, > but also quite a few others that we didn't for various reasons especially > on huge_pte_offset() usage. > > One more thing to mention is that besides the vma lock, i_mmap_rwsem can > also be used to protect the pgtable page (along with its pgtable lock) from > being freed from under us. IOW, huge_pte_offset() callers need to either > hold the vma lock or i_mmap_rwsem to safely walk the pgtables. > > A reproducer of such problem, based on hugetlb GUP (NOTE: since the race is > very hard to trigger, one needs to apply another kernel delay patch too, > see below): Thanks, David / dhildenb
On Wed, Nov 30, 2022 at 10:46:24AM +0100, David Hildenbrand wrote: > > huge_pte_offset() is always called with mmap lock held with either read or > > write. It was assumed to be safe but it's actually not. One race > > condition can easily trigger by: (1) firstly trigger pmd share on a memory > > range, (2) do huge_pte_offset() on the range, then at the meantime, (3) > > another thread unshare the pmd range, and the pgtable page is prone to lost > > if the other shared process wants to free it completely (by either munmap > > or exit mm). > > So just that I understand correctly: > > Two processes, #A and #B, share a page table. Process #A runs two threads, > #A1 and #A2. > > #A1 walks that shared page table (using huge_pte_offset()), for example, to > resolve a page fault. Concurrently, #A2 triggers unsharing of that page > table (replacing it by a private page table), Not yet replacing it, just unsharing. If the replacement happened we shouldn't trigger a bug either because huge_pte_offset() will return the private pgtable page instead. > for example, using munmap(). munmap() may not work because it needs mmap lock, so it'll wait until #A1 completes huge_pte_offset() walks and release mmap lock read. Many of other things can trigger unshare, though. In the reproducer I used MADV_DONTNEED. > > So #A1 will eventually read/write the shared page table while we're placing > a private page table. Which would be fine (assuming no unsharing would be > required by #A1), however, if #B also concurrently drops the reference to > the shared page table (), the shared page table could essentially get freed > while #A1 is still walking it. > > I suspect, looking at the reproducer, that the page table deconstructor was > called. Will the page table also actually get freed already? IOW, could #A1 > be reading/writing a freed page? If with the existing code base, I think it could. If with RCU lock, it couldn't, but still since the pgtable lock is freed even if the page is not, we'll still hit weird issues when accessing the lock. And with vma lock it should be all safe.
diff --git a/mm/hugetlb.c b/mm/hugetlb.c index 9d97c9a2a15d..f8d99dad5004 100644 --- a/mm/hugetlb.c +++ b/mm/hugetlb.c @@ -38,6 +38,7 @@ #include <asm/page.h> #include <asm/pgalloc.h> #include <asm/tlb.h> +#include <asm/delay.h> #include <linux/io.h> #include <linux/hugetlb.h> @@ -6290,6 +6291,7 @@ long follow_hugetlb_page(struct mm_struct *mm, struct vm_area_struct *vma, bool unshare = false; int absent; struct page *page; + unsigned long c = 0; /* * If we have a pending SIGKILL, don't keep faulting pages and @@ -6309,6 +6311,13 @@ long follow_hugetlb_page(struct mm_struct *mm, struct vm_area_struct *vma, */ pte = huge_pte_offset(mm, vaddr & huge_page_mask(h), huge_page_size(h)); + + pr_info("%s: withhold 1 sec...\n", __func__); + for (c = 0; c < 100; c++) { + udelay(10000); + } + pr_info("%s: withhold 1 sec...done\n", __func__); + if (pte) ptl = huge_pte_lock(h, mm, pte); absent = !pte || huge_pte_none(huge_ptep_get(pte));