From patchwork Wed Dec 28 01:29:02 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: SeongJae Park X-Patchwork-Id: 13082669 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 57F12C10F1B for ; Wed, 28 Dec 2022 01:29:14 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id D22F58E0002; Tue, 27 Dec 2022 20:29:13 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id CD24B8E0001; Tue, 27 Dec 2022 20:29:13 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id B9A588E0002; Tue, 27 Dec 2022 20:29:13 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id A95EA8E0001 for ; Tue, 27 Dec 2022 20:29:13 -0500 (EST) Received: from smtpin19.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 7A79FAB064 for ; Wed, 28 Dec 2022 01:29:13 +0000 (UTC) X-FDA: 80289981786.19.57DCBA2 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by imf27.hostedemail.com (Postfix) with ESMTP id 0E78940005 for ; Wed, 28 Dec 2022 01:29:11 +0000 (UTC) Authentication-Results: imf27.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=hKkaqU5o; dmarc=pass (policy=none) header.from=kernel.org; spf=pass (imf27.hostedemail.com: domain of sj@kernel.org designates 139.178.84.217 as permitted sender) smtp.mailfrom=sj@kernel.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1672190952; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:references:dkim-signature; bh=+GjBIcG1XK4MOTxEs5gCnUbmFtQjxi34Q8bMQCgOi+g=; b=QFZlmp+VNHYNzOR7RokKSMU+o4ViOx1Jc+7ppXFaKLqjTrZtIlu+e5nSTgGhtayeoh//rW BiOSppJmFWyufunNW8MSrQLrZtjhDx2VcJNenctYqbCxSe/yR3PCZPEEXT6QT8ZrlAdpD9 ao+LtsbKiiU4zk+GXsm2V6/CdLwW2rQ= ARC-Authentication-Results: i=1; imf27.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=hKkaqU5o; dmarc=pass (policy=none) header.from=kernel.org; spf=pass (imf27.hostedemail.com: domain of sj@kernel.org designates 139.178.84.217 as permitted sender) smtp.mailfrom=sj@kernel.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1672190952; a=rsa-sha256; cv=none; b=4aQA+5ThfZNtLh+WStGPdtwkYVUP29rv3IFmcUYxs5MzV1R+4kP204Llx83gHa82j807jq lNI5ZDCOS0+Ce1iiLry43v/ru7qpGp17eIsM4I1ToGnEpiFT4B1y5KgJ5WM+XoN5tH70o/ sJTw5Dczwo/3hm7J6XgzOUwfS2tKPq4= 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 dfw.source.kernel.org (Postfix) with ESMTPS id F3807611F9; Wed, 28 Dec 2022 01:29:10 +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 X-Rspamd-Queue-Id: 0E78940005 X-Rspamd-Server: rspam09 X-Rspam-User: X-Stat-Signature: pjchc4bkh3jkyundh45sy5ceombk4ja9 X-HE-Tag: 1672190951-773216 X-HE-Meta: U2FsdGVkX18QeaWYsOozuXN+j/3dd8PUqcUrCkCsrK3ea2K68FDGSeIv3EqTU8bRK4mEBt/vzK86JkRvhCr2owaXrNxzVJr303bMK1SwCIdy0RrD4N3EQ0CzoqcLmGQsON8y1RPvQm2emJv6Ylz9a85VcywsdRopneVDkiqLOv0IPabtED03uadojBozXm4NbH+HDzJwu/4liO4PnRy8PMDAufRIURouysrkQorRo4xRFd++FzxDQEtMU6SSoCIUiWrqQegw9ggoZPalEfzMW651RK1SZCF8BJ96EJSJIccARnpGD6px0bWwd0MBWLi2JlAqeYQaYpd4+JwpA4FHhrmwGc52Qk3fb66mKphX940shAbnEqiRx7qFL1UsVNp5JU+Gr0K78oxc4W6PfexNuhePIfl9kifxvjTv5k3qLHFyk0xY/kSfYhr6WyTS5CBcoFwvInbZ+p9hiB+a24sDQna3K19xdwNJ/GTMlsAtbVdi6d/c8VoIG4H7GMvAUxOMH8yjoX2fUpH1ZxegHPu4qdoxZ3l0/B46zFe/nNOHOyVm2kISheITntIZJkt84c7WyPtMF66qYyZV7QnqNnpqjosTG+0aTF9+YRAzmmOBL4blAvf+q4LOVTHt5/0HxlxYyuguOZA1osZMBcpdh0IVZHNvT7E1wlI41c3kJo9scZKiiXouEq7JTaZC8hJg7hhsDGgb8WunV5zQbz0wu+Q6xEtOBHn4xcAGOje6R7+XAKC9HXrsOthpLzNwjtzelK5f3SZl1GIDeHT8CNKVT1hP+rPUNY4ZfkubgaA7XLgHst0ASnAmbE/egZXsppfaTn9s7W0m98FQXJY8q97wDpc5B+MTVRxqjHbJTbtf9Pv6j65tpwZrDOczlPpQ7bZB5qMzzikLUp6F7Dkmj6fBPkWlMCUoyxsLf5or6MH4KzWQ2mQGo4ONRPeMwkwk5lHHg7m+Vpz3QeKqZdM+HUMjMxf o+eCCoqI TSkyyFQeshLUPMhEll1cCqiF5GggkSHiWfo1+cr5/+tHKVn0+CEO7745g3W3bUumsC9B07aBwitH99Cno+WHQeHJODIJHmVzTUrUEooujRAvADXaKBiE8lON7zw== X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: 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();