Received: by 2002:a05:6358:d09b:b0:dc:cd0c:909e with SMTP id jc27csp1011352rwb; Tue, 29 Nov 2022 07:59:13 -0800 (PST) X-Google-Smtp-Source: AA0mqf7kwC/EvUPEB7gxMjypTx08dwMNBS9s3D1lm1wNSgWGhVcAa4m/2xqstcqLy7eCQeszDJmH X-Received: by 2002:a17:902:8d98:b0:189:8e39:3c88 with SMTP id v24-20020a1709028d9800b001898e393c88mr9940950plo.102.1669737553123; Tue, 29 Nov 2022 07:59:13 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1669737553; cv=none; d=google.com; s=arc-20160816; b=IsFQ9d+3JWotuRoG2mFyt11esFK3HV7bR9j77OLAbvbBkkFQSqv5u6nH8oJlqmy1dP GqwmyzVT/sgh9wPSaq/34/C4eR9za1boeIrfC+CzamJ+Mj3h0COjLAetuais/+wASAo3 1s+heklA2lmKSG8kxUKYHszS2u7ney55ly8vN5JTJ22UIIyR+lkDIP0IDnhslaBrOga9 jE8bp0aJtSC15mwLuNOiprQFYnOYqS+LphDA0y9JQxZ84rnuOM4JMKsgl4Xa3fNTa1dt hJw/O37KF93SWaRVe5gxFF9NId97Oq06BUkxPwMTBjat9+6K3J8Di06RHyFVOSQCBgN/ aTsg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:subject:message-id:date:from:in-reply-to :references:mime-version:dkim-signature; bh=uMjNraLB6scr/xv9kM9mpf61HY1js/ZvtGhWlHG2rk8=; b=khOasNI8rh9oQGXN0hzfZ/B7upGjk9sJKNX1B95AxBE5BzXgPGNz82GRHxqQZ5CyBK PAIhfjcfzryqgfhf5Adl2XRFKXkcMJo4IF5jjXv5D9Qsv0ibPSO+pg2HESL1eMGwDrik vzuS+v/nKDeKvA51UsYSBpgV67QghWMbqtYBTUAhRKj+qitdruBThYrtsBM+1j7ugJ69 kuJ6P9YKKaXcotBr6+47+SftQpFgP/E7anTh5iENyloKieDnN6ZHtMc+1sYo/aDK8rqa uI6qIllxFbwx/QJQhVKjJ25IZaXhvbmT08KzXdM4Ra5yBrTRz7+/qkOM7x2xm+p/2jCn 7hpw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=e2HH0dG5; 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=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id jo6-20020a170903054600b0018996404dd9si3284333plb.267.2022.11.29.07.59.02; Tue, 29 Nov 2022 07:59:13 -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=@google.com header.s=20210112 header.b=e2HH0dG5; 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=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235846AbiK2PbJ (ORCPT + 84 others); Tue, 29 Nov 2022 10:31:09 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59674 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235823AbiK2PbH (ORCPT ); Tue, 29 Nov 2022 10:31:07 -0500 Received: from mail-il1-x130.google.com (mail-il1-x130.google.com [IPv6:2607:f8b0:4864:20::130]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8BBBA60EBF for ; Tue, 29 Nov 2022 07:31:03 -0800 (PST) Received: by mail-il1-x130.google.com with SMTP id f6so6758143ilu.13 for ; Tue, 29 Nov 2022 07:31:03 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=uMjNraLB6scr/xv9kM9mpf61HY1js/ZvtGhWlHG2rk8=; b=e2HH0dG5vmD1BA10H/FjRsLuqF361HQkMIjd2fOscEY5B1/yxduJlciZ+dvNrFYBsm scC3vBN1hlQXikK9x21+E4UlkGiX3srvT/7XmurqXi0b2LdY0xsm4E1izws3WaVNrPcc jJkCzD+7a2khe/8FXDY8wXwNjUxoo+NxaWcS5Oap2mSg83+KMPGf1T+D2mrbFrCTYvvY Lhv9I9rmVTa0GGM41qyDFFAOkT2vakmP6Jq38DU7slDN2WzlFO4s7s8AILaTaVmAXR1h f67tsuAow7TRlWkS9T7oHDNZNg++2n3Hz9VQ1+n3SJc+zkcg1tkq+pcPBvaKyIkLae4B gPsA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=uMjNraLB6scr/xv9kM9mpf61HY1js/ZvtGhWlHG2rk8=; b=ZgsHiaIWp/YoXQKlJGHSXnuMNXZCujofa25FcoBfb7hCvkvhSF8AdEeFRWfALuopgO hxpzZ7b00tzYbiTYnPosrBXkgOfDURSoUsgQ5H6jRU1htUXegEAZnXsJvcQFk13FSdlT IIO/wklZUhfPh/FE0VMRE/MEmxXKp24+dEv/Gbp26+XrEpMzUchqbR5MxvLR1aBOkpbs lZ0Ba5DuIBaILzDr+O63iQyclhuoCEl+1QYhS56z9SRAcluY68VATdsDdh89D/Sy42cw 50A++NvWsk43Mvd1lzvAhBYCfVuKUfcdcVI31OCt0El5ZoSkt+bWgJ9cNRDg92TtyTCo AcEQ== X-Gm-Message-State: ANoB5pmiKIZAVZ9N0VkJouZJTp/I/KnXWlyaZjW/hxyz3Ngro0d2ZhiI CzcvUG2iqmcuYtC4Xgr9D5RHEArtPDhecRza4Le1CZCgQ3Q= X-Received: by 2002:a92:c10f:0:b0:303:1f6a:b30c with SMTP id p15-20020a92c10f000000b003031f6ab30cmr2118645ile.254.1669735862770; Tue, 29 Nov 2022 07:31:02 -0800 (PST) MIME-Version: 1.0 References: <20221128180252.1684965-1-jannh@google.com> <20221128180252.1684965-2-jannh@google.com> In-Reply-To: From: Jann Horn Date: Tue, 29 Nov 2022 16:30:26 +0100 Message-ID: Subject: Re: [PATCH v4 2/3] mm/khugepaged: Fix GUP-fast interaction by sending IPI To: Yang Shi Cc: security@kernel.org, Andrew Morton , David Hildenbrand , Peter Xu , John Hubbard , linux-kernel@vger.kernel.org, linux-mm@kvack.org Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-17.6 required=5.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF, ENV_AND_HDR_SPF_MATCH,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS, USER_IN_DEF_DKIM_WL,USER_IN_DEF_SPF_WL 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, Nov 28, 2022 at 11:10 PM Yang Shi wrote: > > On Mon, Nov 28, 2022 at 12:12 PM Jann Horn wrote: > > > > On Mon, Nov 28, 2022 at 9:10 PM Yang Shi wrote: > > > On Mon, Nov 28, 2022 at 11:57 AM Jann Horn wrote: > > > > > > > > On Mon, Nov 28, 2022 at 8:54 PM Yang Shi wrote: > > > > > > > > > > On Mon, Nov 28, 2022 at 10:03 AM Jann Horn wrote: > > > > > > > > > > > > Since commit 70cbc3cc78a99 ("mm: gup: fix the fast GUP race against THP > > > > > > collapse"), the lockless_pages_from_mm() fastpath rechecks the pmd_t to > > > > > > ensure that the page table was not removed by khugepaged in between. > > > > > > > > > > > > However, lockless_pages_from_mm() still requires that the page table is not > > > > > > concurrently freed or reused to store non-PTE data. Otherwise, problems > > > > > > can occur because: > > > > > > > > > > > > - deposited page tables can be freed when a THP page somewhere in the > > > > > > mm is removed > > > > > > - some architectures store non-PTE information inside deposited page > > > > > > tables (see radix__pgtable_trans_huge_deposit()) > > > > > > > > > > > > Additionally, lockless_pages_from_mm() is also somewhat brittle with > > > > > > regards to page tables being repeatedly moved back and forth, but > > > > > > that shouldn't be an issue in practice. > > > > > > > > > > > > Fix it by sending IPIs (if the architecture uses > > > > > > semi-RCU-style page table freeing) before freeing/reusing page tables. > > > > > > > > > > > > As noted in mm/gup.c, on configs that define CONFIG_HAVE_FAST_GUP, > > > > > > there are two possible cases: > > > > > > > > > > > > 1. CONFIG_MMU_GATHER_RCU_TABLE_FREE is set, causing > > > > > > tlb_remove_table_sync_one() to send an IPI to synchronize with > > > > > > lockless_pages_from_mm(). > > > > > > 2. CONFIG_MMU_GATHER_RCU_TABLE_FREE is unset, indicating that all > > > > > > TLB flushes are already guaranteed to send IPIs. > > > > > > tlb_remove_table_sync_one() will do nothing, but we've already > > > > > > run pmdp_collapse_flush(), which did a TLB flush, which must have > > > > > > involved IPIs. > > > > > > > > > > I'm trying to catch up with the discussion after the holiday break. I > > > > > understand you switched from always allocating a new page table page > > > > > (we decided before) to sending IPIs to serialize against fast-GUP, > > > > > this is fine to me. > > > > > > > > > > So the code now looks like: > > > > > pmdp_collapse_flush() > > > > > sending IPI > > > > > > > > > > But the missing part is how we reached "TLB flushes are already > > > > > guaranteed to send IPIs" when CONFIG_MMU_GATHER_RCU_TABLE_FREE is > > > > > unset? ARM64 doesn't do it IIRC. Or did I miss something? > > > > > > > > From arch/arm64/Kconfig: > > > > > > > > select MMU_GATHER_RCU_TABLE_FREE > > > > > > > > CONFIG_MMU_GATHER_RCU_TABLE_FREE is not a config option that the user > > > > can freely toggle; it is an option selected by the architecture. > > > > > > Aha, I see :-) BTW, shall we revert "mm: gup: fix the fast GUP race > > > against THP collapse"? It seems not necessary anymore if this approach > > > is used IIUC. > > > > Yeah, I agree. > > Since this patch could solve two problems: the use-after-free of the > data page (pinned by fast-GUP) and the page table page and my patch > will be reverted, so could you please catch both issues in this > patch's commit log? I'd like to preserve the description of the issue > fixed by my patch. I think that it is helpful to see the information > about all the fixed problems in one commit instead of digging into > another reverted commit. OK, I will rewrite the commit message to describe the overall problem, including the part addressed by your patch.