Message ID | 20240117090018.152031-1-chentao@kylinos.cn |
---|---|
State | New |
Headers |
Return-Path: <linux-kernel+bounces-28691-ouuuleilei=gmail.com@vger.kernel.org> Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:42cf:b0:101:a8e8:374 with SMTP id q15csp782642dye; Wed, 17 Jan 2024 01:06:31 -0800 (PST) X-Google-Smtp-Source: AGHT+IHetVCxZG5s9I9RphC0PSxw3+LxM4VMSJseDLGC+z7fxyA3lBnN73+HhRb2H4KjkUxz8sKh X-Received: by 2002:a37:c247:0:b0:783:6387:3cb8 with SMTP id j7-20020a37c247000000b0078363873cb8mr3898637qkm.20.1705482391046; Wed, 17 Jan 2024 01:06:31 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1705482391; cv=pass; d=google.com; s=arc-20160816; b=V4Si4pDkjhxy3zLYgmFCp7Ng1rvaSDOFJgsCD8M+zlCJMN37xhuvW/0uPN9asvwh70 d2g3NX5oGp7Lfvtxd7fe4X6AnjY5ugeFq+qik0gCP+RTulph2aUQYsW3n/mxgwZrSQ8P EnaWQROMlmrU1qjFTNbBUBdB8K6fn5HvIco0BqSYAfTZ53Z+v6bNYnes8nrypgWOuEfu RNl7wrtG3y8T9Bq/hLcfbMyMU410F8hpzjropp6y8ucVnZ5FmwdiUc/Kh9TZCDCb7iCe j0PpAzOTzZyYYMv99U2elkVFOJ2yxuw9j94tpghgw542BACCJINKZvJW6GrHhlhHENol 70sg== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:message-id:date:subject:cc:to :from; bh=mKJLz487dGefKjHN9mdYi80p8dw5MAok4iO0oT5wbBQ=; fh=ZLeW4kAuKuUOBVqOFgJ7kCB3otCth9EGTGDoD52aOvw=; b=uSWSel9NIGs0wMDgNZhqWLE3RzlERqu+9FAlaqVCuRSWYgL2YLEHuGdslK1creeZQZ q/fmxFpd6eFFHmuzDNSpt0fFbGeow03DWOmFT0U0IZanLuFsaBKyLo0n4QCYdoDPtVZv i6VGesnU6B4QTo5MaDDS16oMxlQTRJ5O1V7Oy2u/Ixkpxu19B8HjjpDnBAno7rVJGAiP CxwljvDqO62NUZHvsbYbrUcAzipZv76XzRaNOOdRkXYnPTrZDi2pPDau1DIRZbt2NOsa Vmvkz5cQHR/W01Kj36KDg0C1kwhnAXjcHsYu+cY8LBnuDPpcOYuZfNK4Ywyz7ZBcMbAM L5jw== ARC-Authentication-Results: i=2; mx.google.com; arc=pass (i=1 spf=pass spfdomain=kylinos.cn); spf=pass (google.com: domain of linux-kernel+bounces-28691-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-28691-ouuuleilei=gmail.com@vger.kernel.org" Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [147.75.199.223]) by mx.google.com with ESMTPS id a2-20020a05620a102200b00783070846e9si11122033qkk.385.2024.01.17.01.06.30 for <ouuuleilei@gmail.com> (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 17 Jan 2024 01:06:31 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-28691-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) client-ip=147.75.199.223; Authentication-Results: mx.google.com; arc=pass (i=1 spf=pass spfdomain=kylinos.cn); spf=pass (google.com: domain of linux-kernel+bounces-28691-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-28691-ouuuleilei=gmail.com@vger.kernel.org" Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ny.mirrors.kernel.org (Postfix) with ESMTPS id D2C971C248E7 for <ouuuleilei@gmail.com>; Wed, 17 Jan 2024 09:06:30 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id F2C161427E; Wed, 17 Jan 2024 09:06:02 +0000 (UTC) Received: from mailgw.kylinos.cn (mailgw.kylinos.cn [124.126.103.232]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 37CB5134BC for <linux-kernel@vger.kernel.org>; Wed, 17 Jan 2024 09:05:56 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=124.126.103.232 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1705482361; cv=none; b=ol23XUDvMAndUl0EUQRvYi2BnVQon0Z6ecl8E08n1sOuBAYNRtE96aJnmblOoCcHyC13qgET54vFszSEYfSqiGTY4qz8Ox0FYHXXj1DdBxod/C8OgKN4ckCeyL8MVWo9o5LX2092BTi0lYmp5gv6E3el1l24o+PuevR17C4w/PA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1705482361; c=relaxed/simple; bh=qwmCZTEQzfIJoPmH2a9jsKkXWTPb3LIj72q2fNLHiZI=; h=X-UUID:X-CID-P-RULE:X-CID-O-INFO:X-CID-INFO:X-CID-META:X-CID-BVR: X-CID-BAS:X-CID-FACTOR:X-UUID:Received:Received:X-ns-mid:Received: From:To:Cc:Subject:Date:Message-Id:X-Mailer:MIME-Version: Content-Transfer-Encoding; b=rkuWeV9n1xa6o1vN74kwZNZyuOhNicCqq3Ra0IEGazvytKuoSdOwCSl5TZ0KQ6m6Vo9JVOqluE/v50Fl2fjTJJYDDeyuqXuW4t2SSD+2i5pM9WBi1koOz4fF9mFrM7VFL3U+P//qEn4DRGiCELW+hxP7HQ14r8QC5OGgZXDeGQU= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=kylinos.cn; spf=pass smtp.mailfrom=kylinos.cn; arc=none smtp.client-ip=124.126.103.232 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=kylinos.cn Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=kylinos.cn X-UUID: ee76e0f9ed9346d98d9002f4e6f06742-20240117 X-CID-P-RULE: Release_Ham X-CID-O-INFO: VERSION:1.1.35,REQID:60c887b3-2dea-43c2-9bf1-1657082d5671,IP:10, URL:0,TC:0,Content:-25,EDM:0,RT:0,SF:-3,FILE:0,BULK:0,RULE:Release_Ham,ACT ION:release,TS:-18 X-CID-INFO: VERSION:1.1.35,REQID:60c887b3-2dea-43c2-9bf1-1657082d5671,IP:10,UR L:0,TC:0,Content:-25,EDM:0,RT:0,SF:-3,FILE:0,BULK:0,RULE:Release_Ham,ACTIO N:release,TS:-18 X-CID-META: VersionHash:5d391d7,CLOUDID:16dc588e-e2c0-40b0-a8fe-7c7e47299109,B ulkID:2401171700272YDODMF1,BulkQuantity:0,Recheck:0,SF:19|44|101|66|38|24| 100|17|102,TC:nil,Content:0,EDM:-3,IP:-2,URL:11|1,File:nil,Bulk:nil,QS:nil ,BEC:nil,COL:0,OSI:0,OSA:0,AV:0,LES:1,SPR:NO,DKR:0,DKP:0,BRR:0,BRE:0 X-CID-BVR: 0 X-CID-BAS: 0,_,0,_ X-CID-FACTOR: TF_CID_SPAM_SNR,TF_CID_SPAM_FAS,TF_CID_SPAM_FSD,TF_CID_SPAM_FSI, TF_CID_SPAM_ULN X-UUID: ee76e0f9ed9346d98d9002f4e6f06742-20240117 Received: from mail.kylinos.cn [(39.156.73.10)] by mailgw (envelope-from <chentao@kylinos.cn>) (Generic MTA) with ESMTP id 819164830; Wed, 17 Jan 2024 17:00:25 +0800 Received: from mail.kylinos.cn (localhost [127.0.0.1]) by mail.kylinos.cn (NSMail) with SMTP id D3324E000EB9; Wed, 17 Jan 2024 17:00:24 +0800 (CST) X-ns-mid: postfix-65A79728-755627552 Received: from kernel.. (unknown [172.20.15.234]) by mail.kylinos.cn (NSMail) with ESMTPA id 67E86E000EB9; Wed, 17 Jan 2024 17:00:20 +0800 (CST) From: Kunwu Chan <chentao@kylinos.cn> To: jgross@suse.com, boris.ostrovsky@oracle.com, tglx@linutronix.de, mingo@redhat.com, bp@alien8.de, dave.hansen@linux.intel.com, x86@kernel.org, hpa@zytor.com Cc: xen-devel@lists.xenproject.org, linux-kernel@vger.kernel.org, Kunwu Chan <chentao@kylinos.cn>, kernel test robot <lkp@intel.com> Subject: [PATCH v2] x86/xen: Add some null pointer checking to smp.c Date: Wed, 17 Jan 2024 17:00:18 +0800 Message-Id: <20240117090018.152031-1-chentao@kylinos.cn> X-Mailer: git-send-email 2.39.2 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: <linux-kernel.vger.kernel.org> List-Subscribe: <mailto:linux-kernel+subscribe@vger.kernel.org> List-Unsubscribe: <mailto:linux-kernel+unsubscribe@vger.kernel.org> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1788327903255457582 X-GMAIL-MSGID: 1788327903255457582 |
Series |
[v2] x86/xen: Add some null pointer checking to smp.c
|
|
Commit Message
Kunwu Chan
Jan. 17, 2024, 9 a.m. UTC
kasprintf() returns a pointer to dynamically allocated memory
which can be NULL upon failure. Ensure the allocation was successful
by checking the pointer validity.
Signed-off-by: Kunwu Chan <chentao@kylinos.cn>
Reported-by: kernel test robot <lkp@intel.com>
Closes: https://lore.kernel.org/oe-kbuild-all/202401161119.iof6BQsf-lkp@intel.com/
---
v2: Initial rc and return errno in error paths
---
arch/x86/xen/smp.c | 18 +++++++++++++++++-
1 file changed, 17 insertions(+), 1 deletion(-)
Comments
> kasprintf() returns a pointer to dynamically allocated memory > which can be NULL upon failure. Ensure the allocation was successful > by checking the pointer validity. … > +++ b/arch/x86/xen/smp.c > @@ -61,10 +61,14 @@ void xen_smp_intr_free(unsigned int cpu) > > int xen_smp_intr_init(unsigned int cpu) > { > - int rc; > + int rc = 0; I find the indication of a successful function execution sufficient by the statement “return 0;” at the end. How do you think about to omit such an extra variable initialisation? > char *resched_name, *callfunc_name, *debug_name; > > resched_name = kasprintf(GFP_KERNEL, "resched%d", cpu); > + if (!resched_name) { > + rc = -ENOMEM; > + goto fail; > + } > per_cpu(xen_resched_irq, cpu).name = resched_name; > rc = bind_ipi_to_irqhandler(XEN_RESCHEDULE_VECTOR, > cpu, You propose to apply the same error code in four if branches. I suggest to avoid the specification of duplicate assignment statements for this purpose. How do you think about to use another label like “e_nomem”? Regards, Markus
On 2024/1/17 18:40, Markus Elfring wrote: >> kasprintf() returns a pointer to dynamically allocated memory >> which can be NULL upon failure. Ensure the allocation was successful >> by checking the pointer validity. > … >> +++ b/arch/x86/xen/smp.c >> @@ -61,10 +61,14 @@ void xen_smp_intr_free(unsigned int cpu) >> >> int xen_smp_intr_init(unsigned int cpu) >> { >> - int rc; >> + int rc = 0; > > I find the indication of a successful function execution sufficient by > the statement “return 0;” at the end. > How do you think about to omit such an extra variable initialisation? Thanks, it's no need now. I'll remove it in v3. > > >> char *resched_name, *callfunc_name, *debug_name; >> >> resched_name = kasprintf(GFP_KERNEL, "resched%d", cpu); >> + if (!resched_name) { >> + rc = -ENOMEM; >> + goto fail; >> + } >> per_cpu(xen_resched_irq, cpu).name = resched_name; >> rc = bind_ipi_to_irqhandler(XEN_RESCHEDULE_VECTOR, >> cpu, > > You propose to apply the same error code in four if branches. > I suggest to avoid the specification of duplicate assignment statements > for this purpose. > How do you think about to use another label like “e_nomem”? I'll add a new label to simply the code. > > Regards, > Markus
On Fri, Jan 19, 2024 at 05:22:25PM +0800, Kunwu Chan wrote: > On 2024/1/17 18:40, Markus Elfring wrote: > > > kasprintf() returns a pointer to dynamically allocated memory > > > which can be NULL upon failure. Ensure the allocation was successful > > > by checking the pointer validity. > > … > > > +++ b/arch/x86/xen/smp.c > > > @@ -61,10 +61,14 @@ void xen_smp_intr_free(unsigned int cpu) > > > > > > int xen_smp_intr_init(unsigned int cpu) > > > { > > > - int rc; > > > + int rc = 0; > > > > I find the indication of a successful function execution sufficient by > > the statement “return 0;” at the end. > > How do you think about to omit such an extra variable initialisation? > Thanks, it's no need now. I'll remove it in v3. This advice is good. Don't do unnecessary assignments. > > > > > > > char *resched_name, *callfunc_name, *debug_name; > > > > > > resched_name = kasprintf(GFP_KERNEL, "resched%d", cpu); > > > + if (!resched_name) { > > > + rc = -ENOMEM; > > > + goto fail; > > > + } > > > per_cpu(xen_resched_irq, cpu).name = resched_name; > > > rc = bind_ipi_to_irqhandler(XEN_RESCHEDULE_VECTOR, > > > cpu, > > > > You propose to apply the same error code in four if branches. > > I suggest to avoid the specification of duplicate assignment statements > > for this purpose. > > How do you think about to use another label like “e_nomem”? > I'll add a new label to simply the code. I'm not a Xen maintainer so I can't really comment on their style choices. However, as one of the kernel-janitors list people, I would say that not everyone agrees with Markus's style preferences. Markus was banned from the list for a while, but we unbanned everyone when we transitioned to the new list infrastructure. Do a search on lore to find out more. https://lore.kernel.org/all/?q=Elfring Perhaps wait for feedback from the maintainers for how to proceed? regards, dan carpenter
On 2024/1/22 16:30, Dan Carpenter wrote: > On Fri, Jan 19, 2024 at 05:22:25PM +0800, Kunwu Chan wrote: >> On 2024/1/17 18:40, Markus Elfring wrote: >>>> kasprintf() returns a pointer to dynamically allocated memory >>>> which can be NULL upon failure. Ensure the allocation was successful >>>> by checking the pointer validity. >>> … >>>> +++ b/arch/x86/xen/smp.c >>>> @@ -61,10 +61,14 @@ void xen_smp_intr_free(unsigned int cpu) >>>> >>>> int xen_smp_intr_init(unsigned int cpu) >>>> { >>>> - int rc; >>>> + int rc = 0; >>> >>> I find the indication of a successful function execution sufficient by >>> the statement “return 0;” at the end. >>> How do you think about to omit such an extra variable initialisation? >> Thanks, it's no need now. I'll remove it in v3. > > This advice is good. Don't do unnecessary assignments. Thanks for your suggestions, I'll keep it in mind. > >>> >>> >>>> char *resched_name, *callfunc_name, *debug_name; >>>> >>>> resched_name = kasprintf(GFP_KERNEL, "resched%d", cpu); >>>> + if (!resched_name) { >>>> + rc = -ENOMEM; >>>> + goto fail; >>>> + } >>>> per_cpu(xen_resched_irq, cpu).name = resched_name; >>>> rc = bind_ipi_to_irqhandler(XEN_RESCHEDULE_VECTOR, >>>> cpu, >>> >>> You propose to apply the same error code in four if branches. >>> I suggest to avoid the specification of duplicate assignment statements >>> for this purpose. >>> How do you think about to use another label like “e_nomem”? >> I'll add a new label to simply the code. > > I'm not a Xen maintainer so I can't really comment on their style > choices. However, as one of the kernel-janitors list people, I would > say that not everyone agrees with Markus's style preferences. Markus > was banned from the list for a while, but we unbanned everyone when we > transitioned to the new list infrastructure. Do a search on lore to > find out more. https://lore.kernel.org/all/?q=Elfring > > Perhaps wait for feedback from the maintainers for how to proceed? OK, I'll wait for the feedback. > > regards, > dan carpenter >
>>> How do you think about to use another label like “e_nomem”? >> I'll add a new label to simply the code. > > I'm not a Xen maintainer so I can't really comment on their style choices. Linux contributors can discuss various implementation details. > However, as one of the kernel-janitors list people, I would > say that not everyone agrees with Markus's style preferences. Can a corresponding document be improved accordingly? Centralized exiting of functions https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/process/coding-style.rst?h=v6.8-rc1#n526 Do you find a related information source helpful? https://wiki.sei.cmu.edu/confluence/display/c/MEM12-C.+Consider+using+a+goto+chain+when+leaving+a+function+on+error+when+using+and+releasing+resources Regards, Markus
diff --git a/arch/x86/xen/smp.c b/arch/x86/xen/smp.c index 4b0d6fff88de..0ea4f1b2ab21 100644 --- a/arch/x86/xen/smp.c +++ b/arch/x86/xen/smp.c @@ -61,10 +61,14 @@ void xen_smp_intr_free(unsigned int cpu) int xen_smp_intr_init(unsigned int cpu) { - int rc; + int rc = 0; char *resched_name, *callfunc_name, *debug_name; resched_name = kasprintf(GFP_KERNEL, "resched%d", cpu); + if (!resched_name) { + rc = -ENOMEM; + goto fail; + } per_cpu(xen_resched_irq, cpu).name = resched_name; rc = bind_ipi_to_irqhandler(XEN_RESCHEDULE_VECTOR, cpu, @@ -77,6 +81,10 @@ int xen_smp_intr_init(unsigned int cpu) per_cpu(xen_resched_irq, cpu).irq = rc; callfunc_name = kasprintf(GFP_KERNEL, "callfunc%d", cpu); + if (!callfunc_name) { + rc = -ENOMEM; + goto fail; + } per_cpu(xen_callfunc_irq, cpu).name = callfunc_name; rc = bind_ipi_to_irqhandler(XEN_CALL_FUNCTION_VECTOR, cpu, @@ -90,6 +98,10 @@ int xen_smp_intr_init(unsigned int cpu) if (!xen_fifo_events) { debug_name = kasprintf(GFP_KERNEL, "debug%d", cpu); + if (!debug_name) { + rc = -ENOMEM; + goto fail; + } per_cpu(xen_debug_irq, cpu).name = debug_name; rc = bind_virq_to_irqhandler(VIRQ_DEBUG, cpu, xen_debug_interrupt, @@ -101,6 +113,10 @@ int xen_smp_intr_init(unsigned int cpu) } callfunc_name = kasprintf(GFP_KERNEL, "callfuncsingle%d", cpu); + if (!callfunc_name) { + rc = -ENOMEM; + goto fail; + } per_cpu(xen_callfuncsingle_irq, cpu).name = callfunc_name; rc = bind_ipi_to_irqhandler(XEN_CALL_FUNCTION_SINGLE_VECTOR, cpu,