Received: by 2002:a25:683:0:0:0:0:0 with SMTP id 125csp1491628ybg; Thu, 11 Jun 2020 11:12:42 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzZN3WpDs2ZFPaBq1WC2eTJAdi9SbLBRD55iuV5UHvtNNlXuly1W1JR6DB4vmhk5+Cllhga X-Received: by 2002:a17:906:1d56:: with SMTP id o22mr9298232ejh.406.1591899162380; Thu, 11 Jun 2020 11:12:42 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1591899162; cv=none; d=google.com; s=arc-20160816; b=zbUYQD/wV0QXFjiygBmiXmi/1Rub8QPj2x5kkveDiSkXY9WgGT1A1ko8h65I2V7QvL H3hkaWmnjJ3+ZFZP2lsYvqQTGBrAaPlWwLpOzgQa07m8PXOKP8E4SnSNJQ3cDk5i+8pu xHdbHxcyfkNWKqCg6gTAjpKqoPJNS8ZObNqN63fpdYc1EvSBfMKWefCxQ0xWojftJ3qe +9xZrDPFGJtMquY9qmYpLt/84ZyNAB6KZBp8NR0+qADn5VEBnf/DcuI9kWPNWv2S4dGw 3fnzxz/t+Waeby22FVp6lHtX/RxSMrlD+pe+2z8jfEtBykTjMZr+Wubw3v+s16k60b6Z dNqg== 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:mime-version :organization:references:in-reply-to:message-id:subject:cc:to:from :date:dkim-signature; bh=PDcY/aNN0AijO6KeFZRrqAa1c2dHhA8tsB/uBtT8Fjk=; b=XWJUoy9Ln6cnZKWX2ANd4jx2EYN20A7t0qmX6phRBL2O8ezNiXgaIl1MhJyABLg+Y6 kYJr86LfaNxP0fNRCVogSoaq1PTm7p1m4H/H8YM1viWvtl3ypBwarfOi2p6JADKMJSKE m13mq3iKzi2PBSp9AsCxloAGnziw2llc8pdFrPjNreoAsx6FUZMwkeGPhOGfbWYmClxu znGmk5Om08DRpFzmk0DgqhAKtUnuGeoZh6WRT1O5qzuVxyv1JsiS4kaOsX78Mn2sF1qY 5oWJ1LpoXzin1DC8l3vfJzPrGcExVYoGzsc5Hz0hWYN9tIjHBAzVdhTFOy5VzgBj8S7f tsew== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=CPZnbZJ1; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id n22si2234313edy.377.2020.06.11.11.12.19; Thu, 11 Jun 2020 11:12:42 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=CPZnbZJ1; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728259AbgFKSCP (ORCPT + 99 others); Thu, 11 Jun 2020 14:02:15 -0400 Received: from us-smtp-delivery-1.mimecast.com ([207.211.31.120]:21972 "EHLO us-smtp-1.mimecast.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1726251AbgFKSCH (ORCPT ); Thu, 11 Jun 2020 14:02:07 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1591898525; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=PDcY/aNN0AijO6KeFZRrqAa1c2dHhA8tsB/uBtT8Fjk=; b=CPZnbZJ1UhvPoBVorW8ZkBeoQ7TyZC9g5Lr6upuhb5R7vDKjP3T9voyOk+yiYmay9/5d3f Ed9mS2OP7k/tvQ8gouaBrxWgdbVgste+u7NONbtdQAnDqYx/8QXOEfLPn26nxj8snpFJSI f4MJKYxVIUSxnbXrSjsz2xtOGCmSElM= Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-155-V63-PS4kPWWBKyLEDIScKQ-1; Thu, 11 Jun 2020 14:01:51 -0400 X-MC-Unique: V63-PS4kPWWBKyLEDIScKQ-1 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id B46B0BFC6; Thu, 11 Jun 2020 18:01:48 +0000 (UTC) Received: from carbon (unknown [10.40.208.9]) by smtp.corp.redhat.com (Postfix) with ESMTP id ECCD05D9DC; Thu, 11 Jun 2020 18:01:43 +0000 (UTC) Date: Thu, 11 Jun 2020 20:01:40 +0200 From: Jesper Dangaard Brouer To: Gaurav Singh Cc: Alexei Starovoitov , Daniel Borkmann , "David S. Miller" , Jakub Kicinski , Jesper Dangaard Brouer , John Fastabend , Martin KaFai Lau , Song Liu , Yonghong Song , Andrii Nakryiko , KP Singh , netdev@vger.kernel.org (open list:XDP (eXpress Data Path)), bpf@vger.kernel.org (open list:XDP (eXpress Data Path)), linux-kernel@vger.kernel.org (open list) Subject: Re: [PATCH] xdp_rxq_info_user: Replace malloc/memset w/calloc Message-ID: <20200611200140.259423b4@carbon> In-Reply-To: <20200611150221.15665-1-gaurav1086@gmail.com> References: <20200611150221.15665-1-gaurav1086@gmail.com> Organization: Red Hat Inc. MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit X-Scanned-By: MIMEDefang 2.79 on 10.5.11.14 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Thu, 11 Jun 2020 11:02:21 -0400 Gaurav Singh wrote: > Replace malloc/memset with calloc Please also mention/describe that this also solves the bug you found. As this fix a potential bug, it will be appropriate to add a "Fixes:" line, just before "Signed-off-by" (meaning no newline between the two). Fixes: 0fca931a6f21 ("samples/bpf: program demonstrating access to xdp_rxq_info") > Signed-off-by: Gaurav Singh > --- > samples/bpf/xdp_rxq_info_user.c | 13 +++---------- > 1 file changed, 3 insertions(+), 10 deletions(-) > > diff --git a/samples/bpf/xdp_rxq_info_user.c b/samples/bpf/xdp_rxq_info_user.c > index 4fe47502ebed..caa4e7ffcfc7 100644 > --- a/samples/bpf/xdp_rxq_info_user.c > +++ b/samples/bpf/xdp_rxq_info_user.c > @@ -198,11 +198,8 @@ static struct datarec *alloc_record_per_cpu(void) > { > unsigned int nr_cpus = bpf_num_possible_cpus(); > struct datarec *array; > - size_t size; > > - size = sizeof(struct datarec) * nr_cpus; > - array = malloc(size); > - memset(array, 0, size); > + array = calloc(nr_cpus, sizeof(struct datarec)); > if (!array) { > fprintf(stderr, "Mem alloc error (nr_cpus:%u)\n", nr_cpus); > exit(EXIT_FAIL_MEM); > @@ -214,11 +211,8 @@ static struct record *alloc_record_per_rxq(void) > { > unsigned int nr_rxqs = bpf_map__def(rx_queue_index_map)->max_entries; > struct record *array; > - size_t size; > > - size = sizeof(struct record) * nr_rxqs; > - array = malloc(size); > - memset(array, 0, size); > + array = calloc(nr_rxqs, sizeof(struct record)); > if (!array) { > fprintf(stderr, "Mem alloc error (nr_rxqs:%u)\n", nr_rxqs); > exit(EXIT_FAIL_MEM); > @@ -232,8 +226,7 @@ static struct stats_record *alloc_stats_record(void) > struct stats_record *rec; > int i; > > - rec = malloc(sizeof(*rec)); > - memset(rec, 0, sizeof(*rec)); > + rec = calloc(1, sizeof(struct stats_record)); > if (!rec) { > fprintf(stderr, "Mem alloc error\n"); > exit(EXIT_FAIL_MEM); -- Best regards, Jesper Dangaard Brouer MSc.CS, Principal Kernel Engineer at Red Hat LinkedIn: http://www.linkedin.com/in/brouer