]> git.dujemihanovic.xyz Git - linux.git/commit
mm: avoid leaving partial pfn mappings around in error case
authorLinus Torvalds <torvalds@linux-foundation.org>
Thu, 12 Sep 2024 00:11:23 +0000 (17:11 -0700)
committerLinus Torvalds <torvalds@linux-foundation.org>
Thu, 12 Sep 2024 19:10:00 +0000 (12:10 -0700)
commit79a61cc3fc0466ad2b7b89618a6157785f0293b3
tree7eb2bef15767c8f330f9035651794e2477f4b6f5
parent77f587896757708780a7e8792efe62939f25a5ab
mm: avoid leaving partial pfn mappings around in error case

As Jann points out, PFN mappings are special, because unlike normal
memory mappings, there is no lifetime information associated with the
mapping - it is just a raw mapping of PFNs with no reference counting of
a 'struct page'.

That's all very much intentional, but it does mean that it's easy to
mess up the cleanup in case of errors.  Yes, a failed mmap() will always
eventually clean up any partial mappings, but without any explicit
lifetime in the page table mapping itself, it's very easy to do the
error handling in the wrong order.

In particular, it's easy to mistakenly free the physical backing store
before the page tables are actually cleaned up and (temporarily) have
stale dangling PTE entries.

To make this situation less error-prone, just make sure that any partial
pfn mapping is torn down early, before any other error handling.

Reported-and-tested-by: Jann Horn <jannh@google.com>
Cc: Andrew Morton <akpm@linux-foundation.org>
Cc: Jason Gunthorpe <jgg@ziepe.ca>
Cc: Simona Vetter <simona.vetter@ffwll.ch>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
mm/memory.c