Received: by 2002:a05:6a10:eb17:0:0:0:0 with SMTP id hx23csp2336321pxb; Sun, 5 Sep 2021 15:53:16 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzf49Wtva3xDV2fJftlw/H8rPBwl+G+RUnnn+s44Lhir8zUnHGErDD03y89jKr9VNFsxme/ X-Received: by 2002:a5e:c70c:: with SMTP id f12mr7482187iop.166.1630882396400; Sun, 05 Sep 2021 15:53:16 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1630882396; cv=none; d=google.com; s=arc-20160816; b=oHFS+1R4swy2v4KQfzjWCZldB2jf1wwJaY9Np8vCnoI1gAr5WXLEVefbp2PBiJG/fZ Kj3Z3RtETF/RFYuW9zTLog7c5hNDZMIlav1UcXxDDDz5ItFHptSSZjrhSJdMxn4D7+Vw QBJkYKbjxUj6lzdK+vUE27evLohUdHPim1FOGs2fJQIVWWWghubWvHDQMmYu83KCWmPK /QBgxOosRgzlTOSizpTcSP5jsyT0c+RnlK/0bkTIKTXtR/HR6m6wBb3shQIj6PcZKC6W 4eiaY1OldIMHpjSevz7WY8pS3zeJC1v/L0xOgAENJAahE9FMZWCTUs3jfrWT0SEDVPqB pQxA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:subject:message-id:date:from:in-reply-to :references:mime-version:dkim-signature; bh=BoHJHDDUQvDuxt5VzrZQ7aDSXlW3dvl3n4+BEi9wJZM=; b=rtL8ilDIcKOX1AOpg7YDw2YmP8bk2VT4DVWsW0RMVmJ8Ms1iYPI7+TDdmsu7mI7Ffz vvJFRf74TfCG1quAveaZaKLfnNkK+OfwRmzfaG4sqigEvVtq9GjZgHvGnSIxcW9l0ArF pcPNlxsVbriwedK/lN+3bNiq4HhEOgFD4FcynILuokLjQ1Ok853Gpz943cA7kkU17gnn 2VBngAlK33Q7ingOgfSEuLw1SAAQnGqPPO21j0BhPy1kui7BZExqMCdhOTQDFnAgDHjD Xt+Gg45olaptXPbqvvGQFy1EybYUVAhGC/tgczcvNz5RimZ5CX68Mxs+inPq2uTKV0Uy CsJg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@usp.br header.s=usp-google header.b=hmwVZv0I; 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=QUARANTINE sp=NONE dis=NONE) header.from=usp.br Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id t24si5368494iog.114.2021.09.05.15.52.50; Sun, 05 Sep 2021 15:53:16 -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=@usp.br header.s=usp-google header.b=hmwVZv0I; 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=QUARANTINE sp=NONE dis=NONE) header.from=usp.br Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230085AbhIEWlR (ORCPT + 99 others); Sun, 5 Sep 2021 18:41:17 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45296 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229510AbhIEWlR (ORCPT ); Sun, 5 Sep 2021 18:41:17 -0400 Received: from mail-ed1-x52e.google.com (mail-ed1-x52e.google.com [IPv6:2a00:1450:4864:20::52e]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5842CC061757 for ; Sun, 5 Sep 2021 15:40:13 -0700 (PDT) Received: by mail-ed1-x52e.google.com with SMTP id q3so6841992edt.5 for ; Sun, 05 Sep 2021 15:40:13 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=usp.br; s=usp-google; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=BoHJHDDUQvDuxt5VzrZQ7aDSXlW3dvl3n4+BEi9wJZM=; b=hmwVZv0IehSDHdKr/5gEMvaA4SxOUAKSho+Jh+NgZ1YtJJgB87/nqAiVmkq66RdbcM m+ku3jJOCWZwOt2J5SoOy3JSSPHsPHq/HNv3OST+Lj4JPBXdJbI5bWnRVPkzOHJ/bgyL hDAAmJUDK7Yf/504RbkVBTMeqTlIYtS5Axr6ED6mLvfljtZRZRHq8umoerOAbOYfvHvo 7SK0Ll3OUpn2XZdNw35HWbMzVovHYKntMZKX4Am3V5qDrrBvCQrmM20NtEc7zgpd8+pX 6CCKzma4s/8LIWTRLRNTjmG0ylRPBY8z5WgYsZzuIZ0MSe31xdzN2Y6fxwEfSybJiBgY krOw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=BoHJHDDUQvDuxt5VzrZQ7aDSXlW3dvl3n4+BEi9wJZM=; b=sJHh3HamqVHwibwpyx0xhOwviuJag6VInkqgRCntRAMih9FKEARXBnqfyXQKbsLhNi HkJKlgP5DSSTM46YuhY8jGaDhTPkgISBANRbQ4IJ4FPWsldjObfGu2nArYnYlCeCUvzr tEVBFfFYJkeqFDTICrLj026XhuyB3CBT8/IoyNf2DH/U8YC/MPOEPnNaUtpwZX3TDrtD +ypqEN9QXS16ByS7S/9/tPAkZ0KJVdPNTO8lbf3HCgkav12HVp+oyEsvIop6/J+5vd9G duOgq1KbMrJzcVrhEaz94iPpi0TRLMBPEZd8DAsHmsL5kBFFuC53wc+pNhnWC7YsmN0/ K+pg== X-Gm-Message-State: AOAM5306R3svNCxfc/8Tei4Fwd3+ExZHZRqrG+/IRLvnu/XvAEH+b0W+ 21iGjLv75G7stdM48LxFyXVQiSA+SnfDVQUqz555ug== X-Received: by 2002:aa7:de92:: with SMTP id j18mr10392290edv.141.1630881611812; Sun, 05 Sep 2021 15:40:11 -0700 (PDT) MIME-Version: 1.0 References: <20210826012626.1163705-1-isabellabdoamaral@usp.br> <20210826012626.1163705-3-isabellabdoamaral@usp.br> In-Reply-To: From: Isabella B do Amaral Date: Sun, 5 Sep 2021 19:40:00 -0300 Message-ID: Subject: Re: [PATCH 2/6] test_hash.c: move common definitions to top of file To: Marco Elver Cc: geert@linux-m68k.org, Enzo Ferreira , =?UTF-8?Q?Augusto_Dur=C3=A3es_Camargo?= , Brendan Higgins , Daniel Latypov , David Gow , linux-kselftest@vger.kernel.org, linux-kernel@vger.kernel.org, kunit-dev@googlegroups.com, ~lkcamp/patches@lists.sr.ht, rodrigosiqueiramelo@gmail.com Content-Type: text/plain; charset="UTF-8" Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Hello, Marco, On Thu, Aug 26, 2021 at 11:36 AM Marco Elver wrote: > > On Thu, 26 Aug 2021 at 03:26, 'Isabella Basso' via KUnit Development > wrote: > > Keep function signatures minimal by making common definitions static. > > This does not change any behavior. > > This seems like an odd change; if I read it right it's changing the > out-param passed to test_int_hash() to simply be static globals. > > For one, it makes the code harder to read because now test_int_hash() > is no longer "pure" (no global side-effects ... modulo printfs), and > what was previously an out-param, is now a global. > > Unfortunately this is poor style and likely to lead to hard-to-debug > problems. One such problem is if suddenly you have multiple threads > involved. While this is just a test and unlikely to be a problem, I > would recommend not introducing global state carelessly. I see. My peers at LKCamp and I talked over the thread-safety problems for a while but we concluded it wasn't a big deal (precisely because this is a test). Though being stylistically poor seems a huge heads up, so I'm really thankful for your thorough explanation(, and review)! Noted! :) > An alternative common idiom, where a set of variables are always > passed around to other functions, is to introduce a struct and pass a > pointer to it along. > > > Signed-off-by: Isabella Basso > > --- > > lib/test_hash.c | 13 ++++++++----- > > 1 file changed, 8 insertions(+), 5 deletions(-) > > > > diff --git a/lib/test_hash.c b/lib/test_hash.c > > index d4b0cfdb0377..8bcc645a7294 100644 > > --- a/lib/test_hash.c > > +++ b/lib/test_hash.c > > @@ -23,6 +23,11 @@ > > #include > > #include > > > > +#define SIZE 256 /* Run time is cubic in SIZE */ > > + > > +static u32 string_or; /* stores or-ed string output */ > > +static u32 hash_or[2][33] = { { 0, } }; /* stores or-ed hash output */ > > These now use up memory for as long as this module is loaded, vs. > before where it would only use up stack space. (For a test that's not > a problem, but in non-test code it might.) > > > /* 32-bit XORSHIFT generator. Seed must not be zero. */ > > static u32 __init __attribute_const__ > > xorshift(u32 seed) > > @@ -66,7 +71,7 @@ fill_buf(char *buf, size_t len, u32 seed) > > * recompile and re-test the module without rebooting. > > */ > > static bool __init > > -test_int_hash(unsigned long long h64, u32 hash_or[2][33]) > > +test_int_hash(unsigned long long h64) > > { > > int k; > > u32 h0 = (u32)h64, h1, h2; > > @@ -123,17 +128,15 @@ test_int_hash(unsigned long long h64, u32 hash_or[2][33]) > > return true; > > } > > > > -#define SIZE 256 /* Run time is cubic in SIZE */ > > - > > static int __init > > test_hash_init(void) > > { > > char buf[SIZE+1]; > > - u32 string_or = 0, hash_or[2][33] = { { 0, } }; > > unsigned tests = 0; > > unsigned long long h64 = 0; > > int i, j; > > > > + string_or = 0; > > That's another problem with changes like this; now the compiler has no > chance to warn you in case the variable is not initialized correctly. > > Also, I don't see string_or used anywhere else. Why make it global? > If a later change would require that, it should say so in the commit > message. But my guess is you can avoid all that by bundling everything > up in a struct. > > > fill_buf(buf, SIZE, 1); > > > > /* Test every possible non-empty substring in the buffer. */ > > @@ -161,7 +164,7 @@ test_hash_init(void) > > > > string_or |= h0; > > h64 = h64 << 32 | h0; /* For use with hash_64 */ > > - if (!test_int_hash(h64, hash_or)) > > + if (!test_int_hash(h64)) > > return -EINVAL; > > tests++; > > } /* i */ > > -- > > 2.33.0 Thanks, -- Isabella Basso