Received: by 2002:a05:6358:3188:b0:123:57c1:9b43 with SMTP id q8csp31388rwd; Wed, 31 May 2023 18:33:14 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ6QVcmvwGc7abE0gNt8Y9rsnDGnXz3A9ssrp62GdJzf67O0uKRAF2+mtFruMEXtV3p7qqs/ X-Received: by 2002:a17:902:cec7:b0:1b0:56cf:b8c0 with SMTP id d7-20020a170902cec700b001b056cfb8c0mr395568plg.13.1685583194475; Wed, 31 May 2023 18:33:14 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1685583194; cv=none; d=google.com; s=arc-20160816; b=wKeeoCoqOrGJfNf4GYyRhZe0Ko3Nl2zBptD1QwlLjObd5/0csbGNrXtR+gc0IOeuAv KlMpl+DcWqq7q5kcqZZ9DKvzR5cltwsACPg56lsXOt0NvrZ387D4zbxNfw1GE+g3RGuT KQGLfhqYD9G0gFh8KNpzbAl73Qg39PKOcjvKQpf6cM7/qLt1cXupE4DMf1aoM+Iu5T1b j1OhYYLAdC6jpE/VaOj4IGs9Yd/rHUNnb51yKfyYp1ozPI0fxI3MFxTSqEQWkOwNO23Q ph/n1JNKE84YcQZs4oVjhcxZhAXOgA50D6evu4wzx8KQKYJRJCAroRAjGCw/vZdmdume ZSMA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:in-reply-to:from :references:to:content-language:subject:cc:user-agent:mime-version :date:message-id:dkim-signature; bh=dmE1Wy29IYfIipdHrvldNTlOuB8205VSqyOYdOEJd2Y=; b=GbvXzNTTzV11kGEYpERrfvSLwE6RlVI9D4EVsBARcJ77PRIHgibIso9SFD6mqEimEV 45r4+vC6jaN50ke88b8kzXQbmzU4RAFyWPaQEzFBBTcMatb1ov/FhP9vn7WcC6eK6zRq Q6zz5r/lzzjFPt3h5y4V/g0f9oeL42tDOfYAtUmFQdaT9SUTq0UXFKQFlvzu5/uTIa4o VNKXX7u/0BB4EH0mZlKgHyEMxrtGmk/Zu/VOBJifld3qcl35Es5Xzeqtyv/P5nWESIsI VWwMats7bHj6TDKvdxQbyGaDIprfNzzOzuBSQs5Qv5Irvqjwse9PfSz66M4NkwehkgdP JVBA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=OXu7pWA2; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id s5-20020a170902ea0500b001afd1a40242si1894228plg.637.2023.05.31.18.33.01; Wed, 31 May 2023 18:33:14 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=OXu7pWA2; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230387AbjFABYp (ORCPT + 99 others); Wed, 31 May 2023 21:24:45 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38354 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229469AbjFABYo (ORCPT ); Wed, 31 May 2023 21:24:44 -0400 Received: from mga02.intel.com (mga02.intel.com [134.134.136.20]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7B43C101 for ; Wed, 31 May 2023 18:24:43 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1685582683; x=1717118683; h=message-id:date:mime-version:cc:subject:to:references: from:in-reply-to:content-transfer-encoding; bh=vkqhGsgk3MYAdTf0+chQ1jY4UVD8G35fFDs8sGIJl94=; b=OXu7pWA2sMdu/8XB71W6WbgnwKnpIfCMCwQvJggCdY9cDkRvWsQRVz4r xJx2otpGI+nOO8pKVR8hpnMDOsxihRfqG6gz/FqyA8/GegUFvDDqdfkoi qQsqyNXI4hUlSIhNbqfdCTlWExSs7zPTmuCVKVQT/DIz914qtcC4czTtH F9VAlYj/cIAj2JZL5RFveNQ8mO5o4ChEUSHFDLYFSrPI50kVG5Ta6S3B/ 3n/2UrKrS1lq0ceP5lj/YEMEcHkU2ITOKJqC6dcFLXwrXAfOjsq90CgTY LwE8CnSg0MR7Q5m2CMHYiKKndJ2fpAsYzfF2SGLuhLR98iY6lZV/AmN6r A==; X-IronPort-AV: E=McAfee;i="6600,9927,10727"; a="344942005" X-IronPort-AV: E=Sophos;i="6.00,207,1681196400"; d="scan'208";a="344942005" Received: from fmsmga005.fm.intel.com ([10.253.24.32]) by orsmga101.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 31 May 2023 18:24:43 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10727"; a="1037272774" X-IronPort-AV: E=Sophos;i="6.00,207,1681196400"; d="scan'208";a="1037272774" Received: from allen-box.sh.intel.com (HELO [10.239.159.127]) ([10.239.159.127]) by fmsmga005.fm.intel.com with ESMTP; 31 May 2023 18:24:40 -0700 Message-ID: Date: Thu, 1 Jun 2023 09:23:44 +0800 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.11.0 Cc: baolu.lu@linux.intel.com, iommu@lists.linux.dev, linux-kernel@vger.kernel.org Subject: Re: [PATCH 2/2] iommu/vt-d: Use BUG_ON to check NULL value of 'table' Content-Language: en-US To: Yanfei Xu , dwmw2@infradead.org, joro@8bytes.org, will@kernel.org, robin.murphy@arm.com References: <20230530092503.152926-1-yanfei.xu@intel.com> <20230530092503.152926-3-yanfei.xu@intel.com> <3e75f9de-4c67-93aa-9fb4-28da9849dd48@linux.intel.com> From: Baolu Lu In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-4.6 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_EF,NICE_REPLY_A,RCVD_IN_DNSWL_MED, RCVD_IN_MSPIKE_H3,RCVD_IN_MSPIKE_WL,SPF_HELO_NONE,SPF_NONE, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 5/31/23 3:09 PM, Yanfei Xu wrote: > On 5/31/2023 11:26 AM, Baolu Lu wrote: >> On 5/30/23 5:25 PM, Yanfei Xu wrote: >>> Checking NULL value of 'table' variable deserves a BUG_ON as the >>> following code will trigger a crash by dereferencing the NULL >>> 'table' pointer. Crash in advance with BUG_ON to avoid WARN_ON >>> plus NULL pointer dereferencing can simplify the crash log. >>> >>> Signed-off-by: Yanfei Xu >>> --- >>>   drivers/iommu/intel/iommu.c | 2 +- >>>   1 file changed, 1 insertion(+), 1 deletion(-) >>> >>> diff --git a/drivers/iommu/intel/iommu.c b/drivers/iommu/intel/iommu.c >>> index e98f1b122b49..8aa3bfdb7f95 100644 >>> --- a/drivers/iommu/intel/iommu.c >>> +++ b/drivers/iommu/intel/iommu.c >>> @@ -1944,7 +1944,7 @@ static int domain_context_mapping_one(struct >>> dmar_domain *domain, >>>       if (sm_supported(iommu)) { >>>           unsigned long pds; >>>   -        WARN_ON(!table); >>> +        BUG_ON(!table); >> >> BUG_ON() is not recommended. Perhaps, >> >>         if (!table) >>             -ENODEV; >> > Agree:) It is always better to handle the error than crash kernel. > > How about: > diff --git a/drivers/iommu/intel/iommu.c b/drivers/iommu/intel/iommu.c > index 8096273b034c..7f077e3a4128 100644 > --- a/drivers/iommu/intel/iommu.c > +++ b/drivers/iommu/intel/iommu.c > @@ -1944,7 +1944,10 @@ static int domain_context_mapping_one(struct > dmar_domain *domain, >         if (sm_supported(iommu)) { >                 unsigned long pds; > > -               WARN_ON(!table); > +               if (WARN_ON(!table)) { > +                       ret = -ENODEV; > +                       goto out_unlock; > +               } I'd recommend to remove this line directly. This pointer will be accessed in the following code, if empty "table" really happens, the kernel will report a NULL pointer reference warning at the first place. In the same function, I also saw "WARN_ON(did == 0);". It's unnecessary as domain_id_iommu() will never return 0. Perhaps we can clean it up as well. Best regards, baolu