History

random: move randomize_page() into mm where it belongs

randomize_page is an mm function. It is documented like one. It contains the history of one. It has the naming convention of one. It looks just like another very similar function in mm, randomize_stack_top(). And it has always been maintained and updated by mm people. There is no need for it to be in random.c. In the "which shape does not look like the other ones" test, pointing to randomize_page() is correct. So move randomize_page() into mm/util.c, right next to the similar randomize_stack_top() function. This commit contains no actual code changes.

Jason A. Donenfeld committed 4 years ago

Commit 5ad7dd81 parent 6701de64 files · +33 −34Browse files

Unified · Split

4 files
  1. drivers/char/random.c +0 −32
  2. include/linux/mm.h +1 −0
  3. include/linux/random.h +0 −2
  4. mm/util.c +32 −0
drivers/char/random.c+0−32
@@ -573,38 +573,6 @@ int __cold random_prepare_cpu(unsigned int cpu) · 572 unmodified lines
573573 }
574574 #endif
575575
576−/**
577− * randomize_page - Generate a random, page aligned address
578− * @start: The smallest acceptable address the caller will take.
579− * @range: The size of the area, starting at @start, within which the
580− * random address must fall.
581− *
582− * If @start + @range would overflow, @range is capped.
583− *
584− * NOTE: Historical use of randomize_range, which this replaces, presumed that
585− * @start was already page aligned. We now align it regardless.
586− *
587− * Return: A page aligned address within [start, start + range). On error,
588− * @start is returned.
589− */
590−unsigned long randomize_page(unsigned long start, unsigned long range)
591−{
592− if (!PAGE_ALIGNED(start)) {
593− range -= PAGE_ALIGN(start) - start;
594− start = PAGE_ALIGN(start);
595− }
596−
597− if (start > ULONG_MAX - range)
598− range = ULONG_MAX - start;
599−
600− range >>= PAGE_SHIFT;
601−
602− if (range == 0)
603− return start;
604−
605− return start + (get_random_long() % range << PAGE_SHIFT);
606−}
607−
608576
609577 /**********************************************************************
610578 *
include/linux/mm.h+1−0
@@ -2677,6 +2677,7 @@ extern int install_special_mapping(struct mm_struct *mm, · 2676 unmodified lines
26772677 unsigned long flags, struct page **pages);
26782678
26792679 unsigned long randomize_stack_top(unsigned long stack_top);
2680+unsigned long randomize_page(unsigned long start, unsigned long range);
26802681
26812682 extern unsigned long get_unmapped_area(struct file *, unsigned long, unsigned long, unsigned long, unsigned long);
26822683
include/linux/random.h+0−2
@@ -72,8 +72,6 @@ static inline unsigned long get_random_canary(void) · 71 unmodified lines
7272 return get_random_long() & CANARY_MASK;
7373 }
7474
75−unsigned long randomize_page(unsigned long start, unsigned long range);
76−
7775 int __init random_init(const char *command_line);
7876 bool rng_is_initialized(void);
7977 bool rng_has_arch_random(void);
mm/util.c+32−0
@@ -343,6 +343,38 @@ unsigned long randomize_stack_top(unsigned long stack_top) · 342 unmodified lines
343343 #endif
344344 }
345345
346+/**
347+ * randomize_page - Generate a random, page aligned address
348+ * @start: The smallest acceptable address the caller will take.
349+ * @range: The size of the area, starting at @start, within which the
350+ * random address must fall.
351+ *
352+ * If @start + @range would overflow, @range is capped.
353+ *
354+ * NOTE: Historical use of randomize_range, which this replaces, presumed that
355+ * @start was already page aligned. We now align it regardless.
356+ *
357+ * Return: A page aligned address within [start, start + range). On error,
358+ * @start is returned.
359+ */
360+unsigned long randomize_page(unsigned long start, unsigned long range)
361+{
362+ if (!PAGE_ALIGNED(start)) {
363+ range -= PAGE_ALIGN(start) - start;
364+ start = PAGE_ALIGN(start);
365+ }
366+
367+ if (start > ULONG_MAX - range)
368+ range = ULONG_MAX - start;
369+
370+ range >>= PAGE_SHIFT;
371+
372+ if (range == 0)
373+ return start;
374+
375+ return start + (get_random_long() % range << PAGE_SHIFT);
376+}
377+
346378 #ifdef CONFIG_ARCH_WANT_DEFAULT_TOPDOWN_MMAP_LAYOUT
347379 unsigned long arch_randomize_brk(struct mm_struct *mm)
348380 {