Should I use "NULL" in kernel code?

Programming, for all ages and all languages.
Post Reply
songziming
Member
Member
Posts: 70
Joined: Fri Jun 28, 2013 1:48 am
Contact:

Should I use "NULL" in kernel code?

Post by songziming »

Hi,

In userspace programming I can safely use NULL (0) to indicate invalid pointer value. Because accessing address zero causes pagee fault.

In kernel should I still use NULL for invalid pointer value? Zero for invalid physical address and invalid page frame number?

It's possible that I want to use data from address zero, like allocating the first physical page, or access data dynamically mapped at address zero.

So for kernel, NULL and 0 shouldn't be seen as invalid address, right? What's the proper value for an invalid pointer? invalid physical address? Or am I just over complicating things?

Thanks
Reinventing the Wheel, code: https://github.com/songziming/wheel
nullplan
Member
Member
Posts: 1766
Joined: Wed Aug 30, 2017 8:24 am

Re: Should I use "NULL" in kernel code?

Post by nullplan »

songziming wrote: Wed Jul 31, 2024 11:47 am In userspace programming I can safely use NULL (0) to indicate invalid pointer value.
You can use NULL, or you can just use 0. NULL gives false assurances. Most people assume that NULL is defined as ((void *)0), but that is only one of the allowed implementations. In contexts where the final type is clear (initializations or named arguments), NULL and 0 are equivalent, and in cases where the type is not clear (variable arguments!) both must be cast to (void *) to be safe.
songziming wrote: Wed Jul 31, 2024 11:47 am Because accessing address zero causes pagee fault.
That is incidental, and indeed nothing says that the implementation implements the null pointer as a pointer to address zero. The standard only says that pointers must be convertible to integer type and back, and that uintptr_t and intptr_t are large enough for this conversion to round-trip without loss of information, but only for pointers to object type. Pointers to function type can be larger. And converting integer 0 to pointer type results in a null pointer, and all null pointers are equal, and all null pointers are unequal to any valid pointer.
songziming wrote: Wed Jul 31, 2024 11:47 am In kernel should I still use NULL for invalid pointer value? Zero for invalid physical address and invalid page frame number?

It's possible that I want to use data from address zero, like allocating the first physical page, or access data dynamically mapped at address zero.

So for kernel, NULL and 0 shouldn't be seen as invalid address, right? What's the proper value for an invalid pointer? invalid physical address? Or am I just over complicating things?
C already defines that the pointer you get from converting 0 to pointer type is an invalid pointer that cannot be dereferenced. Therefore you cannot do so. If you try, the compiler is allowed to miscompile it and do something else, and clang has already done so. If your compiler implements the integer-pointer conversion such that the null pointer is a pointer to address 0 (and most do), then 0 is off limits.

Thankfully, address 0 is rarely needed. If you have a kernel in virtual mode, address 0 names an address in user space, which you don't care about in kernel space. Only in identity-mapped mode or physical mode, you might want to access physical address 0, but that case is really rare.
Carpe diem!
Post Reply