Received: by 2002:a05:7412:3b8b:b0:fc:a2b0:25d7 with SMTP id nd11csp972022rdb; Fri, 9 Feb 2024 06:46:50 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCUWSDB9r4+IY96947SWfImcAG+UJrAl5S8ijDbNlspsEpvEXH6czpIcjoujzTgpBckuTByI+zJJQLVvDtZhpNXKIgHD6vMlGS1WEP5eDQ== X-Google-Smtp-Source: AGHT+IG8pDFiPJSKOF3Qt6mCyOuWrz1x+Er+Idw56KERWdi/QJVkW66T3hCLI8UAUnHnupBDfwaF X-Received: by 2002:a05:6102:32d4:b0:46d:617b:1bfb with SMTP id o20-20020a05610232d400b0046d617b1bfbmr1978798vss.10.1707490010376; Fri, 09 Feb 2024 06:46:50 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1707490010; cv=pass; d=google.com; s=arc-20160816; b=Eb1VdbtTfcWrXwFIlVaaoSrnd7Pj3HFIl2s3NbcO5qKBmQg9Z4i/5Zscrzml6chl+6 sNtPgKcUQKHTUnme8cnzZ3gc7oa8LTp1B+p50981N+Jqee9R0OxpbVnWwA1j8DZB4nhM awcE9alzk3CLS2vJbLBv6KUATxgWxZBlrkl9nxPfpFMg1+X2v8Jg8pKcOaeLVLq9NpMI 7yMLYiLfGUkfOF7202DLan3k2j96C1vojvhdjzs0nPELZNmKanGjnoOiAoML2bRtKI6/ aZ+oZJ9y9vjdPfePRmDOEhaTQi9qAuw0JhuAnRsVUpsgLx7dTkXYb28QlpYo8VCGRLRB L5WQ== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=mime-version:list-unsubscribe:list-subscribe:list-id:precedence :user-agent:message-id:date:references:organization:in-reply-to :subject:cc:to:from:dkim-signature:dkim-signature:dkim-signature :dkim-signature; bh=3oUQkc/1wI2qWJWC7YK0GbMsld4JtgJbyTyEQVQCM+E=; fh=Z7LBCZu12M1wzLwZ17mCPE2uKhu/WffV87AmgmtYK6Y=; b=v0CTsWLwHYvzmgxyFMluUDJ/Qp+9EHIAoL0y0mXhifGeN7ufLHTTYU8nQePKVe5U/i qVLt3dMprrf3+jbSKcfqRRC2rrsN6xNZ6UBWNE1goVBuFbktYSnnco69oZXRb35q7mvt DynRPVgP1ZQ1PfAjuWSeHJFDtKoeE7eEvvpQ24puFfuWutK7YXSoxxFbX2yIzDxAwMy4 jr2i5WidyNRGxDXWqtQfXKpXNZNbuLZoN0CtwzAl+LvB58q68buYBeeiLWZ/kJHJbyJq XlUPv2oPH5n3HN55BfWHh7alRpMq2zqrd7zhBCxit7ljfo7gZMiJ7RM6zqC7QJy/lIW2 or5w==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@suse.de header.s=susede2_rsa header.b=Udgh1gJw; dkim=neutral (no key) header.i=@suse.de header.s=susede2_ed25519 header.b=iOEeVhjP; dkim=pass header.i=@suse.de header.s=susede2_rsa header.b=Udgh1gJw; dkim=neutral (no key) header.i=@suse.de header.s=susede2_ed25519 header.b=iOEeVhjP; arc=pass (i=1 spf=pass spfdomain=suse.de dkim=pass dkdomain=suse.de dkim=pass dkdomain=suse.de dmarc=pass fromdomain=suse.de); spf=pass (google.com: domain of linux-ext4+bounces-1188-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-ext4+bounces-1188-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=suse.de X-Forwarded-Encrypted: i=2; AJvYcCUE4UzpSzN++mZgLIAB7btrY/PXhLEu8dD4oHiCga/vFcFidDeW0925TvACuPUd+0Uapb/vAkU1BpoaM9fmuKWCZAOrXnpggJQzX1Ipog== Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [2604:1380:45d1:ec00::1]) by mx.google.com with ESMTPS id jh16-20020a056102185000b0046d096987f4si253189vsb.279.2024.02.09.06.46.50 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 09 Feb 2024 06:46:50 -0800 (PST) Received-SPF: pass (google.com: domain of linux-ext4+bounces-1188-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) client-ip=2604:1380:45d1:ec00::1; Authentication-Results: mx.google.com; dkim=pass header.i=@suse.de header.s=susede2_rsa header.b=Udgh1gJw; dkim=neutral (no key) header.i=@suse.de header.s=susede2_ed25519 header.b=iOEeVhjP; dkim=pass header.i=@suse.de header.s=susede2_rsa header.b=Udgh1gJw; dkim=neutral (no key) header.i=@suse.de header.s=susede2_ed25519 header.b=iOEeVhjP; arc=pass (i=1 spf=pass spfdomain=suse.de dkim=pass dkdomain=suse.de dkim=pass dkdomain=suse.de dmarc=pass fromdomain=suse.de); spf=pass (google.com: domain of linux-ext4+bounces-1188-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-ext4+bounces-1188-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=suse.de 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 ny.mirrors.kernel.org (Postfix) with ESMTPS id 0C2731C212E7 for ; Fri, 9 Feb 2024 14:46:50 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id B21C26A018; Fri, 9 Feb 2024 14:46:43 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=suse.de header.i=@suse.de header.b="Udgh1gJw"; dkim=permerror (0-bit key) header.d=suse.de header.i=@suse.de header.b="iOEeVhjP"; dkim=pass (1024-bit key) header.d=suse.de header.i=@suse.de header.b="Udgh1gJw"; dkim=permerror (0-bit key) header.d=suse.de header.i=@suse.de header.b="iOEeVhjP" X-Original-To: linux-ext4@vger.kernel.org Received: from smtp-out1.suse.de (smtp-out1.suse.de [195.135.223.130]) (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 88B414D112; Fri, 9 Feb 2024 14:46:41 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=195.135.223.130 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707490003; cv=none; b=XKBrFqJjCrojLNOajqreUH18D3zRHIZgD54K+zXYtrIGZBG6SB4jSgAKdNbmrSCX5QMxgHA1KY7UoXAKmj1YYx3MK2jsGZc6yW5XByJ0dzVF//Y4X5ev63DyzTVO9kB8snUmuo4/vDajizRnh2ZomADcxgktRvqaV4khogjAlV4= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707490003; c=relaxed/simple; bh=+apgDI2AugL6rqmIPz9McG/DX5pEwWcqyZ0qSy2xde4=; h=From:To:Cc:Subject:In-Reply-To:References:Date:Message-ID: MIME-Version:Content-Type; b=CqHFFNEOPhR2+vn213yXJ8R6CVrnR6v4XZfOs4xEWRa1OLJV6B036hQ4+UVXWjl52xIqfIQRDCGiIEqIPzxdDrqfR7f1vHl3Xyh+OuwyxjvwIHm+CJFFNDs4ZH7EbKNZmSFDkHP6cFcZ5SfX+dLVZFhX8AxiFjGJb9SM7YRoXls= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=suse.de; spf=pass smtp.mailfrom=suse.de; dkim=pass (1024-bit key) header.d=suse.de header.i=@suse.de header.b=Udgh1gJw; dkim=permerror (0-bit key) header.d=suse.de header.i=@suse.de header.b=iOEeVhjP; dkim=pass (1024-bit key) header.d=suse.de header.i=@suse.de header.b=Udgh1gJw; dkim=permerror (0-bit key) header.d=suse.de header.i=@suse.de header.b=iOEeVhjP; arc=none smtp.client-ip=195.135.223.130 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=suse.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=suse.de Received: from imap1.dmz-prg2.suse.org (imap1.dmz-prg2.suse.org [IPv6:2a07:de40:b281:104:10:150:64:97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by smtp-out1.suse.de (Postfix) with ESMTPS id A986422233; Fri, 9 Feb 2024 14:46:39 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1707489999; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=3oUQkc/1wI2qWJWC7YK0GbMsld4JtgJbyTyEQVQCM+E=; b=Udgh1gJw12RNLIfc49aMEGAdhUHni7kqfa6KytMVtTCRwj8Hl6h+Na22JFyn9fe8kKShY9 yCAGw+qQOlIjET1tnB8kZBUh8JQYpnpUanbLELojNdiBQb9m1g4bF0Za+9oCXr1JjA608D iFIXXrozeMIWyUSn/gc8asBGGQc8Zfo= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1707489999; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=3oUQkc/1wI2qWJWC7YK0GbMsld4JtgJbyTyEQVQCM+E=; b=iOEeVhjPZtE+jOQOn6c2YmDU8wR86QYTDBTtdGxcXIrLSqa2DuHtdqo86NnOTqDp502GY+ i+Qi3OjQb83HhkDg== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1707489999; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=3oUQkc/1wI2qWJWC7YK0GbMsld4JtgJbyTyEQVQCM+E=; b=Udgh1gJw12RNLIfc49aMEGAdhUHni7kqfa6KytMVtTCRwj8Hl6h+Na22JFyn9fe8kKShY9 yCAGw+qQOlIjET1tnB8kZBUh8JQYpnpUanbLELojNdiBQb9m1g4bF0Za+9oCXr1JjA608D iFIXXrozeMIWyUSn/gc8asBGGQc8Zfo= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1707489999; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=3oUQkc/1wI2qWJWC7YK0GbMsld4JtgJbyTyEQVQCM+E=; b=iOEeVhjPZtE+jOQOn6c2YmDU8wR86QYTDBTtdGxcXIrLSqa2DuHtdqo86NnOTqDp502GY+ i+Qi3OjQb83HhkDg== Received: from imap1.dmz-prg2.suse.org (localhost [127.0.0.1]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by imap1.dmz-prg2.suse.org (Postfix) with ESMTPS id 707A413353; Fri, 9 Feb 2024 14:46:39 +0000 (UTC) Received: from dovecot-director2.suse.de ([2a07:de40:b281:106:10:150:64:167]) by imap1.dmz-prg2.suse.org with ESMTPSA id Sw5LFc86xmWidwAAD6G6ig (envelope-from ); Fri, 09 Feb 2024 14:46:39 +0000 From: Gabriel Krisman Bertazi To: Christian Brauner Cc: Eric Biggers , viro@zeniv.linux.org.uk, jaegeuk@kernel.org, tytso@mit.edu, amir73il@gmail.com, linux-ext4@vger.kernel.org, linux-f2fs-devel@lists.sourceforge.net, linux-fsdevel@vger.kernel.org Subject: Re: [PATCH v5 04/12] fscrypt: Drop d_revalidate for valid dentries during lookup In-Reply-To: <20240209-netto-ungehalten-35cfdd4b6473@brauner> (Christian Brauner's message of "Fri, 9 Feb 2024 15:03:23 +0100") Organization: SUSE References: <20240129204330.32346-1-krisman@suse.de> <20240129204330.32346-5-krisman@suse.de> <20240131004724.GC2020@sol.localdomain> <871q9x2vwj.fsf@mailhost.krisman.be> <20240201032433.GB1526@sol.localdomain> <87le82yl7k.fsf@mailhost.krisman.be> <20240209-netto-ungehalten-35cfdd4b6473@brauner> Date: Fri, 09 Feb 2024 09:46:38 -0500 Message-ID: <87le7tu241.fsf@mailhost.krisman.be> User-Agent: Gnus/5.13 (Gnus v5.13) Precedence: bulk X-Mailing-List: linux-ext4@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain X-Spam-Level: Authentication-Results: smtp-out1.suse.de; dkim=pass header.d=suse.de header.s=susede2_rsa header.b=Udgh1gJw; dkim=pass header.d=suse.de header.s=susede2_ed25519 header.b=iOEeVhjP X-Rspamd-Server: rspamd2.dmz-prg2.suse.org X-Spamd-Result: default: False [-6.51 / 50.00]; ARC_NA(0.00)[]; RCVD_VIA_SMTP_AUTH(0.00)[]; R_DKIM_ALLOW(-0.20)[suse.de:s=susede2_rsa,suse.de:s=susede2_ed25519]; FROM_HAS_DN(0.00)[]; TO_DN_SOME(0.00)[]; FREEMAIL_ENVRCPT(0.00)[gmail.com]; TO_MATCH_ENVRCPT_ALL(0.00)[]; MIME_GOOD(-0.10)[text/plain]; NEURAL_HAM_LONG(-1.00)[-1.000]; DWL_DNSWL_MED(-2.00)[suse.de:dkim]; HAS_ORG_HEADER(0.00)[]; RCVD_COUNT_THREE(0.00)[3]; DKIM_SIGNED(0.00)[suse.de:s=susede2_rsa,suse.de:s=susede2_ed25519]; DKIM_TRACE(0.00)[suse.de:+]; MX_GOOD(-0.01)[]; RCPT_COUNT_SEVEN(0.00)[9]; DBL_BLOCKED_OPENRESOLVER(0.00)[suse.de:dkim]; FUZZY_BLOCKED(0.00)[rspamd.com]; FROM_EQ_ENVFROM(0.00)[]; MIME_TRACE(0.00)[0:+]; NEURAL_HAM_SHORT(-0.20)[-1.000]; FREEMAIL_CC(0.00)[kernel.org,zeniv.linux.org.uk,mit.edu,gmail.com,vger.kernel.org,lists.sourceforge.net]; RCVD_TLS_ALL(0.00)[]; BAYES_HAM(-3.00)[100.00%] X-Spam-Score: -6.51 X-Rspamd-Queue-Id: A986422233 X-Spam-Flag: NO Christian Brauner writes: > On Fri, Feb 02, 2024 at 11:50:07AM -0300, Gabriel Krisman Bertazi wrote: >> Eric Biggers writes: >> >> > On Wed, Jan 31, 2024 at 03:35:40PM -0300, Gabriel Krisman Bertazi wrote: >> >> Eric Biggers writes: >> >> >> >> > On Mon, Jan 29, 2024 at 05:43:22PM -0300, Gabriel Krisman Bertazi wrote: >> >> >> Unencrypted and encrypted-dentries where the key is available don't need >> >> >> to be revalidated with regards to fscrypt, since they don't go stale >> >> >> from under VFS and the key cannot be removed for the encrypted case >> >> >> without evicting the dentry. Mark them with d_set_always_valid, to >> >> > >> >> > "d_set_always_valid" doesn't appear in the diff itself. >> >> > >> >> >> diff --git a/include/linux/fscrypt.h b/include/linux/fscrypt.h >> >> >> index 4aaf847955c0..a22997b9f35c 100644 >> >> >> --- a/include/linux/fscrypt.h >> >> >> +++ b/include/linux/fscrypt.h >> >> >> @@ -942,11 +942,22 @@ static inline int fscrypt_prepare_rename(struct inode *old_dir, >> >> >> static inline void fscrypt_prepare_lookup_dentry(struct dentry *dentry, >> >> >> bool is_nokey_name) >> >> >> { >> >> >> - if (is_nokey_name) { >> >> >> - spin_lock(&dentry->d_lock); >> >> >> + spin_lock(&dentry->d_lock); >> >> >> + >> >> >> + if (is_nokey_name) >> >> >> dentry->d_flags |= DCACHE_NOKEY_NAME; >> >> >> - spin_unlock(&dentry->d_lock); >> >> >> + else if (dentry->d_flags & DCACHE_OP_REVALIDATE && >> >> >> + dentry->d_op->d_revalidate == fscrypt_d_revalidate) { >> >> >> + /* >> >> >> + * Unencrypted dentries and encrypted dentries where the >> >> >> + * key is available are always valid from fscrypt >> >> >> + * perspective. Avoid the cost of calling >> >> >> + * fscrypt_d_revalidate unnecessarily. >> >> >> + */ >> >> >> + dentry->d_flags &= ~DCACHE_OP_REVALIDATE; >> >> >> } >> >> >> + >> >> >> + spin_unlock(&dentry->d_lock); >> >> > >> >> > This makes lookups in unencrypted directories start doing the >> >> > spin_lock/spin_unlock pair. Is that really necessary? >> >> > >> >> > These changes also make the inline function fscrypt_prepare_lookup() very long >> >> > (when including the fscrypt_prepare_lookup_dentry() that's inlined into it). >> >> > The rule that I'm trying to follow is that to the extent that the fscrypt helper >> >> > functions are inlined, the inline part should be a fast path for unencrypted >> >> > directories. Encrypted directories should be handled out-of-line. >> >> > >> >> > So looking at the original fscrypt_prepare_lookup(): >> >> > >> >> > static inline int fscrypt_prepare_lookup(struct inode *dir, >> >> > struct dentry *dentry, >> >> > struct fscrypt_name *fname) >> >> > { >> >> > if (IS_ENCRYPTED(dir)) >> >> > return __fscrypt_prepare_lookup(dir, dentry, fname); >> >> > >> >> > memset(fname, 0, sizeof(*fname)); >> >> > fname->usr_fname = &dentry->d_name; >> >> > fname->disk_name.name = (unsigned char *)dentry->d_name.name; >> >> > fname->disk_name.len = dentry->d_name.len; >> >> > return 0; >> >> > } >> >> > >> >> > If you could just add the DCACHE_OP_REVALIDATE clearing for dentries in >> >> > unencrypted directories just before the "return 0;", hopefully without the >> >> > spinlock, that would be good. Yes, that does mean that >> >> > __fscrypt_prepare_lookup() will have to handle it too, for the case of dentries >> >> > in encrypted directories, but that seems okay. >> >> >> >> ok, will do. IIUC, we might be able to do without the d_lock >> >> provided there is no store tearing. >> >> >> >> But what was the reason you need the d_lock to set DCACHE_NOKEY_NAME >> >> during lookup? Is there a race with parallel lookup setting d_flag that >> >> I couldn't find? Or is it another reason? >> > >> > d_flags is documented to be protected by d_lock. So for setting >> > DCACHE_NOKEY_NAME, fs/crypto/ just does the safe thing of taking d_lock. I >> > never really looked into whether the lock can be skipped there (i.e., whether >> > anything else can change d_flags while ->lookup is running), since this code >> > only ran for no-key names, for which performance isn't really important. >> >> Yes, I was looking for the actual race that could happen here, and >> couldn't find one. As far as I understand it, the only thing that could >> see the dentry during a lookup would be a parallel lookup, but those >> will be held waiting for completion in d_alloc_parallel, and won't touch >> d_flags. Currently, right after this code, we call d_set_d_op() in >> generic_set_encrypted_ci_d_ops(), which will happily write d_flags without >> the d_lock. If this is a problem here, we have a problem there. >> >> What I really don't want to do is keep the lock for DCACHE_NOKEY_NAME, >> but drop it for unsetting DCACHE_OP_REVALIDATE right in the same field, >> without a good reason. I get the argument that unencrypted >> dentries are a much hotter path and we care more. But the locking rules >> of ->d_lookup don't change for both cases. > > Even if it were to work in this case I don't think it is generally safe > to do. But also, for DCACHE_OP_REVALIDATE afaict this is an > optimization. Why don't you simply accept the raciness, just like fuse > does in fuse_dentry_settime(), check for DCACHE_OP_REVALIDATE locklessly > and only take the lock if that thing is set? That sounds extremely reasonable. I will follow that approach! Thanks, -- Gabriel Krisman Bertazi