Received: by 2002:a05:6a10:9e8c:0:0:0:0 with SMTP id y12csp3379151pxx; Mon, 2 Nov 2020 07:24:55 -0800 (PST) X-Google-Smtp-Source: ABdhPJwzSjvWfMjZSKSDzEHxx4SvVi2mZErvY9uw6I55diHQ9DerUFjHKfvSiWBjVK7baMESt+Y6 X-Received: by 2002:a17:906:647:: with SMTP id t7mr16394360ejb.428.1604330694812; Mon, 02 Nov 2020 07:24:54 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1604330694; cv=none; d=google.com; s=arc-20160816; b=E8eZwok9/WDTJW8O04fKfb67Endv3srrSmgYdKiH9bH7dDfcCJGeiX86WSNelA1ogr bnUZhSZTzN5SUGQT13MmFi7VPly3IxnJHvz/g0sQE7VxOUZ2DQAL0FKEf+Awa4vGdwfx 6rFNHIaDQK5ejlsMWsIP6bnOY+ZHtRQi+OaLhqzDHQXMH7G38/gBkjbD8yArh0rGMSiq S6l9Uv/j+x7OA444Mu97Ij4PZqwmPDbTTHHAjES5udkLqTNoCmz7k1zT0Rw5t0cDGH3M XGvjC0XLMRdIDGwBxf86V15qKQD/oLBgraMMYJTnx76aG7wNep8aYzgozatvjhFd0Nh7 dc6w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=//ohTnlCNdzVCN7+X/oV4S8f/hxX7VdQ1OV3L6uwfhQ=; b=yqxTGcpi1HNN6xU8MB2Ti1AzeFtjX1WJOBAE2NXkfDCDTQjE1xa5GY8GfPBRZjuvbw 1mfqF4RxA02hYO4qgu0SlYqwh7Y9qLYBQVIb8DYCiYqJyC2rgCsQFN+Nd+FWmd/J0zKE Nsn8bxgTzfAqao+Jgf93nM0+W4DQIffhjUDZydQ8EazRYp1GlUplRQiVXVDZnBtYGYxw rnRSUG6bcLBnIHnFbfmIkh9HqbZ2m6UrwwBJTeUGyHVcGXonDwuX+MNcHLpWCLUMzXbU aqsHLoS0x/CANMlRe3S8XILQLp1x0rAXtTKJgd/EG4+ZNry7NuKMRRR70s1Q8lJw/FsP 8SrQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@bgdev-pl.20150623.gappssmtp.com header.s=20150623 header.b=hf6tvqnc; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id du13si10123684ejc.60.2020.11.02.07.24.30; Mon, 02 Nov 2020 07:24:54 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@bgdev-pl.20150623.gappssmtp.com header.s=20150623 header.b=hf6tvqnc; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726507AbgKBPUr (ORCPT + 99 others); Mon, 2 Nov 2020 10:20:47 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56702 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726482AbgKBPUp (ORCPT ); Mon, 2 Nov 2020 10:20:45 -0500 Received: from mail-wm1-x343.google.com (mail-wm1-x343.google.com [IPv6:2a00:1450:4864:20::343]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id BF253C061A4A for ; Mon, 2 Nov 2020 07:20:44 -0800 (PST) Received: by mail-wm1-x343.google.com with SMTP id h62so5047582wme.3 for ; Mon, 02 Nov 2020 07:20:44 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bgdev-pl.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=//ohTnlCNdzVCN7+X/oV4S8f/hxX7VdQ1OV3L6uwfhQ=; b=hf6tvqncwp3fkOIckHP3XNhZspBIs+DJCE72k4fKwWNNEV8tVYATPrk61RVN2LLtoy wyuT8kZJhciWHJYnIywhLp+tPKS38UhRZoDdJRNvydyfnF4jPWOOLNFaCIa5fWpPbqiE CPEcfMQCT8yeENUO+TRhPv8NZwnqBa/5qBgOFPacWV7BhHFuUJ5ASnX9vmHxuqU/2Ygy yl+QCczTs9fSAAWT/g0PiS+PxK7jWNW2R3D8R0j5FcPe+c2M0+55tW8i3tKhxnOrTffM d3zN4lSBg/UxZjb51Fwl4foaEs43EhxOe1ue2FxQvf65j62JS7V+E/ior/FALPI70AY8 3BnQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=//ohTnlCNdzVCN7+X/oV4S8f/hxX7VdQ1OV3L6uwfhQ=; b=NAYzamJw0dmrwxovuCflLZZoJ0g7KJW7iUq9hcBvZqC612ovELpiThZbRPNUkeqzgw zmi9oeIgg9AopF7O78Aknv9Ku3swhhlBGVKUnK5uJnXMO/97zusFOmsW0wfxh1vdJjyp AKrh0bIsovNiHZIC9ZvWO1VmwsZL5zR5fCphZRv8H0Drhc2ZM5fV91b9YhxNNHbcbwMK R2NMNoQvQ5UMWoJF7o6riqam1YfQ1/Qc/sYYv5dtn/uJCoiwIzjYWIaK3uWKV9k/fLKb osLFgElRhWLOmVHKHpq/D88NO0D6ceexYVi7kQl34621CcgIr1UYrLT/19qN9J+eD0Y0 HOhQ== X-Gm-Message-State: AOAM530VgelzeIZALU7dF8shs1kI303C6CL9xYcnyUC3OT4nYAP1yom2 xdAgkm2n8vMT2wm+7YlZwVQHSQ== X-Received: by 2002:a7b:cf25:: with SMTP id m5mr18034156wmg.124.1604330443460; Mon, 02 Nov 2020 07:20:43 -0800 (PST) Received: from debian-brgl.home (amarseille-656-1-4-167.w90-8.abo.wanadoo.fr. [90.8.158.167]) by smtp.gmail.com with ESMTPSA id b18sm15138014wmj.41.2020.11.02.07.20.41 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 02 Nov 2020 07:20:42 -0800 (PST) From: Bartosz Golaszewski To: Andy Shevchenko , Sumit Semwal , Gustavo Padovan , =?UTF-8?q?Christian=20K=C3=B6nig?= , Mauro Carvalho Chehab , Borislav Petkov , Tony Luck , James Morse , Robert Richter , Maarten Lankhorst , Maxime Ripard , Thomas Zimmermann , David Airlie , Daniel Vetter , Alexander Shishkin , Linus Walleij , "Michael S . Tsirkin" , Jason Wang , Christoph Lameter , Pekka Enberg , David Rientjes , Joonsoo Kim , Andrew Morton , Jaroslav Kysela , Takashi Iwai Cc: linux-media@vger.kernel.org, dri-devel@lists.freedesktop.org, linaro-mm-sig@lists.linaro.org, linux-kernel@vger.kernel.org, linux-edac@vger.kernel.org, linux-gpio@vger.kernel.org, kvm@vger.kernel.org, virtualization@lists.linux-foundation.org, netdev@vger.kernel.org, linux-mm@kvack.org, alsa-devel@alsa-project.org, Bartosz Golaszewski , Vlastimil Babka Subject: [PATCH v2 1/8] mm: slab: provide krealloc_array() Date: Mon, 2 Nov 2020 16:20:30 +0100 Message-Id: <20201102152037.963-2-brgl@bgdev.pl> X-Mailer: git-send-email 2.29.1 In-Reply-To: <20201102152037.963-1-brgl@bgdev.pl> References: <20201102152037.963-1-brgl@bgdev.pl> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Bartosz Golaszewski When allocating an array of elements, users should check for multiplication overflow or preferably use one of the provided helpers like: kmalloc_array(). There's no krealloc_array() counterpart but there are many users who use regular krealloc() to reallocate arrays. Let's provide an actual krealloc_array() implementation. While at it: add some documentation regarding krealloc. Signed-off-by: Bartosz Golaszewski Acked-by: Vlastimil Babka --- Documentation/core-api/memory-allocation.rst | 4 ++++ include/linux/slab.h | 18 ++++++++++++++++++ 2 files changed, 22 insertions(+) diff --git a/Documentation/core-api/memory-allocation.rst b/Documentation/core-api/memory-allocation.rst index 4446a1ac36cc..6dc38b40439a 100644 --- a/Documentation/core-api/memory-allocation.rst +++ b/Documentation/core-api/memory-allocation.rst @@ -147,6 +147,10 @@ The address of a chunk allocated with `kmalloc` is aligned to at least ARCH_KMALLOC_MINALIGN bytes. For sizes which are a power of two, the alignment is also guaranteed to be at least the respective size. +Chunks allocated with `kmalloc` can be resized with `krealloc`. Similarly +to `kmalloc_array`: a helper for resising arrays is provided in the form of +`krealloc_array`. + For large allocations you can use vmalloc() and vzalloc(), or directly request pages from the page allocator. The memory allocated by `vmalloc` and related functions is not physically contiguous. diff --git a/include/linux/slab.h b/include/linux/slab.h index dd6897f62010..be4ba5867ac5 100644 --- a/include/linux/slab.h +++ b/include/linux/slab.h @@ -592,6 +592,24 @@ static inline void *kmalloc_array(size_t n, size_t size, gfp_t flags) return __kmalloc(bytes, flags); } +/** + * krealloc_array - reallocate memory for an array. + * @p: pointer to the memory chunk to reallocate + * @new_n: new number of elements to alloc + * @new_size: new size of a single member of the array + * @flags: the type of memory to allocate (see kmalloc) + */ +static __must_check inline void * +krealloc_array(void *p, size_t new_n, size_t new_size, gfp_t flags) +{ + size_t bytes; + + if (unlikely(check_mul_overflow(new_n, new_size, &bytes))) + return NULL; + + return krealloc(p, bytes, flags); +} + /** * kcalloc - allocate memory for an array. The memory is set to zero. * @n: number of elements. -- 2.29.1