|
|
Subscribe / Log in / New account

The perils of pinning

The perils of pinning

Posted Sep 20, 2022 8:40 UTC (Tue) by farnz (subscriber, #17727)
In reply to: The perils of pinning by kreijack
Parent article: The perils of pinning

I'm going to stick to C syntax throughout, since I think that's easier for kernel programmers to follow than Rust.

In C, you might have code like:

struct NetworkBuffer {
    struct IpHeader ip_hdr;
    union {
        struct TcpSegment tcp;
        struct UdpSegment udp;
    };
};

volatile * NetworkBuffer buf;

It's then tempting (but often wrong) to write code that does things like buf->ip_hdr.src_addr, when this isn't actually a good idea because of the volatile reads that will be done.

Rust doesn't have a volatile qualifier on storage that changes all codegen accessing that storage. Instead it has the equivalent of void * memcpy_volatile(void * dest, volatile void * src, size_t count); (but using generics from Rust's type system to replace void * and size_t count). Because your only dependable operation on the buffer is to copy out of the shared space that can change underneath you at no notice (imagine that, for example, NetworkBuffer actually lives in memory the far side of the PCIe bus, on the NIC itself), that's what you'll do.


to post comments


Copyright © 2025, Eklektix, Inc.
Comments and public postings are copyrighted by their creators.
Linux is a registered trademark of Linus Torvalds