From patchwork Fri Feb 16 05:16:39 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Vinicius Costa Gomes X-Patchwork-Id: 201929 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:c619:b0:108:e6aa:91d0 with SMTP id hn25csp307896dyb; Thu, 15 Feb 2024 21:18:48 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCW9xB4SYhHMxAQH1YLlG/vcPhXigjyKIC0VNo/l9gMrYzafylMFD1y4NqO2PuJiDjdPUnOwG9mDBC+bhA/AYpUnB3ndCA== X-Google-Smtp-Source: AGHT+IGEBLUSf2VkXc+tTF1NJVVzlTsX2PQcBRme/ZOAizEp1vY7eM92n8W2Lxg+wR+2l7IA3xmR X-Received: by 2002:a05:620a:45a3:b0:787:2450:38ac with SMTP id bp35-20020a05620a45a300b00787245038acmr4741415qkb.2.1708060728451; Thu, 15 Feb 2024 21:18:48 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1708060728; cv=pass; d=google.com; s=arc-20160816; b=C9TQzl1ShiHCNMDheJOtUznxIY3GV2dNPSSYIqfa/cI5MDiFdQ0W3PrwHFSGJsbo6J q50bFRKFT11y479DsWUOcjnzvycOAhSGVAuKctiBlpryHgPl48MCr2piLXoHp3eN9UyG SZpmoA/FtIIEFDG7DJgFN6iOihc+heaEeBnlJfcShje6SSn95OMSySXHxUCae+wxc+nY CGemB3O0y9dLIYiWcAryKO1Is+GOExxdAYCd+py6BjOW92JItdZ4VQIdCuGbfovnII// 1brEJyRfnekFRTAAGhCRJSnMBDXqUtaD19g9vNiLjQddcN8YsAzxcLvxiU9dPmFfw0ki 3Rdw== 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:references:in-reply-to:message-id :date:subject:cc:to:from:dkim-signature; bh=ZIG1EIz8JQh8Fag4B1omBJ21G+SH4PZaZDuG2dnrRow=; fh=1kbPIdRAAhRfWB0HtLOoTrw3f8mqTn3euA1J4XxIyoQ=; b=RK05aMgtWtIeelfJ8RU817pzVTO1ncx+Hug1Rycy12LYkyJZEY6YvSwWPU37R0dr9L 48fFdLeHX2knyyrPZIw8e+8nEukTTDk1gKe2PbhBaZiGeBdeOvy7LisFsSnJOYyX7m+Y yuRBC6ZLxO+jfOh/uuQByBNcv93TctiR3optjddFK5JLr731ylV/zNBe5TwbFyclq1ry 98uw7pAWAwX6aYv+/2d47vuDmrrckExI/nh6I1UvAlSJP5PM/MmZReJUGCE8KG8sXOuR 56iXamYi/Ty278hjptj2lYJeKmt4GohhZB2MFGeAUGr8JForQxKYKCvxS34IECtry+Hl lJcw==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=e5n6K1NH; arc=pass (i=1 spf=pass spfdomain=intel.com dkim=pass dkdomain=intel.com dmarc=pass fromdomain=intel.com); spf=pass (google.com: domain of linux-kernel+bounces-68068-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-68068-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [2604:1380:45d1:ec00::1]) by mx.google.com with ESMTPS id vz25-20020a05620a495900b007862d19342esi3195829qkn.779.2024.02.15.21.18.48 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 15 Feb 2024 21:18:48 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-68068-ouuuleilei=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=@intel.com header.s=Intel header.b=e5n6K1NH; arc=pass (i=1 spf=pass spfdomain=intel.com dkim=pass dkdomain=intel.com dmarc=pass fromdomain=intel.com); spf=pass (google.com: domain of linux-kernel+bounces-68068-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-68068-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.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 ny.mirrors.kernel.org (Postfix) with ESMTPS id B52861C2251F for ; Fri, 16 Feb 2024 05:18:47 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 638B31B803; Fri, 16 Feb 2024 05:17:06 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="e5n6K1NH" Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.13]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 7D5AF14A9F; Fri, 16 Feb 2024 05:17:01 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=192.198.163.13 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708060623; cv=none; b=oK2lzbgoEp63ZASrEe1eSQ6Co41v3l3Vhe8To5932j1zc6lfsS23qpUEQO+TL+0ft2qv4asaBiPi8dqk/U+7ry87EIp2M5y1efHE6npFgAfhUrb19VYI4j5Cbc5xL0YXpqju3mxXGeUx80zW63B/7ErI7dFwraS28jEDBCD9mDk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708060623; c=relaxed/simple; bh=MaswYxz/q3Dw9RFo7bv6BMCxnVmoIlC4gCgWtYO+cJI=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=QZWvC/6h7HdauVK1oHL70jHuLdTzWcC6n3lBWuYpnscvPcRQMj+eNgVShdQvrRNIWOETWhtyyWOd75TMi5+nXYZCOUymFl+FwOb/ryhOzRy+/Zjs5pS8BsZ9RVdhjzt5rgVm5B8vE7I5iS+whTj3jbNByPUltJC6X8fHFFSZhLo= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com; spf=pass smtp.mailfrom=intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=e5n6K1NH; arc=none smtp.client-ip=192.198.163.13 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=intel.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1708060621; x=1739596621; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=MaswYxz/q3Dw9RFo7bv6BMCxnVmoIlC4gCgWtYO+cJI=; b=e5n6K1NHN8gr7Hdn/s8zz7SxVBoV2iC1fM44bF5s3WM+0INhp7RHBBqN KASgi/iuhordvlyScNXhSyxNjAOoSaPgVVGH1y2JrBLXB7vAeqXMxea4c JNSN83eykQN0Pejc2JhjEG1N3/ndcGUUTckhwEmdhkiNqxC2zC2W53TIy eqTM/acbxAWcRsQXmIRR6VmlstL+uAkgazWTieY/yHoIv3vj9s53Ksdj6 ptptz4uji4eTFOQQ/TZTEKV5PVg90td6xB52mo+TVGErxQEE/wWBbMub+ DefSaswW+wRk6xTcFW2BAdDltG0OyhQ7Bye+Bsw/t7iHY+IYWCFIl88jV g==; X-IronPort-AV: E=McAfee;i="6600,9927,10985"; a="5149353" X-IronPort-AV: E=Sophos;i="6.06,163,1705392000"; d="scan'208";a="5149353" Received: from orviesa007.jf.intel.com ([10.64.159.147]) by fmvoesa107.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 15 Feb 2024 21:16:57 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.06,163,1705392000"; d="scan'208";a="4063894" Received: from lvngo-mobl1.amr.corp.intel.com (HELO vcostago-mobl3.intel.com) ([10.125.17.186]) by orviesa007-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 15 Feb 2024 21:16:57 -0800 From: Vinicius Costa Gomes To: brauner@kernel.org, amir73il@gmail.com, hu1.chen@intel.com Cc: miklos@szeredi.hu, malini.bhandaru@intel.com, tim.c.chen@intel.com, mikko.ylinen@intel.com, lizhen.you@intel.com, linux-unionfs@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org, Vinicius Costa Gomes Subject: [RFC v3 4/5] fs: Optimize credentials reference count for backing file ops Date: Thu, 15 Feb 2024 21:16:39 -0800 Message-ID: <20240216051640.197378-5-vinicius.gomes@intel.com> X-Mailer: git-send-email 2.43.1 In-Reply-To: <20240216051640.197378-1-vinicius.gomes@intel.com> References: <20240216051640.197378-1-vinicius.gomes@intel.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1791031486259881657 X-GMAIL-MSGID: 1791031486259881657 For backing file operations, users are expected to pass credentials that will outlive the backing file common operations. Use the specialized guard statements to override/revert the credentials. Signed-off-by: Vinicius Costa Gomes --- fs/backing-file.c | 27 +++++++-------------------- 1 file changed, 7 insertions(+), 20 deletions(-) diff --git a/fs/backing-file.c b/fs/backing-file.c index a681f38d84d8..2cd015f49382 100644 --- a/fs/backing-file.c +++ b/fs/backing-file.c @@ -140,7 +140,6 @@ ssize_t backing_file_read_iter(struct file *file, struct iov_iter *iter, struct backing_file_ctx *ctx) { struct backing_aio *aio = NULL; - const struct cred *old_cred; ssize_t ret; if (WARN_ON_ONCE(!(file->f_mode & FMODE_BACKING))) @@ -153,7 +152,7 @@ ssize_t backing_file_read_iter(struct file *file, struct iov_iter *iter, !(file->f_mode & FMODE_CAN_ODIRECT)) return -EINVAL; - old_cred = override_creds(ctx->cred); + guard(cred)(ctx->cred); if (is_sync_kiocb(iocb)) { rwf_t rwf = iocb_to_rw_flags(flags); @@ -174,8 +173,6 @@ ssize_t backing_file_read_iter(struct file *file, struct iov_iter *iter, backing_aio_cleanup(aio, ret); } out: - revert_creds(old_cred); - if (ctx->accessed) ctx->accessed(ctx->user_file); @@ -187,7 +184,6 @@ ssize_t backing_file_write_iter(struct file *file, struct iov_iter *iter, struct kiocb *iocb, int flags, struct backing_file_ctx *ctx) { - const struct cred *old_cred; ssize_t ret; if (WARN_ON_ONCE(!(file->f_mode & FMODE_BACKING))) @@ -210,7 +206,7 @@ ssize_t backing_file_write_iter(struct file *file, struct iov_iter *iter, */ flags &= ~IOCB_DIO_CALLER_COMP; - old_cred = override_creds(ctx->cred); + guard(cred)(ctx->cred); if (is_sync_kiocb(iocb)) { rwf_t rwf = iocb_to_rw_flags(flags); @@ -222,12 +218,12 @@ ssize_t backing_file_write_iter(struct file *file, struct iov_iter *iter, ret = backing_aio_init_wq(iocb); if (ret) - goto out; + return ret; ret = -ENOMEM; aio = kmem_cache_zalloc(backing_aio_cachep, GFP_KERNEL); if (!aio) - goto out; + return ret; aio->orig_iocb = iocb; aio->end_write = ctx->end_write; @@ -240,9 +236,6 @@ ssize_t backing_file_write_iter(struct file *file, struct iov_iter *iter, if (ret != -EIOCBQUEUED) backing_aio_cleanup(aio, ret); } -out: - revert_creds(old_cred); - return ret; } EXPORT_SYMBOL_GPL(backing_file_write_iter); @@ -252,15 +245,13 @@ ssize_t backing_file_splice_read(struct file *in, loff_t *ppos, unsigned int flags, struct backing_file_ctx *ctx) { - const struct cred *old_cred; ssize_t ret; if (WARN_ON_ONCE(!(in->f_mode & FMODE_BACKING))) return -EIO; - old_cred = override_creds(ctx->cred); + guard(cred)(ctx->cred); ret = vfs_splice_read(in, ppos, pipe, len, flags); - revert_creds(old_cred); if (ctx->accessed) ctx->accessed(ctx->user_file); @@ -274,7 +265,6 @@ ssize_t backing_file_splice_write(struct pipe_inode_info *pipe, unsigned int flags, struct backing_file_ctx *ctx) { - const struct cred *old_cred; ssize_t ret; if (WARN_ON_ONCE(!(out->f_mode & FMODE_BACKING))) @@ -284,11 +274,10 @@ ssize_t backing_file_splice_write(struct pipe_inode_info *pipe, if (ret) return ret; - old_cred = override_creds(ctx->cred); + guard(cred)(ctx->cred); file_start_write(out); ret = iter_file_splice_write(pipe, out, ppos, len, flags); file_end_write(out); - revert_creds(old_cred); if (ctx->end_write) ctx->end_write(ctx->user_file); @@ -300,7 +289,6 @@ EXPORT_SYMBOL_GPL(backing_file_splice_write); int backing_file_mmap(struct file *file, struct vm_area_struct *vma, struct backing_file_ctx *ctx) { - const struct cred *old_cred; int ret; if (WARN_ON_ONCE(!(file->f_mode & FMODE_BACKING)) || @@ -312,9 +300,8 @@ int backing_file_mmap(struct file *file, struct vm_area_struct *vma, vma_set_file(vma, file); - old_cred = override_creds(ctx->cred); + guard(cred)(ctx->cred); ret = call_mmap(vma->vm_file, vma); - revert_creds(old_cred); if (ctx->accessed) ctx->accessed(ctx->user_file);