Received: by 2002:a05:6358:3188:b0:123:57c1:9b43 with SMTP id q8csp358525rwd; Mon, 12 Jun 2023 14:56:13 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ7yojGl51GYkubnHfRc92DAebBUbXwLKjEsPf9vMJ8c6KmWfTABmY22k61d0vLx+ACoWYFp X-Received: by 2002:a17:907:8a21:b0:97d:783e:df8b with SMTP id sc33-20020a1709078a2100b0097d783edf8bmr8565806ejc.73.1686606972770; Mon, 12 Jun 2023 14:56:12 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1686606972; cv=none; d=google.com; s=arc-20160816; b=lFeLOB+/uMloJWpQ2OrpusQoQUaYPKHUVdhlpgboX9bsYNXCRYqulfiUEuZCwZi4qV R/73EXfrJKa3FhHc+PUh+7G8mh8vmE9FO2EC8+vkDqGhUwYeqgmQp9d8Wc2q7MSmzyG7 qaErlcYJOWsl2z5uTPXlRkDi+n2kWsDUBv9ZEIhegwpH6X4qP4IT73b/WwKAg+Pm66qu /iMAcyE0JfyJHx4E9wgUUVhHYF1lEoYTVj944A9axGLE3+Dn/IccOi9k0IaytIDgtYhM 5Klfdxu+XuYnj8aXn3aQpZFZl3SAKJbjHSMbdANaK92fzU/uL7tr9iW0EQPeW7AwDUUa jGYA== 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 :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=35s0wI4i9zSZNHB8vRdbzCTKxCrc/UuNrjJTeVELBVQ=; b=1KHDM1MQwljqJH6vifftNg1MmW6S1W/1u3pD/exI+qW9XH96ccR1+suDQOQTnWblZ2 WZNdTamGRYB4vs9McJdG1kj/AuGwuIscYJ68MF9CMlvYqi7d6pgG6sUdUsf8tZGOCWB6 7aH95KhGF852CyHQiQybn4/6h3mTAUPbgJoJQiZzQNDCk+YDZ5bxyLdb9QS/uBYwiJ4h Rxt1h2eI5anPX+bbrTLTxvdOhSUfyGjgSEANPGr7ZIzrpuTVh+dycCwHomo8kzn7mA46 CienPtNmffnOeRI1/QKTPHsvQCr7OgCxuFImPmPIHEQp7eVugf1P9bgqdfGTT4cSyPKg m7TQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=dacuBPoS; 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 y20-20020a170906471400b0097e2a90d223si3992008ejq.720.2023.06.12.14.55.48; Mon, 12 Jun 2023 14:56:12 -0700 (PDT) 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=dacuBPoS; 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 S233876AbjFLV2T (ORCPT + 99 others); Mon, 12 Jun 2023 17:28:19 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43038 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233480AbjFLV1i (ORCPT ); Mon, 12 Jun 2023 17:27:38 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [IPv6:2604:1380:4641:c500::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 808E51B8 for ; Mon, 12 Jun 2023 14:27:28 -0700 (PDT) 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 16D3262277 for ; Mon, 12 Jun 2023 21:27:28 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 9BE59C433D2; Mon, 12 Jun 2023 21:27:24 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1686605247; bh=7LfNjfS4Zf4U5lMlQ2I09BVw2IXRjySt/jv0/P+rlX4=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=dacuBPoSFVDZwmysWY0+sJX2kCpLfzlOADMPWlPI1SXhY5oymB/CjE1k3GxRDWEvy +K68RJCTIWgm2AXWaHWcUrZeQzQSnCsu+OTNcTTgAHVbgq63G39+XV47Am+tCO4Y0Y xc9hxFjwncmMg/Twg/IsjTGVO87I/4PrZUqrl48TttOWMtN2d6vRlZoskBNffkVCJZ Z1GZRN5dZffADCi0q8aVKxHHhkX5HLiwjCbIGku1nIoQLs2+lxofK9jefoj0jetdPt twTY2rj6QVhglYyTNio8xUlTVQDpyJHbHRIi5Jb7iiTi/LUeoY3c/7Mk+b5X/cfkQS IAWgPT5j0O17Q== From: SeongJae Park To: Ryan Roberts Cc: "SeongJae Park" , "Matthew Wilcox (Oracle)" , "Kirill A. Shutemov" , "Mike Rapoport" , "Yu Zhao" , "Jason Gunthorpe" , "David Airlie" , "Daniel Vetter" , "Dimitri Sivanich" , "Alex Williamson" , "Oleksandr Tyshchenko" , "Alexander Viro" , "Christian Brauner" , "Mike Kravetz" , "Muchun Song" , "Mark Rutland" , "Alexander Shishkin" , "Jiri Olsa" , "Namhyung Kim" , "Ian Rogers" , "Adrian Hunter" , =?UTF-8?q?J=C3=A9r=C3=B4me=20Glisse?= , "Andrey Ryabinin" , "Alexander Potapenko" , "Andrey Konovalov" , "Dmitry Vyukov" , "Vincenzo Frascino" , "Johannes Weiner" , "Michal Hocko" , "Roman Gushchin" , "Shakeel Butt" , "Naoya Horiguchi" , "Miaohe Lin" , "Pasha Tatashin" , "Uladzislau Rezki" , "Christoph Hellwig" , "Lorenzo Stoakes" , linux-kernel@vger.kernel.org, linux-mm@kvack.org, damon@lists.linux.dev, kernel test robot Subject: Re: [PATCH v3 3/3] mm: ptep_get() conversion Date: Mon, 12 Jun 2023 21:27:23 +0000 Message-Id: <20230612212723.196693-1-sj@kernel.org> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230612151545.3317766-4-ryan.roberts@arm.com> References: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-4.4 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE 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 On Mon, 12 Jun 2023 16:15:45 +0100 Ryan Roberts wrote: > Convert all instances of direct pte_t* dereferencing to instead use > ptep_get() helper. This means that by default, the accesses change from > a C dereference to a READ_ONCE(). This is technically the correct thing > to do since where pgtables are modified by HW (for access/dirty) they > are volatile and therefore we should always ensure READ_ONCE() > semantics. > > But more importantly, by always using the helper, it can be overridden > by the architecture to fully encapsulate the contents of the pte. Arch > code is deliberately not converted, as the arch code knows best. It is > intended that arch code (arm64) will override the default with its own > implementation that can (e.g.) hide certain bits from the core code, or > determine young/dirty status by mixing in state from another source. > > Conversion was done using Coccinelle: > > ---- > > // $ make coccicheck \ > // COCCI=ptepget.cocci \ > // SPFLAGS="--include-headers" \ > // MODE=patch > > virtual patch > > @ depends on patch @ > pte_t *v; > @@ > > - *v > + ptep_get(v) > > ---- > > Then reviewed and hand-edited to avoid multiple unnecessary calls to > ptep_get(), instead opting to store the result of a single call in a > variable, where it is correct to do so. This aims to negate any cost of > READ_ONCE() and will benefit arch-overrides that may be more complex. > > Included is a fix for an issue in an earlier version of this patch that > was pointed out by kernel test robot. The issue arose because config > MMU=n elides definition of the ptep helper functions, including > ptep_get(). HUGETLB_PAGE=n configs still define a simple > huge_ptep_clear_flush() for linking purposes, which dereferences the > ptep. So when both configs are disabled, this caused a build error > because ptep_get() is not defined. Fix by continuing to do a direct > dereference when MMU=n. This is safe because for this config the arch > code cannot be trying to virtualize the ptes because none of the ptep > helpers are defined. > > Reported-by: kernel test robot > Link: https://lore.kernel.org/oe-kbuild-all/202305120142.yXsNEo6H-lkp@intel.com/ > Signed-off-by: Ryan Roberts > --- > .../drm/i915/gem/selftests/i915_gem_mman.c | 8 +- > drivers/misc/sgi-gru/grufault.c | 2 +- > drivers/vfio/vfio_iommu_type1.c | 7 +- > drivers/xen/privcmd.c | 2 +- > fs/proc/task_mmu.c | 33 +++--- > fs/userfaultfd.c | 6 +- > include/linux/hugetlb.h | 4 + > include/linux/mm_inline.h | 2 +- > include/linux/pgtable.h | 6 +- > kernel/events/uprobes.c | 2 +- > mm/damon/ops-common.c | 2 +- > mm/damon/paddr.c | 2 +- > mm/damon/vaddr.c | 10 +- [...] > diff --git a/mm/damon/ops-common.c b/mm/damon/ops-common.c > index d4ab81229136..e940802a15a4 100644 > --- a/mm/damon/ops-common.c > +++ b/mm/damon/ops-common.c > @@ -39,7 +39,7 @@ struct folio *damon_get_folio(unsigned long pfn) > > void damon_ptep_mkold(pte_t *pte, struct vm_area_struct *vma, unsigned long addr) > { > - struct folio *folio = damon_get_folio(pte_pfn(*pte)); > + struct folio *folio = damon_get_folio(pte_pfn(ptep_get(pte))); > > if (!folio) > return; > diff --git a/mm/damon/paddr.c b/mm/damon/paddr.c > index 5b3a3463d078..40801e38fcf0 100644 > --- a/mm/damon/paddr.c > +++ b/mm/damon/paddr.c > @@ -89,7 +89,7 @@ static bool __damon_pa_young(struct folio *folio, struct vm_area_struct *vma, > while (page_vma_mapped_walk(&pvmw)) { > addr = pvmw.address; > if (pvmw.pte) { > - *accessed = pte_young(*pvmw.pte) || > + *accessed = pte_young(ptep_get(pvmw.pte)) || > !folio_test_idle(folio) || > mmu_notifier_test_young(vma->vm_mm, addr); > } else { > diff --git a/mm/damon/vaddr.c b/mm/damon/vaddr.c > index e814f66dfc2e..2fcc9731528a 100644 > --- a/mm/damon/vaddr.c > +++ b/mm/damon/vaddr.c > @@ -323,7 +323,7 @@ static int damon_mkold_pmd_entry(pmd_t *pmd, unsigned long addr, > walk->action = ACTION_AGAIN; > return 0; > } > - if (!pte_present(*pte)) > + if (!pte_present(ptep_get(pte))) > goto out; > damon_ptep_mkold(pte, walk->vma, addr); > out: > @@ -433,6 +433,7 @@ static int damon_young_pmd_entry(pmd_t *pmd, unsigned long addr, > unsigned long next, struct mm_walk *walk) > { > pte_t *pte; > + pte_t ptent; > spinlock_t *ptl; > struct folio *folio; > struct damon_young_walk_private *priv = walk->private; > @@ -471,12 +472,13 @@ static int damon_young_pmd_entry(pmd_t *pmd, unsigned long addr, > walk->action = ACTION_AGAIN; > return 0; > } > - if (!pte_present(*pte)) > + ptent = ptep_get(pte); > + if (!pte_present(ptent)) > goto out; > - folio = damon_get_folio(pte_pfn(*pte)); > + folio = damon_get_folio(pte_pfn(ptent)); > if (!folio) > goto out; > - if (pte_young(*pte) || !folio_test_idle(folio) || > + if (pte_young(ptent) || !folio_test_idle(folio) || > mmu_notifier_test_young(walk->mm, addr)) > priv->young = true; > *priv->folio_sz = folio_size(folio); For above DAMON part, Reviewed-by: SeongJae Park Thanks, SJ [...]