Received: by 2002:a05:6358:16cc:b0:ea:6187:17c9 with SMTP id r12csp3895638rwl; Tue, 27 Dec 2022 17:39:32 -0800 (PST) X-Google-Smtp-Source: AMrXdXv894zzifq8fPYBSWj9uPglS99MInIlYCNAGIaZk5RuvfacS1GCm3OooigbASgUjq1sYUhs X-Received: by 2002:a05:6a20:1392:b0:b1:a867:79ca with SMTP id w18-20020a056a20139200b000b1a86779camr36000608pzh.10.1672191572122; Tue, 27 Dec 2022 17:39:32 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1672191572; cv=none; d=google.com; s=arc-20160816; b=vaveuetnAdZvymDT6p0aMoiH4Lvx6CdZS5mGy0zaJ51UGA2LAR/mZsJ96bX/erghuW pmnU6LIUczJghjngwh7/6xzSV3HK6lzkpzEawYrsP77y3jjikjplVAHIVWOtuLkBGuAW jt2IG6M94QtCBW5WcqOiqA/7+PRgHseoBOG07FszLlT1QivPHEONjDuKIvb/ItErwibW lQ7tCKz7O5MOdDX5RczDjEuJ/SvN/DpKJvkzpZKw15zukPCSHAQOqbdsmH2ms7aSGgSm wSz7ZOCsV5Xny8DPMmgYv5ZnPyDunNlstSCB3m8hZt/VuChO+KGnrI8P8AiaZabFJ+Th Um0g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :message-id:date:subject:cc:to:from:dkim-signature; bh=+GjBIcG1XK4MOTxEs5gCnUbmFtQjxi34Q8bMQCgOi+g=; b=cPst8X2ixetkRI+6laohrzWDRYnM+daP5x+Lhk1R4dFPOAb3M/4V7XUHnFnX5RF/wG S+cnREcbtM2/gl36Yeog7JvSYteLv/0DDe3fa9GeaolnegkZhixHjfylQtihWmtS3WDu kunCtHUrOkkj0g7jes/CCz0D5Lzu7IB+N2GvbEmRPewv0PgYG+6o4/N92GSULYMPDPgr tXhajksdLgBHX8skg5ckE/t162BDne3CZAFlBzJpR6GWBSsYR60EpyO/kTiXSFrpeBNJ VGBux0owtFW3OArM6W077h1f54WN5jyK8yahA69k7cCJrVik1A/tJo8yIlhBs44Z3t04 qUvA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=hKkaqU5o; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id p12-20020a65490c000000b00489a4cba354si16099989pgs.453.2022.12.27.17.39.23; Tue, 27 Dec 2022 17:39:32 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=hKkaqU5o; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230064AbiL1B3P (ORCPT + 65 others); Tue, 27 Dec 2022 20:29:15 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58568 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229475AbiL1B3O (ORCPT ); Tue, 27 Dec 2022 20:29:14 -0500 Received: from ams.source.kernel.org (ams.source.kernel.org [IPv6:2604:1380:4601:e00::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 21F67DF15; Tue, 27 Dec 2022 17:29:13 -0800 (PST) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ams.source.kernel.org (Postfix) with ESMTPS id A57D2B80FEC; Wed, 28 Dec 2022 01:29:11 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id B905CC433D2; Wed, 28 Dec 2022 01:29:09 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1672190950; bh=3XVIwz9Ohm85QPhk+zsQOi2XQO0+IAfPFOpLecTxMg0=; h=From:To:Cc:Subject:Date:From; b=hKkaqU5oJoC5WDr5ZPFVqhMzboiYc9mkt1GblQx3R3L6rpSJqAja5O+NLlk2MuppA gXQOuUvTWqG6KJWIYIcJSFkQgUxFBKhVgskJiAjO0sYxGEXKo/A4ci4wp/Yl/Nj+Cb jzeklfprYSsSHFv5+VbC6Z0u6taoVst5nFDyV2mm0itOc44wstERjnIM0b8tlnYt9i mfiBsn4N0ZWslLezaermTPArVLxYi1kmZiARJRvd4WbC/aTyfsjTsug+czx8161kcF agtfinvxgJI+dBy74z20AVTCGPDjDnaRVb0RAIuV7fcId33dDZLxs8Ro1I7ynE6+a3 arjxjrw/UNoYA== From: SeongJae Park To: akpm@linux-foundation.org Cc: alexander.h.duyck@linux.intel.com, paulmck@kernel.org, linux-mm@kvack.org, rcu@vger.kernel.org, linux-kernel@vger.kernel.org, SeongJae Park Subject: [PATCH v2] mm/page_reporting: replace rcu_access_pointer() with rcu_dereference_protected() Date: Wed, 28 Dec 2022 01:29:02 +0000 Message-Id: <20221228012902.1510901-1-sj@kernel.org> X-Mailer: git-send-email 2.25.1 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-7.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_HI, SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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 --- Changes from v1 (https://lore.kernel.org/linux-mm/20221227192158.2553-1-sj@kernel.org/) - Explicitly set the protection condition (Matthew Wilcox) mm/page_reporting.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/mm/page_reporting.c b/mm/page_reporting.c index 79a8554f024c..5c557a3e1423 100644 --- a/mm/page_reporting.c +++ b/mm/page_reporting.c @@ -356,7 +356,8 @@ 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, + lockdep_is_held(&page_reporting_order))) { err = -EBUSY; goto err_out; } @@ -401,7 +402,8 @@ void page_reporting_unregister(struct page_reporting_dev_info *prdev) { mutex_lock(&page_reporting_mutex); - if (rcu_access_pointer(pr_dev_info) == prdev) { + if (prdev == rcu_dereference_protected(pr_dev_info, + lockdep_is_held(&page_reporting_mutex))) { /* Disable page reporting notification */ RCU_INIT_POINTER(pr_dev_info, NULL); synchronize_rcu(); -- 2.25.1