From patchwork Mon Jun 24 08:54:02 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mateusz Guzik X-Patchwork-Id: 13709186 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 7AADFC2BD09 for ; Mon, 24 Jun 2024 08:54:18 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 10FAA6B0402; Mon, 24 Jun 2024 04:54:18 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 0BF1F6B0403; Mon, 24 Jun 2024 04:54:18 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id ED2EF6B0404; Mon, 24 Jun 2024 04:54:17 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id D050D6B0402 for ; Mon, 24 Jun 2024 04:54:17 -0400 (EDT) Received: from smtpin05.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id 5E8B380CC4 for ; Mon, 24 Jun 2024 08:54:17 +0000 (UTC) X-FDA: 82265170554.05.7EAFDCC Received: from mail-ej1-f51.google.com (mail-ej1-f51.google.com [209.85.218.51]) by imf19.hostedemail.com (Postfix) with ESMTP id 9710F1A0014 for ; Mon, 24 Jun 2024 08:54:15 +0000 (UTC) Authentication-Results: imf19.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b="S/O8v7dJ"; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf19.hostedemail.com: domain of mjguzik@gmail.com designates 209.85.218.51 as permitted sender) smtp.mailfrom=mjguzik@gmail.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1719219240; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:references:dkim-signature; bh=Pv+RV8N0QpN1Ud2s2pIRCR72Ahfy/aJs1kgG36gGrLk=; b=u2VkCudyTYf9K7/55PAsVHtmujZ2WACgJzZnziVUOPMe8iT+byXoIg/92wFPpCZOd5ngYD 2h3KUXYiLH1PwYxpiDP2TPoeccoBWNfuWdd5kK4NUCUih4URA1zTsJBf/0mWwy4PiT5cCn bB6V7dDOUNPTxAn9c28PM1eOQiz9vpk= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1719219240; a=rsa-sha256; cv=none; b=x8nhoUSIk2s5jPu6FHl9QV+iWLdyoK/oyBo9gGimC77H4Pup8ZJn7sn8wlESH/s5ohLfKr VvGAUyQDh9tvAmwynqerHIJTmA6tFwu5xzQR7+FXnFctHv95sEWzgccSu+uAfIhWloYVL1 B+G+OBarONrFWtC7XdVPEw2l+bpSt64= ARC-Authentication-Results: i=1; imf19.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b="S/O8v7dJ"; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf19.hostedemail.com: domain of mjguzik@gmail.com designates 209.85.218.51 as permitted sender) smtp.mailfrom=mjguzik@gmail.com Received: by mail-ej1-f51.google.com with SMTP id a640c23a62f3a-a724598cfe3so150722066b.1 for ; Mon, 24 Jun 2024 01:54:15 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1719219254; x=1719824054; darn=kvack.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=Pv+RV8N0QpN1Ud2s2pIRCR72Ahfy/aJs1kgG36gGrLk=; b=S/O8v7dJmWH3w5kz3bjEQuxk/vIEPu06qvGgqu47qA6brn5CYKjomS1416ysdnLVFR k/8ca2VNd8bQZHKmMvdZFGcSknSFJhMqI7aV6act1XiFxi0wv6WAEjNtTDowPepRQYs5 Q/3xBnhlJbXqZwx5Tq3i09A7ITd6b6pXNW7BevWVQdm4T4r7oe+PjIKdBrNMfN7OAG6f TIDVWWqrc33nGzGvT1bU5JBT07/ayzgRx1Hlk2QNj+XLbd9Oeu8ySOJWDNOlRXlgZrMO u6sfnnKhA4/s2Z247RG/7M0etkCL1uJZilbTOPZn+qLb71/5E0LPq84fQ+lKipKPPHRg k2tg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1719219254; x=1719824054; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=Pv+RV8N0QpN1Ud2s2pIRCR72Ahfy/aJs1kgG36gGrLk=; b=RPYRHEeGnyIjjuon0k0ThqLTEFcrDPL51A6HVp65hOd2zkpO1YsaA2eK6hSbhloSYV lBQBU3Ojkb7NY7V0h6fQN4wWln+hrzPfr2OQBBDT7+c0vtLXFTftNrskAGcbJzccpGRU JygP3UvHd6cl8Lud0or7WcDBZ/U9IuOkgtJZEnXiYkSl6lqod4ojC0lRW9daYhMaPiWY OAvaoeEdWeFSp94iKeGSBwtmnFNcuLDMd4O5YNzA92AW/ZDwbi5UtNFq5mv6CI6VP9z/ DPkuD1JImpM2BAjEi/1oXA7QmDtzTBwxzo5D0dMkZIZi6sZExFGauYvY1gH7OuJ46Kdd g46A== X-Forwarded-Encrypted: i=1; AJvYcCXSSzHjnPSrWAHl0IOPive4lsEWJbxNMCXY2fZrC+Ugwb9qJOr33jzIiON7udWx0+fMBsSt1us4O5TdGvB0sxxwmBc= X-Gm-Message-State: AOJu0YyLBYa5H7xm0M6kzeaCR3SOGHo4luH59hzSJvsYMMMeul0sfshr jKe8OieH+w2rmkncnAXnSxIcNprq9I4p89AFhWEpUG5wyyFRUNo1 X-Google-Smtp-Source: AGHT+IHxO+wFiekwRcIhzp1CPJgHEZF3oE57OtnkPZG236M1H3FMttHEXj+qYiiFlVl8qiHX6USccw== X-Received: by 2002:a17:906:80c2:b0:a6f:d867:4259 with SMTP id a640c23a62f3a-a7245ba3cb5mr260877866b.26.1719219253603; Mon, 24 Jun 2024 01:54:13 -0700 (PDT) Received: from f.. (cst-prg-81-171.cust.vodafone.cz. [46.135.81.171]) by smtp.gmail.com with ESMTPSA id a640c23a62f3a-a724fe52345sm126046666b.53.2024.06.24.01.54.11 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 24 Jun 2024 01:54:13 -0700 (PDT) From: Mateusz Guzik To: akpm@linux-foundation.org Cc: brauner@kernel.org, viro@zeniv.linux.org.uk, jack@suse.cz, linux-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, Mateusz Guzik Subject: [PATCH v2] vfs: remove redundant smp_mb for thp handling in do_dentry_open Date: Mon, 24 Jun 2024 10:54:02 +0200 Message-ID: <20240624085402.493630-1-mjguzik@gmail.com> X-Mailer: git-send-email 2.43.0 MIME-Version: 1.0 X-Rspamd-Server: rspam07 X-Rspamd-Queue-Id: 9710F1A0014 X-Stat-Signature: ud9i4goi1wq5gxg67axnaq93q3hmi3xq X-Rspam-User: X-HE-Tag: 1719219255-299317 X-HE-Meta: U2FsdGVkX193Uamn5X0dnnIKPlMLC4xZhnCahuGHzCJhLSkwnDR1nPKkMno8ErKgE42ZLALZMdTsMUz53+sEbpYalzuyX/0W3WL28/Gj+41iXev1mh9jNqbxUJwHOKIWGVKvfDnJXs+wzCLjIt4v7g3bZ1d5prI0VL1/uhf4AZZlob4tunnNjTHDY17uworHW4py53b0LgONMmANfobBxbUnmWnUs8fbe7566itzAuUxumut+TmGA15tbSdgGeGhWK6AViEnuu2577sAlah4W/9N1qnLFFJiasSYKVuuIV182B+zCpe2OxXgV59YUQv6TAkmuVbNvNCCDSKiZ9/Hblw/+PFQ+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amicity=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: opening for write performs: if (f->f_mode & FMODE_WRITE) { [snip] smp_mb(); if (filemap_nr_thps(inode->i_mapping)) { [snip] } } filemap_nr_thps on kernels built without CONFIG_READ_ONLY_THP_FOR expands to 0, allowing the compiler to eliminate the entire thing, with exception of the fence (and the branch leading there). So happens required synchronisation between i_writecount and nr_thps changes is already provided by the full fence coming from get_write_access -> atomic_inc_unless_negative, thus the smp_mb instance above can be removed regardless of CONFIG_READ_ONLY_THP_FOR. While I updated commentary in places claiming to match the now-removed fence, I did not try to patch them to act on the compile option. I did not bother benchmarking it, not issuing a spurious full fence in the fast path does not warrant justification from perf standpoint. Signed-off-by: Mateusz Guzik Reviewed-by: Jan Kara --- v2: - just whack the fence instead of ifdefing - change To recipient, the person who committed the original change is no longer active fs/open.c | 9 ++++----- mm/khugepaged.c | 10 +++++----- 2 files changed, 9 insertions(+), 10 deletions(-) diff --git a/fs/open.c b/fs/open.c index 28f2fcbebb1b..64976b6dc75f 100644 --- a/fs/open.c +++ b/fs/open.c @@ -986,12 +986,11 @@ static int do_dentry_open(struct file *f, */ if (f->f_mode & FMODE_WRITE) { /* - * Paired with smp_mb() in collapse_file() to ensure nr_thps - * is up to date and the update to i_writecount by - * get_write_access() is visible. Ensures subsequent insertion - * of THPs into the page cache will fail. + * Depends on full fence from get_write_access() to synchronize + * against collapse_file() regarding i_writecount and nr_thps + * updates. Ensures subsequent insertion of THPs into the page + * cache will fail. */ - smp_mb(); if (filemap_nr_thps(inode->i_mapping)) { struct address_space *mapping = inode->i_mapping; diff --git a/mm/khugepaged.c b/mm/khugepaged.c index 409f67a817f1..2e017585f813 100644 --- a/mm/khugepaged.c +++ b/mm/khugepaged.c @@ -1997,9 +1997,9 @@ static int collapse_file(struct mm_struct *mm, unsigned long addr, if (!is_shmem) { filemap_nr_thps_inc(mapping); /* - * Paired with smp_mb() in do_dentry_open() to ensure - * i_writecount is up to date and the update to nr_thps is - * visible. Ensures the page cache will be truncated if the + * Paired with the fence in do_dentry_open() -> get_write_access() + * to ensure i_writecount is up to date and the update to nr_thps + * is visible. Ensures the page cache will be truncated if the * file is opened writable. */ smp_mb(); @@ -2187,8 +2187,8 @@ static int collapse_file(struct mm_struct *mm, unsigned long addr, if (!is_shmem && result == SCAN_COPY_MC) { filemap_nr_thps_dec(mapping); /* - * Paired with smp_mb() in do_dentry_open() to - * ensure the update to nr_thps is visible. + * Paired with the fence in do_dentry_open() -> get_write_access() + * to ensure the update to nr_thps is visible. */ smp_mb(); }