Received: by 2002:a05:7412:3b8b:b0:fc:a2b0:25d7 with SMTP id nd11csp2940928rdb; Tue, 13 Feb 2024 01:45:44 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCV5NxAAGgYRVUWJHNF/ltVfKxe3GgMAHdzV0CzP2Ax6jnaUqSsutNjvOdwkKaj1paJA3mCplTbFE0MAzWBGDhsL9mFin3bHHbKCgZeUpg== X-Google-Smtp-Source: AGHT+IHqfrxhpVL2CnPj6KgY1S00013MpTaZuJfq64EsYgZdZgBcXnJ//4lGo7XZmBoe+7raHJrl X-Received: by 2002:a62:e70b:0:b0:6e0:f752:b46a with SMTP id s11-20020a62e70b000000b006e0f752b46amr1440889pfh.19.1707817544475; Tue, 13 Feb 2024 01:45:44 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1707817544; cv=pass; d=google.com; s=arc-20160816; b=vsgs+90t0JDvqDxZDSq1CgA5C1JGUSGV4IgPINKJPVup0tzXmj0JPlrI0092eqK5F3 dg0fP5Z+062srkGuGdgb2O6PqvJ0ORJf8izvuii7tIW8m2copnerEPPOXTn4XurZBLUP hHe/JK6nw9JpNkuonQuF9VCG9YRT+lVwFAPEoE9eaosp05kIYeGgp/7MlnNwDdrGwH7O GEKLnBcbglZqAd4i1BII9BfeZ4G3NNKkGJ1i8rZio3rMzoXk7weEgw+uriONcpcRqr+e jJ0qFZrWS1Nl3USlCotdLGQ2zy+Z11WeTHNxvhL0Fzt/AW7HV1ADZDu8X2RYCDuCFgda cKFg== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from:dkim-signature; bh=BKECb1eTQQJ/f0GKxMxXWqmjCFHiATDCrLFETxE2qgY=; fh=ZrZcLr7SjU1yw/CjmoaeAoPNTgktwhPMEMJ59EuzWDw=; b=TH528rC1Ls6liAMkm5RddphxzEgUxqDYwTmklvzXaR9GOb42FowF6jqwDezW4cPpf2 vsEBuURPrtBMWuK6a8XOhIA/lnxixLpfhehqDq1Mjyh5TBMANb6aPKm4GPd+9X4zeOtK tyMpQad9ITAcK5BZoOreJJ1QjM1HGWDVE9l9tEPwIlPqG1i4uzio/xhre/qgImGmZRa0 kvBKn9vUFeEtC73nVh2hgeA6vGIInsbL/kg0qIsfWesvkxCcazydjtv4o+V+qDBDH/2N Leb7MnVqIQye6mhs7p6S+5lToxRfBNRdKxIGhUjrwboMltldLFvFxs72kAvHAkQe0N+M RqVw==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@linux.dev header.s=key1 header.b=Kv8VMoYw; arc=pass (i=1 spf=pass spfdomain=linux.dev dkim=pass dkdomain=linux.dev dmarc=pass fromdomain=linux.dev); spf=pass (google.com: domain of linux-kernel+bounces-63218-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-63218-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linux.dev X-Forwarded-Encrypted: i=2; AJvYcCU04jJpWiMqvtBEWylMmdNP+6hYbdrhypgVHLpzJaehJ7dkHzoi2h8xG/Wuk5VPv32j5PhGPzYubs2WZq7zojJ00fNZkkHStgGYSlpVZQ== Return-Path: Received: from sy.mirrors.kernel.org (sy.mirrors.kernel.org. [2604:1380:40f1:3f00::1]) by mx.google.com with ESMTPS id p21-20020a056a000b5500b006e09ea64bb4si6184254pfo.2.2024.02.13.01.45.43 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 13 Feb 2024 01:45:44 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-63218-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) client-ip=2604:1380:40f1:3f00::1; Authentication-Results: mx.google.com; dkim=pass header.i=@linux.dev header.s=key1 header.b=Kv8VMoYw; arc=pass (i=1 spf=pass spfdomain=linux.dev dkim=pass dkdomain=linux.dev dmarc=pass fromdomain=linux.dev); spf=pass (google.com: domain of linux-kernel+bounces-63218-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-63218-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linux.dev 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 sy.mirrors.kernel.org (Postfix) with ESMTPS id 5A4D6B24B49 for ; Tue, 13 Feb 2024 09:38:02 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 4BABC51025; Tue, 13 Feb 2024 09:33:40 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b="Kv8VMoYw" Received: from out-175.mta1.migadu.com (out-175.mta1.migadu.com [95.215.58.175]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id E8955364DB for ; Tue, 13 Feb 2024 09:33:37 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=95.215.58.175 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707816819; cv=none; b=fATo7uoaTe19HtlBR5iiHFN2rXhwj3RfresGf8MwZbhwZVCJca+SKJCPwpbsvtM+Qx8hFptpul7Fh1H32A6M/Uf0HDD8piN4OggAjrvtnv9dB2dfusAqZlSGfZDHcNaDO9+UqHdsTLIcTUBmhEu5mtXL+J1EUv9xJeNJaLfYBMY= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707816819; c=relaxed/simple; bh=Qi8rU6NjOkVcx9TX1/O9p73F+ULC5MIdl/I9CDfvIew=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=hsmiMYczPi3RM7gIZISGgDedbmy/i0i4dQEX8qHoeyRXAjNmDUuQZLyglgG1PTmoJmXYi3ngMEdvDN2gIuuBV7NY46/jiKrTNSGV00TpBwh6aIPM04mTrKZ6DRj2q1fxmHlO9+Alu8cXuSZx9RBwfbSFTWuGn/6x0N/1tteANxs= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev; spf=pass smtp.mailfrom=linux.dev; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b=Kv8VMoYw; arc=none smtp.client-ip=95.215.58.175 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.dev X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1707816816; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=BKECb1eTQQJ/f0GKxMxXWqmjCFHiATDCrLFETxE2qgY=; b=Kv8VMoYwDOw5o/4ifDv6LsdWhzI+zrP+hXZ/dyggg6voxrWhMxjS3n2slHUN6XjTEoqdYA dQuL0Hu9I6PuXqEFLSppH2l3M8sTV/qJmjOHGnCDT3mXHmHde1DdJLi7H/mz2ONCtgqeki OIZDsSupblvzEtuDnC7WHJDgthL8+mY= From: Oliver Upton To: kvmarm@lists.linux.dev Cc: kvm@vger.kernel.org, Marc Zyngier , James Morse , Suzuki K Poulose , Zenghui Yu , linux-kernel@vger.kernel.org, Oliver Upton Subject: [PATCH v2 13/23] KVM: arm64: vgic-its: Pick cache victim based on usage count Date: Tue, 13 Feb 2024 09:32:50 +0000 Message-ID: <20240213093250.3960069-14-oliver.upton@linux.dev> In-Reply-To: <20240213093250.3960069-1-oliver.upton@linux.dev> References: <20240213093250.3960069-1-oliver.upton@linux.dev> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Migadu-Flow: FLOW_OUT To date the translation cache LRU policy relies on the ordering of the linked-list to pick the victim, as entries are moved to the head of the list on every cache hit. These sort of transformations are incompatible with an rculist, necessitating a different strategy for recording usage in-place. Count the number of cache hits since the last translation cache miss for every entry. The preferences for selecting a victim are as follows: - Invalid entries over valid entries - Valid entry with the lowest usage count - In the case of a tie, pick the entry closest to the tail (oldest) Signed-off-by: Oliver Upton --- arch/arm64/kvm/vgic/vgic-its.c | 46 ++++++++++++++++++++++++++-------- 1 file changed, 36 insertions(+), 10 deletions(-) diff --git a/arch/arm64/kvm/vgic/vgic-its.c b/arch/arm64/kvm/vgic/vgic-its.c index a7ba20b57264..35926d5ae561 100644 --- a/arch/arm64/kvm/vgic/vgic-its.c +++ b/arch/arm64/kvm/vgic/vgic-its.c @@ -157,6 +157,7 @@ struct vgic_translation_cache_entry { u32 devid; u32 eventid; struct vgic_irq *irq; + atomic64_t usage_count; }; /** @@ -580,13 +581,7 @@ static struct vgic_irq *__vgic_its_check_cache(struct vgic_dist *dist, cte->eventid != eventid) continue; - /* - * Move this entry to the head, as it is the most - * recently used. - */ - if (!list_is_first(&cte->entry, &dist->lpi_translation_cache)) - list_move(&cte->entry, &dist->lpi_translation_cache); - + atomic64_inc(&cte->usage_count); return cte->irq; } @@ -619,6 +614,36 @@ static unsigned int vgic_its_max_cache_size(struct kvm *kvm) return atomic_read(&kvm->online_vcpus) * LPI_DEFAULT_PCPU_CACHE_SIZE; } +static struct vgic_translation_cache_entry *vgic_its_cache_victim(struct vgic_dist *dist, + s64 *max_usage) +{ + struct vgic_translation_cache_entry *cte, *victim = NULL; + s64 min, tmp, max = S64_MIN; + + /* + * Find the least used cache entry since the last cache miss, preferring + * older entries in the case of a tie. Return the max usage count seen + * during the scan to initialize the new cache entry. + */ + list_for_each_entry(cte, &dist->lpi_translation_cache, entry) { + tmp = atomic64_read(&cte->usage_count); + max = max(max, tmp); + + if (!cte->irq) { + victim = cte; + break; + } + + if (!victim || tmp <= min) { + victim = cte; + min = tmp; + } + } + + *max_usage = max; + return victim; +} + static void vgic_its_cache_translation(struct kvm *kvm, struct vgic_its *its, u32 devid, u32 eventid, struct vgic_irq *irq) @@ -627,6 +652,7 @@ static void vgic_its_cache_translation(struct kvm *kvm, struct vgic_its *its, struct vgic_dist *dist = &kvm->arch.vgic; unsigned long flags; phys_addr_t db; + s64 usage = 0; /* Do not cache a directly injected interrupt */ if (irq->hw) @@ -650,9 +676,8 @@ static void vgic_its_cache_translation(struct kvm *kvm, struct vgic_its *its, } if (dist->lpi_cache_count >= vgic_its_max_cache_size(kvm)) { - /* Always reuse the last entry (LRU policy) */ - victim = list_last_entry(&dist->lpi_translation_cache, - typeof(*cte), entry); + victim = vgic_its_cache_victim(dist, &usage); + list_del(&victim->entry); dist->lpi_cache_count--; } @@ -664,6 +689,7 @@ static void vgic_its_cache_translation(struct kvm *kvm, struct vgic_its *its, lockdep_assert_held(&its->its_lock); vgic_get_irq_kref(irq); + atomic64_set(&new->usage_count, usage); new->db = db; new->devid = devid; new->eventid = eventid; -- 2.43.0.687.g38aa6559b0-goog