Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

mm/madvise: remove redundant mmap_lock operations from process_madvise()

Optimize redundant mmap lock operations from process_madvise() by directly
doing the mmap locking first, and then the remaining works for all ranges
in the loop.

[akpm@linux-foundation.org: update comment, per Lorenzo]
Link: https://lkml.kernel.org/r/20250206061517.2958-5-sj@kernel.org
Signed-off-by: SeongJae Park <sj@kernel.org>
Reviewed-by: Shakeel Butt <shakeel.butt@linux.dev>
Reviewed-by: Liam R. Howlett <howlett@gmail.com>
Reviewed-by: Lorenzo Stoakes <lorenzo.stoakes@oracle.com>
Cc: David Hildenbrand <david@redhat.com>
Cc: Davidlohr Bueso <dave@stgolabs.net>
Cc: Vlastimil Babka <vbabka@suse.cz>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>

authored by

SeongJae Park and committed by
Andrew Morton
4000e3d0 457753da

+25 -3
+25 -3
mm/madvise.c
··· 1778 1778 1779 1779 total_len = iov_iter_count(iter); 1780 1780 1781 + ret = madvise_lock(mm, behavior); 1782 + if (ret) 1783 + return ret; 1784 + 1781 1785 while (iov_iter_count(iter)) { 1782 - ret = do_madvise(mm, (unsigned long)iter_iov_addr(iter), 1783 - iter_iov_len(iter), behavior); 1786 + unsigned long start = (unsigned long)iter_iov_addr(iter); 1787 + size_t len_in = iter_iov_len(iter); 1788 + size_t len; 1789 + 1790 + if (!is_valid_madvise(start, len_in, behavior)) { 1791 + ret = -EINVAL; 1792 + break; 1793 + } 1794 + 1795 + len = PAGE_ALIGN(len_in); 1796 + if (start + len == start) 1797 + ret = 0; 1798 + else 1799 + ret = madvise_do_behavior(mm, start, len_in, len, 1800 + behavior); 1784 1801 /* 1785 1802 * An madvise operation is attempting to restart the syscall, 1786 1803 * but we cannot proceed as it would not be correct to repeat 1787 1804 * the operation in aggregate, and would be surprising to the 1788 1805 * user. 1789 1806 * 1790 - * As we have already dropped locks, it is safe to just loop and 1807 + * We drop and reacquire locks so it is safe to just loop and 1791 1808 * try again. We check for fatal signals in case we need exit 1792 1809 * early anyway. 1793 1810 */ ··· 1813 1796 ret = -EINTR; 1814 1797 break; 1815 1798 } 1799 + 1800 + /* Drop and reacquire lock to unwind race. */ 1801 + madvise_unlock(mm, behavior); 1802 + madvise_lock(mm, behavior); 1816 1803 continue; 1817 1804 } 1818 1805 if (ret < 0) 1819 1806 break; 1820 1807 iov_iter_advance(iter, iter_iov_len(iter)); 1821 1808 } 1809 + madvise_unlock(mm, behavior); 1822 1810 1823 1811 ret = (total_len - iov_iter_count(iter)) ? : ret; 1824 1812