Received: by 2002:ab2:6991:0:b0:1f7:f6c3:9cb1 with SMTP id v17csp346163lqo; Wed, 8 May 2024 01:15:13 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCVpkUCdFs7ZJLYhw4kuR6ZKEmysDI436DCWmEZ30LA6WaF2Jh8UvOAoammtKpIjGvbNlnhQsF9US3hIwhYdZfnRAjbNHU9YCZxmb5NXEQ== X-Google-Smtp-Source: AGHT+IEaJ8shWdLFj2ZJ2Mh5niuOu75OOfTWz9hyUlvdsJ8Ox1YQqv6UfP3RW1/C94/qk7LPpVNx X-Received: by 2002:a17:906:3147:b0:a59:a83b:d437 with SMTP id a640c23a62f3a-a59fb94e699mr102184266b.32.1715156113430; Wed, 08 May 2024 01:15:13 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1715156113; cv=pass; d=google.com; s=arc-20160816; b=ib9cGDZuXvfnZ/rKWmDKzGZzq0y2z/U57wYtgOyXWuMKyQZgknUPUTkDmpdJ5qFrC5 AvVZ21fvQaY4TAuyJ6Ty+HW9rKiR+36QrP/b0ZpYXuolFo86yrpB0LIJTqcfsIQLY/vG 4q191akd7iXVJ9Tpr85Nx0zowhoTaR+QxQUKugnoMtj+3nbQWaIcyoSNiMnBN7Vg6Lup h6IxYC0JKVdxbnA178xN9mdnk/mKOCk2fmzpqrVw79kSDiwrUwUNBT9JYmfx/RO1fWeS 2MUsZF84wWHRVCn+HCSITf26B079jccYBpUj3mKPozIrE2izDnTrlgz1tmsdvqN6oFou oh+Q== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:message-id:date:subject:cc:to :from:dkim-signature; bh=yxjVgqQ8RzjwngaiN/l2Vqbmt62iFzpTBxTJvkY2rMg=; fh=sueEDp32xPAaGs87l28wEe2EVmkReUbZLWCQQGcsutM=; b=WST+WCQBoIX5xU9/59lSKFWMcGvONxdHdpq2z42TMWnpoy5QxlFKvpXHUdfVyYD/2E 1dqdof26ptj2vlqrRp6dYsMvWRf5mUr4aZAzzhyNjMsXgZfkUFjhfoyvI1ohhLL315wR 5evP4N6KSSktAATZQ/H50qJzO3TEGejqeDCfq6RXzxn0A77EU2rLDwMsEOnwPDFeWkAk fDtIAe5/QDGJOcVdaqVC+sIYTNNLUMFN0g50RJFK5Kxp2/cmew5jwHvNk2lQBjVVNLE0 sgPw3uOnG1+lWPZ4z8bvYcNoERYL46pGY4AYRk4tlDoD5k1UjUBM4d7EHCBsc4XG7h7e ck0A==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=KjdvZsSX; arc=pass (i=1 spf=pass spfdomain=gmail.com dkim=pass dkdomain=gmail.com dmarc=pass fromdomain=gmail.com); spf=pass (google.com: domain of linux-kernel+bounces-172894-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-172894-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [2604:1380:4601:e00::3]) by mx.google.com with ESMTPS id j26-20020a1709062a1a00b00a596e9d58dbsi6810963eje.901.2024.05.08.01.15.13 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 08 May 2024 01:15:13 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-172894-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) client-ip=2604:1380:4601:e00::3; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=KjdvZsSX; arc=pass (i=1 spf=pass spfdomain=gmail.com dkim=pass dkdomain=gmail.com dmarc=pass fromdomain=gmail.com); spf=pass (google.com: domain of linux-kernel+bounces-172894-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-172894-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by am.mirrors.kernel.org (Postfix) with ESMTPS id 2452D1F24B68 for ; Wed, 8 May 2024 08:15:13 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 2C2BB47F6C; Wed, 8 May 2024 08:15:03 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="KjdvZsSX" Received: from mail-ej1-f41.google.com (mail-ej1-f41.google.com [209.85.218.41]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id A98024502A; Wed, 8 May 2024 08:14:57 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.218.41 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715156100; cv=none; b=Uv7ClyXaG4gUROL24F3v2up6ycEl5abOz0SxTvJ33N/jSyXbKoNUybyuRtpZqQFRks5CR3uh/A4AGE8CKoNzKF9DfPuMpsOHUen9zMKyiM7y4O7sGEr5ZKwt3ZjaFIiZners9XT5hpApESuKVJaVK9yzw0t21ggP1GZZwyh7bW0= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715156100; c=relaxed/simple; bh=lGZ43BLAjq7zd/fsmMu7JBwCQg2TGtkHhkMNyb0g4dQ=; h=From:To:Cc:Subject:Date:Message-ID:MIME-Version; b=o+t69/fhYUfk0r50OH3lh3kGGmX3MFDaamoBUAOcARFtOPZdqlY9mVNxUOUIBGjXxi4ssY3zkRWS+fwv/GExQLCxRPtXr8JxnQGVB5V5UI9slyMtWFlO2WclU6ornyWL9xcTUiE81PZ684GJ9tJmrOImtKkuOQCoxGqh/Q3Yvi8= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=KjdvZsSX; arc=none smtp.client-ip=209.85.218.41 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Received: by mail-ej1-f41.google.com with SMTP id a640c23a62f3a-a4702457ccbso1060847566b.3; Wed, 08 May 2024 01:14:57 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1715156096; x=1715760896; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=yxjVgqQ8RzjwngaiN/l2Vqbmt62iFzpTBxTJvkY2rMg=; b=KjdvZsSXGUMKlIFuyMIXjASgQeNM09Tf4CtigxJbJbOsbyHPR4fdSQUvlAFtS+lXq7 n5GX2kvuLOTxFzIBdLcaG5JQ1Ryvq/7lzOKPOtLXymoW3qtp9gItyAH3RdfNsllbdd7A V6Y+ycCpWJ7sWRsSfMEtkwOVwZLGPNvdHZvdZoEHjMeZybrWzuHDfD5kNjEYTGtGmVKU Do1llcdQl3ZaA5r+j7vXMGiTBCh33DP5PtPGyRxQt32dTzkbvX7LJUWL/Hbu2T0SmrMo ALfOt+SGbcMlttYY0eIRkD0sbhocOug0p8xm1Nka2frfLQwgHrMOgazv6640M9cAZ8Su n1mQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1715156096; x=1715760896; 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=yxjVgqQ8RzjwngaiN/l2Vqbmt62iFzpTBxTJvkY2rMg=; b=h/OYY7LF7ipJS1ZRWRGA4vpWkCuFpkGpTNLQ3hQuuAd4MxPFZGLeMfcjTTN8RcIIqi yRBKmqM/N502kevn6S+AiNoI8Fz9e06q7pmWU89C58q9vHpHfgsqYA+WTb67+f/tDj+5 ZPwW7IWwQDSTOz2O6+UMklSdmlgDwFuZeStA3YkbXFm2MOfXOvrv9vrgjghQ851rAEhG iaPPAWii4gmp7a8NMHvzOqt1B/0V26gmRhpyKWcp0eO+FVg2kEDTqGjvzEYeAeDQY6bd vZa4o48t2W/00jQSUy3HzhISX5xAGgq3kI0kIfpEP6alJgYdkthExuAH5IDpxBoMC3gX pnlA== X-Forwarded-Encrypted: i=1; AJvYcCVhjd4yRHzbms/sLeDA1Eo7CDUfYo4S4DNE0YmO4BuN1IGZX2mNYovU3CFpwUftKkccNr+SjrDWZ4rAw1o6Efeqp7i7cZKpkv5WHhTH0e18AIe6+euUUjC8nybnNnqDTb8g4gW99KDBI1FuGhpIIeYiGTbgR7TC+RweW2LbKWJX9R8QKkah9MouWPE= X-Gm-Message-State: AOJu0YzQRlclCx9U/342cq9rGaIxYSmPRhrQ+hN6csASAx6lDmxV01mP tS7pjturRqevVWKqyZrXtNJGj/elFY4CEmeAC2ALAAjDJ4gpS1df X-Received: by 2002:a17:906:ce26:b0:a59:9b75:b84 with SMTP id a640c23a62f3a-a59fb95d551mr116933466b.35.1715156095580; Wed, 08 May 2024 01:14:55 -0700 (PDT) Received: from f.. (cst-prg-20-30.cust.vodafone.cz. [46.135.20.30]) by smtp.gmail.com with ESMTPSA id mf1-20020a170906cb8100b00a55778c1af7sm7505684ejb.11.2024.05.08.01.14.53 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 08 May 2024 01:14:55 -0700 (PDT) From: Mateusz Guzik To: ebiggers@kernel.org Cc: tytso@mit.edu, jaegeuk@kernel.org, linux-kernel@vger.kernel.org, linux-fscrypt@vger.kernel.org, linux-fsdevel@vger.kernel.org, Mateusz Guzik Subject: [PATCH v2] fscrypt: try to avoid refing parent dentry in fscrypt_file_open Date: Wed, 8 May 2024 10:14:00 +0200 Message-ID: <20240508081400.422212-1-mjguzik@gmail.com> X-Mailer: git-send-email 2.43.0 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Merely checking if the directory is encrypted happens for every open when using ext4, at the moment refing and unrefing the parent, costing 2 atomics and serializing opens of different files. The most common case of encryption not being used can be checked for with RCU instead. Sample result from open1_processes -t 20 ("Separate file open/close") from will-it-scale on Sapphire Rapids (ops/s): before: 12539898 after: 25575494 (+103%) v2: - add a comment justifying rcu usage, submitted by Eric Biggers - whack spurious IS_ENCRYPTED check from the refed case Signed-off-by: Mateusz Guzik --- fs/crypto/hooks.c | 32 ++++++++++++++++++++++++++------ 1 file changed, 26 insertions(+), 6 deletions(-) diff --git a/fs/crypto/hooks.c b/fs/crypto/hooks.c index 104771c3d3f6..d8d5049b8fe1 100644 --- a/fs/crypto/hooks.c +++ b/fs/crypto/hooks.c @@ -30,21 +30,41 @@ int fscrypt_file_open(struct inode *inode, struct file *filp) { int err; - struct dentry *dir; + struct dentry *dentry, *dentry_parent; + struct inode *inode_parent; err = fscrypt_require_key(inode); if (err) return err; - dir = dget_parent(file_dentry(filp)); - if (IS_ENCRYPTED(d_inode(dir)) && - !fscrypt_has_permitted_context(d_inode(dir), inode)) { + dentry = file_dentry(filp); + + /* + * Getting a reference to the parent dentry is needed for the actual + * encryption policy comparison, but it's expensive on multi-core + * systems. Since this function runs on unencrypted files too, start + * with a lightweight RCU-mode check for the parent directory being + * unencrypted (in which case it's fine for the child to be either + * unencrypted, or encrypted with any policy). Only continue on to the + * full policy check if the parent directory is actually encrypted. + */ + rcu_read_lock(); + dentry_parent = READ_ONCE(dentry->d_parent); + inode_parent = d_inode_rcu(dentry_parent); + if (inode_parent != NULL && !IS_ENCRYPTED(inode_parent)) { + rcu_read_unlock(); + return 0; + } + rcu_read_unlock(); + + dentry_parent = dget_parent(dentry); + if (!fscrypt_has_permitted_context(d_inode(dentry_parent), inode)) { fscrypt_warn(inode, "Inconsistent encryption context (parent directory: %lu)", - d_inode(dir)->i_ino); + d_inode(dentry_parent)->i_ino); err = -EPERM; } - dput(dir); + dput(dentry_parent); return err; } EXPORT_SYMBOL_GPL(fscrypt_file_open); -- 2.39.2