perf_events: Fix perf_counter_mmap() hook in mprotect()

As pointed out by Linus, commit dab5855 ("perf_counter: Add mmap event hooks to
mprotect()") is fundamentally wrong as mprotect_fixup() can free 'vma' due to
merging. Fix the problem by moving perf_event_mmap() hook to
mprotect_fixup().

Note: there's another successful return path from mprotect_fixup() if old
flags equal to new flags. We don't, however, need to call
perf_event_mmap() there because 'perf' already knows the VMA is
executable.

Reported-by: Dave Jones <davej@redhat.com>
Analyzed-by: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Ingo Molnar <mingo@elte.hu>
Reviewed-by: Peter Zijlstra <a.p.zijlstra@chello.nl>
Signed-off-by: Pekka Enberg <penberg@kernel.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>

authored by Pekka Enberg and committed by Linus Torvalds 63bfd738 814ce252

+1 -1
+1 -1
mm/mprotect.c
··· 211 mmu_notifier_invalidate_range_end(mm, start, end); 212 vm_stat_account(mm, oldflags, vma->vm_file, -nrpages); 213 vm_stat_account(mm, newflags, vma->vm_file, nrpages); 214 return 0; 215 216 fail: ··· 300 error = mprotect_fixup(vma, &prev, nstart, tmp, newflags); 301 if (error) 302 goto out; 303 - perf_event_mmap(vma); 304 nstart = tmp; 305 306 if (nstart < prev->vm_end)
··· 211 mmu_notifier_invalidate_range_end(mm, start, end); 212 vm_stat_account(mm, oldflags, vma->vm_file, -nrpages); 213 vm_stat_account(mm, newflags, vma->vm_file, nrpages); 214 + perf_event_mmap(vma); 215 return 0; 216 217 fail: ··· 299 error = mprotect_fixup(vma, &prev, nstart, tmp, newflags); 300 if (error) 301 goto out; 302 nstart = tmp; 303 304 if (nstart < prev->vm_end)