Skip to content

Commit b978096

Browse files
committed
Merge branch 'rust-next' of https://github.com/Rust-for-Linux/linux.git
2 parents 6b3196c + 3857af3 commit b978096

File tree

21 files changed

+279
-336
lines changed

21 files changed

+279
-336
lines changed

Documentation/process/changes.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ you probably needn't concern yourself with pcmciautils.
3131
====================== =============== ========================================
3232
GNU C 5.1 gcc --version
3333
Clang/LLVM (optional) 11.0.0 clang --version
34-
Rust (optional) 1.71.1 rustc --version
34+
Rust (optional) 1.73.0 rustc --version
3535
bindgen (optional) 0.65.1 bindgen --version
3636
GNU make 3.82 make --version
3737
bash 4.2 bash --version

Documentation/rust/index.rst

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,25 @@ Rust
66
Documentation related to Rust within the kernel. To start using Rust
77
in the kernel, please read the quick-start.rst guide.
88

9+
10+
The Rust experiment
11+
-------------------
12+
13+
The Rust support was merged in v6.1 into mainline in order to help in
14+
determining whether Rust as a language was suitable for the kernel, i.e. worth
15+
the tradeoffs.
16+
17+
Currently, the Rust support is primarily intended for kernel developers and
18+
maintainers interested in the Rust support, so that they can start working on
19+
abstractions and drivers, as well as helping the development of infrastructure
20+
and tools.
21+
22+
If you are an end user, please note that there are currently no in-tree
23+
drivers/modules suitable or intended for production use, and that the Rust
24+
support is still in development/experimental, especially for certain kernel
25+
configurations.
26+
27+
928
.. only:: rustdoc and html
1029

1130
You can also browse `rustdoc documentation <rustdoc/kernel/index.html>`_.

MAINTAINERS

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18887,9 +18887,10 @@ R: Andreas Hindborg <a.hindborg@samsung.com>
1888718887
R: Alice Ryhl <aliceryhl@google.com>
1888818888
L: rust-for-linux@vger.kernel.org
1888918889
S: Supported
18890-
W: https://github.com/Rust-for-Linux/linux
18890+
W: https://rust-for-linux.com
1889118891
B: https://github.com/Rust-for-Linux/linux/issues
1889218892
C: zulip://rust-for-linux.zulipchat.com
18893+
P: https://rust-for-linux.com/contributing
1889318894
T: git https://github.com/Rust-for-Linux/linux.git rust-next
1889418895
F: Documentation/rust/
1889518896
F: rust/

arch/x86/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ ifeq ($(CONFIG_X86_KERNEL_IBT),y)
8181
# https://gcc.gnu.org/bugzilla/show_bug.cgi?id=104816
8282
#
8383
KBUILD_CFLAGS += $(call cc-option,-fcf-protection=branch -fno-jump-tables)
84+
KBUILD_RUSTFLAGS += -Zcf-protection=branch -Zno-jump-tables
8485
else
8586
KBUILD_CFLAGS += $(call cc-option,-fcf-protection=none)
8687
endif

rust/Makefile

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -336,13 +336,13 @@ quiet_cmd_bindgen = BINDGEN $@
336336
$(bindgen_target_cflags) $(bindgen_target_extra)
337337

338338
$(obj)/bindings/bindings_generated.rs: private bindgen_target_flags = \
339-
$(shell grep -v '^#\|^$$' $(srctree)/$(src)/bindgen_parameters)
339+
$(shell grep -Ev '^#|^$$' $(srctree)/$(src)/bindgen_parameters)
340340
$(obj)/bindings/bindings_generated.rs: $(src)/bindings/bindings_helper.h \
341341
$(src)/bindgen_parameters FORCE
342342
$(call if_changed_dep,bindgen)
343343

344344
$(obj)/uapi/uapi_generated.rs: private bindgen_target_flags = \
345-
$(shell grep -v '^#\|^$$' $(srctree)/$(src)/bindgen_parameters)
345+
$(shell grep -Ev '^#|^$$' $(srctree)/$(src)/bindgen_parameters)
346346
$(obj)/uapi/uapi_generated.rs: $(src)/uapi/uapi_helper.h \
347347
$(src)/bindgen_parameters FORCE
348348
$(call if_changed_dep,bindgen)
@@ -364,9 +364,7 @@ $(obj)/bindings/bindings_helpers_generated.rs: $(src)/helpers.c FORCE
364364
quiet_cmd_exports = EXPORTS $@
365365
cmd_exports = \
366366
$(NM) -p --defined-only $< \
367-
| grep -E ' (T|R|D) ' | cut -d ' ' -f 3 \
368-
| xargs -Isymbol \
369-
echo 'EXPORT_SYMBOL_RUST_GPL(symbol);' > $@
367+
| awk '/ (T|R|D) / {printf "EXPORT_SYMBOL_RUST_GPL(%s);\n",$$3}' > $@
370368

371369
$(obj)/exports_core_generated.h: $(obj)/core.o FORCE
372370
$(call if_changed,exports)

rust/alloc/alloc.rs

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,7 @@
66

77
#[cfg(not(test))]
88
use core::intrinsics;
9-
use core::intrinsics::{min_align_of_val, size_of_val};
109

11-
use core::ptr::Unique;
1210
#[cfg(not(test))]
1311
use core::ptr::{self, NonNull};
1412

@@ -40,7 +38,6 @@ extern "Rust" {
4038
#[rustc_nounwind]
4139
fn __rust_alloc_zeroed(size: usize, align: usize) -> *mut u8;
4240

43-
#[cfg(not(bootstrap))]
4441
static __rust_no_alloc_shim_is_unstable: u8;
4542
}
4643

@@ -98,7 +95,6 @@ pub unsafe fn alloc(layout: Layout) -> *mut u8 {
9895
unsafe {
9996
// Make sure we don't accidentally allow omitting the allocator shim in
10097
// stable code until it is actually stabilized.
101-
#[cfg(not(bootstrap))]
10298
core::ptr::read_volatile(&__rust_no_alloc_shim_is_unstable);
10399

104100
__rust_alloc(layout.size(), layout.align())
@@ -339,22 +335,6 @@ unsafe fn exchange_malloc(size: usize, align: usize) -> *mut u8 {
339335
}
340336
}
341337

342-
#[cfg_attr(not(test), lang = "box_free")]
343-
#[inline]
344-
// This signature has to be the same as `Box`, otherwise an ICE will happen.
345-
// When an additional parameter to `Box` is added (like `A: Allocator`), this has to be added here as
346-
// well.
347-
// For example if `Box` is changed to `struct Box<T: ?Sized, A: Allocator>(Unique<T>, A)`,
348-
// this function has to be changed to `fn box_free<T: ?Sized, A: Allocator>(Unique<T>, A)` as well.
349-
pub(crate) unsafe fn box_free<T: ?Sized, A: Allocator>(ptr: Unique<T>, alloc: A) {
350-
unsafe {
351-
let size = size_of_val(ptr.as_ref());
352-
let align = min_align_of_val(ptr.as_ref());
353-
let layout = Layout::from_size_align_unchecked(size, align);
354-
alloc.deallocate(From::from(ptr.cast()), layout)
355-
}
356-
}
357-
358338
// # Allocation error handler
359339

360340
#[cfg(not(no_global_oom_handling))]
@@ -414,7 +394,6 @@ pub mod __alloc_error_handler {
414394
static __rust_alloc_error_handler_should_panic: u8;
415395
}
416396

417-
#[allow(unused_unsafe)]
418397
if unsafe { __rust_alloc_error_handler_should_panic != 0 } {
419398
panic!("memory allocation of {size} bytes failed")
420399
} else {

rust/alloc/boxed.rs

Lines changed: 39 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -159,12 +159,12 @@ use core::hash::{Hash, Hasher};
159159
use core::iter::FusedIterator;
160160
use core::marker::Tuple;
161161
use core::marker::Unsize;
162-
use core::mem;
162+
use core::mem::{self, SizedTypeProperties};
163163
use core::ops::{
164164
CoerceUnsized, Deref, DerefMut, DispatchFromDyn, Generator, GeneratorState, Receiver,
165165
};
166166
use core::pin::Pin;
167-
use core::ptr::{self, Unique};
167+
use core::ptr::{self, NonNull, Unique};
168168
use core::task::{Context, Poll};
169169

170170
#[cfg(not(no_global_oom_handling))]
@@ -483,8 +483,12 @@ impl<T, A: Allocator> Box<T, A> {
483483
where
484484
A: Allocator,
485485
{
486-
let layout = Layout::new::<mem::MaybeUninit<T>>();
487-
let ptr = alloc.allocate(layout)?.cast();
486+
let ptr = if T::IS_ZST {
487+
NonNull::dangling()
488+
} else {
489+
let layout = Layout::new::<mem::MaybeUninit<T>>();
490+
alloc.allocate(layout)?.cast()
491+
};
488492
unsafe { Ok(Box::from_raw_in(ptr.as_ptr(), alloc)) }
489493
}
490494

@@ -553,8 +557,12 @@ impl<T, A: Allocator> Box<T, A> {
553557
where
554558
A: Allocator,
555559
{
556-
let layout = Layout::new::<mem::MaybeUninit<T>>();
557-
let ptr = alloc.allocate_zeroed(layout)?.cast();
560+
let ptr = if T::IS_ZST {
561+
NonNull::dangling()
562+
} else {
563+
let layout = Layout::new::<mem::MaybeUninit<T>>();
564+
alloc.allocate_zeroed(layout)?.cast()
565+
};
558566
unsafe { Ok(Box::from_raw_in(ptr.as_ptr(), alloc)) }
559567
}
560568

@@ -679,14 +687,16 @@ impl<T> Box<[T]> {
679687
#[unstable(feature = "allocator_api", issue = "32838")]
680688
#[inline]
681689
pub fn try_new_uninit_slice(len: usize) -> Result<Box<[mem::MaybeUninit<T>]>, AllocError> {
682-
unsafe {
690+
let ptr = if T::IS_ZST || len == 0 {
691+
NonNull::dangling()
692+
} else {
683693
let layout = match Layout::array::<mem::MaybeUninit<T>>(len) {
684694
Ok(l) => l,
685695
Err(_) => return Err(AllocError),
686696
};
687-
let ptr = Global.allocate(layout)?;
688-
Ok(RawVec::from_raw_parts_in(ptr.as_mut_ptr() as *mut _, len, Global).into_box(len))
689-
}
697+
Global.allocate(layout)?.cast()
698+
};
699+
unsafe { Ok(RawVec::from_raw_parts_in(ptr.as_ptr(), len, Global).into_box(len)) }
690700
}
691701

692702
/// Constructs a new boxed slice with uninitialized contents, with the memory
@@ -711,14 +721,16 @@ impl<T> Box<[T]> {
711721
#[unstable(feature = "allocator_api", issue = "32838")]
712722
#[inline]
713723
pub fn try_new_zeroed_slice(len: usize) -> Result<Box<[mem::MaybeUninit<T>]>, AllocError> {
714-
unsafe {
724+
let ptr = if T::IS_ZST || len == 0 {
725+
NonNull::dangling()
726+
} else {
715727
let layout = match Layout::array::<mem::MaybeUninit<T>>(len) {
716728
Ok(l) => l,
717729
Err(_) => return Err(AllocError),
718730
};
719-
let ptr = Global.allocate_zeroed(layout)?;
720-
Ok(RawVec::from_raw_parts_in(ptr.as_mut_ptr() as *mut _, len, Global).into_box(len))
721-
}
731+
Global.allocate_zeroed(layout)?.cast()
732+
};
733+
unsafe { Ok(RawVec::from_raw_parts_in(ptr.as_ptr(), len, Global).into_box(len)) }
722734
}
723735
}
724736

@@ -1215,8 +1227,18 @@ impl<T: ?Sized, A: Allocator> Box<T, A> {
12151227

12161228
#[stable(feature = "rust1", since = "1.0.0")]
12171229
unsafe impl<#[may_dangle] T: ?Sized, A: Allocator> Drop for Box<T, A> {
1230+
#[inline]
12181231
fn drop(&mut self) {
1219-
// FIXME: Do nothing, drop is currently performed by compiler.
1232+
// the T in the Box is dropped by the compiler before the destructor is run
1233+
1234+
let ptr = self.0;
1235+
1236+
unsafe {
1237+
let layout = Layout::for_value_raw(ptr.as_ptr());
1238+
if layout.size() != 0 {
1239+
self.1.deallocate(From::from(ptr.cast()), layout);
1240+
}
1241+
}
12201242
}
12211243
}
12221244

@@ -2165,7 +2187,7 @@ impl dyn Error + Send {
21652187
let err: Box<dyn Error> = self;
21662188
<dyn Error>::downcast(err).map_err(|s| unsafe {
21672189
// Reapply the `Send` marker.
2168-
mem::transmute::<Box<dyn Error>, Box<dyn Error + Send>>(s)
2190+
Box::from_raw(Box::into_raw(s) as *mut (dyn Error + Send))
21692191
})
21702192
}
21712193
}
@@ -2179,7 +2201,7 @@ impl dyn Error + Send + Sync {
21792201
let err: Box<dyn Error> = self;
21802202
<dyn Error>::downcast(err).map_err(|s| unsafe {
21812203
// Reapply the `Send + Sync` marker.
2182-
mem::transmute::<Box<dyn Error>, Box<dyn Error + Send + Sync>>(s)
2204+
Box::from_raw(Box::into_raw(s) as *mut (dyn Error + Send + Sync))
21832205
})
21842206
}
21852207
}

rust/alloc/lib.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,11 @@
5858
//! [`Rc`]: rc
5959
//! [`RefCell`]: core::cell
6060
61+
// To run alloc tests without x.py without ending up with two copies of alloc, Miri needs to be
62+
// able to "empty" this crate. See <https://github.com/rust-lang/miri-test-libstd/issues/4>.
63+
// rustc itself never sets the feature, so this line has no effect there.
64+
#![cfg(any(not(feature = "miri-test-libstd"), test, doctest))]
65+
//
6166
#![allow(unused_attributes)]
6267
#![stable(feature = "alloc", since = "1.36.0")]
6368
#![doc(
@@ -77,11 +82,6 @@
7782
))]
7883
#![no_std]
7984
#![needs_allocator]
80-
// To run alloc tests without x.py without ending up with two copies of alloc, Miri needs to be
81-
// able to "empty" this crate. See <https://github.com/rust-lang/miri-test-libstd/issues/4>.
82-
// rustc itself never sets the feature, so this line has no affect there.
83-
#![cfg(any(not(feature = "miri-test-libstd"), test, doctest))]
84-
//
8585
// Lints:
8686
#![deny(unsafe_op_in_unsafe_fn)]
8787
#![deny(fuzzy_provenance_casts)]
@@ -90,6 +90,8 @@
9090
#![warn(missing_docs)]
9191
#![allow(explicit_outlives_requirements)]
9292
#![warn(multiple_supertrait_upcastable)]
93+
#![cfg_attr(not(bootstrap), allow(internal_features))]
94+
#![cfg_attr(not(bootstrap), allow(rustdoc::redundant_explicit_links))]
9395
//
9496
// Library features:
9597
// tidy-alphabetical-start
@@ -139,7 +141,6 @@
139141
#![feature(maybe_uninit_uninit_array_transpose)]
140142
#![feature(pattern)]
141143
#![feature(pointer_byte_offsets)]
142-
#![feature(provide_any)]
143144
#![feature(ptr_internals)]
144145
#![feature(ptr_metadata)]
145146
#![feature(ptr_sub_ptr)]

rust/alloc/raw_vec.rs

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -471,16 +471,26 @@ impl<T, A: Allocator> RawVec<T, A> {
471471
let (ptr, layout) = if let Some(mem) = self.current_memory() { mem } else { return Ok(()) };
472472
// See current_memory() why this assert is here
473473
let _: () = const { assert!(mem::size_of::<T>() % mem::align_of::<T>() == 0) };
474-
let ptr = unsafe {
475-
// `Layout::array` cannot overflow here because it would have
476-
// overflowed earlier when capacity was larger.
477-
let new_size = mem::size_of::<T>().unchecked_mul(cap);
478-
let new_layout = Layout::from_size_align_unchecked(new_size, layout.align());
479-
self.alloc
480-
.shrink(ptr, layout, new_layout)
481-
.map_err(|_| AllocError { layout: new_layout, non_exhaustive: () })?
482-
};
483-
self.set_ptr_and_cap(ptr, cap);
474+
475+
// If shrinking to 0, deallocate the buffer. We don't reach this point
476+
// for the T::IS_ZST case since current_memory() will have returned
477+
// None.
478+
if cap == 0 {
479+
unsafe { self.alloc.deallocate(ptr, layout) };
480+
self.ptr = Unique::dangling();
481+
self.cap = 0;
482+
} else {
483+
let ptr = unsafe {
484+
// `Layout::array` cannot overflow here because it would have
485+
// overflowed earlier when capacity was larger.
486+
let new_size = mem::size_of::<T>().unchecked_mul(cap);
487+
let new_layout = Layout::from_size_align_unchecked(new_size, layout.align());
488+
self.alloc
489+
.shrink(ptr, layout, new_layout)
490+
.map_err(|_| AllocError { layout: new_layout, non_exhaustive: () })?
491+
};
492+
self.set_ptr_and_cap(ptr, cap);
493+
}
484494
Ok(())
485495
}
486496
}

0 commit comments

Comments
 (0)