linux-mm.kvack.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] mm/page_reporting: replace rcu_access_pointer() with rcu_dereference_protected()
@ 2022-12-27 19:21 SeongJae Park
  2022-12-27 19:55 ` Matthew Wilcox
  0 siblings, 1 reply; 3+ messages in thread
From: SeongJae Park @ 2022-12-27 19:21 UTC (permalink / raw)
  To: akpm
  Cc: alexander.h.duyck, paulmck, linux-mm, rcu, linux-kernel, SeongJae Park

Page reporting fetches pr_dev_info using rcu_access_pointer(), which is
for safely fetching a pointer that will not be dereferenced but could
concurrently updated.  The code indeed does not dereference pr_dev_info
after fetcing it using rcu_access_pointer(), but it fetches the pointer
while concurrent updtes to the pointer is avoided by holding the update
side lock, page_reporting_mutex.

In the case, rcu_dereference_protected() is recommended because it
provides better readability and performance on some cases, as
rcu_dereference_protected() avoids use of READ_ONCE().  Replace the
rcu_access_pointer() calls with rcu_dereference_protected().

Signed-off-by: SeongJae Park <sj@kernel.org>
---
 mm/page_reporting.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/mm/page_reporting.c b/mm/page_reporting.c
index 79a8554f024c..079e0ebe1fb7 100644
--- a/mm/page_reporting.c
+++ b/mm/page_reporting.c
@@ -356,7 +356,7 @@ int page_reporting_register(struct page_reporting_dev_info *prdev)
 	mutex_lock(&page_reporting_mutex);
 
 	/* nothing to do if already in use */
-	if (rcu_access_pointer(pr_dev_info)) {
+	if (rcu_dereference_protected(pr_dev_info, true)) {
 		err = -EBUSY;
 		goto err_out;
 	}
@@ -401,7 +401,7 @@ void page_reporting_unregister(struct page_reporting_dev_info *prdev)
 {
 	mutex_lock(&page_reporting_mutex);
 
-	if (rcu_access_pointer(pr_dev_info) == prdev) {
+	if (rcu_dereference_protected(pr_dev_info, true) == prdev) {
 		/* Disable page reporting notification */
 		RCU_INIT_POINTER(pr_dev_info, NULL);
 		synchronize_rcu();
-- 
2.25.1



^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH] mm/page_reporting: replace rcu_access_pointer() with rcu_dereference_protected()
  2022-12-27 19:21 [PATCH] mm/page_reporting: replace rcu_access_pointer() with rcu_dereference_protected() SeongJae Park
@ 2022-12-27 19:55 ` Matthew Wilcox
  2022-12-27 19:56   ` SeongJae Park
  0 siblings, 1 reply; 3+ messages in thread
From: Matthew Wilcox @ 2022-12-27 19:55 UTC (permalink / raw)
  To: SeongJae Park
  Cc: akpm, alexander.h.duyck, paulmck, linux-mm, rcu, linux-kernel

On Tue, Dec 27, 2022 at 07:21:58PM +0000, SeongJae Park wrote:
> +++ b/mm/page_reporting.c
> @@ -356,7 +356,7 @@ int page_reporting_register(struct page_reporting_dev_info *prdev)
>  	mutex_lock(&page_reporting_mutex);
>  
>  	/* nothing to do if already in use */
> -	if (rcu_access_pointer(pr_dev_info)) {
> +	if (rcu_dereference_protected(pr_dev_info, true)) {

Pretty sure that passing a bare 'true' is an antipattern.
Instead, document _what_ lock protects us, ie:

	if (rcu_dereference_protected(pr_dev_info,
				      lockdep_is_held(&page_reporting_mutex))) {

Obviously, we took it just one line up, but if code moves around, it
may save us.


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] mm/page_reporting: replace rcu_access_pointer() with rcu_dereference_protected()
  2022-12-27 19:55 ` Matthew Wilcox
@ 2022-12-27 19:56   ` SeongJae Park
  0 siblings, 0 replies; 3+ messages in thread
From: SeongJae Park @ 2022-12-27 19:56 UTC (permalink / raw)
  To: Matthew Wilcox
  Cc: SeongJae Park, akpm, alexander.h.duyck, paulmck, linux-mm, rcu,
	linux-kernel

On Tue, 27 Dec 2022 19:55:09 +0000 Matthew Wilcox <willy@infradead.org> wrote:

> On Tue, Dec 27, 2022 at 07:21:58PM +0000, SeongJae Park wrote:
> > +++ b/mm/page_reporting.c
> > @@ -356,7 +356,7 @@ int page_reporting_register(struct page_reporting_dev_info *prdev)
> >  	mutex_lock(&page_reporting_mutex);
> >  
> >  	/* nothing to do if already in use */
> > -	if (rcu_access_pointer(pr_dev_info)) {
> > +	if (rcu_dereference_protected(pr_dev_info, true)) {
> 
> Pretty sure that passing a bare 'true' is an antipattern.
> Instead, document _what_ lock protects us, ie:
> 
> 	if (rcu_dereference_protected(pr_dev_info,
> 				      lockdep_is_held(&page_reporting_mutex))) {
> 
> Obviously, we took it just one line up, but if code moves around, it
> may save us.

Good point, agreed.  Will do so in the next version.


Thanks,
SJ


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2022-12-27 19:57 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-12-27 19:21 [PATCH] mm/page_reporting: replace rcu_access_pointer() with rcu_dereference_protected() SeongJae Park
2022-12-27 19:55 ` Matthew Wilcox
2022-12-27 19:56   ` SeongJae Park

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).