From mboxrd@z Thu Jan 1 00:00:00 1970 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 B46D1C4332F for ; Fri, 18 Nov 2022 01:35:09 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 4E0446B0078; Thu, 17 Nov 2022 20:35:09 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 4908C6B007B; Thu, 17 Nov 2022 20:35:09 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 315148E0001; Thu, 17 Nov 2022 20:35:09 -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 1E0456B0078 for ; Thu, 17 Nov 2022 20:35:09 -0500 (EST) Received: from smtpin28.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id EF21CABA12 for ; Fri, 18 Nov 2022 01:35:08 +0000 (UTC) X-FDA: 80144844696.28.147804C Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by imf05.hostedemail.com (Postfix) with ESMTP id 90B44100008 for ; Fri, 18 Nov 2022 01:35:08 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1668735308; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=XuhgMGt5eYKxb7zJ8raKWs44aO6StvNoESpzCP1pzG4=; b=hxYfk6tjsg8cTgr1NjxNEjIQRHYG+wfUAQcU4z9BJVaLUeH5hNDeU9WEfhQMVEyCb83RbU I4vxC4fzN2ogOh9hhbrG4ZP3085baBq4KKY4hLJR5ksN/NGETKY9tO3Cv9Cp3/ekgH9yhT 6//wme3EMYKU+BoLKUxGTXHjvuJAyS0= Received: from mail-qt1-f200.google.com (mail-qt1-f200.google.com [209.85.160.200]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_128_GCM_SHA256) id us-mta-580-iFEC1gWyMa6AsvJ1zXpz9Q-1; Thu, 17 Nov 2022 20:35:07 -0500 X-MC-Unique: iFEC1gWyMa6AsvJ1zXpz9Q-1 Received: by mail-qt1-f200.google.com with SMTP id fb5-20020a05622a480500b003a525d52abcso3467745qtb.10 for ; Thu, 17 Nov 2022 17:35:07 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=XuhgMGt5eYKxb7zJ8raKWs44aO6StvNoESpzCP1pzG4=; b=GSo4T8XWSzZzoSMXJ8FLfrrrRXO9ztcNfj1M6HNVWSV3V8JQXqxrP9W7kBgjuRtA0C 3w4oIn2DvPiNJXSM56guqjLG15twsL2EN+D4R9+ccSqGll1DHZBftQ9doYFTn0ez5Jd/ xpAjr/TS3mUKzH1tDCYt0P6Qg2ifFysCoKewmyEhQoMRnd+7yvOuPXLdjXbHrmQuov1/ dA14hJYgluk3m6DWDuOcCvxTpnthpZKP7S/bbKZr2PTKYFRsZwBC0r8U1M4hxXlnifeY pof+uUpwEjVFI/s4JUFKsCmGNV0ayAgO/A1WzZuqn+UGsZ8y4SwLLWJ/0D5CkRIHZj96 7KCQ== X-Gm-Message-State: ANoB5pnjfC7Gjm6MS4wxqUKgwkxmYMCjSL9W15d5EyMNuRqW0rWpF3sN 6jV+hyXjNIeJkihA/Y6j7MPdLHuYkKLSMptlYmkqBVe/xM7RLT+OepD1ZTSpjPVs6+vjwi435d9 yYMlko64ar8ZJ5T1FHpcJxxBC+T4McoTo7Rnh0fowsGUCPbcGzJaavda77lGm X-Received: by 2002:a05:622a:1496:b0:3a5:7eb0:4962 with SMTP id t22-20020a05622a149600b003a57eb04962mr4712932qtx.525.1668735306439; Thu, 17 Nov 2022 17:35:06 -0800 (PST) X-Google-Smtp-Source: AA0mqf4wFkW2f8XkH1HOv7vs0jJoE2AQxy7v0LZq0260rY80eGcGRw4N1oBy3rmx6CIWrwhmo8OG3A== X-Received: by 2002:a05:622a:1496:b0:3a5:7eb0:4962 with SMTP id t22-20020a05622a149600b003a57eb04962mr4712906qtx.525.1668735306151; Thu, 17 Nov 2022 17:35:06 -0800 (PST) Received: from x1n (bras-base-aurron9127w-grc-46-70-31-27-79.dsl.bell.ca. [70.31.27.79]) by smtp.gmail.com with ESMTPSA id dt18-20020a05620a479200b006ea7f9d8644sm1519176qkb.96.2022.11.17.17.35.05 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 17 Nov 2022 17:35:05 -0800 (PST) Date: Thu, 17 Nov 2022 20:35:04 -0500 From: Peter Xu To: linux-mm@kvack.org, linux-kernel@vger.kernel.org Cc: Rik van Riel , Muchun Song , Andrew Morton , James Houghton , Nadav Amit , Andrea Arcangeli , David Hildenbrand , Miaohe Lin , Mike Kravetz Subject: Re: [PATCH RFC v2 02/12] mm/hugetlb: Move swap entry handling into vma lock for fault Message-ID: References: <20221118011025.2178986-1-peterx@redhat.com> <20221118011025.2178986-3-peterx@redhat.com> MIME-Version: 1.0 In-Reply-To: <20221118011025.2178986-3-peterx@redhat.com> X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset=utf-8 Content-Disposition: inline ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1668735308; a=rsa-sha256; cv=none; b=2AAfwtxSZF0w1U0GfmmHu722mSm1CxGMTQzxkN4IUesEcESgL6CIEW8ZV3wkvtO309Yu9S jpEU8NjT+3J1/cx4Wak7s1HXNjZGNhhtyhVTN7+QXDAQgCeWyVge+dL0DjbLvgOLVJrmqQ ek0ihBnEmn7hKF47uMLnY6eH6EK2TrU= ARC-Authentication-Results: i=1; imf05.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=hxYfk6tj; spf=pass (imf05.hostedemail.com: domain of peterx@redhat.com designates 170.10.133.124 as permitted sender) smtp.mailfrom=peterx@redhat.com; dmarc=pass (policy=none) header.from=redhat.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1668735308; 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-type:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=XuhgMGt5eYKxb7zJ8raKWs44aO6StvNoESpzCP1pzG4=; b=cnnniXYHPt8P7kKdOTqDMJ+dL0YuzbsD5THbh+jKsWV5PvlmajNrvTdMlpyXK6uzJJJ0YL F+Yr1+hK8jKnuGeVSZqFNcLsFbfZ9qUVzPp2sNMM4JR35cWuHq9Ycdz7fr9zrP68cWVdBK YT5eCX75xABjXEk8YCzxDuzaL+o9MOw= X-Stat-Signature: xpctfj5gf7ues4d615yjtzt1bing8e1u X-Rspamd-Queue-Id: 90B44100008 Authentication-Results: imf05.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=hxYfk6tj; spf=pass (imf05.hostedemail.com: domain of peterx@redhat.com designates 170.10.133.124 as permitted sender) smtp.mailfrom=peterx@redhat.com; dmarc=pass (policy=none) header.from=redhat.com X-Rspam-User: X-Rspamd-Server: rspam06 X-HE-Tag: 1668735308-95209 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: On Thu, Nov 17, 2022 at 08:10:15PM -0500, Peter Xu wrote: > In hugetlb_fault(), there used to have a special path to handle swap entry > at the entrance using huge_pte_offset(). That's unsafe because > huge_pte_offset() for a pmd sharable range can access freed pgtables if > without either the walker lock or vma lock. > > Here the simplest solution for making it safe is just to move the swap > handling to be after the vma lock being held. We may need to take the > fault mutex on either migration or hwpoison entries now (also the vma lock, > but that's really needed), however neither of them is hot path so it should > be fine. > > Signed-off-by: Peter Xu > --- > mm/hugetlb.c | 24 +++++++----------------- > 1 file changed, 7 insertions(+), 17 deletions(-) > > diff --git a/mm/hugetlb.c b/mm/hugetlb.c > index c3aab6d5b7aa..62ff3fc51d4e 100644 > --- a/mm/hugetlb.c > +++ b/mm/hugetlb.c > @@ -5824,22 +5824,6 @@ vm_fault_t hugetlb_fault(struct mm_struct *mm, struct vm_area_struct *vma, > int need_wait_lock = 0; > unsigned long haddr = address & huge_page_mask(h); > > - ptep = huge_pte_offset(mm, haddr, huge_page_size(h)); > - if (ptep) { > - /* > - * Since we hold no locks, ptep could be stale. That is > - * OK as we are only making decisions based on content and > - * not actually modifying content here. > - */ > - entry = huge_ptep_get(ptep); > - if (unlikely(is_hugetlb_entry_migration(entry))) { > - migration_entry_wait_huge(vma, ptep); > - return 0; > - } else if (unlikely(is_hugetlb_entry_hwpoisoned(entry))) > - return VM_FAULT_HWPOISON_LARGE | > - VM_FAULT_SET_HINDEX(hstate_index(h)); > - } > - > /* > * Serialize hugepage allocation and instantiation, so that we don't > * get spurious allocation failures if two CPUs race to instantiate > @@ -5886,8 +5870,14 @@ vm_fault_t hugetlb_fault(struct mm_struct *mm, struct vm_area_struct *vma, > * fault, and is_hugetlb_entry_(migration|hwpoisoned) check will > * properly handle it. > */ > - if (!pte_present(entry)) > + if (!pte_present(entry)) { > + if (unlikely(is_hugetlb_entry_migration(entry))) > + migration_entry_wait_huge(vma, ptep); Hmm no, need to release the vma lock and fault mutex.. So I remembered why I had a note that I need to rework migration wait code.. I'll try that on next version, it would be a callback just to release the proper locks in migration_entry_wait_huge() right after releasing the pgtable lock, in e.g. migration_entry_wait_on_locked(). > + else if (unlikely(is_hugetlb_entry_hwpoisoned(entry))) > + ret = VM_FAULT_HWPOISON_LARGE | > + VM_FAULT_SET_HINDEX(hstate_index(h)); > goto out_mutex; > + } > > /* > * If we are going to COW/unshare the mapping later, we examine the > -- > 2.37.3 > -- Peter Xu