Received: by 2002:a25:ef43:0:0:0:0:0 with SMTP id w3csp690722ybm; Thu, 28 May 2020 12:42:33 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzJByxE1cbDABXyg68v9w9zc4B6OC0hyf44eNKK1/rwuo69BnK+vhNEX26t2R3LNk/nVHMs X-Received: by 2002:a17:906:2e03:: with SMTP id n3mr4581492eji.424.1590694953141; Thu, 28 May 2020 12:42:33 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1590694953; cv=none; d=google.com; s=arc-20160816; b=JuUzaalMs7JDWpTg/RlxW8xy34j5HkukGE6NDQ5tggBbsP7CPgO5zdIQv8mguugO3l q3cTvSMgi8gRuhCpov5NIw7Fh2AWLI8DWHxfwaOHISuKI6W9wkrMme+NSxT5czAkv1XI VPn4iqQKLO4YXBZJyXv/HXczQYEU65hoPTIC1xLo17lYlc8MS2AdsXum/QpRHL6jSszR SxL/SMeCIrA3ztLRdgqTePOIU2GeMCkS/jzX7W6xc8jzePGt71lbNDx8rDSonl6Mb6mC rKA6UdkG/p8/VO0DG2g1Ekpr+3rUZSWUETq1LncHqxOjlA7u9pr9AxVj2jmuIbxyZ+Fu xXeA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:dkim-signature; bh=1zYINYS5M5H4v7usoMXZUPWVFvds+U+lV2Ynob3L6pg=; b=GqELEkNd6h7+Lwkp1q1hx0EPQrqgpSVfUeQgr5GRzNjUPIW7CdOOmdM/I+UwzV9/ow dJiQYs81p3Li+KLNvHVu9slhVRhBAq0YD96p8awZv31lDECYz5JNR/n3PqtNIWNt1OTz diN19vdU2E6wM3xBZ9Kd0Bc8e3OhIkTrAZbyjv0EsTvp1H9d/AuaqgKKz7OqUlfP8TZK GIqfG7omFPBwvdC40NqkYa4IpQxH91LZUznj3373/rdfq301lON4rozj8zn3AW65re9b R3R2bvn/RfFV3ZXq9FBAAmZpA+jK5NX0X1OLGOT36ogtT5ZmvkSw7KEJWWgPSG5MeTTE 71YA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=FoXMQP8T; 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=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id jp7si4561869ejb.617.2020.05.28.12.42.09; Thu, 28 May 2020 12:42:33 -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=@google.com header.s=20161025 header.b=FoXMQP8T; 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=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2406649AbgE1TkV (ORCPT + 99 others); Thu, 28 May 2020 15:40:21 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34164 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2406540AbgE1TkO (ORCPT ); Thu, 28 May 2020 15:40:14 -0400 Received: from mail-pf1-x441.google.com (mail-pf1-x441.google.com [IPv6:2607:f8b0:4864:20::441]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 86193C08C5C7 for ; Thu, 28 May 2020 12:40:13 -0700 (PDT) Received: by mail-pf1-x441.google.com with SMTP id e11so13122893pfn.3 for ; Thu, 28 May 2020 12:40:13 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=1zYINYS5M5H4v7usoMXZUPWVFvds+U+lV2Ynob3L6pg=; b=FoXMQP8TGdKIsTYwBlxJfrVj2VQ8RR/kNIgT4CxA3KobyKEU1+//9t64t3OLh2Khi+ 3vuUcAJFpfayXZesLB0YySRHjZz50yewdX78MfGCTUDUym7HAZ/sEZgGqKUO+F7X7Pk6 taRil4k2kvl5UYMcHl7NDrUI1mIZMJjxCQ465VJHxLnrPICiYv/Vi0UXic4hHDqRAvfW gqZcYlc3sXu8BEQeytCGnQA807rsOddGVE0d5t1iPEZNc8/uur3Vfn0e9Z/XuQahAovq mNxpuC8YNyLKpRRdmmkZkpSmwmMKkOH51NN/AI6z+GhLENAmi5i0h6GvVVzSD/0cGpFf 5G/Q== 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=1zYINYS5M5H4v7usoMXZUPWVFvds+U+lV2Ynob3L6pg=; b=HSUg7NRh2nEyybZ5LG4tXtyNHfv7xozncFSfXFFb7FQSW3vDf+uGm5ujnllTMk76pA TR8k1tnSM9MpqTwJ5PZwSvtSA/UruB8iD8/Jn+8FZDov1nTAILfU+QIJjQ+4i3/7bSZz +7nonrRePj3vZ849C7+qSUscGERzwcEnjssKw9ycc6Tz2UPVdN9aWJ/3hWyBKVR7lBTJ ptvF2Xs1SKIjshgMW2mrUErPIslTgJr8TpfIwhrzn+dbW+KxYDwshfojK2XnoW675BBu B9i2csKLILmOMeweLhlIgyLV4d45KpxOc/4v1m/1R3jqY9gx8F70a2W5/8UrkQFGM5vu mT9g== X-Gm-Message-State: AOAM533DRoaLyUT9g7c/ef+Tm8UkOQXhzC7ghFCnp3GdZwRGEToiD6kO bdTYJjmhX21BymLs2E6LME9WJ61FmOcc0loH8FyKCQ== X-Received: by 2002:a62:8c42:: with SMTP id m63mr4710270pfd.106.1590694812613; Thu, 28 May 2020 12:40:12 -0700 (PDT) MIME-Version: 1.0 References: <1585313122-26441-1-git-send-email-alan.maguire@oracle.com> <1585313122-26441-2-git-send-email-alan.maguire@oracle.com> In-Reply-To: <1585313122-26441-2-git-send-email-alan.maguire@oracle.com> From: Brendan Higgins Date: Thu, 28 May 2020 12:40:01 -0700 Message-ID: Subject: Re: [PATCH v3 kunit-next 1/2] kunit: generalize kunit_resource API beyond allocated resources To: Alan Maguire Cc: shuah , Patricia Alfonso , "open list:KERNEL SELFTEST FRAMEWORK" , KUnit Development , Linux Kernel Mailing List Content-Type: text/plain; charset="UTF-8" Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Fri, Mar 27, 2020 at 5:45 AM Alan Maguire wrote: > > In its original form, the kunit resources API - consisting the > struct kunit_resource and associated functions - was focused on > adding allocated resources during test operation that would be > automatically cleaned up on test completion. > > The recent RFC patch proposing converting KASAN tests to KUnit [1] > showed another potential model - where outside of test context, > but with a pointer to the test state, we wish to access/update > test-related data, but expressly want to avoid allocations. > > It turns out we can generalize the kunit_resource to support > static resources where the struct kunit_resource * is passed > in and initialized for us. As part of this work, we also > change the "allocation" field to the more general "data" name, > as instead of associating an allocation, we can associate a > pointer to static data. Static data is distinguished by a NULL > free functions. A test is added to cover using kunit_add_resource() > with a static resource and data. > > Finally we also make use of the kernel's krefcount interfaces > to manage reference counting of KUnit resources. The motivation > for this is simple; if we have kernel threads accessing and > using resources (say via kunit_find_resource()) we need to > ensure we do not remove said resources (or indeed free them > if they were dynamically allocated) until the reference count > reaches zero. A new function - kunit_put_resource() - is > added to handle this, and it should be called after a > thread using kunit_find_resource() is finished with the > retrieved resource. > > We ensure that the functions needed to look up, use and > drop reference count are "static inline"-defined so that > they can be used by builtin code as well as modules in > the case that KUnit is built as a module. > > A cosmetic change here also; I've tried moving to > kunit_[action]_resource() as the format of function names > for consistency and readability. > > [1] https://lkml.org/lkml/2020/2/26/1286 > > Signed-off-by: Alan Maguire One comment below, other than that: Reviewed-by: Brendan Higgins Sorry for not keeping up with this. I forgot that I didn't give this a reviewed-by. > --- > include/kunit/test.h | 157 +++++++++++++++++++++++++++++++++++++--------- > lib/kunit/kunit-test.c | 74 ++++++++++++++++------ > lib/kunit/string-stream.c | 14 ++--- > lib/kunit/test.c | 154 ++++++++++++++++++++++++--------------------- > 4 files changed, 270 insertions(+), 129 deletions(-) > > diff --git a/include/kunit/test.h b/include/kunit/test.h > index 9b0c46a..8c7f3ff 100644 > --- a/include/kunit/test.h > +++ b/include/kunit/test.h > @@ -52,30 +59,27 @@ > * > * static void kunit_kmalloc_free(struct kunit_resource *res) > * { > - * kfree(res->allocation); > + * kfree(res->data); > * } > * > * void *kunit_kmalloc(struct kunit *test, size_t size, gfp_t gfp) > * { > * struct kunit_kmalloc_params params; > - * struct kunit_resource *res; > * > * params.size = size; > * params.gfp = gfp; > * > - * res = kunit_alloc_resource(test, kunit_kmalloc_init, > + * return kunit_alloc_resource(test, kunit_kmalloc_init, > * kunit_kmalloc_free, ¶ms); > - * if (res) > - * return res->allocation; > - * > - * return NULL; > * } > */ > struct kunit_resource { > - void *allocation; > - kunit_resource_free_t free; > + void *data; > > /* private: internal use only. */ > + kunit_resource_init_t init; Apologies for bringing this up so late, but it looks like you never addressed my comment from v1: I don't think you use this `init` field anywhere; I only see it passed as a parameter. Is there something obvious that I am missing? > + kunit_resource_free_t free; > + struct kref refcount; > struct list_head node; > };