From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 2D66B3B7B76 for ; Mon, 8 Jun 2026 08:41:05 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.133.124 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1780908067; cv=none; b=e1rFg7UTXMWgUCSkxosAw1WIYvc+nLOIgREV4hbnyvDrmki7DoM1zL4GmOYTRkPKfn6A+7ktYwJpptOC+sbOvyDmwTctWacqmNF6AqWuDBVuWesomQmv83r+g9nkkTA6EqMNKCs8Eczzay7ESpaiNuwFblSWT8PUidQB11TYG9M= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1780908067; c=relaxed/simple; bh=d4eezH+pcRDMoTex5H3SWhKAQD5cm7eZ/LyLch14vB0=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=JpKZfPnRzRTSiS5ZLdx8U2VqHTw+AfI7FUKJBvcwbGX2PPkk+INm7eifaDrHGZ2rvr9cayPXIUUe0r0hFzar5rVMVXPDMQxQMeW3a9xfQQc3mAXpKzwNhr8NCUP2rK/OaihSEwNdGD8M9HLJ0AXRYIO/E8UCFKUOlzASvABf+5w= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=I8EsqeaM; dkim=pass (2048-bit key) header.d=redhat.com header.i=@redhat.com header.b=VJxDQtGd; arc=none smtp.client-ip=170.10.133.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="I8EsqeaM"; dkim=pass (2048-bit key) header.d=redhat.com header.i=@redhat.com header.b="VJxDQtGd" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1780908065; 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=4AnVGJ057Et7SfWebx9lNogVowiJVkQh33o78IbbqYo=; b=I8EsqeaM5jqdxGuhecUfYsa+g9chAiVYIM95CK9GPSals06lWyJx95EBk4aSj0Y0G6NkK9 6zPCsmbP2D1dspgoXZ76mZv/0CgveMzqEzU8v81g0gwlyHYxGCjN37M3OEFaUF3DRbHbkO GJg2JAmyAQme1wv9/r2ctYvVXhnREFY= Received: from mail-wr1-f70.google.com (mail-wr1-f70.google.com [209.85.221.70]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-534-iUBkw9RuNXG5VoRi3t_lPA-1; Mon, 08 Jun 2026 04:41:04 -0400 X-MC-Unique: iUBkw9RuNXG5VoRi3t_lPA-1 X-Mimecast-MFC-AGG-ID: iUBkw9RuNXG5VoRi3t_lPA_1780908063 Received: by mail-wr1-f70.google.com with SMTP id ffacd0b85a97d-4600c8cb13aso2428389f8f.1 for ; Mon, 08 Jun 2026 01:41:04 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=google; t=1780908063; x=1781512863; darn=vger.kernel.org; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:from:to:cc:subject:date:message-id:reply-to; bh=4AnVGJ057Et7SfWebx9lNogVowiJVkQh33o78IbbqYo=; b=VJxDQtGdLnQa3fBcHFTZZKyQMxYFX7gAQTZEfvpoBBU5Nj+BDtanhrUX0ENMRfftXW T00NOJGg+MKom3TmeFAv+37xzZjyPqIFIW1Ng1ot1jwB8p3BtI0eV+4MliS3P2cZ20qE YWKuhR7c8o+cb43ksu5xAS6pXRqlS0rF9g1YZoJB62R+wTekK01jW6qMump0PgFtuCat /o8Mmqu3hxA3qjCYig7IpIehp9fR4iPkF1yAqZYtcgC5BIgsM2NUtpZ1arAEUfS70whp 9Ea1xO29nwGVCu/ZHAE3qVmHXlSI3AlcdmisqKIA5HacHTp5pIYj+0z7XqiXDiTgd9De Ic4w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1780908063; x=1781512863; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:x-gm-gg:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=4AnVGJ057Et7SfWebx9lNogVowiJVkQh33o78IbbqYo=; b=g59NwRtFhHXlG4t1Ell2qXDTIpoZuEE6Yn7EIOIpszS0I8h8WaKwsetvOcin+xKaFF 9hApHncY6JlTCZVb+30E4LRwy/AALfRJzlPGJ3c2GXfl/tY9TCt1K0qbhchQ0OmnAydt oaFE87WMKlwdqJVPTCite1ECto7P1iQX8pJN8ecaJZ4A4LEW6Vbltd854+m1HepRPGvM qSE/8iJ+lvxOyPy7FjQXbJh8b/A1E8XjH3nkfGoIdpjJUAj74VXC1+Ut5i8CDDFcVzgd PuzhaEWnFr+tZQ80tKppFTKR/2+5b9Sb8GA0gHBn701h+ofnVJwtPmivBDHj3iAfRtb7 FnqA== X-Gm-Message-State: AOJu0YzcVQ+hUnPwtnSsNPzUg94EimHoQGgG4mjxPgAd8bZqjZG/DgVQ 3bhMi3pKchI1WMB8+BWEknkkZKioyUGqpE0l73uW9VKdGNAnT30C3MXXTHTzKMYFxzACEXJHOXk qMv9lbNeAXTp38VpvMDqULc75tE/EjJPvIG1ZoR4m+athsPyNhLSm8lHVeIDLNLiyPtH3AvlHdy +Q87YQJx6vKAPCXkrhbmu2Joo86MQTGszg6taOW/0AGgs= X-Gm-Gg: Acq92OFQYrQY0E9KLtS8hDfBxe1ZFcyjjXatu9EspRdHc8TKWlErftMElEUJGql0ind WMlyCAc8MK9qxsrmghSZA4KcHQ+cfk+cKe3jAcdOdawTHZ8d5XWCh5Kf5bhO+8U9rSSgukX+Xfs IOaTSKgXZ2VJjS6yb98syOaWxFpJfnxzxmOAo9HISSwE9KxIbcd7IQGv6+Nfs8ZsQwSL0CgW+w7 TY+ukRHXST3OqVVheLTuq2qxiZ1rSuWwfRYlDbH19AOuMggi+HVH6DNwfpjnGmftyIyVX9zTIVu Pq85cjVtmcr279B/I+J2s3H58wrxCrXWOqJLE9zrB0qlTn3Q9MysXRS55AGmM0D+UsGN69OyMbQ B4+aLamuAhHdvmfBjOtn2UuM= X-Received: by 2002:a05:6000:240f:b0:456:b23d:e57 with SMTP id ffacd0b85a97d-46030182732mr23389961f8f.0.1780908062722; Mon, 08 Jun 2026 01:41:02 -0700 (PDT) X-Received: by 2002:a05:6000:240f:b0:456:b23d:e57 with SMTP id ffacd0b85a97d-46030182732mr23389854f8f.0.1780908062126; Mon, 08 Jun 2026 01:41:02 -0700 (PDT) Received: from redhat.com ([31.152.37.159]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-4602cda363bsm38418630f8f.31.2026.06.08.01.40.55 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 08 Jun 2026 01:41:01 -0700 (PDT) Date: Mon, 8 Jun 2026 04:40:54 -0400 From: "Michael S. Tsirkin" To: linux-kernel@vger.kernel.org Cc: "David Hildenbrand (Arm)" , Jason Wang , Xuan Zhuo , Eugenio =?utf-8?B?UMOpcmV6?= , Muchun Song , Oscar Salvador , Andrew Morton , Lorenzo Stoakes , "Liam R. Howlett" , Vlastimil Babka , Mike Rapoport , Suren Baghdasaryan , Michal Hocko , Brendan Jackman , Johannes Weiner , Zi Yan , Baolin Wang , Nico Pache , Ryan Roberts , Dev Jain , Barry Song , Lance Yang , Hugh Dickins , Matthew Brost , Joshua Hahn , Rakie Kim , Byungchul Park , Gregory Price , Ying Huang , Alistair Popple , Christoph Lameter , David Rientjes , Roman Gushchin , Harry Yoo , Axel Rasmussen , Yuanchu Xie , Wei Xu , Chris Li , Kairui Song , Kemeng Shi , Nhat Pham , Baoquan He , virtualization@lists.linux.dev, linux-mm@kvack.org, Andrea Arcangeli Subject: [PATCH v10 37/37] virtio_balloon: implement VIRTIO_BALLOON_F_DEVICE_INIT_ON_INFLATE Message-ID: References: Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: X-Mailer: git-send-email 2.51.2.2891.g4157995a80.dirty X-Mutt-Fcc: =sent When the device offers DEVICE_INIT_ON_INFLATE (bit 7), the device initializes inflated pages and returns a per-page bitmap indicating which pages were successfully initialized. The driver appends a device-writable bitmap buffer to each inflate descriptor chain via virtqueue_add_sgs. After the host acknowledges, the driver checks bitmap bits (bounded by used_len) and marks pages with SetPageZeroed. tell_host() returns used_len from virtqueue_get_buf(). Bitmap reads are bounded: fill_balloon() and virtballoon_migratepage() only trust bits within the used_len range. On deflate, release_pages_balloon checks PageZeroed per page and uses put_page_zeroed for pages the host initialized, propagating the zeroed hint to the buddy allocator. If inflate_vq has fewer than 2 descriptors, probe fails with -ENOSPC. If PAGE_POISON is negotiated with non-zero poison_val, the feature is cleared in validate(). See the virtio spec change: https://lore.kernel.org/all/9c69b992c3dd83dfef3db92cd86b2fd8a0730d48.1777731396.git.mst@redhat.com Signed-off-by: Michael S. Tsirkin Assisted-by: Claude:claude-opus-4-6 Assisted-by: cursor-agent:GPT-5.4-xhigh --- drivers/virtio/virtio_balloon.c | 107 ++++++++++++++++++++++++---- include/linux/page-flags.h | 1 + include/uapi/linux/virtio_balloon.h | 1 + 3 files changed, 97 insertions(+), 12 deletions(-) diff --git a/drivers/virtio/virtio_balloon.c b/drivers/virtio/virtio_balloon.c index bf1172ad5419..0cd4fd06ca2d 100644 --- a/drivers/virtio/virtio_balloon.c +++ b/drivers/virtio/virtio_balloon.c @@ -20,6 +20,7 @@ #include #include #include +#include /* * Balloon device works in 4K page units. So each page is pointed to by @@ -122,6 +123,13 @@ struct virtio_balloon { struct virtqueue *reporting_vq; struct page_reporting_dev_info pr_dev_info; + /* No DMA alignment needed: ACCESS_PLATFORM is cleared, + * so virtio bypasses the DMA API. If this ever changes, + * add ____dma_from_device_aligned here. + */ + /* Bitmap returned by host for DEVICE_INIT_ON_INFLATE */ + DECLARE_BITMAP(inflate_bitmap, VIRTIO_BALLOON_ARRAY_PFNS_MAX); + /* State for keeping the wakeup_source active while adjusting the balloon */ spinlock_t wakeup_lock; bool processing_wakeup_event; @@ -182,22 +190,33 @@ static void balloon_ack(struct virtqueue *vq) wake_up(&vb->acked); } -static void tell_host(struct virtio_balloon *vb, struct virtqueue *vq) +static unsigned int tell_host(struct virtio_balloon *vb, struct virtqueue *vq) { - struct scatterlist sg; + struct scatterlist sg_out, sg_in; + struct scatterlist *sgs[] = { &sg_out, &sg_in }; unsigned int len; - sg_init_one(&sg, vb->pfns, sizeof(vb->pfns[0]) * vb->num_pfns); + sg_init_one(&sg_out, vb->pfns, sizeof(vb->pfns[0]) * vb->num_pfns); /* We made sure the vq is large enough so we should always * be able to add one buffer to an empty queue. */ - virtqueue_add_outbuf(vq, &sg, 1, vb, GFP_KERNEL); + if (vq == vb->inflate_vq && + virtio_has_feature(vb->vdev, + VIRTIO_BALLOON_F_DEVICE_INIT_ON_INFLATE)) { + unsigned int bitmap_bytes; + + bitmap_bytes = DIV_ROUND_UP(vb->num_pfns, 8); + bitmap_zero(vb->inflate_bitmap, vb->num_pfns); + sg_init_one(&sg_in, vb->inflate_bitmap, bitmap_bytes); + virtqueue_add_sgs(vq, sgs, 1, 1, vb, GFP_KERNEL); + } else { + virtqueue_add_outbuf(vq, &sg_out, 1, vb, GFP_KERNEL); + } virtqueue_kick(vq); - /* When host has read buffer, this completes via balloon_ack */ wait_event(vb->acked, virtqueue_get_buf(vq, &len)); - + return len; } static int virtballoon_free_page_report(struct page_reporting_dev_info *pr_dev_info, @@ -300,8 +319,37 @@ static unsigned int fill_balloon(struct virtio_balloon *vb, size_t num) num_allocated_pages = vb->num_pfns; /* Did we get any? */ - if (vb->num_pfns != 0) - tell_host(vb, vb->inflate_vq); + if (vb->num_pfns != 0) { + unsigned int used_len = tell_host(vb, vb->inflate_vq); + + if (virtio_has_feature(vb->vdev, + VIRTIO_BALLOON_F_DEVICE_INIT_ON_INFLATE)) { + unsigned int i; + unsigned int valid_bits = used_len * 8; + + for (i = 0; i < vb->num_pfns; + i += VIRTIO_BALLOON_PAGES_PER_PAGE) { + unsigned int pfn, j; + bool zeroed = true; + + if (i + VIRTIO_BALLOON_PAGES_PER_PAGE > valid_bits) + break; + for (j = 0; j < VIRTIO_BALLOON_PAGES_PER_PAGE; j++) { + if (!test_bit_le(i + j, vb->inflate_bitmap)) { + zeroed = false; + break; + } + } + if (zeroed) { + pfn = virtio32_to_cpu(vb->vdev, + vb->pfns[i]); + SetPageZeroed(pfn_to_page(pfn >> + (PAGE_SHIFT - + VIRTIO_BALLOON_PFN_SHIFT))); + } + } + } + } mutex_unlock(&vb->balloon_lock); return num_allocated_pages; @@ -314,7 +362,12 @@ static void release_pages_balloon(struct virtio_balloon *vb, list_for_each_entry_safe(page, next, pages, lru) { list_del(&page->lru); - put_page(page); /* balloon reference */ + if (PageZeroed(page)) { + ClearPageZeroed(page); + put_page_zeroed(page); + } else { + put_page(page); + } } } @@ -861,8 +914,27 @@ static int virtballoon_migratepage(struct balloon_dev_info *vb_dev_info, /* balloon's page migration 1st step -- inflate "newpage" */ vb->num_pfns = VIRTIO_BALLOON_PAGES_PER_PAGE; set_page_pfns(vb, vb->pfns, newpage); - tell_host(vb, vb->inflate_vq); + { + unsigned int used_len = tell_host(vb, vb->inflate_vq); + if (virtio_has_feature(vb->vdev, + VIRTIO_BALLOON_F_DEVICE_INIT_ON_INFLATE) && + used_len >= DIV_ROUND_UP(VIRTIO_BALLOON_PAGES_PER_PAGE, 8)) { + unsigned int j; + bool zeroed = true; + + for (j = 0; j < VIRTIO_BALLOON_PAGES_PER_PAGE; j++) { + if (!test_bit_le(j, vb->inflate_bitmap)) { + zeroed = false; + break; + } + } + if (zeroed) + SetPageZeroed(newpage); + } + } + + ClearPageZeroed(page); /* balloon's page migration 2nd step -- deflate "page" */ vb->num_pfns = VIRTIO_BALLOON_PAGES_PER_PAGE; set_page_pfns(vb, vb->pfns, page); @@ -966,6 +1038,12 @@ static int virtballoon_probe(struct virtio_device *vdev) if (err) goto out_free_vb; + if (virtio_has_feature(vdev, VIRTIO_BALLOON_F_DEVICE_INIT_ON_INFLATE) && + virtqueue_get_vring_size(vb->inflate_vq) < 2) { + err = -ENOSPC; + goto out_del_vqs; + } + if (!virtio_has_feature(vb->vdev, VIRTIO_BALLOON_F_DEFLATE_ON_OOM)) vb->vb_dev_info.adjust_managed_page_count = true; #ifdef CONFIG_BALLOON_MIGRATION @@ -1191,11 +1269,15 @@ static int virtballoon_validate(struct virtio_device *vdev) /* Device fills with poison_val, not zeros; disable zeroed hint */ if (virtio_has_feature(vdev, VIRTIO_BALLOON_F_PAGE_POISON) && - !want_init_on_free()) + !want_init_on_free()) { __virtio_clear_bit(vdev, VIRTIO_BALLOON_F_DEVICE_INIT_REPORTED); + __virtio_clear_bit(vdev, VIRTIO_BALLOON_F_DEVICE_INIT_ON_INFLATE); + } - if (cc_platform_has(CC_ATTR_GUEST_MEM_ENCRYPT)) + if (cc_platform_has(CC_ATTR_GUEST_MEM_ENCRYPT)) { __virtio_clear_bit(vdev, VIRTIO_BALLOON_F_DEVICE_INIT_REPORTED); + __virtio_clear_bit(vdev, VIRTIO_BALLOON_F_DEVICE_INIT_ON_INFLATE); + } /* * Balloon submits 1-2 sg entries max per buffer, virtqueue * sizes are 128+. Disable indirect descriptors to avoid @@ -1215,6 +1297,7 @@ static unsigned int features[] = { VIRTIO_BALLOON_F_PAGE_POISON, VIRTIO_BALLOON_F_REPORTING, VIRTIO_BALLOON_F_DEVICE_INIT_REPORTED, + VIRTIO_BALLOON_F_DEVICE_INIT_ON_INFLATE, }; static struct virtio_driver virtio_balloon_driver = { diff --git a/include/linux/page-flags.h b/include/linux/page-flags.h index 9365d59ac1d6..caecf0fbf0e2 100644 --- a/include/linux/page-flags.h +++ b/include/linux/page-flags.h @@ -680,6 +680,7 @@ FOLIO_FLAG_FALSE(idle) * uses this to skip redundant zeroing in post_alloc_hook(). */ __PAGEFLAG(Zeroed, zeroed, PF_NO_COMPOUND) +SETPAGEFLAG(Zeroed, zeroed, PF_NO_COMPOUND) CLEARPAGEFLAG(Zeroed, zeroed, PF_NO_COMPOUND) #define __PG_ZEROED (1UL << PG_zeroed) diff --git a/include/uapi/linux/virtio_balloon.h b/include/uapi/linux/virtio_balloon.h index 13074631f300..cbaf18e0b17c 100644 --- a/include/uapi/linux/virtio_balloon.h +++ b/include/uapi/linux/virtio_balloon.h @@ -38,6 +38,7 @@ #define VIRTIO_BALLOON_F_PAGE_POISON 4 /* Guest is using page poisoning */ #define VIRTIO_BALLOON_F_REPORTING 5 /* Page reporting virtqueue */ #define VIRTIO_BALLOON_F_DEVICE_INIT_REPORTED 6 /* Device initializes reported pages */ +#define VIRTIO_BALLOON_F_DEVICE_INIT_ON_INFLATE 7 /* Device initializes pages on inflate */ /* Size of a PFN in the balloon interface. */ #define VIRTIO_BALLOON_PFN_SHIFT 12 -- MST