Received: from mx0a-001b2d01.pphosted.com (mx0a-001b2d01.pphosted.com [148.163.156.1]) (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 CC0F8442FA7 for ; Mon, 10 Aug 2026 19:51:37 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=148.163.156.1 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1786391499; cv=none; b=NDrwuIcZc17klDBZvUA/hixEosNpFnSS4O97m6sRojb7hlGrVkFCseM7DtUib5LZ1mVzgFXc9B40UFMoiJeEfDyoZjjlnJs6fl52jkZaZCEA5kCqiLqWP36ywFqRnmChVu7R2GJJ/6IHRI5MWVzDowgYPodHRxqNzUgJlHSsuWk= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1786391499; c=relaxed/simple; bh=6mlbR9MMcvdcKcFC7Z4kJEBnktJkApKANM25LSGyrxI=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=uA2yXSwZPVmy866hHBhFkNlQE5memzgMucBjWmKl9zNqasqmqWc2he0A1C5J68oviKCTtXiSoGJnO4VYPY9KDU+DtDSlS/k8St+NCAlfEacGv2+bxW1itGVbxctYEhl7Ue0MGDD7EtRG9VmTXmxSvRVhwJbm95ie40p3506mGS4= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.ibm.com; spf=pass smtp.mailfrom=linux.ibm.com; dkim=pass (2048-bit key) header.d=ibm.com header.i=@ibm.com header.b=dVwa/7hs; arc=none smtp.client-ip=148.163.156.1 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.ibm.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.ibm.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=ibm.com header.i=@ibm.com header.b="dVwa/7hs" Received: from pps.filterd (m0356517.ppops.net [127.0.0.1]) by mx0a-001b2d01.pphosted.com (8.18.1.11/8.18.1.11) with ESMTP id 67AJ2GZx2476676; Mon, 10 Aug 2026 19:51:35 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ibm.com; h=cc :content-transfer-encoding:date:from:in-reply-to:message-id :mime-version:references:subject:to; s=pp1; bh=nlhZvBkJo9Ij2ltNQ 9WJa3fzDPffy5j9bU5VjokU6vc=; b=dVwa/7hszRgF93mIxulbULOnrTldDhUSn 0lf0WHmlocZsvlCI93hxfhvO8eGA2+QFSIgFcfs9YuPcVUbenI5/rasr2QElAR62 RdW1ncm37PguEarER2cXzWLi4aWgXgG8TVpIxiwJ9x6KPcvoc75ZJNINt8MYj1s2 nb7NkO+YVlWS/5YLBKkUfZJj5+iINIf9ptb/dQVb9A/ArJDRWaoGJqW/rasS4ket f8A1RjvSWz2C9/jPe2W//PHuD+0YtEQKnHxD6dZjsnGRkkaC5i2u9C85TB02y2+K tOcjtdgbPBzQ0wHiTExSMcUcLEeJyNF/WKFZsVHZRblycoAbakInQ== Received: from ppma11.dal12v.mail.ibm.com (db.9e.1632.ip4.static.sl-reverse.com [50.22.158.219]) by mx0a-001b2d01.pphosted.com (PPS) with ESMTPS id 4fwvm9huqk-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Mon, 10 Aug 2026 19:51:34 +0000 (GMT) Received: from pps.filterd (ppma11.dal12v.mail.ibm.com [127.0.0.1]) by ppma11.dal12v.mail.ibm.com (8.18.1.7/8.18.1.7) with ESMTP id 67AJfIJG003612; Mon, 10 Aug 2026 19:51:34 GMT Received: from smtprelay04.dal12v.mail.ibm.com ([172.16.1.6]) by ppma11.dal12v.mail.ibm.com (PPS) with ESMTPS id 4fxhfxx09e-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Mon, 10 Aug 2026 19:51:34 +0000 (GMT) Received: from smtpav04.wdc07v.mail.ibm.com (smtpav04.wdc07v.mail.ibm.com [10.39.53.231]) by smtprelay04.dal12v.mail.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id 67AJpWmh48890228 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Mon, 10 Aug 2026 19:51:33 GMT Received: from smtpav04.wdc07v.mail.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id DACBA58052; Mon, 10 Aug 2026 19:51:32 +0000 (GMT) Received: from smtpav04.wdc07v.mail.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 1C9FC58050; Mon, 10 Aug 2026 19:51:32 +0000 (GMT) Received: from IBM-D32RQW3.ibm.com (unknown [9.61.246.243]) by smtpav04.wdc07v.mail.ibm.com (Postfix) with ESMTP; Mon, 10 Aug 2026 19:51:31 +0000 (GMT) From: Farhan Ali To: stable@vger.kernel.org, gregkh@linuxfoundation.org, sashal@kernel.org Cc: alifm@linux.ibm.com, Christian Borntraeger , Matthew Rosato Subject: [PATCH v1 3/7] KVM: s390: pci: Fix memory accounting for pinned/unpinned pages Date: Mon, 10 Aug 2026 12:51:24 -0700 Message-ID: <20260810195128.7861-4-alifm@linux.ibm.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20260810195128.7861-1-alifm@linux.ibm.com> References: <20260810195128.7861-1-alifm@linux.ibm.com> Precedence: bulk X-Mailing-List: stable@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-TM-AS-GCONF: 00 X-Proofpoint-Spam-Details-Enc: AW1haW4tMjYwODEwMDE2NiBTYWx0ZWRfX+kOJzgbzqH87 rX4mOxQMK01hKopng9xnSbuJw6LmSfb88uq8uZFPjbVfUCihYdMfvK35AOlHl8ZNtGfkxnxFAlp xbF313AxXi6PW/665XtTzyg8416nwOF9IIjcUF4b80GusxwPimaaB97CEqkxrygxfG1EstW2eXz sqwk4OfiMqDw3rJQXP8l8RMx19wgTthcrjhrShez1oLcn6hUbA0jrsxYwGXQ3QsgJko4dJ39Vf3 /i9VoZjFp18m8Y2rxRxJowT/+/euY7A6kfknA0MtNweCDQk1SjHZMI1rELAbftCHERkQLGcewmx CAwOZFB8cpqKpePcuw7sRZrK49vmrI/NgKn+3OVPPd2eWfe2MufC0B2WCWUIca8a9F7h/P5+65V MDKmgD6uuIaxXJc0kNcDJ/1TmiJCpHBu2mYktpBILM+OZ+Vq3jrc1mfhSkCHztnQpfKkPcLZPrM QnyoieB1lCXWh3bNeaw== X-Proofpoint-ORIG-GUID: hyYPP9DN_4C1nsCcMYHir7YgnjXGHsFZ X-Proofpoint-Spam-Info: AW1haW4tMjYwODEwMDE2NiBTYWx0ZWRfX42ZH+yXPDFqF rzRIEG3mHbSeKrP+tHIcpgBzpdOgpH0J0mLfgUtPwJiG52oAr4IVi0hpQWEbLJ3ombOPeNm4rs1 ozZOmZGS9m0PgE+uPr+aQjGmTknhBF8= X-Authority-Analysis: v=2.4 cv=IfK3n2qa c=1 sm=1 tr=0 ts=6a7a2bc7 cx=c_pps a=aDMHemPKRhS1OARIsFnwRA==:117 a=aDMHemPKRhS1OARIsFnwRA==:17 a=Sv0fKeRqtYgA:10 a=VkNPw1HP01LnGYTKEx00:22 a=RnoormkPH1_aCDwRdu11:22 a=U7nrCbtTmkRpXpFmAIza:22 a=VwQbUJbxAAAA:8 a=VnNF1IyMAAAA:8 a=axipAET6x17LYUbcMfoA:9 X-Proofpoint-GUID: hyYPP9DN_4C1nsCcMYHir7YgnjXGHsFZ X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.293,Aquarius:18.0.1176,Hydra:6.1.134,FMLib:17.12.100.49 definitions=2026-08-10_05,2026-08-10_02,2025-10-01_01 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 phishscore=0 priorityscore=1501 impostorscore=0 bulkscore=0 clxscore=1015 lowpriorityscore=0 adultscore=0 malwarescore=0 spamscore=0 suspectscore=0 classifier=typeunknown authscore=0 authtc= authcc= route=outbound adjust=0 reason=mlx scancount=1 engine=8.22.0-2606150000 definitions=main-2608100166 The account_mem() and unaccount_mem() functions call get_uid() which increments the reference count of struct user_struct on every invocation. But we don't decrement the count by calling free_uid(). It also accounted/unaccounted the pages against the current->mm. But its possible the unaccount_mem() can be called from a different process context than the one that originally pinned the pages. Let's fix this by storing the pinning process user_struct and mm_struct when accounting for pinned pages, and subsequently free these resources when the pages are unpinned. Fixes: 3c5a1b6f0a18 ("KVM: s390: pci: provide routines for enabling/disabling interrupt forwarding") Cc: stable@vger.kernel.org Reviewed-by: Christian Borntraeger Reviewed-by: Matthew Rosato Signed-off-by: Farhan Ali Tested-by: Matthew Rosato [borntraeger@linux.ibm.com: Fixed whitespace] Signed-off-by: Christian Borntraeger (cherry picked from commit 36f6999ecde3976731a8bfc0b8e667da6f593069) [alifm@linux.ibm.com: Resolved merge conflict] Signed-off-by: Farhan Ali --- arch/s390/kvm/pci.c | 48 ++++++++++++++++++++++++++++++++------------- arch/s390/kvm/pci.h | 2 ++ 2 files changed, 36 insertions(+), 14 deletions(-) diff --git a/arch/s390/kvm/pci.c b/arch/s390/kvm/pci.c index e97f5a5af113..0ba9bbbac45d 100644 --- a/arch/s390/kvm/pci.c +++ b/arch/s390/kvm/pci.c @@ -191,34 +191,54 @@ static int kvm_zpci_clear_airq(struct zpci_dev *zdev) return cc ? -EIO : 0; } -static inline void unaccount_mem(unsigned long nr_pages) +static inline void unaccount_mem(struct kvm_zdev *kzdev, unsigned long nr_pages) { - struct user_struct *user = get_uid(current_user()); + struct user_struct *user = kzdev->user_account; + struct mm_struct *mm_account = kzdev->mm_account; - if (user) + if (user) { atomic_long_sub(nr_pages, &user->locked_vm); - if (current->mm) - atomic64_sub(nr_pages, ¤t->mm->pinned_vm); + free_uid(user); + kzdev->user_account = NULL; + } + + if (mm_account) { + atomic64_sub(nr_pages, &mm_account->pinned_vm); + mmdrop(mm_account); + kzdev->mm_account = NULL; + } } -static inline int account_mem(unsigned long nr_pages) +static inline int account_mem(struct kvm_zdev *kzdev, unsigned long nr_pages) { struct user_struct *user = get_uid(current_user()); unsigned long page_limit, cur_pages, new_pages; + int rc = 0; page_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT; + cur_pages = atomic_long_read(&user->locked_vm); do { - cur_pages = atomic_long_read(&user->locked_vm); new_pages = cur_pages + nr_pages; - if (new_pages > page_limit) - return -ENOMEM; - } while (atomic_long_cmpxchg(&user->locked_vm, cur_pages, - new_pages) != cur_pages); + if (new_pages > page_limit) { + rc = -ENOMEM; + goto out; + } + } while (!atomic_long_try_cmpxchg(&user->locked_vm, &cur_pages, new_pages)); + + if (current->mm) { + mmgrab(current->mm); + atomic64_add(nr_pages, ¤t->mm->pinned_vm); + } - atomic64_add(nr_pages, ¤t->mm->pinned_vm); + kzdev->user_account = user; + kzdev->mm_account = current->mm; return 0; + +out: + free_uid(user); + return rc; } static int kvm_s390_pci_aif_enable(struct zpci_dev *zdev, struct zpci_fib *fib, @@ -281,7 +301,7 @@ static int kvm_s390_pci_aif_enable(struct zpci_dev *zdev, struct zpci_fib *fib, } /* Account for pinned pages, roll back on failure */ - if (account_mem(pcount)) + if (account_mem(zdev->kzdev, pcount)) goto unpin2; /* AISB must be allocated before we can fill in GAITE */ @@ -402,7 +422,7 @@ static int kvm_s390_pci_aif_disable(struct zpci_dev *zdev, bool force) pcount++; } if (pcount > 0) - unaccount_mem(pcount); + unaccount_mem(kzdev, pcount); out: mutex_unlock(&aift->aift_lock); diff --git a/arch/s390/kvm/pci.h b/arch/s390/kvm/pci.h index 486d06ef563f..6beea19cea45 100644 --- a/arch/s390/kvm/pci.h +++ b/arch/s390/kvm/pci.h @@ -22,6 +22,8 @@ struct kvm_zdev { struct kvm *kvm; struct zpci_fib fib; struct list_head entry; + struct user_struct *user_account; + struct mm_struct *mm_account; }; struct zpci_gaite { -- 2.43.0