Received: by 2002:ab2:7041:0:b0:1f4:bcc8:f211 with SMTP id x1csp181448lql; Fri, 12 Apr 2024 07:23:20 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCUgyfZmapUGk6JNj5vISMOUibWE/Y0qXpT+Pk/rmHTQq78YpnubEoTKlbtDCAhlQFAYoty7OG7UuLTOHrj5kTDBjhdCcKXGMlDoV3HcLQ== X-Google-Smtp-Source: AGHT+IHyrOIFqETRdYAo/uMwX/CCXjx/zHKTLTTBAjGn+hmyqHphdiCoXu4+lMd4Lc8FZss3vebU X-Received: by 2002:a05:6122:458d:b0:4da:9900:a547 with SMTP id de13-20020a056122458d00b004da9900a547mr3221600vkb.1.1712931799813; Fri, 12 Apr 2024 07:23:19 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1712931799; cv=pass; d=google.com; s=arc-20160816; b=GTifwSg1je6f0p18FrtIvza+HDJg3KPCpYLuW6me83L2gBWFI1hcWH25m3Gce2S1is mMAMyVzxRz2sXIpiJXQoW1HkTqyN0a98M8gvylotHAkpwUSsAt0353AMIHn41f15QR4M dB1x5PyeQjiNMZrx6nbGF8J7ujSihQeEXdfQMGnl1LU6G///pMDwNF1hzArLCt23rM1J IHe4h2jroFrM62aQ/VYviPAaNi+bwRN3/PysTsFyXfdF4vT+V1AVWq9nChVE1MRVFfdl 1O8S2TAOvaW7uSsQthZ/1lNS49/dTEiEtgD4j8/JlSEh7KioJIuJqzGE2uWdikIeZcLN psGw== 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=fF1JT3Ro4zg1bI3TxDVBJ5avgRwDbgOZB+fig1j1Nw8=; fh=A+QcyawwK6Yq8daxuWFm+g/417ivXyGCFoP00qnMPro=; b=LJQGX3graGNbBtdjFFeZQdjJoN8i3sGArjiqjKMw5/8WhEKoVPNtYurPbkhnOirA+e nIn6elH7ECoPMMWPFO9yrdO6RCpqBreHsr/hTgaZIqpr+lMOTHZnCyCZ93HhQM+FZV+Z He4R5Q+h90/BKAYJImNdt14oQzsWq9BDSkkDqb6KZ8uE8dZoql0/vfp2LELr+MoHWKdl UQ26i5N8lSGzelNcNzsMw7bAyEfpDVu2pRluSch6WHeynKvs2M2DAj2UIoCysCUzKLTF MlRbOoSjm1AygodjAmAl0RuWTxTEUWMWioEG5PCy+SPenWRY9AdOHn+gP6+96Y+PgLV/ FzGw==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b="dWYEAY/Z"; arc=pass (i=1 spf=pass spfdomain=redhat.com dkim=pass dkdomain=redhat.com dmarc=pass fromdomain=redhat.com); spf=pass (google.com: domain of linux-kernel+bounces-142817-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-142817-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [147.75.199.223]) by mx.google.com with ESMTPS id p11-20020a056102274b00b00479c8e0ccf2si540556vsu.586.2024.04.12.07.23.19 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 12 Apr 2024 07:23:19 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-142817-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) client-ip=147.75.199.223; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b="dWYEAY/Z"; arc=pass (i=1 spf=pass spfdomain=redhat.com dkim=pass dkdomain=redhat.com dmarc=pass fromdomain=redhat.com); spf=pass (google.com: domain of linux-kernel+bounces-142817-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-142817-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.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 BC3D71C24245 for ; Fri, 12 Apr 2024 14:23:09 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 05B0C12BF3F; Fri, 12 Apr 2024 14:22:06 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="dWYEAY/Z" Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) (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 8FF4886658 for ; Fri, 12 Apr 2024 14:22:03 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.133.124 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712931725; cv=none; b=pX7miVtKHyYf3I+yQcKGNEHrlbxeSoJtqsGv0Wii4W58h72zeMiBPh2I3+HYTuqY8lU0Nzp0ncxRcDG2a/JQ7AXuoGCULqqaWpEdzTOuug4dwPTodxqnEM1w0PYifGNOkS8yxRmRImcDVRCXgVAM073QLQwqEHc5akJIuDaDMqc= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712931725; c=relaxed/simple; bh=gys6Zge317eX8qBO/ELZYmqSCqhQRVq9GVGChtZKn9Y=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=OeIusGM/34AYlNZNY97pWd4c54wrQTy95F47UPeTs7An8falX7GfjLUTOCuEa0wHhnqnFLCeAnXYqEzV4JcJ0Rqn4ocrf8bH4bZUPw5cEi8l7afnzlcf8MnY68rz6T5uF/hrGomlLDQ8I/vOZnNPzmex2mKQ4z6qNCisbMh1PIw= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=dWYEAY/Z; arc=none smtp.client-ip=170.10.133.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1712931721; 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=fF1JT3Ro4zg1bI3TxDVBJ5avgRwDbgOZB+fig1j1Nw8=; b=dWYEAY/ZDL3sia7HBQHCV5we7RGFUedKzt7fAZZNb8FViVatXoiyq5ztn7KjCYnVdUtawb VG1o47MR24GlZ2Yvxmh7ALgrNSdPWwS2TciYKTTYnfbIbWrFd2HiRM90iuQPRqAdhNCEmH qHkH0lOpGPyRWiuDjUgzKIgW6iq8E+U= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-152-c5-IRZKjOwKMWQetYrG22Q-1; Fri, 12 Apr 2024 10:21:52 -0400 X-MC-Unique: c5-IRZKjOwKMWQetYrG22Q-1 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.rdu2.redhat.com [10.11.54.2]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 41D7D881E63; Fri, 12 Apr 2024 14:21:52 +0000 (UTC) Received: from t14s.redhat.com (unknown [10.39.193.165]) by smtp.corp.redhat.com (Postfix) with ESMTP id D44E440C6DAE; Fri, 12 Apr 2024 14:21:48 +0000 (UTC) From: David Hildenbrand To: linux-kernel@vger.kernel.org Cc: kvm@vger.kernel.org, linux-s390@vger.kernel.org, David Hildenbrand , Heiko Carstens , Vasily Gorbik , Alexander Gordeev , Christian Borntraeger , Sven Schnelle , Janosch Frank , Claudio Imbrenda , Gerald Schaefer , Matthew Wilcox , Thomas Huth Subject: [PATCH v2 02/10] s390/uv: gmap_make_secure() cleanups for further changes Date: Fri, 12 Apr 2024 16:21:12 +0200 Message-ID: <20240412142120.220087-3-david@redhat.com> In-Reply-To: <20240412142120.220087-1-david@redhat.com> References: <20240412142120.220087-1-david@redhat.com> 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-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.2 Let's factor out handling of LRU cache draining and convert the if-else chain to a switch-case. Signed-off-by: David Hildenbrand --- arch/s390/kernel/uv.c | 66 ++++++++++++++++++++++++++----------------- 1 file changed, 40 insertions(+), 26 deletions(-) diff --git a/arch/s390/kernel/uv.c b/arch/s390/kernel/uv.c index 016993e9eb72..25fe28d189df 100644 --- a/arch/s390/kernel/uv.c +++ b/arch/s390/kernel/uv.c @@ -266,6 +266,36 @@ static bool should_export_before_import(struct uv_cb_header *uvcb, struct mm_str return atomic_read(&mm->context.protected_count) > 1; } +/* + * Drain LRU caches: the local one on first invocation and the ones of all + * CPUs on successive invocations. Returns "true" on the first invocation. + */ +static bool drain_lru(bool *drain_lru_called) +{ + /* + * If we have tried a local drain and the folio refcount + * still does not match our expected safe value, try with a + * system wide drain. This is needed if the pagevecs holding + * the page are on a different CPU. + */ + if (*drain_lru_called) { + lru_add_drain_all(); + /* We give up here, don't retry immediately. */ + return false; + } + /* + * We are here if the folio refcount does not match the + * expected safe value. The main culprits are usually + * pagevecs. With lru_add_drain() we drain the pagevecs + * on the local CPU so that hopefully the refcount will + * reach the expected safe value. + */ + lru_add_drain(); + *drain_lru_called = true; + /* The caller should try again immediately */ + return true; +} + /* * Requests the Ultravisor to make a page accessible to a guest. * If it's brought in the first time, it will be cleared. If @@ -275,7 +305,7 @@ static bool should_export_before_import(struct uv_cb_header *uvcb, struct mm_str int gmap_make_secure(struct gmap *gmap, unsigned long gaddr, void *uvcb) { struct vm_area_struct *vma; - bool local_drain = false; + bool drain_lru_called = false; spinlock_t *ptelock; unsigned long uaddr; struct folio *folio; @@ -331,37 +361,21 @@ int gmap_make_secure(struct gmap *gmap, unsigned long gaddr, void *uvcb) out: mmap_read_unlock(gmap->mm); - if (rc == -EAGAIN) { + switch (rc) { + case -EAGAIN: /* * If we are here because the UVC returned busy or partial * completion, this is just a useless check, but it is safe. */ folio_wait_writeback(folio); folio_put(folio); - } else if (rc == -EBUSY) { - /* - * If we have tried a local drain and the folio refcount - * still does not match our expected safe value, try with a - * system wide drain. This is needed if the pagevecs holding - * the page are on a different CPU. - */ - if (local_drain) { - lru_add_drain_all(); - /* We give up here, and let the caller try again */ - return -EAGAIN; - } - /* - * We are here if the folio refcount does not match the - * expected safe value. The main culprits are usually - * pagevecs. With lru_add_drain() we drain the pagevecs - * on the local CPU so that hopefully the refcount will - * reach the expected safe value. - */ - lru_add_drain(); - local_drain = true; - /* And now we try again immediately after draining */ - goto again; - } else if (rc == -ENXIO) { + return -EAGAIN; + case -EBUSY: + /* Additional folio references. */ + if (drain_lru(&drain_lru_called)) + goto again; + return -EAGAIN; + case -ENXIO: if (gmap_fault(gmap, gaddr, FAULT_FLAG_WRITE)) return -EFAULT; return -EAGAIN; -- 2.44.0