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 7F54B337118 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=sRhXGAaSX041kbBLFMs81r8oPGtSw67sQm5NdTrrb7h4ObGD247C4tPAdX6M88V18rckA2gNh0KrOo17j7frhVsLfVpFTE+lnJ1SUm9P+2Oj93n8i5c9s/g2U21Zfih8eJmFDDTE8M3ZwmX/HI74XilxWAhBz0J5PJhkH/imQX4= 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:content-type; b=DUyqq496GuLu4jB6kYMf7lbrNYLU2qhr/0fk5cnVz1F6U37g3l0Ap70znr6pjGrvDaN+tbf1SnUj7lnmwOhT413aGPQ2+ygHzape9vFlL3CyACD8CrTSkp6PEwAGtQtrnmOf+R9nO5QHqKbYMcZoAt820NFuEI0yj6VdJ7Xi5ro= 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=PtOr0JvT; 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="PtOr0JvT" 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-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=z5VcU60xQeN7ydJcNuOrJoqdazEeOf9bmMF6YJ9dhbk=; b=PtOr0JvTQkBPjfcbE6v5FhDSnoTRKMxQkr5AiQa4LwXFomT8WagkYOLMKeI+GU3YqtSSRa SFl93AvzhQWP1X55eubeRw0ndEqCAZqb9f9xjJNdKvWe1ckWktduIQuAPnuejfnI31DWKP uxq2y9kyS/98vBe1BavaSBHiG3MPpYY= Received: from mail-wm1-f72.google.com (mail-wm1-f72.google.com [209.85.128.72]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-114-Y80gjB7MN72sOIIMJoTSdQ-1; Tue, 21 Oct 2025 08:59:49 -0400 X-MC-Unique: Y80gjB7MN72sOIIMJoTSdQ-1 X-Mimecast-MFC-AGG-ID: Y80gjB7MN72sOIIMJoTSdQ_1761051588 Received: by mail-wm1-f72.google.com with SMTP id 5b1f17b1804b1-470fd92ad57so112211245e9.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=jQh6TC5pmzNWvVRaYG68lJmdyn0D8v8t+7yNwn8iNaY2jKWsm+VMEQlgSi9QNMpxDn rOu8IxQEJs7Gratxlu8+t9JEZZjr7HBWZnnOFPMkhe+4ulU1NDfho7RJC9ofeEdv3Th7 9Y0PuM8QdZwKeN0q4iJlyjigANHpa/X91Wzggz53EZN8LdcNX+l0ZuNJBXcYkgBQRutp HjWWHjysEXBu5uOgeuj77kqPDHW5AcRx5hMoMoqjk7X9LEoCuV1bWnQhMUTUJq1jHLrT I2i6Q9teuLIQUXpzX1WW92GjH6a2Vy4CD5x7UJL3SQWLstdkscjlisapTxpKCXN4GUVf pubA== X-Forwarded-Encrypted: i=1; AJvYcCV/ipAOXUEInnWq7r/xw12FqvkwrYO9jpTmhWKKAtfktb8b6YmYr1CjZRrRjG2E5WTOKPTWSZHc7M7zrZcPXA==@lists.linux.dev X-Gm-Message-State: AOJu0Yw3pOCkkrajZd66fDFBLzwG6yNWmoYz222YBfm1aC7bFLi0X0uA oM7n1uzQPw898LSVkm9T+AsikZxFJm0lv0n95yQAsBjX8NdXZMnheL90EMjOG6EwD2ir5bWMWMU A2+QYzTOpATAAMJi0PmuCO+G8XR7uZqstAq5OYAwPnqUtlQg9zWlwsjJbGS6/pnHo1Zwn X-Gm-Gg: ASbGncsieqpMQcXGnkgV2YpGyAR845MzMPCGOk2Q1FkHinUEiL0kHH/YFIdOeEPxpPZ zJQIbMqcGx/5vxelL9M4CkelBODa1kOWxiWRrq7aMeoqNltPQvARTrH7BTFtQhsCVToEtMUti04 +SipaGgDCWIVmLfmrT6dJ96ZLsMxy7i3B8LliQtBE/IkakCJIt4rcyPtrQLVNBIGMuea8EGGdpn ozRs4tEaxGilPatl2+0tj45RM0QWDZKvc2f4z/RdD0A712Dm7NNxdUbG0AkULPap9/Rv56wo18V 5QbPjDoOk1Fvbn74ppxQZUW8QFoOnAHYpAAdXwJLOOI85aAgUcRInaFbzgm1mPawq8YYEe43Gt7 EzYprPZIXWai3E4YoQcJVeSpIVPOobRBArqQSsKygAlCyienxf3Uf6JPPOSCj X-Received: by 2002:a05:600c:45ca:b0:46e:59dd:1b4d with SMTP id 5b1f17b1804b1-471178a4bb0mr123277855e9.16.1761051588074; 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: virtualization@lists.linux.dev List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-MFC-PROC-ID: hMuYCeuKz_dylBae6W4gn5rpWEBoRBWusDOtjt8n9o0_1761051588 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: 8bit content-type: text/plain; charset="US-ASCII"; x-default=true 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_compaction.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 following * 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 with * the aforementioned balloon page corner case, as well as to ensure the simple @@ -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_dev_info *b_dev_info, static inline void balloon_devinfo_init(struct balloon_dev_info *balloon) { balloon->isolated_pages = 0; - spin_lock_init(&balloon->pages_lock); INIT_LIST_HEAD(&balloon->pages); balloon->migratepage = NULL; balloon->adjust_managed_page_count = 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 +/* + * 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_info *b_dev_info, unsigned long flags; size_t n_pages = 0; - 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 = 0; - 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 == n_req_pages) break; @@ -106,7 +112,7 @@ size_t balloon_page_list_dequeue(struct balloon_dev_info *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); return n_pages; } @@ -149,9 +155,9 @@ void balloon_page_enqueue(struct balloon_dev_info *b_dev_info, { unsigned long flags; - 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); @@ -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, isolate_mode_t mode) if (!b_dev_info) return false; - 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); return true; } @@ -230,10 +236,10 @@ static void balloon_page_putback(struct page *page) if (WARN_ON_ONCE(!b_dev_info)) return; - 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); } static int balloon_page_migrate(struct page *newpage, struct page *page, @@ -253,7 +259,7 @@ static int balloon_page_migrate(struct page *newpage, struct page *page, rc = 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); /* Insert the new page into the balloon list. */ get_page(newpage); @@ -272,7 +278,7 @@ static int balloon_page_migrate(struct page *newpage, struct page *page, } break; case -ENOENT: - spin_lock_irqsave(&b_dev_info->pages_lock, flags); + spin_lock_irqsave(&balloon_pages_lock, flags); /* 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, struct page *page, } b_dev_info->isolated_pages--; - spin_unlock_irqrestore(&b_dev_info->pages_lock, flags); + spin_unlock_irqrestore(&balloon_pages_lock, flags); /* Free the now-deflated page we isolated in balloon_page_isolate(). */ balloon_page_finalize(page); -- 2.51.0