* [PATCH] mm, swap: Use prandom_u32_max()
@ 2020-05-12 6:41 Huang Ying
2020-05-12 6:50 ` Michal Hocko
0 siblings, 1 reply; 5+ messages in thread
From: Huang Ying @ 2020-05-12 6:41 UTC (permalink / raw)
To: Andrew Morton
Cc: linux-mm, linux-kernel, Huang Ying, Michal Hocko, Minchan Kim,
Tim Chen, Hugh Dickins
To improve the code readability and get random number with higher
quality.
Signed-off-by: "Huang, Ying" <ying.huang@intel.com>
Cc: Michal Hocko <mhocko@suse.com>
Cc: Minchan Kim <minchan@kernel.org>
Cc: Tim Chen <tim.c.chen@linux.intel.com>
Cc: Hugh Dickins <hughd@google.com>
---
mm/swapfile.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/mm/swapfile.c b/mm/swapfile.c
index a0a123e59ce6..2ec8b21201d6 100644
--- a/mm/swapfile.c
+++ b/mm/swapfile.c
@@ -3220,7 +3220,7 @@ SYSCALL_DEFINE2(swapon, const char __user *, specialfile, int, swap_flags)
* select a random position to start with to help wear leveling
* SSD
*/
- p->cluster_next = 1 + (prandom_u32() % p->highest_bit);
+ p->cluster_next = 1 + prandom_u32_max(p->highest_bit);
nr_cluster = DIV_ROUND_UP(maxpages, SWAPFILE_CLUSTER);
cluster_info = kvcalloc(nr_cluster, sizeof(*cluster_info),
--
2.26.2
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH] mm, swap: Use prandom_u32_max()
2020-05-12 6:41 [PATCH] mm, swap: Use prandom_u32_max() Huang Ying
@ 2020-05-12 6:50 ` Michal Hocko
2020-05-12 7:14 ` Huang, Ying
0 siblings, 1 reply; 5+ messages in thread
From: Michal Hocko @ 2020-05-12 6:50 UTC (permalink / raw)
To: Huang Ying
Cc: Andrew Morton, linux-mm, linux-kernel, Minchan Kim, Tim Chen,
Hugh Dickins
On Tue 12-05-20 14:41:46, Huang Ying wrote:
> To improve the code readability and get random number with higher
> quality.
I understand the readability argument but why should prandom_u32_max
(which I was not aware of) provide a higher quality randomness?
> Signed-off-by: "Huang, Ying" <ying.huang@intel.com>
> Cc: Michal Hocko <mhocko@suse.com>
> Cc: Minchan Kim <minchan@kernel.org>
> Cc: Tim Chen <tim.c.chen@linux.intel.com>
> Cc: Hugh Dickins <hughd@google.com>
To the change itself
Acked-by: Michal Hocko <mhocko@suse.com>
> ---
> mm/swapfile.c | 2 +-
> 1 file changed, 1 insertion(+), 1 deletion(-)
>
> diff --git a/mm/swapfile.c b/mm/swapfile.c
> index a0a123e59ce6..2ec8b21201d6 100644
> --- a/mm/swapfile.c
> +++ b/mm/swapfile.c
> @@ -3220,7 +3220,7 @@ SYSCALL_DEFINE2(swapon, const char __user *, specialfile, int, swap_flags)
> * select a random position to start with to help wear leveling
> * SSD
> */
> - p->cluster_next = 1 + (prandom_u32() % p->highest_bit);
> + p->cluster_next = 1 + prandom_u32_max(p->highest_bit);
> nr_cluster = DIV_ROUND_UP(maxpages, SWAPFILE_CLUSTER);
>
> cluster_info = kvcalloc(nr_cluster, sizeof(*cluster_info),
> --
> 2.26.2
--
Michal Hocko
SUSE Labs
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH] mm, swap: Use prandom_u32_max()
2020-05-12 6:50 ` Michal Hocko
@ 2020-05-12 7:14 ` Huang, Ying
2020-05-12 7:32 ` Michal Hocko
0 siblings, 1 reply; 5+ messages in thread
From: Huang, Ying @ 2020-05-12 7:14 UTC (permalink / raw)
To: Michal Hocko
Cc: Andrew Morton, linux-mm, linux-kernel, Minchan Kim, Tim Chen,
Hugh Dickins
Michal Hocko <mhocko@kernel.org> writes:
> On Tue 12-05-20 14:41:46, Huang Ying wrote:
>> To improve the code readability and get random number with higher
>> quality.
>
> I understand the readability argument but why should prandom_u32_max
> (which I was not aware of) provide a higher quality randomness?
I am not expert on random number generator. I have heard about that the
randomness of the low order bits of some random number generator isn't
good enough. Anyway, by using the common implementation, the real
random number generator expert can fix the possible issue once for all
users.
>> Signed-off-by: "Huang, Ying" <ying.huang@intel.com>
>> Cc: Michal Hocko <mhocko@suse.com>
>> Cc: Minchan Kim <minchan@kernel.org>
>> Cc: Tim Chen <tim.c.chen@linux.intel.com>
>> Cc: Hugh Dickins <hughd@google.com>
>
> To the change itself
> Acked-by: Michal Hocko <mhocko@suse.com>
Thanks!
Best Regards,
Huang, Ying
>> ---
>> mm/swapfile.c | 2 +-
>> 1 file changed, 1 insertion(+), 1 deletion(-)
>>
>> diff --git a/mm/swapfile.c b/mm/swapfile.c
>> index a0a123e59ce6..2ec8b21201d6 100644
>> --- a/mm/swapfile.c
>> +++ b/mm/swapfile.c
>> @@ -3220,7 +3220,7 @@ SYSCALL_DEFINE2(swapon, const char __user *, specialfile, int, swap_flags)
>> * select a random position to start with to help wear leveling
>> * SSD
>> */
>> - p->cluster_next = 1 + (prandom_u32() % p->highest_bit);
>> + p->cluster_next = 1 + prandom_u32_max(p->highest_bit);
>> nr_cluster = DIV_ROUND_UP(maxpages, SWAPFILE_CLUSTER);
>>
>> cluster_info = kvcalloc(nr_cluster, sizeof(*cluster_info),
>> --
>> 2.26.2
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH] mm, swap: Use prandom_u32_max()
2020-05-12 7:14 ` Huang, Ying
@ 2020-05-12 7:32 ` Michal Hocko
2020-05-12 7:38 ` Huang, Ying
0 siblings, 1 reply; 5+ messages in thread
From: Michal Hocko @ 2020-05-12 7:32 UTC (permalink / raw)
To: Huang, Ying
Cc: Andrew Morton, linux-mm, linux-kernel, Minchan Kim, Tim Chen,
Hugh Dickins
On Tue 12-05-20 15:14:46, Huang, Ying wrote:
> Michal Hocko <mhocko@kernel.org> writes:
>
> > On Tue 12-05-20 14:41:46, Huang Ying wrote:
> >> To improve the code readability and get random number with higher
> >> quality.
> >
> > I understand the readability argument but why should prandom_u32_max
> > (which I was not aware of) provide a higher quality randomness?
>
> I am not expert on random number generator. I have heard about that the
> randomness of the low order bits of some random number generator isn't
> good enough. Anyway, by using the common implementation, the real
> random number generator expert can fix the possible issue once for all
> users.
Please drop the quality argument if you cannot really justify it. This
will likely just confuse future readers the same way it confused me
here. Because prandom_u32_max uses the same source of randomness the
only difference is the way how modulo vs. u64 overflow arithmetic is
used for distributing values. I am not aware the later would be
a way to achieve a higher quality randomness. If the interval
distribution is better with the later then it would be great to have it
documented.
> >> Signed-off-by: "Huang, Ying" <ying.huang@intel.com>
> >> Cc: Michal Hocko <mhocko@suse.com>
> >> Cc: Minchan Kim <minchan@kernel.org>
> >> Cc: Tim Chen <tim.c.chen@linux.intel.com>
> >> Cc: Hugh Dickins <hughd@google.com>
> >
> > To the change itself
> > Acked-by: Michal Hocko <mhocko@suse.com>
>
> Thanks!
>
> Best Regards,
> Huang, Ying
>
> >> ---
> >> mm/swapfile.c | 2 +-
> >> 1 file changed, 1 insertion(+), 1 deletion(-)
> >>
> >> diff --git a/mm/swapfile.c b/mm/swapfile.c
> >> index a0a123e59ce6..2ec8b21201d6 100644
> >> --- a/mm/swapfile.c
> >> +++ b/mm/swapfile.c
> >> @@ -3220,7 +3220,7 @@ SYSCALL_DEFINE2(swapon, const char __user *, specialfile, int, swap_flags)
> >> * select a random position to start with to help wear leveling
> >> * SSD
> >> */
> >> - p->cluster_next = 1 + (prandom_u32() % p->highest_bit);
> >> + p->cluster_next = 1 + prandom_u32_max(p->highest_bit);
> >> nr_cluster = DIV_ROUND_UP(maxpages, SWAPFILE_CLUSTER);
> >>
> >> cluster_info = kvcalloc(nr_cluster, sizeof(*cluster_info),
> >> --
> >> 2.26.2
--
Michal Hocko
SUSE Labs
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH] mm, swap: Use prandom_u32_max()
2020-05-12 7:32 ` Michal Hocko
@ 2020-05-12 7:38 ` Huang, Ying
0 siblings, 0 replies; 5+ messages in thread
From: Huang, Ying @ 2020-05-12 7:38 UTC (permalink / raw)
To: Michal Hocko
Cc: Andrew Morton, linux-mm, linux-kernel, Minchan Kim, Tim Chen,
Hugh Dickins
Michal Hocko <mhocko@kernel.org> writes:
> On Tue 12-05-20 15:14:46, Huang, Ying wrote:
>> Michal Hocko <mhocko@kernel.org> writes:
>>
>> > On Tue 12-05-20 14:41:46, Huang Ying wrote:
>> >> To improve the code readability and get random number with higher
>> >> quality.
>> >
>> > I understand the readability argument but why should prandom_u32_max
>> > (which I was not aware of) provide a higher quality randomness?
>>
>> I am not expert on random number generator. I have heard about that the
>> randomness of the low order bits of some random number generator isn't
>> good enough. Anyway, by using the common implementation, the real
>> random number generator expert can fix the possible issue once for all
>> users.
>
> Please drop the quality argument if you cannot really justify it. This
> will likely just confuse future readers the same way it confused me
> here. Because prandom_u32_max uses the same source of randomness the
> only difference is the way how modulo vs. u64 overflow arithmetic is
> used for distributing values. I am not aware the later would be
> a way to achieve a higher quality randomness. If the interval
> distribution is better with the later then it would be great to have it
> documented.
OK. Fair enough.
Best Regards,
Huang, Ying
>> >> Signed-off-by: "Huang, Ying" <ying.huang@intel.com>
>> >> Cc: Michal Hocko <mhocko@suse.com>
>> >> Cc: Minchan Kim <minchan@kernel.org>
>> >> Cc: Tim Chen <tim.c.chen@linux.intel.com>
>> >> Cc: Hugh Dickins <hughd@google.com>
>> >
>> > To the change itself
>> > Acked-by: Michal Hocko <mhocko@suse.com>
>>
>> Thanks!
>>
>> Best Regards,
>> Huang, Ying
>>
>> >> ---
>> >> mm/swapfile.c | 2 +-
>> >> 1 file changed, 1 insertion(+), 1 deletion(-)
>> >>
>> >> diff --git a/mm/swapfile.c b/mm/swapfile.c
>> >> index a0a123e59ce6..2ec8b21201d6 100644
>> >> --- a/mm/swapfile.c
>> >> +++ b/mm/swapfile.c
>> >> @@ -3220,7 +3220,7 @@ SYSCALL_DEFINE2(swapon, const char __user *, specialfile, int, swap_flags)
>> >> * select a random position to start with to help wear leveling
>> >> * SSD
>> >> */
>> >> - p->cluster_next = 1 + (prandom_u32() % p->highest_bit);
>> >> + p->cluster_next = 1 + prandom_u32_max(p->highest_bit);
>> >> nr_cluster = DIV_ROUND_UP(maxpages, SWAPFILE_CLUSTER);
>> >>
>> >> cluster_info = kvcalloc(nr_cluster, sizeof(*cluster_info),
>> >> --
>> >> 2.26.2
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2020-05-12 7:38 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-05-12 6:41 [PATCH] mm, swap: Use prandom_u32_max() Huang Ying
2020-05-12 6:50 ` Michal Hocko
2020-05-12 7:14 ` Huang, Ying
2020-05-12 7:32 ` Michal Hocko
2020-05-12 7:38 ` Huang, Ying
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox