From nobody Sun Feb 8 23:35:19 2026 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 90C2433711A for ; Tue, 21 Oct 2025 12:59:51 +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=1761051593; cv=none; b=LjdA5XhZ1VqBD+p86lm3WioFY1CPxz49tpYI5LDXsPnUkTZy520bn/uowuzrKhGcpmXVJuq9SbsDDTrhCFNIeV2ifqFez+2jWsno16jLneHR+TiByBhek0iM7f+dBTAlw6rQvDisK0tOLjPq+Nldfd8xAIT63Fvz3FqOnCO7zlQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1761051593; c=relaxed/simple; bh=XZj/6LkkjOgMnlFzr0MmN0PRPlNc6Qo/DW9DiPOOcnM=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=Xie2XPVK226OqvaEGtueXE7Z/bvs/PjjaXMOni+C6ZQTPBxhzDUnsQHKpfQOCgRfvyUCXAnvWVUWyOgxWCLw0w4tuslzEgeb4jujv7PrugkHculKwB3pHrduc0ptgt46hZtWlmN4j07UDio+BBlRn166OhcNDza4GzBlHds0wIY= 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=Ee2eNV9e; 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="Ee2eNV9e" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1761051590; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=z5VcU60xQeN7ydJcNuOrJoqdazEeOf9bmMF6YJ9dhbk=; b=Ee2eNV9e2RwUKDoSun2tjdXX4XkKlAtS1DrDA74BskbOxVXMy4FggscWyrblPRHZXM3Myr w54wi0J+v0XqKBkkhjEgZY2zV2oVt6vDs9d3UHSfM3kednkERSpRrahj/iH3DKBvVuDlXu RoAbHQVHXD7CX/wNbY6lGOx+d9mqABI= Received: from mail-wm1-f71.google.com (mail-wm1-f71.google.com [209.85.128.71]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-190-b9OpLyxBPL6--EgCJsyQIA-1; Tue, 21 Oct 2025 08:59:49 -0400 X-MC-Unique: b9OpLyxBPL6--EgCJsyQIA-1 X-Mimecast-MFC-AGG-ID: b9OpLyxBPL6--EgCJsyQIA_1761051588 Received: by mail-wm1-f71.google.com with SMTP id 5b1f17b1804b1-470fd92ad57so112211265e9.3 for ; Tue, 21 Oct 2025 05:59:49 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1761051588; x=1761656388; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=z5VcU60xQeN7ydJcNuOrJoqdazEeOf9bmMF6YJ9dhbk=; b=mn5qozP0Y0XFT3gdbOLtOF+kvyWkQLiLzgP6Ko2QP0wv/dMFykF7ruhVfwJQT9aJxQ G7TykkoFQfAQWE1nSOjHtxnhQCj0Z6rh9W9tpuHG3it8bJbnIHnbghTtCMTabAQgk4mw uzIHQtTGrhRlSiL70QFH7hEhsHQAGTLMY3rc7zX59tvyHacKw+HMcpxKyVrLQh143iyM JTkh/2AOFvGyxPbWKMryo16GlBhwuscnH86gC8DqXrL95DGGyaO+HMHqmEcQGFOQr5f+ 1psa0S///+vsMgieg/rccfTyO+/4JC6R/zicaBm5WhKMKAejt0tSM8wrFXzoGkpXXBcs 2LCw== X-Gm-Message-State: AOJu0YyhGsPhs6jAXV2tDn3C21dmW241rbevhIYOS1/nNb9ie+falBn2 A3u8MGtHX1AssYhCfi3N8921DYe+MUeH/G3SMnb+L/z2j+0/mbZpXSrQGj45PS49L0OZwwWHtX2 ImAnwb754341VqwH5jGvpxWN9GserMKn+Yo6pp/bMNVPtMdZNDDVXVAhDLKhUXSZ3ysjRKCX749 oaJ7j+1uj2HX57tXyOe6mYLYta1dM1gxRtN1HmS61o7aTFng== X-Gm-Gg: ASbGncsC0aSabkOXe7y91Cp52SLtSD9qRa4F1gKaM1zz/545rUWwLBw94SCAViFrwX3 u+ViSjYOe3rLDaAqPYXihrhq+py8aNUw89I29GaEEh7yRAglWp1mm0uFwsj3tKq9Ydz9wCEpZux PBIcAy3ASmo86hhne6/3Dk1WS23ZQYJ+VJonUFyJHKLr2WbxFLL7NOELKYIMhftvhbbMoq5MeMU Fd2TGa+3HZnvLZyiH+rn5xvnQiFXUhvuy7bi4O0OcFENB3XwcsXFTTM5bkhXfncFL8iWnvKNs18 3JJkEK6QDgCK+xZ5GehvwJ5IN0nywG8D2ZDsdmwxhlKjum8mhkSO/4D4I1uEA255Sx2qfEX82LL LoiJ5QlRgblsOwSEh70M1/U5BwBuTlVDdbFIfSVFEw/oLdBMA2S0/3vl0yFWK X-Received: by 2002:a05:600c:45ca:b0:46e:59dd:1b4d with SMTP id 5b1f17b1804b1-471178a4bb0mr123278165e9.16.1761051588140; Tue, 21 Oct 2025 05:59:48 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHERIP76djccuxwEVDdc3jxl3cHQS1Z1VLDzbGDsvVWwgXU4e20hoqSfb9F5zq9YX0z7BLvwQ== X-Received: by 2002:a05:600c:45ca:b0:46e:59dd:1b4d with SMTP id 5b1f17b1804b1-471178a4bb0mr123277565e9.16.1761051587656; Tue, 21 Oct 2025 05:59:47 -0700 (PDT) Received: from localhost (p200300d82f4e3200c99da38b3f3ad4b3.dip0.t-ipconnect.de. [2003:d8:2f4e:3200:c99d:a38b:3f3a:d4b3]) by smtp.gmail.com with UTF8SMTPSA id 5b1f17b1804b1-471556e17afsm190432085e9.17.2025.10.21.05.59.46 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Tue, 21 Oct 2025 05:59:47 -0700 (PDT) From: David Hildenbrand To: linux-kernel@vger.kernel.org Cc: linux-mm@kvack.org, linuxppc-dev@lists.ozlabs.org, Broadcom internal kernel review list , linux-doc@vger.kernel.org, virtualization@lists.linux.dev, David Hildenbrand , Andrew Morton , Oscar Salvador , Lorenzo Stoakes , "Liam R. Howlett" , Vlastimil Babka , Mike Rapoport , Suren Baghdasaryan , Michal Hocko , Jonathan Corbet , Madhavan Srinivasan , Michael Ellerman , Nicholas Piggin , Christophe Leroy , Arnd Bergmann , Greg Kroah-Hartman , Jerrin Shaji George , "Michael S. Tsirkin" , Jason Wang , Xuan Zhuo , =?UTF-8?q?Eugenio=20P=C3=A9rez?= , Zi Yan Subject: [PATCH v1 07/23] mm/balloon_compaction: use a device-independent balloon (list) lock Date: Tue, 21 Oct 2025 14:59:12 +0200 Message-ID: <20251021125929.377194-8-david@redhat.com> X-Mailer: git-send-email 2.51.0 In-Reply-To: <20251021125929.377194-1-david@redhat.com> References: <20251021125929.377194-1-david@redhat.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" In order to remove the dependency on the page lock for balloon pages, we need a lock that is independent of the page. It's crucial that we can handle the scenario where balloon deflation (clearing page->private) can race with page isolation (using page->private to obtain the balloon_dev_info where the lock currently resides). The current lock in balloon_dev_info is therefore not suitable. Fortunately, we never really have more than a single balloon device per VM, so we can just keep it simple and use a static lock to protect all balloon devices. Based on this change we will remove the dependency on the page lock next. Signed-off-by: David Hildenbrand --- include/linux/balloon_compaction.h | 6 ++--- mm/balloon_compaction.c | 36 +++++++++++++++++------------- 2 files changed, 23 insertions(+), 19 deletions(-) diff --git a/include/linux/balloon_compaction.h b/include/linux/balloon_com= paction.h index 3109d3c43d306..e2d9eb40e1fbb 100644 --- a/include/linux/balloon_compaction.h +++ b/include/linux/balloon_compaction.h @@ -21,10 +21,10 @@ * i. Setting the PG_movable_ops flag and page->private with the followi= ng * lock order * +-page_lock(page); - * +--spin_lock_irq(&b_dev_info->pages_lock); + * +--spin_lock_irq(&balloon_pages_lock); * * ii. isolation or dequeueing procedure must remove the page from balloon - * device page list under b_dev_info->pages_lock. + * device page list under &balloon_pages_lock * * The functions provided by this interface are placed to help on coping w= ith * the aforementioned balloon page corner case, as well as to ensure the s= imple @@ -52,7 +52,6 @@ */ struct balloon_dev_info { unsigned long isolated_pages; /* # of isolated pages for migration */ - spinlock_t pages_lock; /* Protection to pages list */ struct list_head pages; /* Pages enqueued & handled to Host */ int (*migratepage)(struct balloon_dev_info *, struct page *newpage, struct page *page, enum migrate_mode mode); @@ -71,7 +70,6 @@ extern size_t balloon_page_list_dequeue(struct balloon_de= v_info *b_dev_info, static inline void balloon_devinfo_init(struct balloon_dev_info *balloon) { balloon->isolated_pages =3D 0; - spin_lock_init(&balloon->pages_lock); INIT_LIST_HEAD(&balloon->pages); balloon->migratepage =3D NULL; balloon->adjust_managed_page_count =3D false; diff --git a/mm/balloon_compaction.c b/mm/balloon_compaction.c index fd9ec47cf4670..97e838795354d 100644 --- a/mm/balloon_compaction.c +++ b/mm/balloon_compaction.c @@ -11,6 +11,12 @@ #include #include =20 +/* + * Lock protecting the balloon_dev_info of all devices. We don't really + * expect more than one device. + */ +static DEFINE_SPINLOCK(balloon_pages_lock); + static void balloon_page_enqueue_one(struct balloon_dev_info *b_dev_info, struct page *page) { @@ -47,13 +53,13 @@ size_t balloon_page_list_enqueue(struct balloon_dev_inf= o *b_dev_info, unsigned long flags; size_t n_pages =3D 0; =20 - spin_lock_irqsave(&b_dev_info->pages_lock, flags); + spin_lock_irqsave(&balloon_pages_lock, flags); list_for_each_entry_safe(page, tmp, pages, lru) { list_del(&page->lru); balloon_page_enqueue_one(b_dev_info, page); n_pages++; } - spin_unlock_irqrestore(&b_dev_info->pages_lock, flags); + spin_unlock_irqrestore(&balloon_pages_lock, flags); return n_pages; } EXPORT_SYMBOL_GPL(balloon_page_list_enqueue); @@ -83,7 +89,7 @@ size_t balloon_page_list_dequeue(struct balloon_dev_info = *b_dev_info, unsigned long flags; size_t n_pages =3D 0; =20 - spin_lock_irqsave(&b_dev_info->pages_lock, flags); + spin_lock_irqsave(&balloon_pages_lock, flags); list_for_each_entry_safe(page, tmp, &b_dev_info->pages, lru) { if (n_pages =3D=3D n_req_pages) break; @@ -106,7 +112,7 @@ size_t balloon_page_list_dequeue(struct balloon_dev_inf= o *b_dev_info, dec_node_page_state(page, NR_BALLOON_PAGES); n_pages++; } - spin_unlock_irqrestore(&b_dev_info->pages_lock, flags); + spin_unlock_irqrestore(&balloon_pages_lock, flags); =20 return n_pages; } @@ -149,9 +155,9 @@ void balloon_page_enqueue(struct balloon_dev_info *b_de= v_info, { unsigned long flags; =20 - spin_lock_irqsave(&b_dev_info->pages_lock, flags); + spin_lock_irqsave(&balloon_pages_lock, flags); balloon_page_enqueue_one(b_dev_info, page); - spin_unlock_irqrestore(&b_dev_info->pages_lock, flags); + spin_unlock_irqrestore(&balloon_pages_lock, flags); } EXPORT_SYMBOL_GPL(balloon_page_enqueue); =20 @@ -191,11 +197,11 @@ struct page *balloon_page_dequeue(struct balloon_dev_= info *b_dev_info) * BUG() here, otherwise the balloon driver may get stuck in * an infinite loop while attempting to release all its pages. */ - spin_lock_irqsave(&b_dev_info->pages_lock, flags); + spin_lock_irqsave(&balloon_pages_lock, flags); if (unlikely(list_empty(&b_dev_info->pages) && !b_dev_info->isolated_pages)) BUG(); - spin_unlock_irqrestore(&b_dev_info->pages_lock, flags); + spin_unlock_irqrestore(&balloon_pages_lock, flags); return NULL; } return list_first_entry(&pages, struct page, lru); @@ -213,10 +219,10 @@ static bool balloon_page_isolate(struct page *page, i= solate_mode_t mode) if (!b_dev_info) return false; =20 - spin_lock_irqsave(&b_dev_info->pages_lock, flags); + spin_lock_irqsave(&balloon_pages_lock, flags); list_del(&page->lru); b_dev_info->isolated_pages++; - spin_unlock_irqrestore(&b_dev_info->pages_lock, flags); + spin_unlock_irqrestore(&balloon_pages_lock, flags); =20 return true; } @@ -230,10 +236,10 @@ static void balloon_page_putback(struct page *page) if (WARN_ON_ONCE(!b_dev_info)) return; =20 - spin_lock_irqsave(&b_dev_info->pages_lock, flags); + spin_lock_irqsave(&balloon_pages_lock, flags); list_add(&page->lru, &b_dev_info->pages); b_dev_info->isolated_pages--; - spin_unlock_irqrestore(&b_dev_info->pages_lock, flags); + spin_unlock_irqrestore(&balloon_pages_lock, flags); } =20 static int balloon_page_migrate(struct page *newpage, struct page *page, @@ -253,7 +259,7 @@ static int balloon_page_migrate(struct page *newpage, s= truct page *page, rc =3D b_dev_info->migratepage(b_dev_info, newpage, page, mode); switch (rc) { case 0: - spin_lock_irqsave(&b_dev_info->pages_lock, flags); + spin_lock_irqsave(&balloon_pages_lock, flags); =20 /* Insert the new page into the balloon list. */ get_page(newpage); @@ -272,7 +278,7 @@ static int balloon_page_migrate(struct page *newpage, s= truct page *page, } break; case -ENOENT: - spin_lock_irqsave(&b_dev_info->pages_lock, flags); + spin_lock_irqsave(&balloon_pages_lock, flags); =20 /* Old page was deflated but new page not inflated. */ __count_vm_event(BALLOON_DEFLATE); @@ -285,7 +291,7 @@ static int balloon_page_migrate(struct page *newpage, s= truct page *page, } =20 b_dev_info->isolated_pages--; - spin_unlock_irqrestore(&b_dev_info->pages_lock, flags); + spin_unlock_irqrestore(&balloon_pages_lock, flags); =20 /* Free the now-deflated page we isolated in balloon_page_isolate(). */ balloon_page_finalize(page); --=20 2.51.0