x86/mm/pat: Change free_memtype() to support shrinking case
authorToshi Kani <toshi.kani@hpe.com>
Wed, 23 Dec 2015 00:54:24 +0000 (17:54 -0700)
committerThomas Gleixner <tglx@linutronix.de>
Tue, 5 Jan 2016 10:10:23 +0000 (11:10 +0100)
commit2039e6acaf94d83ec6b6d9f3d0bce7ea1f099918
treec87c5e2d5fc668127dd3441ce33c5fd6e4faf508
parentd9fe4fab11976e56b2e992980bf6ce948bdf02ac
x86/mm/pat: Change free_memtype() to support shrinking case

Using mremap() to shrink the map size of a VM_PFNMAP range causes
the following error message, and leaves the pfn range allocated.

 x86/PAT: test:3493 freeing invalid memtype [mem 0x483200000-0x4863fffff]

This is because rbt_memtype_erase(), called from free_memtype()
with spin_lock held, only supports to free a whole memtype node in
memtype_rbroot.  Therefore, this patch changes rbt_memtype_erase()
to support a request that shrinks the size of a memtype node for
mremap().

memtype_rb_exact_match() is renamed to memtype_rb_match(), and
is enhanced to support EXACT_MATCH and END_MATCH in @match_type.
Since the memtype_rbroot tree allows overlapping ranges,
rbt_memtype_erase() checks with EXACT_MATCH first, i.e. free
a whole node for the munmap case.  If no such entry is found,
it then checks with END_MATCH, i.e. shrink the size of a node
from the end for the mremap case.

On the mremap case, rbt_memtype_erase() proceeds in two steps,
1) remove the node, and then 2) insert the updated node.  This
allows proper update of augmented values, subtree_max_end, in
the tree.

Signed-off-by: Toshi Kani <toshi.kani@hpe.com>
Cc: Ingo Molnar <mingo@redhat.com>
Cc: H. Peter Anvin <hpa@zytor.com>
Cc: Borislav Petkov <bp@suse.de>
Cc: stsp@list.ru
Cc: linux-mm@kvack.org
Link: http://lkml.kernel.org/r/1450832064-10093-3-git-send-email-toshi.kani@hpe.com
Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
arch/x86/mm/pat.c
arch/x86/mm/pat_rbtree.c