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 bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 39A58C6FD1F for ; Wed, 20 Mar 2024 18:48:52 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:Content-Type: Content-Transfer-Encoding:List-Subscribe:List-Help:List-Post:List-Archive: List-Unsubscribe:List-Id:In-Reply-To:References:Subject:To:From:MIME-Version: Date:Message-ID:Reply-To:Cc:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=KLLPyMhAwY5saYTh5pjMzXa2ih2g2BLPV44+cx3b82c=; b=ny3NU0r1EOT+iKG2RP0wj8AQUx EpQVU7P2cOdfTZytujCm9hKNi7nMz9vBczpn5xEphx1DAWL9EWAPL9DArwXpZ2xCLaBtLSqYf6NVy SVM8Mfu9fuHlhWh/ChmPA6X6Ae09+FnECcM0W/fWjv2X3J5288S5VvjB2cjQHQr1yRjSKjwz+u4M1 uz7uBwdTRHIV2owvzNuhlXBRsrcWqsIctjcT3loFe22cOp9eAKlWE1h+kz8bwi6bOd6CJRxgIGYWb 40pgeGAUfsrCa1t/6o+Imk8zgXwupmE3/ac7zTHfxqAzcCwUaAmHqJ1DipTxg92k+jeorEF8bJ8+6 daOuPmgg==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1rn0zI-00000000gFf-1q5K; Wed, 20 Mar 2024 18:48:32 +0000 Received: from mail-qk1-x730.google.com ([2607:f8b0:4864:20::730]) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1rn0zE-00000000gDm-2MHM for linux-arm-kernel@lists.infradead.org; Wed, 20 Mar 2024 18:48:30 +0000 Received: by mail-qk1-x730.google.com with SMTP id af79cd13be357-78a2290b48eso12899185a.3 for ; Wed, 20 Mar 2024 11:48:27 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1710960507; x=1711565307; darn=lists.infradead.org; h=content-transfer-encoding:in-reply-to:references:subject:to:from :user-agent:mime-version:date:message-id:from:to:cc:subject:date :message-id:reply-to; bh=I0gaq5RcKTDZI49zfun+o6WuwUPWgQ8VI+wKMO9QTpk=; b=LZBiLnTYca4FaR9yo+br3S75cE5wFzsqUbyGr2/ku/jJQdh3YE3GVkz//i+MekheNl lRFiCHXUyqLVWRXxn8OxUkn22GmjELk74Qy3XQnkpryGvZQzJ6fRb+wk52jMhycb/dc8 Q2YujJZmu2kbKBjhcK6PksSWoq1aVHRex560hDiKGflNFjdBFWFDPg4Ynk4p+aOXWCa1 4hZmFDDCvCJGA92FByt7HhyKU2OA3t9y4toGrgYQTc5qEH0RbLSqRh3YDAogEJAwiZjn NRsfxgrm5pL7fDnOZ6+F0OHXl3TAtG7IgGUtmot9ExKBTSMJVEaPaODJJUUl4g+mmasl yzAw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1710960507; x=1711565307; h=content-transfer-encoding:in-reply-to:references:subject:to:from :user-agent:mime-version:date:message-id:x-gm-message-state:from:to :cc:subject:date:message-id:reply-to; bh=I0gaq5RcKTDZI49zfun+o6WuwUPWgQ8VI+wKMO9QTpk=; b=nHKKXJeA/DgUsj5OEVlTj4EYFBMtDdcV+cWtaF0Z/4EzahCmqlSoeEDoSIevvcklCa /cj27GZ7HI6GnKx9CM5a90pEXxIJ39vC6EYcor77SdfKKFBAvF3YdLwkAmjc5cXpMC/x nQmCg9SXT+6bl4Buxw4c12TOdgjhlmfK4sFofD4cnFp1iTMikRmHiD9bKjOnHF/eigNd c5IIn2MCGuDhl89peNfzGSAOV4ZoBC/5nrdspsFeo5Nw6QTSaIIw+UG0mV+yWFOg1cSC Sm5QwPClSSSmVCoCZ76nKPkyJAj76g/ME7hcIevl2Vjgd8xVNroWYE20cLMltG/8rX9A T1sg== X-Forwarded-Encrypted: i=1; AJvYcCUbNkuAssQn8XycRSAIs7zjWqx65qs50ziuvAiS5VIdaQnlSoxsaPzPU0OC9DPjGUKtmON5YEOuNjPnrhaRYyYKaLx1lLDmn52gyEQ0d8RZlA2VBkM= X-Gm-Message-State: AOJu0Yyac6aAzpXEBSMCZ58FfncGSRblV89pCI5ygKFWFTx1X7+v2ZvB 0amS/UyC/zsO9DMJPGmwj8CDDkh/f7ABRLERgtdu8Pg8PQysQS/x X-Google-Smtp-Source: AGHT+IG+PSfJSw9eVaTglP5VuVX2HeFWvXZaCx3BJeKNEFSzGlcs6Tm5IV6diCMUFZJSCVNNodbY9w== X-Received: by 2002:a05:620a:950:b0:788:267c:8a1f with SMTP id w16-20020a05620a095000b00788267c8a1fmr18927855qkw.58.1710960507103; Wed, 20 Mar 2024 11:48:27 -0700 (PDT) Received: from [10.193.190.170] (mobile-130-126-255-72.near.illinois.edu. [130.126.255.72]) by smtp.gmail.com with ESMTPSA id p1-20020ae9f301000000b0078838c7acbfsm6835609qkg.42.2024.03.20.11.48.26 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Wed, 20 Mar 2024 11:48:26 -0700 (PDT) Message-ID: <2baa9e59-fe81-4c92-b6ca-a86233c5e06d@gmail.com> Date: Wed, 20 Mar 2024 13:48:26 -0500 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird From: zzjas98@gmail.com To: Andrew Lunn , gregory.clement@bootlin.com, sebastian.hesselbarth@gmail.com, linux@armlinux.org.uk, linux-arm-kernel@lists.infradead.org, chenyuan0y@gmail.com Subject: Re: [arch/arm/mach-mvebu] Question about kzalloc NULL check in i2c_quirk References: <1e40d5f4-72ac-40ca-8fb0-34884e7ac549@gmail.com> In-Reply-To: X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240320_114828_743916_0FCB776E X-CRM114-Status: GOOD ( 19.67 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset="us-ascii"; Format="flowed" Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org On 3/20/24 7:27 AM, Andrew Lunn wrote: > On Tue, Mar 19, 2024 at 10:03:22PM -0500, Zijie Zhao wrote: > > Dear ARM/Marvell maintainers, > > > > We came across an unusual usage of kzalloc in > > arch/arm/mach-mvebu/board-v7.c, function i2c_quirk: > > > > https://elixir.bootlin.com/linux/v6.8/source/arch/arm/mach-mvebu/board-v7.c#L127 > > ``` > > static void __init i2c_quirk(void) > > { > > struct device_node *np; > > u32 dev, rev; > > > > /* > > * Only revisons more recent than A0 support the offload > > * mechanism. We can exit only if we are sure that we can > > * get the SoC revision and it is more recent than A0. > > */ > > if (mvebu_get_soc_id(&dev, &rev) == 0 && rev > MV78XX0_A0_REV) > > return; > > > > for_each_compatible_node(np, NULL, "marvell,mv78230-i2c") { > > struct property *new_compat; > > > > new_compat = kzalloc(sizeof(*new_compat), GFP_KERNEL); > > > > new_compat->name = kstrdup("compatible", GFP_KERNEL); > > new_compat->length = sizeof("marvell,mv78230-a0-i2c"); > > new_compat->value = kstrdup("marvell,mv78230-a0-i2c", > > GFP_KERNEL); > > > > of_update_property(np, new_compat); > > } > > } > > ``` > > > > Should the new_compat be checked against NULL in case kzalloc fails, to > > avoid NULL dereference later in the code? > > > > Please kindly let us know if we missed any key information and this is > > actually intended. We appreciate your information and time! Thanks! > > What context is this code run in? What would need to happen for the > allocation to fail? And what happens next if it does fail, both in > this function and the system in general? > > Andrew > Hi Andrew, Thanks for checking! We encountered this kzalloc while doing a static analysis for the kernel code. kzalloc would return NULL in case of out-of-memory and would make the next field access new_compat->name segfault. However, we are not sure if i2c_quirk, used in an init_machine hook, has any special assumption, so would appreciate your knowledge to decide whether an NULL check is needed. Best, Zijie _______________________________________________ linux-arm-kernel mailing list linux-arm-kernel@lists.infradead.org http://lists.infradead.org/mailman/listinfo/linux-arm-kernel