Skip to contentSkip to conversation

ocfs2/heartbeat: Unlink live slots when their node is missing

PATCH Superseded Cen Zhang 1 participant 2 messages Oct 9 1 message not in the archive OCFS2

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Cen Zhang

author
#0 lore raw

PATCH 1/1 ocfs2/heartbeat: Unlink live slots when their node is missing

o2hb_shutdown_slot() returns immediately when its node is no longer in the configfs registry. A slot can still be linked in o2hb_live_slots when that lookup fails. Region teardown or startup-error cleanup then frees the slot array without removing its embedded list member.

With the same peer live in another region, the next list update can follow a predecessor into the freed array. Reusing the node number can also make a later insertion encounter that stale member.

Always remove the slot under o2hb_live_lock, even when node lookup fails. Preserve aggregate membership accounting and the last-region DOWN callback: the existing callback interface explicitly permits a NULL node for DOWN events. Only drop the node reference when lookup returned one. This closes both normal release and startup-abort paths without changing the publication or callback locking protocol.

A controlled probe-instrumented test reproduced this while stopping one of two regions after removing the peer from configfs. Its KASAN report includes:

  BUG: KASAN: slab-use-after-free in __list_del_entry_valid_or_report+0x1e0/0x210
  Read of size 8 at addr ffff888106a4ac60 by task o2hb-pmbd0058_b/497
  Call Trace:
   <TASK>
   dump_stack_lvl+0x93/0xd0
   print_report+0xce/0x630
   kasan_report+0xe0/0x110
   __list_del_entry_valid_or_report+0x1e0/0x210
   o2hb_do_disk_heartbeat+0x1591/0x2c50
   o2hb_thread+0x1ed/0xe70
   kthread+0x351/0x460
   ret_from_fork+0x659/0x940
   ret_from_fork_asm+0x1a/0x30
   </TASK>
  Allocated by task 491:
   kasan_save_stack+0x33/0x60
   kasan_save_track+0x14/0x30
   __kasan_kmalloc+0xaa/0xb0
   __kmalloc_noprof+0x2de/0x780
   o2hb_region_dev_store+0x7c2/0x1b60
   configfs_write_iter+0x2f9/0x4f0
   vfs_write+0x639/0x1010
   ksys_write+0x111/0x200
   do_syscall_64+0x114/0x620
   entry_SYSCALL_64_after_hwframe+0x77/0x7f
  Freed by task 491:
   kasan_save_stack+0x33/0x60
   kasan_save_track+0x14/0x30
   kasan_save_free_info+0x3b/0x60
   __kasan_slab_free+0x5f/0x80
   kfree+0x308/0x580
   o2hb_unmap_slot_data+0x1db/0x330
   o2hb_region_release+0x14b/0x4f0
   config_item_cleanup+0x14b/0x230
   config_item_put+0x79/0x90
   configfs_rmdir+0x58a/0x910
   vfs_rmdir+0x2e8/0x820
   filename_rmdir+0x3b1/0x520
   __x64_sys_rmdir+0x4b/0x70
   do_syscall_64+0x114/0x620
   entry_SYSCALL_64_after_hwframe+0x77/0x7f
Fixes a7f6a5fb4bde ("[PATCH] OCFS2: The Second Oracle Cluster Filesystem")
Assisted-by LLM
---
 fs/ocfs2/cluster/heartbeat.c | 10 +++++++---
 1 file changed, 7 insertions(+), 3 deletions(-)
fs/ocfs2/cluster/heartbeat.c +7 -3
index 1c3def99bb0765deb828ad6415fde31ed9316002..95b9061fce854ac8f32acf3c29f71336fb37a372 100644
@@ -880,8 +880,11 @@ static void o2hb_shutdown_slot(struct o2hb_disk_slot *slot)
 	int queued = 0;
 
 	node = o2nm_get_node_by_num(slot->ds_node_num);
-	if (!node)
-		return;
+	/*
+	 * The node may have been removed from the node table while this
+	 * region's slot is still linked.  The list member must still be
+	 * removed before the region releases its slot array.
+	 */
 
 	spin_lock(&o2hb_live_lock);
 	if (!list_empty(&slot->ds_live_item)) {
@@ -903,7 +906,8 @@ static void o2hb_shutdown_slot(struct o2hb_disk_slot *slot)
 	if (queued)
 		o2hb_run_event_list(&event);
 
-	o2nm_node_put(node);
+	if (node)
+		o2nm_node_put(node);
 }
 
 static void o2hb_set_quorum_device(struct o2hb_region *reg)

A message that is not in the archive (sent off-list or not yet received): [email protected]

author
#2 lore raw

Hi,

Thanks for the review. The reported race is real: v1 closed the unlink path, but o2hb_check_slot() could still admit a slot with a NULL node after the last live slot in another region was removed, queuing an UP event that hits the BUG_ON in o2hb_queue_node_event().

v2 will add the missing admission-side guard.

Quoted 73 lines · <[email protected]> 于2026年10月9日周五 09:51写道:

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Critical] ocfs2: BUG_ON triggered by TOCTOU race in o2hb_check_slot after node removal --

commit d6d3e4144add75ad80b3ba9eaf567ae43865596c
Author: Cen Zhang <[email protected]>

ocfs2/heartbeat: Unlink live slots when their node is missing

o2hb_shutdown_slot() returns immediately when its node is no longer in the configfs registry. A slot can still be linked in o2hb_live_slots when that lookup fails. Region teardown or startup-error cleanup then frees the slot array without removing its embedded list member.

Quoted 8 lines
fs/ocfs2/cluster/heartbeat.c +0 -0
index a4c8ea695f5cf..43d98b4dce7da 100644
@@ -880,8 +880,11 @@ static void o2hb_shutdown_slot(struct o2hb_disk_slot *slot)
      int queued = 0;
 
      node = o2nm_get_node_by_num(slot->ds_node_num);

fs/ocfs2/cluster/heartbeat.c static void o2hb_shutdown_slot(struct o2hb_disk_slot *slot) · line 890

Quoted 10 lines
fs/ocfs2/cluster/heartbeat.c +5 -2
-     if (!node)
-             return;
+     /*
+      * The node may have been removed from the node table while this
+      * region's slot is still linked.  The list member must still be
+      * removed before the region releases its slot array.
+      */
 
      spin_lock(&o2hb_live_lock);
      if (!list_empty(&slot->ds_live_item)) {

[Severity: Critical]
If a remote node is removed from configfs but still heartbeating in multiple regions (e.g., A and B), o2nm_get_node_by_num() returns NULL.

In Region A, o2hb_check_slot() evaluates the node. The node is NULL. It checks o2hb_live_node_bitmap and finds it true (since Region B considers it live). Region A drops o2hb_live_lock and proceeds.

Concurrently, Region B shuts down. With this patch, o2hb_shutdown_slot() successfully removes the node's last live item from o2hb_live_slots and clears the bitmap, even though node == NULL.

Region A resumes, increments ds_changed_samples past O2HB_LIVE_THRESHOLD, and re-acquires o2hb_live_lock in o2hb_check_slot().

Region A checks list_empty(&o2hb_live_slots[slot->ds_node_num]). Since Region B just emptied it during the TOCTOU window, this evaluates to true.

Region A calls o2hb_queue_node_event(..., O2HB_NODE_UP_CB, node, ...).

Since node is NULL, this directly triggers the explicit BUG_ON in o2hb_queue_node_event():

fs/ocfs2/cluster/heartbeat.c:o2hb_queue_node_event() {
    ...
    BUG_ON((!node) && (type != O2HB_NODE_DOWN_CB));
    ...
}

Will this regression cause a kernel panic due to the BUG_ON when the race condition is met?

While the race theoretically existed prior to the patch via the node dead timeout path, this patch directly triggers it during normal region teardown or startup-abort by deliberately allowing o2hb_live_slots to be emptied while node == NULL.

--
Sashiko AI review · https://sashiko.dev/#/patchset/pm-ocfs2-objects-candidate-0058-v1-177573785904d0ea43c7@gmail.com?part=1