From nobody Sun Oct 5 00:07:11 2025 Received: from mail-wr1-f73.google.com (mail-wr1-f73.google.com [209.85.221.73]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 0C4352BE03D for ; Mon, 11 Aug 2025 12:32:07 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.221.73 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1754915529; cv=none; b=Y6tyLJU+4p27CL3ERk4CDwEFbaLn5939KUcf+p22lmsHfiaTKEmqzSaIRSBa+lyibsP5AHzoxigXJtyKpRP+WSvFfEvsS5MTekq9HnKYqe86zme90ph3lDRmWrMfi3rsoKVjo5dCJABpJms05vQxC7G4syscC4CjkksFfS+RPF8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1754915529; c=relaxed/simple; bh=xrRdYUi/ackTh+GxFSroXvpaNgAFYJLwSEWSCNu0Vh4=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=MjcJds4/gdBrGaPMTP32bhj1/uOfGBwXPHDp0mVy9fwHScoiB0GBSyN+rLx4acfiQhi3Vey6wVOkhpop6Od6djA8SjCYxrZ08aAm+wuP0KX1DdQFLLncxnJJ2eM8PDo/G/3jR0p25GDGGOKFUwjtxnvj83qg8NV2K7zejn7U0wU= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--aliceryhl.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=FqtOTpmi; arc=none smtp.client-ip=209.85.221.73 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--aliceryhl.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="FqtOTpmi" Received: by mail-wr1-f73.google.com with SMTP id ffacd0b85a97d-3b7825a2ca5so2761336f8f.1 for ; Mon, 11 Aug 2025 05:32:07 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1754915526; x=1755520326; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=EeF3bGUOa7e9fi1mYtzUmovCQl97kCDgwj4UAdIAQzA=; b=FqtOTpmip6K/t/2AoFauJ8XTdnZIhwBHy++AGHJzMdNTPMd77rcstudICYRMJswx+F kvnzeOHiOfoC9cnvS6lwhLGunEBzd6o2cjrxObP54T2PXFY1txOeWwmcJO+tkyaTzLFa AVPhtNtMGk5ztPvJ2U3QVK2eq+LglUKUIh8hCv3FhGTiR5/7CdTeDCR/u0WTFhfP0kIL XHNZcMjYDQiJkkilwJyyDWYvNmD/+T4qmKyKn9B5orL7GjHhzyavGJz7JWxMjNDg2OPB oiQLb7x0fI4MiiZgAbkL/Y9cwGlkJP7IUrHOr5EgJB0rc6n6yprzg4P8IoAXvr5rb2A3 B/bQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1754915526; x=1755520326; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=EeF3bGUOa7e9fi1mYtzUmovCQl97kCDgwj4UAdIAQzA=; b=X2pIyulEOiXB8zCsTHj4juQe3Jk9Ji0EkBGU0yffIfmDyoLSY2vuS0iq0lDd2GAYGN Fo6VzDiPOnPKD2Lvz0iUMTQpcl/YOTNB3m1BBERwvMIEwiUuCUQm/0uW37TLxjn8Ivar XBQ3a7PGlqUq8IA3fQEugXwceaeez8rhGqCnYwoppyveZ+MSD0BdHj5eDpXK9i2f3VLk R/Tm2bIJkjQt0DAR+H/ilG6JFgTHQlKWPfeDp9OkzC2JcNyyivswze7qiq4Vq5PmIF/E gqp6mwGKGpLoi3Z+qTuoa+2TXVQBAHOuwq4dcBwyed/PA/xbZ6aGvV3AweQhTLHA6G2n BRkQ== X-Forwarded-Encrypted: i=1; AJvYcCUmX/ccFpuJS1RH2L8Ja/bLE7Qvr3UWWSdO8pBPyiAGp1tx91MHGxJ6lLKejGrvy9HoHzl+FMe7LNgqCdI=@vger.kernel.org X-Gm-Message-State: AOJu0Yyh5rgIODI3v8Ham3njiuew27Q3R+HR6YFkPfXAIoL4UrMo0lNA Lf/Eqh1uHW3qXY+di945wsKkYKrZCqfeOfpHH7g3i0OXX+7CWLcSMciY4c49+gJNHcztb01kw4H MBQJIB+qX17HuGah81g== X-Google-Smtp-Source: AGHT+IHuDZSpEk8s4TEttjba6L2NvFtGfFsONeAGvCghisyrKanTpyDc2apiIaxwNjc3et8Cs5h0CmJDe4Mrfv4= X-Received: from wrbeh10.prod.google.com ([2002:a05:6000:410a:b0:3b7:d75e:c4a7]) (user=aliceryhl job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6000:18a6:b0:3a4:f7dd:6fad with SMTP id ffacd0b85a97d-3b900fdd6femr9908384f8f.14.1754915526236; Mon, 11 Aug 2025 05:32:06 -0700 (PDT) Date: Mon, 11 Aug 2025 12:31:50 +0000 In-Reply-To: <20250811-align-min-allocator-v2-0-3386cc94f4fc@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250811-align-min-allocator-v2-0-3386cc94f4fc@google.com> X-Developer-Key: i=aliceryhl@google.com; a=openpgp; fpr=49F6C1FAA74960F43A5B86A1EE7A392FDE96209F X-Developer-Signature: v=1; a=openpgp-sha256; l=3229; i=aliceryhl@google.com; h=from:subject:message-id; bh=xrRdYUi/ackTh+GxFSroXvpaNgAFYJLwSEWSCNu0Vh4=; b=owEBbQKS/ZANAwAKAQRYvu5YxjlGAcsmYgBomeLDLapzSgRvvWowdCWxONAxAhx/5FT2Y5YJk gx+2bT0DGyJAjMEAAEKAB0WIQSDkqKUTWQHCvFIvbIEWL7uWMY5RgUCaJniwwAKCRAEWL7uWMY5 Rt1QD/9YADVnV44goF9lcDd9dHPZxmydoBRcrufELmZmXlCQlrzMOEHsw0sBYEc6FhjWrVW46vm Kb8ps8JkxIQFAla48r0G19xim2rb3zCBUhfFqpE31VZRkl8dDghHjNMP/hMLSWb+8E7AMH9ZlU2 jHcDfkxJFqvQW9qHe+vAJUKx2POgZktnewhvHWUMwn4HFKg+etyqSloAV0CZ/lzm9GPTVCt7Bpv SsjsqBfcVoTjxXEiG/LjL20spSbiv2Uf2LcSm/zKBQMC/Y6goWtowLiBV64uRztcYQaNqdsLsQD L6SKhK53K18XdvSltKZaXVvyR/ckouWMtnIVneZblav0IpnoG0EibcFhzslXngg5izAVmqMaHLG VEBFWqRt3eCT6HXp7Y1KKhW2nj0pvEhwvgtG0fSM0UT/279tGzCSkc4Ua4VyX9Ule0Dp+lof8wV TD1LA0+PQWUvYMTArKetljTUSiJmoXCuUiJtlQ9U+uAWRi9qiU9sjbFDrRQ7epQw0QS45DmkkMy AvKsMjuMyoQKwTf3LYsXLUlzx1NqgxMOld0YahE+Zl8wmjy2mK5i7ERAIBo9mNecm/sF55tBidr rZEeeG+fR23ufyG4H2hvz6VhTXvpklJDtZ/cWudrSt4ho1ok4OAVPTqWq9mCEJEE4Q8I0+AxIsR WYeTU9cOEOY1jfQ== X-Mailer: b4 0.14.2 Message-ID: <20250811-align-min-allocator-v2-1-3386cc94f4fc@google.com> Subject: [PATCH v2 1/2] rust: alloc: specify the minimum alignment of each allocator From: Alice Ryhl To: Lorenzo Stoakes , "Liam R. Howlett" , Andrew Morton , Danilo Krummrich , Matthew Wilcox , Tamir Duberstein , Andreas Hindborg , Miguel Ojeda Cc: Boqun Feng , Gary Guo , "=?utf-8?q?Bj=C3=B6rn_Roy_Baron?=" , Benno Lossin , Trevor Gross , linux-mm@kvack.org, rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, Alice Ryhl Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable The kernel's allocators sometimes provide a higher alignment than the end-user requested, so add a new constant on the Allocator trait to let the allocator specify what its minimum guaranteed alignment is. This allows the ForeignOwnable trait to provide a more accurate value of FOREIGN_ALIGN when using a pointer type such as Box, which will be useful with certain collections such as XArray that store its own data in the low bits of pointers. Reviewed-by: Benno Lossin Signed-off-by: Alice Ryhl Acked-by: Liam R. Howlett Reviewed-by: Andreas Hindborg --- rust/kernel/alloc.rs | 8 ++++++++ rust/kernel/alloc/allocator.rs | 8 ++++++++ 2 files changed, 16 insertions(+) diff --git a/rust/kernel/alloc.rs b/rust/kernel/alloc.rs index a2c49e5494d334bfde67328464dafcdb31052947..907301334d8c111eb26719ad897= 00a289e03f037 100644 --- a/rust/kernel/alloc.rs +++ b/rust/kernel/alloc.rs @@ -137,6 +137,14 @@ pub mod flags { /// - Implementers must ensure that all trait functions abide by the guara= ntees documented in the /// `# Guarantees` sections. pub unsafe trait Allocator { + /// The minimum alignment satisfied by all allocations from this alloc= ator. + /// + /// # Guarantees + /// + /// Any pointer allocated by this allocator is guaranteed to be aligne= d to `MIN_ALIGN` even if + /// the requested layout has a smaller alignment. + const MIN_ALIGN: usize; + /// Allocate memory based on `layout` and `flags`. /// /// On success, returns a buffer represented as `NonNull<[u8]>` that s= atisfies the layout diff --git a/rust/kernel/alloc/allocator.rs b/rust/kernel/alloc/allocator.rs index aa2dfa9dca4c309e5a9eafc7da6a8a9bd7b54b11..25fc9f9ae3b4e471a08d77130b3= 74bd1397f7384 100644 --- a/rust/kernel/alloc/allocator.rs +++ b/rust/kernel/alloc/allocator.rs @@ -17,6 +17,8 @@ use crate::bindings; use crate::pr_warn; =20 +const ARCH_KMALLOC_MINALIGN: usize =3D bindings::ARCH_KMALLOC_MINALIGN as = usize; + /// The contiguous kernel allocator. /// /// `Kmalloc` is typically used for physically contiguous allocations up t= o page size, but also @@ -128,6 +130,8 @@ unsafe fn call( // - passing a pointer to a valid memory allocation is OK, // - `realloc` satisfies the guarantees, since `ReallocFunc::call` has the= same. unsafe impl Allocator for Kmalloc { + const MIN_ALIGN: usize =3D ARCH_KMALLOC_MINALIGN; + #[inline] unsafe fn realloc( ptr: Option>, @@ -145,6 +149,8 @@ unsafe fn realloc( // - passing a pointer to a valid memory allocation is OK, // - `realloc` satisfies the guarantees, since `ReallocFunc::call` has the= same. unsafe impl Allocator for Vmalloc { + const MIN_ALIGN: usize =3D kernel::page::PAGE_SIZE; + #[inline] unsafe fn realloc( ptr: Option>, @@ -169,6 +175,8 @@ unsafe fn realloc( // - passing a pointer to a valid memory allocation is OK, // - `realloc` satisfies the guarantees, since `ReallocFunc::call` has the= same. unsafe impl Allocator for KVmalloc { + const MIN_ALIGN: usize =3D ARCH_KMALLOC_MINALIGN; + #[inline] unsafe fn realloc( ptr: Option>, --=20 2.50.1.703.g449372360f-goog From nobody Sun Oct 5 00:07:11 2025 Received: from mail-wr1-f74.google.com (mail-wr1-f74.google.com [209.85.221.74]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 79E432D3A9D for ; Mon, 11 Aug 2025 12:32:09 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.221.74 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1754915531; cv=none; b=WIbMGpCkT0Gu78T7JivWiNmzIhr0PkvsHiv2rBsYu0RD9kU/9c+uWwUmIPGw8vgR9z/8kzLILIK5xsih45tPDXhkbjrUam1MVVDf01A88+DCksPJzscQAHOG4BX2ylpxc9KWEKZcsmg6hOAaN1z2yGifXQs/57OWKm/JpmvpYoc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1754915531; c=relaxed/simple; bh=3TOHdsav1ycB+aZ84BPSmdU5PVabBdrJYr0uju0OSxE=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=thq6aegdsit942Krq77AQwL5TCrRrpyj9VpdSYWbxJpG2vHNUz1GOS0rTYSR7s6Ot1gPUDaDBv6GRvSllZ/3RPhxgCztTHeE7ZSsFe7prRitD6Z9aXyWP+3V5LNNyqRy7vDOJkJz46EAIRPtRazHYSStnUl5DHREfgKiSXDa5WU= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--aliceryhl.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=t+mYYA3m; arc=none smtp.client-ip=209.85.221.74 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--aliceryhl.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="t+mYYA3m" Received: by mail-wr1-f74.google.com with SMTP id ffacd0b85a97d-3b79629bd88so1854072f8f.0 for ; Mon, 11 Aug 2025 05:32:09 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1754915528; x=1755520328; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=9rlOmIicLtEp3jt4sv4CSTuwkEdgZbSMhZPZuiG0qNY=; b=t+mYYA3mMQvkekV2PRoPz1Toy0BH1Egyl91FoUPmdK/yH8GVPmfMXJJX4JRMMqs30C V5C78yDx/8GOfgpT0gQgU1IJYoxK1LZrm4qjQllChU4WZkWk3NnlvhPL0aR6kJSSAnoM 5HivFRLTn5STS5rad7RSjCFlMd7pNL3zerx+7NrfZieY26dHYUrj1diDUgUhLJexsFYA IjZbHTntlP/mxhiBNRokh7oT3BuGZoqdSUxzRTzpZgN+LY29J7nROVV558Q1pO79jHPG pYu0zp61+aUb+hi+9t4j4TEonNKf2rT1ApLFydPB8zCdRcuE5EzRoUKwsmaPSXLRvoAO vJSw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1754915528; x=1755520328; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=9rlOmIicLtEp3jt4sv4CSTuwkEdgZbSMhZPZuiG0qNY=; b=SiIwLiCv9T8WtEBqADeHry21ZZsGhC4HgGdt9120pAM34nqaOXLPhy2KPn0GPZCING ujDMUcT6sUZs5oiusvAfZQh0vaS3h6FdRk0Cnogmm5GRT43DFxHbHjmK8nzpjvD2k2vp urnxiylp9UTudXTRlwiV+4gycXq9HGpWn36aVh5fv049YYrNAx7NKeA3MwHygSkApKKL QbK65xxGr2gxLPw3ustoKVS8Cz0qKB15sE6TbLJtEuHBFUDBvPjJugmju9277CaGv+8W R5F03PZNn8L9QhH/AL0jzHe/PSDKTWKc8bfCTk6dCp9w3JJ6y2Kh3xzAAG1FVxflp6gI MsaQ== X-Forwarded-Encrypted: i=1; AJvYcCWS3Sc2A2ilVQ9vy7WGvboYyUNisSWwEBDCiU7f3W+6+xWzniCSCPjVFJMHdz7u8J+OnVvhABksRWfKfgo=@vger.kernel.org X-Gm-Message-State: AOJu0Yy+EQ0mPgYsHI5R000UgDfcPD9QuLESOxmLIB90gZwIT1+K9sGF eWLQg/kSK4PEN1xKj9GC6hiaSCflZ6cGM7hlMj6MnF0iLMpE7BZVAPtiMqpxORT30T70WimvduN 8JgeXPnfFFfgpGsUKsQ== X-Google-Smtp-Source: AGHT+IGLqvqODrNKUqFGV1RPHxhNKh2ajU+3kusbjIAlTVO5vOTLv+p7fkC+SL8LO9kaXdUyiw31plWpnKodurU= X-Received: from wmth4.prod.google.com ([2002:a05:600c:8b64:b0:459:dcf5:1b54]) (user=aliceryhl job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6000:25c4:b0:3b7:76ac:8b72 with SMTP id ffacd0b85a97d-3b9009294e5mr9096299f8f.12.1754915527779; Mon, 11 Aug 2025 05:32:07 -0700 (PDT) Date: Mon, 11 Aug 2025 12:31:51 +0000 In-Reply-To: <20250811-align-min-allocator-v2-0-3386cc94f4fc@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250811-align-min-allocator-v2-0-3386cc94f4fc@google.com> X-Developer-Key: i=aliceryhl@google.com; a=openpgp; fpr=49F6C1FAA74960F43A5B86A1EE7A392FDE96209F X-Developer-Signature: v=1; a=openpgp-sha256; l=3044; i=aliceryhl@google.com; h=from:subject:message-id; bh=3TOHdsav1ycB+aZ84BPSmdU5PVabBdrJYr0uju0OSxE=; b=owEBbQKS/ZANAwAKAQRYvu5YxjlGAcsmYgBomeLD7qI0nZRsYh9EJ8g3j8Abdi0u+B75wO6Tr Lti2BBLUReJAjMEAAEKAB0WIQSDkqKUTWQHCvFIvbIEWL7uWMY5RgUCaJniwwAKCRAEWL7uWMY5 RvAWD/4qmVr48iymYXFuDkGmxltJS6ILykjTpfpC8zNFFY76rrYxiOtJRaNzi/FbdFsdgGHH8pn U0Dtcj+pEVp/ac9Jej3eTY8RdHtWiHTTza/GeRYt0vKrl3j4qp2GHh4oN9wOLqbpdNL5QbAaxjg tXiwjycVXRR5inzf7VtQ+6ERQHeN4ja6xDxdRNOze2ltFpQ2pELfdJbXhlQEGqyQOhVTZpqjfsg EUJi3VLNXZnyJGCj3florbFn848EvfG/QkErQdXkFee+qxsVygZ0ev+z0G9p5y4crdFdM9Qt0E9 gWUpG2sWvqV1qLgdXfwMwhDfJxEHNJz5Vm6nG92uwaWkn5EsjmUsZs7MgawgTWTtZmrxZc0R1ik NDgBhTaWzKXM0XCga0yHRhiOMZqH9XB9IGSnUKTsXedMRhP8As3rnf6FF1TVb9j6Ru3eb2/AljH 4q7FWAuTi3flms0P0eG5AYEY8rvlRz7Wj+QA0Owu32mVmiJ6bCEeX0H4swgrsY1aJu9dbck5izo 1S+kHlcnmzXt65jisEuoc6rH6ZeI3RI4jzfeSLEc2xBl8m7MbEXOuJsiuHURnTn6+g8oFV5VlRo Rizotk0lWMfBDg2orMlo48dCPAIUQrYPoRD2JAejExoHgqLXffV5tz1anAo6KMnTE5rpam+aV8z JDLA7pMlhh3Ymog== X-Mailer: b4 0.14.2 Message-ID: <20250811-align-min-allocator-v2-2-3386cc94f4fc@google.com> Subject: [PATCH v2 2/2] rust: alloc: take the allocator into account for FOREIGN_ALIGN From: Alice Ryhl To: Lorenzo Stoakes , "Liam R. Howlett" , Andrew Morton , Danilo Krummrich , Matthew Wilcox , Tamir Duberstein , Andreas Hindborg , Miguel Ojeda Cc: Boqun Feng , Gary Guo , "=?utf-8?q?Bj=C3=B6rn_Roy_Baron?=" , Benno Lossin , Trevor Gross , linux-mm@kvack.org, rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, Alice Ryhl Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable When converting a Box into a void pointer, the allocator might guarantee a higher alignment than the type itself does, and in that case it is guaranteed that the void pointer has that higher alignment. This is quite useful when combined with the XArray, which you can only create using a ForeignOwnable whose FOREIGN_ALIGN is at least 4. This means that you can now always use a Box with the XArray no matter the alignment of T. Reviewed-by: Benno Lossin Signed-off-by: Alice Ryhl Acked-by: Liam R. Howlett Reviewed-by: Andreas Hindborg --- rust/kernel/alloc/kbox.rs | 13 +++++++++---- rust/kernel/sync/arc.rs | 6 +++--- 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/rust/kernel/alloc/kbox.rs b/rust/kernel/alloc/kbox.rs index 856d05aa60f13485d8afc98f0b7fe7593867b5a1..eedab0be1eff78a815052434634= 8ec0759f852e2 100644 --- a/rust/kernel/alloc/kbox.rs +++ b/rust/kernel/alloc/kbox.rs @@ -401,12 +401,17 @@ fn try_init(init: impl Init, flags: Flags) -= > Result } =20 // SAFETY: The pointer returned by `into_foreign` comes from a well aligned -// pointer to `T`. +// pointer to `T` allocated by `A`. unsafe impl ForeignOwnable for Box where A: Allocator, { - const FOREIGN_ALIGN: usize =3D core::mem::align_of::(); + const FOREIGN_ALIGN: usize =3D if core::mem::align_of::() < A::MIN_= ALIGN { + A::MIN_ALIGN + } else { + core::mem::align_of::() + }; + type Borrowed<'a> =3D &'a T; type BorrowedMut<'a> =3D &'a mut T; =20 @@ -435,12 +440,12 @@ unsafe fn borrow_mut<'a>(ptr: *mut c_void) -> &'a mut= T { } =20 // SAFETY: The pointer returned by `into_foreign` comes from a well aligned -// pointer to `T`. +// pointer to `T` allocated by `A`. unsafe impl ForeignOwnable for Pin> where A: Allocator, { - const FOREIGN_ALIGN: usize =3D core::mem::align_of::(); + const FOREIGN_ALIGN: usize =3D as ForeignOwnable>::FOREIGN_= ALIGN; type Borrowed<'a> =3D Pin<&'a T>; type BorrowedMut<'a> =3D Pin<&'a mut T>; =20 diff --git a/rust/kernel/sync/arc.rs b/rust/kernel/sync/arc.rs index 63a66761d0c7d752e09ce7372bc230661b2f7c6d..74121cf935f364c16799b5c31cc= 88714dfd6b702 100644 --- a/rust/kernel/sync/arc.rs +++ b/rust/kernel/sync/arc.rs @@ -373,10 +373,10 @@ pub fn into_unique_or_drop(self) -> Option>> { } } =20 -// SAFETY: The pointer returned by `into_foreign` comes from a well aligned -// pointer to `ArcInner`. +// SAFETY: The pointer returned by `into_foreign` was originally allocated= as an +// `KBox>`, so that type is what determines the alignment. unsafe impl ForeignOwnable for Arc { - const FOREIGN_ALIGN: usize =3D core::mem::align_of::>(); + const FOREIGN_ALIGN: usize =3D > as ForeignOwnable>::= FOREIGN_ALIGN; =20 type Borrowed<'a> =3D ArcBorrow<'a, T>; type BorrowedMut<'a> =3D Self::Borrowed<'a>; --=20 2.50.1.703.g449372360f-goog