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 5DF4EC43334 for ; Thu, 23 Jun 2022 20:21:56 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id EA6BF8E018A; Thu, 23 Jun 2022 16:21:55 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id E56B88E0187; Thu, 23 Jun 2022 16:21:55 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id D1FB78E018A; Thu, 23 Jun 2022 16:21:55 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id C3C518E0187 for ; Thu, 23 Jun 2022 16:21:55 -0400 (EDT) Received: from smtpin08.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay11.hostedemail.com (Postfix) with ESMTP id 9728B80585 for ; Thu, 23 Jun 2022 20:21:55 +0000 (UTC) X-FDA: 79610621790.08.E6F72C1 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by imf22.hostedemail.com (Postfix) with ESMTP id 29813C0021 for ; Thu, 23 Jun 2022 20:21:52 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1656015712; 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: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=MGTe6KngeQd+vhwkXpPPd5v7NkorBp+zMw2BEOEYKVU=; b=Gw8dKA6dxjm2IafCz0tlPIZB5LWc8TcFTAkB2UaerfV0+CIQ5uA/idk4nY1K+Plz3Q1b8L qHVOFuaSCYDmFALCIfKRFcQSJPpWsyWl/AjOyXDvBdG3iPF2AX0YTAaj9TJKhQ3XTeDRSW 0fvmNMruhJqK+gJk0csPNoe7A3G5rdc= Received: from mail-il1-f200.google.com (mail-il1-f200.google.com [209.85.166.200]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-145-m3aSCdQWPsmlEkuPaBjsTw-1; Thu, 23 Jun 2022 16:21:41 -0400 X-MC-Unique: m3aSCdQWPsmlEkuPaBjsTw-1 Received: by mail-il1-f200.google.com with SMTP id g11-20020a056e021e0b00b002d1b5e8389bso101009ila.2 for ; Thu, 23 Jun 2022 13:21:41 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:from:to:cc:subject:message-id:in-reply-to :references:organization:mime-version:content-transfer-encoding; bh=MGTe6KngeQd+vhwkXpPPd5v7NkorBp+zMw2BEOEYKVU=; b=BHxh/ek3YNrkDTF7yE1xiBC+fddx2/Y02iog72Do5EDDpzPyIKwU+yNZDQxL+fibJx +0PB6b8Q7P+lI8yMnujnjI6ugMFk3WMtub5ABMW7bpjZ5ZqJSme+okTow3zHviESQ8sx 6A1e3LssVK2omrx9G5Oe0x1P3aH5nfp7qaW1jPBwHzXQXjRtrxbxtIN/w+L80+zQpVp4 dnd0VaYOBMwT96HI4G89tJMJkHkF0k0X50OPyh6+AXHO5D8uxSkoT2fyz9gm+aD6aqvN wpQhhwyZzX+CFdC6Akuy5+vTZzFYrWw2XPh/tglaiZ0Yms9LsW8DYvg6Bev6K3yy1GlG yuwg== X-Gm-Message-State: AJIora+Z6tipSRukHvAyZD0RgZoPSlH+68cylHs+y2oLZXbh93HAVsA9 c/+sOd/caZ39ONOyESWwAYYjF4YlzSaQUkCqD4d7ADIVpfd6EU2yQs5zI+mmhrnt1N4BBQDWmCv /GT5fFWzU2DU= X-Received: by 2002:a05:6602:4017:b0:671:eae4:9d35 with SMTP id bk23-20020a056602401700b00671eae49d35mr5675160iob.6.1656015701263; Thu, 23 Jun 2022 13:21:41 -0700 (PDT) X-Google-Smtp-Source: AGRyM1sERAz0Cbsp1FFDc97zOssvDAgvgNpRQPBpJO08spAwNaML5gRq5IGwmQEPahTsSQjDefXMdA== X-Received: by 2002:a05:6602:4017:b0:671:eae4:9d35 with SMTP id bk23-20020a056602401700b00671eae49d35mr5675147iob.6.1656015701023; Thu, 23 Jun 2022 13:21:41 -0700 (PDT) Received: from redhat.com ([38.15.36.239]) by smtp.gmail.com with ESMTPSA id a11-20020a92d58b000000b002d8f2385d56sm226923iln.63.2022.06.23.13.21.40 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 23 Jun 2022 13:21:40 -0700 (PDT) Date: Thu, 23 Jun 2022 14:21:39 -0600 From: Alex Williamson To: David Hildenbrand Cc: Jason Gunthorpe , akpm@linux-foundation.org, minchan@kernel.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org, paulmck@kernel.org, jhubbard@nvidia.com, joaodias@google.com Subject: Re: [PATCH] mm: Re-allow pinning of zero pfns Message-ID: <20220623142139.462a0841.alex.williamson@redhat.com> In-Reply-To: References: <165490039431.944052.12458624139225785964.stgit@omen> <20220615155659.GA7684@nvidia.com> Organization: Red Hat MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1656015713; a=rsa-sha256; cv=none; b=jGx8SyCtinnlmol2ZXEma4ompaubU9qjR5BfM2DUbWIwEu1auWXfuil9e4wbATz4LtMzZ/ UZLPRwVWvw7Wu2pP8ymzEpNQW4kpmSpzz98sxCECKO/7ITxJMcRQoN7lsAyLbD1xnVkBUG ns+bYmNgIFM0H6WmxIEBs9iI3ccwTuA= ARC-Authentication-Results: i=1; imf22.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=Gw8dKA6d; dmarc=pass (policy=none) header.from=redhat.com; spf=none (imf22.hostedemail.com: domain of alex.williamson@redhat.com has no SPF policy when checking 170.10.129.124) smtp.mailfrom=alex.williamson@redhat.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1656015713; 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:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=MGTe6KngeQd+vhwkXpPPd5v7NkorBp+zMw2BEOEYKVU=; b=oZHlDHjK93tmknMbsjQk1YT6QGqgI1ICc6t2aoIelzuuMiMfsDOI5ZxEfRktXYJhSqPzB4 hhCZXZh7rHqKrginLU5nUgHzSj1M990huMuBwKrwKfM+LB4Eu9PMs+hTWySOyWsUSicPCk x4BHpHFTR5yETPh54LgqJArxa2Gj44U= X-Stat-Signature: n9shzpy95u6cjjgg91pget5be9qwguz1 X-Rspamd-Queue-Id: 29813C0021 Authentication-Results: imf22.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=Gw8dKA6d; dmarc=pass (policy=none) header.from=redhat.com; spf=none (imf22.hostedemail.com: domain of alex.williamson@redhat.com has no SPF policy when checking 170.10.129.124) smtp.mailfrom=alex.williamson@redhat.com X-Rspamd-Server: rspam10 X-Rspam-User: X-HE-Tag: 1656015712-624492 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, 23 Jun 2022 20:07:14 +0200 David Hildenbrand wrote: > On 15.06.22 17:56, Jason Gunthorpe wrote: > > On Sat, Jun 11, 2022 at 08:29:47PM +0200, David Hildenbrand wrote: > >> On 11.06.22 00:35, Alex Williamson wrote: > >>> The commit referenced below subtly and inadvertently changed the logic > >>> to disallow pinning of zero pfns. This breaks device assignment with > >>> vfio and potentially various other users of gup. Exclude the zero page > >>> test from the negation. > >> > >> I wonder which setups can reliably work with a long-term pin on a shared > >> zeropage. In a MAP_PRIVATE mapping, any write access via the page tables > >> will end up replacing the shared zeropage with an anonymous page. > >> Something similar should apply in MAP_SHARED mappings, when lazily > >> allocating disk blocks. > > ^ correction, shared zeropage is never user in MAP_SHARED mappings > (fortunally). > > >> > >> In the future, we might trigger unsharing when taking a R/O pin for the > >> shared zeropage, just like we do as of now upstream for shared anonymous > >> pages (!PageAnonExclusive). And something similar could then be done > >> when finding a !anon page in a MAP_SHARED mapping. > > > > I'm also confused how qemu is hitting this and it isn't already a bug? > > > > I assume it's just some random thingy mapped into the guest physical > address space (by the bios? R/O?), that actually never ends up getting > used by a device. > > So vfio simply only needs this to keep working ... but weon't actually > ever user that data. > > But this is just my best guess after thinking about it. Good guess. > > It is arising because vfio doesn't use FOLL_FORCE|FOLL_WRITE to move > > away the zero page in most cases. > > > > And why does Yishai say it causes an infinite loop in the kernel? > > > Good question. Maybe $something keeps retying if pinning fails, either > in the kernel (which would be bad) or in user space. At least QEMU seems > to just fail if pinning fails, but maybe it's a different user space? The loop is in __gup_longterm_locked(): do { rc = __get_user_pages_locked(mm, start, nr_pages, pages, vmas, NULL, gup_flags); if (rc <= 0) break; rc = check_and_migrate_movable_pages(rc, pages, gup_flags); } while (!rc); It appears we're pinning a 32 page (128K) range, __get_user_pages_locked() returns 32, but check_and_migrate_movable_pages() perpetually returns zero. I believe this is because folio_is_pinnable() previously returned true, and now returns false. Therefore we drop down to fail at folio_isolate_lru(), incrementing isolation_error_count. From there we do nothing more than unpin the pages, return zero, and hope for better luck next time, which obviously doesn't happen. If I generate an errno here, QEMU reports failing on the pc.rom memory region at 0xc0000. Thanks, Alex