From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id D447312A16C for ; Sat, 6 Jul 2024 15:16:40 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.133.124 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1720279002; cv=none; b=WYffVjydIpQPFjlYlqGyUo2GFH6ACbLT2264iJcFIRnclZs4ENji5VjkT/DgBgrY/oo9zhXrbKTS/TyxJpUy4mxI4UoyiL1Eq7sCdwC5P3v2JFWpjALZbH6422nmcBjTygwxxJ2urf5EaLFfovcVpLW9vN5QygbvFmePTYCe7ic= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1720279002; c=relaxed/simple; bh=V8xKziIWvw+z7yiIc3EtT3gOTGIXEMHhjqqvEppRrXc=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: In-Reply-To:Content-Type:Content-Disposition; b=q9dl8IujMz/JnOyewd7iwaLRhfK7Yg/OS5LFH6MntV5btl3DUaYcIKcM9TLXaWI0Q67duqZoxV8lDxectqHrx5s4FeY4hvkl5GkzsFSJa6vdX3RQqw7fnuEoy3rWDIwjJB5CCoCzf1DoDc+Sdx8jkXa3xD52taKbgx2XJwC7NVo= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=FvOZ78AU; arc=none smtp.client-ip=170.10.133.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="FvOZ78AU" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1720278999; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=R7m3OsbObA3XsltJe97nQ6y2kpIXDhS9/pjgnJGh+oo=; b=FvOZ78AU7cBujBtxi662MkXDPOzpxZmXJ4RztlDfB+n0r80FfEziZuJRkgvzYEZ+SEMTVs D7pe97F+tRPeGQ2W4C0Wy1+XkOpBr66bw5BxhR0lU9qszD4To00qsg4nXX2iRUiSa8li1t ZGh/rIozml+TIT3Xm9E77aQyL3QuPRk= Received: from mail-wr1-f70.google.com (mail-wr1-f70.google.com [209.85.221.70]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-84-EGgY7UEeOlWdDXHZBV_b3w-1; Sat, 06 Jul 2024 11:16:38 -0400 X-MC-Unique: EGgY7UEeOlWdDXHZBV_b3w-1 Received: by mail-wr1-f70.google.com with SMTP id ffacd0b85a97d-36794fcfdd6so1307342f8f.3 for ; Sat, 06 Jul 2024 08:16:38 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1720278997; x=1720883797; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=R7m3OsbObA3XsltJe97nQ6y2kpIXDhS9/pjgnJGh+oo=; b=E+xGcxWQ4IWPKyjJG1VY4aiien/5ivl7FhE8lLdkQ/qL1gQ1VqPG+lZVccOubPImPD wBAglbf9UP01i9tFIraMclDQhc83YQ69wHyJoQBRkOJlOK/S9Go7VeBpFiG5wzPhSkNK bgivq7WcrAv6XqJKVMMgDhuZr19AO+rdOT/lUB4phNhlaElpXZSz7BW1RmTL6gAinsn4 DCNwM1l6RORvRivvNXm+x9/ANX/CHrYDGpcZGMop27ze390cRHcH7U2tNdJZraOJpdQi Bbbx0bI6oCUHZwMVthifKuwMsoGDXLD/a6mPre8z1V9yv7m6tCjocHQCWI8Wk/asfdhz bWNw== X-Forwarded-Encrypted: i=1; AJvYcCVqpSh/uaVeI1rJ2IXPuJ1zX0tLnxsq0Jeu4KN4NGGhtZCXohPXfI2WA/boYfmKBMpagqYpFwGCC1O2d6o2WYG3TkOfPbxZJRxh1vYwK94= X-Gm-Message-State: AOJu0YzHBpGTi4craZddUloJSNqpSjHw0agk5woJyXvQ7UcX5rgYar1s ApIyCF1LpGNhpbKfzw2PamOEGKA59NsOecbtMLmwpkQeSUSXdwT55IhMb5GX/jTOjxLa/GPiBm+ i0KhjKZeTBv7vhKHba3GMGdTvRs/3paA9LujlIdMurHUoIs2h1u8uPOtbTUet1Tk2 X-Received: by 2002:a5d:56ce:0:b0:367:83e9:b4a5 with SMTP id ffacd0b85a97d-3679dd652fcmr4832295f8f.49.1720278997261; Sat, 06 Jul 2024 08:16:37 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHafTnjFM5kZgi7HFwiFktMu0O4vCTp/tC4spHajbb7STT0WS9kIOsUlPmPs+jwkyFOOdkfyQ== X-Received: by 2002:a5d:56ce:0:b0:367:83e9:b4a5 with SMTP id ffacd0b85a97d-3679dd652fcmr4832271f8f.49.1720278996838; Sat, 06 Jul 2024 08:16:36 -0700 (PDT) Received: from pollux.localdomain ([2a02:810d:4b3f:ee94:701e:8fb8:a84f:6308]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-36797e5d97bsm7901940f8f.92.2024.07.06.08.16.35 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 06 Jul 2024 08:16:36 -0700 (PDT) Date: Sat, 6 Jul 2024 17:16:35 +0200 From: Danilo Krummrich To: Benno Lossin Cc: ojeda@kernel.org, alex.gaynor@gmail.com, wedsonaf@gmail.com, boqun.feng@gmail.com, gary@garyguo.net, bjorn3_gh@protonmail.com, a.hindborg@samsung.com, aliceryhl@google.com, daniel.almeida@collabora.com, faith.ekstrand@collabora.com, boris.brezillon@collabora.com, lina@asahilina.net, mcanal@igalia.com, zhiw@nvidia.com, acurrid@nvidia.com, cjia@nvidia.com, jhubbard@nvidia.com, airlied@redhat.com, ajanulgu@redhat.com, lyude@redhat.com, linux-kernel@vger.kernel.org, rust-for-linux@vger.kernel.org Subject: Re: [PATCH 07/20] rust: alloc: implement `Vmalloc` allocator Message-ID: References: <20240704170738.3621-1-dakr@redhat.com> <20240704170738.3621-8-dakr@redhat.com> <596c6446-eca4-46a8-91c5-e71e92c61062@proton.me> <796b50c4-0824-4b9f-97f9-750f34096ed6@proton.me> Precedence: bulk X-Mailing-List: rust-for-linux@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 In-Reply-To: <796b50c4-0824-4b9f-97f9-750f34096ed6@proton.me> X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset=us-ascii Content-Disposition: inline On Sat, Jul 06, 2024 at 01:21:39PM +0000, Benno Lossin wrote: > On 06.07.24 13:13, Danilo Krummrich wrote: > > On Sat, Jul 06, 2024 at 10:41:28AM +0000, Benno Lossin wrote: > >> On 04.07.24 19:06, Danilo Krummrich wrote: > >>> @@ -112,6 +118,55 @@ unsafe fn alloc_zeroed(&self, layout: Layout) -> *mut u8 { > >>> } > >>> } > >>> > >>> +unsafe impl Allocator for Vmalloc { > >>> + unsafe fn realloc( > >>> + &self, > >>> + src: *mut u8, > >>> + old_size: usize, > >>> + layout: Layout, > >>> + flags: Flags, > >>> + ) -> Result, AllocError> { > >>> + let mut size = aligned_size(layout); > >>> + > >>> + let dst = if size == 0 { > >>> + // SAFETY: `src` is guaranteed to be previously allocated with this `Allocator` or NULL. > >>> + unsafe { bindings::vfree(src.cast()) }; > >>> + NonNull::dangling() > >>> + } else if size <= old_size { > >>> + size = old_size; > >>> + NonNull::new(src).ok_or(AllocError)? > >>> + } else { > >>> + // SAFETY: `src` is guaranteed to point to valid memory with a size of at least > >>> + // `old_size`, which was previously allocated with this `Allocator` or NULL. > >>> + let dst = unsafe { bindings::__vmalloc_noprof(size as u64, flags.0) }; > >>> + > >>> + // Validate that we actually allocated the requested memory. > >>> + let dst = NonNull::new(dst.cast()).ok_or(AllocError)?; > >>> + > >>> + if !src.is_null() { > >>> + // SAFETY: `src` is guaranteed to point to valid memory with a size of at least > >>> + // `old_size`; `dst` is guaranteed to point to valid memory with a size of at least > >>> + // `size`. > >>> + unsafe { > >>> + core::ptr::copy_nonoverlapping( > >>> + src, > >>> + dst.as_ptr(), > >>> + core::cmp::min(old_size, size), > >>> + ) > >>> + }; > >>> + > >>> + // SAFETY: `src` is guaranteed to be previously allocated with this `Allocator` or > >>> + // NULL. > >>> + unsafe { bindings::vfree(src.cast()) } > >>> + } > >>> + > >>> + dst > >>> + }; > >> > >> If we had not a single realloc, but shrink/grow/free/alloc, then we > >> would not need these checks here, I personally would prefer that, what > >> do you guys think? > > > > Yeah, but look at `Kmalloc`, you'd have these checks in `Kmalloc`'s shrink/grow > > functions instead, since `krealloc()` already behaves this way. > > In the kmalloc case you would have different instantiations, no checks. > IIUC for freeing you would do `krealloc(ptr, 0, flags)`. We can't allow to shrink on a `grow` call and we can't allow to grow on a `shrink` call, so we have to do the checks there before we call into krealloc(). Unless, we say that passing stupid arguments to `grow` can actually shrink and vice versa, but then we can just keep `realloc`, right? > > > Personally, I don't see much value in `shrink` and `grow`. I think > > implementations end up calling into some `realloc` with either `new < old` or > > `new > old` anyway. > > I think a `resize` would make more sense. In general, splitting > resizing, creating and freeing makes sense to me. Please see the other mail thread. > > --- > Cheers, > Benno >