rust-for-linux.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Abdiel Janulgue <abdiel.janulgue@gmail.com>
To: Lyude Paul <lyude@redhat.com>, dakr@kernel.org
Cc: "Miguel Ojeda" <ojeda@kernel.org>,
	"Alex Gaynor" <alex.gaynor@gmail.com>,
	"Boqun Feng" <boqun.feng@gmail.com>,
	"Gary Guo" <gary@garyguo.net>,
	"Björn Roy Baron" <bjorn3_gh@protonmail.com>,
	"Benno Lossin" <benno.lossin@proton.me>,
	"Andreas Hindborg" <a.hindborg@kernel.org>,
	"Alice Ryhl" <aliceryhl@google.com>,
	"Trevor Gross" <tmgross@umich.edu>,
	"Valentin Obst" <kernel@valentinobst.de>,
	"open list" <linux-kernel@vger.kernel.org>,
	"Marek Szyprowski" <m.szyprowski@samsung.com>,
	"Robin Murphy" <robin.murphy@arm.com>,
	airlied@redhat.com, rust-for-linux@vger.kernel.org,
	"open list:DMA MAPPING HELPERS" <iommu@lists.linux.dev>,
	"Petr Tesarik" <petr@tesarici.cz>,
	"Andrew Morton" <akpm@linux-foundation.org>,
	"Herbert Xu" <herbert@gondor.apana.org.au>,
	"Sui Jingfeng" <sui.jingfeng@linux.dev>,
	"Randy Dunlap" <rdunlap@infradead.org>,
	"Michael Kelley" <mhklinux@outlook.com>
Subject: Re: [RFC PATCH 1/2] rust: add initial scatterlist bindings
Date: Mon, 26 May 2025 16:04:09 +0300	[thread overview]
Message-ID: <44a70b0c-af38-4706-90e3-0e0a7f6ad6f8@gmail.com> (raw)
In-Reply-To: <36b45e41522a61409b379c15f21bb547d4913d1e.camel@redhat.com>

Hi Lyude,

On 15/05/2025 23:01, Lyude Paul wrote:
> 
> I think you might have made a mistake below
> 
>> +impl<'a> Iterator for SGTableIter<'a> {
>> +    type Item = &'a SGEntry;
>> +
>> +    fn next(&mut self) -> Option<Self::Item> {
>> +        self.pos.map(|entry| {
>> +            let sg = entry.as_raw();
> 
>                 ^ sg is the last iterator position
> 
>> +            // SAFETY: `sg` is guaranteed to be valid and non-NULL while inside this closure.
>> +            unsafe {
>> +                let next = bindings::sg_next(sg);
> 
>                     ^ and we fetch the next iterator position
> 
>> +                self.pos = (!next.is_null()).then(|| SGEntry::as_ref(next));
>> +                SGEntry::as_ref(sg)
> 
>                     ^ but then we return the previous iterator position, sg
>> +            }
> 

Just had a second look at this again.I think the previous code is okay. 
I integrated the iterator fixes you suggested, however the mapped 
sgtable crashes on dma_unmap_sgtable().

One hint is that the fix seems to skip the initial entry of the sgtable? 
The previous code doesn't skip the initial entry when iterating:

fn next(&mut self) -> Option<Self::Item> {

   self.pos.take().map(|entry| {
      let sg = entry.as_raw();
      ^ sg is the current iterator position from self.pos

      let next = bindings::sg_next(sg);
      self.pos = (!next.is_null()).then(|| SGEntry::as_mut(next));
      ^ update the iterator position for the next loop to sg_next()
        or set to null if sg_next() is the last entry

      SGEntry::as_mut(sg)
      ^ Return the current iterator position
    })
}


Regards,
Abdiel

  parent reply	other threads:[~2025-05-26 13:04 UTC|newest]

Thread overview: 24+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-05-12  9:53 [RFC PATCH 0/2] scatterlist rust bindings Abdiel Janulgue
2025-05-12  9:53 ` [RFC PATCH 1/2] rust: add initial scatterlist bindings Abdiel Janulgue
2025-05-12 11:39   ` Daniel Almeida
2025-05-15 19:26     ` Lyude Paul
2025-05-15 21:11     ` Danilo Krummrich
2025-05-16 16:57       ` Daniel Almeida
2025-05-16 17:55         ` Danilo Krummrich
2025-05-12 16:42   ` Jason Gunthorpe
2025-05-12 20:01     ` Daniel Almeida
2025-05-12 20:10       ` Danilo Krummrich
2025-05-14  8:29   ` Alexandre Courbot
2025-05-14 12:50     ` Alexandre Courbot
2025-05-16  7:52       ` Abdiel Janulgue
2025-05-15 20:01   ` Lyude Paul
2025-05-16  7:52     ` Abdiel Janulgue
2025-05-26 13:04     ` Abdiel Janulgue [this message]
2025-05-12  9:53 ` [RFC PATCH 2/2] samples: rust: add sample code for " Abdiel Janulgue
2025-05-12 11:19 ` [RFC PATCH 0/2] scatterlist rust bindings Daniel Almeida
2025-05-14  7:00   ` Abdiel Janulgue
2025-05-14 12:12     ` Marek Szyprowski
2025-05-16  7:47       ` Abdiel Janulgue
2025-05-13  2:19 ` Herbert Xu
2025-05-13  5:50   ` Christoph Hellwig
2025-05-13  7:38     ` Petr Tesařík

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=44a70b0c-af38-4706-90e3-0e0a7f6ad6f8@gmail.com \
    --to=abdiel.janulgue@gmail.com \
    --cc=a.hindborg@kernel.org \
    --cc=airlied@redhat.com \
    --cc=akpm@linux-foundation.org \
    --cc=alex.gaynor@gmail.com \
    --cc=aliceryhl@google.com \
    --cc=benno.lossin@proton.me \
    --cc=bjorn3_gh@protonmail.com \
    --cc=boqun.feng@gmail.com \
    --cc=dakr@kernel.org \
    --cc=gary@garyguo.net \
    --cc=herbert@gondor.apana.org.au \
    --cc=iommu@lists.linux.dev \
    --cc=kernel@valentinobst.de \
    --cc=linux-kernel@vger.kernel.org \
    --cc=lyude@redhat.com \
    --cc=m.szyprowski@samsung.com \
    --cc=mhklinux@outlook.com \
    --cc=ojeda@kernel.org \
    --cc=petr@tesarici.cz \
    --cc=rdunlap@infradead.org \
    --cc=robin.murphy@arm.com \
    --cc=rust-for-linux@vger.kernel.org \
    --cc=sui.jingfeng@linux.dev \
    --cc=tmgross@umich.edu \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).