Received: by 2002:a25:683:0:0:0:0:0 with SMTP id 125csp3649792ybg; Sun, 7 Jun 2020 06:24:44 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzjlQsNBCFEwbUQIlJGYKjvvf7HbD4J1VOPdOBMk8a05s5zTCHXu9HGuZghwJj2ujTfd+Jx X-Received: by 2002:a05:6402:8d8:: with SMTP id d24mr17418446edz.287.1591536284080; Sun, 07 Jun 2020 06:24:44 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1591536284; cv=none; d=google.com; s=arc-20160816; b=Ww23uu4tkW6fQEOBKBPahvtfedLz3MqOSUlJDUm5gb0+Sm7Z8ubZqUS9Y8jlhU9Tsf BaL52SFtxJ1mflJ84Ill62njQ+VLIhF+f2T8+zgJ0nZyVM31z/VNYx9de0vuHoGQ7iPw Y+feFAzviWR7HolI9sVYWy68VxUCROW0LiV27/7v6E/cH17VXCOJaNN/DeIxSQknbWd0 fiAjxV1WtgwE1Cjf1qAZFQeiWqAXKTHg81ca3f9sfzsNOlyJkKkpGrlHwE5iZNm/L4fC xrDI618KjBxpDyp9kyledrMqO9PTu5NxavYsKRWxeGYrQUgVyHRBBXWLi4DUZt643YZT k5Tw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding :content-language:in-reply-to:mime-version:user-agent:date :message-id:from:references:cc:to:subject:dkim-signature; bh=fzHKd/0Iik52KxAHm17miqHrvv8VGhsBlmCTklHYBK0=; b=wbHxNoXauCJBGN+jWHv4L0/pzniV4qwhDm1ZHsnihoacPt+HpHF6GZ251Jq4WAFaKr 5aHtLnE0vVxrBF04c2gkTNwOStR+Q/BwV2P7VStw8h4cRI2wQN9Hz27KNCHwpvrhUVyA qZxK9/UhHs0SJR6gUyyofzk0MGOASfox9Gr/xaL0f+WExPbSxrX4sPwmvCRITPMfBpRp IyFBNd2/R7898Uwl6Z72/jWP+s4VHp4fr+wKTfknLmaiMcAlc4+CUzSZ9J2bFBvVQBa0 j594xctUk+Af9bo3gmXpzQUcovL4zP1VxcDDm64ijCXX2OOh4a2Lc+OgDLnOTSpQJlQ7 iXrg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@rasmusvillemoes.dk header.s=google header.b=RTqzxRQS; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id oe24si6800807ejb.598.2020.06.07.06.24.21; Sun, 07 Jun 2020 06:24:44 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@rasmusvillemoes.dk header.s=google header.b=RTqzxRQS; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726531AbgFGNWU (ORCPT + 99 others); Sun, 7 Jun 2020 09:22:20 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33092 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726465AbgFGNWT (ORCPT ); Sun, 7 Jun 2020 09:22:19 -0400 Received: from mail-ej1-x642.google.com (mail-ej1-x642.google.com [IPv6:2a00:1450:4864:20::642]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7D959C08C5C3 for ; Sun, 7 Jun 2020 06:22:18 -0700 (PDT) Received: by mail-ej1-x642.google.com with SMTP id mb16so15258118ejb.4 for ; Sun, 07 Jun 2020 06:22:18 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rasmusvillemoes.dk; s=google; h=subject:to:cc:references:from:message-id:date:user-agent :mime-version:in-reply-to:content-language:content-transfer-encoding; bh=fzHKd/0Iik52KxAHm17miqHrvv8VGhsBlmCTklHYBK0=; b=RTqzxRQSbAil8qchRl589zrlc8vgCLgJXlDKlctrTiR6SE4e630dxGjEVVMmvoI0g7 Rjn9gWOtfOxqB1rhf5xpCNQ5YtandOBkwYH1zPlSmnoGT2bfKdckbdTDe4PJtwpBgunz IF6+1Mn8yjbbVH77tmCjaT7tRzqLyScUFLGSw= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:subject:to:cc:references:from:message-id:date :user-agent:mime-version:in-reply-to:content-language :content-transfer-encoding; bh=fzHKd/0Iik52KxAHm17miqHrvv8VGhsBlmCTklHYBK0=; b=ZmWAuxmIvMrKlRg/XmcSvTfklWwiYSCEoM/D55pAixN3a4CyPxT695ZJS+Ect0f8Oz Tmbekq9TK3NfgXX4HOc+mPvhjzZhC9QjEcqT+rJpw0g0iaBqaQHCKYV7awLWIzSNDAD8 MHbgSfD4lTOPeSva4qYlJaKCKq6XXpIIqEGL5EzR0rjkjLivgxzKN8vrl7w08PBMYflM rQG1OZLnzAJf+DGiIVAvFJQ3fCeL+Kt5Prp5FIOooPXnpJlQ9dy5dfj1LbMp6FQlwVm6 o/V0RorvyYqfH9v1zMD/zkREY+2OCr4oCJHKLhO5v7Mpt5UETfDHxVpIINe/AN4AVH/Y 1ACQ== X-Gm-Message-State: AOAM5323sByT4k1W4x/k7hER39cjYxPymAmet59Rm3yvlz52KsXid5fN itpSuSk4YUVNjlcV8pGPGwPr+p0A1yw= X-Received: by 2002:a17:906:2615:: with SMTP id h21mr16577823ejc.84.1591536136693; Sun, 07 Jun 2020 06:22:16 -0700 (PDT) Received: from [192.168.1.149] (ip-5-186-116-45.cgn.fibianet.dk. [5.186.116.45]) by smtp.gmail.com with ESMTPSA id l1sm8526325ejd.114.2020.06.07.06.22.14 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Sun, 07 Jun 2020 06:22:15 -0700 (PDT) Subject: Re: [PATCH resend] fs/namei.c: micro-optimize acl_permission_check To: Linus Torvalds Cc: Alexander Viro , linux-fsdevel , Linux Kernel Mailing List References: <20200605142300.14591-1-linux@rasmusvillemoes.dk> From: Rasmus Villemoes Message-ID: Date: Sun, 7 Jun 2020 15:22:13 +0200 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Thunderbird/68.8.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8 Content-Language: en-US Content-Transfer-Encoding: 7bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 05/06/2020 22.18, Linus Torvalds wrote: > On Fri, Jun 5, 2020 at 7:23 AM Rasmus Villemoes > wrote: >> >> + /* >> + * If the "group" and "other" permissions are the same, >> + * there's no point calling in_group_p() to decide which >> + * set to use. >> + */ >> + if ((((mode >> 3) ^ mode) & 7) && in_group_p(inode->i_gid)) >> mode >>= 3; > > Ugh. Not only is this ugly, but it's not even the best optimization. > > We don't care that group and other match exactly. We only care that > they match in the low 3 bits of the "mask" bits. Yes, I did think about that, but I thought this was the more obviously correct approach, and that in practice one only sees the 0X44 and 0X55 cases. > So if we want this optimization - and it sounds worth it - I think we > should do it right. But I also think it should be written more > legibly. > > And the "& 7" is the same "& (MAY_READ | MAY_WRITE | MAY_EXEC)" we do later. > > In other words, if we do this, I'd like it to be done even more > aggressively, but I'd also like the end result to be a lot more > readable and have more comments about why we do that odd thing. > > Something like this *UNTESTED* patch, perhaps? That will kinda work, except you do that mask &= MAY_RWX before check_acl(), which cares about MAY_NOT_BLOCK and who knows what other bits. > I might have gotten something wrong, so this would need > double-checking, but if it's right, I find it a _lot_ more easy to > understand than making one expression that is pretty complicated and > opaque. Well, I thought this was readable enough with the added comment. There's already that magic constant 3 in the shifts, so the 7 seemed entirely sensible, though one could spell it 0007. Whatever. Perhaps this? As a whole function, I think that's a bit easier for brain-storming. It's your patch, just with that rwx thing used instead of mask, except for the call to check_acl(). static int acl_permission_check(struct inode *inode, int mask) { unsigned int mode = inode->i_mode; unsigned int rwx = mask & (MAY_READ | MAY_WRITE | MAY_EXEC); /* Are we the owner? If so, ACL's don't matter */ if (likely(uid_eq(current_fsuid(), inode->i_uid))) { if ((rwx << 6) & ~mode) return -EACCES; return 0; } /* Do we have ACL's? */ if (IS_POSIXACL(inode) && (mode & S_IRWXG)) { int error = check_acl(inode, mask); if (error != -EAGAIN) return error; } /* * Are the group permissions different from * the other permissions in the bits we care * about? Need to check group ownership if so. */ if (rwx & (mode ^ (mode >> 3))) { if (in_group_p(inode->i_gid)) mode >>= 3; } /* Bits in 'mode' clear that we require? */ return (rwx & ~mode) ? -EACCES : 0; } Rasmus