From patchwork Sat Feb 3 00:09:09 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 196102 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7301:9bc1:b0:106:209c:c626 with SMTP id op1csp769519dyc; Fri, 2 Feb 2024 16:10:57 -0800 (PST) X-Google-Smtp-Source: AGHT+IFI5NOWy1do4CUjQZ0fR+VFYGBext1PxVEl0h8/q91f6rf+Pc2q24qaoFMLzb74bLRG/iuj X-Received: by 2002:a05:622a:181a:b0:42a:5923:b0cc with SMTP id t26-20020a05622a181a00b0042a5923b0ccmr9934720qtc.59.1706919056953; Fri, 02 Feb 2024 16:10:56 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1706919056; cv=pass; d=google.com; s=arc-20160816; b=skHlh3nbMlZLruJ1YyXAhcMGHlLz+3tkpfETKlSeccbo3GsNITNoN9+eyA0YUFOI6H wYLjVgiaoWwWafZ2jycistJicEFceoVKKx3ndILKrAzW2PwTVQ3al+qfbS6usRk182e/ rLbomF+C+zA2P5Ljbuj8Gb8oOBIg+YxtCdaagJREOe66XP45tLu2qYTS81CYT0zkwq57 YkPkTpWfP+iQ3PhTlXW0oY07cgwK5zcyBqKKOLCATAAhQjylNGN9L+ZXEvHfJcAaYe0L VCIKDp/4pdnrsmEoL0vo6aT605ezRrk1N9S4jHHlp/so1HS4hXQSVvVFwLlLx9sJS8+C MoZw== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=cc:to:from:subject:message-id:references:mime-version :list-unsubscribe:list-subscribe:list-id:precedence:in-reply-to:date :reply-to:dkim-signature; bh=EJcVR2aXcmJcYN2R0niM7r+O1TbQOV0XdUcPDJMxbc8=; fh=sEMHsSH/YS1Znc/5csPgpjo29UzyALhBI8Cs+cUtdCo=; b=vl/0Dm6OhnyXIuIimkNYvu7iauA5GUlWA8/TRlYx+8OZKhYpYff8FYj4y/7TJDahr/ 5hvqzau8LDctzCFO2iPObKdFmufYLwEEXWLn6VW4qYDS0ssUq12JpU0P9tzfMJZIgSAz /GrQSLcbgt63xzV8qC1AMjL3OWm+x511Ib5UPMt2w+Dh4c67OzNWspFaLDoE5yf21U9j BC64bNL/wguO6bb1jESKHfN3C4vqnezLTP0J/u/pqgFYHgs9wa9KrX3wqiSSC5kafB5G XL0UoGLsHBVURi+TwBlNhG5nwONJE17+YUThM1zxg2wn/o9Jz1L2GjMQFDLht8Bh83x8 UvFQ==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=GS2Axujj; arc=pass (i=1 spf=pass spfdomain=flex--seanjc.bounces.google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-50749-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-50749-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com X-Forwarded-Encrypted: i=1; AJvYcCWU1cGBUWfaS/N+Fq5pVmXEmtpdq6+EGf7oRpF2lCzeS2Z4SwEwd26fvm2LBsTV7En33UDjcKYJa11KOexdt08unUNjsQ== Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [2604:1380:45d1:ec00::1]) by mx.google.com with ESMTPS id v20-20020a05622a189400b0042bda762eedsi3149569qtc.759.2024.02.02.16.10.56 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 02 Feb 2024 16:10:56 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-50749-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) client-ip=2604:1380:45d1:ec00::1; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=GS2Axujj; arc=pass (i=1 spf=pass spfdomain=flex--seanjc.bounces.google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-50749-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-50749-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ny.mirrors.kernel.org (Postfix) with ESMTPS id BB83B1C21342 for ; Sat, 3 Feb 2024 00:10:56 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 6B483C135; Sat, 3 Feb 2024 00:09:32 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="GS2Axujj" Received: from mail-yw1-f201.google.com (mail-yw1-f201.google.com [209.85.128.201]) (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 B276B63D1 for ; Sat, 3 Feb 2024 00:09:27 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706918969; cv=none; b=HDZeyPDkMZ5hstr5J5hhboP7F9suRCTDRqh9P/vVTM2mhyvWN3CUxE+hR+UCjF9nP2X1j6EerUEvKGAKt1LzZIKoLrAqcutzYUpKc/OfpNxE1OjZQVeWdDfMGllV0WgTennk48tcd2PQIsFxxlAsaxg25l3Ut0cR66I0KjvtSwY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706918969; c=relaxed/simple; bh=5adFsddT99jRoR/vIsu3Y6j5XoRwGq4E/i/qqsJ/xJA=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=lih4ng0N1u0RrmV7fSvhsG7U+B1V2Xcyyz43H9h9RHig1GBuBqF0mJgKGWPPMlDbfdXw4XmcEQuXsSXMxq55ie7iefG24goDREUO3vBIuF7ftC674oj+1CnXlx7kI45LAyoXLQ9qKVjc7xd1ADZsQoIq+YgeVKPkt/XG19xziZo= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=GS2Axujj; arc=none smtp.client-ip=209.85.128.201 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com Received: by mail-yw1-f201.google.com with SMTP id 00721157ae682-6041bb56dbfso39644147b3.2 for ; Fri, 02 Feb 2024 16:09:27 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1706918966; x=1707523766; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:from:to:cc:subject:date:message-id:reply-to; bh=EJcVR2aXcmJcYN2R0niM7r+O1TbQOV0XdUcPDJMxbc8=; b=GS2AxujjAaHn3yveWNxDHDiVD5cIhLg+IDoVBNBCG6toa3XyqVGvPcN5Dd90VnftUZ uMAPrfOrjQAL7BaP8P4/pGlXpBiTrO1nDjnyKN8Ru0RbZml84yaVo1WO3pKcTm1BP4yH grbN7uBD6LnBTIWU+sJwT+nBPKz53ud5rIyGygu9me/8PUrBxHFlBhVShtQ56BH1PzUZ c20+2TFooNzEjhAsyOdGt4Wzrhnov6rtefQdk7oN0bsbmojLQNTkyKc5/a75E+K0PX30 kH9LiAYpkfXkbW8SjI+mDIXbYYrSXzsn7alT5Yc5nD9u2OqbBR3xO89UVPGmlHHr37vX TXVA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1706918966; x=1707523766; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=EJcVR2aXcmJcYN2R0niM7r+O1TbQOV0XdUcPDJMxbc8=; b=XUzLO4WLvR6m8t+NCIDMB+O/G/2J8PiyXDaJIilDNRykx/Pwyv9FCey40m4GgYtRYl VpMsobbBTUyNrEwvsJsokidq/Gu1qHNqq7qDgXcEoLrhCSErNuEAqged62RoxormpkIX OeW+pjk2AXrEky0MqWATZ66H3PhI6acZsfGjPRugaCBF74A+YJ+/vTDGHd8lSeVW7AZR O2NkVNFCq41YQ17AE/nxw993Vn3tWMxbyf7TZnbA/og9ll2ht6SZMZ5gwWfkD8fIz9CU Hdjh2gwfjyE2enICVNCK2qVZttXfBNUtBFHj9FpI9D7ZUdiCpE1p022XgF2ZnEjZSvAY EFUA== X-Gm-Message-State: AOJu0YzkIq0HPvo6uAyQGrB/Qqo0JsX8yQPXyBniJAIUakGNAsEHKghs PAzY8pJMM1lqXr4YSICFUNAr123Hj5zlH0W7juY9t2edp8E+3db8IYhuv4fG6BxgvQ1P77v8T3d i3g== X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a81:9b4b:0:b0:5ff:6041:c17d with SMTP id s72-20020a819b4b000000b005ff6041c17dmr1167011ywg.2.1706918966644; Fri, 02 Feb 2024 16:09:26 -0800 (PST) Reply-To: Sean Christopherson Date: Fri, 2 Feb 2024 16:09:09 -0800 In-Reply-To: <20240203000917.376631-1-seanjc@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240203000917.376631-1-seanjc@google.com> X-Mailer: git-send-email 2.43.0.594.gd9cf4e227d-goog Message-ID: <20240203000917.376631-4-seanjc@google.com> Subject: [PATCH v8 03/10] KVM: selftests: Add a macro to iterate over a sparsebit range From: Sean Christopherson To: Paolo Bonzini , Marc Zyngier , Oliver Upton , Anup Patel , Paul Walmsley , Palmer Dabbelt , Albert Ou , Christian Borntraeger , Janosch Frank , Claudio Imbrenda , Sean Christopherson Cc: kvm@vger.kernel.org, linux-arm-kernel@lists.infradead.org, kvmarm@lists.linux.dev, kvm-riscv@lists.infradead.org, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, Vishal Annapurve , Ackerley Tng , Andrew Jones , Tom Lendacky , Michael Roth , Peter Gonda X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1789834357244809597 X-GMAIL-MSGID: 1789834357244809597 From: Ackerley Tng Add sparsebit_for_each_set_range() to allow iterator over a range of set bits in a range. This will be used by x86 SEV guests to process protected physical pages (each such page needs to be encrypted _after_ being "added" to the VM). Signed-off-by: Ackerley Tng [sean: split to separate patch] Signed-off-by: Sean Christopherson --- .../testing/selftests/kvm/include/sparsebit.h | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/tools/testing/selftests/kvm/include/sparsebit.h b/tools/testing/selftests/kvm/include/sparsebit.h index fb5170d57fcb..bc760761e1a3 100644 --- a/tools/testing/selftests/kvm/include/sparsebit.h +++ b/tools/testing/selftests/kvm/include/sparsebit.h @@ -66,6 +66,26 @@ void sparsebit_dump(FILE *stream, const struct sparsebit *sbit, unsigned int indent); void sparsebit_validate_internal(const struct sparsebit *sbit); +/* + * Iterate over an inclusive ranges within sparsebit @s. In each iteration, + * @range_begin and @range_end will take the beginning and end of the set + * range, which are of type sparsebit_idx_t. + * + * For example, if the range [3, 7] (inclusive) is set, within the + * iteration,@range_begin will take the value 3 and @range_end will take + * the value 7. + * + * Ensure that there is at least one bit set before using this macro with + * sparsebit_any_set(), because sparsebit_first_set() will abort if none + * are set. + */ +#define sparsebit_for_each_set_range(s, range_begin, range_end) \ + for (range_begin = sparsebit_first_set(s), \ + range_end = sparsebit_next_clear(s, range_begin) - 1; \ + range_begin && range_end; \ + range_begin = sparsebit_next_set(s, range_end), \ + range_end = sparsebit_next_clear(s, range_begin) - 1) + #ifdef __cplusplus } #endif