From patchwork Tue Sep 17 14:34:02 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Boqun Feng X-Patchwork-Id: 13806224 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 1E686EE6426 for ; Tue, 17 Sep 2024 14:35:10 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 8B4096B008C; Tue, 17 Sep 2024 10:35:04 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 865226B0092; Tue, 17 Sep 2024 10:35:04 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 6673B6B0093; Tue, 17 Sep 2024 10:35:04 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id 4206E6B008C for ; Tue, 17 Sep 2024 10:35:04 -0400 (EDT) Received: from smtpin28.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id 04FB0C04E3 for ; Tue, 17 Sep 2024 14:35:03 +0000 (UTC) X-FDA: 82574477328.28.951416F Received: from mail-qv1-f52.google.com (mail-qv1-f52.google.com [209.85.219.52]) by imf06.hostedemail.com (Postfix) with ESMTP id C7FAE18000A for ; Tue, 17 Sep 2024 14:35:01 +0000 (UTC) Authentication-Results: imf06.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=Sny00JNC; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf06.hostedemail.com: domain of boqun.feng@gmail.com designates 209.85.219.52 as permitted sender) smtp.mailfrom=boqun.feng@gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1726583670; a=rsa-sha256; cv=none; b=zbQebcFvh3yGmBLaNYldenVEoIyfQeU7a6kHqOtQyW6NYXcepcXkXUJqEIg4bBEPQpCFFx XEBXfdn8aNLN0ltstOnzNooY59/6IdWq8powpMc4z0PNCG+zOBBTGNV2e0EDzw2Zzg4vn5 cJLDH8MaCj9OOYsAIZ/jTjwL9vRddRo= ARC-Authentication-Results: i=1; imf06.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=Sny00JNC; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf06.hostedemail.com: domain of boqun.feng@gmail.com designates 209.85.219.52 as permitted sender) smtp.mailfrom=boqun.feng@gmail.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1726583670; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=cLQI6kZ6TnjcLlPN0cihMK1F/E38o+9g1wKleRuuDyM=; b=5iWUO35aKlQ3DgDOkgZJj/rNV+aIFKau+BzacceShFIclIFGQv3PhfnoRpD0CSmRIjubah U9ZIARBfWlo1pv0/ZKl89zNE5SPU8hWERWSGV8kUXDMmWjCLQ/Clmt+YbFvHElha93p6np LPFh3FqNQgKlb86kKqrz2+fGMjdz6eI= Received: by mail-qv1-f52.google.com with SMTP id 6a1803df08f44-6c579748bf4so44300056d6.1 for ; Tue, 17 Sep 2024 07:35:01 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1726583701; x=1727188501; darn=kvack.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:feedback-id:from:to:cc:subject :date:message-id:reply-to; bh=cLQI6kZ6TnjcLlPN0cihMK1F/E38o+9g1wKleRuuDyM=; b=Sny00JNCOETEzrwNHbbT4CwRom7E7chzTKEgUN0cSNIXs6xWtCj7uSvsJOKGHfrdcg ou7HhQPEvLvPs7d9EcAZ5zu0iRMCg3HcqxoI5GajbnYNoUeo4Wpdt4y3fYF2drgqVYY4 8i/DnMkm4VQWj+K7bVdzoIiGP7pr5SFxcNiDVlqRC78fGDZAr5rbkG337/xBIeEzl/1h g4OtItwPodrosDwCbASXiMHETj20kYMEukiVDiangyPPCyC4Q0Mo+bQPwUkpQtU4Ff/L vDKSi0TaEngE20nqgHxlEJjM5PVa+T59+kbUaGewjtl4ErE9WevjstLCV5hOqCqIxE5A RkZg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1726583701; x=1727188501; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:feedback-id:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=cLQI6kZ6TnjcLlPN0cihMK1F/E38o+9g1wKleRuuDyM=; b=G2rc+tAieaSRnL/J90Zqau8R5DCleI0N8hJqfQj9L56loCWnpG9e6r2xkkGprVwc/h l/O4OzF+RvqtUKhkRcyH48ehZQ2o8Jtau+T8nnT/d7WIrF3fNLFVEacXHwpc1BhQ58q5 K/+01epvQJpIQ4msDoHrekAM5HEzoe/yxryVJVqpVk0WZUvztu5bDTDdMHTlBeDYJg8g MF4wSyYU04hnFLkqTSW6vzYCcK3vnQTcPVE9svlrFV6VEQp/Zy7QXa1sACotbX/At66s CRI9zESqOLpQjeiML0co/T2s5xIJrURv0Q+cORLdYq5+9wt96Pmh4UDXz2GECypM7Zsu dq+w== X-Forwarded-Encrypted: i=1; AJvYcCWXdRDGFBK2D/j/CVqtrzK+elWKF3RpTDAQ7i9NiQK+3yY1gbeUfvCXQ7og6iXJsNlsPm+fDfniWw==@kvack.org X-Gm-Message-State: AOJu0YwAB+JgHhEPbX8PSUfq8zsa3z3C0QNUE7hfkv+0JoljxAj1jTu+ GtYcw9NTFbZjyA9gbF+bjvbvClsxGjU3m2QhSLDMiYTguIAkrXhE X-Google-Smtp-Source: AGHT+IHxAlrf3264M1eG0sU+WYQOpz1SeCGp6P57WflWDJF78YqG3aNM3gQ3OugWPUyeVTVmITSVWQ== X-Received: by 2002:a05:6214:440b:b0:6c3:62bc:5de3 with SMTP id 6a1803df08f44-6c57e240938mr223177656d6.53.1726583700929; Tue, 17 Sep 2024 07:35:00 -0700 (PDT) Received: from fauth1-smtp.messagingengine.com (fauth1-smtp.messagingengine.com. [103.168.172.200]) by smtp.gmail.com with ESMTPSA id 6a1803df08f44-6c58c645d27sm35018466d6.73.2024.09.17.07.35.00 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 17 Sep 2024 07:35:00 -0700 (PDT) Received: from phl-compute-03.internal (phl-compute-03.phl.internal [10.202.2.43]) by mailfauth.phl.internal (Postfix) with ESMTP id F227C1200070; Tue, 17 Sep 2024 10:34:59 -0400 (EDT) Received: from phl-mailfrontend-02 ([10.202.2.163]) by phl-compute-03.internal (MEProxy); Tue, 17 Sep 2024 10:34:59 -0400 X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeeftddrudekjedgjeelucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdggtfgfnhhsuhgsshgtrhhisggvpdfu rfetoffkrfgpnffqhgenuceurghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnh htshculddquddttddmnecujfgurhephffvvefufffkofgjfhgggfestdekredtredttden ucfhrhhomhepuehoqhhunhcuhfgvnhhguceosghoqhhunhdrfhgvnhhgsehgmhgrihhlrd gtohhmqeenucggtffrrghtthgvrhhnpeegleejiedthedvheeggfejveefjeejkefgveff ieeujefhueeigfegueehgeeggfenucevlhhushhtvghrufhiiigvpedtnecurfgrrhgrmh epmhgrihhlfhhrohhmpegsohhquhhnodhmvghsmhhtphgruhhthhhpvghrshhonhgrlhhi thihqdeiledvgeehtdeigedqudejjeekheehhedvqdgsohhquhhnrdhfvghngheppehgmh grihhlrdgtohhmsehfihigmhgvrdhnrghmvgdpnhgspghrtghpthhtohepvdehpdhmohgu vgepshhmthhpohhuthdprhgtphhtthhopehlihhnuhigqdhkvghrnhgvlhesvhhgvghrrd hkvghrnhgvlhdrohhrghdprhgtphhtthhopehrtghusehvghgvrhdrkhgvrhhnvghlrdho rhhgpdhrtghpthhtoheplhhinhhugidqmhhmsehkvhgrtghkrdhorhhgpdhrtghpthhtoh eplhhkmhhmsehvghgvrhdrkhgvrhhnvghlrdhorhhgpdhrtghpthhtohepphgruhhlmhgt kheskhgvrhhnvghlrdhorhhgpdhrtghpthhtohepfhhrvgguvghrihgtsehkvghrnhgvlh drohhrghdprhgtphhtthhopehnvggvrhgrjhdruhhprgguhhihrgihsehkvghrnhgvlhdr ohhrghdprhgtphhtthhopehjohgvlhesjhhovghlfhgvrhhnrghnuggvshdrohhrghdprh gtphhtthhopehjohhshhesjhhoshhhthhrihhplhgvthhtrdhorhhg X-ME-Proxy: Feedback-ID: iad51458e:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Tue, 17 Sep 2024 10:34:59 -0400 (EDT) From: Boqun Feng To: linux-kernel@vger.kernel.org, rcu@vger.kernel.org, linux-mm@kvack.org, lkmm@vger.kernel.org Cc: "Paul E. McKenney" , Frederic Weisbecker , Neeraj Upadhyay , Joel Fernandes , Josh Triplett , Boqun Feng , Uladzislau Rezki , Steven Rostedt , Mathieu Desnoyers , Lai Jiangshan , Zqiang , Peter Zijlstra , Ingo Molnar , Will Deacon , Waiman Long , Mark Rutland , Thomas Gleixner , Kent Overstreet , Linus Torvalds , Vlastimil Babka , maged.michael@gmail.com Subject: [RFC PATCH 4/4] WIP: hazptr: Add hazptr test sample Date: Tue, 17 Sep 2024 07:34:02 -0700 Message-ID: <20240917143402.930114-5-boqun.feng@gmail.com> X-Mailer: git-send-email 2.45.2 In-Reply-To: <20240917143402.930114-1-boqun.feng@gmail.com> References: <20240917143402.930114-1-boqun.feng@gmail.com> MIME-Version: 1.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jbYAgNJw1j7AbSUvA/Xf8KnNEhsKPV nqVDJdgj zeJfONqb41H0PiwjB4w3IeEYHt2mk3vY4/n5KIMfyv91A7a6/pkK5sxZIRLRrWxQsuhMIRcMqrZ0WEPqUGpPsBp/3wnSmIhBcWKhqGMlZEFl394fZ+FwhmPlBGHfIUazN037/NYJz0uPiq6gelwyxAJMOOgV7JTfTWO7cAAmlL6rv1ePSgVonXaylyOHlnpouxWWDZQzgsGpMNM+KTmpFfyDTX6UlbW2KN2KvxAT5STon4pq6kiWUcW7+U+O0RoW+EcH0rHWVx54qYddPVr7gr7quXACoQbJnP50A+QMadc3MlXSU8l9yCcnVKdQx2HYQRN+2fTr3n7zVBEvEOOhBmwPQOvWRyKi1FX28bGY+GwfmcBg/RzabG/0j0/18mm5gXSpJeap0GL/k0feKj2H7klDLtfMbfj0CFJmto/dJ+Kx8zjI2IgXLdkufYKK9vIavj1Ar X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: Sample code for hazptr. This should go away or get more polished when hazptr support is added into rcutorture. Signed-off-by: Boqun Feng --- samples/Kconfig | 6 +++ samples/Makefile | 1 + samples/hazptr/hazptr_test.c | 87 ++++++++++++++++++++++++++++++++++++ 3 files changed, 94 insertions(+) create mode 100644 samples/hazptr/hazptr_test.c diff --git a/samples/Kconfig b/samples/Kconfig index b288d9991d27..9b42cde35dca 100644 --- a/samples/Kconfig +++ b/samples/Kconfig @@ -293,6 +293,12 @@ config SAMPLE_CGROUP source "samples/rust/Kconfig" +config SAMPLE_HAZPTR + bool "Build hazptr sample code" + help + Build samples that shows hazard pointer usage. Currently only + builtin usage is supported. + endif # SAMPLES config HAVE_SAMPLE_FTRACE_DIRECT diff --git a/samples/Makefile b/samples/Makefile index b85fa64390c5..0be21edc8a30 100644 --- a/samples/Makefile +++ b/samples/Makefile @@ -39,3 +39,4 @@ obj-$(CONFIG_SAMPLE_KMEMLEAK) += kmemleak/ obj-$(CONFIG_SAMPLE_CORESIGHT_SYSCFG) += coresight/ obj-$(CONFIG_SAMPLE_FPROBE) += fprobe/ obj-$(CONFIG_SAMPLES_RUST) += rust/ +obj-$(CONFIG_SAMPLE_HAZPTR) += hazptr/ diff --git a/samples/hazptr/hazptr_test.c b/samples/hazptr/hazptr_test.c new file mode 100644 index 000000000000..3cf0cdc8a83a --- /dev/null +++ b/samples/hazptr/hazptr_test.c @@ -0,0 +1,87 @@ +#include +#include +#include + +struct foo { + int i; + struct callback_head head; +}; + +static void simple_func(struct callback_head *head) +{ + struct foo *ptr = container_of(head, struct foo, head); + + printk("callback called %px, i is %d\n", ptr, ptr->i); + kfree(ptr); +} + +static void simple(void) +{ + struct hazptr_context ctx; + struct foo *dummy, *tmp, *other; + hazptr_t *hptr; + hazptr_t *hptr2; + + dummy = kzalloc(sizeof(*dummy), GFP_KERNEL); + dummy->i = 42; + + other = kzalloc(sizeof(*dummy), GFP_KERNEL); + other->i = 43; + + if (!dummy || !other) { + printk("allocation failed, skip test\n"); + return; + } + + init_hazptr_context(&ctx); + hptr = hazptr_alloc(&ctx); + BUG_ON(!hptr); + + // Get a second hptr. + hptr2 = hazptr_alloc(&ctx); + BUG_ON(!hptr2); + + // No one is modifying 'dummy', protection must succeed. + BUG_ON(!hazptr_tryprotect(hptr, dummy, head)); + + // Simulate changing a global pointer. + tmp = dummy; + WRITE_ONCE(dummy, other); + + // Callback will run after no active readers. + printk("callback added, %px\n", tmp); + + call_hazptr(&tmp->head, simple_func); + + // No one is modifying 'dummy', protection must succeed. + tmp = hazptr_protect(hptr2, dummy, head); + + printk("reader2 got %px, i is %d\n", tmp, tmp->i); + + // The above callback should run after this. + hazptr_clear(hptr); + printk("first reader is out\n"); + + for (int i = 0; i < 10; i++) + schedule(); // yield a few times. + + // Simulate freeing a global pointer. + tmp = dummy; + WRITE_ONCE(dummy, NULL); + printk("callback added, %px\n", tmp); + call_hazptr(&tmp->head, simple_func); + + cleanup_hazptr_context(&ctx); + printk("no reader here\n"); + + for (int i = 0; i < 10; i++) + schedule(); // yield a few times. +} + +static int hazptr_test(void) +{ + simple(); + printk("test ends\n"); + return 0; +} +module_init(hazptr_test);