Received: by 2002:a5b:505:0:0:0:0:0 with SMTP id o5csp5080155ybp; Mon, 7 Oct 2019 19:38:35 -0700 (PDT) X-Google-Smtp-Source: APXvYqz6Zsuc6QDwap1l7NyhVZMpAeJuL3OljA46X+FIey+HItCJ8UrZxXUMqQhQHnjsnVoARrXx X-Received: by 2002:a50:d758:: with SMTP id i24mr31401820edj.246.1570502315862; Mon, 07 Oct 2019 19:38:35 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1570502315; cv=none; d=google.com; s=arc-20160816; b=gnTK1LRm9/4p0tVxtuhtCodQP0nGghlEDiNjGYUqm063TD7ZBSqLT8lUnUtVmKDJ7g 4UnepQzATRRbPzY5tcsBpSDfTGEBcgtnaQCi8Y1tqcCQgpRw9V90MsShR7HX1IeISjrS XRb2ZU1hbSrF7Uvidvv041xiZ33WrqduYwBcz25s72Jh5sbnG+X88344EqSBRZToSECk KwzwcGf6VRFFw5NiU8OH1dtPeITxxCmpOG5Tl/5b4MonGodIa8nhswm5L3MY5eDlxb/M z7ei+NSgPIn7Vkz5ebvS5OF/Lv7wfYk2NWv+ihupLuVg9lHmfKRI7wzO7sn0Pi0eFN93 wsag== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding :content-language:in-reply-to:mime-version:user-agent:date :message-id:from:references:cc:to:subject; bh=USC54LbybyPf5zAO5xYeox15ne1cAkahBmt5tDk6D40=; b=JYpdTWJGDqJLD57e1oK4/oJFRuIlh+YSdncnrYSBX1Tl/NGBMm5ucMuS0P08VD+FCL tFpCba622qPykvcQ2JSpCg5dSWt0+mvIgMdhZW1FaR1e8yy2owX33DgseRgLNCCpyxoX nIkFQdy7NL/C+m15sXd8jqIVce5iZpFrVw3mGn9y9gzKWqr5aC81cMCZDfEGZuD7Ek2L Xm2v6wVdHX8j4kO9ncdtX8twHgOR1Xv1idnB1cQbrd6Q5D70RwnoUBoaPB9xXrl3C3+w 0tg3H3sWzuFqOuDj4Nvcks+hcwUk24o5RgFOoqhc6tm+IrFtkIT76FzGizM5c6+aPCSS Lehw== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id gh12si7812249ejb.191.2019.10.07.19.38.12; Mon, 07 Oct 2019 19:38:35 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1729773AbfJHCeh (ORCPT + 99 others); Mon, 7 Oct 2019 22:34:37 -0400 Received: from szxga05-in.huawei.com ([45.249.212.191]:3266 "EHLO huawei.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1729212AbfJHCeh (ORCPT ); Mon, 7 Oct 2019 22:34:37 -0400 Received: from DGGEMS407-HUB.china.huawei.com (unknown [172.30.72.58]) by Forcepoint Email with ESMTP id 6D5EA265082CC7F488AD; Tue, 8 Oct 2019 10:34:35 +0800 (CST) Received: from [127.0.0.1] (10.177.251.225) by DGGEMS407-HUB.china.huawei.com (10.3.19.207) with Microsoft SMTP Server id 14.3.439.0; Tue, 8 Oct 2019 10:34:31 +0800 Subject: Re: [PATCH v2] arm64: armv8_deprecated: Checking return value for memory allocation To: Will Deacon CC: , , , , , , , References: <20191007153710.7xpx27kgeewz75kt@willie-the-truck> From: Yunfeng Ye Message-ID: Date: Tue, 8 Oct 2019 10:33:17 +0800 User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:60.0) Gecko/20100101 Thunderbird/60.6.1 MIME-Version: 1.0 In-Reply-To: <20191007153710.7xpx27kgeewz75kt@willie-the-truck> Content-Type: text/plain; charset="utf-8" Content-Language: en-US Content-Transfer-Encoding: 7bit X-Originating-IP: [10.177.251.225] X-CFilter-Loop: Reflected Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 2019/10/7 23:37, Will Deacon wrote: > On Mon, Oct 07, 2019 at 06:06:35PM +0800, Yunfeng Ye wrote: >> There are no return value checking when using kzalloc() and kcalloc() for >> memory allocation. so add it. >> >> Signed-off-by: Yunfeng Ye >> --- >> v1 -> v2: >> - return error code when memory allocation failure >> >> arch/arm64/kernel/armv8_deprecated.c | 57 +++++++++++++++++++++++++++--------- >> 1 file changed, 43 insertions(+), 14 deletions(-) >> >> diff --git a/arch/arm64/kernel/armv8_deprecated.c b/arch/arm64/kernel/armv8_deprecated.c >> index 2ec09de..2284fcb 100644 >> --- a/arch/arm64/kernel/armv8_deprecated.c >> +++ b/arch/arm64/kernel/armv8_deprecated.c >> @@ -168,12 +168,15 @@ static int update_insn_emulation_mode(struct insn_emulation *insn, >> return ret; >> } >> >> -static void __init register_insn_emulation(struct insn_emulation_ops *ops) >> +static int __init register_insn_emulation(struct insn_emulation_ops *ops) >> { >> unsigned long flags; >> struct insn_emulation *insn; >> >> insn = kzalloc(sizeof(*insn), GFP_KERNEL); >> + if (!insn) >> + return -ENOMEM; >> + >> insn->ops = ops; >> insn->min = INSN_UNDEF; >> >> @@ -197,6 +200,7 @@ static void __init register_insn_emulation(struct insn_emulation_ops *ops) >> >> /* Register any handlers if required */ >> update_insn_emulation_mode(insn, INSN_UNDEF); >> + return 0; >> } >> >> static int emulation_proc_handler(struct ctl_table *table, int write, >> @@ -224,7 +228,7 @@ static int emulation_proc_handler(struct ctl_table *table, int write, >> return ret; >> } >> >> -static void __init register_insn_emulation_sysctl(void) >> +static int __init register_insn_emulation_sysctl(void) >> { >> unsigned long flags; >> int i = 0; >> @@ -233,6 +237,8 @@ static void __init register_insn_emulation_sysctl(void) >> >> insns_sysctl = kcalloc(nr_insn_emulated + 1, sizeof(*sysctl), >> GFP_KERNEL); >> + if (!insns_sysctl) >> + return -ENOMEM; >> >> raw_spin_lock_irqsave(&insn_emulation_lock, flags); >> list_for_each_entry(insn, &insn_emulation, node) { >> @@ -251,6 +257,7 @@ static void __init register_insn_emulation_sysctl(void) >> raw_spin_unlock_irqrestore(&insn_emulation_lock, flags); >> >> register_sysctl("abi", insns_sysctl); >> + return 0; >> } >> >> /* >> @@ -617,25 +624,47 @@ static int t16_setend_handler(struct pt_regs *regs, u32 instr) >> */ >> static int __init armv8_deprecated_init(void) >> { >> - if (IS_ENABLED(CONFIG_SWP_EMULATION)) >> - register_insn_emulation(&swp_ops); >> + int ret = 0; >> + int err = 0; >> + >> + if (IS_ENABLED(CONFIG_SWP_EMULATION)) { >> + ret = register_insn_emulation(&swp_ops); >> + if (ret) { >> + pr_err("register insn emulation swp: fail\n"); >> + err = ret; >> + } >> + } > > Is there much point in continuing here? May as well just return ret, I > think. I also don't think you need to print anything, since kmalloc > should already have shouted. > The registration of each instruction simulation is independent. I think that one failure does not affect the registration of other instructions. In addition, if return directly, is it need to unregister? Of course, the first instruction registration can be directly returned, If the following instruction registration fails, is it need unregister operation? currently the unregistration of instruction simulation is not be implemented yet. The purpose of printing information is to replace the direct return, which can distinguish which instruction failed to register. There is no need to print information if it returns directly. thanks. >> - if (IS_ENABLED(CONFIG_CP15_BARRIER_EMULATION)) >> - register_insn_emulation(&cp15_barrier_ops); >> + if (IS_ENABLED(CONFIG_CP15_BARRIER_EMULATION)) { >> + ret = register_insn_emulation(&cp15_barrier_ops); >> + if (ret) { >> + pr_err("register insn emulation cpu15_barrier: fail\n"); >> + err = ret; >> + } >> + } >> >> if (IS_ENABLED(CONFIG_SETEND_EMULATION)) { >> - if(system_supports_mixed_endian_el0()) >> - register_insn_emulation(&setend_ops); >> - else >> + if (system_supports_mixed_endian_el0()) { >> + ret = register_insn_emulation(&setend_ops); >> + if (ret) { >> + pr_err("register insn emulation setend: fail\n"); >> + err = ret; >> + } >> + } else { >> pr_info("setend instruction emulation is not supported on this system\n"); >> + } >> } >> >> - cpuhp_setup_state_nocalls(CPUHP_AP_ARM64_ISNDEP_STARTING, >> - "arm64/isndep:starting", >> - run_all_insn_set_hw_mode, NULL); >> - register_insn_emulation_sysctl(); >> + if (nr_insn_emulated) { >> + cpuhp_setup_state_nocalls(CPUHP_AP_ARM64_ISNDEP_STARTING, >> + "arm64/isndep:starting", >> + run_all_insn_set_hw_mode, NULL); >> + ret = register_insn_emulation_sysctl(); >> + if (ret) >> + err = ret; >> + } > > I'm dubious about leaving the cpuhp notifier registered if we fail here. > Can we simply reorder the logic so that the notifier is registered after > successfully calling register_insn_emulation_sysctl()? thanks. > ok, I will reorder the logic. And the same question: is it need to unregister the instruction emulation if register_insn_emulation_sysctl() invoked fail? > Will > > . >