From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 3BE0CEB8FA5 for ; Wed, 6 Sep 2023 08:48:03 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 84247280004; Wed, 6 Sep 2023 04:48:02 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 7D0908E0014; Wed, 6 Sep 2023 04:48:02 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 61DF2280004; Wed, 6 Sep 2023 04:48:02 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id 4D2D88E0014 for ; Wed, 6 Sep 2023 04:48:02 -0400 (EDT) Received: from smtpin12.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 20481B401E for ; Wed, 6 Sep 2023 08:48:02 +0000 (UTC) X-FDA: 81205545204.12.0175C31 Received: from smtp-out1.suse.de (smtp-out1.suse.de [195.135.220.28]) by imf22.hostedemail.com (Postfix) with ESMTP id EA636C0009 for ; Wed, 6 Sep 2023 08:47:58 +0000 (UTC) Authentication-Results: imf22.hostedemail.com; dkim=pass header.d=suse.cz header.s=susede2_rsa header.b=21NqZz92; dkim=pass header.d=suse.cz header.s=susede2_ed25519 header.b=Yv4ga7yR; dmarc=none; spf=pass (imf22.hostedemail.com: domain of vbabka@suse.cz designates 195.135.220.28 as permitted sender) smtp.mailfrom=vbabka@suse.cz ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1693990079; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=0uT0YFvIdeCTeE263WdlZfe3neZinltqXCqjpX729zw=; b=3xjGkxPcWA8MDbMEvOB2q6bW8YfB9yB4IlWhwprcr+CGXMmTG++0VQSatvDvGdiZ0uwLai TUWoCxLr2ZAr62eGxesEEo5kC7OoL6CmwT6ja7ZYuaK1KLL3BcfJ7btv9U0ShPwNil17Hl 8Xi+IwJBKPrJSEzREWsSnCEdG5OyTAk= ARC-Authentication-Results: i=1; imf22.hostedemail.com; dkim=pass header.d=suse.cz header.s=susede2_rsa header.b=21NqZz92; dkim=pass header.d=suse.cz header.s=susede2_ed25519 header.b=Yv4ga7yR; dmarc=none; spf=pass (imf22.hostedemail.com: domain of vbabka@suse.cz designates 195.135.220.28 as permitted sender) smtp.mailfrom=vbabka@suse.cz ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1693990079; a=rsa-sha256; cv=none; b=hSp7fjkD0lShqN6d/gwfDepzJo6syEVF5gtnWUgBRPI3ihVUgSxkm5fDFRRIealenNyUKq 7XxgE39Ut7EW+qR98GiDYy1DQZjvv/AXv0CNkdM9sjF1kAYitqA19fqd+UMiOYTIXYPy2Y qMCAy9eYkHGlDNsViSJfugSisNKJ9+w= Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by smtp-out1.suse.de (Postfix) with ESMTPS id 38B962243E; Wed, 6 Sep 2023 08:47:57 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.cz; s=susede2_rsa; t=1693990077; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=0uT0YFvIdeCTeE263WdlZfe3neZinltqXCqjpX729zw=; b=21NqZz92+OsEE5a9/xev+KsJtysrVwI5yH5OkVNTHBXjHiP/HmE1qvNQs92sjF41gsmJEv rca7+EX4iPm9W3uhzvXEkuils4PvkhMQ864WRdkD2eHWzAfrt5EyqDoz2Wq61jjI2LugAn pw+ga6eLVsJEaPrfH7q5qZ0iLBfO2mU= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.cz; s=susede2_ed25519; t=1693990077; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=0uT0YFvIdeCTeE263WdlZfe3neZinltqXCqjpX729zw=; b=Yv4ga7yRQr/H6F4uobneZT4bXjAHJB1O9JB6TvbS5TJPFZputXmEpCrYcFYon7Q47xd/fN uHYl8gIdoHu+R+BA== Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by imap2.suse-dmz.suse.de (Postfix) with ESMTPS id 08B8B1333E; Wed, 6 Sep 2023 08:47:57 +0000 (UTC) Received: from dovecot-director2.suse.de ([192.168.254.65]) by imap2.suse-dmz.suse.de with ESMTPSA id 4tsoAb08+GQndAAAMHmgww (envelope-from ); Wed, 06 Sep 2023 08:47:57 +0000 Message-ID: <2cb3f35b-a18c-75fa-d73e-95a4fb8cf079@suse.cz> Date: Wed, 6 Sep 2023 10:47:56 +0200 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.14.0 Subject: Re: [PATCH] slab: kmalloc_size_roundup() must not return 0 for non-zero size Content-Language: en-US To: David Laight , "linux-kernel@vger.kernel.org" , "'linux-mm@kvack.org'" Cc: 'Kees Cook' , 'Christoph Lameter' , 'Pekka Enberg' , 'David Rientjes' , 'Joonsoo Kim' , 'Andrew Morton' , 'Eric Dumazet' , Hyeonggon Yoo <42.hyeyoo@gmail.com>, Roman Gushchin References: From: Vlastimil Babka In-Reply-To: Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit X-Rspamd-Queue-Id: EA636C0009 X-Rspam-User: X-Rspamd-Server: rspam02 X-Stat-Signature: gtoy588o197c6wjg69d4k7j3jyeoqfe7 X-HE-Tag: 1693990078-974962 X-HE-Meta: U2FsdGVkX1+qPVtlHU98WR1Djy3mVaobQIvSr+2SlQRvp4qfT8M61tL6r2FXQYsKIZ/j4Jtz5c4y48vQNS3a78g08ZpGZwAqokmS9AqtWMp9/OyZokt0MgPAkxVDdfuqpZUrRswOzJkPOPUAAu2FiKn6jvhmnTmwbZ3E1TnNlz7nPOK7ZO9da5DVVn/bEZx8fVvZ3P+7yZCkNGy1UVSWpBj/Q3afUZYwMdlPryKi4jbbWR7isyHu8pe0cjr/rgBgItBdNTLGGeYLt88BKxSeZ2SqtPNdIUsQv6LS+pn31PQPQtRTgG4DLY6NjoFl3d9uk2JYswWTBvucjonsrWLickuEFM2AoLjoQieuelPyPuRV5qnzIf8FX+f65vmtv1Io6UYqwaYAEliKaTMFQ0DEz5WC0yrlfBBhrdBzw0nP7AZGrpU0iR89WoTRGstOChOUUH+QheqkrQu6LmDNntzZAGBdgoZ2IHn2EjuoahBUyvCgNS6fu3PQfi0bOiSRGRW8QxYBNdx0iwFigYO5okoL44Z4a7Dg3Ta/Z9IU7GQTDtKBse6sK+FROKo6XM8cGaRlLUozCFAV8E6bn0F7KpicKNi5SYofmUL1Kol7qEZM8wpRVeMFHshzSjumqSMUM5mbaC9dD7o/6N9WwgPePK5+cDt//vE9uGD9SKxPZkpvnziJWC7J9ErXfGyKOWOubHldC4F96OeM4y/4Zjd+cenUibCG5nvfyfz85pms1n+cAgMHfx98LtNiAhZJMN0JIo6gkFCCbZQ3oS6Wh29j/mKnyCNEaLWEENBfaJqxZ9EwjU/NPEKTYsu7mQzdv0pRg5gMum4fyyDbrVfdI5oO4T5+haQwZONrvnkzF6LI74rxxEKdOH7AKGmGBHCXf9lDW0hEsJFFPxPKfKw+HM0wJ3ha83h06ibQJLifwVe8IHyRA38l9hlszusYJRWQXKhTBBtgjLdHyYQdob8VwpdRFR/ yyhBydPG 4oEdzWNrBCnS3FgcbDxGGLCx/Xpe79gQiPx2kWboNAFMKWR1wW7mnWj0mmFddv6TGugdeuG7LD0l+fqG3pQeG7IAlZvyuDqxezRQGSHtl9J6Tz6bgfu3PJigIcQeXiSaxy7fz68mplvC7qwVYbW0sPVTsDEWOgpZLrZ1xHpRL5DiBX3nPDa/ObRhinap1JYRKTgR3Di7iurIIJIph6HJ4Ov4ecSuy5bZFTwdl0sKjuuW130FpreepvUNi4FnK7YM4L29hnNyTTT53zMxRvCrUjArzzyusiU/oL8TmWKJop15sF1iOlfMCfuM+WnnvKKhxZ7+5FjuXR3DXYiDGIuWTOLGVI2YeDMHsRwpk3Oli27eYtIr3SFQ1GGz9TZ1JpyUarGXds+I1ak+2TNy7EvML78/XuFQ5+WY7TcDv X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: Please Cc: also R: folks in MAINTAINERS, adding them now. On 9/6/23 10:18, David Laight wrote: > The typical use of kmalloc_size_roundup() is: > ptr = kmalloc(sz = kmalloc_size_roundup(size), ...); > if (!ptr) return -ENOMEM. > This means it is vitally important that the returned value isn't > less than the argument even if the argument is insane. > In particular if kmalloc_slab() fails or the value is above > (MAX_ULONG - PAGE_SIZE) zero is returned and kmalloc() will return > it's single zero-length buffer. > > Fix by returning the input size on error or if the size exceeds > a 'sanity' limit. > kmalloc() will then return NULL is the size really is too big. > > Signed-off-by: David Laight > Fixes: 05a940656e1eb ("slab: Introduce kmalloc_size_roundup()") > --- > The 'sanity limit' value doesn't really matter (even if too small) > It could be 'MAX_ORDER + PAGE_SHIFT' but one ppc64 has MAX_ORDER 16 > and I don't know if that also has large pages. Well we do have KMALLOC_MAX_SIZE, which is based on MAX_ORDER + PAGE_SHIFT (and no issues on ppc64 so I'd expect the combination of MAX_ORDER and PAGE_SHIFT should always be such that it doesn't overflow on the particular arch) so I think it would be the most straightforward to simply use that. > Maybe it could be 1ul << 30 on 64bit, but it really doesn't matter > if it is too big. > > The original patch also added kmalloc_size_roundup() to mm/slob.c > that can also round up a value to zero - but has since been removed. > > mm/slab_common.c | 29 ++++++++++++++--------------- > 1 file changed, 14 insertions(+), 15 deletions(-) > > diff --git a/mm/slab_common.c b/mm/slab_common.c > index cd71f9581e67..8418eccda8cf 100644 > --- a/mm/slab_common.c > +++ b/mm/slab_common.c > @@ -747,22 +747,21 @@ size_t kmalloc_size_roundup(size_t size) > { > struct kmem_cache *c; > > - /* Short-circuit the 0 size case. */ > - if (unlikely(size == 0)) > - return 0; > - /* Short-circuit saturated "too-large" case. */ > - if (unlikely(size == SIZE_MAX)) > - return SIZE_MAX; > - /* Above the smaller buckets, size is a multiple of page size. */ > - if (size > KMALLOC_MAX_CACHE_SIZE) > - return PAGE_SIZE << get_order(size); > + if (size && size <= KMALLOC_MAX_CACHE_SIZE) { I guess the whole test could all be likely(). Also this patch could probably be just replacing the SIZE_MAX test with >= KMALLOC_MAX_SIZE, but since the majority is expected to be 0 < size <= KMALLOC_MAX_CACHE_SIZE, your reordering makes sense to me. > + /* > + * The flags don't matter since size_index is common to all. > + * Neither does the caller for just getting ->object_size. > + */ > + c = kmalloc_slab(size, GFP_KERNEL, 0); > + return likely(c) ? c->object_size : size; > + } > > - /* > - * The flags don't matter since size_index is common to all. > - * Neither does the caller for just getting ->object_size. > - */ > - c = kmalloc_slab(size, GFP_KERNEL, 0); > - return c ? c->object_size : 0; > + /* Return 'size' for 0 and very large - kmalloc() may fail. */ > + if (unlikely((size - 1) >> (sizeof (long) == 8 ? 34 : 30))) So I'd just test for size == 0 || size > KMALLOC_MAX_SIZE? > + return size; > + > + /* Above the smaller buckets, size is a multiple of page size. */ > + return PAGE_SIZE << get_order(size); > } > EXPORT_SYMBOL(kmalloc_size_roundup); >