From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-wr1-f41.google.com (mail-wr1-f41.google.com [209.85.221.41]) (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 E682736D510 for ; Mon, 11 May 2026 11:59:12 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.221.41 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1778500754; cv=none; b=o/Tnjd421it9UX/UOP4nip7ikivtlUWqqGPvZbQGtcAUTfmFyoxZhS/wtsAVGFHov06zcJXF7SWP6Yowl7VAjKxdtgbzx4Ruf83s+66xPIDWdjjp2EVsjMs/E992yZ2qCNocd8RJ6sm9poTFUI1O+itg4R1o6DPZ1pCkyuxmSg0= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1778500754; c=relaxed/simple; bh=E/TJvFs4t4lBs160Q7ddNOnGS+1yrt+GmYwsY8ieCxk=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=tl3cVSIWOR+ncgKY9EVTrr7c9bJBqKr21N0wmx51aE2n5jbMPg2+JmEM6JO1fc7fxlZ0qGL6BuKT8/KVJUUGlbYwICim5iGMuZrNK+p85TObdTMwVr+TxP4zOdJikCQI1i/4Z0BQ3eIGGwBB8c2FtXFd0D+M7B6tlDFj/dsjzL4= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=crVZvZQp; arc=none smtp.client-ip=209.85.221.41 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="crVZvZQp" Received: by mail-wr1-f41.google.com with SMTP id ffacd0b85a97d-43fe608cb92so2621689f8f.2 for ; Mon, 11 May 2026 04:59:12 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1778500751; x=1779105551; darn=vger.kernel.org; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:from:to:cc:subject:date:message-id:reply-to; bh=btlqUdi0qGhMKZ3fIin3gDvxyyqT63yde/F745AE8nE=; b=crVZvZQpDtP2iYrYETO5PJ5Zteho20POdFeIfBtnrH/7TGw21YcyeLQq8AzeyTJGhh /nvqZXJ1gcsV06WBca+TVHOIK+yLbtS5TK6T8NBCeWAfRrUQN6hww4WxWQ/kXNZJq05X 9sqW0FfCqDD/TwHGq6pXTHnYit95O1g+pRuLKbOZbnRWSm1DR9a7cndIn4BrlumGoc1t rIW/DMlWreE7P8fcZ6lSpJeiOeFHVEqaseNaraKpYWj7ByqBMtGqe2ucX4NOqUmqFeQb VLeeQs+s8pSmZM0/7LjtRcRE5YIEdZV2HqMwnflNxHs6k+wOXj4eShGIsj1J0zQML7nb 4HUA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1778500751; x=1779105551; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:x-gm-gg:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=btlqUdi0qGhMKZ3fIin3gDvxyyqT63yde/F745AE8nE=; b=Xoj9fpLPh9sO5v1/FHPTu7NEExsH/XK/zJcv8WVlJulPnOtvvIsxgfQ1AsUVgUO3Gd 0WTqDcawcEArW4eV01Bx2Sqn9XjqPYkLK6eR2qghIbJmnLfDtz4Z/P1rq0WE3wrroiUh IxZgVC72PAIDpX/lZhYpTMoOInSLwgeFORNZLhg4kg2NsZcfwRP1QU6tHZDaNdpT1EXU KmeaoTyuS3riCk3Q2uEhdXOyBhoGDzPufQrNCOktRVPdhRzk0ImzBQogzwfJlZfIOlfG 2+4clQd5boHetNFOx+b/dN7Gw3pzPwsTXTJNtGNX07wgo2q+q+laleZFfCTcqdlajSid auGQ== X-Forwarded-Encrypted: i=1; AFNElJ/DPORMOmIpJsS38paZZj1zCzM1u3SBArYH8YzzCr2fVXDUW6QR9T2OS2oLfZC9oAnmzpIUb6lCCRstJSk=@vger.kernel.org X-Gm-Message-State: AOJu0YweG8LBu0P+kT1+Nq6vktmWcK+VtygQUK7lbMNhiWAzx/poTtkd tNyS2kFcKN9DAiMZRKBfAxEqY45mUcpmIJJRdxUSadxlp928Ryf4Bxet X-Gm-Gg: Acq92OEaYm5nIT95k4CV0BYFkKDzVj9CGlCytKvXl826MxJjxUgVphAs+JeeyHnVXA1 eqhOzvlUFEgW/2WC7PhxSBpB6Rzce2Sd2czRd+dkVB0xgAjL2TayleICMIszeWxS1qBgM/LKV6m 7n4RZdyLH+GrAN7WtNf1yBOBrDp8VN40Nl3wfiybX9IFyZYQk+7l6VJh+Oi6uW7nawIHTvpVykV bX9Ab+ZA63AAoLjuNjXGdd7NZYBJgY5O/2mRNyTAzIb+TMb+tjB38ez3bd6sv5XVbBrLkehuGFD T3P0/xVycjsK7GlJTkrhMQJkSfK7Qythz1ROSYhQ8+SMuWkhRE66abFxpSbWzLdBbhqIzWiyZ8b pmUMjC4tQCShngkZpaqfHivmrYikcKrIH69Qv24pRgmL7xDrKutgstPwYbh8aauKMIJFb5mchF2 qTztXlh7zYHmr7bOPsbSiGhliXVHgyCLmXhQboSVh6GYjMxCEGCmx5JjyId1sCs6tLk4Wcskrhu jDhWcPYA71g X-Received: by 2002:a5d:588e:0:b0:43d:184:8aa2 with SMTP id ffacd0b85a97d-4546140b235mr21067301f8f.16.1778500750944; Mon, 11 May 2026 04:59:10 -0700 (PDT) Received: from fedora ([185.193.234.8]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-45491bae13csm24580060f8f.29.2026.05.11.04.59.09 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 11 May 2026 04:59:10 -0700 (PDT) Date: Mon, 11 May 2026 12:59:08 +0100 From: Vishal Moola To: Hongfu Li Cc: hannes@cmpxchg.org, mhocko@kernel.org, roman.gushchin@linux.dev, shakeel.butt@linux.dev, muchun.song@linux.dev, tj@kernel.org, mkoutny@suse.com, shuah@kernel.org, cgroups@vger.kernel.org, linux-mm@kvack.org, linux-kselftest@vger.kernel.org, linux-kernel@vger.kernel.org Subject: Re: [PATCH] selftests/cgroup: check malloc return value in alloc_anon functions Message-ID: References: <20260511021615.1768623-1-lihongfu@kylinos.cn> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20260511021615.1768623-1-lihongfu@kylinos.cn> On Mon, May 11, 2026 at 10:16:15AM +0800, Hongfu Li wrote: > The alloc_anon() function calls malloc() without checking for a NULL > return. If memory allocation fails, a NULL pointer dereference will > occur when accessing the buffer. > > Add proper error handling to return -1 when malloc() fails in all > four alloc_anon variants: > - alloc_anon() Just a nit, It looks like the below already have proper error handling. > - alloc_anon_50M_check() > - alloc_anon_noexit() > - alloc_anon_50M_check_swap() > > Signed-off-by: Hongfu Li > --- > tools/testing/selftests/cgroup/test_memcontrol.c | 5 +++++ > 1 file changed, 5 insertions(+) > > diff --git a/tools/testing/selftests/cgroup/test_memcontrol.c b/tools/testing/selftests/cgroup/test_memcontrol.c > index b43da9bc20c4..8ef9c99a82eb 100644 > --- a/tools/testing/selftests/cgroup/test_memcontrol.c > +++ b/tools/testing/selftests/cgroup/test_memcontrol.c > @@ -61,6 +61,11 @@ int alloc_anon(const char *cgroup, void *arg) > char *buf, *ptr; > > buf = malloc(size); > + if (buf == NULL) { > + fprintf(stderr, "malloc() failed\n"); > + return -1; > + } > + > for (ptr = buf; ptr < buf + size; ptr += PAGE_SIZE) > *ptr = 0; Every malloc() call in this file has this same pattern. Maybe we'd be better off making it a helper function? Either way: Reviewed-by: Vishal Moola