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: 13229389 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id E4F5DC77B78 for ; Tue, 2 May 2023 23:56:12 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230039AbjEBX4L (ORCPT ); Tue, 2 May 2023 19:56:11 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41224 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229768AbjEBX4J (ORCPT ); Tue, 2 May 2023 19:56:09 -0400 Received: from mail-yw1-x114a.google.com (mail-yw1-x114a.google.com [IPv6:2607:f8b0:4864:20::114a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 31AF030E2 for ; Tue, 2 May 2023 16:56:08 -0700 (PDT) Received: by mail-yw1-x114a.google.com with SMTP id 00721157ae682-556011695d1so75245257b3.1 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=OfSFE8FH9Gf5t6XwgmNCb/BiZqYqOVYIWM1sPFjYlzAI9esBF/phvImdGXcgXHkCYE N8ai73Nksp/3kPTTlKWUa2rjMSHgMx84GG7pYbKtz9nJieQFbDTQQJ78n++zLYWOWVf0 rj593eoVR+6qvAK+QEHTizEyhZV6xBlzB2egk5yhqQQDasCUKrRZyB9m0nWK6zwjvg91 p6VhvcKTkpTqbguHUaq1XfwhoxMVyn01A3deNevfhhKnHCO+sCvMo2W5LFuQBNjaJvf2 Fo7taAX1pYuiKIu2EQU+fVtgVphsSVpwXqD8lpGsqbQ1/Gb8AGZxUYtoK4cY+pK8dR7r NuRg== X-Gm-Message-State: AC+VfDysXbmrh82SYRJ0mlozK8xy8uzpee6TJWl2nMU7u1DfuLhbqmLd lkGbnQ6BLzd0H6HjiMR99J0K7hvaSAOADcSy/Q== X-Google-Smtp-Source: ACHHUZ4q9/YhcCBGN0gQnRI8Ol1Z4ZZs3Rm5jmAnbsCWwCheFAJAc64ACgZuO2mDTRSg0K7U9fnxqYL2vTo7g2S1Ng== 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 Precedence: bulk List-ID: X-Mailing-List: linux-fsdevel@vger.kernel.org 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: 13229390 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 42BAFC7EE24 for ; Tue, 2 May 2023 23:56:14 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229539AbjEBX4N (ORCPT ); Tue, 2 May 2023 19:56:13 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41234 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229865AbjEBX4K (ORCPT ); Tue, 2 May 2023 19:56:10 -0400 Received: from mail-pf1-x44a.google.com (mail-pf1-x44a.google.com [IPv6:2607:f8b0:4864:20::44a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9EC0E213E for ; Tue, 2 May 2023 16:56:09 -0700 (PDT) Received: by mail-pf1-x44a.google.com with SMTP id d2e1a72fcca58-63b5e149e1fso2622085b3a.3 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=Ni67p4AX4Yi/JhTLUy0TZ9H3f96AfFqFfarcsY+q0ulVxgU0zmvBZngDDWNIqD61p1 +eBWn0IISyv69eSRkME6fL3TjZvWCBxAMKpQzi3LJE3A//GelIV2lDnHach30ut/HPJw m1cDHnowugC/VdmKaUmitC2sHZzrkJql84jJa5UTL8KxXkvyeM3MezyMk7SCX3Lska/K NH+MoWBSCWnJcS0ObydO4/pWVfvwsFAvkjHB0gPL3E+TCAVt8gXOumvBRFXIqa2cl2uZ uIM+DBDzBcOuH4y3n6MRUbsj1FO+G6Kz20jMmBXCKwgLF9d6am/z/OUO0GctPlOolCLV e7hg== X-Gm-Message-State: AC+VfDz/3bDq8OAJoCpaOUq+AnM0yKlGG7KKjY+a0jBwfog1jLrz0Zl4 0zaPkCHGTcSANNbmM7uOwr8OOWmyQu88Q+ea5Q== X-Google-Smtp-Source: ACHHUZ4uGtyigwr4nUSi3otC5a7lX2METWY1283Yc+awcEh00aN0uIMJwvcd5ZBL+DarH6qtHL28xEqjZBOaZg2lxw== 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 Precedence: bulk List-ID: X-Mailing-List: linux-fsdevel@vger.kernel.org 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;