From patchwork Wed Mar 20 14:54:03 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kuan-Wei Chiu X-Patchwork-Id: 13597938 X-Patchwork-Delegate: snitzer@redhat.com Received: from mail-pg1-f174.google.com (mail-pg1-f174.google.com [209.85.215.174]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 650EB4879E for ; Wed, 20 Mar 2024 14:54:30 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.215.174 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1710946472; cv=none; b=HlaUtiWvqPP+4rvTnbQ/kSPmcmg3H5RTIs9FcOfAUahMZKbN6kBh9w/l2YEl8CNNPONpLxuxa8wYCRv1b2Y/wA8B7V4UpVWoooiPTB21eYDIawbVyvWo25ZQB8ezXvFUXp7qvyVcsTZDZkOU/afCj9YWBA4aRrHJht1PRw4hsYM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1710946472; c=relaxed/simple; bh=4n3PXoq7ifTVripzMWtTfmPNaw+I1mTBtc6mbpGaWSs=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=OFbBt3lVSShgh4uY/peqcnInKqbuYKSAFB3Q/l1qOJ3Nz9vbXsVo2/lI0zisvW4cRHkzi09YY/9mdMFLtRP4cZycSLaSWvdBEw8QXjPYod3yTy04g67aZf6Vo9H+878/g1OO+aMaabmCKtiS+cII9CMO3Ub5n1XegB0w5ufUwmc= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=MNmms5Cb; arc=none smtp.client-ip=209.85.215.174 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="MNmms5Cb" Received: by mail-pg1-f174.google.com with SMTP id 41be03b00d2f7-5cfcf509fbdso1038243a12.1 for ; Wed, 20 Mar 2024 07:54:30 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1710946470; x=1711551270; darn=lists.linux.dev; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=QdmOvG2jwhJ+3WEh/LmFh3bdry4XeMTeGj+wdIbS7uw=; b=MNmms5CbslRsQEufdrJtQa1OfWIzrEtuXoiUba/z+++0+tDNTHpc+kVcn21zpKZEWf ckUcKAp/J4ifAn9Ykr/PZiziME64MOYcJ660venYE/OYOtuOPff4C2TzCi8adl0cFAJW 7lA08VZNyq6GlWOCxfQZCssfMUazfXlq3KRm/lq7iOQVGdVYtUUUxXRBTLkRO240rfej 8ouf6K3BTQAKZ7QmfjRI69pj1HmrKEO15+6l1c50qb4bR3bN4RnF6LDLn+zo/y0ybq6p jVT/lYyi+5lYfgA0Szi6s9U99GPHT97aNdVo3dPLlHKaJLDw1kMyRqY9KGrha1GkXVi+ fnlQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1710946470; x=1711551270; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=QdmOvG2jwhJ+3WEh/LmFh3bdry4XeMTeGj+wdIbS7uw=; b=pnzFRouhv2ZvquK5kE2IuhV6Qlizl1odKKRo3GT00DWYc2epMW18oBVqJzdOGiaasH HrZnVVkhkk43Y7i1qEvEaS6DBYKJ1O1P2zU9w7iVkek8v6V3mOLXHCmeysxIBMvgFfoH hi2x8dZk1QRYCJQazk8Cb0DvzTbHjQRwcN+ygLLfAPLOgdM9eBh5O4fx9MBQrhanRmO+ qIwWyiXF94h6pGuQwx+ytDXTAmyp4MIPYal0Jo9ELOHhRtfB3+Q2qMvA3azg5zcDkVIr zuMyudyXJfi6yjAwQMUgQA9JStuLNyppOh6klAcxg67Fd9n7p0pANt2u4qbTDT3DSkr3 6wcg== X-Forwarded-Encrypted: i=1; AJvYcCUVqUMYj3IoMV1SCeP7hZgnJx9M9GStXMZmJ4LRP4i8SE8Ldg+phMiVuAeKLBMeMXyMFvgHCo/KAs+wJfwkHarSFAyzUnRGvk0= X-Gm-Message-State: AOJu0YzAmk/Hz6ofNeXUUgD+tVjIFbr2TRNdry1zWG+GG0CG4wcDQnig 5mE+q3C+tjjuPzNnqYR2dYXWGgUH+WzGtVmAWcPruoxhkQqS5YE0 X-Google-Smtp-Source: AGHT+IEmpQfc+LEIDAEcR53CezqUQZscGcnrsPciIpHwU9PJHh1fb/sTR++utbSjALwem23U0uwdmw== X-Received: by 2002:a17:902:d2ca:b0:1df:fbc3:d130 with SMTP id n10-20020a170902d2ca00b001dffbc3d130mr1970515plc.1.1710946469727; Wed, 20 Mar 2024 07:54:29 -0700 (PDT) Received: from visitorckw-System-Product-Name.. ([140.113.216.168]) by smtp.gmail.com with ESMTPSA id la11-20020a170902fa0b00b001dc30f13e6asm13719989plb.137.2024.03.20.07.54.26 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 20 Mar 2024 07:54:29 -0700 (PDT) From: Kuan-Wei Chiu To: colyli@suse.de, kent.overstreet@linux.dev, msakai@redhat.com, peterz@infradead.org, mingo@redhat.com, acme@kernel.org, namhyung@kernel.org, akpm@linux-foundation.org Cc: bfoster@redhat.com, mark.rutland@arm.com, alexander.shishkin@linux.intel.com, jolsa@kernel.org, irogers@google.com, adrian.hunter@intel.com, jserv@ccns.ncku.edu.tw, dm-devel@lists.linux.dev, linux-bcache@vger.kernel.org, linux-kernel@vger.kernel.org, linux-bcachefs@vger.kernel.org, linux-perf-users@vger.kernel.org, Kuan-Wei Chiu Subject: [PATCH v2 01/15] perf/core: Fix several typos Date: Wed, 20 Mar 2024 22:54:03 +0800 Message-Id: <20240320145417.336208-2-visitorckw@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240320145417.336208-1-visitorckw@gmail.com> References: <20240320145417.336208-1-visitorckw@gmail.com> Precedence: bulk X-Mailing-List: dm-devel@lists.linux.dev List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Replace 'artifically' with 'artificially'. Replace 'irrespecive' with 'irrespective'. Replace 'futher' with 'further'. Replace 'sufficent' with 'sufficient'. Signed-off-by: Kuan-Wei Chiu Reviewed-by: Ian Rogers --- kernel/events/core.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/kernel/events/core.c b/kernel/events/core.c index 724e6d7e128f..10ac2db83f14 100644 --- a/kernel/events/core.c +++ b/kernel/events/core.c @@ -534,7 +534,7 @@ void perf_sample_event_took(u64 sample_len_ns) __this_cpu_write(running_sample_length, running_len); /* - * Note: this will be biased artifically low until we have + * Note: this will be biased artificially low until we have * seen NR_ACCUMULATED_SAMPLES. Doing it this way keeps us * from having to maintain a count. */ @@ -596,10 +596,10 @@ static inline u64 perf_event_clock(struct perf_event *event) * * Event groups make things a little more complicated, but not terribly so. The * rules for a group are that if the group leader is OFF the entire group is - * OFF, irrespecive of what the group member states are. This results in + * OFF, irrespective of what the group member states are. This results in * __perf_effective_state(). * - * A futher ramification is that when a group leader flips between OFF and + * A further ramification is that when a group leader flips between OFF and * !OFF, we need to update all group member times. * * @@ -891,7 +891,7 @@ static int perf_cgroup_ensure_storage(struct perf_event *event, int cpu, heap_size, ret = 0; /* - * Allow storage to have sufficent space for an iterator for each + * Allow storage to have sufficient space for an iterator for each * possibly nested cgroup plus an iterator for events with no cgroup. */ for (heap_size = 1; css; css = css->parent) From patchwork Wed Mar 20 14:54:04 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kuan-Wei Chiu X-Patchwork-Id: 13597939 X-Patchwork-Delegate: snitzer@redhat.com Received: from mail-pl1-f179.google.com (mail-pl1-f179.google.com [209.85.214.179]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 138F351C52 for ; Wed, 20 Mar 2024 14:54:34 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.179 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1710946476; cv=none; b=fM+OcthhDNTy8AqIBKrEefJGM9dpQ+hTkxmzjmmItFd+5Bv6ODHb9GH9BTQdblv7jqyiDfVJeccKTmgEzHqzCBUzJP9DRB9BK0QfKz18v9oza6anQGQ+TLJFCA10xJI4qQSiAwp9ILkbDkDamYWU87DsBc5wczYS5FPQZt8nReg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1710946476; c=relaxed/simple; bh=ccFsqSUbNmoZfjXj/UyIC9+OiMIUuYR/yY6H+xsWda4=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=MCl4J82Lw+nuqDaRHdHk5Ys5PfMVL7O8/UrTnhnke5t+WluDFOjHpVPH8LLMEn1y3YZczjlkERjjuqL6hiPRgc8/DUxbLAQU9473Cio8BDn//A54TuMwqHBRNmkiYFb0ZeNoEnQD7ciADoWFQGHULgWtGDQGJngZghgHgiAXZrg= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=MPj9hNLa; arc=none smtp.client-ip=209.85.214.179 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="MPj9hNLa" Received: by mail-pl1-f179.google.com with SMTP id d9443c01a7336-1dee6672526so6078575ad.1 for ; Wed, 20 Mar 2024 07:54:34 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1710946474; x=1711551274; darn=lists.linux.dev; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=vCMluLf0OPIP5MvhSlBnewMLss6iSf4B08EHRoBbLMI=; b=MPj9hNLawkyXGsOL8N6720Sg7/SG7jsDpDk0Kav47XndVDhVdukRXseKUaA0YAXAfK 9a5qgA7Zt9ercFcci1cRxWGfDEezoFsodR18i5rmMu2lfY5kPmHZ/Hqjg8yI1H+kYPDp Ce6tFWE0/9mKgwcLKrjkTOHPwljO1hjKY84PNEGS8tZYCccKvn47bYur8yShqQSR4m0c +s9r9bcFt4GMirnKr2reOb54qUwGHxxxR54kTIzuvIf/yqCxoBjtFx1P+S+sod2TKQTx muh0Z/vk3AF+Jry8EHswN9oAgLyRqkoELOiSmSl+QSl7lz5/NPEU4+LOVj9oI90820pn /OXg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1710946474; x=1711551274; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=vCMluLf0OPIP5MvhSlBnewMLss6iSf4B08EHRoBbLMI=; b=Z/TILDye5TKICsKzmryFbFgoAf2Yo9xy/1c5cRlGSOBffgcpwydYBM4uMd5L4Qlqal jSdvf4s3oGNc2OUNbNxxZJzgYmJkEvJRLFevb7HVnoR1A3jZ4xEt2oz0kpbEVTG9l6xS inZDwTUpRmQtLCQeG0x7sgQRe0X6R+Mo/UFrXI2bQPA9bOrHMU0BqUcO2Dsrf+JzgK2O 5dg4dJ+4g1UTj46NHlOchOBdbmlXIvdd6r4Uu0Q9wwTJgMhizObt8GmYLTpVPPWCYcdw WC26fe4JH4cPJzY2YECIGH+DpfUrCt7qvvT9cXrIg1kHekA8i6vxHjyYNq7j1Ku2yNW1 dMrg== X-Forwarded-Encrypted: i=1; AJvYcCWU8eAPCER2NhDxIZL2UhZ75U4OxopS7U5dZ1mbNcIiVbUftvMBlEyCQE9QQWtOHWes2T6blkjGqx3tGZrFsl0UsdPzokZbf+k= X-Gm-Message-State: AOJu0YykzfYiOrlxFdJATyLDTxgG0mcSp6shy1X9AOVFvwzZwS6vVE0O +tRjLFZhQUamrW8/QahDio6OnG9dW/hyeG6ODQQSFoAVEELlMTah X-Google-Smtp-Source: AGHT+IEnN4HYZbE2rHcsFmRm8DkhdOasT2m/ZrfFc02i9m2gwbsINOw3gtS3SpqFPr7j1X9xPow9nw== X-Received: by 2002:a17:902:ce91:b0:1dc:df03:ad86 with SMTP id f17-20020a170902ce9100b001dcdf03ad86mr20347085plg.2.1710946474388; Wed, 20 Mar 2024 07:54:34 -0700 (PDT) Received: from visitorckw-System-Product-Name.. ([140.113.216.168]) by smtp.gmail.com with ESMTPSA id la11-20020a170902fa0b00b001dc30f13e6asm13719989plb.137.2024.03.20.07.54.30 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 20 Mar 2024 07:54:33 -0700 (PDT) From: Kuan-Wei Chiu To: colyli@suse.de, kent.overstreet@linux.dev, msakai@redhat.com, peterz@infradead.org, mingo@redhat.com, acme@kernel.org, namhyung@kernel.org, akpm@linux-foundation.org Cc: bfoster@redhat.com, mark.rutland@arm.com, alexander.shishkin@linux.intel.com, jolsa@kernel.org, irogers@google.com, adrian.hunter@intel.com, jserv@ccns.ncku.edu.tw, dm-devel@lists.linux.dev, linux-bcache@vger.kernel.org, linux-kernel@vger.kernel.org, linux-bcachefs@vger.kernel.org, linux-perf-users@vger.kernel.org, Kuan-Wei Chiu Subject: [PATCH v2 02/15] bcache: Fix typo Date: Wed, 20 Mar 2024 22:54:04 +0800 Message-Id: <20240320145417.336208-3-visitorckw@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240320145417.336208-1-visitorckw@gmail.com> References: <20240320145417.336208-1-visitorckw@gmail.com> Precedence: bulk X-Mailing-List: dm-devel@lists.linux.dev List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Replace 'utiility' with 'utility'. Signed-off-by: Kuan-Wei Chiu Reviewed-by: Ian Rogers --- drivers/md/bcache/util.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/drivers/md/bcache/util.c b/drivers/md/bcache/util.c index ae380bc3992e..410d8cb49e50 100644 --- a/drivers/md/bcache/util.c +++ b/drivers/md/bcache/util.c @@ -1,6 +1,6 @@ // SPDX-License-Identifier: GPL-2.0 /* - * random utiility code, for bcache but in theory not specific to bcache + * random utility code, for bcache but in theory not specific to bcache * * Copyright 2010, 2011 Kent Overstreet * Copyright 2012 Google, Inc. From patchwork Wed Mar 20 14:54:05 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kuan-Wei Chiu X-Patchwork-Id: 13597940 X-Patchwork-Delegate: snitzer@redhat.com Received: from mail-pg1-f181.google.com (mail-pg1-f181.google.com [209.85.215.181]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 723F74AEC4 for ; Wed, 20 Mar 2024 14:54:39 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.215.181 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1710946480; cv=none; b=p1rS0XIGMqDhCduWtw2HUyzhty7XNZQUDt8dy8yULK/QAyWmV8yny/H0LQUeN36vfTkAfYdhEYJ53YoSEiZ2xHcHArPUwIqRItCXCpzRFcmb63vxM0b8XAqvNlvXB2LTzC9lkaIwxLUmDK0xBq/6b/eh3jJJXglS0sWCK12Zbro= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1710946480; c=relaxed/simple; bh=06vCmwEx1RrMDZDf+vtsHJ5OiX/iBIhb0hcMuWL3G6A=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=GLrUf4N8+rwS67ASZtsFMak3ugVFPrpFfVViENizl+SHFxrgPjHJYmTZfg5oR8oqE2oJyyvwxCdo6opRrteYCmNZO7c/y1PYxWT4E8rJbVJwuX7yqqRqTF4tCnq+v45635vOKNfkQy0N7AcclKRKBooxXRgMAHhNagaRx1zblbE= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=L4ftmIKZ; arc=none smtp.client-ip=209.85.215.181 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="L4ftmIKZ" Received: by mail-pg1-f181.google.com with SMTP id 41be03b00d2f7-5cfcf509fbdso1038270a12.1 for ; Wed, 20 Mar 2024 07:54:39 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1710946479; x=1711551279; darn=lists.linux.dev; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=NTsQEVNVJaaM77/A8IxlTDaKS3O7Y7L1hXdgoCCnWBw=; b=L4ftmIKZ1CKkxF5EIs0PAInu1WlUCxGHJ7VUlwaJNzXnz2ST5lqDKPEGUo2gNf4wHw kAHUNpIM6TL/oXdDe3VIAWl9dqOwaMKx2An5jan5H5YRkGB4YaoJHj59MvXYl0o13fNS AXnb4EjiC2pJCo5a+w4AKvE8htA7/RhgbM8bgRCAyS8av3jcBQCP06NYkkw/p2iC85Wv p/90wIORqCx9A9pz4drukYd8R81o+TOOW80OJAqrSCsTPqHpjjXbZDJcvxgwSvdbl3Kf AR1IPPyLyui709AohaqhlX/aSn1ke2qvSKGRv50Z8MRlOpNZ7qDZx0L8zQDEJFOD90nv iQpQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1710946479; x=1711551279; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=NTsQEVNVJaaM77/A8IxlTDaKS3O7Y7L1hXdgoCCnWBw=; b=DZvsuMFs+rw6FJAQVocnYOCQGBuuvkzDdVJHcda75fq0nEA2qEMQXL1JpDbaHOSVDr vAN6KMVHEHr5lyOu/mUcKWqrtPq7c+CUCXzuRcQFk6hkPDa92VNHOjbgnZ/v4KxfzqND FOUAfVRipIa11UvA3A8kpmiUYhwE1t1QnCWSjtQrXWUfSWyxcvGOlxCuYqy6/8Gw7goT NtZBSzzPll1HwLiV6mRb6BR7Re9WtCQ6KmriYO3RywhnJt6E6A/RkoDva1m3vwxE6SC4 YMfSQO/+NG+NgFuX4wZjnYHxakotbY+nzzx1w/MHLr+JTK4PItJZY+bNlisQGWMhEo0B T06Q== X-Forwarded-Encrypted: i=1; AJvYcCXIW7lqkVf4LmYu8PAoO8rguM0HoLM/ydKX/ggT4LofEd9YJ9fbOvOsKUcYEKe+aqzK6YoVvtC91RTmAy4pMuRxB6yN+zY9D6M= X-Gm-Message-State: AOJu0YzsC3cCCwDQ34mOoNYvwnbQDos3GFsurnn3bhdPz12A+weEuhnx b0IUoqTz6HkCKmALCNeRWimAyocFxBwpmYM65kzPyku62/0jnDnD X-Google-Smtp-Source: AGHT+IGB07JOeLXyWGxhruoog8ATzipnYUw/ZWM4f99QwxnPadWqwP8bL4PFsFnlu/KaSWs0YM/T5Q== X-Received: by 2002:a17:903:2290:b0:1dd:7350:29f6 with SMTP id b16-20020a170903229000b001dd735029f6mr1956561plh.3.1710946478818; Wed, 20 Mar 2024 07:54:38 -0700 (PDT) Received: from visitorckw-System-Product-Name.. ([140.113.216.168]) by smtp.gmail.com with ESMTPSA id la11-20020a170902fa0b00b001dc30f13e6asm13719989plb.137.2024.03.20.07.54.35 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 20 Mar 2024 07:54:38 -0700 (PDT) From: Kuan-Wei Chiu To: colyli@suse.de, kent.overstreet@linux.dev, msakai@redhat.com, peterz@infradead.org, mingo@redhat.com, acme@kernel.org, namhyung@kernel.org, akpm@linux-foundation.org Cc: bfoster@redhat.com, mark.rutland@arm.com, alexander.shishkin@linux.intel.com, jolsa@kernel.org, irogers@google.com, adrian.hunter@intel.com, jserv@ccns.ncku.edu.tw, dm-devel@lists.linux.dev, linux-bcache@vger.kernel.org, linux-kernel@vger.kernel.org, linux-bcachefs@vger.kernel.org, linux-perf-users@vger.kernel.org, Kuan-Wei Chiu Subject: [PATCH v2 03/15] bcachefs: Fix typo Date: Wed, 20 Mar 2024 22:54:05 +0800 Message-Id: <20240320145417.336208-4-visitorckw@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240320145417.336208-1-visitorckw@gmail.com> References: <20240320145417.336208-1-visitorckw@gmail.com> Precedence: bulk X-Mailing-List: dm-devel@lists.linux.dev List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Replace 'utiility' with 'utility'. Signed-off-by: Kuan-Wei Chiu Reviewed-by: Ian Rogers --- fs/bcachefs/util.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/fs/bcachefs/util.c b/fs/bcachefs/util.c index 216fadf16928..f5a16ad65424 100644 --- a/fs/bcachefs/util.c +++ b/fs/bcachefs/util.c @@ -1,6 +1,6 @@ // SPDX-License-Identifier: GPL-2.0 /* - * random utiility code, for bcache but in theory not specific to bcache + * random utility code, for bcache but in theory not specific to bcache * * Copyright 2010, 2011 Kent Overstreet * Copyright 2012 Google, Inc. From patchwork Wed Mar 20 14:54:06 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kuan-Wei Chiu X-Patchwork-Id: 13597941 X-Patchwork-Delegate: snitzer@redhat.com Received: from mail-pl1-f174.google.com (mail-pl1-f174.google.com [209.85.214.174]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id A86C354BE9 for ; Wed, 20 Mar 2024 14:54:44 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.174 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1710946486; cv=none; b=Oauq7gSYiew42e3kW7L4OY63aTcKJciQXWEXQa4xWri82UtTPQV8hgj0K/8J+D470PUi/xR8Lyql154EwrzLC/L8NWmdUW5hj3rIFpJja1BmZF1r2uaDwMvPB0lxtpTmJrKCLl/YNZDHGBH6qjcPFUdhfi4ilCUF9ZiBVUNQwz0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1710946486; c=relaxed/simple; bh=JaNYkoxO7r1B7Zx5LUOW9tQ9euz0Qs5qGwUUQSB2JLY=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=BOaJxxsSAiePTH6sdLVaVTbmaa++c5qrN0xYBQkeNqqtd/PQNuhMA8jZvXpJIEaT3e+j+Wv445TUokmKmUaslyOB9lu7A3Xqqjsx7GCeTjXCILIbWUH3ozjtMPa5ZAh/aT9fFLfCTHW0K5FqnhG1Y5pSPcAKnuT+We3CVNPFdyU= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=WaFXmMBi; arc=none smtp.client-ip=209.85.214.174 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="WaFXmMBi" Received: by mail-pl1-f174.google.com with SMTP id d9443c01a7336-1dee6672526so6078915ad.1 for ; Wed, 20 Mar 2024 07:54:44 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1710946484; x=1711551284; darn=lists.linux.dev; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=HBm65YX/b6ZbML2YrG4Mac39XWOyxvJJGteRmERwfdo=; b=WaFXmMBiZPgXatxJ9Wt5qPBMEMLE11UrrtXlgxD/0guV0G2q5c05OG7kX5RDWV/gqZ Pl5VH7y21+55w2AYHESSooW1WJjyCrFBH46WIH6HTTmoRGjqOC/mw4VdUcAIpBAR8oLz FV6/xxUUhzjlrvsFzNt3iKaunuuiYXIc+Ubdd5p6+fQkqmv2IBZN2n04GOqv0ktDDCAo VJX/BKI4YNXcqd4R1km7We2dg1TzG0XqvdfarcRVBWt09e+1DBBj41VvGEoCCpV2lEWZ AoeUM1xu0kOZbtDfs1jxIj9I/7PsvG8yWPLXb8wO2XVdmKn4GJdEoye+loBXBGQ5ahTK Fybg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1710946484; x=1711551284; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=HBm65YX/b6ZbML2YrG4Mac39XWOyxvJJGteRmERwfdo=; b=h7FLaYmtuOtKntYZfrVIIUx2Rcu6zGrGshRr0FXk543U3BML2gHXiA8WjuyCg2P4mo r/xLyCOqbsic7B3YM67ztdOGZnJMwb7QrCvPrYdLKkSFmVFqnIe5Np6NhT2TRU5gWP34 1tilDYDYWp2byT52+zasx7zCc+Blyxhf0nZZ0LYwwWIvumve4CZnYdIff9efsn4SdhSE +pi3NL9LE0c9/Pyp/T9rT2rXfZHBNobg92QknO53mehKTOMZ0BAvQSZ07GE2RJjNSJDZ ondjTFqvIOX3/7MIc/515fzsAhyxGnWLK750V98P4LiGuWdvmCOlBCZhE6TfYixaGXnX AAMA== X-Forwarded-Encrypted: i=1; AJvYcCVS/QisAh4JjJQycuE4p/DeMe8jVdNZ6jqVqs+hY1JTrFo8FlBN6ZvMp9VM5Ye/U7jHTu4auQMah4K/xOmUl5WDiIdoSQ8ZK5g= X-Gm-Message-State: AOJu0YyOMVmpS0cOi3RSo2Ir4L50dEq7cS0rfR1hAHVglPmt5Fh1iF2O frQhU9dDv7rrzUK1HFkKeicsEghntRT+w2wCBkh+jJsL3BFTBgG1 X-Google-Smtp-Source: AGHT+IF2ugLrdSX290JiJEAWYeGRM2GOW4+e4oc8SAkBFLEF98k0SODvSVruRzVRXRHs1WGkp0VQXg== X-Received: by 2002:a17:902:ea03:b0:1dd:a3d6:3aff with SMTP id s3-20020a170902ea0300b001dda3d63affmr20470236plg.3.1710946483685; Wed, 20 Mar 2024 07:54:43 -0700 (PDT) Received: from visitorckw-System-Product-Name.. ([140.113.216.168]) by smtp.gmail.com with ESMTPSA id la11-20020a170902fa0b00b001dc30f13e6asm13719989plb.137.2024.03.20.07.54.40 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 20 Mar 2024 07:54:43 -0700 (PDT) From: Kuan-Wei Chiu To: colyli@suse.de, kent.overstreet@linux.dev, msakai@redhat.com, peterz@infradead.org, mingo@redhat.com, acme@kernel.org, namhyung@kernel.org, akpm@linux-foundation.org Cc: bfoster@redhat.com, mark.rutland@arm.com, alexander.shishkin@linux.intel.com, jolsa@kernel.org, irogers@google.com, adrian.hunter@intel.com, jserv@ccns.ncku.edu.tw, dm-devel@lists.linux.dev, linux-bcache@vger.kernel.org, linux-kernel@vger.kernel.org, linux-bcachefs@vger.kernel.org, linux-perf-users@vger.kernel.org, Kuan-Wei Chiu Subject: [PATCH v2 04/15] lib min_heap: Add type safe interface Date: Wed, 20 Mar 2024 22:54:06 +0800 Message-Id: <20240320145417.336208-5-visitorckw@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240320145417.336208-1-visitorckw@gmail.com> References: <20240320145417.336208-1-visitorckw@gmail.com> Precedence: bulk X-Mailing-List: dm-devel@lists.linux.dev List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Introduce a type-safe interface for min_heap by adding small macro wrappers around functions and using a 0-size array to store type information. This enables the use of __minheap_cast and __minheap_obj_size macros for type casting and obtaining element size. The implementation draws inspiration from generic-radix-tree.h, eliminating the need to pass element size in min_heap_callbacks. Link: https://lkml.kernel.org/ioyfizrzq7w7mjrqcadtzsfgpuntowtjdw5pgn4qhvsdp4mqqg@nrlek5vmisbu Signed-off-by: Kuan-Wei Chiu Reviewed-by: Ian Rogers --- drivers/md/dm-vdo/repair.c | 21 +++++----- drivers/md/dm-vdo/slab-depot.c | 13 +++--- include/linux/min_heap.h | 75 +++++++++++++++++++++++----------- kernel/events/core.c | 35 ++++++++-------- lib/test_min_heap.c | 49 +++++++++++----------- 5 files changed, 107 insertions(+), 86 deletions(-) diff --git a/drivers/md/dm-vdo/repair.c b/drivers/md/dm-vdo/repair.c index defc9359f10e..7663fa2098f4 100644 --- a/drivers/md/dm-vdo/repair.c +++ b/drivers/md/dm-vdo/repair.c @@ -51,6 +51,8 @@ struct recovery_point { bool increment_applied; }; +MIN_HEAP(struct numbered_block_mapping *, replay_heap); + struct repair_completion { /* The completion header */ struct vdo_completion completion; @@ -97,7 +99,7 @@ struct repair_completion { * order, then original journal order. This permits efficient iteration over the journal * entries in order. */ - struct min_heap replay_heap; + struct replay_heap replay_heap; /* Fields tracking progress through the journal entries. */ struct numbered_block_mapping *current_entry; struct numbered_block_mapping *current_unfetched_entry; @@ -163,25 +165,24 @@ static void swap_mappings(void *item1, void *item2) } static const struct min_heap_callbacks repair_min_heap = { - .elem_size = sizeof(struct numbered_block_mapping), .less = mapping_is_less_than, .swp = swap_mappings, }; static struct numbered_block_mapping *sort_next_heap_element(struct repair_completion *repair) { - struct min_heap *heap = &repair->replay_heap; + struct replay_heap *heap = &repair->replay_heap; struct numbered_block_mapping *last; - if (heap->nr == 0) + if (heap->heap.nr == 0) return NULL; /* * Swap the next heap element with the last one on the heap, popping it off the heap, * restore the heap invariant, and return a pointer to the popped element. */ - last = &repair->entries[--heap->nr]; - swap_mappings(heap->data, last); + last = &repair->entries[--heap->heap.nr]; + swap_mappings(heap->heap.data, last); min_heapify(heap, 0, &repair_min_heap); return last; } @@ -1117,11 +1118,9 @@ static void recover_block_map(struct vdo_completion *completion) * Organize the journal entries into a binary heap so we can iterate over them in sorted * order incrementally, avoiding an expensive sort call. */ - repair->replay_heap = (struct min_heap) { - .data = repair->entries, - .nr = repair->block_map_entry_count, - .size = repair->block_map_entry_count, - }; + repair->replay_heap.heap.data = repair->entries; + repair->replay_heap.heap.nr = repair->block_map_entry_count; + repair->replay_heap.heap.size = repair->block_map_entry_count; min_heapify_all(&repair->replay_heap, &repair_min_heap); vdo_log_info("Replaying %zu recovery entries into block map", diff --git a/drivers/md/dm-vdo/slab-depot.c b/drivers/md/dm-vdo/slab-depot.c index 46e4721e5b4f..3309480170c3 100644 --- a/drivers/md/dm-vdo/slab-depot.c +++ b/drivers/md/dm-vdo/slab-depot.c @@ -3309,7 +3309,6 @@ static void swap_slab_statuses(void *item1, void *item2) } static const struct min_heap_callbacks slab_status_min_heap = { - .elem_size = sizeof(struct slab_status), .less = slab_status_is_less_than, .swp = swap_slab_statuses, }; @@ -3509,7 +3508,7 @@ static int get_slab_statuses(struct block_allocator *allocator, static int __must_check vdo_prepare_slabs_for_allocation(struct block_allocator *allocator) { struct slab_status current_slab_status; - struct min_heap heap; + MIN_HEAP(struct slab_status *, heap) heap; int result; struct slab_status *slab_statuses; struct slab_depot *depot = allocator->depot; @@ -3521,14 +3520,12 @@ static int __must_check vdo_prepare_slabs_for_allocation(struct block_allocator return result; /* Sort the slabs by cleanliness, then by emptiness hint. */ - heap = (struct min_heap) { - .data = slab_statuses, - .nr = allocator->slab_count, - .size = allocator->slab_count, - }; + heap.heap.data = slab_statuses; + heap.heap.nr = allocator->slab_count; + heap.heap.size = allocator->slab_count; min_heapify_all(&heap, &slab_status_min_heap); - while (heap.nr > 0) { + while (heap.heap.nr > 0) { bool high_priority; struct vdo_slab *slab; struct slab_journal *journal; diff --git a/include/linux/min_heap.h b/include/linux/min_heap.h index d52daf45861b..c3635a7fdb88 100644 --- a/include/linux/min_heap.h +++ b/include/linux/min_heap.h @@ -7,45 +7,59 @@ #include /** - * struct min_heap - Data structure to hold a min-heap. + * struct __min_heap - Data structure to hold a min-heap. * @data: Start of array holding the heap elements. * @nr: Number of elements currently in the heap. * @size: Maximum number of elements that can be held in current storage. */ -struct min_heap { +struct __min_heap { void *data; int nr; int size; }; +/* + * We use a 0 size array to stash the type we're storing without taking any + * space at runtime - then the various accessor macros can use typeof() to get + * to it for casts/sizeof - we also force the alignment so that storing a type + * with a ridiculous alignment doesn't blow up the alignment or size of the + * min_heap. + */ +#define MIN_HEAP(_type, _name) \ +struct _name { \ + struct __min_heap heap; \ + _type type[0] __aligned(1); \ +} + +#define __minheap_cast(_heap) (typeof((_heap)->type[0]) *) +#define __minheap_obj_size(_heap) sizeof((_heap)->type[0]) + /** * struct min_heap_callbacks - Data/functions to customise the min_heap. - * @elem_size: The nr of each element in bytes. * @less: Partial order function for this heap. * @swp: Swap elements function. */ struct min_heap_callbacks { - int elem_size; bool (*less)(const void *lhs, const void *rhs); void (*swp)(void *lhs, void *rhs); }; /* Sift the element at pos down the heap. */ static __always_inline -void min_heapify(struct min_heap *heap, int pos, +void __min_heapify(struct __min_heap *heap, int pos, size_t elem_size, const struct min_heap_callbacks *func) { void *left, *right; void *data = heap->data; - void *root = data + pos * func->elem_size; + void *root = data + pos * elem_size; int i = pos, j; /* Find the sift-down path all the way to the leaves. */ for (;;) { if (i * 2 + 2 >= heap->nr) break; - left = data + (i * 2 + 1) * func->elem_size; - right = data + (i * 2 + 2) * func->elem_size; + left = data + (i * 2 + 1) * elem_size; + right = data + (i * 2 + 2) * elem_size; i = func->less(left, right) ? i * 2 + 1 : i * 2 + 2; } @@ -54,31 +68,37 @@ void min_heapify(struct min_heap *heap, int pos, i = i * 2 + 1; /* Backtrack to the correct location. */ - while (i != pos && func->less(root, data + i * func->elem_size)) + while (i != pos && func->less(root, data + i * elem_size)) i = (i - 1) / 2; /* Shift the element into its correct place. */ j = i; while (i != pos) { i = (i - 1) / 2; - func->swp(data + i * func->elem_size, data + j * func->elem_size); + func->swp(data + i * elem_size, data + j * elem_size); } } +#define min_heapify(_heap, _pos, _func) \ + __min_heapify(&(_heap)->heap, _pos, __minheap_obj_size(_heap), _func) + /* Floyd's approach to heapification that is O(nr). */ static __always_inline -void min_heapify_all(struct min_heap *heap, +void __min_heapify_all(struct __min_heap *heap, size_t elem_size, const struct min_heap_callbacks *func) { int i; for (i = heap->nr / 2 - 1; i >= 0; i--) - min_heapify(heap, i, func); + __min_heapify(heap, i, elem_size, func); } +#define min_heapify_all(_heap, _func) \ + __min_heapify_all(&(_heap)->heap, __minheap_obj_size(_heap), _func) + /* Remove minimum element from the heap, O(log2(nr)). */ static __always_inline -void min_heap_pop(struct min_heap *heap, +void __min_heap_pop(struct __min_heap *heap, size_t elem_size, const struct min_heap_callbacks *func) { void *data = heap->data; @@ -88,27 +108,33 @@ void min_heap_pop(struct min_heap *heap, /* Place last element at the root (position 0) and then sift down. */ heap->nr--; - memcpy(data, data + (heap->nr * func->elem_size), func->elem_size); - min_heapify(heap, 0, func); + memcpy(data, data + (heap->nr * elem_size), elem_size); + __min_heapify(heap, 0, elem_size, func); } +#define min_heap_pop(_heap, _func) \ + __min_heap_pop(&(_heap)->heap, __minheap_obj_size(_heap), _func) + /* * Remove the minimum element and then push the given element. The * implementation performs 1 sift (O(log2(nr))) and is therefore more * efficient than a pop followed by a push that does 2. */ static __always_inline -void min_heap_pop_push(struct min_heap *heap, - const void *element, +void __min_heap_pop_push(struct __min_heap *heap, + const void *element, size_t elem_size, const struct min_heap_callbacks *func) { - memcpy(heap->data, element, func->elem_size); - min_heapify(heap, 0, func); + memcpy(heap->data, element, elem_size); + __min_heapify(heap, 0, elem_size, func); } +#define min_heap_pop_push(_heap, _element, _func) \ + __min_heap_pop_push(&(_heap)->heap, _element, __minheap_obj_size(_heap), _func) + /* Push an element on to the heap, O(log2(nr)). */ static __always_inline -void min_heap_push(struct min_heap *heap, const void *element, +void __min_heap_push(struct __min_heap *heap, const void *element, size_t elem_size, const struct min_heap_callbacks *func) { void *data = heap->data; @@ -120,17 +146,20 @@ void min_heap_push(struct min_heap *heap, const void *element, /* Place at the end of data. */ pos = heap->nr; - memcpy(data + (pos * func->elem_size), element, func->elem_size); + memcpy(data + (pos * elem_size), element, elem_size); heap->nr++; /* Sift child at pos up. */ for (; pos > 0; pos = (pos - 1) / 2) { - child = data + (pos * func->elem_size); - parent = data + ((pos - 1) / 2) * func->elem_size; + child = data + (pos * elem_size); + parent = data + ((pos - 1) / 2) * elem_size; if (func->less(parent, child)) break; func->swp(parent, child); } } +#define min_heap_push(_heap, _element, _func) \ + __min_heap_push(&(_heap)->heap, _element, __minheap_obj_size(_heap), _func) + #endif /* _LINUX_MIN_HEAP_H */ diff --git a/kernel/events/core.c b/kernel/events/core.c index 10ac2db83f14..065dfaa8b009 100644 --- a/kernel/events/core.c +++ b/kernel/events/core.c @@ -3698,19 +3698,20 @@ static void swap_ptr(void *l, void *r) swap(*lp, *rp); } +MIN_HEAP(struct perf_event *, perf_event_min_heap); + static const struct min_heap_callbacks perf_min_heap = { - .elem_size = sizeof(struct perf_event *), .less = perf_less_group_idx, .swp = swap_ptr, }; -static void __heap_add(struct min_heap *heap, struct perf_event *event) +static void __heap_add(struct perf_event_min_heap *heap, struct perf_event *event) { - struct perf_event **itrs = heap->data; + struct perf_event **itrs = heap->heap.data; if (event) { - itrs[heap->nr] = event; - heap->nr++; + itrs[heap->heap.nr] = event; + heap->heap.nr++; } } @@ -3738,7 +3739,7 @@ static noinline int visit_groups_merge(struct perf_event_context *ctx, struct perf_cpu_context *cpuctx = NULL; /* Space for per CPU and/or any CPU event iterators. */ struct perf_event *itrs[2]; - struct min_heap event_heap; + struct perf_event_min_heap event_heap; struct perf_event **evt; int ret; @@ -3747,11 +3748,9 @@ static noinline int visit_groups_merge(struct perf_event_context *ctx, if (!ctx->task) { cpuctx = this_cpu_ptr(&perf_cpu_context); - event_heap = (struct min_heap){ - .data = cpuctx->heap, - .nr = 0, - .size = cpuctx->heap_size, - }; + event_heap.heap.data = cpuctx->heap; + event_heap.heap.nr = 0; + event_heap.heap.size = cpuctx->heap_size; lockdep_assert_held(&cpuctx->ctx.lock); @@ -3760,15 +3759,13 @@ static noinline int visit_groups_merge(struct perf_event_context *ctx, css = &cpuctx->cgrp->css; #endif } else { - event_heap = (struct min_heap){ - .data = itrs, - .nr = 0, - .size = ARRAY_SIZE(itrs), - }; + event_heap.heap.data = itrs; + event_heap.heap.nr = 0; + event_heap.heap.size = ARRAY_SIZE(itrs); /* Events not within a CPU context may be on any CPU. */ __heap_add(&event_heap, perf_event_groups_first(groups, -1, pmu, NULL)); } - evt = event_heap.data; + evt = event_heap.heap.data; __heap_add(&event_heap, perf_event_groups_first(groups, cpu, pmu, NULL)); @@ -3777,14 +3774,14 @@ static noinline int visit_groups_merge(struct perf_event_context *ctx, __heap_add(&event_heap, perf_event_groups_first(groups, cpu, pmu, css->cgroup)); #endif - if (event_heap.nr) { + if (event_heap.heap.nr) { __link_epc((*evt)->pmu_ctx); perf_assert_pmu_disabled((*evt)->pmu_ctx->pmu); } min_heapify_all(&event_heap, &perf_min_heap); - while (event_heap.nr) { + while (event_heap.heap.nr) { ret = func(*evt, data); if (ret) return ret; diff --git a/lib/test_min_heap.c b/lib/test_min_heap.c index 7b01b4387cfb..af2e446034d8 100644 --- a/lib/test_min_heap.c +++ b/lib/test_min_heap.c @@ -11,6 +11,8 @@ #include #include +MIN_HEAP(int, min_heap_test); + static __init bool less_than(const void *lhs, const void *rhs) { return *(int *)lhs < *(int *)rhs; @@ -30,16 +32,16 @@ static __init void swap_ints(void *lhs, void *rhs) } static __init int pop_verify_heap(bool min_heap, - struct min_heap *heap, + struct min_heap_test *heap, const struct min_heap_callbacks *funcs) { - int *values = heap->data; + int *values = heap->heap.data; int err = 0; int last; last = values[0]; min_heap_pop(heap, funcs); - while (heap->nr > 0) { + while (heap->heap.nr > 0) { if (min_heap) { if (last > values[0]) { pr_err("error: expected %d <= %d\n", last, @@ -63,13 +65,12 @@ static __init int test_heapify_all(bool min_heap) { int values[] = { 3, 1, 2, 4, 0x8000000, 0x7FFFFFF, 0, -3, -1, -2, -4, 0x8000000, 0x7FFFFFF }; - struct min_heap heap = { - .data = values, - .nr = ARRAY_SIZE(values), - .size = ARRAY_SIZE(values), - }; + struct min_heap_test heap; + + heap.heap.data = values; + heap.heap.nr = ARRAY_SIZE(values); + heap.heap.size = ARRAY_SIZE(values); struct min_heap_callbacks funcs = { - .elem_size = sizeof(int), .less = min_heap ? less_than : greater_than, .swp = swap_ints, }; @@ -81,8 +82,8 @@ static __init int test_heapify_all(bool min_heap) /* Test with randomly generated values. */ - heap.nr = ARRAY_SIZE(values); - for (i = 0; i < heap.nr; i++) + heap.heap.nr = ARRAY_SIZE(values); + for (i = 0; i < heap.heap.nr; i++) values[i] = get_random_u32(); min_heapify_all(&heap, &funcs); @@ -96,13 +97,12 @@ static __init int test_heap_push(bool min_heap) const int data[] = { 3, 1, 2, 4, 0x80000000, 0x7FFFFFFF, 0, -3, -1, -2, -4, 0x80000000, 0x7FFFFFFF }; int values[ARRAY_SIZE(data)]; - struct min_heap heap = { - .data = values, - .nr = 0, - .size = ARRAY_SIZE(values), - }; + struct min_heap_test heap; + + heap.heap.data = values; + heap.heap.nr = 0; + heap.heap.size = ARRAY_SIZE(values); struct min_heap_callbacks funcs = { - .elem_size = sizeof(int), .less = min_heap ? less_than : greater_than, .swp = swap_ints, }; @@ -115,7 +115,7 @@ static __init int test_heap_push(bool min_heap) err = pop_verify_heap(min_heap, &heap, &funcs); /* Test with randomly generated values. */ - while (heap.nr < heap.size) { + while (heap.heap.nr < heap.heap.size) { temp = get_random_u32(); min_heap_push(&heap, &temp, &funcs); } @@ -129,13 +129,12 @@ static __init int test_heap_pop_push(bool min_heap) const int data[] = { 3, 1, 2, 4, 0x80000000, 0x7FFFFFFF, 0, -3, -1, -2, -4, 0x80000000, 0x7FFFFFFF }; int values[ARRAY_SIZE(data)]; - struct min_heap heap = { - .data = values, - .nr = 0, - .size = ARRAY_SIZE(values), - }; + struct min_heap_test heap; + + heap.heap.data = values; + heap.heap.nr = 0; + heap.heap.size = ARRAY_SIZE(values); struct min_heap_callbacks funcs = { - .elem_size = sizeof(int), .less = min_heap ? less_than : greater_than, .swp = swap_ints, }; @@ -152,7 +151,7 @@ static __init int test_heap_pop_push(bool min_heap) err = pop_verify_heap(min_heap, &heap, &funcs); - heap.nr = 0; + heap.heap.nr = 0; for (i = 0; i < ARRAY_SIZE(data); i++) min_heap_push(&heap, &temp, &funcs); From patchwork Wed Mar 20 14:54:07 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kuan-Wei Chiu X-Patchwork-Id: 13597942 X-Patchwork-Delegate: snitzer@redhat.com Received: from mail-pl1-f173.google.com (mail-pl1-f173.google.com [209.85.214.173]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 0385254BFE for ; Wed, 20 Mar 2024 14:54:48 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.173 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1710946491; cv=none; b=a2KxfPIgxSawQ6oJjjwPZyoQ7oAUvn+TiRqE4H8FdL4qW12nqESn7UmCj9hPa1/rvWUleCyj3wytgg05BhCVGVumcl7MOz5/Lz9oBQcSq8tfXfWYRG7sYh5L6WNaxti1lWlIF1bGP27ZMKdwf07x9e/OVjw+uTxL2LCDQLDlluc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1710946491; c=relaxed/simple; bh=1kcI5O448ZoSUJqM7Fwo+CLjTMdYaBqUzIGDOV4I8Wc=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=V52eWv9/ZEhVlOZM4HssBS/Oaw7ePo2epACw3LX0cN1seBv4kHY3mvtLb92mOS6nQ47eCwScRyUk7fRzxZBqcAY6Mz3XIT5XdvEOc8LnTBx1A0KOA7w2kqioC9zDrKP/WENAOgECbIMVG7Mm9+aeoNxEEbh4p10IQJia5q/uZhc= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=ZLNKyzRG; arc=none smtp.client-ip=209.85.214.173 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="ZLNKyzRG" Received: by mail-pl1-f173.google.com with SMTP id d9443c01a7336-1dd8ee2441dso11936285ad.1 for ; Wed, 20 Mar 2024 07:54:48 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1710946488; x=1711551288; darn=lists.linux.dev; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=wIY+iLTtXDqlHadPKUvUaL+ptK+GK0/HaTJmss4inJc=; b=ZLNKyzRG9ZxpuDamuM1zrvOvHhbjfsBBbDKKuiBA7PillK6IsnmXHRMBRe+iDCFr0C 0jcHVROdG1/wh2qUemtaqP3u1ROHu/McwzsV8XbTLypR9YT4S3roltQ3uCg4EYG3O0YB 5yKyARgt7KNMs2b4BEF2JG6bWGxSy1VRDPD76jOCW/5/pKrBtiWBlTpkR7aO3nl/yEdF jCfDqAv1WvbeSa6SD4+ZxkxYEqrfTN35PQXoCPJSueW7IRtwD6V173waR9at0g9tLc2F snj4s0NsRx2Xa7Nq3IqHhJiixEypjVufOpoJCmGqhZ/+qmndhePEx2SAXcumfFuztuzW yn3A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1710946488; x=1711551288; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=wIY+iLTtXDqlHadPKUvUaL+ptK+GK0/HaTJmss4inJc=; b=M4Tk7lCQI623QjTS2RdOcsFCF9depWg60pL5mVNqGtbsWVgH+rOW0mi4fN/22aZHz3 098sCY65O0krDJcDMj5a2qW5+7jXVjmhxR4Q0bjPBMevC+9DRELeVkZQsTpn0npFdC1O wokRVXGX9KKmXUEMd8PHKZTnNYaXH1h73fhxwaRy7atq8P7UAUeTJLptEramf4oMLVT0 zWQArtX7P5B615WwYqsfJ4zj1gcIsWNCtDCmM5aP0ZwlfJ3x6tqWg0F/qSaHD4eVJ7S9 Gb/2s2iyaduaFXqt1sVrLRIFdxq1VvzHUKdX3FpYy4e9zQzyCQdD3O7b3S0T7xiDa3EQ d3lA== X-Forwarded-Encrypted: i=1; AJvYcCWGXpZq9Kh450oLPSi0wWAzrAkEk/cua+njdrXIlqzBqhVoyWOseXoIdn+4+OPT9oiUp1aan+WRZaFMFbOR7oT0j4onWqqEAsE= X-Gm-Message-State: AOJu0YwQSxxh6Eo5URzYA0IqtTNzJ4551Pxd4Oa0wvgIQITXL/Gs+rvg Glhhk85VEkSMZfxEK82AU5GkEWSNFwehPD53ybQuMUB2IMzOHC7g X-Google-Smtp-Source: AGHT+IGcYha2L/klvjE95SX1NwjIcrrjx6lhQgqEP8rC4e+/MaAzigVjqy5yAHiaD5OOHjIki9QMLg== X-Received: by 2002:a17:902:d4d1:b0:1dd:6f1a:2106 with SMTP id o17-20020a170902d4d100b001dd6f1a2106mr6183213plg.0.1710946488296; Wed, 20 Mar 2024 07:54:48 -0700 (PDT) Received: from visitorckw-System-Product-Name.. ([140.113.216.168]) by smtp.gmail.com with ESMTPSA id la11-20020a170902fa0b00b001dc30f13e6asm13719989plb.137.2024.03.20.07.54.44 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 20 Mar 2024 07:54:47 -0700 (PDT) From: Kuan-Wei Chiu To: colyli@suse.de, kent.overstreet@linux.dev, msakai@redhat.com, peterz@infradead.org, mingo@redhat.com, acme@kernel.org, namhyung@kernel.org, akpm@linux-foundation.org Cc: bfoster@redhat.com, mark.rutland@arm.com, alexander.shishkin@linux.intel.com, jolsa@kernel.org, irogers@google.com, adrian.hunter@intel.com, jserv@ccns.ncku.edu.tw, dm-devel@lists.linux.dev, linux-bcache@vger.kernel.org, linux-kernel@vger.kernel.org, linux-bcachefs@vger.kernel.org, linux-perf-users@vger.kernel.org, Kuan-Wei Chiu Subject: [PATCH v2 05/15] lib min_heap: Add min_heap_init() Date: Wed, 20 Mar 2024 22:54:07 +0800 Message-Id: <20240320145417.336208-6-visitorckw@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240320145417.336208-1-visitorckw@gmail.com> References: <20240320145417.336208-1-visitorckw@gmail.com> Precedence: bulk X-Mailing-List: dm-devel@lists.linux.dev List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Add min_heap_init() for initializing heap with data, nr, and size. Signed-off-by: Kuan-Wei Chiu Reviewed-by: Ian Rogers --- include/linux/min_heap.h | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/include/linux/min_heap.h b/include/linux/min_heap.h index c3635a7fdb88..ed462f194b88 100644 --- a/include/linux/min_heap.h +++ b/include/linux/min_heap.h @@ -44,6 +44,18 @@ struct min_heap_callbacks { void (*swp)(void *lhs, void *rhs); }; +/* Initialize a min-heap. */ +static __always_inline +void __min_heap_init(struct __min_heap *heap, void *data, int size) +{ + heap->data = data; + heap->nr = 0; + heap->size = size; +} + +#define min_heap_init(_heap, _data, _size) \ + __min_heap_init(&(_heap)->heap, _data, _size) + /* Sift the element at pos down the heap. */ static __always_inline void __min_heapify(struct __min_heap *heap, int pos, size_t elem_size, From patchwork Wed Mar 20 14:54:08 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kuan-Wei Chiu X-Patchwork-Id: 13597943 X-Patchwork-Delegate: snitzer@redhat.com Received: from mail-pl1-f178.google.com (mail-pl1-f178.google.com [209.85.214.178]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id A5E185646D for ; Wed, 20 Mar 2024 14:54:53 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.178 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1710946495; cv=none; b=QEs4UX8/X6AqqYwVGlKJ7t25vMzufyE3wwpEgerEuxRRo37I5xkbmP0+0gO9ZBv85lqVczHZVYlbmQnG/g1M0eXtEsdKDZzo9rjkrgbLAhooGGY7iP1u3+jwULMabiBrRQ83mCgfxf7wi4ahe04vH1SG9v4B0U/NnQuU1YAMNSI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1710946495; c=relaxed/simple; bh=8a68K+W3qZD++KgYqdJ8QFpXlGJ3ofQ9m/5i3nVK9u0=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=Xjar9W0Dp6Dc6GldK1R94zhZc8Tn4T4d5EXHSDnVEhzq2rBT0FjWsBa1HqRz/zIzMUqol4wDPm6/f9MgbHUtFlrA36JRJwGd+k9Qpu/vIZBtuUmlE2PMY2hGmcAIqdKXCOK3mEJ64XoN5+8NquAu21XMn/2Tbw2T2ShfjqaHBSs= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=IlaEoQiq; arc=none smtp.client-ip=209.85.214.178 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="IlaEoQiq" Received: by mail-pl1-f178.google.com with SMTP id d9443c01a7336-1dee6672526so6079175ad.1 for ; Wed, 20 Mar 2024 07:54:53 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1710946493; x=1711551293; darn=lists.linux.dev; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=g5/a5el4a61wFJIBzCgWNmWU3XZlSJ2PCR5OhDq59lw=; b=IlaEoQiq1arkQITXLUfZzvmdq2KCmJi6m1CmhBDDUXQ4KneY2p1sp7SMHU7SHnlZd0 tImwyRO9HwpQ5FxIH4K8kSDqKCIo6Sk9QrGjtRJsZfBhgifEI0LT4t4FyaMseer9CQ7T s4xrZwAHmAdtxZQGpla/2buJGt8fv/pheKjV0YqxT1uMqArm/inNcUlWbhR9xNAEQu9E EXRfOeMiTxvCNn8eKNqM1tP75ZXeQtxR/jg5k0HIJpyEqTBYUBkdkcPOnyW8QN32nlw5 lahVkrLzJHdL/RbnyCP+lBfxv2LSoHFRwjR9T97PayOgPC01Qf6jw/vHR8YG7JSREfPo GipQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1710946493; x=1711551293; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=g5/a5el4a61wFJIBzCgWNmWU3XZlSJ2PCR5OhDq59lw=; b=bhamGwbIfY2jojavbMn5XhaBS9bqn+3qO5eqHq0nzCPx08/RyySxKXXCQDkUIXfqya wF0Ge2C+4jsCITTIC8gY5C4ZPqQgUs+b/iX68rOm+W2zoFMoAKIESYkzdQkNCbGJAOwk 3bB93J1/8NsRTieOhxQBf6kAlgFu8HGhvG8H4hRH146+vyOKPXJ1klS1tUHAAHbPf0Us 3uBVHAiAPyEFwKxjzj/zcuYMonUBrNuNIGlajH9TgICdkbcm9hvGTt9szKncg53r7Qvf iYw5A21Gs5PwFjmyik7pu1+KMvEducu/aoMGb1cngtY2Oud4iJWX0HtIm2pJdqOM1DI7 SeYg== X-Forwarded-Encrypted: i=1; AJvYcCV9h9zLqht11FqeCgZbeaO+1qLa7Sq5bgv78Wy2PnFIbkFia9VVUN2L5jCozXOue5cL60ln+TTQmRF8sJqmMXr1tLqaIexoFDg= X-Gm-Message-State: AOJu0Yy0+KvXt7hphLSIUuf0yBXDnQRLc30azRSJQ1WdIgzkw6XXScys QFyYQY6tp6o0mne1EshxvEaVpLOjLA8TyT2U2fMFjS0eWOOTdN3l X-Google-Smtp-Source: AGHT+IGvhyqndniFbkMNrKQajfiHJUdmj6ZvIM3gEC4yAeOXQXFolKTC1Reahvgx53VYp1rBgyowXg== X-Received: by 2002:a17:902:ce91:b0:1dc:df03:ad86 with SMTP id f17-20020a170902ce9100b001dcdf03ad86mr20347899plg.2.1710946492946; Wed, 20 Mar 2024 07:54:52 -0700 (PDT) Received: from visitorckw-System-Product-Name.. ([140.113.216.168]) by smtp.gmail.com with ESMTPSA id la11-20020a170902fa0b00b001dc30f13e6asm13719989plb.137.2024.03.20.07.54.49 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 20 Mar 2024 07:54:52 -0700 (PDT) From: Kuan-Wei Chiu To: colyli@suse.de, kent.overstreet@linux.dev, msakai@redhat.com, peterz@infradead.org, mingo@redhat.com, acme@kernel.org, namhyung@kernel.org, akpm@linux-foundation.org Cc: bfoster@redhat.com, mark.rutland@arm.com, alexander.shishkin@linux.intel.com, jolsa@kernel.org, irogers@google.com, adrian.hunter@intel.com, jserv@ccns.ncku.edu.tw, dm-devel@lists.linux.dev, linux-bcache@vger.kernel.org, linux-kernel@vger.kernel.org, linux-bcachefs@vger.kernel.org, linux-perf-users@vger.kernel.org, Kuan-Wei Chiu Subject: [PATCH v2 06/15] lib min_heap: Add min_heap_peek() Date: Wed, 20 Mar 2024 22:54:08 +0800 Message-Id: <20240320145417.336208-7-visitorckw@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240320145417.336208-1-visitorckw@gmail.com> References: <20240320145417.336208-1-visitorckw@gmail.com> Precedence: bulk X-Mailing-List: dm-devel@lists.linux.dev List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Add min_heap_peek() function to retrieve a pointer to the smallest element. The pointer is cast to the appropriate type of heap elements. Signed-off-by: Kuan-Wei Chiu Reviewed-by: Ian Rogers --- include/linux/min_heap.h | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/include/linux/min_heap.h b/include/linux/min_heap.h index ed462f194b88..7c1fd1ddc71a 100644 --- a/include/linux/min_heap.h +++ b/include/linux/min_heap.h @@ -56,6 +56,16 @@ void __min_heap_init(struct __min_heap *heap, void *data, int size) #define min_heap_init(_heap, _data, _size) \ __min_heap_init(&(_heap)->heap, _data, _size) +/* Get the minimum element from the heap. */ +static __always_inline +void *__min_heap_peek(struct __min_heap *heap) +{ + return heap->nr ? heap->data : NULL; +} + +#define min_heap_peek(_heap) \ + (__minheap_cast(_heap) __min_heap_peek(&(_heap)->heap)) + /* Sift the element at pos down the heap. */ static __always_inline void __min_heapify(struct __min_heap *heap, int pos, size_t elem_size, From patchwork Wed Mar 20 14:54:09 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kuan-Wei Chiu X-Patchwork-Id: 13597944 X-Patchwork-Delegate: snitzer@redhat.com Received: from mail-pl1-f179.google.com (mail-pl1-f179.google.com [209.85.214.179]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 0E2F44C3CD for ; Wed, 20 Mar 2024 14:54:58 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.179 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1710946500; cv=none; b=OSWJUG7S++0ZF6hbDIhikgFVTWK+sQsvElOnr1JHJbX/LDchHHhFcsI+Pm6of/EBwKCqijTTGvA3UZCMluiRiJwHTPBsDCri46vld2NWIq4z0M9rV8/4J4XA+whjoac1KbDpNpp3CrELF+ncutJuPsVGb+V8JS+SNtmhoacyEDw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1710946500; c=relaxed/simple; bh=87oXCcVzX378JZ7P9KHFZTxbp5kTVN7ioJ6nxJ/kTRs=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=sPODTd6DSQ6UBv62fCqJsTZXQ6pOPhT4VIdo/UEaVPrcHG0ykwHgtBeCcJsbfsJ+KJ2sZTmzKcJNngKiMorbI7z2lmbRExs2UANMEmmlzk4SpUel3sGc/dy82G3ElKdQN7zBNsIZ6FY5KHVE/QeOpiRgEdGV76xPg1W5OuTjZuM= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=a+r/YpOK; arc=none smtp.client-ip=209.85.214.179 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="a+r/YpOK" Received: by mail-pl1-f179.google.com with SMTP id d9443c01a7336-1e0678265b3so618355ad.0 for ; Wed, 20 Mar 2024 07:54:58 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1710946498; x=1711551298; darn=lists.linux.dev; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=6wlW/VG1bZr/fW5x2pRSDPzGgSGqgaQobdRLr7/yKpQ=; b=a+r/YpOKp+Vte8lRlzYGEGiwhPvKy0Fx0VgZp/oVKzisqcC/Xv0HFw3naJl8EEDfQH cNQake1nnwmDqDjd49CC8Oa3XvJ4Lb4pFiQhkbaPfUgQoW0yFWR70l28Fwg9ybU8QJ+V 6KINhfCnNJc1v81/yQl99OO2udUENogDobuJBjKOmHagCtarLguQWYY53ooI370n7/pX omhTLKIPI7WFDqrhHuX9dC/kaBbsFxKZ4u/7kIYOkIh4OBvBmVgCPrKiq3JoLZFtizCL CwkurKa7EUJsgOvvwFqwQQI+Cxapf/jERvTzq87Pl+GUFqujwtcsjfYsZQSeeNt6NzpF p2yg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1710946498; x=1711551298; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=6wlW/VG1bZr/fW5x2pRSDPzGgSGqgaQobdRLr7/yKpQ=; b=Nk/n+wy8BwsLSdOxo0jPuoXL2CJK/MGbdTUTmaK3ca+6jIn/XBmr/C8R+rE9L3++Se x+/les8O++s+elZNE/Jv6kOD1XDsBdaCNsFUbYnoKiF6RxAQqHJDzR0ZS9ci1PKxOGUA 89ZCLKTfImtfV+b+W21eKWOsGSUG++ewLlKJH52r//IcQSgiO3sYq75qzVcSQZMRcsov P3JXVkL4ZFroXjPfJ+lmJYu/rHPX53/AWCVGBEAcYdrM9p4uHEf/OrOObBtOfxQ7vwxV p5sMpVFAw9GFnoonYq5VZoW+t4N+3WopKbQ7CHxHHwMKcx+OA4TIeAHYrBkfwaLcfzfY TPvw== X-Forwarded-Encrypted: i=1; AJvYcCVaUSu+xM1EKBRjBfgiwnTlo5vYoW3rbEYMcw+0uuyxLp/g4vS/u/973P4EARLB53RBir1OJpPCTqaI0SSGKqiILgZV3eBN7UA= X-Gm-Message-State: AOJu0YxSl2zaYog5iA4J8srXHC7DIjRJpAbYf7LwOe6qwJJQTFpC3q9/ WTY19iLPbnGDaI0t3Ix+Cyx3JXLtgONiO6A6Ow82AV+wlWQlZw2PLXBtr9WmSM2O7A== X-Google-Smtp-Source: AGHT+IHPNXqxNRiHHdL94gEllLU3VLR0+ZSU1ayFv4vdbYAP2+XpkPoVncG1MrhmG5iQ5COCDdzKFQ== X-Received: by 2002:a17:903:1c1:b0:1dd:7c4c:c6b6 with SMTP id e1-20020a17090301c100b001dd7c4cc6b6mr19250306plh.5.1710946498281; Wed, 20 Mar 2024 07:54:58 -0700 (PDT) Received: from visitorckw-System-Product-Name.. ([140.113.216.168]) by smtp.gmail.com with ESMTPSA id la11-20020a170902fa0b00b001dc30f13e6asm13719989plb.137.2024.03.20.07.54.54 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 20 Mar 2024 07:54:57 -0700 (PDT) From: Kuan-Wei Chiu To: colyli@suse.de, kent.overstreet@linux.dev, msakai@redhat.com, peterz@infradead.org, mingo@redhat.com, acme@kernel.org, namhyung@kernel.org, akpm@linux-foundation.org Cc: bfoster@redhat.com, mark.rutland@arm.com, alexander.shishkin@linux.intel.com, jolsa@kernel.org, irogers@google.com, adrian.hunter@intel.com, jserv@ccns.ncku.edu.tw, dm-devel@lists.linux.dev, linux-bcache@vger.kernel.org, linux-kernel@vger.kernel.org, linux-bcachefs@vger.kernel.org, linux-perf-users@vger.kernel.org, Kuan-Wei Chiu Subject: [PATCH v2 07/15] lib min_heap: Add min_heap_full() Date: Wed, 20 Mar 2024 22:54:09 +0800 Message-Id: <20240320145417.336208-8-visitorckw@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240320145417.336208-1-visitorckw@gmail.com> References: <20240320145417.336208-1-visitorckw@gmail.com> Precedence: bulk X-Mailing-List: dm-devel@lists.linux.dev List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Add min_heap_full() which returns a boolean value indicating whether the heap has reached its maximum capacity. Signed-off-by: Kuan-Wei Chiu Reviewed-by: Ian Rogers --- include/linux/min_heap.h | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/include/linux/min_heap.h b/include/linux/min_heap.h index 7c1fd1ddc71a..b1d874f4d536 100644 --- a/include/linux/min_heap.h +++ b/include/linux/min_heap.h @@ -66,6 +66,16 @@ void *__min_heap_peek(struct __min_heap *heap) #define min_heap_peek(_heap) \ (__minheap_cast(_heap) __min_heap_peek(&(_heap)->heap)) +/* Check if the heap is full. */ +static __always_inline +bool __min_heap_full(struct __min_heap *heap) +{ + return heap->nr == heap->size; +} + +#define min_heap_full(_heap) \ + __min_heap_full(&(_heap)->heap) + /* Sift the element at pos down the heap. */ static __always_inline void __min_heapify(struct __min_heap *heap, int pos, size_t elem_size, From patchwork Wed Mar 20 14:54:10 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kuan-Wei Chiu X-Patchwork-Id: 13597945 X-Patchwork-Delegate: snitzer@redhat.com Received: from mail-pl1-f180.google.com (mail-pl1-f180.google.com [209.85.214.180]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 0D7C65730D for ; Wed, 20 Mar 2024 14:55:03 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.180 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1710946505; cv=none; b=AjUnWen5k5dySlOUifRmi1ldzy6O+iK2YkTYGhgo/c16RvDjODu4AQvbqjdok1u21eQOsVdgJxa6+deWfA3oIOid8x/GIi0CzPC4ilDL7a9jCsWaoa/YqTXBVvQUtztD+reL8ddR9IHGuf8RWX1f2heImlGHZ+oRYA2QVWHG5tI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1710946505; c=relaxed/simple; bh=qteByzsELspBqYOuIARYBU2Zdpp8cSKVC+Oyjn250gs=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=rOWKuEwU1egLjLvYUndtCalgQaQaK82b2fUkYWl5qDB6BPWsbEFQrHnyw1jUo9tSlWzQhnIaGizM8d1KMbrx6pOwptSyLUIGPIPSxMaSwLD+3rW6F1pJOQ3i92oiKEcnY2yaM9FEBEr5JKPGUmkecJDEDdJz/qgaOg+jG/E5L8g= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=F46rLZe8; arc=none smtp.client-ip=209.85.214.180 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="F46rLZe8" Received: by mail-pl1-f180.google.com with SMTP id d9443c01a7336-1def81ee762so9760675ad.0 for ; Wed, 20 Mar 2024 07:55:03 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1710946503; x=1711551303; darn=lists.linux.dev; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=qQWgWJZiZRkB0L8mwTWbJhKi4LMazpagttqLDd/n3Ao=; b=F46rLZe8jEuJE4ZKuooXmFYy7sz6yN0YSovZrHAnTTkwKn+N1Xw0y42RPzHmVgt0Xm KatITplXq/7aYsMfyci9NAV9airfzzBjJsltPZJjR2X26jmVXENGEkVrXP8FNmXT9YLI pVGqt9DaCoryKDRsfgAQKP+Jz/tM3sjHCrXxVEX/tlYBN4lZWxBnQtQXlXUvMvoe25zH eQDk7tAkUUsiF3yLTUjIZXN6/RBgeAiKilnmZ11rXVU3QVyrZ/1+OM3IJC4p+qI6EFFX 3lIAJDHZHWMFWyBBIvUAZSMWAIuplRYddypCEJE0I2EQ0YNSqLQN1fvlA241DhPlikNP JQ7A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1710946503; x=1711551303; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=qQWgWJZiZRkB0L8mwTWbJhKi4LMazpagttqLDd/n3Ao=; b=pTEKuH85EiOvfQbN/OEY2U7K9GHnImPPooeX/7f1wyCPktAJCuEinEUxrQjPFQ2t2Y 0AKlOAGQVJHEqyF/tkpBmDm0oS4/QRsFjYWazcpHAPjQv0H4ja1p9dF1rJ4llbUF4CN6 g0uo4Pm22s2Dow6kE6OC7ls4j2WUR/jdiyuzGcgbu9ZH6XJ9LYmHSF5sVxPQnJdpU7mv c+/6OF+m6V7ZyiHdUtQ//bUCISQWixyatga5Y5pcHPPNbMGfWGLwghX14niZLTt5a7PA 1359Fed0JrEUxkxiU3UjHdnrQ7jIsFY5MztEjg84HkQMFLsgOL8pebJTKHfMelr3vfHH bwsg== X-Forwarded-Encrypted: i=1; AJvYcCVl9Z68xVOUhKsWmi2KIY33lAqFpAd51saJ9HiHqavt49HG4wZpYCtA+UE9l+9QbSZeRyuXnEE77lvJYl4cLgacG+vJb/sUdyg= X-Gm-Message-State: AOJu0YxlnftMuVQE6mSdJ6xWqFV6dVJBOFaul/lsYICkClxcy1qcnsvW uuVJbWMwJNOt0kP5VqzUleFAUT1RuLBwrAFiFM/7EjXTIIHluaXEuAxo+x5KotNr7w== X-Google-Smtp-Source: AGHT+IHQMu7VAPDiqxJgKvWwfBIcERpY9KKEw9e5I4279Pib64kSmLnENjmLOaIX8wFBH1sb7n9z5Q== X-Received: by 2002:a17:902:c94d:b0:1dc:e469:6f5d with SMTP id i13-20020a170902c94d00b001dce4696f5dmr6103504pla.4.1710946503210; Wed, 20 Mar 2024 07:55:03 -0700 (PDT) Received: from visitorckw-System-Product-Name.. ([140.113.216.168]) by smtp.gmail.com with ESMTPSA id la11-20020a170902fa0b00b001dc30f13e6asm13719989plb.137.2024.03.20.07.54.59 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 20 Mar 2024 07:55:02 -0700 (PDT) From: Kuan-Wei Chiu To: colyli@suse.de, kent.overstreet@linux.dev, msakai@redhat.com, peterz@infradead.org, mingo@redhat.com, acme@kernel.org, namhyung@kernel.org, akpm@linux-foundation.org Cc: bfoster@redhat.com, mark.rutland@arm.com, alexander.shishkin@linux.intel.com, jolsa@kernel.org, irogers@google.com, adrian.hunter@intel.com, jserv@ccns.ncku.edu.tw, dm-devel@lists.linux.dev, linux-bcache@vger.kernel.org, linux-kernel@vger.kernel.org, linux-bcachefs@vger.kernel.org, linux-perf-users@vger.kernel.org, Kuan-Wei Chiu Subject: [PATCH v2 08/15] lib min_heap: Add min_heap_del() Date: Wed, 20 Mar 2024 22:54:10 +0800 Message-Id: <20240320145417.336208-9-visitorckw@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240320145417.336208-1-visitorckw@gmail.com> References: <20240320145417.336208-1-visitorckw@gmail.com> Precedence: bulk X-Mailing-List: dm-devel@lists.linux.dev List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Add min_heap_del() to delete the element at index 'idx' in the heap. Signed-off-by: Kuan-Wei Chiu Reviewed-by: Ian Rogers --- include/linux/min_heap.h | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/include/linux/min_heap.h b/include/linux/min_heap.h index b1d874f4d536..36023e0be232 100644 --- a/include/linux/min_heap.h +++ b/include/linux/min_heap.h @@ -194,4 +194,28 @@ void __min_heap_push(struct __min_heap *heap, const void *element, size_t elem_s #define min_heap_push(_heap, _element, _func) \ __min_heap_push(&(_heap)->heap, _element, __minheap_obj_size(_heap), _func) +/* Remove ith element from the heap, O(log2(nr)). */ +static __always_inline +bool __min_heap_del(struct __min_heap *heap, size_t elem_size, size_t idx, + const struct min_heap_callbacks *func, void *args) +{ + void *data = heap->data; + + if (WARN_ONCE(heap->nr <= 0, "Popping an empty heap")) + return false; + + /* Place last element at the root (position 0) and then sift down. */ + heap->nr--; + if (idx == heap->nr) + return true; + memcpy(data, data + (heap->nr * elem_size), elem_size); + __min_heap_sift_up(heap, elem_size, idx, func, args); + __min_heapify(heap, idx, elem_size, func, args); + + return true; +} + +#define min_heap_del(_heap, _idx, _func, _args) \ + __min_heap_del(&(_heap)->heap, __minheap_obj_size(_heap), _idx, _func, _args) + #endif /* _LINUX_MIN_HEAP_H */ From patchwork Wed Mar 20 14:54:11 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kuan-Wei Chiu X-Patchwork-Id: 13597946 X-Patchwork-Delegate: snitzer@redhat.com Received: from mail-pl1-f171.google.com (mail-pl1-f171.google.com [209.85.214.171]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 8FB5958113 for ; Wed, 20 Mar 2024 14:55:08 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.171 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1710946509; cv=none; b=LkZ4tEkLQSyUVzKTEyiSUq3cMEOjZi0VzAQximy2gxzMB6xcx7oKVZGGDumYiVDhtRj16b4xlciMzYXBHrXHdYY2mKg34UZ5QICoeMRWjV2EUqgusWVhl9QaBSqywG9EK41VuQnEA40ZgGLAOSCxg8M9jJIgxjRVVURDiKUTRX0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1710946509; c=relaxed/simple; bh=3u7SHTx5BWR6PFeMX82UOtRGC+rWOaoSO0kUgAe6Dt4=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=KKFvjk+wVYyMOPLm+wdUO+to26lldec2qIwSjI6wBoBn8jFXQtKQ9V1AlL9rBCfT5ZR/ILbeyZSLkkHdlVt1I3z+DfHNBkjDC1HG5YogiikZgPy8RwI13naEMEOgtNF0MDQXqP9d+DqYOiBKO6z5lfdDmkSOzsZNyiWkxBiMz5o= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=Lz3N1YIu; arc=none smtp.client-ip=209.85.214.171 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="Lz3N1YIu" Received: by mail-pl1-f171.google.com with SMTP id d9443c01a7336-1dde24ec08cso8716855ad.1 for ; Wed, 20 Mar 2024 07:55:08 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1710946508; x=1711551308; darn=lists.linux.dev; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=BaX+3tHSfR6Y1hlzEWAzFG8m2A4U3O7BTlzKixVR79s=; b=Lz3N1YIu7FtUGBdgNmjItSJ316cEsWaLJCsc1zUFVbV13A52eGoivicYTenIqiZQjA 0D2kuzHXGemD2bTXGbZOo9HBUKBrdrV7L2ZgD0P0Yg0SfmRZuVDvTdrFZfK/vCjlyLpy 5L23civhaUiuN4nP3qzYSi4wHm7yQYKumFlIfI72UZUjS2+MnLUeA7yA6AgJROeRiLLj CS71z1Zuqm3tW8JPuoNicAdwr91WMw69MbI+unU94IBAmLahH86qTH2sksPi7b5oyqdB z+qaPaAyBbD8dSM3II4ajBiA2kybotbWcy+4ipakv1xt6fuw7N5jEwKCbB9Vpoq+8sbp vinA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1710946508; x=1711551308; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=BaX+3tHSfR6Y1hlzEWAzFG8m2A4U3O7BTlzKixVR79s=; b=F4/0W1yBUpJOqteRu1uc7arl9TVWoqOgMgAIbTNSj+eJbcWT5hACdwOaDPlAhgh+Uj XWax3eWaJPhp/zKo8U/BoGIvLRWjHqQE0TW2KluY0/sTX+ntaLxjXo/g8mBjGnsGskG+ +G6uWmCwUTkqvur8xzupU4IdMhl3oqAWvfRtEwuHi6ZGWSZsSgoDf5rTTouV2YIvgkSB /uLAGE2u2pj5OCfUiIWxphf1kzIesP0unFmh3LZwDfh+LTAZ8xmwBhrhmbnSZnuW8CtL xu4tcKSIGyTcThMm9/vlQYRXkfGbjVqfP+N/5GWK4Xwexf7s1lRLTtUykmOH/EElm9/0 aRew== X-Forwarded-Encrypted: i=1; AJvYcCUbIGTeT3Jp0jvBjx2BP4JRpgkflNgvQhwC+1hNuLF1Z1LV8+DePmPh1fEoYtJJOhsRulg6ycqlUPmW97cwaVHKpK0yl6msMsw= X-Gm-Message-State: AOJu0YxyoCC10gQ3nzVfDRv6TQMKT7MtW+VCZD4v9QzokIJf5WVqUS9L hD2qzl0/Pv9zPQO+wZAQ74E82PcqaQ7lh9wgoD0CH/Lz10uuV14i X-Google-Smtp-Source: AGHT+IGd6DO4qwAk4p4e7JmQ1R4ZPRJj4m0jMv3XVjEK0nV1sCafyICIXS1wY7+2qQE9k1iEQlC2Rw== X-Received: by 2002:a17:903:18e:b0:1dd:b54c:df51 with SMTP id z14-20020a170903018e00b001ddb54cdf51mr6005831plg.4.1710946507885; Wed, 20 Mar 2024 07:55:07 -0700 (PDT) Received: from visitorckw-System-Product-Name.. ([140.113.216.168]) by smtp.gmail.com with ESMTPSA id la11-20020a170902fa0b00b001dc30f13e6asm13719989plb.137.2024.03.20.07.55.04 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 20 Mar 2024 07:55:07 -0700 (PDT) From: Kuan-Wei Chiu To: colyli@suse.de, kent.overstreet@linux.dev, msakai@redhat.com, peterz@infradead.org, mingo@redhat.com, acme@kernel.org, namhyung@kernel.org, akpm@linux-foundation.org Cc: bfoster@redhat.com, mark.rutland@arm.com, alexander.shishkin@linux.intel.com, jolsa@kernel.org, irogers@google.com, adrian.hunter@intel.com, jserv@ccns.ncku.edu.tw, dm-devel@lists.linux.dev, linux-bcache@vger.kernel.org, linux-kernel@vger.kernel.org, linux-bcachefs@vger.kernel.org, linux-perf-users@vger.kernel.org, Kuan-Wei Chiu Subject: [PATCH v2 09/15] lib min_heap: Add min_heap_sift_up() Date: Wed, 20 Mar 2024 22:54:11 +0800 Message-Id: <20240320145417.336208-10-visitorckw@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240320145417.336208-1-visitorckw@gmail.com> References: <20240320145417.336208-1-visitorckw@gmail.com> Precedence: bulk X-Mailing-List: dm-devel@lists.linux.dev List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Add min_heap_sift_up() to sift up the element at index 'idx' in the heap. Signed-off-by: Kuan-Wei Chiu --- include/linux/min_heap.h | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/include/linux/min_heap.h b/include/linux/min_heap.h index 36023e0be232..af12531474a4 100644 --- a/include/linux/min_heap.h +++ b/include/linux/min_heap.h @@ -194,6 +194,26 @@ void __min_heap_push(struct __min_heap *heap, const void *element, size_t elem_s #define min_heap_push(_heap, _element, _func) \ __min_heap_push(&(_heap)->heap, _element, __minheap_obj_size(_heap), _func) +/* Sift up ith element from the heap, O(log2(nr)). */ +static __always_inline +void __min_heap_sift_up(struct __min_heap *heap, size_t elem_size, size_t idx, + const struct min_heap_callbacks *func, void *args) +{ + void *data = heap->data; + size_t parent; + + while (idx) { + parent = (idx - 1) / 2; + if (func->less(data + parent * elem_size, data + idx * elem_size, args)) + break; + func->swp(data + parent * elem_size, data + idx * elem_size, args); + idx = parent; + } +} + +#define min_heap_sift_up(_heap, _idx, _func, _args) \ + __min_heap_sift_up(&(_heap)->heap, __minheap_obj_size(_heap), _idx, _func, _args) + /* Remove ith element from the heap, O(log2(nr)). */ static __always_inline bool __min_heap_del(struct __min_heap *heap, size_t elem_size, size_t idx, From patchwork Wed Mar 20 14:54:12 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kuan-Wei Chiu X-Patchwork-Id: 13597947 X-Patchwork-Delegate: snitzer@redhat.com Received: from mail-pf1-f180.google.com (mail-pf1-f180.google.com [209.85.210.180]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 461F258AAD for ; Wed, 20 Mar 2024 14:55:13 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.180 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1710946515; cv=none; b=ujMJTW2r0fNfV8XT9Q3rrRZhnyuIBAYcLaTFBdS732tpwWMivzg319Jf4Tq3ZbrGfHIpf0Dof7aWqnwRq3WibUMLtCp3+I3A5ceBDuxwazBWbjjLnj9a3jpf5HshixQw6vv8Nn0/nIGSCBpwgCI3Ci3fr0uDLWFnM66LA7vHr+o= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1710946515; c=relaxed/simple; bh=v2QDGboZDpSdtXVf/44hZgB01YixUJPmq1o6qcA+7gA=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=kR5UyvIOb6vU/o2xlHGA85UlfWzGz+aOSRsteU9G4O2/M8e0KXtqQICyhIA2odITU5HFBTjfU4byQjhnJkWPyva0g1T2pHGIegJ7WhQsf9FZXs+GOE74CwCNiSWWPfaOOWBJ4daN2YQaAm7TJ48GlOBXfyXpaSStE7WRhhDy86U= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=fOKMxzvD; arc=none smtp.client-ip=209.85.210.180 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="fOKMxzvD" Received: by mail-pf1-f180.google.com with SMTP id d2e1a72fcca58-6e961027e7aso45284b3a.1 for ; Wed, 20 Mar 2024 07:55:13 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1710946512; x=1711551312; darn=lists.linux.dev; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=3j4BwpnzsCTFbTLEfHkq7OVaw0zB9zsQ8DozRNlNpcs=; b=fOKMxzvDNHQ4EJq2jo9bqH2/GVzabZdGrH9PLnljhMge2bzHaPsS9vrx6TK3ZoCwDh tKCledY/4oZiNuARl61VIMQtnreBH6CLvc/zXV4nqYodSThbJ6IbEIqNzZKBkKTFZu8W CW9KR2MmEbGKL2BTqo9zDtsUeXu0ycoL1Y7681T8Ew7lHPCbilfcVK+1Ix+3IlltKlUG k8qpw6uplHGla0df3mpddl/U55vlBKDlEWxpnTVFTfEe5C2LVszGC5ajKtPAOMeZmEMa jS9G1+8tPuXutfCGkLkAvGAnuIF9182n/p/lSYOXQ2bbQxncVxFyrxOm6G+jYDbB2Cvn 18QA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1710946512; x=1711551312; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=3j4BwpnzsCTFbTLEfHkq7OVaw0zB9zsQ8DozRNlNpcs=; b=lEL4dwGbbDpUaZQCQlegT/OGm6aXafepje/9lou0vl0rl6LN0XekUb79nMQFGO8Ykw ieeLnmkcNvlK7w5NV0lOR3NcvJvxHCvqj5s0iXnuAKc6AUifCgvz1AeYCoNShQgt/RCs 8hp/fHBIrKQZ6mNCjB2OGFeG2ZQg4g3LweN0HGNqs3YwrgDUBvEw2fW7qqt/gRjSfw/V j1v9+A+wCVPqY9vljuD7O+4mcHWYd68G4flwdCuyQdrhojDTUpuJnuD9/Lv0JUmfs51i ArQGKRAjDzaoAVBlbQfQbweIo0qtHdimbHbcJkZVNZrAej3saHTFvHwVmvB3SJQEMH6V 7BWA== X-Forwarded-Encrypted: i=1; AJvYcCXJebxEPpXGvP9KF6C80I9its9OpT4mgdKtg5CZxswxX9Z1F6NtU+7+vw8rkWq3bfV4WSCsBEXx1dq7Fgxv36xkO7mE2PnpwoE= X-Gm-Message-State: AOJu0YzP4RdfqqqTUZSl5rI4//IpTUP8dLzispemWc3rNAifXRbcE9+d JSdFVdKUhDrhmdnotJzhAOmJs7t626wp8AZHcI8ZwVkjejQ7loMS X-Google-Smtp-Source: AGHT+IEjtfUBQQINRHbNCFh+4mc0PM0bj31+zrpBMLrSahwyX96NxG1VTkI8k/KehbVPts8K4VsiIA== X-Received: by 2002:a05:6a20:7f8f:b0:1a3:4721:ded3 with SMTP id d15-20020a056a207f8f00b001a34721ded3mr6093871pzj.1.1710946512376; Wed, 20 Mar 2024 07:55:12 -0700 (PDT) Received: from visitorckw-System-Product-Name.. ([140.113.216.168]) by smtp.gmail.com with ESMTPSA id la11-20020a170902fa0b00b001dc30f13e6asm13719989plb.137.2024.03.20.07.55.08 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 20 Mar 2024 07:55:11 -0700 (PDT) From: Kuan-Wei Chiu To: colyli@suse.de, kent.overstreet@linux.dev, msakai@redhat.com, peterz@infradead.org, mingo@redhat.com, acme@kernel.org, namhyung@kernel.org, akpm@linux-foundation.org Cc: bfoster@redhat.com, mark.rutland@arm.com, alexander.shishkin@linux.intel.com, jolsa@kernel.org, irogers@google.com, adrian.hunter@intel.com, jserv@ccns.ncku.edu.tw, dm-devel@lists.linux.dev, linux-bcache@vger.kernel.org, linux-kernel@vger.kernel.org, linux-bcachefs@vger.kernel.org, linux-perf-users@vger.kernel.org, Kuan-Wei Chiu Subject: [PATCH v2 10/15] lib min_heap: Add args for min_heap_callbacks Date: Wed, 20 Mar 2024 22:54:12 +0800 Message-Id: <20240320145417.336208-11-visitorckw@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240320145417.336208-1-visitorckw@gmail.com> References: <20240320145417.336208-1-visitorckw@gmail.com> Precedence: bulk X-Mailing-List: dm-devel@lists.linux.dev List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Add a third parameter 'args' for the 'less' and 'swp' functions in the 'struct min_heap_callbacks'. This additional parameter allows these comparison and swap functions to handle extra arguments when necessary. Signed-off-by: Kuan-Wei Chiu Reviewed-by: Ian Rogers --- Changes in v2: - Add attribute __always_unused to the compare and swap functions that do not use the args parameter. drivers/md/dm-vdo/repair.c | 10 +++---- drivers/md/dm-vdo/slab-depot.c | 9 +++--- include/linux/min_heap.h | 51 +++++++++++++++++----------------- kernel/events/core.c | 10 +++---- lib/test_min_heap.c | 26 ++++++++--------- 5 files changed, 54 insertions(+), 52 deletions(-) diff --git a/drivers/md/dm-vdo/repair.c b/drivers/md/dm-vdo/repair.c index 7663fa2098f4..6acaebcd305d 100644 --- a/drivers/md/dm-vdo/repair.c +++ b/drivers/md/dm-vdo/repair.c @@ -137,7 +137,7 @@ struct repair_completion { * to sort by slot while still ensuring we replay all entries with the same slot in the exact order * as they appeared in the journal. */ -static bool mapping_is_less_than(const void *item1, const void *item2) +static bool mapping_is_less_than(const void *item1, const void *item2, void __always_unused *args) { const struct numbered_block_mapping *mapping1 = (const struct numbered_block_mapping *) item1; @@ -156,7 +156,7 @@ static bool mapping_is_less_than(const void *item1, const void *item2) return 0; } -static void swap_mappings(void *item1, void *item2) +static void swap_mappings(void *item1, void *item2, void __always_unused *args) { struct numbered_block_mapping *mapping1 = item1; struct numbered_block_mapping *mapping2 = item2; @@ -182,8 +182,8 @@ static struct numbered_block_mapping *sort_next_heap_element(struct repair_compl * restore the heap invariant, and return a pointer to the popped element. */ last = &repair->entries[--heap->heap.nr]; - swap_mappings(heap->heap.data, last); - min_heapify(heap, 0, &repair_min_heap); + swap_mappings(heap->heap.data, last, NULL); + min_heapify(heap, 0, &repair_min_heap, NULL); return last; } @@ -1121,7 +1121,7 @@ static void recover_block_map(struct vdo_completion *completion) repair->replay_heap.heap.data = repair->entries; repair->replay_heap.heap.nr = repair->block_map_entry_count; repair->replay_heap.heap.size = repair->block_map_entry_count; - min_heapify_all(&repair->replay_heap, &repair_min_heap); + min_heapify_all(&repair->replay_heap, &repair_min_heap, NULL); vdo_log_info("Replaying %zu recovery entries into block map", repair->block_map_entry_count); diff --git a/drivers/md/dm-vdo/slab-depot.c b/drivers/md/dm-vdo/slab-depot.c index 3309480170c3..102f492bb1f8 100644 --- a/drivers/md/dm-vdo/slab-depot.c +++ b/drivers/md/dm-vdo/slab-depot.c @@ -3288,7 +3288,8 @@ int vdo_release_block_reference(struct block_allocator *allocator, * Thus, the ordering is reversed from the usual sense since min_heap returns smaller elements * before larger ones. */ -static bool slab_status_is_less_than(const void *item1, const void *item2) +static bool slab_status_is_less_than(const void *item1, const void *item2, + void __always_unused *args) { const struct slab_status *info1 = item1; const struct slab_status *info2 = item2; @@ -3300,7 +3301,7 @@ static bool slab_status_is_less_than(const void *item1, const void *item2) return info1->slab_number < info2->slab_number; } -static void swap_slab_statuses(void *item1, void *item2) +static void swap_slab_statuses(void *item1, void *item2, void __always_unused *args) { struct slab_status *info1 = item1; struct slab_status *info2 = item2; @@ -3523,7 +3524,7 @@ static int __must_check vdo_prepare_slabs_for_allocation(struct block_allocator heap.heap.data = slab_statuses; heap.heap.nr = allocator->slab_count; heap.heap.size = allocator->slab_count; - min_heapify_all(&heap, &slab_status_min_heap); + min_heapify_all(&heap, &slab_status_min_heap, NULL); while (heap.heap.nr > 0) { bool high_priority; @@ -3531,7 +3532,7 @@ static int __must_check vdo_prepare_slabs_for_allocation(struct block_allocator struct slab_journal *journal; current_slab_status = slab_statuses[0]; - min_heap_pop(&heap, &slab_status_min_heap); + min_heap_pop(&heap, &slab_status_min_heap, NULL); slab = depot->slabs[current_slab_status.slab_number]; if ((depot->load_type == VDO_SLAB_DEPOT_REBUILD_LOAD) || diff --git a/include/linux/min_heap.h b/include/linux/min_heap.h index af12531474a4..879a9d12e030 100644 --- a/include/linux/min_heap.h +++ b/include/linux/min_heap.h @@ -40,8 +40,8 @@ struct _name { \ * @swp: Swap elements function. */ struct min_heap_callbacks { - bool (*less)(const void *lhs, const void *rhs); - void (*swp)(void *lhs, void *rhs); + bool (*less)(const void *lhs, const void *rhs, void *args); + void (*swp)(void *lhs, void *rhs, void *args); }; /* Initialize a min-heap. */ @@ -79,7 +79,7 @@ bool __min_heap_full(struct __min_heap *heap) /* Sift the element at pos down the heap. */ static __always_inline void __min_heapify(struct __min_heap *heap, int pos, size_t elem_size, - const struct min_heap_callbacks *func) + const struct min_heap_callbacks *func, void *args) { void *left, *right; void *data = heap->data; @@ -92,7 +92,7 @@ void __min_heapify(struct __min_heap *heap, int pos, size_t elem_size, break; left = data + (i * 2 + 1) * elem_size; right = data + (i * 2 + 2) * elem_size; - i = func->less(left, right) ? i * 2 + 1 : i * 2 + 2; + i = func->less(left, right, args) ? i * 2 + 1 : i * 2 + 2; } /* Special case for the last leaf with no sibling. */ @@ -100,38 +100,38 @@ void __min_heapify(struct __min_heap *heap, int pos, size_t elem_size, i = i * 2 + 1; /* Backtrack to the correct location. */ - while (i != pos && func->less(root, data + i * elem_size)) + while (i != pos && func->less(root, data + i * elem_size, args)) i = (i - 1) / 2; /* Shift the element into its correct place. */ j = i; while (i != pos) { i = (i - 1) / 2; - func->swp(data + i * elem_size, data + j * elem_size); + func->swp(data + i * elem_size, data + j * elem_size, args); } } -#define min_heapify(_heap, _pos, _func) \ - __min_heapify(&(_heap)->heap, _pos, __minheap_obj_size(_heap), _func) +#define min_heapify(_heap, _pos, _func, _args) \ + __min_heapify(&(_heap)->heap, _pos, __minheap_obj_size(_heap), _func, _args) /* Floyd's approach to heapification that is O(nr). */ static __always_inline void __min_heapify_all(struct __min_heap *heap, size_t elem_size, - const struct min_heap_callbacks *func) + const struct min_heap_callbacks *func, void *args) { int i; for (i = heap->nr / 2 - 1; i >= 0; i--) - __min_heapify(heap, i, elem_size, func); + __min_heapify(heap, i, elem_size, func, args); } -#define min_heapify_all(_heap, _func) \ - __min_heapify_all(&(_heap)->heap, __minheap_obj_size(_heap), _func) +#define min_heapify_all(_heap, _func, _args) \ + __min_heapify_all(&(_heap)->heap, __minheap_obj_size(_heap), _func, _args) /* Remove minimum element from the heap, O(log2(nr)). */ static __always_inline void __min_heap_pop(struct __min_heap *heap, size_t elem_size, - const struct min_heap_callbacks *func) + const struct min_heap_callbacks *func, void *args) { void *data = heap->data; @@ -141,11 +141,11 @@ void __min_heap_pop(struct __min_heap *heap, size_t elem_size, /* Place last element at the root (position 0) and then sift down. */ heap->nr--; memcpy(data, data + (heap->nr * elem_size), elem_size); - __min_heapify(heap, 0, elem_size, func); + __min_heapify(heap, 0, elem_size, func, args); } -#define min_heap_pop(_heap, _func) \ - __min_heap_pop(&(_heap)->heap, __minheap_obj_size(_heap), _func) +#define min_heap_pop(_heap, _func, _args) \ + __min_heap_pop(&(_heap)->heap, __minheap_obj_size(_heap), _func, _args) /* * Remove the minimum element and then push the given element. The @@ -155,19 +155,20 @@ void __min_heap_pop(struct __min_heap *heap, size_t elem_size, static __always_inline void __min_heap_pop_push(struct __min_heap *heap, const void *element, size_t elem_size, - const struct min_heap_callbacks *func) + const struct min_heap_callbacks *func, + void *args) { memcpy(heap->data, element, elem_size); - __min_heapify(heap, 0, elem_size, func); + __min_heapify(heap, 0, elem_size, func, args); } -#define min_heap_pop_push(_heap, _element, _func) \ - __min_heap_pop_push(&(_heap)->heap, _element, __minheap_obj_size(_heap), _func) +#define min_heap_pop_push(_heap, _element, _func, _args) \ + __min_heap_pop_push(&(_heap)->heap, _element, __minheap_obj_size(_heap), _func, _args) /* Push an element on to the heap, O(log2(nr)). */ static __always_inline void __min_heap_push(struct __min_heap *heap, const void *element, size_t elem_size, - const struct min_heap_callbacks *func) + const struct min_heap_callbacks *func, void *args) { void *data = heap->data; void *child, *parent; @@ -185,14 +186,14 @@ void __min_heap_push(struct __min_heap *heap, const void *element, size_t elem_s for (; pos > 0; pos = (pos - 1) / 2) { child = data + (pos * elem_size); parent = data + ((pos - 1) / 2) * elem_size; - if (func->less(parent, child)) + if (func->less(parent, child, args)) break; - func->swp(parent, child); + func->swp(parent, child, args); } } -#define min_heap_push(_heap, _element, _func) \ - __min_heap_push(&(_heap)->heap, _element, __minheap_obj_size(_heap), _func) +#define min_heap_push(_heap, _element, _func, _args) \ + __min_heap_push(&(_heap)->heap, _element, __minheap_obj_size(_heap), _func, _args) /* Sift up ith element from the heap, O(log2(nr)). */ static __always_inline diff --git a/kernel/events/core.c b/kernel/events/core.c index 065dfaa8b009..c32a78c489f3 100644 --- a/kernel/events/core.c +++ b/kernel/events/core.c @@ -3683,7 +3683,7 @@ void __perf_event_task_sched_out(struct task_struct *task, perf_cgroup_switch(next); } -static bool perf_less_group_idx(const void *l, const void *r) +static bool perf_less_group_idx(const void *l, const void *r, void __always_unused *args) { const struct perf_event *le = *(const struct perf_event **)l; const struct perf_event *re = *(const struct perf_event **)r; @@ -3691,7 +3691,7 @@ static bool perf_less_group_idx(const void *l, const void *r) return le->group_index < re->group_index; } -static void swap_ptr(void *l, void *r) +static void swap_ptr(void *l, void *r, void __always_unused *args) { void **lp = l, **rp = r; @@ -3779,7 +3779,7 @@ static noinline int visit_groups_merge(struct perf_event_context *ctx, perf_assert_pmu_disabled((*evt)->pmu_ctx->pmu); } - min_heapify_all(&event_heap, &perf_min_heap); + min_heapify_all(&event_heap, &perf_min_heap, NULL); while (event_heap.heap.nr) { ret = func(*evt, data); @@ -3788,9 +3788,9 @@ static noinline int visit_groups_merge(struct perf_event_context *ctx, *evt = perf_event_groups_next(*evt, pmu); if (*evt) - min_heapify(&event_heap, 0, &perf_min_heap); + min_heapify(&event_heap, 0, &perf_min_heap, NULL); else - min_heap_pop(&event_heap, &perf_min_heap); + min_heap_pop(&event_heap, &perf_min_heap, NULL); } return 0; diff --git a/lib/test_min_heap.c b/lib/test_min_heap.c index af2e446034d8..062e908e9fa3 100644 --- a/lib/test_min_heap.c +++ b/lib/test_min_heap.c @@ -13,17 +13,17 @@ MIN_HEAP(int, min_heap_test); -static __init bool less_than(const void *lhs, const void *rhs) +static __init bool less_than(const void *lhs, const void *rhs, void __always_unused *args) { return *(int *)lhs < *(int *)rhs; } -static __init bool greater_than(const void *lhs, const void *rhs) +static __init bool greater_than(const void *lhs, const void *rhs, void __always_unused *args) { return *(int *)lhs > *(int *)rhs; } -static __init void swap_ints(void *lhs, void *rhs) +static __init void swap_ints(void *lhs, void *rhs, void __always_unused *args) { int temp = *(int *)lhs; @@ -40,7 +40,7 @@ static __init int pop_verify_heap(bool min_heap, int last; last = values[0]; - min_heap_pop(heap, funcs); + min_heap_pop(heap, funcs, NULL); while (heap->heap.nr > 0) { if (min_heap) { if (last > values[0]) { @@ -56,7 +56,7 @@ static __init int pop_verify_heap(bool min_heap, } } last = values[0]; - min_heap_pop(heap, funcs); + min_heap_pop(heap, funcs, NULL); } return err; } @@ -77,7 +77,7 @@ static __init int test_heapify_all(bool min_heap) int i, err; /* Test with known set of values. */ - min_heapify_all(&heap, &funcs); + min_heapify_all(&heap, &funcs, NULL); err = pop_verify_heap(min_heap, &heap, &funcs); @@ -86,7 +86,7 @@ static __init int test_heapify_all(bool min_heap) for (i = 0; i < heap.heap.nr; i++) values[i] = get_random_u32(); - min_heapify_all(&heap, &funcs); + min_heapify_all(&heap, &funcs, NULL); err += pop_verify_heap(min_heap, &heap, &funcs); return err; @@ -110,14 +110,14 @@ static __init int test_heap_push(bool min_heap) /* Test with known set of values copied from data. */ for (i = 0; i < ARRAY_SIZE(data); i++) - min_heap_push(&heap, &data[i], &funcs); + min_heap_push(&heap, &data[i], &funcs, NULL); err = pop_verify_heap(min_heap, &heap, &funcs); /* Test with randomly generated values. */ while (heap.heap.nr < heap.heap.size) { temp = get_random_u32(); - min_heap_push(&heap, &temp, &funcs); + min_heap_push(&heap, &temp, &funcs, NULL); } err += pop_verify_heap(min_heap, &heap, &funcs); @@ -143,22 +143,22 @@ static __init int test_heap_pop_push(bool min_heap) /* Fill values with data to pop and replace. */ temp = min_heap ? 0x80000000 : 0x7FFFFFFF; for (i = 0; i < ARRAY_SIZE(data); i++) - min_heap_push(&heap, &temp, &funcs); + min_heap_push(&heap, &temp, &funcs, NULL); /* Test with known set of values copied from data. */ for (i = 0; i < ARRAY_SIZE(data); i++) - min_heap_pop_push(&heap, &data[i], &funcs); + min_heap_pop_push(&heap, &data[i], &funcs, NULL); err = pop_verify_heap(min_heap, &heap, &funcs); heap.heap.nr = 0; for (i = 0; i < ARRAY_SIZE(data); i++) - min_heap_push(&heap, &temp, &funcs); + min_heap_push(&heap, &temp, &funcs, NULL); /* Test with randomly generated values. */ for (i = 0; i < ARRAY_SIZE(data); i++) { temp = get_random_u32(); - min_heap_pop_push(&heap, &temp, &funcs); + min_heap_pop_push(&heap, &temp, &funcs, NULL); } err += pop_verify_heap(min_heap, &heap, &funcs); From patchwork Wed Mar 20 14:54:13 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kuan-Wei Chiu X-Patchwork-Id: 13597948 X-Patchwork-Delegate: snitzer@redhat.com Received: from mail-pl1-f178.google.com (mail-pl1-f178.google.com [209.85.214.178]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id AEE8658AC1 for ; Wed, 20 Mar 2024 14:55:17 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.178 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1710946519; cv=none; b=P3ROsliM2iRTmfIHZSUVRquvKCUA/1eoF2WnhY3p+2OuyqePeYHoY+oPHv+El4k9SVvB9WhlMZwXaiHu2yGvI0iwo3/JTpDxgYftxqhb1rw0Jwf818Rktfk93MBZDym2YVVha0USHJ2OGFnEaohTpfKfNz19cvSdUllnrrOUo/I= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1710946519; c=relaxed/simple; bh=syfYOYnUwJ/5pwXO5mVU6Pp5rxMGLYlg5kt0rZBWh54=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=sE2R4QyS19swMh7Ian1/L6xTJuxM+5C2ETxQM7HXeMIU4OS4wTq2Vb6jLVpG2m96TBPDazOZjtM1Slqz9M6CnrlCOHqfcQQKzGDBdHcHW/wJg/6a0dKYwkzlKsFyMVkz9Xl4piVHr7o4Gv7aNsJNRybCemwbH2YHqOn0KQDIdrY= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=lf5+NsQN; arc=none smtp.client-ip=209.85.214.178 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="lf5+NsQN" Received: by mail-pl1-f178.google.com with SMTP id d9443c01a7336-1def81ee762so9761015ad.0 for ; Wed, 20 Mar 2024 07:55:17 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1710946517; x=1711551317; darn=lists.linux.dev; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=cESTsZP2b4VWyk/Zv78LUG0DCiwWqDlR4UPH+ifArVI=; b=lf5+NsQNlYFy1mQSVYJ2TzZRd/vX1w9VARchsqaIOlFXITZCxJO/eHWUq+/erntc7g jzh+aSfecudMHrCp4hvSz7N/y+RQjg5IVV3+PSM6A8jGPK5Ua+gLJXDYuHZGj2j9fspc aMuTOIArCVWVubt/ebdP1XmHcO+ZMzLZa0ZvrcnUTQlKppOQ2+NIdH2xTIdM5pZtQHsW dMH2KKDfcH6LhDYL0rC8q3z0cmirCkCOAed7m5peh5Cqg/k1lJcKPQvZ0h9k5j5ZqeKx ahDqq42OzEwjRV44+K9AD5aAhft5R8PnL6i/BcN7FPZuCSyAwwMnCjECWekv3ncpwxvb WVDA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1710946517; x=1711551317; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=cESTsZP2b4VWyk/Zv78LUG0DCiwWqDlR4UPH+ifArVI=; b=a9kQn8xMzIvBcawA3+OJ5o6v0bahfSm6FK+Sbbot6TDml+VZIj92RsYi3RhR+P/IEe AAHL637wvR7QouvTEglC2KNsyVYwqkHQ2YKX2kq2LhMZ0znUAWQv3Gc5W2dN+qHRHXnj wbM4N7VbCR0s0rK1IlmlL98JeUSKpEx/gswTFlo/GOlgkUpSRn/yM6FL6SIOUbxQ2WCF J1dETsBPumoGPlhlBC7rKP5fg16Qs2cPxgEDeQgd++aT0uZLSIyRvqgZGGvzNv3wycd+ ACg/6CPSLI60B5dSG4EOt+HezgYUZGjeLK5dddbobsCpfv3xxNQQqfK2PBaipAgb+IKS 3ZJA== X-Forwarded-Encrypted: i=1; AJvYcCUvtKE6OITiBXKTfYwVL1Ucs3OfGdBgFt+gpKQGw7MnuI4K3w9g6Jk5Lv5GvL7IlM0or62nXUw02gySVnC5HAr2tySf5XOZsFs= X-Gm-Message-State: AOJu0Yw1Tv1E0x2Q+5+uRsNiudkNVrJ2/r2IAS8DO4DL25XNTU3S9Ywb t5LH94IULIOu6rwVeU36fjjPPRAUw23YNeCEMIxC5A0m3yF3GMW4pfU8V+CWYSEsvQ== X-Google-Smtp-Source: AGHT+IFyBgv5oFc1N7tzqDalBpTJCZL08QL7Xi0JuKEsJZmPb5q63CmqPKSNUk+sFsubtLKiucyg1g== X-Received: by 2002:a17:903:2292:b0:1dd:5a49:7a98 with SMTP id b18-20020a170903229200b001dd5a497a98mr6117689plh.3.1710946517076; Wed, 20 Mar 2024 07:55:17 -0700 (PDT) Received: from visitorckw-System-Product-Name.. ([140.113.216.168]) by smtp.gmail.com with ESMTPSA id la11-20020a170902fa0b00b001dc30f13e6asm13719989plb.137.2024.03.20.07.55.13 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 20 Mar 2024 07:55:16 -0700 (PDT) From: Kuan-Wei Chiu To: colyli@suse.de, kent.overstreet@linux.dev, msakai@redhat.com, peterz@infradead.org, mingo@redhat.com, acme@kernel.org, namhyung@kernel.org, akpm@linux-foundation.org Cc: bfoster@redhat.com, mark.rutland@arm.com, alexander.shishkin@linux.intel.com, jolsa@kernel.org, irogers@google.com, adrian.hunter@intel.com, jserv@ccns.ncku.edu.tw, dm-devel@lists.linux.dev, linux-bcache@vger.kernel.org, linux-kernel@vger.kernel.org, linux-bcachefs@vger.kernel.org, linux-perf-users@vger.kernel.org, Kuan-Wei Chiu Subject: [PATCH v2 11/15] lib min_heap: Update min_heap_push() and min_heap_pop() to return bool values Date: Wed, 20 Mar 2024 22:54:13 +0800 Message-Id: <20240320145417.336208-12-visitorckw@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240320145417.336208-1-visitorckw@gmail.com> References: <20240320145417.336208-1-visitorckw@gmail.com> Precedence: bulk X-Mailing-List: dm-devel@lists.linux.dev List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Modify the min_heap_push() and min_heap_pop() to return a boolean value. They now return false when the operation fails and true when it succeeds. Signed-off-by: Kuan-Wei Chiu Reviewed-by: Ian Rogers --- include/linux/min_heap.h | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/include/linux/min_heap.h b/include/linux/min_heap.h index 879a9d12e030..586965977104 100644 --- a/include/linux/min_heap.h +++ b/include/linux/min_heap.h @@ -130,18 +130,20 @@ void __min_heapify_all(struct __min_heap *heap, size_t elem_size, /* Remove minimum element from the heap, O(log2(nr)). */ static __always_inline -void __min_heap_pop(struct __min_heap *heap, size_t elem_size, +bool __min_heap_pop(struct __min_heap *heap, size_t elem_size, const struct min_heap_callbacks *func, void *args) { void *data = heap->data; if (WARN_ONCE(heap->nr <= 0, "Popping an empty heap")) - return; + return false; /* Place last element at the root (position 0) and then sift down. */ heap->nr--; memcpy(data, data + (heap->nr * elem_size), elem_size); __min_heapify(heap, 0, elem_size, func, args); + + return true; } #define min_heap_pop(_heap, _func, _args) \ @@ -167,7 +169,7 @@ void __min_heap_pop_push(struct __min_heap *heap, /* Push an element on to the heap, O(log2(nr)). */ static __always_inline -void __min_heap_push(struct __min_heap *heap, const void *element, size_t elem_size, +bool __min_heap_push(struct __min_heap *heap, const void *element, size_t elem_size, const struct min_heap_callbacks *func, void *args) { void *data = heap->data; @@ -175,7 +177,7 @@ void __min_heap_push(struct __min_heap *heap, const void *element, size_t elem_s int pos; if (WARN_ONCE(heap->nr >= heap->size, "Pushing on a full heap")) - return; + return false; /* Place at the end of data. */ pos = heap->nr; @@ -190,6 +192,8 @@ void __min_heap_push(struct __min_heap *heap, const void *element, size_t elem_s break; func->swp(parent, child, args); } + + return true; } #define min_heap_push(_heap, _element, _func, _args) \ From patchwork Wed Mar 20 14:54:14 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kuan-Wei Chiu X-Patchwork-Id: 13597949 X-Patchwork-Delegate: snitzer@redhat.com Received: from mail-pl1-f176.google.com (mail-pl1-f176.google.com [209.85.214.176]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 884F55FB98 for ; Wed, 20 Mar 2024 14:55:22 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.176 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1710946524; cv=none; b=rPlMiU3xLSP9QyOiHgQjhNcMZ8m4WWkdj6MRgQ6EIdbM/gKw/UEqT2J9b6zK8azhfbpjvZ5MnyuZv5152UaQa1Xj/PFmlcqcxScKmvzXfFmIaanhkkqu9IeLtZGsHg98rJSpD76ojQb20P5pKpqQYbghgd+gxnelQdrSj5Ydqfc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1710946524; c=relaxed/simple; bh=nCQxYZsgsRAx/5f4AE04kZbnhBIilxiKGZiKSwq7sAs=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=fkuDRB/eC2WNUf9sacnHPSOfhw9oCyfWfScMwrxV22ObWFLbNhpoSjkb6EesyhW+93j4wnSVtKMY8XGBpO9Z9fsVh2dNpihFbhEv9gXRe6InZHKrQIEy8/IG9NSV2qUXOPr1nZjNT6mqWThf7RG2+z7NAusSp27N300WQUqopSc= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=EcLwEslE; arc=none smtp.client-ip=209.85.214.176 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="EcLwEslE" Received: by mail-pl1-f176.google.com with SMTP id d9443c01a7336-1dd8ee2441dso11937445ad.1 for ; Wed, 20 Mar 2024 07:55:22 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1710946522; x=1711551322; darn=lists.linux.dev; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=HQTQoXpl9kZmZcCaoDjVcMBQxJEeqtUU5fbEvGOwL9A=; b=EcLwEslENgerZyOkZOGeHZs2r7/JwpI67wja1SfF9MXVRJQ4udAhNMvz50GdmUYqe8 oo+s/XFo+hV4pxFIIk6LcOhQ0pDYFySiXKN5makQWmBp+C3MJ41B7DDjTAP6Jsa1WysK LTtWgCH83wmjhyLO27aMtwEro1SujuzYgwWEeYxq/EE0TAmXdNj3JwuBwlEf9217UVYC 0GHIyepFkrhVSXKw1CSFrohNztiOeVfHTdrqke1DkW7XWAumAf67cb6icyx/o6lpSsua bYK4g4hIpeTRhl+iEv10FqiN4TIjE1NvcrZlqTCdcCJl4m8wUtpBFeuDZMvwqW/FleTm OZAw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1710946522; x=1711551322; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=HQTQoXpl9kZmZcCaoDjVcMBQxJEeqtUU5fbEvGOwL9A=; b=pwku1MNXp4w/95l9KNWnng16vUtp4snsJfZN85/dEhe2eHKBp+IUQMT0nSHHg91F7D /467AfQkgyw2M2H06YZSoWMOCRqTxlmBl3Qp1wPizvZWXrWx0a3gFsNK6xrUTM5Qjwa/ IQ1K8wErb+U8OBJYAUnTXxu+okY5e8qVW8+EJqNS9/2Zcy7AmdQ39o1OZNGuhDHzVJPQ YGhFT0WgN4ssbilbgC2cH0Cx6uEHzEK2qPd/SoLRBej6ZTXV+P0/ShqVqJh0zxDFPGRM M4JMrU1P7JiB/fSBpeAl2loMqoSRI84EmG/ay/eoyUf2J4LHPD5hjk6+xkLnOso2yi2B EfKQ== X-Forwarded-Encrypted: i=1; AJvYcCUAXm5mZDOmJodyFS5ZgtI9zEFUkvKQOVMh7qnWtawyYGN0IEg6UuQ2q9QWeLr2mfCeGuIO21qJqjzbzXQdSdvwXkANZzF+u44= X-Gm-Message-State: AOJu0YyZCjRtY/sro8xExXO5qMeJ1fPml1dw6NhPKifnHohwpYhg2L9Z h9G5FH1qhcV9cB0IYo4RaxHEvXQ0/XRIjVJHsNev1N02CAOaA07Lif9c0qhtYIPk4Q== X-Google-Smtp-Source: AGHT+IEVgULr7fKahVSqb9Mn8wh/rmuWNNtX4qJfG3yVKS6HTCYRncGWrGCpJXxKSTFPl2pdaEjEKw== X-Received: by 2002:a17:902:c94d:b0:1dc:e469:6f5d with SMTP id i13-20020a170902c94d00b001dce4696f5dmr6104360pla.4.1710946521802; Wed, 20 Mar 2024 07:55:21 -0700 (PDT) Received: from visitorckw-System-Product-Name.. ([140.113.216.168]) by smtp.gmail.com with ESMTPSA id la11-20020a170902fa0b00b001dc30f13e6asm13719989plb.137.2024.03.20.07.55.18 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 20 Mar 2024 07:55:21 -0700 (PDT) From: Kuan-Wei Chiu To: colyli@suse.de, kent.overstreet@linux.dev, msakai@redhat.com, peterz@infradead.org, mingo@redhat.com, acme@kernel.org, namhyung@kernel.org, akpm@linux-foundation.org Cc: bfoster@redhat.com, mark.rutland@arm.com, alexander.shishkin@linux.intel.com, jolsa@kernel.org, irogers@google.com, adrian.hunter@intel.com, jserv@ccns.ncku.edu.tw, dm-devel@lists.linux.dev, linux-bcache@vger.kernel.org, linux-kernel@vger.kernel.org, linux-bcachefs@vger.kernel.org, linux-perf-users@vger.kernel.org, Kuan-Wei Chiu Subject: [PATCH v2 12/15] lib min_heap: Rename min_heapify() to min_heap_sift_down() Date: Wed, 20 Mar 2024 22:54:14 +0800 Message-Id: <20240320145417.336208-13-visitorckw@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240320145417.336208-1-visitorckw@gmail.com> References: <20240320145417.336208-1-visitorckw@gmail.com> Precedence: bulk X-Mailing-List: dm-devel@lists.linux.dev List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 After adding min_heap_sift_up(), the naming convention has been adjusted to maintain consistency with the min_heap_sift_up(). Consequently, min_heapify() has been renamed to min_heap_sift_down(). Link: https://lkml.kernel.org/CAP-5=fVcBAxt8Mw72=NCJPRJfjDaJcqk4rjbadgouAEAHz_q1A@mail.gmail.com Signed-off-by: Kuan-Wei Chiu Reviewed-by: Ian Rogers --- drivers/md/dm-vdo/repair.c | 2 +- include/linux/min_heap.h | 14 +++++++------- kernel/events/core.c | 2 +- 3 files changed, 9 insertions(+), 9 deletions(-) diff --git a/drivers/md/dm-vdo/repair.c b/drivers/md/dm-vdo/repair.c index 6acaebcd305d..e99f908bbdb9 100644 --- a/drivers/md/dm-vdo/repair.c +++ b/drivers/md/dm-vdo/repair.c @@ -183,7 +183,7 @@ static struct numbered_block_mapping *sort_next_heap_element(struct repair_compl */ last = &repair->entries[--heap->heap.nr]; swap_mappings(heap->heap.data, last, NULL); - min_heapify(heap, 0, &repair_min_heap, NULL); + min_heap_sift_down(heap, 0, &repair_min_heap, NULL); return last; } diff --git a/include/linux/min_heap.h b/include/linux/min_heap.h index 586965977104..436997070f4e 100644 --- a/include/linux/min_heap.h +++ b/include/linux/min_heap.h @@ -78,7 +78,7 @@ bool __min_heap_full(struct __min_heap *heap) /* Sift the element at pos down the heap. */ static __always_inline -void __min_heapify(struct __min_heap *heap, int pos, size_t elem_size, +void __min_heap_sift_down(struct __min_heap *heap, int pos, size_t elem_size, const struct min_heap_callbacks *func, void *args) { void *left, *right; @@ -111,8 +111,8 @@ void __min_heapify(struct __min_heap *heap, int pos, size_t elem_size, } } -#define min_heapify(_heap, _pos, _func, _args) \ - __min_heapify(&(_heap)->heap, _pos, __minheap_obj_size(_heap), _func, _args) +#define min_heap_sift_down(_heap, _pos, _func, _args) \ + __min_heap_sift_down(&(_heap)->heap, _pos, __minheap_obj_size(_heap), _func, _args) /* Floyd's approach to heapification that is O(nr). */ static __always_inline @@ -122,7 +122,7 @@ void __min_heapify_all(struct __min_heap *heap, size_t elem_size, int i; for (i = heap->nr / 2 - 1; i >= 0; i--) - __min_heapify(heap, i, elem_size, func, args); + __min_heap_sift_down(heap, i, elem_size, func, args); } #define min_heapify_all(_heap, _func, _args) \ @@ -141,7 +141,7 @@ bool __min_heap_pop(struct __min_heap *heap, size_t elem_size, /* Place last element at the root (position 0) and then sift down. */ heap->nr--; memcpy(data, data + (heap->nr * elem_size), elem_size); - __min_heapify(heap, 0, elem_size, func, args); + __min_heap_sift_down(heap, 0, elem_size, func, args); return true; } @@ -161,7 +161,7 @@ void __min_heap_pop_push(struct __min_heap *heap, void *args) { memcpy(heap->data, element, elem_size); - __min_heapify(heap, 0, elem_size, func, args); + __min_heap_sift_down(heap, 0, elem_size, func, args); } #define min_heap_pop_push(_heap, _element, _func, _args) \ @@ -235,7 +235,7 @@ bool __min_heap_del(struct __min_heap *heap, size_t elem_size, size_t idx, return true; memcpy(data, data + (heap->nr * elem_size), elem_size); __min_heap_sift_up(heap, elem_size, idx, func, args); - __min_heapify(heap, idx, elem_size, func, args); + __min_heap_sift_down(heap, idx, elem_size, func, args); return true; } diff --git a/kernel/events/core.c b/kernel/events/core.c index c32a78c489f3..314fb7ea4ec3 100644 --- a/kernel/events/core.c +++ b/kernel/events/core.c @@ -3788,7 +3788,7 @@ static noinline int visit_groups_merge(struct perf_event_context *ctx, *evt = perf_event_groups_next(*evt, pmu); if (*evt) - min_heapify(&event_heap, 0, &perf_min_heap, NULL); + min_heap_sift_down(&event_heap, 0, &perf_min_heap, NULL); else min_heap_pop(&event_heap, &perf_min_heap, NULL); } From patchwork Wed Mar 20 14:54:15 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kuan-Wei Chiu X-Patchwork-Id: 13597950 X-Patchwork-Delegate: snitzer@redhat.com Received: from mail-pl1-f169.google.com (mail-pl1-f169.google.com [209.85.214.169]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 3E1965FDA3 for ; Wed, 20 Mar 2024 14:55:27 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.169 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1710946528; cv=none; b=R6UoBqmJu2ryEF+Mm0HPDMl1g51GB28opouMDB1neaRayr+82dwxsKdJlqJHdX82ykJuMLpys6CG9WY50b7T03/0UNrrE5n4DOxOkaWbVGQ9rd8go5h6WCvzz44sDPNFZaFVclV45TjWQocetxl0HPwS4sYz3VHSxjQMxfhvy3c= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1710946528; c=relaxed/simple; bh=GQUVsfUcpz8LUorm5To23Yew7xCHJVZumueYQTygjj8=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=bRwEFiBfKxFslTswaYYIp3VyK4JIhjIoBY6fj1nDL+K8eHMq2SxGm4JsZ6zr9+HmGN5IeB/oJEXw1mMR1qXTrCqGbRVhsOqter4R4URCZHLQycIe4xdtokq88pOw1u7ihrpPWAx+MqnZAjCmh+CXYjqcMaRSCcbLXe5Bwh/Gyx0= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=UBcehHWp; arc=none smtp.client-ip=209.85.214.169 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="UBcehHWp" Received: by mail-pl1-f169.google.com with SMTP id d9443c01a7336-1e0678265b3so619465ad.0 for ; Wed, 20 Mar 2024 07:55:27 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1710946526; x=1711551326; darn=lists.linux.dev; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=zmVSQEC3TeRF4GcV4dCeypJopsqrkB0AwEs9FDrz4eI=; b=UBcehHWp7Iuia8KIZZvSANe87O3+TtHsDjjlDXFLC7k9K8ATrUNBYXEa/TWHzJgPBI VXoPJTaYL1sIXbvYPRo0aEy2SUD6cA3fz7EYQq7UBmWVd2sN8SCOfWIFfsVhkg/Nx0uP 5WYK6J0DaGdhlaM0zi3qqlmBhj5EMEHzpzeZZqAJ6vIbBjPoRyVFkdvVwELhByDhSEUH yFCmVWYIYunjiwKk7V0BHfSQGexm73QHfbmjmNapdoxjm5cu3VV8OAVXZ1Ia4K3FR087 uIFaXRT19zQRx9rgYHx5/WqJCbXWxcayG9F2L4Sq5hCZXAYIVjvzZmCr6iOVw5Fe0462 ZjMQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1710946526; x=1711551326; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=zmVSQEC3TeRF4GcV4dCeypJopsqrkB0AwEs9FDrz4eI=; b=A5NLuOaV/2odK9Pa18vOZjISufWTnczk09iy2o4RUpwpugly46vGd9ORVa0K7zZYuW v0Un/9I2xTT5qcFaDYc5CIWxvlfX26tmqjmeOSI7uxuVcy0wG9tWY6SE1btt/Zwfs7Rh nGs+a1cbislwvoCaev/wpRsX3YeMun0rwMWGhsgd5RRpHu/dciVa779lDR6JrgRtyHfp yH8kO6JowVsTgn49mQTaOZbDy/qyoaYgegJ2kep/KzBWLWY7/+/oqapdZb8sJjOfTuWa YkmCdzNDL/Wae7DtbCtGuHiNMiNLi6qiy5ELrVec1uteC/LHzIu2FjWNm5girvNLgJN9 K/pA== X-Forwarded-Encrypted: i=1; AJvYcCVM3ykyjJVrcH68pBMYjrUIiUR+tf6AwYXO+KE6oP4sZM0mF3zPI3vHhA2NTk4fR39dRNDVFZFKCbtEQo1UXnxxFmBy9DyjQck= X-Gm-Message-State: AOJu0YwK2ikyE949y1yDN4cKhn9XQlDQejaX4JrkLEYp3I1/UKZxfPID 0OJyO1XqJ4wxHeyRqrOTtkacZ4vFrg0t2eBF3qjOuuCug+Qis1Ip X-Google-Smtp-Source: AGHT+IH+g3VQKU1UT+u7hlzLEfuAex2ovVYbYrYQpeOaTt68BlJN4QEvWb+lKyawL0mHCNYzU4yQXQ== X-Received: by 2002:a17:903:1c1:b0:1dd:7c4c:c6b6 with SMTP id e1-20020a17090301c100b001dd7c4cc6b6mr19251572plh.5.1710946526585; Wed, 20 Mar 2024 07:55:26 -0700 (PDT) Received: from visitorckw-System-Product-Name.. ([140.113.216.168]) by smtp.gmail.com with ESMTPSA id la11-20020a170902fa0b00b001dc30f13e6asm13719989plb.137.2024.03.20.07.55.22 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 20 Mar 2024 07:55:26 -0700 (PDT) From: Kuan-Wei Chiu To: colyli@suse.de, kent.overstreet@linux.dev, msakai@redhat.com, peterz@infradead.org, mingo@redhat.com, acme@kernel.org, namhyung@kernel.org, akpm@linux-foundation.org Cc: bfoster@redhat.com, mark.rutland@arm.com, alexander.shishkin@linux.intel.com, jolsa@kernel.org, irogers@google.com, adrian.hunter@intel.com, jserv@ccns.ncku.edu.tw, dm-devel@lists.linux.dev, linux-bcache@vger.kernel.org, linux-kernel@vger.kernel.org, linux-bcachefs@vger.kernel.org, linux-perf-users@vger.kernel.org, Kuan-Wei Chiu Subject: [PATCH v2 13/15] lib/test_min_heap: Use min_heap_init() for initializing Date: Wed, 20 Mar 2024 22:54:15 +0800 Message-Id: <20240320145417.336208-14-visitorckw@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240320145417.336208-1-visitorckw@gmail.com> References: <20240320145417.336208-1-visitorckw@gmail.com> Precedence: bulk X-Mailing-List: dm-devel@lists.linux.dev List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Replace direct assignment of values to heap data members with min_heap_init() for better code readability and maintainability. Link: https://lkml.kernel.org/CAP-5=fW+FvUu8JL+KrtVv5uC++4AW=VhyEOgmdWzpH1mswQNzw@mail.gmail.com Signed-off-by: Kuan-Wei Chiu Reviewed-by: Ian Rogers --- lib/test_min_heap.c | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/lib/test_min_heap.c b/lib/test_min_heap.c index 062e908e9fa3..8d25fc8256db 100644 --- a/lib/test_min_heap.c +++ b/lib/test_min_heap.c @@ -67,9 +67,8 @@ static __init int test_heapify_all(bool min_heap) -3, -1, -2, -4, 0x8000000, 0x7FFFFFF }; struct min_heap_test heap; - heap.heap.data = values; + min_heap_init(&heap, values, ARRAY_SIZE(values)); heap.heap.nr = ARRAY_SIZE(values); - heap.heap.size = ARRAY_SIZE(values); struct min_heap_callbacks funcs = { .less = min_heap ? less_than : greater_than, .swp = swap_ints, @@ -99,9 +98,7 @@ static __init int test_heap_push(bool min_heap) int values[ARRAY_SIZE(data)]; struct min_heap_test heap; - heap.heap.data = values; - heap.heap.nr = 0; - heap.heap.size = ARRAY_SIZE(values); + min_heap_init(&heap, values, ARRAY_SIZE(values)); struct min_heap_callbacks funcs = { .less = min_heap ? less_than : greater_than, .swp = swap_ints, @@ -131,9 +128,7 @@ static __init int test_heap_pop_push(bool min_heap) int values[ARRAY_SIZE(data)]; struct min_heap_test heap; - heap.heap.data = values; - heap.heap.nr = 0; - heap.heap.size = ARRAY_SIZE(values); + min_heap_init(&heap, values, ARRAY_SIZE(values)); struct min_heap_callbacks funcs = { .less = min_heap ? less_than : greater_than, .swp = swap_ints, From patchwork Wed Mar 20 14:54:16 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kuan-Wei Chiu X-Patchwork-Id: 13597951 X-Patchwork-Delegate: snitzer@redhat.com Received: from mail-pg1-f175.google.com (mail-pg1-f175.google.com [209.85.215.175]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 006C55FDD9 for ; Wed, 20 Mar 2024 14:55:31 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.215.175 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1710946534; cv=none; b=nTPaCZQ/Fk74WOtQW5kOdARBsi8FMVWSA4WFIWDgM+qvZE10m5ai4BA3e7/Wdj44J7lW2POrVuUP2tKY+CJCJu4XuDSwUglQKkdzPquMoxhaNMKFUobSWuHBOkA1sQgWlKxZX6drP+ofU/GQ30KjRWC9ARTzKsnmtB9y4C17qKY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1710946534; c=relaxed/simple; bh=XPBSJLpKhIFdDr4h5vdSQJNEYqSbkGLLU2EsYDWg2NE=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=u1TaSS99VQBEQuIT2GakBWybt/F0TmYzSYb5iu+NIEtOhwZ1Ys3SvktegneUBoczIv+kSu0WxG6iEd7+avCXju847mFresqOXd62CAT8DLXA3B+mykCWXaJ9YjEZydl8vrGMdhJKQA/khlf6Jsr3A3aSItptFE09H7AbvAcm6PQ= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=aepbtVa7; arc=none smtp.client-ip=209.85.215.175 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="aepbtVa7" Received: by mail-pg1-f175.google.com with SMTP id 41be03b00d2f7-5cfcf509fbdso1038451a12.1 for ; Wed, 20 Mar 2024 07:55:31 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1710946531; x=1711551331; darn=lists.linux.dev; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=+g/jsUIA9chYeIMSbZAKeaEXN1CLFxPlPEM36JCClAk=; b=aepbtVa7vCwSHMZl/MnTQBmb9ah2U1EK5N1EpZnKzP+O/BI1nFF0CHM30tRNLR1Bo0 Y3NT8/6oK6+AFo23afReW+fNb4cJZUSAkZb8Y6flg/n3KtvAf1MU6w5m4KD+v58j/ssh yq75Hzk53LUzuz723fIfKpfVMSnx98IkaQ/CDmUkEhp2a/gvDJd/3VJVQjBgO1KGUsuA UWnot4WDv+tUlI1pQeQDOm/4Cf8XD9uwka4rl3VoNLD4LyYa6oyDF95zeog3yWwfRns1 /FJaXWqsYbZwcUTVxsnXSMLzW1Ql56J1f2osdCL49d131kjsaVH+hIOql9BLBGF2ZN/e NOEw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1710946531; x=1711551331; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=+g/jsUIA9chYeIMSbZAKeaEXN1CLFxPlPEM36JCClAk=; b=wYHRPohDw0xkhTDeLDyLWb5W8oYp3HqE1y++LQ0S21Bn+ud4hj55RDhegCWRf/Jekk 9M153QmHRpEqVLFmTyZU24u4YnUiKKH5i6XyVb9OtjNK3HMp1rVSAyGfPyYjXxWycwq7 zvmTSl26Fsi8eWQ7LyZqXMhJ4C0qKAmgSUZ5lcY5sAuLkZuVjBSAR929XaxEzfl8Hni2 9hJXKYnfHxXb49+HIo7eSxZPyXnCQva/acQUPR6Fzwpn3duDIEtHBFCmvyYzcXwOcqYP emcPbKCR8zpBctb72is9AeVNDRTuZ3DVHgRfM9JbBekKJparEnqfE4lLux3ho+2NO+Nd vCLg== X-Forwarded-Encrypted: i=1; AJvYcCW3vTQHL2YMqqbS04HQUv4Twr4IvNI1oC0PBqQyGyGH5EcHZQNWQys0sLjpC4bhSOss6qDunYTB4wbpOyA5Ok5jfqDgM2JrbUQ= X-Gm-Message-State: AOJu0YzZvHPsnxcUINv3zxVOiLXx1/MFu5eoI5ltuwjhmMi99CxdQSEQ 6VZgcecjbVDayqPrOYvVz0iooVUtHILgtbim2TnlwV2fSnO9qRte X-Google-Smtp-Source: AGHT+IHkDB1QY/3dhJ7IXzrCL/CmyWyqTlolHGWu9SVFgpAGC1TE3r84KQbk3o5N3/1JKCL68wqydg== X-Received: by 2002:a17:902:da92:b0:1de:e8ce:9d7a with SMTP id j18-20020a170902da9200b001dee8ce9d7amr1938503plx.5.1710946531332; Wed, 20 Mar 2024 07:55:31 -0700 (PDT) Received: from visitorckw-System-Product-Name.. ([140.113.216.168]) by smtp.gmail.com with ESMTPSA id la11-20020a170902fa0b00b001dc30f13e6asm13719989plb.137.2024.03.20.07.55.27 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 20 Mar 2024 07:55:30 -0700 (PDT) From: Kuan-Wei Chiu To: colyli@suse.de, kent.overstreet@linux.dev, msakai@redhat.com, peterz@infradead.org, mingo@redhat.com, acme@kernel.org, namhyung@kernel.org, akpm@linux-foundation.org Cc: bfoster@redhat.com, mark.rutland@arm.com, alexander.shishkin@linux.intel.com, jolsa@kernel.org, irogers@google.com, adrian.hunter@intel.com, jserv@ccns.ncku.edu.tw, dm-devel@lists.linux.dev, linux-bcache@vger.kernel.org, linux-kernel@vger.kernel.org, linux-bcachefs@vger.kernel.org, linux-perf-users@vger.kernel.org, Kuan-Wei Chiu Subject: [PATCH v2 14/15] bcache: Remove heap-related macros and switch to generic min_heap Date: Wed, 20 Mar 2024 22:54:16 +0800 Message-Id: <20240320145417.336208-15-visitorckw@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240320145417.336208-1-visitorckw@gmail.com> References: <20240320145417.336208-1-visitorckw@gmail.com> Precedence: bulk X-Mailing-List: dm-devel@lists.linux.dev List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Drop the heap-related macros from bcache and replacing them with the generic min_heap implementation from include/linux. By doing so, code readability is improved by using functions instead of macros. Moreover, the min_heap implementation in include/linux adopts a bottom-up variation compared to the textbook version currently used in bcache. This bottom-up variation allows for approximately 50% reduction in the number of comparison operations during heap siftdown, without changing the number of swaps, thus making it more efficient. Link: https://lkml.kernel.org/ioyfizrzq7w7mjrqcadtzsfgpuntowtjdw5pgn4qhvsdp4mqqg@nrlek5vmisbu Signed-off-by: Kuan-Wei Chiu Reviewed-by: Ian Rogers --- Changes in v2: - Add attribute __always_unused to the compare and swap functions that do not use the args parameter. - Rename min_heapify() to min_heap_sift_down(). - Refine the commit message. drivers/md/bcache/alloc.c | 66 +++++++++++++++++++++-------- drivers/md/bcache/bcache.h | 2 +- drivers/md/bcache/bset.c | 73 ++++++++++++++++++++++---------- drivers/md/bcache/bset.h | 38 ++++++++++------- drivers/md/bcache/btree.c | 27 +++++++++++- drivers/md/bcache/extents.c | 44 ++++++++++++-------- drivers/md/bcache/movinggc.c | 40 +++++++++++++----- drivers/md/bcache/super.c | 16 +++++++ drivers/md/bcache/sysfs.c | 3 ++ drivers/md/bcache/util.h | 81 +----------------------------------- 10 files changed, 224 insertions(+), 166 deletions(-) diff --git a/drivers/md/bcache/alloc.c b/drivers/md/bcache/alloc.c index ce13c272c387..b27c0e25b661 100644 --- a/drivers/md/bcache/alloc.c +++ b/drivers/md/bcache/alloc.c @@ -166,40 +166,70 @@ static void bch_invalidate_one_bucket(struct cache *ca, struct bucket *b) * prio is worth 1/8th of what INITIAL_PRIO is worth. */ -#define bucket_prio(b) \ -({ \ - unsigned int min_prio = (INITIAL_PRIO - ca->set->min_prio) / 8; \ - \ - (b->prio - ca->set->min_prio + min_prio) * GC_SECTORS_USED(b); \ -}) +static inline unsigned int bucket_prio(struct cache *ca, struct bucket *b) +{ + unsigned int min_prio = (INITIAL_PRIO - ca->set->min_prio) / 8; + + return (b->prio - ca->set->min_prio + min_prio) * GC_SECTORS_USED(b); + +} + +static inline bool bucket_max_cmp(const void *l, const void *r, void *args) +{ + struct bucket *lhs = (struct bucket *)l; + struct bucket *rhs = (struct bucket *)r; + struct cache *ca = args; + + return bucket_prio(ca, lhs) > bucket_prio(ca, rhs); +} + +static inline bool bucket_min_cmp(const void *l, const void *r, void *args) +{ + struct bucket *lhs = (struct bucket *)l; + struct bucket *rhs = (struct bucket *)r; + struct cache *ca = args; + + return bucket_prio(ca, lhs) < bucket_prio(ca, rhs); +} + +static inline void bucket_swap(void *l, void *r, void __always_unused *args) +{ + struct bucket *lhs = l, *rhs = r; -#define bucket_max_cmp(l, r) (bucket_prio(l) < bucket_prio(r)) -#define bucket_min_cmp(l, r) (bucket_prio(l) > bucket_prio(r)) + swap(*lhs, *rhs); +} static void invalidate_buckets_lru(struct cache *ca) { struct bucket *b; - ssize_t i; + const struct min_heap_callbacks bucket_max_cmp_callback = { + .less = bucket_max_cmp, + .swp = bucket_swap, + }; + const struct min_heap_callbacks bucket_min_cmp_callback = { + .less = bucket_min_cmp, + .swp = bucket_swap, + }; - ca->heap.used = 0; + ca->heap.heap.nr = 0; for_each_bucket(b, ca) { if (!bch_can_invalidate_bucket(ca, b)) continue; - if (!heap_full(&ca->heap)) - heap_add(&ca->heap, b, bucket_max_cmp); - else if (bucket_max_cmp(b, heap_peek(&ca->heap))) { - ca->heap.data[0] = b; - heap_sift(&ca->heap, 0, bucket_max_cmp); + if (!min_heap_full(&ca->heap)) + min_heap_push(&ca->heap, b, &bucket_max_cmp_callback, ca); + else if (!bucket_max_cmp(b, min_heap_peek(&ca->heap), ca)) { + *min_heap_peek(&ca->heap) = b; + min_heap_sift_down(&ca->heap, 0, &bucket_max_cmp_callback, ca); } } - for (i = ca->heap.used / 2 - 1; i >= 0; --i) - heap_sift(&ca->heap, i, bucket_min_cmp); + min_heapify_all(&ca->heap, &bucket_min_cmp_callback, ca); while (!fifo_full(&ca->free_inc)) { - if (!heap_pop(&ca->heap, b, bucket_min_cmp)) { + b = *min_heap_peek(&ca->heap); + if (!min_heap_pop(&ca->heap, &bucket_min_cmp_callback, ca)) { /* * We don't want to be calling invalidate_buckets() * multiple times when it can't do anything diff --git a/drivers/md/bcache/bcache.h b/drivers/md/bcache/bcache.h index 4e6afa89921f..97b0a1768ba7 100644 --- a/drivers/md/bcache/bcache.h +++ b/drivers/md/bcache/bcache.h @@ -457,7 +457,7 @@ struct cache { /* Allocation stuff: */ struct bucket *buckets; - DECLARE_HEAP(struct bucket *, heap); + MIN_HEAP(struct bucket *, heap) heap; /* * If nonzero, we know we aren't going to find any buckets to invalidate diff --git a/drivers/md/bcache/bset.c b/drivers/md/bcache/bset.c index 2bba4d6aaaa2..6b1c8ac0f866 100644 --- a/drivers/md/bcache/bset.c +++ b/drivers/md/bcache/bset.c @@ -56,7 +56,9 @@ int __bch_count_data(struct btree_keys *b) unsigned int ret = 0; struct btree_iter iter; struct bkey *k; + struct btree_iter_set data[MAX_BSETS]; + iter.heap.heap.data = data; if (b->ops->is_extents) for_each_key(b, k, &iter) ret += KEY_SIZE(k); @@ -69,6 +71,9 @@ void __bch_check_keys(struct btree_keys *b, const char *fmt, ...) struct bkey *k, *p = NULL; struct btree_iter iter; const char *err; + struct btree_iter_set data[MAX_BSETS]; + + iter.heap.heap.data = data; for_each_key(b, k, &iter) { if (b->ops->is_extents) { @@ -110,9 +115,9 @@ void __bch_check_keys(struct btree_keys *b, const char *fmt, ...) static void bch_btree_iter_next_check(struct btree_iter *iter) { - struct bkey *k = iter->data->k, *next = bkey_next(k); + struct bkey *k = min_heap_peek(&iter->heap)->k, *next = bkey_next(k); - if (next < iter->data->end && + if (next < min_heap_peek(&iter->heap)->end && bkey_cmp(k, iter->b->ops->is_extents ? &START_KEY(next) : next) > 0) { bch_dump_bucket(iter->b); @@ -882,6 +887,9 @@ unsigned int bch_btree_insert_key(struct btree_keys *b, struct bkey *k, struct btree_iter iter; struct bkey preceding_key_on_stack = ZERO_KEY; struct bkey *preceding_key_p = &preceding_key_on_stack; + struct btree_iter_set data[MAX_BSETS]; + + iter.heap.heap.data = data; BUG_ON(b->ops->is_extents && !KEY_SIZE(k)); @@ -1077,27 +1085,34 @@ struct bkey *__bch_bset_search(struct btree_keys *b, struct bset_tree *t, /* Btree iterator */ -typedef bool (btree_iter_cmp_fn)(struct btree_iter_set, - struct btree_iter_set); +typedef bool (btree_iter_cmp_fn)(const void *, const void *, void *); -static inline bool btree_iter_cmp(struct btree_iter_set l, - struct btree_iter_set r) +static inline bool btree_iter_cmp(const void *l, const void *r, void __always_unused *args) { - return bkey_cmp(l.k, r.k) > 0; + const struct btree_iter_set *_l = l; + const struct btree_iter_set *_r = r; + + return bkey_cmp(_l->k, _r->k) <= 0; } static inline bool btree_iter_end(struct btree_iter *iter) { - return !iter->used; + return !iter->heap.heap.nr; } void bch_btree_iter_push(struct btree_iter *iter, struct bkey *k, struct bkey *end) { + const struct min_heap_callbacks callbacks = { + .less = btree_iter_cmp, + .swp = btree_iter_swap, + }; + if (k != end) - BUG_ON(!heap_add(iter, - ((struct btree_iter_set) { k, end }), - btree_iter_cmp)); + BUG_ON(!min_heap_push(&iter->heap, + &((struct btree_iter_set){ k, end }), + &callbacks, + NULL)); } static struct bkey *__bch_btree_iter_init(struct btree_keys *b, @@ -1107,8 +1122,8 @@ static struct bkey *__bch_btree_iter_init(struct btree_keys *b, { struct bkey *ret = NULL; - iter->size = ARRAY_SIZE(iter->data); - iter->used = 0; + iter->heap.heap.size = MAX_BSETS; + iter->heap.heap.nr = 0; #ifdef CONFIG_BCACHE_DEBUG iter->b = b; @@ -1134,22 +1149,28 @@ static inline struct bkey *__bch_btree_iter_next(struct btree_iter *iter, { struct btree_iter_set b __maybe_unused; struct bkey *ret = NULL; + const struct min_heap_callbacks callbacks = { + .less = cmp, + .swp = btree_iter_swap, + }; if (!btree_iter_end(iter)) { bch_btree_iter_next_check(iter); - ret = iter->data->k; - iter->data->k = bkey_next(iter->data->k); + ret = min_heap_peek(&iter->heap)->k; + min_heap_peek(&iter->heap)->k = bkey_next(min_heap_peek(&iter->heap)->k); - if (iter->data->k > iter->data->end) { + if (min_heap_peek(&iter->heap)->k > min_heap_peek(&iter->heap)->end) { WARN_ONCE(1, "bset was corrupt!\n"); - iter->data->k = iter->data->end; + min_heap_peek(&iter->heap)->k = min_heap_peek(&iter->heap)->end; } - if (iter->data->k == iter->data->end) - heap_pop(iter, b, cmp); + if (min_heap_peek(&iter->heap)->k == min_heap_peek(&iter->heap)->end) { + b = *min_heap_peek(&iter->heap); + min_heap_pop(&iter->heap, &callbacks, NULL); + } else - heap_sift(iter, 0, cmp); + min_heap_sift_down(&iter->heap, 0, &callbacks, NULL); } return ret; @@ -1195,16 +1216,18 @@ static void btree_mergesort(struct btree_keys *b, struct bset *out, struct btree_iter *iter, bool fixup, bool remove_stale) { - int i; struct bkey *k, *last = NULL; BKEY_PADDED(k) tmp; bool (*bad)(struct btree_keys *, const struct bkey *) = remove_stale ? bch_ptr_bad : bch_ptr_invalid; + const struct min_heap_callbacks callbacks = { + .less = b->ops->sort_cmp, + .swp = btree_iter_swap, + }; /* Heapify the iterator, using our comparison function */ - for (i = iter->used / 2 - 1; i >= 0; --i) - heap_sift(iter, i, b->ops->sort_cmp); + min_heapify_all(&iter->heap, &callbacks, NULL); while (!btree_iter_end(iter)) { if (b->ops->sort_fixup && fixup) @@ -1295,7 +1318,9 @@ void bch_btree_sort_partial(struct btree_keys *b, unsigned int start, size_t order = b->page_order, keys = 0; struct btree_iter iter; int oldsize = bch_count_data(b); + struct btree_iter_set data[MAX_BSETS]; + iter.heap.heap.data = data; __bch_btree_iter_init(b, &iter, NULL, &b->set[start]); if (start) { @@ -1324,7 +1349,9 @@ void bch_btree_sort_into(struct btree_keys *b, struct btree_keys *new, { uint64_t start_time = local_clock(); struct btree_iter iter; + struct btree_iter_set data[MAX_BSETS]; + iter.heap.heap.data = data; bch_btree_iter_init(b, &iter, NULL); btree_mergesort(b, new->set->data, &iter, false, true); diff --git a/drivers/md/bcache/bset.h b/drivers/md/bcache/bset.h index d795c84246b0..e9559486a4c5 100644 --- a/drivers/md/bcache/bset.h +++ b/drivers/md/bcache/bset.h @@ -152,6 +152,19 @@ struct btree_iter; struct btree_iter_set; struct bkey_float; +/* Btree key iteration */ + +struct btree_iter_set { + struct bkey *k, *end; +}; + +struct btree_iter { +#ifdef CONFIG_BCACHE_DEBUG + struct btree_keys *b; +#endif + MIN_HEAP(struct btree_iter_set, btree_iter_heap) heap; +}; + #define MAX_BSETS 4U struct bset_tree { @@ -187,8 +200,9 @@ struct bset_tree { }; struct btree_keys_ops { - bool (*sort_cmp)(struct btree_iter_set l, - struct btree_iter_set r); + bool (*sort_cmp)(const void *l, + const void *r, + void *args); struct bkey *(*sort_fixup)(struct btree_iter *iter, struct bkey *tmp); bool (*insert_fixup)(struct btree_keys *b, @@ -258,6 +272,14 @@ static inline unsigned int bset_sector_offset(struct btree_keys *b, return bset_byte_offset(b, i) >> 9; } +static inline void btree_iter_swap(void *iter1, void *iter2, void __always_unused *args) +{ + struct btree_iter_set *_iter1 = iter1; + struct btree_iter_set *_iter2 = iter2; + + swap(*_iter1, *_iter2); +} + #define __set_bytes(i, k) (sizeof(*(i)) + (k) * sizeof(uint64_t)) #define set_bytes(i) __set_bytes(i, i->keys) @@ -312,18 +334,6 @@ enum { BTREE_INSERT_STATUS_FRONT_MERGE, }; -/* Btree key iteration */ - -struct btree_iter { - size_t size, used; -#ifdef CONFIG_BCACHE_DEBUG - struct btree_keys *b; -#endif - struct btree_iter_set { - struct bkey *k, *end; - } data[MAX_BSETS]; -}; - typedef bool (*ptr_filter_fn)(struct btree_keys *b, const struct bkey *k); struct bkey *bch_btree_iter_next(struct btree_iter *iter); diff --git a/drivers/md/bcache/btree.c b/drivers/md/bcache/btree.c index 196cdacce38f..e7333a8c4819 100644 --- a/drivers/md/bcache/btree.c +++ b/drivers/md/bcache/btree.c @@ -157,8 +157,8 @@ void bch_btree_node_read_done(struct btree *b) * See the comment arount cache_set->fill_iter. */ iter = mempool_alloc(&b->c->fill_iter, GFP_NOIO); - iter->size = b->c->cache->sb.bucket_size / b->c->cache->sb.block_size; - iter->used = 0; + iter->heap.heap.size = b->c->cache->sb.bucket_size / b->c->cache->sb.block_size; + iter->heap.heap.nr = 0; #ifdef CONFIG_BCACHE_DEBUG iter->b = &b->keys; @@ -1311,6 +1311,9 @@ static bool btree_gc_mark_node(struct btree *b, struct gc_stat *gc) struct bkey *k; struct btree_iter iter; struct bset_tree *t; + struct btree_iter_set data[MAX_BSETS]; + + iter.heap.heap.data = data; gc->nodes++; @@ -1572,6 +1575,9 @@ static unsigned int btree_gc_count_keys(struct btree *b) struct bkey *k; struct btree_iter iter; unsigned int ret = 0; + struct btree_iter_set data[MAX_BSETS]; + + iter.heap.heap.data = data; for_each_key_filter(&b->keys, k, &iter, bch_ptr_bad) ret += bkey_u64s(k); @@ -1614,6 +1620,9 @@ static int btree_gc_recurse(struct btree *b, struct btree_op *op, struct btree_iter iter; struct gc_merge_info r[GC_MERGE_NODES]; struct gc_merge_info *i, *last = r + ARRAY_SIZE(r) - 1; + struct btree_iter_set data[MAX_BSETS]; + + iter.heap.heap.data = data; bch_btree_iter_init(&b->keys, &iter, &b->c->gc_done); @@ -1912,6 +1921,9 @@ static int bch_btree_check_recurse(struct btree *b, struct btree_op *op) int ret = 0; struct bkey *k, *p = NULL; struct btree_iter iter; + struct btree_iter_set data[MAX_BSETS]; + + iter.heap.heap.data = data; for_each_key_filter(&b->keys, k, &iter, bch_ptr_invalid) bch_initial_mark_key(b->c, b->level, k); @@ -1953,7 +1965,9 @@ static int bch_btree_check_thread(void *arg) struct btree_iter iter; struct bkey *k, *p; int cur_idx, prev_idx, skip_nr; + struct btree_iter_set data[MAX_BSETS]; + iter.heap.heap.data = data; k = p = NULL; cur_idx = prev_idx = 0; ret = 0; @@ -2053,6 +2067,9 @@ int bch_btree_check(struct cache_set *c) struct bkey *k = NULL; struct btree_iter iter; struct btree_check_state check_state; + struct btree_iter_set data[MAX_BSETS]; + + iter.heap.heap.data = data; /* check and mark root node keys */ for_each_key_filter(&c->root->keys, k, &iter, bch_ptr_invalid) @@ -2548,6 +2565,9 @@ static int bch_btree_map_nodes_recurse(struct btree *b, struct btree_op *op, if (b->level) { struct bkey *k; struct btree_iter iter; + struct btree_iter_set data[MAX_BSETS]; + + iter.heap.heap.data = data; bch_btree_iter_init(&b->keys, &iter, from); @@ -2581,6 +2601,9 @@ int bch_btree_map_keys_recurse(struct btree *b, struct btree_op *op, int ret = MAP_CONTINUE; struct bkey *k; struct btree_iter iter; + struct btree_iter_set data[MAX_BSETS]; + + iter.heap.heap.data = data; bch_btree_iter_init(&b->keys, &iter, from); diff --git a/drivers/md/bcache/extents.c b/drivers/md/bcache/extents.c index d626ffcbecb9..8279596004f5 100644 --- a/drivers/md/bcache/extents.c +++ b/drivers/md/bcache/extents.c @@ -32,16 +32,19 @@ static void sort_key_next(struct btree_iter *iter, { i->k = bkey_next(i->k); - if (i->k == i->end) - *i = iter->data[--iter->used]; + if (i->k == i->end) { + struct btree_iter_set *data = iter->heap.heap.data; + *i = data[--iter->heap.heap.nr]; + } } -static bool bch_key_sort_cmp(struct btree_iter_set l, - struct btree_iter_set r) +static bool bch_key_sort_cmp(const void *l, const void *r, void *args) { - int64_t c = bkey_cmp(l.k, r.k); + struct btree_iter_set *_l = (struct btree_iter_set *)l; + struct btree_iter_set *_r = (struct btree_iter_set *)r; + int64_t c = bkey_cmp(_l->k, _r->k); - return c ? c > 0 : l.k < r.k; + return !(c ? c > 0 : _l->k < _r->k); } static bool __ptr_invalid(struct cache_set *c, const struct bkey *k) @@ -255,22 +258,29 @@ const struct btree_keys_ops bch_btree_keys_ops = { * Necessary for btree_sort_fixup() - if there are multiple keys that compare * equal in different sets, we have to process them newest to oldest. */ -static bool bch_extent_sort_cmp(struct btree_iter_set l, - struct btree_iter_set r) +static bool bch_extent_sort_cmp(const void *l, const void *r, void __always_unused *args) { - int64_t c = bkey_cmp(&START_KEY(l.k), &START_KEY(r.k)); + struct btree_iter_set *_l = (struct btree_iter_set *)l; + struct btree_iter_set *_r = (struct btree_iter_set *)r; + + int64_t c = bkey_cmp(&START_KEY(_l->k), &START_KEY(_r->k)); - return c ? c > 0 : l.k < r.k; + return !(c ? c > 0 : _l->k < _r->k); } static struct bkey *bch_extent_sort_fixup(struct btree_iter *iter, struct bkey *tmp) { - while (iter->used > 1) { - struct btree_iter_set *top = iter->data, *i = top + 1; + const struct min_heap_callbacks callbacks = { + .less = bch_extent_sort_cmp, + .swp = btree_iter_swap, + }; + + while (iter->heap.heap.nr > 1) { + struct btree_iter_set *top = min_heap_peek(&iter->heap), *i = top + 1; - if (iter->used > 2 && - bch_extent_sort_cmp(i[0], i[1])) + if (iter->heap.heap.nr > 2 && + !bch_extent_sort_cmp(&i[0], &i[1], NULL)) i++; if (bkey_cmp(top->k, &START_KEY(i->k)) <= 0) @@ -278,7 +288,7 @@ static struct bkey *bch_extent_sort_fixup(struct btree_iter *iter, if (!KEY_SIZE(i->k)) { sort_key_next(iter, i); - heap_sift(iter, i - top, bch_extent_sort_cmp); + min_heap_sift_down(&iter->heap, i - top, &callbacks, NULL); continue; } @@ -288,7 +298,7 @@ static struct bkey *bch_extent_sort_fixup(struct btree_iter *iter, else bch_cut_front(top->k, i->k); - heap_sift(iter, i - top, bch_extent_sort_cmp); + min_heap_sift_down(&iter->heap, i - top, &callbacks, NULL); } else { /* can't happen because of comparison func */ BUG_ON(!bkey_cmp(&START_KEY(top->k), &START_KEY(i->k))); @@ -298,7 +308,7 @@ static struct bkey *bch_extent_sort_fixup(struct btree_iter *iter, bch_cut_back(&START_KEY(i->k), tmp); bch_cut_front(i->k, top->k); - heap_sift(iter, 0, bch_extent_sort_cmp); + min_heap_sift_down(&iter->heap, 0, &callbacks, NULL); return tmp; } else { diff --git a/drivers/md/bcache/movinggc.c b/drivers/md/bcache/movinggc.c index ebd500bdf0b2..0f50192fea2c 100644 --- a/drivers/md/bcache/movinggc.c +++ b/drivers/md/bcache/movinggc.c @@ -182,16 +182,27 @@ err: if (!IS_ERR_OR_NULL(w->private)) closure_sync(&cl); } -static bool bucket_cmp(struct bucket *l, struct bucket *r) +static bool bucket_cmp(const void *l, const void *r, void __always_unused *args) { - return GC_SECTORS_USED(l) < GC_SECTORS_USED(r); + struct bucket *_l = (struct bucket *)l; + struct bucket *_r = (struct bucket *)r; + + return GC_SECTORS_USED(_l) >= GC_SECTORS_USED(_r); +} + +static void bucket_swap(void *l, void *r, void __always_unused *args) +{ + struct bucket *_l = l; + struct bucket *_r = r; + + swap(*_l, *_r); } static unsigned int bucket_heap_top(struct cache *ca) { struct bucket *b; - return (b = heap_peek(&ca->heap)) ? GC_SECTORS_USED(b) : 0; + return (b = *min_heap_peek(&ca->heap)) ? GC_SECTORS_USED(b) : 0; } void bch_moving_gc(struct cache_set *c) @@ -199,6 +210,10 @@ void bch_moving_gc(struct cache_set *c) struct cache *ca = c->cache; struct bucket *b; unsigned long sectors_to_move, reserve_sectors; + const struct min_heap_callbacks callbacks = { + .less = bucket_cmp, + .swp = bucket_swap, + }; if (!c->copy_gc_enabled) return; @@ -209,7 +224,7 @@ void bch_moving_gc(struct cache_set *c) reserve_sectors = ca->sb.bucket_size * fifo_used(&ca->free[RESERVE_MOVINGGC]); - ca->heap.used = 0; + ca->heap.heap.nr = 0; for_each_bucket(b, ca) { if (GC_MARK(b) == GC_MARK_METADATA || @@ -218,25 +233,28 @@ void bch_moving_gc(struct cache_set *c) atomic_read(&b->pin)) continue; - if (!heap_full(&ca->heap)) { + if (!min_heap_full(&ca->heap)) { sectors_to_move += GC_SECTORS_USED(b); - heap_add(&ca->heap, b, bucket_cmp); - } else if (bucket_cmp(b, heap_peek(&ca->heap))) { + min_heap_push(&ca->heap, b, &callbacks, NULL); + } else if (!bucket_cmp(b, min_heap_peek(&ca->heap), NULL)) { sectors_to_move -= bucket_heap_top(ca); sectors_to_move += GC_SECTORS_USED(b); - ca->heap.data[0] = b; - heap_sift(&ca->heap, 0, bucket_cmp); + *min_heap_peek(&ca->heap) = b; + min_heap_sift_down(&ca->heap, 0, &callbacks, NULL); } } while (sectors_to_move > reserve_sectors) { - heap_pop(&ca->heap, b, bucket_cmp); + b = *min_heap_peek(&ca->heap); + min_heap_pop(&ca->heap, &callbacks, NULL); sectors_to_move -= GC_SECTORS_USED(b); } - while (heap_pop(&ca->heap, b, bucket_cmp)) + while ((b = *min_heap_peek(&ca->heap))) { + min_heap_pop(&ca->heap, &callbacks, NULL); SET_GC_MOVE(b, 1); + } mutex_unlock(&c->bucket_lock); diff --git a/drivers/md/bcache/super.c b/drivers/md/bcache/super.c index 330bcd9ea4a9..1c6aeeff2cc3 100644 --- a/drivers/md/bcache/super.c +++ b/drivers/md/bcache/super.c @@ -2193,6 +2193,22 @@ static const char *register_cache_set(struct cache *ca) return err; } +static inline bool init_heap(struct heap *heap, size_t size, gfp_t gfp) +{ + size_t bytes = size * sizeof(struct bucket *); + void *data = kvmalloc(bytes, (gfp) & GFP_KERNEL); + + min_heap_init(heap, data, size); + + return data; +} + +static inline void free_heap(struct heap *heap) +{ + kvfree(heap->heap.data); + heap->heap.data = NULL; +} + /* Cache device */ /* When ca->kobj released */ diff --git a/drivers/md/bcache/sysfs.c b/drivers/md/bcache/sysfs.c index 6956beb55326..eac2039c2cad 100644 --- a/drivers/md/bcache/sysfs.c +++ b/drivers/md/bcache/sysfs.c @@ -661,6 +661,9 @@ static unsigned int bch_root_usage(struct cache_set *c) struct bkey *k; struct btree *b; struct btree_iter iter; + struct btree_iter_set data[MAX_BSETS]; + + iter.heap.heap.data = data; goto lock_root; diff --git a/drivers/md/bcache/util.h b/drivers/md/bcache/util.h index f61ab1bada6c..fa928d1d327a 100644 --- a/drivers/md/bcache/util.h +++ b/drivers/md/bcache/util.h @@ -7,6 +7,7 @@ #include #include #include +#include #include #include #include @@ -30,86 +31,6 @@ struct closure; #endif -#define DECLARE_HEAP(type, name) \ - struct { \ - size_t size, used; \ - type *data; \ - } name - -#define init_heap(heap, _size, gfp) \ -({ \ - size_t _bytes; \ - (heap)->used = 0; \ - (heap)->size = (_size); \ - _bytes = (heap)->size * sizeof(*(heap)->data); \ - (heap)->data = kvmalloc(_bytes, (gfp) & GFP_KERNEL); \ - (heap)->data; \ -}) - -#define free_heap(heap) \ -do { \ - kvfree((heap)->data); \ - (heap)->data = NULL; \ -} while (0) - -#define heap_swap(h, i, j) swap((h)->data[i], (h)->data[j]) - -#define heap_sift(h, i, cmp) \ -do { \ - size_t _r, _j = i; \ - \ - for (; _j * 2 + 1 < (h)->used; _j = _r) { \ - _r = _j * 2 + 1; \ - if (_r + 1 < (h)->used && \ - cmp((h)->data[_r], (h)->data[_r + 1])) \ - _r++; \ - \ - if (cmp((h)->data[_r], (h)->data[_j])) \ - break; \ - heap_swap(h, _r, _j); \ - } \ -} while (0) - -#define heap_sift_down(h, i, cmp) \ -do { \ - while (i) { \ - size_t p = (i - 1) / 2; \ - if (cmp((h)->data[i], (h)->data[p])) \ - break; \ - heap_swap(h, i, p); \ - i = p; \ - } \ -} while (0) - -#define heap_add(h, d, cmp) \ -({ \ - bool _r = !heap_full(h); \ - if (_r) { \ - size_t _i = (h)->used++; \ - (h)->data[_i] = d; \ - \ - heap_sift_down(h, _i, cmp); \ - heap_sift(h, _i, cmp); \ - } \ - _r; \ -}) - -#define heap_pop(h, d, cmp) \ -({ \ - bool _r = (h)->used; \ - if (_r) { \ - (d) = (h)->data[0]; \ - (h)->used--; \ - heap_swap(h, 0, (h)->used); \ - heap_sift(h, 0, cmp); \ - } \ - _r; \ -}) - -#define heap_peek(h) ((h)->used ? (h)->data[0] : NULL) - -#define heap_full(h) ((h)->used == (h)->size) - #define DECLARE_FIFO(type, name) \ struct { \ size_t front, back, size, mask; \ From patchwork Wed Mar 20 14:54:17 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kuan-Wei Chiu X-Patchwork-Id: 13597952 X-Patchwork-Delegate: snitzer@redhat.com Received: from mail-pg1-f173.google.com (mail-pg1-f173.google.com [209.85.215.173]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id EDC7A5BAE4 for ; Wed, 20 Mar 2024 14:55:37 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.215.173 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1710946539; cv=none; b=JpI8L4PL/wemECiAWbIbWA5NjiOQaSa+g/p702Q1gvyG/ZvZZlUh3EXm5Qb1yJ1yplPM9tcxK/ct2psjCVfu4UX9od1qouk6Xfu23X/Ywhi6fw9oHNlvCYXsFTM7MOE0atyPS9kZFiP0AHpU1gxS8PZQW0A20YLwNYnxU+/KLn0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1710946539; c=relaxed/simple; bh=KMFeHSmKU4CYqNneVApB6vY1jmQYmw8PyzHhws9RMTY=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=MzYOi2w0R4PUJQQK/ux21M8mJFHYXBxfbe7EB90Z9zfl51Kn3V0ecHy/Lui1qxv0epNFVj/p+5gLJ0iiLiUou0UQdvxKFSvNpuhDmVQzS8ZG7jGS/Ej1z5A+1bq7XZZueHgrWOByFQ3SqmZdPDo3SU+H/TxnEj4fiA3Y0h+3894= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=hD2pz00L; arc=none smtp.client-ip=209.85.215.173 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="hD2pz00L" Received: by mail-pg1-f173.google.com with SMTP id 41be03b00d2f7-55b5a37acb6so1194312a12.0 for ; Wed, 20 Mar 2024 07:55:37 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1710946537; x=1711551337; darn=lists.linux.dev; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=KPLW0qG40wdUtTexwqA/kqkzCYXZBDRQddNxzWgaIi4=; b=hD2pz00LEI5gXrQehXb5uiNwjkkO3wr+o+N8Ih8S3tl46zzEpiiwhuhlLz1TwxW3Rz LL1Y4m8T/3+uPzdstnqnpbCpiim8XrRd4bTP6tC7HJwujlKWzFxxc9G59MV+xnSgpcsT SPZ/UA60Alg7dwE+ZeqXbsskn5gbTrgI1JWu7KHRnWZ0wHv/PfY9ZpsY4FXGIkVPnHyv rM1SRUT/fNn2pLwS5Moe/ByXZtOfwOXhKsDJ42QJCPHsfuwHQFBgWKKjYow0rfgB9g45 J/1QrLHSkWW+657cwSVZR6XRkdjjqMc/QRUYVvVdnb5nMKtd6sUR+dgvYBmmrPExZPt0 1AoQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1710946537; x=1711551337; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=KPLW0qG40wdUtTexwqA/kqkzCYXZBDRQddNxzWgaIi4=; b=gKeDmFjonKzPKT60SNeO1Aymec5luCpwjA4Yck23xOz5DD1TUCHvL+xDbfo8odtcwk iOgtiDeI/kOZIYhZr45X8dbtSwXw0Id/JhJploKaN+C1Dlq9oL3Fb4BJwB7qVFBaOJlR gSdfmP6iUi2EBXLZV1FGQOiIgVWVQxPFqfs1nfzONGc3/2fxBc1Pn3qHL14rP5r1LFBJ Kgn06dEATs/uJFKIf71R9fCj/JAsbF8lsv43jPmANWgdr6oItqsRe+JKv5V7dRZr3dxI 7JjuhA7Q85BqLGU+hdQ965XuecdgPFVfKi7MGTl9mVD7DM++FYkSOvqL+jawyKr6izfE 9UOw== X-Forwarded-Encrypted: i=1; AJvYcCWtbgh0JVe1dLQw+XOeKamIxZhkggQ9ZkUG3AJ5lvMxLbfQ3tSVoC/TFjNaEASniitAKNr7yfZeKk/kWGnzujZpo2M+4vFV/nY= X-Gm-Message-State: AOJu0YwoV3lOCa4LRhL4fBPzU8Uyse6UHvuTRIswXbQbQ3QLaMSXg2S0 UFP3do5WBFLuM3bc5xvc0naPaanw2sVRc7Nif561MWsFcI/8K35f X-Google-Smtp-Source: AGHT+IERhQnsr5WyFpp2s48qwEVuCnCYvaTAkc4lWuq6KuXLt75oDq0Oi+CsEeR8yPDirIraJHLJpA== X-Received: by 2002:a17:902:d2ca:b0:1df:fbc3:d130 with SMTP id n10-20020a170902d2ca00b001dffbc3d130mr1973394plc.1.1710946537229; Wed, 20 Mar 2024 07:55:37 -0700 (PDT) Received: from visitorckw-System-Product-Name.. ([140.113.216.168]) by smtp.gmail.com with ESMTPSA id la11-20020a170902fa0b00b001dc30f13e6asm13719989plb.137.2024.03.20.07.55.33 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 20 Mar 2024 07:55:36 -0700 (PDT) From: Kuan-Wei Chiu To: colyli@suse.de, kent.overstreet@linux.dev, msakai@redhat.com, peterz@infradead.org, mingo@redhat.com, acme@kernel.org, namhyung@kernel.org, akpm@linux-foundation.org Cc: bfoster@redhat.com, mark.rutland@arm.com, alexander.shishkin@linux.intel.com, jolsa@kernel.org, irogers@google.com, adrian.hunter@intel.com, jserv@ccns.ncku.edu.tw, dm-devel@lists.linux.dev, linux-bcache@vger.kernel.org, linux-kernel@vger.kernel.org, linux-bcachefs@vger.kernel.org, linux-perf-users@vger.kernel.org, Kuan-Wei Chiu Subject: [PATCH v2 15/15] bcachefs: Remove heap-related macros and switch to generic min_heap Date: Wed, 20 Mar 2024 22:54:17 +0800 Message-Id: <20240320145417.336208-16-visitorckw@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240320145417.336208-1-visitorckw@gmail.com> References: <20240320145417.336208-1-visitorckw@gmail.com> Precedence: bulk X-Mailing-List: dm-devel@lists.linux.dev List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Drop the heap-related macros from bcachefs and replacing them with the generic min_heap implementation from include/linux. By doing so, code readability is improved by using functions instead of macros. Moreover, the min_heap implementation in include/linux adopts a bottom-up variation compared to the textbook version currently used in bcachefs. This bottom-up variation allows for approximately 50% reduction in the number of comparison operations during heap siftdown, without changing the number of swaps, thus making it more efficient. Link: https://lkml.kernel.org/ioyfizrzq7w7mjrqcadtzsfgpuntowtjdw5pgn4qhvsdp4mqqg@nrlek5vmisbu Signed-off-by: Kuan-Wei Chiu Reviewed-by: Ian Rogers --- Changes in v2: - Add attribute __always_unused to the compare and swap functions that do not use the args parameter. - Rename min_heapify() to min_heap_sift_down(). - Refine the commit message. fs/bcachefs/clock.c | 53 +++++++++++----- fs/bcachefs/clock_types.h | 2 +- fs/bcachefs/ec.c | 100 +++++++++++++++++++----------- fs/bcachefs/ec_types.h | 2 +- fs/bcachefs/util.h | 127 +++----------------------------------- 5 files changed, 110 insertions(+), 174 deletions(-) diff --git a/fs/bcachefs/clock.c b/fs/bcachefs/clock.c index 363644451106..e898f4693bd0 100644 --- a/fs/bcachefs/clock.c +++ b/fs/bcachefs/clock.c @@ -6,16 +6,29 @@ #include #include -static inline long io_timer_cmp(io_timer_heap *h, - struct io_timer *l, - struct io_timer *r) +static inline bool io_timer_cmp(const void *l, const void *r, void __always_unused *args) { - return l->expire - r->expire; + struct io_timer *_l = (struct io_timer *)l; + struct io_timer *_r = (struct io_timer *)r; + + return _l->expire >= _r->expire; +} + +static inline void io_timer_swp(void *l, void *r, void __always_unused *args) +{ + struct io_timer *_l = (struct io_timer *)l; + struct io_timer *_r = (struct io_timer *)r; + + swap(*_l, *_r); } void bch2_io_timer_add(struct io_clock *clock, struct io_timer *timer) { size_t i; + const struct min_heap_callbacks callbacks = { + .less = io_timer_cmp, + .swp = io_timer_swp, + }; spin_lock(&clock->timer_lock); @@ -26,11 +39,11 @@ void bch2_io_timer_add(struct io_clock *clock, struct io_timer *timer) return; } - for (i = 0; i < clock->timers.used; i++) - if (clock->timers.data[i] == timer) + for (i = 0; i < clock->timers.heap.nr; i++) + if (min_heap_peek(&clock->timers)[i] == timer) goto out; - BUG_ON(!heap_add(&clock->timers, timer, io_timer_cmp, NULL)); + BUG_ON(!min_heap_push(&clock->timers, &timer, &callbacks, NULL)); out: spin_unlock(&clock->timer_lock); } @@ -38,12 +51,16 @@ void bch2_io_timer_add(struct io_clock *clock, struct io_timer *timer) void bch2_io_timer_del(struct io_clock *clock, struct io_timer *timer) { size_t i; + const struct min_heap_callbacks callbacks = { + .less = io_timer_cmp, + .swp = io_timer_swp, + }; spin_lock(&clock->timer_lock); - for (i = 0; i < clock->timers.used; i++) - if (clock->timers.data[i] == timer) { - heap_del(&clock->timers, i, io_timer_cmp, NULL); + for (i = 0; i < clock->timers.heap.nr; i++) + if (min_heap_peek(&clock->timers)[i] == timer) { + min_heap_pop(&clock->timers, &callbacks, NULL); break; } @@ -131,12 +148,16 @@ static struct io_timer *get_expired_timer(struct io_clock *clock, unsigned long now) { struct io_timer *ret = NULL; + const struct min_heap_callbacks callbacks = { + .less = io_timer_cmp, + .swp = io_timer_swp, + }; spin_lock(&clock->timer_lock); - if (clock->timers.used && - time_after_eq(now, clock->timers.data[0]->expire)) - heap_pop(&clock->timers, ret, io_timer_cmp, NULL); + if (clock->timers.heap.nr && + time_after_eq(now, min_heap_peek(&clock->timers)[0]->expire)) + min_heap_pop(&clock->timers, &callbacks, NULL); spin_unlock(&clock->timer_lock); @@ -161,10 +182,10 @@ void bch2_io_timers_to_text(struct printbuf *out, struct io_clock *clock) spin_lock(&clock->timer_lock); now = atomic64_read(&clock->now); - for (i = 0; i < clock->timers.used; i++) + for (i = 0; i < clock->timers.heap.nr; i++) prt_printf(out, "%ps:\t%li\n", - clock->timers.data[i]->fn, - clock->timers.data[i]->expire - now); + min_heap_peek(&clock->timers)[i]->fn, + min_heap_peek(&clock->timers)[i]->expire - now); spin_unlock(&clock->timer_lock); --out->atomic; } diff --git a/fs/bcachefs/clock_types.h b/fs/bcachefs/clock_types.h index 5fae0012d808..b02b24b9d74f 100644 --- a/fs/bcachefs/clock_types.h +++ b/fs/bcachefs/clock_types.h @@ -23,7 +23,7 @@ struct io_timer { /* Amount to buffer up on a percpu counter */ #define IO_CLOCK_PCPU_SECTORS 128 -typedef HEAP(struct io_timer *) io_timer_heap; +typedef MIN_HEAP(struct io_timer *, io_timer_heap) io_timer_heap; struct io_clock { atomic64_t now; diff --git a/fs/bcachefs/ec.c b/fs/bcachefs/ec.c index 082075244e16..68c5e9e928a7 100644 --- a/fs/bcachefs/ec.c +++ b/fs/bcachefs/ec.c @@ -860,14 +860,15 @@ static int __ec_stripe_mem_alloc(struct bch_fs *c, size_t idx, gfp_t gfp) { ec_stripes_heap n, *h = &c->ec_stripes_heap; - if (idx >= h->size) { + if (idx >= h->heap.size) { if (!init_heap(&n, max(1024UL, roundup_pow_of_two(idx + 1)), gfp)) return -BCH_ERR_ENOMEM_ec_stripe_mem_alloc; mutex_lock(&c->ec_stripes_heap_lock); - if (n.size > h->size) { - memcpy(n.data, h->data, h->used * sizeof(h->data[0])); - n.used = h->used; + if (n.heap.size > h->heap.size) { + memcpy(min_heap_peek(&n), min_heap_peek(h), + h->heap.nr * sizeof(*min_heap_peek(h))); + n.heap.nr = h->heap.nr; swap(*h, n); } mutex_unlock(&c->ec_stripes_heap_lock); @@ -958,20 +959,21 @@ static u64 stripe_idx_to_delete(struct bch_fs *c) lockdep_assert_held(&c->ec_stripes_heap_lock); - if (h->used && - h->data[0].blocks_nonempty == 0 && - !bch2_stripe_is_open(c, h->data[0].idx)) - return h->data[0].idx; + if (h->heap.nr && + min_heap_peek(h)->blocks_nonempty == 0 && + !bch2_stripe_is_open(c, min_heap_peek(h)->idx)) + return min_heap_peek(h)->idx; return 0; } -static inline int ec_stripes_heap_cmp(ec_stripes_heap *h, - struct ec_stripe_heap_entry l, - struct ec_stripe_heap_entry r) +static inline bool ec_stripes_heap_cmp(const void *l, const void *r, void __always_unused *args) { - return ((l.blocks_nonempty > r.blocks_nonempty) - - (l.blocks_nonempty < r.blocks_nonempty)); + struct ec_stripe_heap_entry *_l = (struct ec_stripe_heap_entry *)l; + struct ec_stripe_heap_entry *_r = (struct ec_stripe_heap_entry *)r; + + return ((_l->blocks_nonempty > _r->blocks_nonempty) >= + (_l->blocks_nonempty < _r->blocks_nonempty)); } static inline void ec_stripes_heap_set_backpointer(ec_stripes_heap *h, @@ -979,7 +981,21 @@ static inline void ec_stripes_heap_set_backpointer(ec_stripes_heap *h, { struct bch_fs *c = container_of(h, struct bch_fs, ec_stripes_heap); - genradix_ptr(&c->stripes, h->data[i].idx)->heap_idx = i; + genradix_ptr(&c->stripes, min_heap_peek(h)[i].idx)->heap_idx = i; +} + +static inline void ec_stripes_heap_swap(void *l, void *r, void *h) +{ + struct ec_stripe_heap_entry *_l = (struct ec_stripe_heap_entry *)l; + struct ec_stripe_heap_entry *_r = (struct ec_stripe_heap_entry *)r; + ec_stripes_heap *_h = (ec_stripes_heap *)h; + size_t i = _l - min_heap_peek(_h); + size_t j = _r - min_heap_peek(_h); + + ec_stripes_heap_set_backpointer(_h, i); + ec_stripes_heap_set_backpointer(_h, j); + + swap(*_l, *_r); } static void heap_verify_backpointer(struct bch_fs *c, size_t idx) @@ -987,34 +1003,43 @@ static void heap_verify_backpointer(struct bch_fs *c, size_t idx) ec_stripes_heap *h = &c->ec_stripes_heap; struct stripe *m = genradix_ptr(&c->stripes, idx); - BUG_ON(m->heap_idx >= h->used); - BUG_ON(h->data[m->heap_idx].idx != idx); + BUG_ON(m->heap_idx >= h->heap.nr); + BUG_ON(min_heap_peek(h)[m->heap_idx].idx != idx); } void bch2_stripes_heap_del(struct bch_fs *c, struct stripe *m, size_t idx) { + const struct min_heap_callbacks callbacks = { + .less = ec_stripes_heap_cmp, + .swp = ec_stripes_heap_swap, + }; + mutex_lock(&c->ec_stripes_heap_lock); heap_verify_backpointer(c, idx); - heap_del(&c->ec_stripes_heap, m->heap_idx, - ec_stripes_heap_cmp, - ec_stripes_heap_set_backpointer); + min_heap_del(&c->ec_stripes_heap, m->heap_idx, &callbacks, &c->ec_stripes_heap); mutex_unlock(&c->ec_stripes_heap_lock); } void bch2_stripes_heap_insert(struct bch_fs *c, struct stripe *m, size_t idx) { + const struct min_heap_callbacks callbacks = { + .less = ec_stripes_heap_cmp, + .swp = ec_stripes_heap_swap, + }; + mutex_lock(&c->ec_stripes_heap_lock); - BUG_ON(heap_full(&c->ec_stripes_heap)); + BUG_ON(min_heap_full(&c->ec_stripes_heap)); - heap_add(&c->ec_stripes_heap, ((struct ec_stripe_heap_entry) { + genradix_ptr(&c->stripes, idx)->heap_idx = c->ec_stripes_heap.heap.nr; + min_heap_push(&c->ec_stripes_heap, &((struct ec_stripe_heap_entry) { .idx = idx, .blocks_nonempty = m->blocks_nonempty, }), - ec_stripes_heap_cmp, - ec_stripes_heap_set_backpointer); + &callbacks, + &c->ec_stripes_heap); heap_verify_backpointer(c, idx); mutex_unlock(&c->ec_stripes_heap_lock); @@ -1026,17 +1051,20 @@ void bch2_stripes_heap_update(struct bch_fs *c, ec_stripes_heap *h = &c->ec_stripes_heap; bool do_deletes; size_t i; + const struct min_heap_callbacks callbacks = { + .less = ec_stripes_heap_cmp, + .swp = ec_stripes_heap_swap, + }; mutex_lock(&c->ec_stripes_heap_lock); heap_verify_backpointer(c, idx); - h->data[m->heap_idx].blocks_nonempty = m->blocks_nonempty; + min_heap_peek(h)[m->heap_idx].blocks_nonempty = m->blocks_nonempty; i = m->heap_idx; - heap_sift_up(h, i, ec_stripes_heap_cmp, - ec_stripes_heap_set_backpointer); - heap_sift_down(h, i, ec_stripes_heap_cmp, - ec_stripes_heap_set_backpointer); + + min_heap_sift_up(h, i, &callbacks, &c->ec_stripes_heap); + min_heap_sift_down(h, i, &callbacks, &c->ec_stripes_heap); heap_verify_backpointer(c, idx); @@ -1828,12 +1856,12 @@ static s64 get_existing_stripe(struct bch_fs *c, return -1; mutex_lock(&c->ec_stripes_heap_lock); - for (heap_idx = 0; heap_idx < h->used; heap_idx++) { + for (heap_idx = 0; heap_idx < h->heap.nr; heap_idx++) { /* No blocks worth reusing, stripe will just be deleted: */ - if (!h->data[heap_idx].blocks_nonempty) + if (!min_heap_peek(h)[heap_idx].blocks_nonempty) continue; - stripe_idx = h->data[heap_idx].idx; + stripe_idx = min_heap_peek(h)[heap_idx].idx; m = genradix_ptr(&c->stripes, stripe_idx); @@ -2159,14 +2187,14 @@ void bch2_stripes_heap_to_text(struct printbuf *out, struct bch_fs *c) size_t i; mutex_lock(&c->ec_stripes_heap_lock); - for (i = 0; i < min_t(size_t, h->used, 50); i++) { - m = genradix_ptr(&c->stripes, h->data[i].idx); + for (i = 0; i < min_t(size_t, h->heap.nr, 50); i++) { + m = genradix_ptr(&c->stripes, min_heap_peek(h)[i].idx); - prt_printf(out, "%zu %u/%u+%u", h->data[i].idx, - h->data[i].blocks_nonempty, + prt_printf(out, "%zu %u/%u+%u", min_heap_peek(h)[i].idx, + min_heap_peek(h)[i].blocks_nonempty, m->nr_blocks - m->nr_redundant, m->nr_redundant); - if (bch2_stripe_is_open(c, h->data[i].idx)) + if (bch2_stripe_is_open(c, min_heap_peek(h)[i].idx)) prt_str(out, " open"); prt_newline(out); } diff --git a/fs/bcachefs/ec_types.h b/fs/bcachefs/ec_types.h index 976426da3a12..2ed67431a81c 100644 --- a/fs/bcachefs/ec_types.h +++ b/fs/bcachefs/ec_types.h @@ -36,6 +36,6 @@ struct ec_stripe_heap_entry { unsigned blocks_nonempty; }; -typedef HEAP(struct ec_stripe_heap_entry) ec_stripes_heap; +typedef MIN_HEAP(struct ec_stripe_heap_entry, ec_stripes_heap) ec_stripes_heap; #endif /* _BCACHEFS_EC_TYPES_H */ diff --git a/fs/bcachefs/util.h b/fs/bcachefs/util.h index 175aee3074c7..e0c86ad04bf3 100644 --- a/fs/bcachefs/util.h +++ b/fs/bcachefs/util.h @@ -8,6 +8,7 @@ #include #include #include +#include #include #include #include @@ -54,134 +55,20 @@ static inline size_t buf_pages(void *p, size_t len) PAGE_SIZE); } -#define HEAP(type) \ -struct { \ - size_t size, used; \ - type *data; \ -} - -#define DECLARE_HEAP(type, name) HEAP(type) name - #define init_heap(heap, _size, gfp) \ ({ \ - (heap)->used = 0; \ - (heap)->size = (_size); \ - (heap)->data = kvmalloc((heap)->size * sizeof((heap)->data[0]),\ - (gfp)); \ -}) - -#define free_heap(heap) \ -do { \ - kvfree((heap)->data); \ - (heap)->data = NULL; \ -} while (0) - -#define heap_set_backpointer(h, i, _fn) \ -do { \ - void (*fn)(typeof(h), size_t) = _fn; \ - if (fn) \ - fn(h, i); \ -} while (0) - -#define heap_swap(h, i, j, set_backpointer) \ -do { \ - swap((h)->data[i], (h)->data[j]); \ - heap_set_backpointer(h, i, set_backpointer); \ - heap_set_backpointer(h, j, set_backpointer); \ -} while (0) - -#define heap_peek(h) \ -({ \ - EBUG_ON(!(h)->used); \ - (h)->data[0]; \ -}) - -#define heap_full(h) ((h)->used == (h)->size) - -#define heap_sift_down(h, i, cmp, set_backpointer) \ -do { \ - size_t _c, _j = i; \ - \ - for (; _j * 2 + 1 < (h)->used; _j = _c) { \ - _c = _j * 2 + 1; \ - if (_c + 1 < (h)->used && \ - cmp(h, (h)->data[_c], (h)->data[_c + 1]) >= 0) \ - _c++; \ - \ - if (cmp(h, (h)->data[_c], (h)->data[_j]) >= 0) \ - break; \ - heap_swap(h, _c, _j, set_backpointer); \ - } \ -} while (0) - -#define heap_sift_up(h, i, cmp, set_backpointer) \ -do { \ - while (i) { \ - size_t p = (i - 1) / 2; \ - if (cmp(h, (h)->data[i], (h)->data[p]) >= 0) \ - break; \ - heap_swap(h, i, p, set_backpointer); \ - i = p; \ - } \ -} while (0) - -#define __heap_add(h, d, cmp, set_backpointer) \ -({ \ - size_t _i = (h)->used++; \ - (h)->data[_i] = d; \ - heap_set_backpointer(h, _i, set_backpointer); \ - \ - heap_sift_up(h, _i, cmp, set_backpointer); \ - _i; \ -}) - -#define heap_add(h, d, cmp, set_backpointer) \ -({ \ - bool _r = !heap_full(h); \ - if (_r) \ - __heap_add(h, d, cmp, set_backpointer); \ - _r; \ + void *data = kvmalloc(_size * sizeof(*min_heap_peek(heap)), (gfp));\ + min_heap_init(heap, data, _size); \ + min_heap_peek(heap); \ }) -#define heap_add_or_replace(h, new, cmp, set_backpointer) \ -do { \ - if (!heap_add(h, new, cmp, set_backpointer) && \ - cmp(h, new, heap_peek(h)) >= 0) { \ - (h)->data[0] = new; \ - heap_set_backpointer(h, 0, set_backpointer); \ - heap_sift_down(h, 0, cmp, set_backpointer); \ - } \ -} while (0) -#define heap_del(h, i, cmp, set_backpointer) \ +#define free_heap(_heap) \ do { \ - size_t _i = (i); \ - \ - BUG_ON(_i >= (h)->used); \ - (h)->used--; \ - if ((_i) < (h)->used) { \ - heap_swap(h, _i, (h)->used, set_backpointer); \ - heap_sift_up(h, _i, cmp, set_backpointer); \ - heap_sift_down(h, _i, cmp, set_backpointer); \ - } \ + kvfree((_heap)->heap.data); \ + (_heap)->heap.data = NULL; \ } while (0) -#define heap_pop(h, d, cmp, set_backpointer) \ -({ \ - bool _r = (h)->used; \ - if (_r) { \ - (d) = (h)->data[0]; \ - heap_del(h, 0, cmp, set_backpointer); \ - } \ - _r; \ -}) - -#define heap_resort(heap, cmp, set_backpointer) \ -do { \ - ssize_t _i; \ - for (_i = (ssize_t) (heap)->used / 2 - 1; _i >= 0; --_i) \ - heap_sift_down(heap, _i, cmp, set_backpointer); \ -} while (0) #define ANYSINT_MAX(t) \ ((((t) 1 << (sizeof(t) * 8 - 2)) - (t) 1) * (t) 2 + (t) 1)