Received: by 2002:a05:6359:c8b:b0:c7:702f:21d4 with SMTP id go11csp1579957rwb; Wed, 28 Sep 2022 22:15:51 -0700 (PDT) X-Google-Smtp-Source: AMsMyM5F3jtagde1l1GId0MKVfwp/rM334psn0WpK66hNikehyU/WN/DEcZawadb+4JeIlN6BMkP X-Received: by 2002:a17:907:3f9f:b0:782:a14a:fdf9 with SMTP id hr31-20020a1709073f9f00b00782a14afdf9mr1160627ejc.49.1664428551126; Wed, 28 Sep 2022 22:15:51 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1664428551; cv=none; d=google.com; s=arc-20160816; b=uKcUmXi7UsXubga3DSo+Pu88sCYCqKZjB6R2IDvSrRet+PbPrLYAfq2cEwUuMA343U XEmhpeCfwkvnXnl0nKtaLcfn2T1VbVGk99BfswbYBn5OnnbUBS1IKylIAogQmHne+j1w JWEvNFaPQbfBD+UnxGLJcqnzBsJrS8S9oXvTUkbTyucs4MrBar2p6tuOH9AqsvLHz47L anyuDl+RmGrKoBU4UoFez80TcYdOYZgJ8ahUoLiI/9tTXQb6cz/jEmgh42XhSWxMJDKH bpV9lUnEVjM+/nhzZ+SiUPWlkUV8XFmRAKi4EjJJt0OD0s9CYMGI9ykSzU3lyctqDx+3 ywdw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:mime-version:message-id:date:references :in-reply-to:subject:cc:to:from:dkim-signature; bh=LNJLwf4icNuP92vlonmeVOhbmhMWAvb1KZSRpI5pNFE=; b=ASfEY8LMAWdUqiJxwk9uZOr/rW4S43p4alACSWA4SDoYEzNBAMoM5b5C7R4pCcdsCK eR1tdPozGXEvlSclf9Sxl60BfXlR/s7mEZLNXT3nxNijXhGradvBAXTzsv0h74zYPQED dAUityjlfsTqyARz5omVkyoq2SzmAPzik0Gh/M13qBWBI6lDB1jm5RCbfuzBJyy6TStH gePV6LzDySiVHdadnxPwL2V4d853NNAgl/J+5pmpQ1YIXNmdf6oFpCBWOHQ8se0ANAeZ Urf5OlH61juFYNPy8R82BiM/dv28LR1oW21+HqVe98E0drVPSUmz043Xit1t2iRrRwEt rsXg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@ellerman.id.au header.s=201909 header.b=RmmwtB+L; 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 Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id he18-20020a1709073d9200b00741827c6304si7793782ejc.772.2022.09.28.22.15.25; Wed, 28 Sep 2022 22:15:51 -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=@ellerman.id.au header.s=201909 header.b=RmmwtB+L; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233992AbiI2FHd (ORCPT + 99 others); Thu, 29 Sep 2022 01:07:33 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55378 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232120AbiI2FHb (ORCPT ); Thu, 29 Sep 2022 01:07:31 -0400 Received: from gandalf.ozlabs.org (mail.ozlabs.org [IPv6:2404:9400:2221:ea00::3]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 73715E5FB2 for ; Wed, 28 Sep 2022 22:07:29 -0700 (PDT) Received: from authenticated.ozlabs.org (localhost [127.0.0.1]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (P-256) server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by mail.ozlabs.org (Postfix) with ESMTPSA id 4MdLvZ196nz4xGh; Thu, 29 Sep 2022 15:07:22 +1000 (AEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ellerman.id.au; s=201909; t=1664428044; bh=LNJLwf4icNuP92vlonmeVOhbmhMWAvb1KZSRpI5pNFE=; h=From:To:Cc:Subject:In-Reply-To:References:Date:From; b=RmmwtB+Lq8bjkFuvWqmHzVzaEXURluSYEX7ndiVyuWqzynjHwOkXEd7d55TdcPdR3 AKsLc+bQH8LwodnZNM/TXhnavHNQdaIehmVr9TluOfPyWQZui7tDuKbQsH01FhSC8V wr1p2UI1BPVSJA0f/V9l1JzFzgT04uDvRNZ3B9hv9k4fxW8theuwzRQdlgNILubYhq ZK/ymFMP+bM4hv0Q2p4PRL+P9isFHyJhX6hL74EqHJrJiR4ExrEOSFWffFokEb+amQ C7cL/kWRJkajfUPucTviskOUV95IRNe3nZ8ugIFg3iEXJ1+raAUC75VfQfduJLiaJD XlroTz/ZLvaTw== From: Michael Ellerman To: Alistair Popple Cc: linux-mm@kvack.org, Andrew Morton , Nicholas Piggin , Felix Kuehling , Alex Deucher , Christian =?utf-8?Q?K=C3=B6nig?= , "Pan, Xinhui" , David Airlie , Daniel Vetter , Ben Skeggs , Karol Herbst , Lyude Paul , Ralph Campbell , "Matthew Wilcox (Oracle)" , Alex Sierra , John Hubbard , linuxppc-dev@lists.ozlabs.org, linux-kernel@vger.kernel.org, amd-gfx@lists.freedesktop.org, nouveau@lists.freedesktop.org, dri-devel@lists.freedesktop.org, Jason Gunthorpe , Dan Williams Subject: Re: [PATCH 1/7] mm/memory.c: Fix race when faulting a device private page In-Reply-To: <875yh7osye.fsf@nvdebian.thelocal> References: <87fsgbf3gh.fsf@mpe.ellerman.id.au> <875yh7osye.fsf@nvdebian.thelocal> Date: Thu, 29 Sep 2022 15:07:17 +1000 Message-ID: <87bkqyg456.fsf@mpe.ellerman.id.au> MIME-Version: 1.0 Content-Type: text/plain X-Spam-Status: No, score=-4.4 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED,SPF_HELO_PASS, SPF_PASS 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 Alistair Popple writes: > Michael Ellerman writes: >> Alistair Popple writes: >>> When the CPU tries to access a device private page the migrate_to_ram() >>> callback associated with the pgmap for the page is called. However no >>> reference is taken on the faulting page. Therefore a concurrent >>> migration of the device private page can free the page and possibly the >>> underlying pgmap. This results in a race which can crash the kernel due >>> to the migrate_to_ram() function pointer becoming invalid. It also means >>> drivers can't reliably read the zone_device_data field because the page >>> may have been freed with memunmap_pages(). >>> >>> Close the race by getting a reference on the page while holding the ptl >>> to ensure it has not been freed. Unfortunately the elevated reference >>> count will cause the migration required to handle the fault to fail. To >>> avoid this failure pass the faulting page into the migrate_vma functions >>> so that if an elevated reference count is found it can be checked to see >>> if it's expected or not. >>> >>> Signed-off-by: Alistair Popple >>> --- >>> arch/powerpc/kvm/book3s_hv_uvmem.c | 15 ++++++----- >>> drivers/gpu/drm/amd/amdkfd/kfd_migrate.c | 17 +++++++------ >>> drivers/gpu/drm/amd/amdkfd/kfd_migrate.h | 2 +- >>> drivers/gpu/drm/amd/amdkfd/kfd_svm.c | 11 +++++--- >>> include/linux/migrate.h | 8 ++++++- >>> lib/test_hmm.c | 7 ++--- >>> mm/memory.c | 16 +++++++++++- >>> mm/migrate.c | 34 ++++++++++++++----------- >>> mm/migrate_device.c | 18 +++++++++---- >>> 9 files changed, 87 insertions(+), 41 deletions(-) >>> >>> diff --git a/arch/powerpc/kvm/book3s_hv_uvmem.c b/arch/powerpc/kvm/book3s_hv_uvmem.c >>> index 5980063..d4eacf4 100644 >>> --- a/arch/powerpc/kvm/book3s_hv_uvmem.c >>> +++ b/arch/powerpc/kvm/book3s_hv_uvmem.c >>> @@ -508,10 +508,10 @@ unsigned long kvmppc_h_svm_init_start(struct kvm *kvm) ... >>> @@ -994,7 +997,7 @@ static vm_fault_t kvmppc_uvmem_migrate_to_ram(struct vm_fault *vmf) >>> >>> if (kvmppc_svm_page_out(vmf->vma, vmf->address, >>> vmf->address + PAGE_SIZE, PAGE_SHIFT, >>> - pvt->kvm, pvt->gpa)) >>> + pvt->kvm, pvt->gpa, vmf->page)) >>> return VM_FAULT_SIGBUS; >>> else >>> return 0; >> >> I don't have a UV test system, but as-is it doesn't even compile :) > > Ugh, thanks. I did get as far as installing a PPC cross-compiler and > building a kernel. Apparently I did not get as far as enabling > CONFIG_PPC_UV :) No worries, that's really on us. If we're going to keep the code in the tree then it should really be enabled in at least one of our defconfigs. cheers