From patchwork Tue May 2 23:56:02 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ackerley Tng X-Patchwork-Id: 89525 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp980051vqo; Tue, 2 May 2023 17:17:25 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ4vupow+6b0KxwwQNtzB8pcMbJN8fyUhVrkdVSfgUgDOsOyqKg6PkIBz8LjgtoiExAErXfv X-Received: by 2002:a17:90b:315:b0:24d:f597:346c with SMTP id ay21-20020a17090b031500b0024df597346cmr10284098pjb.6.1683073045293; Tue, 02 May 2023 17:17:25 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1683073045; cv=none; d=google.com; s=arc-20160816; b=ADGZ/s5yYMtH93Sj0VwiBWtiXWZqWf690l4j4Bg1mTbqvZURlOHSrMx8YiURvnI5/u IgcIEappT/tw5HvbvxHvI9C3XP5g0hT6d6Dsy93RuLrL8BXLpm98dpk7ubFzoX8S8Qrd 6RwbABmFwhJ0Gg/R49KPVhu3RGdZYAjgFxHGFYNZLcfZSI3f/F+czf21/wa3F1M4i7+z nm8myYovSLmKm/EqWUHo1mzRQWTKx5n4zwVU774YYiKp1cJBq2g9yU2jc4iksapJfSSb zgIWDGsX8hIl61HquV42els/5oxjJ3vIz/+fbIteUT0dqz7T2J81ORB5sIt01+vfnYAb I2kA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:from:subject:message-id:references :mime-version:in-reply-to:date:dkim-signature; bh=TIxhAVAgYS5mapIdI8ZTL33+hofUrDA3Fj2JnyUnXTo=; b=qlaBRsWx8YezhUZA/TtF0N2ODbGdPQM/2S7C7oDnPt/HXNa9+nqh1gEPvUShzomJJ/ PjKBAr6NVfaGNZxsnJsfD4TNpz28gvSWRn1ApkkVqavN81W/ubGvR0HhG2fu7XP2bjqn ywVjfI4KlbfetwVA89ypgTKmeba+Pj+suZht3c8UehjlNqUGDZEMBTvcO+mHzL9We6HF wwpIK4dWJwCe0+10DyzpVMOAC4We1wxPs42vodflubIt4217xWtupUOonl26mHBhxvMA cv6PiHuw2xe35IlcysJhuiH1gb4g60Xa7jfxU8KRyMEWJEYg1pes/l4as28TKQWyUGmg jPdQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20221208 header.b=dNZZa0Le; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id p18-20020a17090ad31200b00246b1a9630csi228904pju.130.2023.05.02.17.17.09; Tue, 02 May 2023 17:17:25 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20221208 header.b=dNZZa0Le; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229806AbjEBX4N (ORCPT + 99 others); Tue, 2 May 2023 19:56:13 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41222 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229576AbjEBX4J (ORCPT ); Tue, 2 May 2023 19:56:09 -0400 Received: from mail-yw1-x1149.google.com (mail-yw1-x1149.google.com [IPv6:2607:f8b0:4864:20::1149]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2FDD330CD for ; Tue, 2 May 2023 16:56:08 -0700 (PDT) Received: by mail-yw1-x1149.google.com with SMTP id 00721157ae682-55a9614c154so25505037b3.2 for ; Tue, 02 May 2023 16:56:08 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1683071767; x=1685663767; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=TIxhAVAgYS5mapIdI8ZTL33+hofUrDA3Fj2JnyUnXTo=; b=dNZZa0LeBYDZrFKtDX5qs0Huzpekye+DGENcdzJGQDDEmBIXmSv36HWyWunH74mYvv IYPkV9Yol53BLGWLbst9q5taC4QG822qYbdKdjNOZLs5p2TJmEPhbjh66IcJTvBUA5SP kUPBtZngsyGsJXYWzbuqy/qAcQIPsfW6qYuE6xxKyYqL6ThrIh2fNwHEeRKouUsYon2A mRtSyLBzArQDmBie63b74T6C2w2V0kJcF5RAHMQ5ytpNzPR7bzzivdKenxaQveRH8kfk 76sucra8VLmAZHJqkCPmWEHri4ZaUVqTJhTvZT50Xvoa8ThsdZG2UvcJP9ETia91jzUg aWFQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1683071767; x=1685663767; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=TIxhAVAgYS5mapIdI8ZTL33+hofUrDA3Fj2JnyUnXTo=; b=CLKgGkztkhYoWe2r/n7WDU6UoCGEfO/YmCbk54ewaABNu/V1N48INOcgCBYiqy4dfz STx6DGKTVALXjn3pWfNJlOOcJ7NVuVic4NROPrHYq2cvnvxl+aIZNwgh038PVupX9K5F QnU1L1FjLdhfxDNFe5p6a9NpZ9kv0AsX7TPgdvoesX4wt5zMQflvDXKuZmSmMlEXBRup MQTxy5ome1W5Nhv77OUV1Bbqtw2dW0NQ0fJjia+Zqvum2hdE0UQLpVLGQYF/r1VCyfzv MlkcKrp/m3CMNmCHC0d52BlT3JODV4sXhoMro1q2TdFXyiPlzu6QfRrdyYgfzmnofOlM 0Wig== X-Gm-Message-State: AC+VfDzbD9JtMKoukm3+Viev5ae8S1bT1cshpHc8VT53rp3xtNlVOKC2 yUh533HJr2+k85GkOFXlibHJDC2JnYPqC5Zwsg== X-Received: from ackerleytng-ctop.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:13f8]) (user=ackerleytng job=sendgmr) by 2002:a81:7653:0:b0:54f:a60c:12eb with SMTP id j19-20020a817653000000b0054fa60c12ebmr10693783ywk.1.1683071767464; Tue, 02 May 2023 16:56:07 -0700 (PDT) Date: Tue, 2 May 2023 23:56:02 +0000 In-Reply-To: Mime-Version: 1.0 References: X-Mailer: git-send-email 2.40.1.495.gc816e09b53d-goog Message-ID: <98624c2f481966492b4eb8272aef747790229b73.1683069252.git.ackerleytng@google.com> Subject: [PATCH 1/2] mm: filemap: Add filemap_has_folio function From: Ackerley Tng To: akpm@linux-foundation.org, linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org, mike.kravetz@oracle.com, muchun.song@linux.dev, willy@infradead.org, sidhartha.kumar@oracle.com, jhubbard@nvidia.com Cc: vannapurve@google.com, erdemaktas@google.com, Ackerley Tng X-Spam-Status: No, score=-9.6 required=5.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE,USER_IN_DEF_DKIM_WL autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1764830001650426889?= X-GMAIL-MSGID: =?utf-8?q?1764830001650426889?= filemap_has_folio() will return whether there is a folio at a given index in a mapping. This function does not affect the folio refcount. Signed-off-by: Ackerley Tng --- include/linux/pagemap.h | 1 + mm/filemap.c | 17 +++++++++++++++++ 2 files changed, 18 insertions(+) diff --git a/include/linux/pagemap.h b/include/linux/pagemap.h index a56308a9d1a4..e49f07cdbff7 100644 --- a/include/linux/pagemap.h +++ b/include/linux/pagemap.h @@ -508,6 +508,7 @@ pgoff_t page_cache_prev_miss(struct address_space *mapping, #define FGP_WRITEBEGIN (FGP_LOCK | FGP_WRITE | FGP_CREAT | FGP_STABLE) +bool filemap_has_folio(struct address_space *mapping, pgoff_t index); void *filemap_get_entry(struct address_space *mapping, pgoff_t index); struct folio *__filemap_get_folio(struct address_space *mapping, pgoff_t index, int fgp_flags, gfp_t gfp); diff --git a/mm/filemap.c b/mm/filemap.c index a34abfe8c654..a7a6e229e33d 100644 --- a/mm/filemap.c +++ b/mm/filemap.c @@ -1835,6 +1835,23 @@ EXPORT_SYMBOL(page_cache_prev_miss); * folio_put(). */ +/** + * filemap_has_folio - Check if filemap has a folio at given index + * @mapping: The address_space to search. + * @index: The page index. + * + * Unlike filemap_get_entry, this does not increment refcount of the folio. + * + * Return: true if folio exists else false. + */ +bool filemap_has_folio(struct address_space *mapping, pgoff_t index) +{ + void *entry = xa_load(&mapping->i_pages, index); + + return entry && !xa_is_value(entry); +} +EXPORT_SYMBOL(filemap_has_folio); + /* * filemap_get_entry - Get a page cache entry. * @mapping: the address_space to search From patchwork Tue May 2 23:56:03 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ackerley Tng X-Patchwork-Id: 89524 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp978259vqo; Tue, 2 May 2023 17:13:31 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ73K1Y1pvLO26hpgr8La38YapQb/P5Pnsv5V5QrXNi0K4xZDfS4xF2cjmM8EJhtaEpzlcrF X-Received: by 2002:a17:902:ba94:b0:1a5:1109:f58e with SMTP id k20-20020a170902ba9400b001a51109f58emr63863pls.29.1683072811207; Tue, 02 May 2023 17:13:31 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1683072811; cv=none; d=google.com; s=arc-20160816; b=s3IUBhObD0x2wjC275e37TEuI/E4C2PbruKZc0dPsLWIIT92/a2jFYmkDVw9t2bAzu YanjtNvKiB/ZV9DUnGqaX+bhXAUzsPjRvauJl5yInX6pIu6FsKPDaE9SXH3TqHEBRR7g Jue89eVd+aIdslpwVcl6QIlRJ74nO0aVydH+Mro7H/pxCsis9DW5pf/R5pLZN+WOXsR+ kxm5lgMzWFhDN5Txh3JIz6SD0mE5u7IYZ6xALt5g31Eroah2PybxlIoyb6DdVWRxxiwQ BzND72v0ynFfMfsZbM69jZi7RsgpdBqN5iiI3+443JjyJUP8n5mEwQyrkdp/oIH2k+0Q i5Bw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:from:subject:message-id:references :mime-version:in-reply-to:date:dkim-signature; bh=JdKcEQmaaWLxe2HiTbkU80Vf3btOicSKEWhjrrEdCro=; b=qtfC1gZhLc6PjwDXyMYQ9luuUEu29FIEUMuDOaVe5F7KM8rJnrRgb6aL0AtiQx1zEQ dm6aV+Ac66oNwp4HWeBdfZRp/ppKDc3tDxBEWxYWeyFte0YOjYRue8TjGIW8VEBam/2C utnUskWWfAxnBwEnOCOKeli+NPWTgXmAKRr6oK3tRgaAM1fu9NNk0oCzWYo2B7SaYU0E m4fxZbbf9glLoFTx0wddNlaMr+Rehe6bpXgiU9nZmYD5VOFHmKl3LyBhtDQP/PJXAiDm /ORv2mIcIcMsAm70hmoUTyOX7oKSejbty4kRE7sdrbSlIix41reE30QYdmXIo9787kWa +/ZQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20221208 header.b=SyzGjzNi; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id j7-20020a170902da8700b001a686b1dc64si34333363plx.412.2023.05.02.17.13.09; Tue, 02 May 2023 17:13:31 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20221208 header.b=SyzGjzNi; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230072AbjEBX4R (ORCPT + 99 others); Tue, 2 May 2023 19:56:17 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41236 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229793AbjEBX4K (ORCPT ); Tue, 2 May 2023 19:56:10 -0400 Received: from mail-pf1-x449.google.com (mail-pf1-x449.google.com [IPv6:2607:f8b0:4864:20::449]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A0D4A2680 for ; Tue, 2 May 2023 16:56:09 -0700 (PDT) Received: by mail-pf1-x449.google.com with SMTP id d2e1a72fcca58-63b60018e71so2629559b3a.0 for ; Tue, 02 May 2023 16:56:09 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1683071769; x=1685663769; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=JdKcEQmaaWLxe2HiTbkU80Vf3btOicSKEWhjrrEdCro=; b=SyzGjzNi7EHG5k8wDd+xH8eat2wyOdXHzClWS8H2God9fXzYy212Ey1RaqjNMwN4Oc vVHPKzhXrxRkWS4Kujnd8ypMX5ysqjrCg5HHCG11NHP4r/2QAaTYWXTbzHJ2xUwpwIsK /+AJr9RGPEuBMB1QRpTr7nsgF2phaCsxeZl1+5ocMSHMtT/T6eIsVMZPqOjAR8Gtkvdg UAntmTtInu0WrqCro+B2cLss2JHJMmSoG8EMdw2CvO7O4yKKzmM0qjZM1Hhn5m55oI/W ugGV+tk1LHPfdCp5eaUjuXQivIGZXMdJzipjaOXDytwjViO2PMbdYNti4ea5xxYc/rsL U3Iw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1683071769; x=1685663769; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=JdKcEQmaaWLxe2HiTbkU80Vf3btOicSKEWhjrrEdCro=; b=fA4/8EgpOUqIhBK95j+gwpKQqv0wS9jIY9Zmkp3N51W2qnfpaQbJE0jQRE/dq4Znpo 30E53mjwrPceu4nObrNMgAA1XbfNAWJhGpK78pRFVk5RnX3s5OJCRNnYFOIRDeXobdMS ND0FGOq1PsA7J/7fxXY+RQxLNKf7eulD/vo1mm6A3ONe3s5bfOp52dCFk/rGuDfHbaq5 TpqyqG+DOAvYbhU9+VXY37VeOcJQS1/5NNnFz+pI2MnK1oRDcYAuAiYVQV/nmA83prLh vIiiChDS3xW1NaTzpSB/mqvFuEzesEIwFvAaFmXIKddml5X82HV68LbUj/0OoDjqPcof d6Sg== X-Gm-Message-State: AC+VfDw2S3fzoYwC9vH7hs03+GF/AQMadWz5By1xNMYNSIBXwvgDPey6 gFwNwT8nSrwyerC7MdQbbiiOZkIaNJL8AGAzTg== X-Received: from ackerleytng-ctop.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:13f8]) (user=ackerleytng job=sendgmr) by 2002:a05:6a00:d52:b0:63d:3f94:8c3a with SMTP id n18-20020a056a000d5200b0063d3f948c3amr4655714pfv.6.1683071769151; Tue, 02 May 2023 16:56:09 -0700 (PDT) Date: Tue, 2 May 2023 23:56:03 +0000 In-Reply-To: Mime-Version: 1.0 References: X-Mailer: git-send-email 2.40.1.495.gc816e09b53d-goog Message-ID: Subject: [PATCH 2/2] fs: hugetlbfs: Fix logic to skip allocation on hit in page cache From: Ackerley Tng To: akpm@linux-foundation.org, linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org, mike.kravetz@oracle.com, muchun.song@linux.dev, willy@infradead.org, sidhartha.kumar@oracle.com, jhubbard@nvidia.com Cc: vannapurve@google.com, erdemaktas@google.com, Ackerley Tng X-Spam-Status: No, score=-9.6 required=5.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE,USER_IN_DEF_DKIM_WL autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1764829756269238972?= X-GMAIL-MSGID: =?utf-8?q?1764829756269238972?= When fallocate() is called twice on the same offset in the file, the second fallocate() should succeed. page_cache_next_miss() always advances index before returning, so even on a page cache hit, the check would set present to false. Signed-off-by: Ackerley Tng --- fs/hugetlbfs/inode.c | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/fs/hugetlbfs/inode.c b/fs/hugetlbfs/inode.c index ecfdfb2529a3..f640cff1bbce 100644 --- a/fs/hugetlbfs/inode.c +++ b/fs/hugetlbfs/inode.c @@ -821,7 +821,6 @@ static long hugetlbfs_fallocate(struct file *file, int mode, loff_t offset, */ struct folio *folio; unsigned long addr; - bool present; cond_resched(); @@ -845,10 +844,7 @@ static long hugetlbfs_fallocate(struct file *file, int mode, loff_t offset, mutex_lock(&hugetlb_fault_mutex_table[hash]); /* See if already present in mapping to avoid alloc/free */ - rcu_read_lock(); - present = page_cache_next_miss(mapping, index, 1) != index; - rcu_read_unlock(); - if (present) { + if (filemap_has_folio(mapping, index)) { mutex_unlock(&hugetlb_fault_mutex_table[hash]); hugetlb_drop_vma_policy(&pseudo_vma); continue;