From patchwork Wed Feb 1 12:52:57 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jean-Philippe Brucker X-Patchwork-Id: 13124389 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 09111C05027 for ; Wed, 1 Feb 2023 14:07:43 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:References:In-Reply-To: Message-Id:Date:Subject:Cc:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=+YrnXdIADdTKw8xnR3Rl82gCyz/qwfW2yzJWbJvOa5M=; b=eXk7SrXwrRTEi4 mEa2CvJjlRuhh1WMyCAr1SX3fkTExhAJWH+mJNEtIGDTEz/mMZ2x8+TQGsd3YQ5xMBmVWQpcTzd2K uYJ1DPQ0dGsHZjR5oBChFG+YLFPiLWelcwG6hvDTc80Nmdq4K3WHOvW1WWiSKBxU8sQ/WojQJ7xKQ Rqod6IPql3VyDfRJCYDCQqIfZf7N1Y+jRF1MHOl9d5w6SKzr2dNbV1FJKC65VUjRn4QyJNceSO/bV bRI4Nr4yG/t4a3KBY3UK+bo8jTRmCuWy0WfQ9GzajyrbcWAktMJ7kmJjdT8NsfCyi3cjM7ESXDchu KySYLtSRhnkMFKP3OXxA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1pNDlC-00CDWo-Be; Wed, 01 Feb 2023 14:06:51 +0000 Received: from desiato.infradead.org ([2001:8b0:10b:1:d65d:64ff:fe57:4e05]) by bombadil.infradead.org with esmtps (Exim 4.94.2 #2 (Red Hat Linux)) id 1pNCiC-00BnOv-5A for linux-arm-kernel@bombadil.infradead.org; Wed, 01 Feb 2023 12:59:40 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=desiato.20200630; h=Content-Transfer-Encoding:MIME-Version :References:In-Reply-To:Message-Id:Date:Subject:Cc:To:From:Sender:Reply-To: Content-Type:Content-ID:Content-Description; bh=yTVD2fQtd31jg24PflRIs2adMeiXA9dKMS0aZE3JHMI=; b=FCEgJah0rwUZUpO7rnBnB4JfeK jvMzARKHVtxCi2DhdPOkPBB7lPRkziKpV1HAG+6LJoaiMB+HD7qyQ/8z6nYLjOa21RaXi6N9AFJ73 2tqTU/PBXZ60z7Qd+1pnffpIHTEO0qJ7zPB/QUwHd+y5fGnTHLV44Mynw511zuE7+wADINLUhtgxF Wx1k3U3wpA70Cwcu3b7yMH4/W78t7VhAxwPMtHHqhXCiBm/veqkfVE+2pB9TPPK6XLFAYRnXYsIcr JLzojmLiJ07BmRBfKyBukhh/JyqUAZ330eVymF6M0NUF6JXEZB/SYd0z5PF/bFF14K5KR216SCPuv glF4dtSw==; Received: from mail-wr1-x429.google.com ([2a00:1450:4864:20::429]) by desiato.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1pNChY-004m0q-39 for linux-arm-kernel@lists.infradead.org; Wed, 01 Feb 2023 12:59:03 +0000 Received: by mail-wr1-x429.google.com with SMTP id r2so17218546wrv.7 for ; Wed, 01 Feb 2023 04:59:30 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; 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=yTVD2fQtd31jg24PflRIs2adMeiXA9dKMS0aZE3JHMI=; b=HgseBKQJR3mUP4ey4NQiqlt22BAAegJr70aI92CwFpJAsdt0eLdcdkeX4f0PdvjHrp Zj1aAgh/KUpylBjc5xQXtKIG2A/Nwa/2YINMMc3PtWgMR/N4QpRe0ODXXfSJrAXEDUtz xERnKveM4HB3z7TGKqVKpIc6rg0joIdZU1zmjyIDTn4eXa+wA3lZg/+fA7rFvBX6rxY0 0IQBXXWZsFDBElNLhlItHdshDMESxCzOp5LE/CQ9ja9Rty5K+toEHeTpBzYoB3ri2g7t gtywvnEpfqPDNv+mKroN/cCOtastA+PJD+XFtNCz38u9spceEJotlwv8BZo4Fh3eW/40 6H3Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; 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=yTVD2fQtd31jg24PflRIs2adMeiXA9dKMS0aZE3JHMI=; b=KEh5fhcn3VIT+Q6SmwAqNpXtt1eNw90iiMje0xur3jry3cqxiQOFYeY0IZgDocNoQZ us9b/56UF9Zqp6VZ76DtpE0G4pb3Aw/e+KsD56mhZxZVZpNRwPxDV7wfostKDMjqTRO+ m9F/+coQR0VhiKuK6rDS6b+z4Fs26MG6zCJZ6yfjFBmNOrNrfCkmBInS3McbR7jNLXeg FY7LJODry4QgNH1zzM1QHjxtNcTLChmWr03USpx0/l+4CF+rLhNYix/BOD4txkopBwhS fmnWF3YUcUn40yVe6+lJ3sJRhKVCsvCGJz+SiJQu66hJFiwl0s+2BKqvVk3VWU3zeoLU I+3Q== X-Gm-Message-State: AO0yUKXQqBHpbn+Mo/vzm2Myl3ZKP42OcOhh6m6kjoEugHrRYuLlN2fM Y3XKygH2JFWoU3jaGqcsx52i+g== X-Google-Smtp-Source: AK7set+anVUapcTWowjU9gRbchOJLs0dsis9UuqvCSYhzwBfEh+gNQ4gaKCP92MQeUd1Iw/kaXXq4A== X-Received: by 2002:adf:c754:0:b0:2bf:d554:30c9 with SMTP id b20-20020adfc754000000b002bfd55430c9mr2456675wrh.21.1675256369585; Wed, 01 Feb 2023 04:59:29 -0800 (PST) Received: from localhost.localdomain (054592b0.skybroadband.com. [5.69.146.176]) by smtp.gmail.com with ESMTPSA id m15-20020a056000024f00b002bfae16ee2fsm17972811wrz.111.2023.02.01.04.59.28 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 01 Feb 2023 04:59:29 -0800 (PST) From: Jean-Philippe Brucker To: maz@kernel.org, catalin.marinas@arm.com, will@kernel.org, joro@8bytes.org Cc: robin.murphy@arm.com, james.morse@arm.com, suzuki.poulose@arm.com, oliver.upton@linux.dev, yuzenghui@huawei.com, smostafa@google.com, dbrazdil@google.com, ryan.roberts@arm.com, linux-arm-kernel@lists.infradead.org, kvmarm@lists.linux.dev, iommu@lists.linux.dev, Jean-Philippe Brucker Subject: [RFC PATCH 13/45] KVM: arm64: pkvm: Add hyp_page_ref_inc_return() Date: Wed, 1 Feb 2023 12:52:57 +0000 Message-Id: <20230201125328.2186498-14-jean-philippe@linaro.org> X-Mailer: git-send-email 2.39.0 In-Reply-To: <20230201125328.2186498-1-jean-philippe@linaro.org> References: <20230201125328.2186498-1-jean-philippe@linaro.org> MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20230201_125901_420574_15F06CFE X-CRM114-Status: GOOD ( 11.84 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org Add a page_ref_inc() helper that returns an error on saturation instead of BUG()ing. There is no limit in the IOMMU API for the number of times a page can be mapped. Since pKVM has this limit at 2^16, error out gracefully. Signed-off-by: Jean-Philippe Brucker --- arch/arm64/kvm/hyp/include/nvhe/memory.h | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/arch/arm64/kvm/hyp/include/nvhe/memory.h b/arch/arm64/kvm/hyp/include/nvhe/memory.h index a8d4a5b919d2..c40fff5d6d22 100644 --- a/arch/arm64/kvm/hyp/include/nvhe/memory.h +++ b/arch/arm64/kvm/hyp/include/nvhe/memory.h @@ -57,10 +57,21 @@ static inline int hyp_page_count(void *addr) return p->refcount; } +/* + * Increase the refcount and return its new value. + * If the refcount is saturated, return a negative error + */ +static inline int hyp_page_ref_inc_return(struct hyp_page *p) +{ + if (p->refcount == USHRT_MAX) + return -EOVERFLOW; + + return ++p->refcount; +} + static inline void hyp_page_ref_inc(struct hyp_page *p) { - BUG_ON(p->refcount == USHRT_MAX); - p->refcount++; + BUG_ON(hyp_page_ref_inc_return(p) <= 0); } static inline void hyp_page_ref_dec(struct hyp_page *p)