Skip to content

rust: kernel: add missing safety comments #450

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: rust
Choose a base branch
from

Conversation

Ayush1325
Copy link

  • Added safety in allocators.rs

related to #351
Signed-off-by: Ayush Singh [email protected]

@Ayush1325 Ayush1325 marked this pull request as draft July 18, 2021 13:37
@Ayush1325 Ayush1325 marked this pull request as ready for review July 18, 2021 14:25
Copy link

@LeSeulArtichaut LeSeulArtichaut left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The goal of SAFETY comments is to explain why the unsafe operations performed in an unsafe block are safe. I'm not familiar with kernel APIs but I tried to write two examples, I hope they are useful.

Comment on lines 15 to 16
// Safety: Returns a raw Pointer. Will return NULL in case of error.
// `krealloc()` is used instead of `kmalloc()` because the latter is
// an inline function and cannot be bound to as a result.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think malloc has particular safety requirements (docs), so this can simply be

Suggested change
// Safety: Returns a raw Pointer. Will return NULL in case of error.
// `krealloc()` is used instead of `kmalloc()` because the latter is
// an inline function and cannot be bound to as a result.
// `krealloc()` is used instead of `kmalloc()` because the latter is
// an inline function and cannot be bound to as a result.
// SAFETY: FFI call.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I also mentioned the C function name, `krealloc' in the new commit only FFI call might be confusing in places where the rust function is named differently.

// `krealloc()` is used instead of `kmalloc()` because the latter is
// an inline function and cannot be bound to as a result.
unsafe { bindings::krealloc(ptr::null(), layout.size(), bindings::GFP_KERNEL) as *mut u8 }
}

unsafe fn dealloc(&self, ptr: *mut u8, _layout: Layout) {
// Safety: Freeing memory not allocated with kmalloc() can cause undefined behavior.
Copy link

@LeSeulArtichaut LeSeulArtichaut Jul 31, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As you noted kfree should only be called on memory allocated with kmalloc. However, as stated in the GlobalAlloc::dealloc docs the caller has to guarantee that:

  • ptr must denote a block of memory currently allocated via this allocator,
  • layout must be the same layout that was used to allocate that block of memory.

So the SAFETY comment could look like:

Suggested change
// Safety: Freeing memory not allocated with kmalloc() can cause undefined behavior.
// SAFETY: the caller must guarantee that `ptr` and `layout` denote memory
// allocated by this allocator, so allocated with `kmalloc`.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have made the edits as suggested.

@@ -14,10 +14,14 @@ unsafe impl GlobalAlloc for KernelAllocator {
unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
// `krealloc()` is used instead of `kmalloc()` because the latter is
// an inline function and cannot be bound to as a result.
// SAFETY: FFI call to krealloc.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Elsewhere we do not mention the function being called:

Suggested change
// SAFETY: FFI call to krealloc.
// SAFETY: FFI call.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok. Removed mention of the function being called.

@ojeda
Copy link
Member

ojeda commented Aug 1, 2021

Please rebase and reword the commit message (take a look at Documentation/process/submitting-patches.rst).

@Ayush1325
Copy link
Author

Please rebase and reword the commit message (take a look at Documentation/process/submitting-patches.rst).

I did the rebase and modeled my latest commit message after a few other safety commits I could find. Do I need to add the signed-off-by line in my previous commits too?

@ojeda
Copy link
Member

ojeda commented Aug 1, 2021

The kernel does not take patches (commits) that fix patches from the same series (PR). In other words, this should be a single commit.

- Added safety comments for rust/kernel/allocator.rs

Signed-off-by: Ayush Singh <[email protected]>
@Ayush1325
Copy link
Author

Ayush1325 commented Aug 1, 2021

The kernel does not take patches (commits) that fix patches from the same series (PR). In other words, this should be a single commit.

I squashed the commits to a single commit. I am assuming that's how it is supposed to be done? I haven't combined commits before.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

3 participants