Received: by 2002:a05:6a10:413:0:0:0:0 with SMTP id 19csp791182pxp; Fri, 11 Mar 2022 15:13:36 -0800 (PST) X-Google-Smtp-Source: ABdhPJzJQldBxA13NxVskWOA8H0hlQyqdN6ZyL2oK3b6DSIGzj+gLsIkq0Wf0V1c21OocIP0IB8M X-Received: by 2002:a17:90a:8c14:b0:1be:e8cc:96bb with SMTP id a20-20020a17090a8c1400b001bee8cc96bbmr24334827pjo.176.1647040416440; Fri, 11 Mar 2022 15:13:36 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1647040416; cv=none; d=google.com; s=arc-20160816; b=ybT+kSap9hZzgmMDxEtRJINA1go9hEOhOTA1y6B9POHrFnFHMfTJOMXxbe7vBqwiwf Bd5D3ci8K6krSghN9fmLDYlyhW2uYgQzjxUePf+AHaC3C31x5erYCeth271Z7XqETox3 J6l/KFa8nYZWAOUZTZUWLw3EYphuN6F63WmVmhAClYg+uNNatu+E5Bu6x+6qSFUHRBNk kxA1QbqSDLgUUMJJGthMxhAFzYzAoQAosQDws41HR9B3yghfcQG4opSQrqo0O9HYsGKD 028jVHH5gRG8VXHl/ZgANvfjb9BxV+u+9bznrcVqxLJNglqH0wEoJ7PCxpBIcjIsaGk1 IWHg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:subject:cc:to:from:date :dkim-signature; bh=QxcnJJvBNCY+bGfnFgLArG30Rx8eamlF+KA/wCgp2ak=; b=eGS0EspigWBgh4kl43gfHRKmmGRC6PYwM4F4Yr2ebfohfFAdb89w2anNqHCn3TTWti kPRrtACczFPHIUen2uUh/pu6ADuaBGjkmprwi7gKjAarV3PeJRFdAreJ3e99sJG8oW6v Pk58YDixkmZbDYZd/SVqPLO9NlX6Sf7r2uBuHlTiiHu9b8HIPSacu62bFwwMxohAnibC plMGLYdycBjAWFjoNmcGyLIuuZH3dadoQG3tG4ebvsZo7dZ7eIajIjYd/rOrSJt/vuVA I+lhnUhtRTfmUBlQrfDEmCGsnNrEbZRQ1Vw//5x5wOK/R7Dl55vDSdUmmRSF2XzKm07i rPsQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@networkplumber-org.20210112.gappssmtp.com header.s=20210112 header.b=sRCgtqRw; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net. [2620:137:e000::1:18]) by mx.google.com with ESMTPS id f15-20020a056a00238f00b004f786642dfbsi4504914pfc.344.2022.03.11.15.13.36 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 11 Mar 2022 15:13:36 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) client-ip=2620:137:e000::1:18; Authentication-Results: mx.google.com; dkim=pass header.i=@networkplumber-org.20210112.gappssmtp.com header.s=20210112 header.b=sRCgtqRw; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id DA1282E1DED; Fri, 11 Mar 2022 14:12:32 -0800 (PST) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1350750AbiCKR0r (ORCPT + 99 others); Fri, 11 Mar 2022 12:26:47 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38116 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S237215AbiCKR0p (ORCPT ); Fri, 11 Mar 2022 12:26:45 -0500 Received: from mail-pl1-x635.google.com (mail-pl1-x635.google.com [IPv6:2607:f8b0:4864:20::635]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1578F1D0D51 for ; Fri, 11 Mar 2022 09:25:42 -0800 (PST) Received: by mail-pl1-x635.google.com with SMTP id p17so8198945plo.9 for ; Fri, 11 Mar 2022 09:25:42 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=networkplumber-org.20210112.gappssmtp.com; s=20210112; h=date:from:to:cc:subject:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=QxcnJJvBNCY+bGfnFgLArG30Rx8eamlF+KA/wCgp2ak=; b=sRCgtqRwTdMHtzhNI7YQxf++43csWqxxhxHiIWZUApUDmTYDJj6RFA3fKtOLrA6TRZ us6iW/6dQnVO9b1lfj1tYw12dO15MDaYQE6sKJczkNASZFF9D4quSBe5hBiC9syF6uf8 2HV4UTD2g3FVMUTUeZWvo0Wtp0D2MsxbQTACr31G+le84FCm8bfgQLOHMy9SYT0YGysB 2iCxbNK47qhvPHQKQhKOr4b91cjsrKCQFsNzdTF1OQff4gEOnwDyMqeA/g4Kg0NkQsSW 87XI4DbRm5XxB7QmhHK3c7Gd9kSjw4M+inl8695FsRfLk3tAfEAegD2HG0+rziACkca2 /ktw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:from:to:cc:subject:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=QxcnJJvBNCY+bGfnFgLArG30Rx8eamlF+KA/wCgp2ak=; b=geNqIebbrzXTR9Da/6qeHHkpstYbKa2l/tGjKIGqTJ6CxyLgLHlv+P5Pf5AGzD6ECm D22/bdXc0EOM6sNiUG24MGsMhvtXqrN3HQ4ZrDsaSurdoU96WgxPfkzqbj75wYEnqdjZ BmQdf6BcBNPN88wCMbz1efRBtRixhMDKyUF6cLHw57p3bVahncmXMk43M7jhcW15VQwP 1NVq771hdWAPLewdGj9VL3Zf34bzDoRIOyAKamnW1lzo6KIcb5/3e+JGJDqC5vTnDa7u y9HhIMZlqVr6T/X7uVjr+60UO6uriS3pKfGtM9mDr04qm5GrmcCcQ9qqWqMkgPQRvwhI 6ycA== X-Gm-Message-State: AOAM531y4UN8wx3tpdahKNTPt+OotCLDoInGnYiGaRkE+TwtRCgoVWOd mvAr1Nyi8NviGpq8UnPzTYv26g== X-Received: by 2002:a17:902:e5ca:b0:152:54c1:f87f with SMTP id u10-20020a170902e5ca00b0015254c1f87fmr11485758plf.59.1647019541529; Fri, 11 Mar 2022 09:25:41 -0800 (PST) Received: from hermes.local (204-195-112-199.wavecable.com. [204.195.112.199]) by smtp.gmail.com with ESMTPSA id v14-20020a056a00148e00b004e1cee6f6b4sm12178835pfu.47.2022.03.11.09.25.40 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 11 Mar 2022 09:25:41 -0800 (PST) Date: Fri, 11 Mar 2022 09:25:38 -0800 From: Stephen Hemminger To: Jiasheng Jiang Cc: kys@microsoft.com, haiyangz@microsoft.com, sthemmin@microsoft.com, wei.liu@kernel.org, decui@microsoft.com, davem@davemloft.net, kuba@kernel.org, ast@kernel.org, daniel@iogearbox.net, hawk@kernel.org, john.fastabend@gmail.com, andrii@kernel.org, kafai@fb.com, songliubraving@fb.com, yhs@fb.com, kpsingh@kernel.org, linux-hyperv@vger.kernel.org, netdev@vger.kernel.org, linux-kernel@vger.kernel.org, bpf@vger.kernel.org Subject: Re: [PATCH] hv_netvsc: Add check for kvmalloc_array Message-ID: <20220311092538.0cb478cf@hermes.local> In-Reply-To: <20220311032035.2037962-1-jiasheng@iscas.ac.cn> References: <20220311032035.2037962-1-jiasheng@iscas.ac.cn> MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,RDNS_NONE, SPF_HELO_NONE,T_SCC_BODY_TEXT_LINE autolearn=no 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 Fri, 11 Mar 2022 11:20:35 +0800 Jiasheng Jiang wrote: > As the potential failure of the kvmalloc_array(), > it should be better to check and restore the 'data' > if fails in order to avoid the dereference of the > NULL pointer. > > Fixes: 6ae746711263 ("hv_netvsc: Add per-cpu ethtool stats for netvsc") > Signed-off-by: Jiasheng Jiang > --- > drivers/net/hyperv/netvsc_drv.c | 6 ++++++ > 1 file changed, 6 insertions(+) > > diff --git a/drivers/net/hyperv/netvsc_drv.c b/drivers/net/hyperv/netvsc_drv.c > index 3646469433b1..018c4a5f6f44 100644 > --- a/drivers/net/hyperv/netvsc_drv.c > +++ b/drivers/net/hyperv/netvsc_drv.c > @@ -1587,6 +1587,12 @@ static void netvsc_get_ethtool_stats(struct net_device *dev, > pcpu_sum = kvmalloc_array(num_possible_cpus(), > sizeof(struct netvsc_ethtool_pcpu_stats), > GFP_KERNEL); > + if (!pcpu_sum) { > + for (j = 0; j < i; j++) > + data[j] = 0; > + return; > + } I don't think you understood what my comment was. The zeroing here is not necessary. Just do: if (!pcpu_sum) return; The data pointer is to buffer allocated here: static int ethtool_get_stats(struct net_device *dev, void __user *useraddr) { ... if (n_stats) { data = vzalloc(array_size(n_stats, sizeof(u64))); <<<<< is already zeroed. if (!data) return -ENOMEM; ops->get_ethtool_stats(dev, &stats, data);