From: Jason Gunthorpe <[email protected]>

Trying to misuse a range outside its lifetime is a kernel bug. Use poison
bytes to help detect this condition. Double unregister will reliably crash.

Signed-off-by: Jason Gunthorpe <[email protected]>
Reviewed-by: Jérôme Glisse <[email protected]>
Reviewed-by: John Hubbard <[email protected]>
Acked-by: Souptick Joarder <[email protected]>
Reviewed-by: Ralph Campbell <[email protected]>
Reviewed-by: Ira Weiny <[email protected]>
Tested-by: Philip Yang <[email protected]>
---
v2
- Keep range start/end valid after unregistration (Jerome)
v3
- Revise some comments (John)
- Remove start/end WARN_ON (Souptick)
v4
- Fix tabs vs spaces in comment (Christoph)
---
 mm/hmm.c | 14 ++++++++------
 1 file changed, 8 insertions(+), 6 deletions(-)

diff --git a/mm/hmm.c b/mm/hmm.c
index 2ef14b2b5505f6..c30aa9403dbe4d 100644
--- a/mm/hmm.c
+++ b/mm/hmm.c
@@ -925,19 +925,21 @@ void hmm_range_unregister(struct hmm_range *range)
 {
        struct hmm *hmm = range->hmm;
 
-       /* Sanity check this really should not happen. */
-       if (hmm == NULL || range->end <= range->start)
-               return;
-
        mutex_lock(&hmm->lock);
        list_del_init(&range->list);
        mutex_unlock(&hmm->lock);
 
        /* Drop reference taken by hmm_range_register() */
-       range->valid = false;
        mmput(hmm->mm);
        hmm_put(hmm);
-       range->hmm = NULL;
+
+       /*
+        * The range is now invalid and the ref on the hmm is dropped, so
+        * poison the pointer.  Leave other fields in place, for the caller's
+        * use.
+        */
+       range->valid = false;
+       memset(&range->hmm, POISON_INUSE, sizeof(range->hmm));
 }
 EXPORT_SYMBOL(hmm_range_unregister);
 
-- 
2.22.0

_______________________________________________
amd-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx

Reply via email to