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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 4C70FC0502A for ; Tue, 30 Aug 2022 08:00:10 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230224AbiH3IAI (ORCPT ); Tue, 30 Aug 2022 04:00:08 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38874 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231375AbiH3H7o (ORCPT ); Tue, 30 Aug 2022 03:59:44 -0400 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5F7F9D276A for ; Tue, 30 Aug 2022 00:59:41 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1661846380; 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=KCfrLsUUliFZAsT7cuTFQ+KKBb55P+r5AzSlUp1Nkos=; b=V/nX782znauQJCKsfwuDTU6icvwpBC0Z+5Ve94Rj6Cx+SjgKkI6WaqK61tAxbhzBtRKMek yNeglz02H37459h2+e4eLLHXuOoj//yZw20MB9ZrYMJroAZC5dr8JMtkng0GxRL/hVNihg 9XdgfodtdsdfRJUwJlmmJc5wVZKvZ0E= Received: from mail-wm1-f70.google.com (mail-wm1-f70.google.com [209.85.128.70]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_128_GCM_SHA256) id us-mta-128-20ZX4OesOfa0xoWUl8dOag-1; Tue, 30 Aug 2022 03:59:35 -0400 X-MC-Unique: 20ZX4OesOfa0xoWUl8dOag-1 Received: by mail-wm1-f70.google.com with SMTP id m22-20020a7bca56000000b003a652939bd1so1802718wml.9 for ; Tue, 30 Aug 2022 00:59:35 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:in-reply-to:organization:from:references :cc:to:content-language:subject:user-agent:mime-version:date :message-id:x-gm-message-state:from:to:cc; bh=KCfrLsUUliFZAsT7cuTFQ+KKBb55P+r5AzSlUp1Nkos=; b=xVV80RHRiXAYo4CYBGT4bi1eVcOyooR7wNaUaMIEVJb7SPojCEXEA8Lc902ZpXkbNR I/ukAryO89jAyS5wDuTSuFE9XYMWFQldDmn9AsPHlqo6o9rqKhwWceKFdsyZM6a7F0TB efPCfHUuz05aZW9BhgYh1jIUHz0KSJowPj8T58wSyaCa3U8zwUkI2/V/5JeU432k9+F6 glZws+ZyHP6MBEBpY49S6Hbe6n/zGrGwBJGhDc09plEUxKrxjmFEsQyfGfa4qDdqhiy0 ihPz6oEVsSzIiiEXA1tEtW1a8ImaGO91AmooweBE0KOyzphynVzxMH0Xs28M8e1LNExY 4jTA== X-Gm-Message-State: ACgBeo3ZFWG+GRZ8M0TtdAJ+JBzdQxGffcuAHEyEPsEEIfJ8GlWkq/FF xKTVyHri3tZlPlyubpnxYk0YSNRkO4DWRbb9TTBoYHqCa39KGdMvgUD7F1Ajmg4qYYVuBK4/4Br tVquhskhmybyo73+FEroTK2h8 X-Received: by 2002:a05:600c:1992:b0:3a6:23f6:8417 with SMTP id t18-20020a05600c199200b003a623f68417mr8373207wmq.14.1661846374816; Tue, 30 Aug 2022 00:59:34 -0700 (PDT) X-Google-Smtp-Source: AA6agR6G+HNENoZbk/kP0Upti13ajYwcNsvPHgO3ZJjPM9lTCEmTs9Are0AGapPE11SllUCzKZrfKw== X-Received: by 2002:a05:600c:1992:b0:3a6:23f6:8417 with SMTP id t18-20020a05600c199200b003a623f68417mr8373198wmq.14.1661846374508; Tue, 30 Aug 2022 00:59:34 -0700 (PDT) Received: from ?IPV6:2003:cb:c70a:1000:ecb4:919b:e3d3:e20b? (p200300cbc70a1000ecb4919be3d3e20b.dip0.t-ipconnect.de. [2003:cb:c70a:1000:ecb4:919b:e3d3:e20b]) by smtp.gmail.com with ESMTPSA id l14-20020a05600c4f0e00b003a83d5f3678sm11136280wmq.7.2022.08.30.00.59.33 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Tue, 30 Aug 2022 00:59:34 -0700 (PDT) Message-ID: <39145649-c378-d027-8856-81b4f09050fc@redhat.com> Date: Tue, 30 Aug 2022 09:59:33 +0200 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101 Thunderbird/91.12.0 Subject: Re: [PATCH] vfio/type1: Unpin zero pages Content-Language: en-US To: Alex Williamson Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, lpivarc@redhat.com References: <166182871735.3518559.8884121293045337358.stgit@omen> From: David Hildenbrand Organization: Red Hat In-Reply-To: <166182871735.3518559.8884121293045337358.stgit@omen> Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 30.08.22 05:05, Alex Williamson wrote: > There's currently a reference count leak on the zero page. We increment > the reference via pin_user_pages_remote(), but the page is later handled > as an invalid/reserved page, therefore it's not accounted against the > user and not unpinned by our put_pfn(). > > Introducing special zero page handling in put_pfn() would resolve the > leak, but without accounting of the zero page, a single user could > still create enough mappings to generate a reference count overflow. > > The zero page is always resident, so for our purposes there's no reason > to keep it pinned. Therefore, add a loop to walk pages returned from > pin_user_pages_remote() and unpin any zero pages. > > Cc: David Hildenbrand > Cc: stable@vger.kernel.org > Reported-by: Luboslav Pivarc > Signed-off-by: Alex Williamson > --- > drivers/vfio/vfio_iommu_type1.c | 12 ++++++++++++ > 1 file changed, 12 insertions(+) > > diff --git a/drivers/vfio/vfio_iommu_type1.c b/drivers/vfio/vfio_iommu_type1.c > index db516c90a977..8706482665d1 100644 > --- a/drivers/vfio/vfio_iommu_type1.c > +++ b/drivers/vfio/vfio_iommu_type1.c > @@ -558,6 +558,18 @@ static int vaddr_get_pfns(struct mm_struct *mm, unsigned long vaddr, > ret = pin_user_pages_remote(mm, vaddr, npages, flags | FOLL_LONGTERM, > pages, NULL, NULL); > if (ret > 0) { > + int i; > + > + /* > + * The zero page is always resident, we don't need to pin it > + * and it falls into our invalid/reserved test so we don't > + * unpin in put_pfn(). Unpin all zero pages in the batch here. > + */ > + for (i = 0 ; i < ret; i++) { > + if (unlikely(is_zero_pfn(page_to_pfn(pages[i])))) > + unpin_user_page(pages[i]); > + } > + > *pfn = page_to_pfn(pages[0]); > goto done; > } > > As discussed offline, for the shared zeropage (that's not even refcounted when mapped into a process), this makes perfect sense to me. Good question raised by Sean if ZONE_DEVICE pages might similarly be problematic. But for them, we cannot simply always unpin here. Reviewed-by: David Hildenbrand -- Thanks, David / dhildenb