From patchwork Wed Dec 28 17:59:42 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: SeongJae Park X-Patchwork-Id: 13083089 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 0DDCDC4167B for ; Wed, 28 Dec 2022 17:59:49 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 696AD8E0002; Wed, 28 Dec 2022 12:59:49 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 645918E0001; Wed, 28 Dec 2022 12:59:49 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 4E8A38E0002; Wed, 28 Dec 2022 12:59:49 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id 3B4328E0001 for ; Wed, 28 Dec 2022 12:59:49 -0500 (EST) Received: from smtpin28.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id 17CFD40C15 for ; Wed, 28 Dec 2022 17:59:49 +0000 (UTC) X-FDA: 80292478098.28.0976394 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by imf24.hostedemail.com (Postfix) with ESMTP id 8A5B618000C for ; Wed, 28 Dec 2022 17:59:47 +0000 (UTC) Authentication-Results: imf24.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=dnUZukkc; spf=pass (imf24.hostedemail.com: domain of sj@kernel.org designates 139.178.84.217 as permitted sender) smtp.mailfrom=sj@kernel.org; dmarc=pass (policy=none) header.from=kernel.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1672250387; 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=uosc9JyuCncGg9dcpXmdMKNTZr8K9ypZx7/ArG07/QY=; b=EpTfPxTpCzf/gmNE38v01GAI1+yiBHkQUuZYDSA+63m3SUg+AS9atP0sync0T6hM9Y045I nseKwqcFWsZkXOGL3KA5bhQFMbI3aJoI740FYBwxVqnKRSKxYdDERGbVyb5Fxx7X3X4sYI jfXqfVfhjkai4T168zbuSUZcLt6Nm2k= ARC-Authentication-Results: i=1; imf24.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=dnUZukkc; spf=pass (imf24.hostedemail.com: domain of sj@kernel.org designates 139.178.84.217 as permitted sender) smtp.mailfrom=sj@kernel.org; dmarc=pass (policy=none) header.from=kernel.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1672250387; a=rsa-sha256; cv=none; b=uFXNqq7YDS1C/O9w6Kt3HygN6iBu9POZZlnyB/WtWuBFvG2A+VK7I8ShTLJ0UhBHz0FKVU KJBf1wa+aBPPC5qqLyKc6xJr33vGESxYs/yzYW039oySlm/rmcVn1IzyDFGIRGNj7H/2w1 AIrUQZrX6xkqgDPdKn0CSmcAJGHSNAs= 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 9328F61582; Wed, 28 Dec 2022 17:59:46 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 5E0E9C433F0; Wed, 28 Dec 2022 17:59:45 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1672250386; bh=XN9PcsyPPRLwqqgJ2SzuIm7xkFHpLYHw7zI07sgsjnc=; h=From:To:Cc:Subject:Date:From; b=dnUZukkc3qKrGLW6R3tkbIKWiwATzVWetlE/iWL6Rx7JJJPCfkJwdj4arTf88CmpJ 7x6mJ27NRKW3BVc8h6cFtsmTUDQEXuUfiUfOdPZ5rmDjibNxqU1i7mqALaVvVtm14P C4hIDOdP5gRo7cHXKrqo3WL91/3iPq88gTKW4496/wH26+Uj7SSYLjSnwfEfotIkmX X7WsfiIvsHrrWDi5HBibzsJKnpQ9s4dlQFlaxGf8wxHrRwf4EyT6wmpRaeLOllAv1a 2QHnGEQiBAKMl0ORqgWjckrysWHXHC5zj+/WzOnmuJsMK2gnbLzCapjL8Qk105IqjH sL4vkh84YjfGQ== From: SeongJae Park To: akpm@linux-foundation.org Cc: SeongJae Park , alexander.h.duyck@linux.intel.com, willy@infradead.org, linmiaohe@huawei.com, linux-mm@kvack.org, rcu@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH v3] mm/page_reporting: replace rcu_access_pointer() with rcu_dereference_protected() Date: Wed, 28 Dec 2022 17:59:42 +0000 Message-Id: <20221228175942.149491-1-sj@kernel.org> X-Mailer: git-send-email 2.25.1 MIME-Version: 1.0 X-Rspamd-Queue-Id: 8A5B618000C X-Stat-Signature: qdaaxnbxigxka1gcq91dtnwt8dtzrswe X-Rspam-User: X-Rspamd-Server: rspam08 X-HE-Tag: 1672250387-316239 X-HE-Meta: U2FsdGVkX19Ii0NItVq5qPMXg/hJaYLOZuFkbK2t6YnTZOo/2h328rFBx9+nMp3ZOUIPwmNPO9Yr/JDDX8XPk6AFrLlskj7z7p0lwUktQySmm3P+Gr7q3+sQ7kH53vzguWFbPHsou65IYiaqG2YvBNj3TGdICtmkZjCV8H8QIXMzHw5OgdRBiENOvVGrEZIHmV0QEVNSojO+i9oyMylsvcBhZLuawzaJOzo7D/RVocUl69gaMz/4k7EQ2IC6T7WkbnvhsSA408Qy1ToJ/5upVd7eRfuc5CBpc0FX/np8PqkCp5MpZbXVELHecDZbZoKKYEuEYJC0qhAVcJNcrJcLBGE2f9Bj44M2N+iQJNGP7gQdzdmJpXIqJffzegVihNkQ95mdT13P1nu7+oQ9CpsNPWDm+ieWSMMU2FvkQByVJwmcDJX/JhJP7d3zreAkI2IkeJC+rQcwuASDA++nRFeuuR+HrR97Le5PTEiVlkRT0I2VShlkovFFJ3q07Nc5nnsyJkAhC1mBGqBW8e9v//nl+0em9Xsr/5GO98Ex9or2AJZJZlOr95XAnyR2xyG+JpFodrXdbxWYEeYMwrkLM4kWC56DIs0RsJooif9x64Hv1P78GAT++xUEnu1bfD48QFtAyJWcY2M8hiAxA6wXuKxnPNrjAfo96pdR+jE0NR5Yglyyv7CQ8ztY9sAc77vWi19JGjKFpkGNxi224SC6zshkAyKY4bAJdi6bY/V59Cc/ldo7dbKuj0945OVnAIeyGApmouBPzyzQM3p9GImIl/nU1cXFWKGlTv1fWQDu/Aw+k1r6J95+LZUl8xR5hKHMdzSrJ9Y29WhSPZwAE5o8GrT9rSAdL88j17pZPC2ADJzPMmSx3ojXaVKdIVDa+BLLhrHyx9cdmhkJ1n3OU+mmbIX83pgPGbXGMR3+xUaEstGLw9B2yR5SFOYPlYdE3HH9J722RulXq71sblm+4fkGiY6 TuZjC3FM oIZYAHKmaLC815wNvWnIWjDwJu62Os6yEO1T2mdndp2MP2Ozzbd6qIMoS2x9+agyuqWGyg46ASdtM7beEabBUgxZ7FfSBN0f9nznGFtljvZWmdchhklBS/u1ajw== 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 fetching it using rcu_access_pointer(), but it fetches the pointer while concurrent updates to the pointer is avoided by holding the update side lock, page_reporting_mutex. In the case, rcu_dereference_protected() should be used instead 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(). Fixes: 36e66c554b5c ("mm: introduce Reported pages") Signed-off-by: SeongJae Park --- Changes from v2 (https://lore.kernel.org/linux-mm/20221228012902.1510901-1-sj@kernel.org/) - Fix typos (Miaohe Lin) - Add Fixes: tag 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..c65813a9dc78 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_mutex))) { 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();