Received: by 2002:a05:7412:8d10:b0:f3:1519:9f41 with SMTP id bj16csp4064630rdb; Mon, 11 Dec 2023 07:58:00 -0800 (PST) X-Google-Smtp-Source: AGHT+IFFSnxEST9ofqwOLAd64dSm7WOETd+/hdSsS7wjL4JYz1lKTn7SS72QdSpnhrkvSFagzsVA X-Received: by 2002:a17:903:1103:b0:1d3:1be6:78c3 with SMTP id n3-20020a170903110300b001d31be678c3mr1034283plh.12.1702310280073; Mon, 11 Dec 2023 07:58:00 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1702310280; cv=none; d=google.com; s=arc-20160816; b=hE76TAC05JjjoBjIrx84hrgxX+xMtyDbBqlctYjHFuVG0sKSivIFzXDF2q6zihfDkj 2pshrZV1GcN1iBtay4yjAHhZx42syw33eqjT7CKmPxjUVxeXA3mPBwsqZ3wk0V3+bXIp q+4yEB0El8pdB8it6m9Sgv/1EmHZ+Q7wn5cGyxzKhapwfjzZcY40iDcfOUe87C56LFW+ au8/JImWtkooPLLfVa2V+6fwJWJcnRP4WRvjVAFTduJcN6aJ+oHVhHSVllDZgpYfsyRQ dtb/6QYRND2VCQ5Tm1rdgLuBLSZ8Waad5m0+cRRjDz9ODUAC++l5JjoYFu3vN5QT4Xhk nCjA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=vgnFsD1rGgCy3ClJpDI9oqAdCvPdJ7fZQsiqLqMiah4=; fh=8N5CPweuu2kWKyOhyj4oGWpUnH8QHns6ctvpzUqgW4A=; b=EDRjR2YVksIv0LZ3Zu62OiIhbOARd8jyCOpg0H44a7UEcqCIifJ1qoAGMQ1QEONaky v8o9NKcIqUtSqqf0Bq5e5w3YN+fuMMJyu4bHAXNDO2YHoMYDHJECki4KWRiDyqfsHF/o +Qd8P/lfq6f6T0565chJA5NYtUCfL/cca/rYpMtowII57H6rmdDWqnGUws/WcsjexlJB axtFEY99Eq2PujubrhlCcDK2bJPicDbXb3+8PMlpSRNI1qgx8HMOvGb/r1F2FmCoAleZ t+YW4NwSIG1YmZ61/DKu+5IUk5J0CJfOeuu8bPDms+GASF+/quYrowdnQPRbI0oKLdZg XNWQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=HE440x8b; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:7 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from snail.vger.email (snail.vger.email. [2620:137:e000::3:7]) by mx.google.com with ESMTPS id r2-20020a170902be0200b001d0cbef2eeesi6267072pls.71.2023.12.11.07.57.57 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 11 Dec 2023 07:58:00 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:7 as permitted sender) client-ip=2620:137:e000::3:7; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=HE440x8b; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:7 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by snail.vger.email (Postfix) with ESMTP id CAA1280A1876; Mon, 11 Dec 2023 07:57:56 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at snail.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1344577AbjLKP5p (ORCPT + 99 others); Mon, 11 Dec 2023 10:57:45 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46728 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1344402AbjLKP50 (ORCPT ); Mon, 11 Dec 2023 10:57:26 -0500 Received: from mail-yw1-x1133.google.com (mail-yw1-x1133.google.com [IPv6:2607:f8b0:4864:20::1133]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C097A106 for ; Mon, 11 Dec 2023 07:57:32 -0800 (PST) Received: by mail-yw1-x1133.google.com with SMTP id 00721157ae682-5d3c7ef7b31so43722547b3.3 for ; Mon, 11 Dec 2023 07:57:32 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1702310252; x=1702915052; darn=vger.kernel.org; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:from:to:cc:subject:date:message-id:reply-to; bh=vgnFsD1rGgCy3ClJpDI9oqAdCvPdJ7fZQsiqLqMiah4=; b=HE440x8bW4ivJ/FnJToCuXSWaSfYNrshA24kBJWkka43pvnWHamxqzAmOuBySjoHPK iMqTSBemht7BXYe63CuQyy9D8PlaUJab6eN6v63svBqK/p7M9/HRGp06p032eDJdxV2j tvD7zvmXeIApnJnDlvRNMS2gggQWQZKa3wsHr1oabl0UekJj2a700KvY+w7+ZomJvzLn Q6YzhLVJnkDgTuiFcZ1Pb98t8mttTYoHxuzTDH6pHj0ac4V4dhHMFUe0Fsgqf5swVA5N bmtPAAv8B+m5CJ9rFowIAeoBfVdO1LiDlYu/BJWZzURfRsTR54csFz/JpdcrUXCnLfYP H4kA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1702310252; x=1702915052; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=vgnFsD1rGgCy3ClJpDI9oqAdCvPdJ7fZQsiqLqMiah4=; b=GUqMAnx9HvZjS8xrdLklyaxYUxF4ARqiAfCva3pg62BtdTYRJiLWJIYxcpgN0hPeDR whCGbQzeSO5EqVg0EUvi0fwHRh3v/ucOTiudnT/7Acz2kpmExDxHFCbJa/3u/ioTRlZo 3Y88BpQ3Oi+w1f7c8RVZ8iPSLRVlYkJgF5ZFCUStDfVxLybvvxp2A6k7gE231g8Wba3Y FDDt+oa8hS3RUF6vDNS1cWURD8qFy+AoPdwWY/z126plt7Lfj9OCS79QI7YSMA/BbihM AQ7B13yd0lLrqP9Jau83ieMZqrhVqD2/au9yZWVJCGBM+ln/sOy41WS53GaHTeAtBgBB 68fg== X-Gm-Message-State: AOJu0YyarelwtfLojHZGbGS1Ms4q+up1/V+CPNDqe6ape5IrJJQq86O3 BxLxQnQRxi+DXaay+ZNMdPvKLybs55P2qshZnhk= X-Received: by 2002:a05:690c:2c90:b0:5e1:8d19:4e77 with SMTP id ep16-20020a05690c2c9000b005e18d194e77mr287921ywb.8.1702310251910; Mon, 11 Dec 2023 07:57:31 -0800 (PST) Received: from ubuntu-server-vm-macos (072-189-067-006.res.spectrum.com. [72.189.67.6]) by smtp.gmail.com with ESMTPSA id d201-20020a814fd2000000b005cc5f5aa533sm3066544ywb.43.2023.12.11.07.57.30 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 11 Dec 2023 07:57:31 -0800 (PST) Date: Mon, 11 Dec 2023 15:57:28 +0000 From: William Breathitt Gray To: Fabrice Gasnier Cc: linux-iio@vger.kernel.org, linux-kernel@vger.kernel.org Subject: Re: [PATCH v3 1/2] tools/counter: add a flexible watch events tool Message-ID: References: <20231206164726.418990-1-fabrice.gasnier@foss.st.com> <20231206164726.418990-2-fabrice.gasnier@foss.st.com> MIME-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha256; protocol="application/pgp-signature"; boundary="DgK1nOAauTq8w6Yp" Content-Disposition: inline In-Reply-To: <20231206164726.418990-2-fabrice.gasnier@foss.st.com> X-Spam-Status: No, score=-1.7 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE,RCVD_IN_XBL, SPF_HELO_NONE,SPF_PASS,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 X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (snail.vger.email [0.0.0.0]); Mon, 11 Dec 2023 07:57:56 -0800 (PST) --DgK1nOAauTq8w6Yp Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Content-Transfer-Encoding: quoted-printable On Wed, Dec 06, 2023 at 05:47:25PM +0100, Fabrice Gasnier wrote: > This adds a new counter tool to be able to test various watch events. > A flexible watch array can be populated from command line, each field > may be tuned with a dedicated command line sub-option in "--watch" string. > Several watch events can be defined, each can have specific watch options, > by using "--watch --watch ". > Watch options is a comma separated list. >=20 > It also comes with a simple default watch (to monitor overflow/underflow > events), used when no watch parameters are provided. It's equivalent to: > counter_watch_events -w comp_count,scope_count,evt_ovf_udf >=20 > The print_usage() routine proposes another example, from the command line, > which generates a 2 elements watch array, to monitor: > - overflow underflow events > - capture events, on channel 3, that reads read captured data by > specifying the component id (capture3_component_id being 7 here). >=20 > Signed-off-by: Fabrice Gasnier > --- > Changes in v3: > - Free the allocated memory, also close the char device > - Split of another patch series[1]. > [1] https://lore.kernel.org/lkml/20230922143920.3144249-1-fabrice.gasnier= @foss.st.com/ Hi Fabrice, Thank you for splitting this from the other patches. I think you may still be leaking memory in a few places below. > + if (nwatch) { > + watches =3D calloc(nwatch, sizeof(*watches)); > + if (!watches) { > + perror("Error allocating watches"); > + return 1; > + } > + } else { > + /* default to simple watch example */ > + watches =3D simple_watch; > + nwatch =3D ARRAY_SIZE(simple_watch); > + } If we go down the calloc() path, then we should free the memory before any return. > + case WATCH_CHANNEL: > + if (!value) { > + fprintf(stderr, "Missing chan=3D\n"); > + return -EINVAL; Such as here. > + } > + watches[i].channel =3D strtoul(value, NULL, 10); > + if (errno) > + return -errno; Here. > + break; > + case WATCH_ID: > + if (!value) { > + fprintf(stderr, "Missing id=3D\n"); > + return -EINVAL; Here. > + } > + watches[i].component.id =3D strtoul(value, NULL, 10); > + if (errno) > + return -errno; Here. > + break; > + case WATCH_PARENT: > + if (!value) { > + fprintf(stderr, "Missing parent=3D\n"); > + return -EINVAL; Here. > + } > + watches[i].component.parent =3D strtoul(value, NULL, 10); > + if (errno) > + return -errno; Here. > + break; > + default: > + fprintf(stderr, "Unknown suboption '%s'\n", value); > + return -EINVAL; Here. > + ret =3D asprintf(&device_name, "/dev/counter%d", dev_num); > + if (ret < 0) > + return -ENOMEM; Here. > + fd =3D open(device_name, O_RDWR); > + if (fd =3D=3D -1) { > + perror("Unable to open counter device"); > + return 1; Here. > + } > + > + for (i =3D 0; i < nwatch; i++) { > + ret =3D ioctl(fd, COUNTER_ADD_WATCH_IOCTL, watches + i); > + if (ret =3D=3D -1) { > + fprintf(stderr, "Error adding watches[%d]: %s\n", i, > + strerror(errno)); > + return 1; Here. > + } > + } > + > + ret =3D ioctl(fd, COUNTER_ENABLE_EVENTS_IOCTL); > + if (ret =3D=3D -1) { > + perror("Error enabling events"); > + return 1; Here. > + } > + > + for (i =3D 0; loop <=3D 0 || i < loop; i++) { > + ret =3D read(fd, &event_data, sizeof(event_data)); > + if (ret =3D=3D -1) { > + perror("Failed to read event data"); > + return 1; Here. > + } > + > + if (ret !=3D sizeof(event_data)) { > + fprintf(stderr, "Failed to read event data\n"); > + return -EIO; And here. > + if (watches !=3D simple_watch) > + free(watches); > + close(fd); > + > + return 0; We finally free watches here, close the file descriptor, and return. So instead of returning an error code directly when you encounter a problem, I would do an unwinding goto section like the following instead. First, the open() call occurs after the calloc(), so move the close() call above the watches check so that we unwind in a first-in-last-out order. Next, add a label to mark the file descriptor close section, and another label to mark the watches free section. Then, rather than returning 0 directly, return a retval that we can set. That way, when you need to return on an error, set retval to the error code and goto the file descriptor close label if we're past the open() call, or the watches free label if we're just past the calloc() call. William Breathitt Gray --DgK1nOAauTq8w6Yp Content-Type: application/pgp-signature; name="signature.asc" -----BEGIN PGP SIGNATURE----- iHUEABYIAB0WIQSNN83d4NIlKPjon7a1SFbKvhIjKwUCZXcxaAAKCRC1SFbKvhIj KyH7AQDS6pGsvZkqLTWO56L/iANt86BNInqaN0ixwVhpsDcpBgD/dVb/3cacHHOH q1S50LhsUQ4jogqKhU20axDclAOqvQQ= =3Fgn -----END PGP SIGNATURE----- --DgK1nOAauTq8w6Yp--