Received: by 2002:a05:7208:9594:b0:7e:5202:c8b4 with SMTP id gs20csp1198030rbb; Mon, 26 Feb 2024 01:40:24 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCUDImri0LlA8iml9c7O5wagmHuiTSLVoZVkcSsGjUxD4Sgq/kAsRKrKZNAXqlEYwLIKbywD3fJcRD406t/M3f4OAFD/88RlE45n2hdhfQ== X-Google-Smtp-Source: AGHT+IGVP78ummXGZyc9iJr31b0Xhg9MibBqXPwRqB+qced+MzE8CPleNzIYxB4dmkdjHZzGUrLg X-Received: by 2002:a17:906:64b:b0:a3f:bcff:18b8 with SMTP id t11-20020a170906064b00b00a3fbcff18b8mr4258079ejb.27.1708940424551; Mon, 26 Feb 2024 01:40:24 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1708940424; cv=pass; d=google.com; s=arc-20160816; b=YFeS3ybaO8t1TOjmc5zLhB1i5Rwd9vTx+71kFvBGL2jCjECqDvLCI6RikxszFdGkp1 N9hwOG5kBMsV64G9NJ1Bw9Jjpts0oXrXnSC5FaqW9GsOPc0YHKwfHy3yosRKR/rd3dJF VPueKofKHBs2V8qtrq1TfuqncrXxg/J52vq2cwHFovkUxv++daCaBpyG+IThsWE0qzZD c1GXKrbupzg4blNwwQbPQeYeGa+hWR+kYUabR+N0BBEkR5J9erVH6OcoW85E4YEc/Cg9 gPy7zytY5VyOa/bMSXq9PLo7pV21Q5F/1ESeLw4cynYdUflOHkHmJ9v6Ibvb2wJ1Tpvf Pt4w== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:message-id:in-reply-to:date :subject:cc:to:from:user-agent:references:dkim-signature; bh=DXJKtDALC3AllytrckFvRAF/ZRCMob4eWSKm4Ha7cls=; fh=1CSn3EsndnbVG5ejW8uX5W26fnrdmaSxMHUyhb4TWYY=; b=yX/+L08Cwh1O14t+z7F4kCMKBkx62BOMOA9en4gsbqLz3YNtjg52ceyjSfiCSzX/HQ 51gWrga2QgQnVgq2xQLv5SGKjzbZ0frkvXtxIYiWCMAbViBqE6Ax4VEZKpDwJo0iuhRC b73MbEp7M8IVuEAwT6PB6hxObyhx7Cmhn30ozhaUgPk7Qy9Ppw0TW7yH8+iwXlWpUfn1 q+ZkkA5xLAuCQ4niQksFENjsVawqJOuGWIBfWH4BJoILgFZcqdD4/R6JrvtG/+/pZZ5Q 9CadbkpzjqGVayvo7LJcPpRdlNWQq1XFirVvJcO4eD7At7VSHMT4hnJqCJe21HzmzA+Y FiDg==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@metaspace-dk.20230601.gappssmtp.com header.s=20230601 header.b=ks0FbFhi; arc=pass (i=1 dkim=pass dkdomain=metaspace-dk.20230601.gappssmtp.com); spf=pass (google.com: domain of linux-kernel+bounces-80999-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-80999-linux.lists.archive=gmail.com@vger.kernel.org" Return-Path: Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [2604:1380:4601:e00::3]) by mx.google.com with ESMTPS id g15-20020a170906198f00b00a3f77bbee26si1996639ejd.139.2024.02.26.01.40.24 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 26 Feb 2024 01:40:24 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-80999-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) client-ip=2604:1380:4601:e00::3; Authentication-Results: mx.google.com; dkim=pass header.i=@metaspace-dk.20230601.gappssmtp.com header.s=20230601 header.b=ks0FbFhi; arc=pass (i=1 dkim=pass dkdomain=metaspace-dk.20230601.gappssmtp.com); spf=pass (google.com: domain of linux-kernel+bounces-80999-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-80999-linux.lists.archive=gmail.com@vger.kernel.org" Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by am.mirrors.kernel.org (Postfix) with ESMTPS id 25FB21F26612 for ; Mon, 26 Feb 2024 09:40:24 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 4759378660; Mon, 26 Feb 2024 09:02:49 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=metaspace-dk.20230601.gappssmtp.com header.i=@metaspace-dk.20230601.gappssmtp.com header.b="ks0FbFhi" Received: from mail-ej1-f51.google.com (mail-ej1-f51.google.com [209.85.218.51]) (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 438A418AF8 for ; Mon, 26 Feb 2024 09:02:44 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.218.51 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708938168; cv=none; b=TLcWT6nGkmzrMXTJXY3OEC18mRQy1sbG00tYdsZ1QTMNNAdfDd1P9drZXXozNILian8SdGckZtQWdBbI/31WEkrDtIFyvIsVg1X/hibKQOITW8Jf9GKNTXV2JsqDOp8QqQ4ej+oYL22yVJ+O1zYephkoNi6u8ndY9dxuvMHBocY= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708938168; c=relaxed/simple; bh=XbXVfHeOwDhV+PDLw6Hx0gES/Ce6Uc4JONajMf23QXo=; h=References:From:To:Cc:Subject:Date:In-reply-to:Message-ID: MIME-Version:Content-Type; b=FDgTCXD4LcFht9+NPKWW53z5Gc3MNY8kcaK4xMyDwHOn6QFNvA/d/R+40/VeImWO1QC3rM+FfiV+UQo1xvtm6a0dzJT9EObn6OZZ8i7gG23/7XUbwH45jQFRTFTDdiVNQHu1+pqI/B1m4f2Rx2urp/VygpsXlvhHPAnamjB2j1Q= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=metaspace.dk; spf=none smtp.mailfrom=metaspace.dk; dkim=pass (2048-bit key) header.d=metaspace-dk.20230601.gappssmtp.com header.i=@metaspace-dk.20230601.gappssmtp.com header.b=ks0FbFhi; arc=none smtp.client-ip=209.85.218.51 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=metaspace.dk Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=metaspace.dk Received: by mail-ej1-f51.google.com with SMTP id a640c23a62f3a-a3e891b5e4eso314138766b.0 for ; Mon, 26 Feb 2024 01:02:44 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=metaspace-dk.20230601.gappssmtp.com; s=20230601; t=1708938163; x=1709542963; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:message-id:in-reply-to:date :subject:cc:to:from:user-agent:references:from:to:cc:subject:date :message-id:reply-to; bh=DXJKtDALC3AllytrckFvRAF/ZRCMob4eWSKm4Ha7cls=; b=ks0FbFhiJIhNZP+mFC4+YnxvuB1LLZ5tmUnlPi2j6nJGNwYBfwgbP+JRA33kMINLYn 0OXEkPIEv6k19TVZbhMKxTq9ng5lCX9CD8mjDkEWykdPMwxi/X5yTLeGzR6/dRpFhCHs GFD8+9q2uBb4f2nK9HtWajwonLfY6kwDhvJCeIpkmC0yDdD4gDFj3MfOtvZdy+FAl5Cc s315j8pSdXCbfxLKWJCrgXjNL+DNsHsk3dTaYyO+NtKO/RIsfW56iOg6YgSYJNetjaGc x7FuwQZPIjsz2FCPL/q+V2lp56sFOLUJdhYNtp3NRPtX23C5DB7KCkTmGRwUIwgZt6G7 bbyQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1708938163; x=1709542963; h=content-transfer-encoding:mime-version:message-id:in-reply-to:date :subject:cc:to:from:user-agent:references:x-gm-message-state:from:to :cc:subject:date:message-id:reply-to; bh=DXJKtDALC3AllytrckFvRAF/ZRCMob4eWSKm4Ha7cls=; b=cvGVk1v8CRlw+UEN1g8kJIcxVB/VrP8mvpLIMwanGaWOjvVzI6laltMVtJWuAx5/c0 BO1/HtDVfgTZktMvmh9D27FATgIcTRW0cVIKjRA5kcsIY2ywnqQlOFq8kEYt4V6j5WdD kiNudxB83wD/3nKnK3s1TF7u14F6WXkRGnanYj2sLFy+WzDx5S1i5202H+bOpU9ZKnHq zanDUk1pjinp7OZNlqs+rn2ICLpsEwmHFB7aV7ljnMgo3GcCJQSZNrt16OF7SDZ3I6+i m5c+0AiC5eNZnUBAbBQznlq62KdRRcGnORPT4VnHb1/BaPqY4163jEuaQgfYpTdRQ/wE NxQQ== X-Forwarded-Encrypted: i=1; AJvYcCW2GIiMSKoJFQFvV9teX7gFcc+8BKToj+U6RYiJuoUmuiIoobZ7i/KbVhiEjdXNCxh9TlFusYs1ehr1RGT4pY3dThYzvti4TMi2EvWL X-Gm-Message-State: AOJu0Yxqsz2K9qZrMtweGX5+pPFrhEnrUcxxywAJ6bEGKy2cGn3gDFO1 ugGbkPCdJq5ERuqfMhqCSxv5IF3o5KzVtZrweclxOUKoW3PupSblGOcMJJpnAIY= X-Received: by 2002:a17:906:b7ca:b0:a3e:6038:42b8 with SMTP id fy10-20020a170906b7ca00b00a3e603842b8mr3801391ejb.39.1708938163246; Mon, 26 Feb 2024 01:02:43 -0800 (PST) Received: from localhost ([194.62.217.1]) by smtp.gmail.com with ESMTPSA id un6-20020a170907cb8600b00a4386852da5sm1198ejc.83.2024.02.26.01.02.42 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 26 Feb 2024 01:02:42 -0800 (PST) References: <20230503090708.2524310-7-nmi@metaspace.dk> <20230503120354.534136-1-aliceryhl@google.com> User-agent: mu4e 1.10.8; emacs 29.2 From: "Andreas Hindborg (Samsung)" To: Alice Ryhl Cc: Damien.LeMoal@wdc.com, alex.gaynor@gmail.com, axboe@kernel.dk, benno.lossin@proton.me, bjorn3_gh@protonmail.com, boqun.feng@gmail.com, gary@garyguo.net, gost.dev@samsung.com, hare@suse.de, hch@lst.de, kbusch@kernel.org, linux-block@vger.kernel.org, linux-kernel@vger.kernel.org, lsf-pc@lists.linux-foundation.org, ojeda@kernel.org, rust-for-linux@vger.kernel.org, wedsonaf@gmail.com, willy@infradead.org Subject: Re: [RFC PATCH 06/11] rust: apply cache line padding for `SpinLock` Date: Fri, 23 Feb 2024 12:29:40 +0100 In-reply-to: <20230503120354.534136-1-aliceryhl@google.com> Message-ID: <87y1b7a994.fsf@metaspace.dk> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Hi Alice, Alice Ryhl writes: > On Wed, 3 May 2023 11:07:03 +0200, Andreas Hindborg wrote: >> The kernel `struct spinlock` is 4 bytes on x86 when lockdep is not enabl= ed. The >> structure is not padded to fit a cache line. The effect of this for `Spi= nLock` >> is that the lock variable and the value protected by the lock will share= a cache >> line, depending on the alignment requirements of the protected value. Al= igning >> the lock variable and the protected value to a cache line yields a 20% >> performance increase for the Rust null block driver for sequential reads= to >> memory backed devices at 6 concurrent readers. >>=20 >> Signed-off-by: Andreas Hindborg > > This applies the cacheline padding to all spinlocks unconditionally. > It's not clear to me that we want to do that. Instead, I suggest using > `SpinLock>` in the null block driver to opt-in to the > cache padding there, and let other drivers choose whether or not they > want to cache pad their locks. I was going to write that this is not going to work because the compiler is going to reorder the fields of `Lock` and put the `data` field first, followed by the `state` field. But I checked the layout, and it seems that I actually get the `state` field first (with an alignment of 4), 60 bytes of padding, and then the `data` field (with alignment 64). I am wondering why the compiler is not reordering these fields? Am I guaranteed that the fields will not be reordered? Looking at the definition of `Lock` there does not seem to be anything that prevents rustc from swapping `state` and `data`. > > On Wed, 3 May 2023 11:07:03 +0200, Andreas Hindborg wrote: >> diff --git a/rust/kernel/cache_padded.rs b/rust/kernel/cache_padded.rs >> new file mode 100644 >> index 000000000000..758678e71f50 >> --- /dev/null >> +++ b/rust/kernel/cache_padded.rs >>=20 >> +impl CachePadded { >> + /// Pads and aligns a value to 64 bytes. >> + #[inline(always)] >> + pub(crate) const fn new(t: T) -> CachePadded { >> + CachePadded:: { value: t } >> + } >> +} > > Please make this `pub` instead of just `pub(crate)`. Other drivers might > want to use this directly. Alright. > > On Wed, 3 May 2023 11:07:03 +0200, Andreas Hindborg wrote: >> diff --git a/rust/kernel/sync/lock/spinlock.rs b/rust/kernel/sync/lock/s= pinlock.rs >> index 979b56464a4e..e39142a8148c 100644 >> --- a/rust/kernel/sync/lock/spinlock.rs >> +++ b/rust/kernel/sync/lock/spinlock.rs >> @@ -100,18 +103,20 @@ unsafe impl super::Backend for SpinLockBackend { >> ) { >> // SAFETY: The safety requirements ensure that `ptr` is valid f= or writes, and `name` and >> // `key` are valid for read indefinitely. >> - unsafe { bindings::__spin_lock_init(ptr, name, key) } >> + unsafe { bindings::__spin_lock_init((&mut *ptr).deref_mut(), na= me, key) } >> } >>=20=20 >> + #[inline(always)] >> unsafe fn lock(ptr: *mut Self::State) -> Self::GuardState { >> // SAFETY: The safety requirements of this function ensure that= `ptr` points to valid >> // memory, and that it has been initialised before. >> - unsafe { bindings::spin_lock(ptr) } >> + unsafe { bindings::spin_lock((&mut *ptr).deref_mut()) } >> } >>=20=20 >> + #[inline(always)] >> unsafe fn unlock(ptr: *mut Self::State, _guard_state: &Self::GuardS= tate) { >> // SAFETY: The safety requirements of this function ensure that= `ptr` is valid and that the >> // caller is the owner of the mutex. >> - unsafe { bindings::spin_unlock(ptr) } >> + unsafe { bindings::spin_unlock((&mut *ptr).deref_mut()) } >> } >> } > > I would prefer to remain in pointer-land for the above operations. I > think that this leads to core that is more obviously correct. > > For example: > > ``` > impl CachePadded { > pub const fn raw_get(ptr: *mut Self) -> *mut T { > core::ptr::addr_of_mut!((*ptr).value) > } > } > > #[inline(always)] > unsafe fn unlock(ptr: *mut Self::State, _guard_state: &Self::GuardState) { > unsafe { bindings::spin_unlock(CachePadded::raw_get(ptr)) } > } > ``` Got it =F0=9F=91=8D BR Andreas