From patchwork Wed Jul 17 23:00:25 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Barry Song <21cnbao@gmail.com> X-Patchwork-Id: 13735876 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 2EDAFC3DA5D for ; Wed, 17 Jul 2024 23:00:48 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id A00A26B00A8; Wed, 17 Jul 2024 19:00:47 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 9898E6B00AC; Wed, 17 Jul 2024 19:00:47 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 802D16B00AD; Wed, 17 Jul 2024 19:00:47 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id 5FCBF6B00A8 for ; Wed, 17 Jul 2024 19:00:47 -0400 (EDT) Received: from smtpin17.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id F39F841847 for ; Wed, 17 Jul 2024 23:00:46 +0000 (UTC) X-FDA: 82350766092.17.343BE92 Received: from mail-pl1-f180.google.com (mail-pl1-f180.google.com [209.85.214.180]) by imf26.hostedemail.com (Postfix) with ESMTP id 20C33140003 for ; Wed, 17 Jul 2024 23:00:43 +0000 (UTC) Authentication-Results: imf26.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=J0Gyvt7n; spf=pass (imf26.hostedemail.com: domain of 21cnbao@gmail.com designates 209.85.214.180 as permitted sender) smtp.mailfrom=21cnbao@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1721257204; 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-transfer-encoding:content-transfer-encoding: in-reply-to:references:dkim-signature; bh=qOyBXX3xp/eykee8FMZ+6xca1O2sKIiZ6Y7CKqB17Rk=; b=MuUeILZo17tN4QaNuH4KkNfAu9vX5YelOCDCs5lfAvTFgUk/OQIi0uqYZtcjIASqCM9Xlq XF5dtYizFIXoHUi8DF6tnC0/7yvcHybF5kq3lQOQGfMW4qQWmMmoY3iKzuZk1a8oPUH7tq qnRYAnJxNte+pHMg6STJvdS7SLHBmY4= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1721257204; a=rsa-sha256; cv=none; b=W6x4BGsDRUNXXItQWMvnmI7y+bxoHKS1t1mql1EC+rm4VsrRKngvcDjg67gQKIi2+xQS26 7hFOM0wVBWdkyqOKrZ4mRWg6+Ox1T2g+VkSWwNfUKVo1sKWADefn+qYdPhsXGA+gkCmmq3 EZ4xGxJviWPKc1m7TQGE8MqjMdyJgXg= ARC-Authentication-Results: i=1; imf26.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=J0Gyvt7n; spf=pass (imf26.hostedemail.com: domain of 21cnbao@gmail.com designates 209.85.214.180 as permitted sender) smtp.mailfrom=21cnbao@gmail.com; dmarc=pass (policy=none) header.from=gmail.com Received: by mail-pl1-f180.google.com with SMTP id d9443c01a7336-1fb3b7d0d56so1712995ad.1 for ; Wed, 17 Jul 2024 16:00:43 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1721257243; x=1721862043; darn=kvack.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=qOyBXX3xp/eykee8FMZ+6xca1O2sKIiZ6Y7CKqB17Rk=; b=J0Gyvt7nfxcej10XGHh6lnRtSPJP4NKjsvm1Yj7hHW7qzhyEND6pdxOGOl75CO8Sa1 wxeawYmSzyU3xPZeaPvvxPmYTwmBFk7l7wgkZNkC6A53GnZn78r1Z9X489ZnluL0N+Tg tTvvF1Nu+KECpZA4G0nZWwpK6j4j7+Nn7jj8IR4MqXimKqkqxuscvnm8GK0XpQ6RZdRY WaU/upr5s2wDxj/L/T0DCm9nbdidikohOVDFm++/qCacDNkN0keDdiOMkTX0RIC0OLgz EFPMJm1xVlAhr3Lv4Njhh6mGl9lUxvDH+F/RAXjxxuXTnNYOeB6/peuPxrVOII9a1pDj qh+Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1721257243; x=1721862043; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=qOyBXX3xp/eykee8FMZ+6xca1O2sKIiZ6Y7CKqB17Rk=; b=W5jF9d3TIqJ4F55GsSJG1NTi60BOpq+pwsy/yBECGSciJuMqLo6qu2ZVxo921zqHgJ NgP2mkHJTsS0xSO5DONyqZjJ6aQgrpsjlLDU/gtUG1WA3R1xk/J3BtYooSZqH6MANfnm fu2G1mVwphney558O4XL2s7F3F8jX2qhh8Yee+khVIHxmTuoay+1Nd/XRNGyF6vy1Vm1 CraDGUcboOjE+EKr1xdujrlGbX8I56n/f7/qqUKXQBqfPnM51YjdgnqIHlci1G/6uhXi /JhznuspFKAMcR7MNs4fg8OFCd3LABoR3refgqgURVRVb20MEpdEx830MOIJ2eSvag35 jkEA== X-Forwarded-Encrypted: i=1; AJvYcCUr+NHXaa9xKspXkU1eVSSmCnegRoXO8SnKkjCjGhxcVI1NajyYnjG9rw6DSy0qvgbcoxr98qJ0rncKzKoW4apQHlc= X-Gm-Message-State: AOJu0Yye3UGuO1RF7sFL0aljMIiYBBbvVW29ug8MP6iEtIF9E0jCGRYu ZnnKcv5mVU9CnwigAFZdpOnakt+qvC1l3jVVbjZ/WcEnd2G0mCdm X-Google-Smtp-Source: AGHT+IGXPi1gP+hJkreQSeagO4vLotljQIytAs5vpJrg1mjj4Emk9wUjqQkrLRsJGYRI+7sWth7nmQ== X-Received: by 2002:a17:902:d483:b0:1fb:a2bb:1688 with SMTP id d9443c01a7336-1fc4e154f36mr26278085ad.38.1721257242192; Wed, 17 Jul 2024 16:00:42 -0700 (PDT) Received: from localhost.localdomain ([2407:7000:8942:5500:aaa1:59ff:fe57:eb97]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-1fc5ef1c53bsm1282685ad.124.2024.07.17.16.00.37 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 17 Jul 2024 16:00:41 -0700 (PDT) From: Barry Song <21cnbao@gmail.com> To: akpm@linux-foundation.org, linux-mm@kvack.org Cc: Barry Song , Michal Hocko , Uladzislau Rezki , Christoph Hellwig , Lorenzo Stoakes , Christoph Lameter , Pekka Enberg , David Rientjes , Joonsoo Kim , Vlastimil Babka , Roman Gushchin , Hyeonggon Yoo <42.hyeyoo@gmail.com> Subject: [PATCH RFC] mm: warn potential return NULL for kmalloc_array and kvmalloc_array with __GFP_NOFAIL Date: Thu, 18 Jul 2024 11:00:25 +1200 Message-Id: <20240717230025.77361-1-21cnbao@gmail.com> X-Mailer: git-send-email 2.34.1 MIME-Version: 1.0 X-Rspam-User: X-Rspamd-Server: rspam04 X-Rspamd-Queue-Id: 20C33140003 X-Stat-Signature: htih4oiwkkwjdtine1qisbwdzpxzj79k X-HE-Tag: 1721257243-313888 X-HE-Meta: U2FsdGVkX1+kVFSfj6h+C7Z/Hk1EiWze/Akp/ouG11ldTIEk6dDz4cV13qY2W/Ex4llxMx056MF6UvqPV9ulTMxbLZXI1HnPI2DyX0s0Bn1mqItrIyUGOTQn2jEkQxzlmBSFO8WSR35XesESmWkkXiomBThfj7e8QYWolkM9iUVMtJ1tMjYx5t4jGz90abjcrdqoVwIolUhgrIuFQy0rOVzxOaSD60QvNKk4Qj9RiLm+iP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LX/J9WRCRWqwZiVQryyMcEg/F4GaX238nIW9PvZWgtNOxBUBDU33+5jkj7tSDQz0VQiXFyLKY3sRCBxTNZ5KgUL49qDEfkmVvwWLZjCehDP1o0rfQ1Z7/d5QcNOXZEcUtEpX7F0Mcqap0t+dgndXIEQXfRneRz0prF7owsg43v3fFas47eJWYMYP5XbAU2V8Jh68Gxdmcfg6gCj4+aTPbpQZK6e/M//pTa4fKvmEV0dvwqVD1lJI7sM//DJPRkH5TPITBpYAoQ0YdL9jtY3EdoKZokmJtvszBYMDqOUse3zEwiIwKz1CjBEJOH5ijajku0TcHpmv4lIWX8jyRItQmt9VvIKviVLt/4a4jCmC/Y+IzvK8JI22sICRt2xheJWLJqjKvtWMhoVFGg8DHzNS1phpomJbIwvfa/8rYRbBt4Sjv6yaLZZToZMUK2Di5mAe5VhEOl7sd3q+ydlGh0XdtjsBSFPPX8Q8lMEV2wmuK8tnxEMlUglu3phoYj9aVibxZG7RJyg1qS9Mp6yjHdbQNJL2lfcf8ffOvUC2SsqG 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: List-Subscribe: List-Unsubscribe: From: Barry Song Overflow in this context is highly unlikely. However, allocations using GFP_NOFAIL are guaranteed to succeed, so checking the return value is unnecessary. One option to fix this is allowing memory allocation with an overflowed size, but it seems pointless. Let's at least issue a warning. Likely BUG_ON() seems better as anyway we can't fix it? Cc: Michal Hocko Cc: Uladzislau Rezki (Sony) Cc: Christoph Hellwig Cc: Lorenzo Stoakes Cc: Christoph Lameter Cc: Pekka Enberg Cc: David Rientjes Cc: Joonsoo Kim Cc: Vlastimil Babka Cc: Roman Gushchin Cc: Hyeonggon Yoo <42.hyeyoo@gmail.com> Signed-off-by: Barry Song --- include/linux/slab.h | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/include/linux/slab.h b/include/linux/slab.h index a332dd2fa6cd..c6aec311864f 100644 --- a/include/linux/slab.h +++ b/include/linux/slab.h @@ -692,8 +692,10 @@ static inline __alloc_size(1, 2) void *kmalloc_array_noprof(size_t n, size_t siz { size_t bytes; - if (unlikely(check_mul_overflow(n, size, &bytes))) + if (unlikely(check_mul_overflow(n, size, &bytes))) { + WARN_ON(flags & __GFP_NOFAIL); return NULL; + } if (__builtin_constant_p(n) && __builtin_constant_p(size)) return kmalloc_noprof(bytes, flags); return kmalloc_noprof(bytes, flags); @@ -794,8 +796,10 @@ kvmalloc_array_node_noprof(size_t n, size_t size, gfp_t flags, int node) { size_t bytes; - if (unlikely(check_mul_overflow(n, size, &bytes))) + if (unlikely(check_mul_overflow(n, size, &bytes))) { + WARN_ON(flags & __GFP_NOFAIL); return NULL; + } return kvmalloc_node_noprof(bytes, flags, node); }