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 06C9C26CE32 for ; Tue, 19 May 2026 23:15:15 +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=1779232518; cv=none; b=gt2V4x1sqlB6pqB2wkW4zL7jeeA2HFJQNK2DSJFvA2CqbM8VpiHdppfFRXbgLkAVRCCmTZvrSdH+5D924jsHJ1HqoPm/U5Dofp1A26piNL4zCFqsB+wun1QMXaWjB+APGZ4nzDSmC8HanCFd2vVZV7RHnjcuWzBKJFkcjpMU8zM= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1779232518; c=relaxed/simple; bh=lHDb2wCVccf4bMdzNh2Rk4e/R44omaI3P5HbRgBPcAE=; h=Message-ID:Date:MIME-Version:Subject:To:Cc:References:From: In-Reply-To:Content-Type; b=STBNkFIFSvcA8MTi7wXYmQvlm9BwjqA+EdqRB46vnv1F7NmcgFH/JDCEVmc9f5lVGokH1H1kpVpMdMZc6XsBZwnUVSocZdqj0rrkjhLqz/L2JVmho5Qz0RHJpw4bhY9hjCYVvyaZA3eO3XC/eoRnIy9JGajQfQZPJTYkViXZX/k= 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=JYlTRe1m; dkim=pass (2048-bit key) header.d=redhat.com header.i=@redhat.com header.b=LG+OG4eO; 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="JYlTRe1m"; dkim=pass (2048-bit key) header.d=redhat.com header.i=@redhat.com header.b="LG+OG4eO" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1779232514; 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=rH7Rm/MFTCSCauaDmPiyviKdVdO8hos6+4Wjad+hNUA=; b=JYlTRe1mUFxB6r7+J0LZYKaD8T4Rr+bFBdUJuNkY/R69mF5QFynfOZRNNVuDG+30ppcK1j ba5IuKSXQ4OGoh49HLxjFNtebrnaEptmD4RJYgslz/uSiuULdTIcZ8sVDlYcO5tk8nn1+c 8brGb1Az917dSVGDUkAqb8/+oGjzgQM= Received: from mail-qv1-f71.google.com (mail-qv1-f71.google.com [209.85.219.71]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-592-ogqUPd6sPcy516yGpU103A-1; Tue, 19 May 2026 19:15:13 -0400 X-MC-Unique: ogqUPd6sPcy516yGpU103A-1 X-Mimecast-MFC-AGG-ID: ogqUPd6sPcy516yGpU103A_1779232513 Received: by mail-qv1-f71.google.com with SMTP id 6a1803df08f44-8ca1e0f5a4bso127920646d6.2 for ; Tue, 19 May 2026 16:15:13 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=google; t=1779232513; x=1779837313; darn=vger.kernel.org; h=content-transfer-encoding:in-reply-to:from:content-language :references:cc:to:subject:user-agent:mime-version:date:message-id :from:to:cc:subject:date:message-id:reply-to; bh=rH7Rm/MFTCSCauaDmPiyviKdVdO8hos6+4Wjad+hNUA=; b=LG+OG4eO8Efy20RbotXKNtIxKqo3lsRNhPR0HvXfFg2LlBy0YvdZujfayM5pBuOP+r nZ8/w8qL5PbTUZsb6V8I1TK/SsIdk8f+r1ubR3NYSMFQwAVSmmopCONlBclFFKUOsCMa Q8sA4B7mbG/iWdqDoCo+e3kOHjLoVxl9xAhdIn91qH25S4bmG7Yk2yOq/hXVJ2b5gTBk wlDMwgIRLfKrqkCGHJeH20tZtUSMYK4AtiTwVIXrA0feF5rcV7go5LvkrJVFJLPQOv99 U0tzpbwVrOxUlBQHu2dhkyP5fSOipMTq0HrhiJe1Zs/hFYWQOVidxdGC5pwEcCBDxINW BpzQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1779232513; x=1779837313; h=content-transfer-encoding:in-reply-to:from:content-language :references:cc:to:subject:user-agent:mime-version:date:message-id :x-gm-gg:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=rH7Rm/MFTCSCauaDmPiyviKdVdO8hos6+4Wjad+hNUA=; b=Y08nErBYhADpincWz2bsjTxscKD4J/qj6+FcvFs0oHOR1FQ1bZ1IGTJa2WpXmZ+jjX APdWkbMyt/csLZq1Da4sAFNJ2h9j5pA/TokW70+uOKRB6rGV955sMQyLPl9hEfx4tpzq 1kCBbG2YBoTaOgyLzybq67RhysV5o5LPipldkQ5oisg/E3ZZJyoE1P08Oq0JcZDU6Emj g2FbkIHD5fEytnw5cHSrvFk2NxkBy1XoO7lVx5VT0hNO6L3tvjHCXcXR/5zPg/vsarmE 1dNdbBLnt8ftVCrQi9u79x1iZNz5Qluqulld9AlE2NVKawWukmnaRxPg3XprfarE1+E0 RmaQ== X-Forwarded-Encrypted: i=1; AFNElJ/Nwn0sOqPrsshfhywG0nx5n5ue85YRF9TILKeEWXEzGx1qhLnqFoVR7wFzrMU9AttEUHDw09YdNJ05GMI=@vger.kernel.org X-Gm-Message-State: AOJu0YxB2G9kmzbuTXBkX1+z8yjKXp6zPzWFpTCAOA5MQKRBplN9dSZP jWccdfHhhUxYpFkHTAO2uW3R/jV5GUx/UJmhPZFBX+GFOJJFN0MiOv0eMT/wR6EKRXwYQnKA6/F BE2134cSpvAN9bcL1IjO7SoWL+7xFysxwK5SMvS+ztS8f4ZUMAmZwOLEYqzAkixfogEL5xs10Jw == X-Gm-Gg: Acq92OFtdwA0u5XIRFwDLBuf4J60Se6FXBYt83ytCJfkVuhJomM4bqVS6a8vlYvByud /4L9PgiS3BiT4gbHY3ll5KlAJinhxsnf6Lvp1fsJo7APpgjXSsZUmfN6jNSGn0WE0yQYABbIHJg ekBJvl/v7Y3Bm7irYhTgjqfdaBsSrWRH17irNTD+tRdy40+ojVIYM3+Z3RSj3NTjYOELwI2HwgC 8ri/h5/EAIeCUVHx/CNcsPAv2KM6yLYG1gH2h43I//+Ygglq3J0116ztjnVubEALazIEp/OE1BJ babkNObNh0Q+2DqBMw8ZQWmQ24YMkt5WDlsUtR3xw6tBmS151E2tFKfzxXYpyWs0/0duv7T1BEB 2R/BXRiuVJhAZr0HdClpg/+5HPz37Qu4BdsmRc2+8rSG0kJ0pv2suMz/gTmh38s+/aUi3VPWYFT 3Wfjab X-Received: by 2002:a05:620a:698a:b0:8cd:8884:2509 with SMTP id af79cd13be357-911cf2f0e0cmr3317756785a.55.1779232512616; Tue, 19 May 2026 16:15:12 -0700 (PDT) X-Received: by 2002:a05:620a:698a:b0:8cd:8884:2509 with SMTP id af79cd13be357-911cf2f0e0cmr3317747985a.55.1779232512071; Tue, 19 May 2026 16:15:12 -0700 (PDT) Received: from ?IPV6:2600:4040:530f:2500:540d:8da2:f197:6711? ([2600:4040:530f:2500:540d:8da2:f197:6711]) by smtp.gmail.com with ESMTPSA id af79cd13be357-910ba9454basm2007293485a.12.2026.05.19.16.15.11 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Tue, 19 May 2026 16:15:11 -0700 (PDT) Message-ID: Date: Tue, 19 May 2026 19:15:11 -0400 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: dm-vdo: iterator after loop end in mark_slab_journal_dirty? To: Maoyi Xie Cc: dm-devel@lists.linux.dev, linux-kernel@vger.kernel.org References: <20260519094132.1964544-1-maoyixie.tju@gmail.com> Content-Language: en-US From: Matthew Sakai In-Reply-To: <20260519094132.1964544-1-maoyixie.tju@gmail.com> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit On 5/19/26 5:41 AM, Maoyi Xie wrote: > Hi all, > > While reading drivers/md/dm-vdo/slab-depot.c I noticed > something that looks like a past the end iterator pattern. > I would appreciate it if you could take a look and let me > know whether this is a real issue, and whether it is worth > fixing. > > The site is mark_slab_journal_dirty() (linux-7.1-rc1, around > line 156): > > list_for_each_entry_reverse(dirty_journal, dirty_list, > dirty_entry) { > if (dirty_journal->recovery_lock <= > journal->recovery_lock) > break; > } > > list_move_tail(&journal->dirty_entry, > dirty_journal->dirty_entry.next); > > When the loop walks all entries without break (the new > journal's recovery_lock is smaller than every existing > entry), dirty_journal is past the end. > dirty_journal->dirty_entry then aliases dirty_list (the > list head) via container_of offset cancellation. So > dirty_journal->dirty_entry.next is head->next, the first > real entry. list_move_tail then inserts journal just > before the first entry, i.e. at the front of the list. > That is the intended behaviour for a sorted insertion > where the new entry has the smallest recovery_lock. The > dereference of the past the end iterator is undefined per > C11. I don't quite follow your train of thought here. Which dereference do you think produces undefined behavior, and why? Everything you describe sounds valid. > Jakob Koschel cleaned up many such sites in 2022, for > example commits 99d8ae4ec8a (tracing: Remove usage of list > iterator variable after the loop), 2966a9918df (clockevents: > Use dedicated list iterator variable) and dc1acd5c946 (dlm: > replace usage of found with dedicated list iterator > variable). This site was not covered. Jakob's series did not address this driver, because this driver was added to the Linux kernel after 2022. I think I understand your point that the code really shouldn't use dirty_journal outside the loop, since it doesn't point to a valid struct slab_journal object. I certainly wouldn't mind a patch to clean this up a bit (perhaps by doing the list insertion inside the loop when possible). But as a practical matter it doesn't seem to matter all that much, since this function is only manipulating the list heads. If you do decide to patch this, it would be nice to clean up the list insertion, too. It's more indirect than it needs to be. > A candidate fix would track an explicit insertion target. > Declare `struct list_head *insert_after = dirty_list` before > the loop. Overwrite it to `&dirty_journal->dirty_entry` only > when the loop breaks early. The final list_move_tail then > reads `insert_after->next`. On break that points to the > entry right after the insertion position. On fall-through > it points to `head->next`, the first real entry. The > behaviour is unchanged in all cases, including an empty > list and a list with one entry. Please don't fix it like this, this would just makes the code messier. Matt > If this is intentional or already known, please disregard. > Otherwise, I am happy to send a [PATCH] or to leave the fix > to you. Thank you for your time, and sorry for the noise if > this is not actually worth fixing or has already been > spotted. > > Thanks, > Maoyi Xie > https://maoyixie.com/ >