From patchwork Wed Sep 4 20:32:00 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Steven Sistare X-Patchwork-Id: 13791414 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 kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id A4726CD4F25 for ; Wed, 4 Sep 2024 20:32:16 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 456D86B00BD; Wed, 4 Sep 2024 16:32:16 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 4072D6B00BE; Wed, 4 Sep 2024 16:32:16 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 2A7E46B01A2; Wed, 4 Sep 2024 16:32:16 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id 0284B6B00BD for ; Wed, 4 Sep 2024 16:32:15 -0400 (EDT) Received: from smtpin06.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id B08ECAAF3A for ; Wed, 4 Sep 2024 20:32:15 +0000 (UTC) X-FDA: 82528203030.06.2676C91 Received: from mx0b-00069f02.pphosted.com (mx0b-00069f02.pphosted.com [205.220.177.32]) by imf15.hostedemail.com (Postfix) with ESMTP id D771FA0024 for ; Wed, 4 Sep 2024 20:32:13 +0000 (UTC) Authentication-Results: imf15.hostedemail.com; dkim=pass header.d=oracle.com header.s=corp-2023-11-20 header.b="F6KKv/xb"; spf=pass (imf15.hostedemail.com: domain of steven.sistare@oracle.com designates 205.220.177.32 as permitted sender) smtp.mailfrom=steven.sistare@oracle.com; dmarc=pass (policy=reject) header.from=oracle.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1725481837; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:content-type: content-transfer-encoding:in-reply-to:references:dkim-signature; bh=STUnVMN8Xwo76HzymE+3sv/692MsFkzr6oskAvnAWS0=; b=8lwVVpAHoXwygGlYIfC7MjmjKKe/bEsjmWGXA6Et7KqtDc68V2HYO4tPlPeoP2RWDAem5S EL3AAN6XJ+1SpcCeGU7CEmghUE18isLPb3meTXzgSguHEpG+7KZR40SJgN74uuOccHNk2V xTmn/deSZ/vAxRtBKTV4VEOOieqxML0= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1725481837; a=rsa-sha256; cv=none; b=krXCZzAb+cypCLGkmf4QF30UlttrM4+nA3RLkVZtGYjYyOiH3qvjBvB7QpxX+C19r8Q/IR fFWsh6Au0A8IxFTEJf8Mm6LtHm4IaEcLpr8ZX3VTNXaUTYpr1QYrwShTB1OPY1uGMjDnUk cB3eq2SSGHIovEERG0Hm3G09Wt9scVY= ARC-Authentication-Results: i=1; imf15.hostedemail.com; dkim=pass header.d=oracle.com header.s=corp-2023-11-20 header.b="F6KKv/xb"; spf=pass (imf15.hostedemail.com: domain of steven.sistare@oracle.com designates 205.220.177.32 as permitted sender) smtp.mailfrom=steven.sistare@oracle.com; dmarc=pass (policy=reject) header.from=oracle.com Received: from pps.filterd (m0333520.ppops.net [127.0.0.1]) by mx0b-00069f02.pphosted.com (8.18.1.2/8.18.1.2) with ESMTP id 484I0U2k002869; Wed, 4 Sep 2024 20:32:09 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.com; h= from:to:cc:subject:date:message-id; s=corp-2023-11-20; bh=STUnVM N8Xwo76HzymE+3sv/692MsFkzr6oskAvnAWS0=; b=F6KKv/xbHGe85UnOAirQkX nAbr17m0iJMhjO2C5q9baSeXugy1qXPzc1mPB/fJ813gWPVfLBdDeSB7FMGCJXRV CCY3Zq+XOp4tU/6QwyJ9R11Aqw2V1sxD3Asj0qiiseVtVnwBz+gy0SkKZmFa+Jq6 kBBj002y6gF0Gk/DSboT5/xzwmEzM9el+yAf8NTqGhXMSbXEYiSS5vl+87rf97pO iGh1PZbNpYKc2x8n38fC0IU7nj7g+BjARjAt4Y6rORlbCSHHq3zL9nHyHzJZhAiY TMw5Wq8Y2dJhW1o9y0yDqDMqNvowYqz8NffNrGGyiO+o1jEVugx2VN+STEut+aYQ == Received: from iadpaimrmta01.imrmtpd1.prodappiadaev1.oraclevcn.com (iadpaimrmta01.appoci.oracle.com [130.35.100.223]) by mx0b-00069f02.pphosted.com (PPS) with ESMTPS id 41dk84n74g-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Wed, 04 Sep 2024 20:32:08 +0000 (GMT) Received: from pps.filterd (iadpaimrmta01.imrmtpd1.prodappiadaev1.oraclevcn.com [127.0.0.1]) by iadpaimrmta01.imrmtpd1.prodappiadaev1.oraclevcn.com (8.18.1.2/8.18.1.2) with ESMTP id 484JFn2t018356; Wed, 4 Sep 2024 20:32:07 GMT Received: from pps.reinject (localhost [127.0.0.1]) by iadpaimrmta01.imrmtpd1.prodappiadaev1.oraclevcn.com (PPS) with ESMTPS id 41bsmgmgjn-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Wed, 04 Sep 2024 20:32:07 +0000 Received: from iadpaimrmta01.imrmtpd1.prodappiadaev1.oraclevcn.com (iadpaimrmta01.imrmtpd1.prodappiadaev1.oraclevcn.com [127.0.0.1]) by pps.reinject (8.17.1.5/8.17.1.5) with ESMTP id 484KW7tV030406; Wed, 4 Sep 2024 20:32:07 GMT Received: from ca-dev63.us.oracle.com (ca-dev63.us.oracle.com [10.211.8.221]) by iadpaimrmta01.imrmtpd1.prodappiadaev1.oraclevcn.com (PPS) with ESMTP id 41bsmgmgg0-1; Wed, 04 Sep 2024 20:32:07 +0000 From: Steve Sistare To: linux-mm@kvack.org Cc: Vivek Kasireddy , Muchun Song , Andrew Morton , Matthew Wilcox , Peter Xu , David Hildenbrand , Jason Gunthorpe , Steve Sistare Subject: [PATCH V2] mm/hugetlb: simplify refs in memfd_alloc_folio Date: Wed, 4 Sep 2024 13:32:00 -0700 Message-Id: <1725481920-82506-1-git-send-email-steven.sistare@oracle.com> X-Mailer: git-send-email 1.8.3.1 X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.293,Aquarius:18.0.1039,Hydra:6.0.680,FMLib:17.12.60.29 definitions=2024-09-04_18,2024-09-04_01,2024-09-02_01 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 spamscore=0 bulkscore=0 mlxscore=0 mlxlogscore=999 suspectscore=0 malwarescore=0 adultscore=0 phishscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2407110000 definitions=main-2409040155 X-Proofpoint-GUID: QtN0letfpeOBepQzlv4cJeSxWB322Vmi X-Proofpoint-ORIG-GUID: QtN0letfpeOBepQzlv4cJeSxWB322Vmi X-Rspam-User: X-Rspamd-Server: rspam04 X-Rspamd-Queue-Id: D771FA0024 X-Stat-Signature: q449k94rxnmibghzmrh9ettbm1e748sw X-HE-Tag: 1725481933-725432 X-HE-Meta: U2FsdGVkX19i4tWmm3E8WM/v48VwttxSz7L7KqOi6TXaGy1ZNToluNqanEei0HX8PFIDEqn7Ck8DsPuxgK9VEWAAOi9ro9rvbIWbUsWIVMBgDZy0U5+KxD1cimqcVpoBE0vUy6nprwbLVG31SZwT7LcZ068/BLk1vTgo4Blxv7COXoLQqIVqgqK97StwaJCNol5Zybap/UnLtTzU5HBeV99bELChRsiDpNRyppCV9dgpP4LePrRNLqY1m0PhYn+KfhQaH030lh5dX/AJGyClUpLt00qFe0tCX/pUlTf0EoKJJPZ/XuoKBmO3C6dF+yPAvSSJYNlUop6ibnSwnNH1d1dc+Az8Hv/Np1uh9svQsBf9b0Ia3Cbri/TIKegdX+A/6X/lJ+sf79hiXzQg9RrRbNUzu0T/GsW7D6knqgzKT5mO4sliiAMf82lEm7tZyskFvzxmJVQjS8M9zr+WgDZzhRln1Qv1npWVZ79xtVZFjBJk0StvIlJmGGZpkmi+X4WmPodDVhFVBUmMV60hqmcwBQC9xofrQlw5eJMrXec3DYylFwIlAwnzwWYlqRbbKCZShthE/aF/C7DwB4mt6c/DUzC22fDnhwqNLUkXh5GFhnmoUS+ZY3jEnkiTsvT8SLG9zRf4fGiEfTvmjLoDbnadGxy4Z6vuqtxZT3Cqw2eet2yzYuinqiGzX65MAC4jGUijWbZQolTe6iRCY6ukxc0ayqKkUA+Izsx+nRwEEQQ8GiGwn5xE0+1dELgOJUz2edOQ/vmqXd03WVwuRhipehXRFTn3qKDlQhISSnxyb61wZWTEjQ6GC4JRpUE5PKjybQueasKldCO/KWgR9kKGSw7TXKjKHzEla3Ld7gLjyOq9QXwRhEVarprTHyU8Cagn2NrsmC2jm26IJJ33vm3nU3BMoP0aWP4e9J8uA/EezwK2Z+zcchMG1EySlWmIb0bHURthZ2giiy8j3bDSO/0b83z y4V4rGwk JRXDhlcD2ILLlLzLcBrThpWX/rVACsF6b3hBsEUujiJMkt9Oj5UDp/gCq5xIw1fR5GrxBgGFfrIolrI7ik0Q1nGLDifqSjeTtmnJQPE3/W6Wx2Oo2ajcJVQK07hAHoh6dljolH3pdHOY7pUvZRPSQX+j0jOcEYJY7w34uNTei6Y4q3REOjvpgMgepkwpJq+Sgey7iT5C7XBDZ+pj6xbgD2I/lcwgAJhc0VTcNLhvdu4oJEb4dEDTC4/b/IRBTwLNFHZXyeFen4b7zBgeBuyVaT3Emkme10TrUEC4cnmRZrna07ersrA/BiZh6r/PWk2FT0LBq4Ih8OyCZ2N0= X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: The folio_try_get in memfd_alloc_folio is not necessary. Delete it, and delete the matching folio_put in memfd_pin_folios. This also avoids leaking a ref if the memfd_alloc_folio call to hugetlb_add_to_page_cache fails. That error path is also broken in a second way -- when its folio_put causes the ref to become 0, it will implicitly call free_huge_folio, but then the path *explicitly* calls free_huge_folio. Delete the latter. This is a continuation of the fix "mm/hugetlb: fix memfd_pin_folios free_huge_pages leak" Fixes: 89c1905d9c14 ("mm/gup: introduce memfd_pin_folios() for pinning memfd folios") Suggested-by: Vivek Kasireddy Suggested-by: Matthew Wilcox Signed-off-by: Steve Sistare --- mm/gup.c | 4 +--- mm/memfd.c | 3 +-- 2 files changed, 2 insertions(+), 5 deletions(-) diff --git a/mm/gup.c b/mm/gup.c index bccabaa..947881ff 100644 --- a/mm/gup.c +++ b/mm/gup.c @@ -3618,7 +3618,7 @@ long memfd_pin_folios(struct file *memfd, loff_t start, loff_t end, pgoff_t start_idx, end_idx, next_idx; struct folio *folio = NULL; struct folio_batch fbatch; - struct hstate *h = NULL; + struct hstate *h; long ret = -EINVAL; if (start < 0 || start > end || !max_folios) @@ -3662,8 +3662,6 @@ long memfd_pin_folios(struct file *memfd, loff_t start, loff_t end, &fbatch); if (folio) { folio_put(folio); - if (h) - folio_put(folio); folio = NULL; } diff --git a/mm/memfd.c b/mm/memfd.c index bcb131d..c17c3ea 100644 --- a/mm/memfd.c +++ b/mm/memfd.c @@ -89,13 +89,12 @@ struct folio *memfd_alloc_folio(struct file *memfd, pgoff_t idx) numa_node_id(), NULL, gfp_mask); - if (folio && folio_try_get(folio)) { + if (folio) { err = hugetlb_add_to_page_cache(folio, memfd->f_mapping, idx); if (err) { folio_put(folio); - free_huge_folio(folio); return ERR_PTR(err); } folio_unlock(folio);