From nobody Fri Dec 19 21:46:57 2025 Received: from mail-wm1-f74.google.com (mail-wm1-f74.google.com [209.85.128.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 6A957214216 for ; Wed, 22 Jan 2025 15:00:43 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.74 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1737558045; cv=none; b=J877hmovRunJgsDmHuXkLxVAqY0EK/9q0nzIecFb+AVHCM9OCmvOMOCogQkz3ddpfBSby9OXotRF/xC8mH299sry3q0EWSW5pNNt9yCDC3YTspU6iCYTJZEnLJ5V2N3e9/lZPvhV6UpwLvfjMMPThLFPxNAisjzF2mghrpfV/bU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1737558045; c=relaxed/simple; bh=07KvXWwT9X3FUu26PatafcUnUgdiuPCMqiW6FbaREys=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=ejJXiKAQLOitmzd3JWtujN+wHPM6WWUdVaQHHrvj8s30AM0vcMvgksnid3aoxlwQ3D0KKs7LBY+/6kYEvrU66iJTA1QkrCk2GHBqM43H5uUJ3hGLJqDT5RE6Urw8rrFEqbxHLKzvDm1skyydfmSWy2kOG84+mTpr5ELHjRkcKw8= 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=Qt+mve1b; arc=none smtp.client-ip=209.85.128.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="Qt+mve1b" Received: by mail-wm1-f74.google.com with SMTP id 5b1f17b1804b1-43651b1ba8aso51457035e9.1 for ; Wed, 22 Jan 2025 07:00:43 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1737558042; x=1738162842; 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=K2jyo6F2nIPEWze5+hxsh8zUD5pqGsM2SbdyCSmT9fA=; b=Qt+mve1bAHuOOhMxMp1HwaJaeIRaVWlJgfY0Oh7AurSGyoTw5lWvmrrKEscf+Zy1Fx w6WAyxoZCIMFtJPDKDCIbpczz3ORkOdlNY3xKc21k49lqRmOdbf8314q1GJht9c0nKSz Ku6VWI1Sf8EyLHl83obKKyZ5rOH3a2UsdcM9vdG7ybo8OjTPjXzXv1onm6O0+6qSRRlR cuYlrbf/iJKkNh+QUEgzO+Q6l4SesJVSSLo9oGxMvdNqKX+rEDzphmGqgkoqVrsSxR31 BEg7AXzNL7gv6L6DA+EeKNn9QVyjSoNDwFZjuWfDMMt+5pqnNrwN0DLpBxG8XlKDW6Qf vqzg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1737558042; x=1738162842; 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=K2jyo6F2nIPEWze5+hxsh8zUD5pqGsM2SbdyCSmT9fA=; b=XuLX/vedkkmAi7hRHkgfsEqfY9EoJ0nhTIeVt1lDxcvX1w6f0hZGbuvagERaMKMbuz 0+iOsPpq70+D9/iKSuuU3Qh/QxY7QGvO+TyKHSOTagCkWrYhAESbXketEYSn9tNXTRjy tz3gmPcMskRx/DCnDS64m9U6ZjlTJFin2cCei+geQAexLtfzzceVWu/tT36ralsuBXre GDudfau61iSMW+px74nLOZHht8R9b2vCkhGcA2d85V2exh/flbmV0b6IsvWhOoyuGMmc /DQ6CQD6WEeyy7tS/Y54otHeV+vnXXk4lQvUYMEA+lmFejk5DMb2fZDgb9/JT4iX6qq8 zZ1w== X-Forwarded-Encrypted: i=1; AJvYcCV6evmVZZKlDqmkhQBo/b0vJc6/zXqpLn6vz/ZbptGN55vk82WHQY9se+ORGHVB9dbk01iiQCqR7MY5VyM=@vger.kernel.org X-Gm-Message-State: AOJu0Yyw8hyEOcIQ76v0744pRWM68oMMNZZrYbVAQ80s70hI7nZMwsHD gfsuhAp6NfiTir5rhk00DvrF6AWLzOcdmns+x/0WsQRpmdrzcxBk4HAQzmxJ1jKH2Q9tf2sNkzS 63jnNyqj40Qt0/g== X-Google-Smtp-Source: AGHT+IEoWiIvAlur5jxITETpHeyb+Ehmgm5n8/TjIJE2NU/IWIuxFg+LEF2EiOqPe3lJLQGAZZ8AeRlDx5OgsfU= X-Received: from wmbjx7.prod.google.com ([2002:a05:600c:5787:b0:435:4bd2:1dcd]) (user=aliceryhl job=prod-delivery.src-stubby-dispatcher) by 2002:a05:600c:1d1d:b0:434:a815:2b5d with SMTP id 5b1f17b1804b1-4389141c211mr177008575e9.24.1737558041903; Wed, 22 Jan 2025 07:00:41 -0800 (PST) Date: Wed, 22 Jan 2025 15:00:17 +0000 In-Reply-To: <20250122-cursor-between-v3-0-aaafbd8af14d@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250122-cursor-between-v3-0-aaafbd8af14d@google.com> X-Developer-Key: i=aliceryhl@google.com; a=openpgp; fpr=49F6C1FAA74960F43A5B86A1EE7A392FDE96209F X-Developer-Signature: v=1; a=openpgp-sha256; l=5203; i=aliceryhl@google.com; h=from:subject:message-id; bh=07KvXWwT9X3FUu26PatafcUnUgdiuPCMqiW6FbaREys=; b=owEBbQKS/ZANAwAKAQRYvu5YxjlGAcsmYgBnkQgUK6HVjT1u9zVRNmfzxMwNEZjwekmFbdMeS 81uEKFZZqWJAjMEAAEKAB0WIQSDkqKUTWQHCvFIvbIEWL7uWMY5RgUCZ5EIFAAKCRAEWL7uWMY5 RgjjD/0RuCjox/EbkI2wszt67IkPXUKJZ2PNDPNzfx7H0DPxjhkyGQJ1JK2QP8J+KqPDQ/iygvg BUkomVcUP1Dm+G8ZCpZnNbaiLw9jsd3FiaGhd3c+IvNSNEhi68JufIAAbcd7F6w7hFo6ZQBYonJ bDAWIKnoA8u4umKJZJ7MqcjqIxTMWIkxaSF3B2i5tbibzUUvmusPs9gFhDL7MBHyEUgfD3PrUkr Uox5wc86exgdeac4V44bsSAXTkwgtJvO0AGhecglkTOLeFQdXgI8AWo8SlRsJDVAbuMeqFIBvW1 MoOZ2wYvJmwyI0LJLhE2iWkazFZ1wIAfImKO12JHM4AcpUDiVl55GAb4qG6tTuS2x6iwQ71S8jE hPt2GHsIQs+wL7T19g/yC3aB5cIFpZrLTv8rtlKSW/bdy8gjTZrPRnM53ZYqNJDGbuwYra+o3ML jSPPtbdHXwvu2fXoBS5lYbcUs62veCqCJeb88b8JCx06dwp9pyxbSQRw4JKk5XK4Se3VNt6wPY5 7kKNotXyKzftjqTOAB74XDO654/Gbebhr4RBmrvvVD5/6gnu3zOA+tlZs6HoIRkrAY+mvl4S8Vh 26t95FNgIAVoutRludhizKsva3qrdRuTNTCBQmGZbapJddCxCxLk05NQemKCgfX9z6ODzEYyJ52 orkhBsR1M42hUsQ== X-Mailer: b4 0.13.0 Message-ID: <20250122-cursor-between-v3-1-aaafbd8af14d@google.com> Subject: [PATCH v3 1/2] rust: list: extract common code for insertion From: Alice Ryhl To: Miguel Ojeda Cc: Alex Gaynor , Boqun Feng , Gary Guo , "=?utf-8?q?Bj=C3=B6rn_Roy_Baron?=" , Benno Lossin , Andreas Hindborg , Trevor Gross , 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 To prepare for a new cursor API that has the ability to insert elements into the list, extract the common code needed for this operation into a new `insert_inner` method. Both `push_back` and `push_front` are updated to use the new function. Reviewed-by: Andreas Hindborg Signed-off-by: Alice Ryhl Reviewed-by: Boqun Feng --- rust/kernel/list.rs | 70 ++++++++++++++++++++++++-------------------------= ---- 1 file changed, 32 insertions(+), 38 deletions(-) diff --git a/rust/kernel/list.rs b/rust/kernel/list.rs index fb93330f4af4..97b3599b7207 100644 --- a/rust/kernel/list.rs +++ b/rust/kernel/list.rs @@ -245,8 +245,20 @@ pub fn is_empty(&self) -> bool { self.first.is_null() } =20 - /// Add the provided item to the back of the list. - pub fn push_back(&mut self, item: ListArc) { + /// Inserts `item` before `next` in the cycle. + /// + /// Returns a pointer to the newly inserted element. Never changes `se= lf.first` unless the list + /// is empty. + /// + /// # Safety + /// + /// * `next` must be an element in this list or null. + /// * if `next` is null, then the list must be empty. + unsafe fn insert_inner( + &mut self, + item: ListArc, + next: *mut ListLinksFields, + ) -> *mut ListLinksFields { let raw_item =3D ListArc::into_raw(item); // SAFETY: // * We just got `raw_item` from a `ListArc`, so it's in an `Arc`. @@ -259,16 +271,16 @@ pub fn push_back(&mut self, item: ListArc) { // SAFETY: We have not yet called `post_remove`, so `list_links` i= s still valid. let item =3D unsafe { ListLinks::fields(list_links) }; =20 - if self.first.is_null() { - self.first =3D item; + // Check if the list is empty. + if next.is_null() { // SAFETY: The caller just gave us ownership of these fields. // INVARIANT: A linked list with one item should be cyclic. unsafe { (*item).next =3D item; (*item).prev =3D item; } + self.first =3D item; } else { - let next =3D self.first; // SAFETY: By the type invariant, this pointer is valid or nul= l. We just checked that // it's not null, so it must be valid. let prev =3D unsafe { (*next).prev }; @@ -282,45 +294,27 @@ pub fn push_back(&mut self, item: ListArc) { (*next).prev =3D item; } } + + item + } + + /// Add the provided item to the back of the list. + pub fn push_back(&mut self, item: ListArc) { + // SAFETY: + // * `self.first` is null or in the list. + // * `self.first` is only null if the list is empty. + unsafe { self.insert_inner(item, self.first) }; } =20 /// Add the provided item to the front of the list. pub fn push_front(&mut self, item: ListArc) { - let raw_item =3D ListArc::into_raw(item); // SAFETY: - // * We just got `raw_item` from a `ListArc`, so it's in an `Arc`. - // * If this requirement is violated, then the previous caller of = `prepare_to_insert` - // violated the safety requirement that they can't give up owner= ship of the `ListArc` - // until they call `post_remove`. - // * We own the `ListArc`. - // * Removing items] from this list is always done using `remove_i= nternal_inner`, which - // calls `post_remove` before giving up ownership. - let list_links =3D unsafe { T::prepare_to_insert(raw_item) }; - // SAFETY: We have not yet called `post_remove`, so `list_links` i= s still valid. - let item =3D unsafe { ListLinks::fields(list_links) }; + // * `self.first` is null or in the list. + // * `self.first` is only null if the list is empty. + let new_elem =3D unsafe { self.insert_inner(item, self.first) }; =20 - if self.first.is_null() { - // SAFETY: The caller just gave us ownership of these fields. - // INVARIANT: A linked list with one item should be cyclic. - unsafe { - (*item).next =3D item; - (*item).prev =3D item; - } - } else { - let next =3D self.first; - // SAFETY: We just checked that `next` is non-null. - let prev =3D unsafe { (*next).prev }; - // SAFETY: Pointers in a linked list are never dangling, and t= he caller just gave us - // ownership of the fields on `item`. - // INVARIANT: This correctly inserts `item` between `prev` and= `next`. - unsafe { - (*item).next =3D next; - (*item).prev =3D prev; - (*prev).next =3D item; - (*next).prev =3D item; - } - } - self.first =3D item; + // INVARIANT: `new_elem` is in the list because we just inserted i= t. + self.first =3D new_elem; } =20 /// Removes the last item from this list. --=20 2.48.0.rc2.279.g1de40edade-goog From nobody Fri Dec 19 21:46:57 2025 Received: from mail-wm1-f73.google.com (mail-wm1-f73.google.com [209.85.128.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 BFBB22144CA for ; Wed, 22 Jan 2025 15:00:45 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.73 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1737558048; cv=none; b=Np1L6zmvJMniC7cKfxf0Py9IQH3ZRAkdFxIzLcgOUuTnTh6njkLVV/LdL+2/FiIwx/RR0CdHTZpeSUvpNoTdEyROgJjqIgqCZheKTxr2l2as0+EKe4rVSitOeNovan78bYZR7tC35pVHch8jkf5NGcralbyvdjRSTvxt4x5pE54= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1737558048; c=relaxed/simple; bh=VoIukpJ0jW+rvwma/DnUGR14jtGq6eBiASSndzwltRE=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=Z1nuAbaA1TezrpoitDwOHxXC4wbOhI1rD+33YLaUtOvW7s5vAexMMFSGfx9lZ+UZrMjCgrpgVeSA03Psr+IcFsZLPo4Wy5k6PwDyMvyxilmzi90T/HvVWXlSGUDOz28iIG4I1HvMJaB0rPJw2Od4TCQEVbuafQD9ISl+mAzZNCY= 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=w02vNvoV; arc=none smtp.client-ip=209.85.128.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="w02vNvoV" Received: by mail-wm1-f73.google.com with SMTP id 5b1f17b1804b1-4362f893bfaso38077505e9.1 for ; Wed, 22 Jan 2025 07:00:45 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1737558044; x=1738162844; 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=wI2ZgrmHeUJUXqNM6UTPag6iKf+yFn85pJiKJJQL+Zg=; b=w02vNvoVcYSX/C4O0SUJipyJHKoDR7VzbhjixPvx2YwRJD2CkjkJgd4Q2AvKASV5eA o2Wcg44+PtbKnq37fJD+42fuZ7puBXwcEtZgzJgn+N1m3YjZgiNqRom+vjnA5qyq5tVy vF1UYUqfvzKKOD0ZKiXdExFTvCuFRUHu5s0iRLFVB9l0hiF9HzknDUWp9ZF83SUcfcfN 8YUupfn7MsUR/fvJfKYPRp6E/Rrm9StaOjxSY5sDpkhgk9+zWCqh1xzjRkJdKEpQtCtJ Yl485A2RJ7in2fHxD9HXx66vMkOhhglsB4o64emYnMhcInuGHrhBW+OxmPaj+x4VjJDh 8caQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1737558044; x=1738162844; 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=wI2ZgrmHeUJUXqNM6UTPag6iKf+yFn85pJiKJJQL+Zg=; b=I7k6D0Vyp3YqsytAU3ceFNn2UeD6pqytfj0ViAenmkcwc3KKHeMKIeli/cqvZQoVAR JsAWKUUhZv5bM02RyakLoYDotLfJ37ktK7Ct9JGW2AIMKxdkmqSY5SRDYJOtLpIia5AV Qxxsg0/KRYPO/WZo0moP1aQh+R7W0cWuAviWFp9v5N5WOdJiwmp1HTFQMm1IAffQaQpm uHVZ+DeAAcdPol5Yp+d4VUJKMEstr7UhbsbrJ4a1GAysVUCwl8x+BkFck86IkQdXH+TA tpp/g/o8uMvlEckqcMxPpwfdwqmoUXGvdXXP3nsZN7FsiATiQLqF60+NDTbXqLVeh79+ iWHg== X-Forwarded-Encrypted: i=1; AJvYcCUDd02+mv/ynxCeHRbWUDlrcYMRnmnutgzVIvx60XqbEnQHqIqgeL7TNpsQoGiBSQS8mFaDGomWWrOoD6U=@vger.kernel.org X-Gm-Message-State: AOJu0YzNBibOBteLd/gIm94QoMJcSML5X1R6uhDypB94mYJEXwhvNUXK UI8fYAbqsBZrUWykQWv0D00FdDezLmaKOv3X23zskYM7sdyfnqqcS7VSGmZ1GgiHfkSBYvSoa1b mxvRYuQZvXr3b4A== X-Google-Smtp-Source: AGHT+IHxlRyh5nlff+wyL4yy04PccI3uaiT1SiqrLHyfm7XHBvMy3fVe7G98QHNpMBPwW4mMD+15HCj1QC35De8= X-Received: from wmqh8.prod.google.com ([2002:a05:600c:3508:b0:436:16c6:831]) (user=aliceryhl job=prod-delivery.src-stubby-dispatcher) by 2002:a05:600c:5021:b0:436:1c0c:bfb6 with SMTP id 5b1f17b1804b1-438914514f2mr186792045e9.27.1737558044301; Wed, 22 Jan 2025 07:00:44 -0800 (PST) Date: Wed, 22 Jan 2025 15:00:18 +0000 In-Reply-To: <20250122-cursor-between-v3-0-aaafbd8af14d@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250122-cursor-between-v3-0-aaafbd8af14d@google.com> X-Developer-Key: i=aliceryhl@google.com; a=openpgp; fpr=49F6C1FAA74960F43A5B86A1EE7A392FDE96209F X-Developer-Signature: v=1; a=openpgp-sha256; l=16340; i=aliceryhl@google.com; h=from:subject:message-id; bh=VoIukpJ0jW+rvwma/DnUGR14jtGq6eBiASSndzwltRE=; b=owEBbQKS/ZANAwAKAQRYvu5YxjlGAcsmYgBnkQgUCrOxsgUKvpKz2MZJ4TN7i0gBg1CWvlGmM CKaU/mVNfuJAjMEAAEKAB0WIQSDkqKUTWQHCvFIvbIEWL7uWMY5RgUCZ5EIFAAKCRAEWL7uWMY5 Rhs/EACGczIDTjvcRCRp1pdOmtXbVf2vJpkzj7ca6pRO5fPsFb+UZ4DjtxPuhHoo8ButG+Nd7cW YtMzxcUVxtGr3QzIjysbCr+G02P//rtNJanr6SN5C4uL1JMiIVDZosAjW/FCKaL/iOFiN6qfbq7 QLH/wv+lKr3+C83N2e09Uy/hJzMxH3yrOhzZ3HkJUrRSKNmvwL7eMexAK3JzDYtrxAyTHn3/+dQ /54pNfyTEi4et+EiBebpPTQCRBxJYjs+t9l3ZjU9LSUHxJStgxxR4ap/q6Jmu0riEtavSLJICdN l8L81GhUjuV/IVlWPHkYw7lG+BKsv8H0aREFzlsjWuYjKcDqHtgmmT5x2bizniZyg08bqnssx9e PdPSRPnqmcHOVAGK3Xt8thHUPsclmTEN9Zcb8XxzUuwmBBXO4AoOahb8hupdxc3NgnOgcye0mUy 3vZa/TBvywV/jhfMYlgL9H6K7jjlxC7Y5TUZzVOZTgXp5Z9Co3fhbht4yv8u4XaNRhPyq57M2Nm xWF8+G2T+ImgR0r1MwO990mU2WUZuCTV/aEv9xjCHlrd2d70HlZa2na5G0sbCBr0Z07gs0RQPK3 VjrmQ+wlxrFmGCYOvkEtUmtOS5zX9uYO/EI0RQs/vy/piPmGCHE/bYjwqOgFJfdGp1+iNLP5xkW YJPFOCti8AKOfHA== X-Mailer: b4 0.13.0 Message-ID: <20250122-cursor-between-v3-2-aaafbd8af14d@google.com> Subject: [PATCH v3 2/2] rust: list: make the cursor point between elements From: Alice Ryhl To: Miguel Ojeda Cc: Alex Gaynor , Boqun Feng , Gary Guo , "=?utf-8?q?Bj=C3=B6rn_Roy_Baron?=" , Benno Lossin , Andreas Hindborg , Trevor Gross , 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 I've been using the linked list cursor for a few different things, and I find it inconvenient to use because all of the functions have signatures along the lines of `Self -> Option`. The root cause of these signatures is that the cursor points *at* an element, rather than *between* two elements. Thus, change the cursor API to point between two elements. This is inspired by the stdlib linked list (well, really by this guy [1]), which also uses cursors that point between elements. The `peek_next` method returns a helper that lets you look at and optionally remove the element, as one common use-case of cursors is to iterate a list to look for an element, then remove that element. For many of the methods, this will reduce how many we need since they now just need a prev/next method, instead of the current state where you may end up needing all of curr/prev/next. Also, if we decide to add a function for splitting a list into two lists at the cursor, then a cursor that points between elements is exactly what makes the most sense. Another advantage is that this means you can now have a cursor into an empty list. Link: https://rust-unofficial.github.io/too-many-lists/sixth-cursors-intro.= html [1] Reviewed-by: Andreas Hindborg Signed-off-by: Alice Ryhl --- rust/kernel/list.rs | 333 +++++++++++++++++++++++++++++++++++++++++++-----= ---- 1 file changed, 278 insertions(+), 55 deletions(-) diff --git a/rust/kernel/list.rs b/rust/kernel/list.rs index 97b3599b7207..1d4e034f818c 100644 --- a/rust/kernel/list.rs +++ b/rust/kernel/list.rs @@ -483,17 +483,21 @@ pub fn push_all_back(&mut self, other: &mut List) { other.first =3D ptr::null_mut(); } =20 - /// Returns a cursor to the first element of the list. - /// - /// If the list is empty, this returns `None`. - pub fn cursor_front(&mut self) -> Option> { - if self.first.is_null() { - None - } else { - Some(Cursor { - current: self.first, - list: self, - }) + /// Returns a cursor that points before the first element of the list. + pub fn cursor_front(&mut self) -> Cursor<'_, T, ID> { + // INVARIANT: `self.first` is in this list. + Cursor { + next: self.first, + list: self, + } + } + + /// Returns a cursor that points after the last element in the list. + pub fn cursor_back(&mut self) -> Cursor<'_, T, ID> { + // INVARIANT: `next` is allowed to be null. + Cursor { + next: core::ptr::null_mut(), + list: self, } } =20 @@ -573,69 +577,288 @@ fn next(&mut self) -> Option> { =20 /// A cursor into a [`List`]. /// +/// A cursor always rests between two elements in the list. This means tha= t a cursor has a previous +/// and next element, but no current element. It also means that it's poss= ible to have a cursor +/// into an empty list. +/// +/// # Examples +/// +/// ``` +/// use kernel::list::{List, ListArc, ListLinks}; +/// +/// #[pin_data] +/// struct ListItem { +/// value: u32, +/// #[pin] +/// links: ListLinks, +/// } +/// +/// kernel::list::impl_has_list_links! { +/// impl HasListLinks<0> for ListItem { self.links } +/// } +/// kernel::list::impl_list_arc_safe! { +/// impl ListArcSafe<0> for ListItem { untracked; } +/// } +/// kernel::list::impl_list_item! { +/// impl ListItem<0> for ListItem { using ListLinks; } +/// } +/// +/// // Use a cursor to remove the first element with the given value. +/// fn remove_first(list: &mut List, value: u32) -> Option> { +/// let mut cursor =3D list.cursor_front(); +/// while let Some(next) =3D cursor.peek_next() { +/// if next.value =3D=3D value { +/// return Some(next.remove()); +/// } +/// cursor.move_next(); +/// } +/// None +/// } +/// +/// // Use a cursor to remove all elements with the given value. The remov= ed elements are moved to +/// // a new list. +/// fn remove_all(list: &mut List, value: u32) -> List= { +/// let mut out =3D List::new(); +/// let mut cursor =3D list.cursor_front(); +/// while let Some(next) =3D cursor.peek_next() { +/// if next.value =3D=3D value { +/// out.push_back(next.remove()); +/// } else { +/// cursor.move_next(); +/// } +/// } +/// out +/// } +/// +/// // Use a cursor to insert a value at a specific index. Returns an erro= r if the index is out of +/// // bounds. +/// fn insert_at(list: &mut List, new: ListArc, idx: u= size) -> Result { +/// let mut cursor =3D list.cursor_front(); +/// for _ in 0..idx { +/// if !cursor.move_next() { +/// return Err(EINVAL); +/// } +/// } +/// cursor.insert_next(new); +/// Ok(()) +/// } +/// +/// // Merge two sorted lists into a single sorted list. +/// fn merge_sorted(list: &mut List, merge: List) { +/// let mut cursor =3D list.cursor_front(); +/// for to_insert in merge { +/// while let Some(next) =3D cursor.peek_next() { +/// if to_insert.value < next.value { +/// break; +/// } +/// cursor.move_next(); +/// } +/// cursor.insert_prev(to_insert); +/// } +/// } +/// ``` +/// /// # Invariants /// -/// The `current` pointer points a value in `list`. +/// The `next` pointer is null or points a value in `list`. pub struct Cursor<'a, T: ?Sized + ListItem, const ID: u64 =3D 0> { - current: *mut ListLinksFields, list: &'a mut List, + /// Points at the element after this cursor, or null if the cursor is = after the last element. + next: *mut ListLinksFields, } =20 impl<'a, T: ?Sized + ListItem, const ID: u64> Cursor<'a, T, ID> { - /// Access the current element of this cursor. - pub fn current(&self) -> ArcBorrow<'_, T> { - // SAFETY: The `current` pointer points a value in the list. - let me =3D unsafe { T::view_value(ListLinks::from_fields(self.curr= ent)) }; - // SAFETY: - // * All values in a list are stored in an `Arc`. - // * The value cannot be removed from the list for the duration of= the lifetime annotated - // on the returned `ArcBorrow`, because removing it from the lis= t would require mutable - // access to the cursor or the list. However, the `ArcBorrow` ho= lds an immutable borrow - // on the cursor, which in turn holds a mutable borrow on the li= st, so any such - // mutable access requires first releasing the immutable borrow = on the cursor. - // * Values in a list never have a `UniqueArc` reference, because = the list has a `ListArc` - // reference, and `UniqueArc` references must be unique. - unsafe { ArcBorrow::from_raw(me) } + /// Returns a pointer to the element before the cursor. + /// + /// Returns null if there is no element before the cursor. + fn prev_ptr(&self) -> *mut ListLinksFields { + let mut next =3D self.next; + let first =3D self.list.first; + if next =3D=3D first { + // We are before the first element. + return core::ptr::null_mut(); + } + + if next.is_null() { + // We are after the last element, so we need a pointer to the = last element, which is + // the same as `(*first).prev`. + next =3D first; + } + + // SAFETY: `next` can't be null, because then `first` must also be= null, but in that case + // we would have exited at the `next =3D=3D first` check. Thus, `n= ext` is an element in the + // list, so we can access its `prev` pointer. + unsafe { (*next).prev } } =20 - /// Move the cursor to the next element. - pub fn next(self) -> Option> { - // SAFETY: The `current` field is always in a list. - let next =3D unsafe { (*self.current).next }; + /// Access the element after this cursor. + pub fn peek_next(&mut self) -> Option>= { + if self.next.is_null() { + return None; + } + + // INVARIANT: + // * We just checked that `self.next` is non-null, so it must be i= n `self.list`. + // * `ptr` is equal to `self.next`. + Some(CursorPeek { + ptr: self.next, + cursor: self, + }) + } + + /// Access the element before this cursor. + pub fn peek_prev(&mut self) -> Option= > { + let prev =3D self.prev_ptr(); + + if prev.is_null() { + return None; + } + + // INVARIANT: + // * We just checked that `prev` is non-null, so it must be in `se= lf.list`. + // * `self.prev_ptr()` never returns `self.next`. + Some(CursorPeek { + ptr: prev, + cursor: self, + }) + } + + /// Move the cursor one element forward. + /// + /// If the cursor is after the last element, then this call does nothi= ng. This call returns + /// `true` if the cursor's position was changed. + pub fn move_next(&mut self) -> bool { + if self.next.is_null() { + return false; + } + + // SAFETY: `self.next` is an element in the list and we borrow the= list mutably, so we can + // access the `next` field. + let mut next =3D unsafe { (*self.next).next }; =20 if next =3D=3D self.list.first { - None - } else { - // INVARIANT: Since `self.current` is in the `list`, its `next= ` pointer is also in the - // `list`. - Some(Cursor { - current: next, - list: self.list, - }) + next =3D core::ptr::null_mut(); } + + // INVARIANT: `next` is either null or the next element after an e= lement in the list. + self.next =3D next; + true } =20 - /// Move the cursor to the previous element. - pub fn prev(self) -> Option> { - // SAFETY: The `current` field is always in a list. - let prev =3D unsafe { (*self.current).prev }; + /// Move the cursor one element backwards. + /// + /// If the cursor is before the first element, then this call does not= hing. This call returns + /// `true` if the cursor's position was changed. + pub fn move_prev(&mut self) -> bool { + if self.next =3D=3D self.list.first { + return false; + } =20 - if self.current =3D=3D self.list.first { - None + // INVARIANT: `prev_ptr()` always returns a pointer that is null o= r in the list. + self.next =3D self.prev_ptr(); + true + } + + /// Inserts an element where the cursor is pointing and get a pointer = to the new element. + fn insert_inner(&mut self, item: ListArc) -> *mut ListLinksFiel= ds { + let ptr =3D if self.next.is_null() { + self.list.first } else { - // INVARIANT: Since `self.current` is in the `list`, its `prev= ` pointer is also in the - // `list`. - Some(Cursor { - current: prev, - list: self.list, - }) - } + self.next + }; + // SAFETY: + // * `ptr` is an element in the list or null. + // * if `ptr` is null, then `self.list.first` is null so the list = is empty. + unsafe { self.list.insert_inner(item, ptr) } + } + + /// Inserts an element after this cursor. + pub fn insert_next(&mut self, item: ListArc) { + self.next =3D self.insert_inner(item); + } + + /// Inserts an element before this cursor. + pub fn insert_prev(&mut self, item: ListArc) { + self.insert_inner(item); } =20 - /// Remove the current element from the list. + /// Remove the next element from the list. + pub fn remove_next(&mut self) -> Option> { + self.peek_next().map(|v| v.remove()) + } + + /// Remove the previous element from the list. + pub fn remove_prev(&mut self) -> Option> { + self.peek_prev().map(|v| v.remove()) + } +} + +/// References the element in the list next to the cursor. +/// +/// # Invariants +/// +/// * `ptr` is an element in `self.cursor.list`. +/// * `ISNEXT =3D=3D (self.ptr =3D=3D self.cursor.next)`. +pub struct CursorPeek<'a, 'b, T: ?Sized + ListItem, const ISNEXT: bool= , const ID: u64> { + cursor: &'a mut Cursor<'b, T, ID>, + ptr: *mut ListLinksFields, +} + +impl<'a, 'b, T: ?Sized + ListItem, const ISNEXT: bool, const ID: u64> + CursorPeek<'a, 'b, T, ISNEXT, ID> +{ + /// Remove the element from the list. pub fn remove(self) -> ListArc { - // SAFETY: The `current` pointer always points at a member of the = list. - unsafe { self.list.remove_internal(self.current) } + if ISNEXT { + self.cursor.move_next(); + } + + // INVARIANT: `self.ptr` is not equal to `self.cursor.next` due to= the above `move_next` + // call. + // SAFETY: By the type invariants of `Self`, `next` is not null, s= o `next` is an element of + // `self.cursor.list` by the type invariants of `Cursor`. + unsafe { self.cursor.list.remove_internal(self.ptr) } + } + + /// Access this value as an [`ArcBorrow`]. + pub fn arc(&self) -> ArcBorrow<'_, T> { + // SAFETY: `self.ptr` points at an element in `self.cursor.list`. + let me =3D unsafe { T::view_value(ListLinks::from_fields(self.ptr)= ) }; + // SAFETY: + // * All values in a list are stored in an `Arc`. + // * The value cannot be removed from the list for the duration of= the lifetime annotated + // on the returned `ArcBorrow`, because removing it from the lis= t would require mutable + // access to the `CursorPeek`, the `Cursor` or the `List`. Howev= er, the `ArcBorrow` holds + // an immutable borrow on the `CursorPeek`, which in turn holds = a mutable borrow on the + // `Cursor`, which in turn holds a mutable borrow on the `List`,= so any such mutable + // access requires first releasing the immutable borrow on the `= CursorPeek`. + // * Values in a list never have a `UniqueArc` reference, because = the list has a `ListArc` + // reference, and `UniqueArc` references must be unique. + unsafe { ArcBorrow::from_raw(me) } + } +} + +impl<'a, 'b, T: ?Sized + ListItem, const ISNEXT: bool, const ID: u64> = core::ops::Deref + for CursorPeek<'a, 'b, T, ISNEXT, ID> +{ + // If you change the `ptr` field to have type `ArcBorrow<'a, T>`, it m= ight seem like you could + // get rid of the `CursorPeek::arc` method and change the deref target= to `ArcBorrow<'a, T>`. + // However, that doesn't work because 'a is too long. You could obtain= an `ArcBorrow<'a, T>` + // and then call `CursorPeek::remove` without giving up the `ArcBorrow= <'a, T>`, which would be + // unsound. + type Target =3D T; + + fn deref(&self) -> &T { + // SAFETY: `self.ptr` points at an element in `self.cursor.list`. + let me =3D unsafe { T::view_value(ListLinks::from_fields(self.ptr)= ) }; + + // SAFETY: The value cannot be removed from the list for the durat= ion of the lifetime + // annotated on the returned `&T`, because removing it from the li= st would require mutable + // access to the `CursorPeek`, the `Cursor` or the `List`. However= , the `&T` holds an + // immutable borrow on the `CursorPeek`, which in turn holds a mut= able borrow on the + // `Cursor`, which in turn holds a mutable borrow on the `List`, s= o any such mutable access + // requires first releasing the immutable borrow on the `CursorPee= k`. + unsafe { &*me } } } =20 --=20 2.48.0.rc2.279.g1de40edade-goog