From patchwork Fri Feb 2 20:39:20 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bart Van Assche X-Patchwork-Id: 13543358 Received: from mail-pg1-f179.google.com (mail-pg1-f179.google.com [209.85.215.179]) (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 F0AC627473 for ; Fri, 2 Feb 2024 20:39:50 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.215.179 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706906392; cv=none; b=Kbc37x+tiIPcu83l1OAVasiYCXK0XgSzGLk+xPryUQuT6OnTuZwlD1V48ZOESKuPY6zpTgAf8d3o75kdFVbIkDN1cMHBxHyw7P5xDRfBF/vQnoVLsjDbF6+vyQmGENKIGWwcWTERqbf/9jPuNTKZQpNNWEiASM2VOc6e3ftgdNk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706906392; c=relaxed/simple; bh=S/P24wcTvrFOg4SsS4CeD+d5rBmERutoXicKIgS4CC4=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=lrjwE775J3pC/xf8mQlvn8l1fbATSU9dsxUInCRZxzQQ0oSOI8IBrHcQ9G0HXnyZ0us7HhkrZUjQY17HHPunanhlwpJ08Au+koLMmzsW7YNMT2oZUuFt2/0pQADxxk1pOSEtu4g5UelM128rq6gtU/nAN3P1PFZZWO8YDU6qK3g= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=fail (p=none dis=none) header.from=acm.org; spf=pass smtp.mailfrom=gmail.com; arc=none smtp.client-ip=209.85.215.179 Authentication-Results: smtp.subspace.kernel.org; dmarc=fail (p=none dis=none) header.from=acm.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Received: by mail-pg1-f179.google.com with SMTP id 41be03b00d2f7-5cddfe0cb64so2114177a12.0 for ; Fri, 02 Feb 2024 12:39:50 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1706906390; x=1707511190; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=4NnkzmgwtRA87qL5gc3Zv1AqjSPa8netVCv6+D4Bmn0=; b=pK4uEGrv3WJZOX5tjzCFybxZOt58/g4TVcY0m+nmqIo4zd1NEZ28eBcdmfeAd1dQaO G4ffk22KJ8NszXhb2frh+qS3TD5Hy4pO497+MDLEnV71D7Bp1DeyjRQuyZs2Gfebh3kK ydd7qtcvSwAhwEbY8oCQOfDuXGmgYWufNDtPFwrsgDCUQKZjz7t7xUOrk0ImBkQnxbyb BRExVnOAUr6YYL/sUy3qcmVoeswPwFRjlaQmkSHTyyNSO5rftYYD0rA4LtDJDPwXkeMh GMwLof4i3W33GLSuGesww1kq3eIZtr35cTDDLbmZc1hl7BDVq+7REoWpfnXRxLjiLYj7 clrQ== X-Gm-Message-State: AOJu0Yxs8t8zCS5nU9sgEiIes7vg+pA0G7eMZq/iUughPW3PDDe0uAae ETSaGBRcGo9pj/yYrTU1oTbRPYdw8rKw+o5N1j5qnQoO6lImR2gCryyc1zz9 X-Google-Smtp-Source: AGHT+IFdqP0z/gU3IvL8+rnfoqhKjtbvkS0EKyYR5LHDkyg6HMGJNj0WB1TpmUPN1RFpvYQwWdzh8Q== X-Received: by 2002:a05:6a20:43aa:b0:19e:4457:ff59 with SMTP id i42-20020a056a2043aa00b0019e4457ff59mr7428069pzl.14.1706906390170; Fri, 02 Feb 2024 12:39:50 -0800 (PST) X-Forwarded-Encrypted: i=0; AJvYcCX4qh7hwpGA1mEauLIIv6VZT3Q/g5lecaVas5dbxZZpUBIpGgCC2SiTNqm8qB5OsYMXQN9iIrTdxo7w5TTvAvrgC/jgbdPcqFwuVrIp06fGf3t6fdAc039Y8whOXRQRIvPJxSQXYxGvUHf16V3X1DGTcz+rDjVNXwgzB29vNgn18yt860hxAz/Pw55TDZdY1/IYVf/4Bjkhj2MOD+Ksrvm21mAFC+CT2rzZt3UtRjWhA+tne09WoTBMyjBxp0SZDuYaHmxPGFALCj/vfZQFTbLT3q1u8cf4aFgZird3WgUfvsiparFvxlJEwKqD Received: from bvanassche-linux.mtv.corp.google.com ([2620:0:1000:8411:4cc3:4ab5:7d2:ddc7]) by smtp.gmail.com with ESMTPSA id f8-20020a63de08000000b005d8aef12380sm2239678pgg.73.2024.02.02.12.39.49 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 02 Feb 2024 12:39:49 -0800 (PST) From: Bart Van Assche To: Christian Brauner , Alexander Viro Cc: linux-fsdevel@vger.kernel.org, Jens Axboe , Christoph Hellwig , "Martin K . Petersen" , Bart Van Assche , Kanchan Joshi , Jeff Layton , Chuck Lever , Stephen Rothwell Subject: [PATCH v2 1/6] fs: Fix rw_hint validation Date: Fri, 2 Feb 2024 12:39:20 -0800 Message-ID: <20240202203926.2478590-2-bvanassche@acm.org> X-Mailer: git-send-email 2.43.0.594.gd9cf4e227d-goog In-Reply-To: <20240202203926.2478590-1-bvanassche@acm.org> References: <20240202203926.2478590-1-bvanassche@acm.org> Precedence: bulk X-Mailing-List: linux-fsdevel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Reject values that are valid rw_hints after truncation but not before truncation by passing an untruncated value to rw_hint_valid(). Reviewed-by: Christoph Hellwig Reviewed-by: Kanchan Joshi Cc: Jeff Layton Cc: Chuck Lever Cc: Jens Axboe Cc: Stephen Rothwell Fixes: 5657cb0797c4 ("fs/fcntl: use copy_to/from_user() for u64 types") Signed-off-by: Bart Van Assche --- fs/fcntl.c | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/fs/fcntl.c b/fs/fcntl.c index c80a6acad742..3ff707bf2743 100644 --- a/fs/fcntl.c +++ b/fs/fcntl.c @@ -268,7 +268,7 @@ static int f_getowner_uids(struct file *filp, unsigned long arg) } #endif -static bool rw_hint_valid(enum rw_hint hint) +static bool rw_hint_valid(u64 hint) { switch (hint) { case RWH_WRITE_LIFE_NOT_SET: @@ -288,19 +288,17 @@ static long fcntl_rw_hint(struct file *file, unsigned int cmd, { struct inode *inode = file_inode(file); u64 __user *argp = (u64 __user *)arg; - enum rw_hint hint; - u64 h; + u64 hint; switch (cmd) { case F_GET_RW_HINT: - h = inode->i_write_hint; - if (copy_to_user(argp, &h, sizeof(*argp))) + hint = inode->i_write_hint; + if (copy_to_user(argp, &hint, sizeof(*argp))) return -EFAULT; return 0; case F_SET_RW_HINT: - if (copy_from_user(&h, argp, sizeof(h))) + if (copy_from_user(&hint, argp, sizeof(hint))) return -EFAULT; - hint = (enum rw_hint) h; if (!rw_hint_valid(hint)) return -EINVAL; From patchwork Fri Feb 2 20:39:21 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bart Van Assche X-Patchwork-Id: 13543359 Received: from mail-oo1-f45.google.com (mail-oo1-f45.google.com [209.85.161.45]) (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 5C6F67C6CE for ; Fri, 2 Feb 2024 20:39:52 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.161.45 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706906393; cv=none; b=W7AFNmK+/H4ABdEE75hDTSJ5QgAAfqnPDC0p+wt8topr1wMaOIBt+2jTtpQZi0Nq9yYRjyadwk7DxahDInI5ZHnbhwlT4GSosmWsvCb3ZPS6n4OBkNyVhzLfMDNLVS6UJKldfQCoYHKRcUECVSQ8Vkn1+91ZwalqBe25yIMyO7c= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706906393; c=relaxed/simple; bh=PeigcjR0fJHqnWYzUNDhF0SumumPwInd9lmx/bLcwVY=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=Xsb7fUpRG9xBO2qwmAGh8fOwQ7mFCseWAgwxXu+K3Fue9mxXjPAob+ih49j0YJnOu6q1qwcY9f8B0aGC28V+XcTtgcqIMCkDv2y+lnc0C/Knm+Nmau1Cwi3euzmTKtDe+9evxbqVbN6/azjA3ePH0v0eVdDDUsGla62c0ZHeHXk= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=fail (p=none dis=none) header.from=acm.org; spf=pass smtp.mailfrom=gmail.com; arc=none smtp.client-ip=209.85.161.45 Authentication-Results: smtp.subspace.kernel.org; dmarc=fail (p=none dis=none) header.from=acm.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Received: by mail-oo1-f45.google.com with SMTP id 006d021491bc7-59a7a6a24b1so1141603eaf.2 for ; Fri, 02 Feb 2024 12:39:52 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1706906391; x=1707511191; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=Jkb7BdFD0GARRMQmHbkGbl9/+4lufBLaMcZRoVBPKBQ=; b=hf8VPcMDPSVlCag8RiQY3C91l07DilBDw7efHOc/lUxWyUx53Cx5UF1JZpvwuvZxUy p/B7m47IXH4TkFwl3udphQgyEh7HlbDTE3kl02O7xX7TR17tFfjH1aqM7p7tle7+OnHw vxlSLx764orx0lu1uIpcSeKjSBYCv/IjXp8tNzs2C3hp8VAOI9+haQmxHXuQlthBAH/c VDnNSVcqduXvlbws4NcmJy0uPvBURI41TtPZCx7j6BmpKOea3HgnI0zVQFpWm7+s7wXw KSLE9yWJX4lU9FH+EGBLEiavkZqE/4BnXwpY33JUL5kSVYAjz9LuoKzXOtoHcizurU6t u14A== X-Gm-Message-State: AOJu0YyKk+7eAtGE4cSGTIzwVBX3gsl9KLKiasJzcxlpiv5YLBdxR6b4 jfnVDAT+yjsUykzgQIzDypsYqbyWXDdHWkd2FUTlKTgAE67mOB19 X-Google-Smtp-Source: AGHT+IG1+uP5CcN9rTTcssYSjIZGMttSjwyGEXw6WrUqRA7pduBBrUW9Bt+kEDDLyVjGrr+305u8iQ== X-Received: by 2002:a05:6358:3121:b0:176:40fb:e123 with SMTP id c33-20020a056358312100b0017640fbe123mr9335719rwe.5.1706906391241; Fri, 02 Feb 2024 12:39:51 -0800 (PST) X-Forwarded-Encrypted: i=0; AJvYcCWVoeej/WqOUFQ1qe4Q1EO8ooUmvGNV4cZ5QAaP9J5aquYgX5M6rwnEzGDG/U5JBeLKfI/W8p0Y28JTy5ei6Zeid0V6GIJT9Z2j8zKgvjS+t37yq1IzTe/wd6rWhB2HglRePN1R/TdNPeAEeSzzMw3vST3omXDFrebbN2vc6+qoluL3/MK9YplQ4NuSPzIkOhO+IdHeUYPAxQsz7XmVRI23kw3zbt0GWXHL/vs5+2Yaqw== Received: from bvanassche-linux.mtv.corp.google.com ([2620:0:1000:8411:4cc3:4ab5:7d2:ddc7]) by smtp.gmail.com with ESMTPSA id f8-20020a63de08000000b005d8aef12380sm2239678pgg.73.2024.02.02.12.39.50 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 02 Feb 2024 12:39:50 -0800 (PST) From: Bart Van Assche To: Christian Brauner , Alexander Viro Cc: linux-fsdevel@vger.kernel.org, Jens Axboe , Christoph Hellwig , "Martin K . Petersen" , Bart Van Assche , Johannes Thumshirn Subject: [PATCH v2 2/6] fs: Verify write lifetime constants at compile time Date: Fri, 2 Feb 2024 12:39:21 -0800 Message-ID: <20240202203926.2478590-3-bvanassche@acm.org> X-Mailer: git-send-email 2.43.0.594.gd9cf4e227d-goog In-Reply-To: <20240202203926.2478590-1-bvanassche@acm.org> References: <20240202203926.2478590-1-bvanassche@acm.org> Precedence: bulk X-Mailing-List: linux-fsdevel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 The code in fs/fcntl.c converts RWH_* constants to and from WRITE_LIFE_* constants using casts. Verify at compile time that these casts will yield the intended effect. Reviewed-by: Christoph Hellwig Suggested-by: Christoph Hellwig Reviewed-by: Johannes Thumshirn Signed-off-by: Bart Van Assche --- fs/fcntl.c | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/fs/fcntl.c b/fs/fcntl.c index 3ff707bf2743..f3bc4662455f 100644 --- a/fs/fcntl.c +++ b/fs/fcntl.c @@ -270,6 +270,13 @@ static int f_getowner_uids(struct file *filp, unsigned long arg) static bool rw_hint_valid(u64 hint) { + BUILD_BUG_ON(WRITE_LIFE_NOT_SET != RWH_WRITE_LIFE_NOT_SET); + BUILD_BUG_ON(WRITE_LIFE_NONE != RWH_WRITE_LIFE_NONE); + BUILD_BUG_ON(WRITE_LIFE_SHORT != RWH_WRITE_LIFE_SHORT); + BUILD_BUG_ON(WRITE_LIFE_MEDIUM != RWH_WRITE_LIFE_MEDIUM); + BUILD_BUG_ON(WRITE_LIFE_LONG != RWH_WRITE_LIFE_LONG); + BUILD_BUG_ON(WRITE_LIFE_EXTREME != RWH_WRITE_LIFE_EXTREME); + switch (hint) { case RWH_WRITE_LIFE_NOT_SET: case RWH_WRITE_LIFE_NONE: From patchwork Fri Feb 2 20:39:22 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bart Van Assche X-Patchwork-Id: 13543360 Received: from mail-pg1-f171.google.com (mail-pg1-f171.google.com [209.85.215.171]) (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 3BA764176D for ; Fri, 2 Feb 2024 20:39:53 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.215.171 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706906395; cv=none; b=uZA+21VLtI/5FUSz3vUA+hvtt3Ulxlj3W1VVT6Bc1zj4cjhA+stw9RM5O2a01OBuRSZiDv6e0f7eRq8lapgpUbIS5simZJIEh9LuuLmztO2fsw9mfOH9RwfaqCN/XUGe7TrE2jgRIqnXI6ESj2Kl/cwju06rV0QJnfuTPdADRqk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706906395; c=relaxed/simple; bh=rOsuHbgIOOOfHazZ42+dHSV3q4tvgcje94XQjRipquc=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=ovDwRPrjWD9L39leW+I6xkQI/qTLGymtyn7smg89NVYGuS3DBiDhkLlTRVYCQfTHojO+jkwIBGOGNQ8p5uVLNizcs5LNegsnMLf6wDg6/qPYTnaqjTyi+oFEkaOvDnJgJXkhefWtPM2OTTdN2Exl9Kk+69kV3w9rOGU21cyEbQk= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=fail (p=none dis=none) header.from=acm.org; spf=pass smtp.mailfrom=gmail.com; arc=none smtp.client-ip=209.85.215.171 Authentication-Results: smtp.subspace.kernel.org; dmarc=fail (p=none dis=none) header.from=acm.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Received: by mail-pg1-f171.google.com with SMTP id 41be03b00d2f7-5ca29c131ebso2280367a12.0 for ; Fri, 02 Feb 2024 12:39:53 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1706906392; x=1707511192; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=C6xn9gMKxEoulxlALoe13cShfItbIgtXnuWDbuTfeiU=; b=lAI+KIOYh8VXggC9NIH7hLd2LOJPllCpAE1xQ8CHlxgHY5BKRHBbpRgbtheiJIB+wy QE1nSJjW8lgxYSiRopkaWAgf7CymqK4Z4WcrmOUxhg7SJppKoKGMWgiZvMCp6ldvponq GCimMdXTSen/ATGVJnISXf16Qv2bcZHgIfe8DjKtBMlXI8jS9EhchPG8srTxWHoznRaV SZFe6ILTy/quq+inpAnFaNlp5jtx/H45FhBRtcXCIClzG2vdFaKv9amjUt2g8qBdHe+y sb4YIs1TJQSZ3iUvh7eAfpWnF+XgNa5/ayHqS+KMTzrnIKKHERFug1325z8BoRYMx6tw 8b+Q== X-Gm-Message-State: AOJu0Yz4HpI1NUztWMhntouRXXRXhIboXZldy3E34p75N89RWwTWMFaD o5heVfetT2AbcUwBAlKWaVyq36fUPJlrlMu/D0dogNky5FqpBzle X-Google-Smtp-Source: AGHT+IEZDixWzZ0iCXGrhtYC+N/N919scyzl/ixWGrMkT46jSUoF3uaTtrbr23cv6ZyV4pPzd00yWg== X-Received: by 2002:a05:6a21:3a87:b0:19c:773c:570e with SMTP id zv7-20020a056a213a8700b0019c773c570emr4673899pzb.39.1706906392468; Fri, 02 Feb 2024 12:39:52 -0800 (PST) X-Forwarded-Encrypted: i=0; AJvYcCUncaRIGzBeayQh8fiBp+SxQxHGTUEfegUh7SCbIlitaVd/mk1Uddsy/CWQ4dcwxXC5xjEgFcTRUKqfSsqC3mGP0rOMFjMmDPrhJnlTfeB0prp0LanME+VAzkobIfEbCZEdEYSwoHxR5mLYvMkohip4k18evnyR4DA+UnZCkuVgsCIiaJAO91YgIs43KgVNkTZiPg68v8VyJArpif2GYBibc+sKeh+I4PvaSZZQVWFrcSU6fxTfNm7qm98DbyHqd+dsjy2URgmbjUnIMi+Hq14BufgJSlkBQ9gzm/w3olQqLNYNt9V/Tg2n72rl Received: from bvanassche-linux.mtv.corp.google.com ([2620:0:1000:8411:4cc3:4ab5:7d2:ddc7]) by smtp.gmail.com with ESMTPSA id f8-20020a63de08000000b005d8aef12380sm2239678pgg.73.2024.02.02.12.39.51 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 02 Feb 2024 12:39:52 -0800 (PST) From: Bart Van Assche To: Christian Brauner , Alexander Viro Cc: linux-fsdevel@vger.kernel.org, Jens Axboe , Christoph Hellwig , "Martin K . Petersen" , Bart Van Assche , Kanchan Joshi , Jeff Layton , Chuck Lever , Stephen Rothwell Subject: [PATCH v2 3/6] fs: Split fcntl_rw_hint() Date: Fri, 2 Feb 2024 12:39:22 -0800 Message-ID: <20240202203926.2478590-4-bvanassche@acm.org> X-Mailer: git-send-email 2.43.0.594.gd9cf4e227d-goog In-Reply-To: <20240202203926.2478590-1-bvanassche@acm.org> References: <20240202203926.2478590-1-bvanassche@acm.org> Precedence: bulk X-Mailing-List: linux-fsdevel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Split fcntl_rw_hint() such that there is one helper function per fcntl. Use READ_ONCE() and WRITE_ONCE() to access the i_write_hint member instead of protecting such accesses with the inode lock. READ_ONCE() is not used in I/O path code that reads i_write_hint. Users who want F_SET_RW_HINT to affect I/O need to make sure that F_SET_RW_HINT has completed before I/O is submitted that should use the configured write hint. Cc: Christoph Hellwig Suggested-by: Christoph Hellwig Cc: Kanchan Joshi Cc: Jeff Layton Cc: Chuck Lever Cc: Jens Axboe Cc: Stephen Rothwell Signed-off-by: Bart Van Assche --- fs/fcntl.c | 45 ++++++++++++++++++++++++--------------------- 1 file changed, 24 insertions(+), 21 deletions(-) diff --git a/fs/fcntl.c b/fs/fcntl.c index f3bc4662455f..d2b15351ab8e 100644 --- a/fs/fcntl.c +++ b/fs/fcntl.c @@ -290,32 +290,33 @@ static bool rw_hint_valid(u64 hint) } } -static long fcntl_rw_hint(struct file *file, unsigned int cmd, - unsigned long arg) +static long fcntl_get_rw_hint(struct file *file, unsigned int cmd, + unsigned long arg) { struct inode *inode = file_inode(file); u64 __user *argp = (u64 __user *)arg; - u64 hint; + u64 hint = READ_ONCE(inode->i_write_hint); - switch (cmd) { - case F_GET_RW_HINT: - hint = inode->i_write_hint; - if (copy_to_user(argp, &hint, sizeof(*argp))) - return -EFAULT; - return 0; - case F_SET_RW_HINT: - if (copy_from_user(&hint, argp, sizeof(hint))) - return -EFAULT; - if (!rw_hint_valid(hint)) - return -EINVAL; + if (copy_to_user(argp, &hint, sizeof(*argp))) + return -EFAULT; + return 0; +} - inode_lock(inode); - inode->i_write_hint = hint; - inode_unlock(inode); - return 0; - default: +static long fcntl_set_rw_hint(struct file *file, unsigned int cmd, + unsigned long arg) +{ + struct inode *inode = file_inode(file); + u64 __user *argp = (u64 __user *)arg; + u64 hint; + + if (copy_from_user(&hint, argp, sizeof(hint))) + return -EFAULT; + if (!rw_hint_valid(hint)) return -EINVAL; - } + + WRITE_ONCE(inode->i_write_hint, hint); + + return 0; } static long do_fcntl(int fd, unsigned int cmd, unsigned long arg, @@ -421,8 +422,10 @@ static long do_fcntl(int fd, unsigned int cmd, unsigned long arg, err = memfd_fcntl(filp, cmd, argi); break; case F_GET_RW_HINT: + err = fcntl_get_rw_hint(filp, cmd, arg); + break; case F_SET_RW_HINT: - err = fcntl_rw_hint(filp, cmd, arg); + err = fcntl_set_rw_hint(filp, cmd, arg); break; default: break; From patchwork Fri Feb 2 20:39:23 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bart Van Assche X-Patchwork-Id: 13543362 Received: from mail-pg1-f179.google.com (mail-pg1-f179.google.com [209.85.215.179]) (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 5ECF77FBBA for ; Fri, 2 Feb 2024 20:39:54 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.215.179 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706906396; cv=none; b=DWscBSK7wzuhg/qTyK0/xgUnDu7pQrl+qj9LV0OUXu8wHHO/25Hr7B2d4VbgxpRxMzlqiK8bD9PeGxlDP4i3EmC9UQRdKJjGZBJHZwIOymNN+214LChqv7SFSbOyuAc01Ds4APjuKEhhscFr45uaFALqqrQKIVjt+FJSCRCQDJA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706906396; c=relaxed/simple; bh=e3ymkD6zZ+VrSZBKaRGDFyMQBo150T8Ix5MIlSXba7o=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=QVdAXsMBm7dr8Ds3UX6umm9sE0h/B8yolmoYWN4sGPyuRubwhLKFs8kX9vEVwuJsZaNgZ2wjXRAn3syJeMxuDnS8RD+pnJJKkEtUl0tlQ72Ow9JQdx7PQz5W5cJQAU34iK1h7VfiwZZjG0K1vi+5CYDD80zNnRzr8yBkI9jP39w= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=fail (p=none dis=none) header.from=acm.org; spf=pass smtp.mailfrom=gmail.com; arc=none smtp.client-ip=209.85.215.179 Authentication-Results: smtp.subspace.kernel.org; dmarc=fail (p=none dis=none) header.from=acm.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Received: by mail-pg1-f179.google.com with SMTP id 41be03b00d2f7-5d7005ea1d0so887746a12.1 for ; Fri, 02 Feb 2024 12:39:54 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1706906394; x=1707511194; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=tW8D9CdFkptVzP2MNKckbivPDn5zWzX1RDd8apJ2rlk=; b=U8abE36Ptkb8OjFD1y5t9n4T687ysuam1KRj9fH1DDkS6lanaQsAdEEsbK/Jj2oUtx Cd6hY0VvqgaN1nPlKvBcfhiCs2/hcZbLOy9givV6njU4a3aWZNocgYsYSCkdEWNQHCfS djA80xfQetaDGKNX/0UamnzT/gCBqDBZW1h6LrQezv1+Fxc080y1AKHM8wneqvLbSA7+ ukRv8FoQk4t+W8GDDn902bfPrp5dgCSHhCYmz5Ii+zr2NORSct5owGtxq76/0VYgH/CP h9iEJVdXgZbpZpZ5O2fn9sIKFlJ9CytNBEm4BtQK3jk8uwIgbAelnaGkrpfYXZHdKLZk PPpQ== X-Gm-Message-State: AOJu0YyTxco4QyQ6Mf4Qg8TzHQZWVG9mA9SvHmL/SN677chR7oHd/j4p 6ooH2IvZ5mWGT2vQr0GF+Y1MJCxvEURxXSLn67V7W8uZ33VnsniK X-Google-Smtp-Source: AGHT+IHTyOa0G3KPp3efLJ79Fc7ey0F5KIJ5GGruApV1QRgyJL6tXsRxZm4Rvq/9Ll89oOm1e59w3g== X-Received: by 2002:a05:6a20:a123:b0:19e:4833:8b73 with SMTP id q35-20020a056a20a12300b0019e48338b73mr352393pzk.12.1706906393593; Fri, 02 Feb 2024 12:39:53 -0800 (PST) X-Forwarded-Encrypted: i=0; AJvYcCWEfh0GqdJGjQmpGeNS2lS5uz56i5mhb+PF0SBQo8BXOWJKcwadiSQ32cZmRxHTGeFq87HLZCTZ5r8atpWYtlMdQD+pHmXP0Wk9FjDIZ5Md1HDU5QPoIU6bSMa6di4fQHITHSHDqwhoRaOfXhZtHIXw6NvT/X+sK3SyV8fxkjhHi/hmioWMTYPZ51G4zA9bgifijqa8qBlrsHHEWadHGnxjwkXlFasxpQieAmSWBqYk5wDERg== Received: from bvanassche-linux.mtv.corp.google.com ([2620:0:1000:8411:4cc3:4ab5:7d2:ddc7]) by smtp.gmail.com with ESMTPSA id f8-20020a63de08000000b005d8aef12380sm2239678pgg.73.2024.02.02.12.39.52 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 02 Feb 2024 12:39:53 -0800 (PST) From: Bart Van Assche To: Christian Brauner , Alexander Viro Cc: linux-fsdevel@vger.kernel.org, Jens Axboe , Christoph Hellwig , "Martin K . Petersen" , Bart Van Assche , Chao Yu , Jan Kara Subject: [PATCH v2 4/6] fs: Move enum rw_hint into a new header file Date: Fri, 2 Feb 2024 12:39:23 -0800 Message-ID: <20240202203926.2478590-5-bvanassche@acm.org> X-Mailer: git-send-email 2.43.0.594.gd9cf4e227d-goog In-Reply-To: <20240202203926.2478590-1-bvanassche@acm.org> References: <20240202203926.2478590-1-bvanassche@acm.org> Precedence: bulk X-Mailing-List: linux-fsdevel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Move enum rw_hint into a new header file to prepare for using this data type in the block layer. Add the attribute __packed to reduce the space occupied by instances of this data type from four bytes to one byte. Change the data type of i_write_hint from u8 into enum rw_hint. Reviewed-by: Christoph Hellwig Acked-by: Chao Yu # for the F2FS part Cc: Alexander Viro Cc: Christian Brauner Cc: Jan Kara Cc: Christoph Hellwig Signed-off-by: Bart Van Assche --- fs/f2fs/f2fs.h | 1 + fs/fcntl.c | 1 + fs/inode.c | 1 + include/linux/fs.h | 16 ++-------------- include/linux/rw_hint.h | 24 ++++++++++++++++++++++++ 5 files changed, 29 insertions(+), 14 deletions(-) create mode 100644 include/linux/rw_hint.h diff --git a/fs/f2fs/f2fs.h b/fs/f2fs/f2fs.h index dda0aed95464..4a4e60cdac4e 100644 --- a/fs/f2fs/f2fs.h +++ b/fs/f2fs/f2fs.h @@ -24,6 +24,7 @@ #include #include #include +#include #include #include diff --git a/fs/fcntl.c b/fs/fcntl.c index d2b15351ab8e..00be0a710bba 100644 --- a/fs/fcntl.c +++ b/fs/fcntl.c @@ -27,6 +27,7 @@ #include #include #include +#include #include #include diff --git a/fs/inode.c b/fs/inode.c index 6d0d54230363..585d79d40158 100644 --- a/fs/inode.c +++ b/fs/inode.c @@ -20,6 +20,7 @@ #include #include #include +#include #include #include "internal.h" diff --git a/include/linux/fs.h b/include/linux/fs.h index 3ce900e34f52..4a953eb8b0d1 100644 --- a/include/linux/fs.h +++ b/include/linux/fs.h @@ -43,6 +43,7 @@ #include #include #include +#include #include #include @@ -309,19 +310,6 @@ struct address_space; struct writeback_control; struct readahead_control; -/* - * Write life time hint values. - * Stored in struct inode as u8. - */ -enum rw_hint { - WRITE_LIFE_NOT_SET = 0, - WRITE_LIFE_NONE = RWH_WRITE_LIFE_NONE, - WRITE_LIFE_SHORT = RWH_WRITE_LIFE_SHORT, - WRITE_LIFE_MEDIUM = RWH_WRITE_LIFE_MEDIUM, - WRITE_LIFE_LONG = RWH_WRITE_LIFE_LONG, - WRITE_LIFE_EXTREME = RWH_WRITE_LIFE_EXTREME, -}; - /* Match RWF_* bits to IOCB bits */ #define IOCB_HIPRI (__force int) RWF_HIPRI #define IOCB_DSYNC (__force int) RWF_DSYNC @@ -677,7 +665,7 @@ struct inode { spinlock_t i_lock; /* i_blocks, i_bytes, maybe i_size */ unsigned short i_bytes; u8 i_blkbits; - u8 i_write_hint; + enum rw_hint i_write_hint; blkcnt_t i_blocks; #ifdef __NEED_I_SIZE_ORDERED diff --git a/include/linux/rw_hint.h b/include/linux/rw_hint.h new file mode 100644 index 000000000000..309ca72f2dfb --- /dev/null +++ b/include/linux/rw_hint.h @@ -0,0 +1,24 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#ifndef _LINUX_RW_HINT_H +#define _LINUX_RW_HINT_H + +#include +#include +#include + +/* Block storage write lifetime hint values. */ +enum rw_hint { + WRITE_LIFE_NOT_SET = RWH_WRITE_LIFE_NOT_SET, + WRITE_LIFE_NONE = RWH_WRITE_LIFE_NONE, + WRITE_LIFE_SHORT = RWH_WRITE_LIFE_SHORT, + WRITE_LIFE_MEDIUM = RWH_WRITE_LIFE_MEDIUM, + WRITE_LIFE_LONG = RWH_WRITE_LIFE_LONG, + WRITE_LIFE_EXTREME = RWH_WRITE_LIFE_EXTREME, +} __packed; + +/* Sparse ignores __packed annotations on enums, hence the #ifndef below. */ +#ifndef __CHECKER__ +static_assert(sizeof(enum rw_hint) == 1); +#endif + +#endif /* _LINUX_RW_HINT_H */ From patchwork Fri Feb 2 20:39:24 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bart Van Assche X-Patchwork-Id: 13543361 Received: from mail-pg1-f178.google.com (mail-pg1-f178.google.com [209.85.215.178]) (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 675DC7C6CE for ; Fri, 2 Feb 2024 20:39:55 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.215.178 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706906396; cv=none; b=lDUILScEKXMBLDlR2Z293GydwH4LtqynyEpZ+u4Liz1UXlDxPohkUa17fIuGmtXWHlL6iHhPcKF+f0mtDI1K2StvqkOigIWY5CCTAZP4xOXbuDqQ4aHdMaiPqwtTox6pnQJOt10P+a5Ao+qPiQK25FWqa4qyUqqNcg3TrsXvt88= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706906396; c=relaxed/simple; bh=rN1qgUmr/T0syCmx7kkbGHIYOGFl1jyWjb1TKufdMCs=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=fEN82UaWvYugXqWme5O/GECn3SOPt3S+e2rSrciJJWcldKRC7oURzknWBDPkHUH2Ev4FyS4mODIljnB/loNPhLgBeiFmZ9NLhsr8eBVguvgdCThGkvfQ5yfDoYQPwL6xgvaiglh9OWW90zpqSZn+O+84zTNfm5kwcgYdg2qdiCw= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=fail (p=none dis=none) header.from=acm.org; spf=pass smtp.mailfrom=gmail.com; arc=none smtp.client-ip=209.85.215.178 Authentication-Results: smtp.subspace.kernel.org; dmarc=fail (p=none dis=none) header.from=acm.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Received: by mail-pg1-f178.google.com with SMTP id 41be03b00d2f7-53fbf2c42bfso2053503a12.3 for ; Fri, 02 Feb 2024 12:39:55 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1706906395; x=1707511195; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=7SiZkNE1v6XzMx7eeMAJZVstqO211OcktCVNeMOZAwY=; b=xJPziBQJ+YJ/EA2NrrL+sM5Yaz516u7Md+OAj8hVClWWlnOjHk6JNp2BRUwKMuYIU4 5C3o8uswJkn8wTgfzPbprQlExdhSaikDNYqpBQQuCSQJZ1TnouD3NR/d5RDgqygBRav6 y8wb2ntjBdjnDuIakDSFznKWYeG6X1A9lfBUvTqJOFw89zp+RyUNfmhfkCMbED6H65k5 sudhB0mVcQ39qWaSArI7sZMmA1kNp1WpjqxmnBmwjgJ4/zBct8zlNaNNzier2FxdtyaQ VYwapXmHkT7Iw29ZzNHzSh5NuKhOb7kNUHwhAEklSDurHcSaVo0r21SaMBaenZHoTKCU KI2Q== X-Gm-Message-State: AOJu0YzyRzpUESONbB04UOjPjBVY2WltQcbhg6Tv+EEj5HbMaWAEn8XD rCom7kBeLvaq8Vi+g8Rn3XTu+t8+lmYQk2k94pT767XJLdTpnr0X X-Google-Smtp-Source: AGHT+IE/ehT9S8XOB4W313tHi41jjWmAMJ8GypHaBU/yJ/t/Ljdm2u95PKJCBiKCSkBx4SoNSNeryg== X-Received: by 2002:a05:6a20:93a7:b0:19e:4e58:5026 with SMTP id x39-20020a056a2093a700b0019e4e585026mr3429805pzh.4.1706906394779; Fri, 02 Feb 2024 12:39:54 -0800 (PST) X-Forwarded-Encrypted: i=0; AJvYcCXoW9o83NMK+HaZMvie+BLaf8jj7qq+AMOoD13R+HfjHRufNAYe1UYQHLG2FMW4cRmGB/aBuNOfBnj6oXjpaBaz/ZZHHR3tKGZjYYlqgOrqoNdIGuyqK/L3D/tskEUuJzELth1JizgeouM0n9QS8Yh5bifx1kIb3miwlfl44SCwpYvBOFfCZtTVAcw9P4ScVgtqdO3kxQSaEgE5aojmhfrmgC58HGTSZ0HKoEhMOx0WpLqLV897b6rVLtr/dWySnyJ0BvM8AdRerynFjXrpfqClSrwLdoY= Received: from bvanassche-linux.mtv.corp.google.com ([2620:0:1000:8411:4cc3:4ab5:7d2:ddc7]) by smtp.gmail.com with ESMTPSA id f8-20020a63de08000000b005d8aef12380sm2239678pgg.73.2024.02.02.12.39.53 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 02 Feb 2024 12:39:54 -0800 (PST) From: Bart Van Assche To: Christian Brauner , Alexander Viro Cc: linux-fsdevel@vger.kernel.org, Jens Axboe , Christoph Hellwig , "Martin K . Petersen" , Bart Van Assche , Jeff Layton , Chuck Lever , Kanchan Joshi Subject: [PATCH v2 5/6] fs: Propagate write hints to the struct block_device inode Date: Fri, 2 Feb 2024 12:39:24 -0800 Message-ID: <20240202203926.2478590-6-bvanassche@acm.org> X-Mailer: git-send-email 2.43.0.594.gd9cf4e227d-goog In-Reply-To: <20240202203926.2478590-1-bvanassche@acm.org> References: <20240202203926.2478590-1-bvanassche@acm.org> Precedence: bulk X-Mailing-List: linux-fsdevel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Write hints applied with F_SET_RW_HINT on a block device affect the block device inode only. Propagate these hints to the inode associated with struct block_device because that is the inode used when writing back dirty pages. Cc: Alexander Viro Cc: Christian Brauner Cc: Jens Axboe Cc: Christoph Hellwig Cc: Jeff Layton Cc: Chuck Lever Cc: Kanchan Joshi Signed-off-by: Bart Van Assche --- fs/fcntl.c | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/fs/fcntl.c b/fs/fcntl.c index 00be0a710bba..8eb6d64a985b 100644 --- a/fs/fcntl.c +++ b/fs/fcntl.c @@ -317,6 +317,13 @@ static long fcntl_set_rw_hint(struct file *file, unsigned int cmd, WRITE_ONCE(inode->i_write_hint, hint); + /* + * file->f_mapping->host may differ from inode. As an example, + * blkdev_open() modifies file->f_mapping. + */ + if (file->f_mapping->host != inode) + WRITE_ONCE(file->f_mapping->host->i_write_hint, hint); + return 0; } From patchwork Fri Feb 2 20:39:25 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bart Van Assche X-Patchwork-Id: 13543363 Received: from mail-pf1-f182.google.com (mail-pf1-f182.google.com [209.85.210.182]) (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 70D4B48788 for ; Fri, 2 Feb 2024 20:39:56 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.182 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706906398; cv=none; b=vGq//D+Qgk7XixhzLdiPUp+eePrW2Z/Ulqao1EFcL7L5+g1xZ6xfw2uFNw3W9KbobqrBv4thWE68Cwq+yLLQhmG04ogbnxNjN/ljRP3Zrur5xkp++8uS0Lw2wep51+iUp3yh7CQE2Q1Ie2PvJlW/b7EVdKU4vTOKMXvlHejIAUY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706906398; c=relaxed/simple; bh=L41fARirgn/wpv0HQOnGJFpqwe9PbkfjMM0eV6TIcoY=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=DGE3h44oPWBZa3XlJjYcZW77d3yERTzLteMoZeb7tzfQ9GOckCrNmObO/42e6qVUG9VjdaCaddaZZ9cUD4FyG1Hqt+B+6RxrEUqyc+3SYnBz42f3PnxphSnxzYbeV+U1XUIoum3uNMs7TbPQY4znqqKqqeeW6ICtXLWvcMpuBT8= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=fail (p=none dis=none) header.from=acm.org; spf=pass smtp.mailfrom=gmail.com; arc=none smtp.client-ip=209.85.210.182 Authentication-Results: smtp.subspace.kernel.org; dmarc=fail (p=none dis=none) header.from=acm.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Received: by mail-pf1-f182.google.com with SMTP id d2e1a72fcca58-6ddc5faeb7fso2095781b3a.3 for ; Fri, 02 Feb 2024 12:39:56 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1706906396; x=1707511196; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=JgOcNYI3tDvpYYgg4ArGvQYp6ynf6FHn8ecqLAA6XjY=; b=Wlg9vhUvuw4cHVG8ewN3kUhlOXbpk3zXx2bWX0LoEJyMBICfIQhUqpjkakdOtcenaC 8L9hitgk/oPLlOc9xxpzWG2W6jh8dEyd/n5Xcs8E/7EELxzKyQIU7G+C63m8Km9A/cXx 698Wp7VQBwHyxMuMPfk9+9eMxdWBtGZtVohhl3D7TUJrivmKSMKelF5Yq4flRj3cJCkK V3ZCL5+dJbvtsUSPHZY1BSUGqxJhiiu7S72iGjFK6oX0lfz8khGa2ZM5hqtyAc2tq0o5 laf846exDDZDHUBxrvrBctq3N5npCv0viFPf4k99vuAIhw44P3+NSCQ7KE2iOoPyqley yHLw== X-Gm-Message-State: AOJu0YyKLU2BBGmD9WtX9LAmDCjLPcnEhAkODsNlSpLO/VRyHtsX8m9r VK0XjvcZZBzqsIlwd3slqIoZLKZS7YtLqImgUji4HTC5xv4UDOUi X-Google-Smtp-Source: AGHT+IFRBccei7Oqds+FaxYrlx1H5uJBR0HaGEBYCTkfYYm8yNrIeTAvARw60uvuq+63SxvLjr+W4A== X-Received: by 2002:a05:6a21:2c82:b0:19c:9d4d:7d7 with SMTP id ua2-20020a056a212c8200b0019c9d4d07d7mr8085928pzb.41.1706906395802; Fri, 02 Feb 2024 12:39:55 -0800 (PST) X-Forwarded-Encrypted: i=0; AJvYcCUD6ME7O/gsSnfMH6DcM3VX+e7/v0vHcB8z5qJ32d7JBGAnh5NTILImRcSVnYTzJPhsivPljWin/rHZhG4E3HeRKeXwrveREiZEFHGi0iTHRDnSugYUe1whlHMqFA9cJbTxOSBVqNhs4njjkV6ZbMv5fDnypV5VnLHN5+GVtz+Z+is7oSM5aRDcjc7o1DXMpSqYlyETYhL7VNz5hKTu/L03PYBd8vJnIyxZ Received: from bvanassche-linux.mtv.corp.google.com ([2620:0:1000:8411:4cc3:4ab5:7d2:ddc7]) by smtp.gmail.com with ESMTPSA id f8-20020a63de08000000b005d8aef12380sm2239678pgg.73.2024.02.02.12.39.54 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 02 Feb 2024 12:39:55 -0800 (PST) From: Bart Van Assche To: Christian Brauner , Alexander Viro Cc: linux-fsdevel@vger.kernel.org, Jens Axboe , Christoph Hellwig , "Martin K . Petersen" , Bart Van Assche , Kanchan Joshi Subject: [PATCH v2 6/6] block, fs: Restore the per-bio/request data lifetime fields Date: Fri, 2 Feb 2024 12:39:25 -0800 Message-ID: <20240202203926.2478590-7-bvanassche@acm.org> X-Mailer: git-send-email 2.43.0.594.gd9cf4e227d-goog In-Reply-To: <20240202203926.2478590-1-bvanassche@acm.org> References: <20240202203926.2478590-1-bvanassche@acm.org> Precedence: bulk X-Mailing-List: linux-fsdevel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Restore support for passing data lifetime information from filesystems to block drivers. This patch reverts commit b179c98f7697 ("block: Remove request.write_hint") and commit c75e707fe1aa ("block: remove the per-bio/request write hint"). This patch does not modify the size of struct bio because the new bi_write_hint member fills a hole in struct bio. pahole reports the following for struct bio on an x86_64 system with this patch applied: /* size: 112, cachelines: 2, members: 20 */ /* sum members: 110, holes: 1, sum holes: 2 */ /* last cacheline: 48 bytes */ Reviewed-by: Kanchan Joshi Cc: Jens Axboe Cc: Christoph Hellwig Signed-off-by: Bart Van Assche --- block/bio.c | 2 ++ block/blk-crypto-fallback.c | 1 + block/blk-merge.c | 8 ++++++++ block/blk-mq.c | 2 ++ block/bounce.c | 1 + block/fops.c | 3 +++ fs/buffer.c | 12 ++++++++---- fs/direct-io.c | 2 ++ fs/iomap/buffered-io.c | 1 + fs/iomap/direct-io.c | 1 + fs/mpage.c | 1 + include/linux/blk-mq.h | 2 ++ include/linux/blk_types.h | 2 ++ 13 files changed, 34 insertions(+), 4 deletions(-) diff --git a/block/bio.c b/block/bio.c index b9642a41f286..c9223e9d31da 100644 --- a/block/bio.c +++ b/block/bio.c @@ -251,6 +251,7 @@ void bio_init(struct bio *bio, struct block_device *bdev, struct bio_vec *table, bio->bi_opf = opf; bio->bi_flags = 0; bio->bi_ioprio = 0; + bio->bi_write_hint = 0; bio->bi_status = 0; bio->bi_iter.bi_sector = 0; bio->bi_iter.bi_size = 0; @@ -813,6 +814,7 @@ static int __bio_clone(struct bio *bio, struct bio *bio_src, gfp_t gfp) { bio_set_flag(bio, BIO_CLONED); bio->bi_ioprio = bio_src->bi_ioprio; + bio->bi_write_hint = bio_src->bi_write_hint; bio->bi_iter = bio_src->bi_iter; if (bio->bi_bdev) { diff --git a/block/blk-crypto-fallback.c b/block/blk-crypto-fallback.c index e6468eab2681..b1e7415f8439 100644 --- a/block/blk-crypto-fallback.c +++ b/block/blk-crypto-fallback.c @@ -172,6 +172,7 @@ static struct bio *blk_crypto_fallback_clone_bio(struct bio *bio_src) if (bio_flagged(bio_src, BIO_REMAPPED)) bio_set_flag(bio, BIO_REMAPPED); bio->bi_ioprio = bio_src->bi_ioprio; + bio->bi_write_hint = bio_src->bi_write_hint; bio->bi_iter.bi_sector = bio_src->bi_iter.bi_sector; bio->bi_iter.bi_size = bio_src->bi_iter.bi_size; diff --git a/block/blk-merge.c b/block/blk-merge.c index 2d470cf2173e..2a06fd33039d 100644 --- a/block/blk-merge.c +++ b/block/blk-merge.c @@ -810,6 +810,10 @@ static struct request *attempt_merge(struct request_queue *q, if (rq_data_dir(req) != rq_data_dir(next)) return NULL; + /* Don't merge requests with different write hints. */ + if (req->write_hint != next->write_hint) + return NULL; + if (req->ioprio != next->ioprio) return NULL; @@ -937,6 +941,10 @@ bool blk_rq_merge_ok(struct request *rq, struct bio *bio) if (!bio_crypt_rq_ctx_compatible(rq, bio)) return false; + /* Don't merge requests with different write hints. */ + if (rq->write_hint != bio->bi_write_hint) + return false; + if (rq->ioprio != bio_prio(bio)) return false; diff --git a/block/blk-mq.c b/block/blk-mq.c index aa87fcfda1ec..34ceb15d2ea4 100644 --- a/block/blk-mq.c +++ b/block/blk-mq.c @@ -2585,6 +2585,7 @@ static void blk_mq_bio_to_request(struct request *rq, struct bio *bio, rq->cmd_flags |= REQ_FAILFAST_MASK; rq->__sector = bio->bi_iter.bi_sector; + rq->write_hint = bio->bi_write_hint; blk_rq_bio_prep(rq, bio, nr_segs); /* This can't fail, since GFP_NOIO includes __GFP_DIRECT_RECLAIM. */ @@ -3185,6 +3186,7 @@ int blk_rq_prep_clone(struct request *rq, struct request *rq_src, } rq->nr_phys_segments = rq_src->nr_phys_segments; rq->ioprio = rq_src->ioprio; + rq->write_hint = rq_src->write_hint; if (rq->bio && blk_crypto_rq_bio_prep(rq, rq->bio, gfp_mask) < 0) goto free_and_out; diff --git a/block/bounce.c b/block/bounce.c index 7cfcb242f9a1..d6a5219f29dd 100644 --- a/block/bounce.c +++ b/block/bounce.c @@ -169,6 +169,7 @@ static struct bio *bounce_clone_bio(struct bio *bio_src) if (bio_flagged(bio_src, BIO_REMAPPED)) bio_set_flag(bio, BIO_REMAPPED); bio->bi_ioprio = bio_src->bi_ioprio; + bio->bi_write_hint = bio_src->bi_write_hint; bio->bi_iter.bi_sector = bio_src->bi_iter.bi_sector; bio->bi_iter.bi_size = bio_src->bi_iter.bi_size; diff --git a/block/fops.c b/block/fops.c index 93bae17ce660..63566a3db186 100644 --- a/block/fops.c +++ b/block/fops.c @@ -73,6 +73,7 @@ static ssize_t __blkdev_direct_IO_simple(struct kiocb *iocb, bio_init(&bio, bdev, vecs, nr_pages, dio_bio_write_op(iocb)); } bio.bi_iter.bi_sector = pos >> SECTOR_SHIFT; + bio.bi_write_hint = file_inode(iocb->ki_filp)->i_write_hint; bio.bi_ioprio = iocb->ki_ioprio; ret = bio_iov_iter_get_pages(&bio, iter); @@ -203,6 +204,7 @@ static ssize_t __blkdev_direct_IO(struct kiocb *iocb, struct iov_iter *iter, for (;;) { bio->bi_iter.bi_sector = pos >> SECTOR_SHIFT; + bio->bi_write_hint = file_inode(iocb->ki_filp)->i_write_hint; bio->bi_private = dio; bio->bi_end_io = blkdev_bio_end_io; bio->bi_ioprio = iocb->ki_ioprio; @@ -321,6 +323,7 @@ static ssize_t __blkdev_direct_IO_async(struct kiocb *iocb, dio->flags = 0; dio->iocb = iocb; bio->bi_iter.bi_sector = pos >> SECTOR_SHIFT; + bio->bi_write_hint = file_inode(iocb->ki_filp)->i_write_hint; bio->bi_end_io = blkdev_bio_end_io_async; bio->bi_ioprio = iocb->ki_ioprio; diff --git a/fs/buffer.c b/fs/buffer.c index b55dea034a5d..d6b64124977a 100644 --- a/fs/buffer.c +++ b/fs/buffer.c @@ -55,7 +55,7 @@ static int fsync_buffers_list(spinlock_t *lock, struct list_head *list); static void submit_bh_wbc(blk_opf_t opf, struct buffer_head *bh, - struct writeback_control *wbc); + enum rw_hint hint, struct writeback_control *wbc); #define BH_ENTRY(list) list_entry((list), struct buffer_head, b_assoc_buffers) @@ -1889,7 +1889,8 @@ int __block_write_full_folio(struct inode *inode, struct folio *folio, do { struct buffer_head *next = bh->b_this_page; if (buffer_async_write(bh)) { - submit_bh_wbc(REQ_OP_WRITE | write_flags, bh, wbc); + submit_bh_wbc(REQ_OP_WRITE | write_flags, bh, + inode->i_write_hint, wbc); nr_underway++; } bh = next; @@ -1944,7 +1945,8 @@ int __block_write_full_folio(struct inode *inode, struct folio *folio, struct buffer_head *next = bh->b_this_page; if (buffer_async_write(bh)) { clear_buffer_dirty(bh); - submit_bh_wbc(REQ_OP_WRITE | write_flags, bh, wbc); + submit_bh_wbc(REQ_OP_WRITE | write_flags, bh, + inode->i_write_hint, wbc); nr_underway++; } bh = next; @@ -2756,6 +2758,7 @@ static void end_bio_bh_io_sync(struct bio *bio) } static void submit_bh_wbc(blk_opf_t opf, struct buffer_head *bh, + enum rw_hint write_hint, struct writeback_control *wbc) { const enum req_op op = opf & REQ_OP_MASK; @@ -2783,6 +2786,7 @@ static void submit_bh_wbc(blk_opf_t opf, struct buffer_head *bh, fscrypt_set_bio_crypt_ctx_bh(bio, bh, GFP_NOIO); bio->bi_iter.bi_sector = bh->b_blocknr * (bh->b_size >> 9); + bio->bi_write_hint = write_hint; __bio_add_page(bio, bh->b_page, bh->b_size, bh_offset(bh)); @@ -2802,7 +2806,7 @@ static void submit_bh_wbc(blk_opf_t opf, struct buffer_head *bh, void submit_bh(blk_opf_t opf, struct buffer_head *bh) { - submit_bh_wbc(opf, bh, NULL); + submit_bh_wbc(opf, bh, WRITE_LIFE_NOT_SET, NULL); } EXPORT_SYMBOL(submit_bh); diff --git a/fs/direct-io.c b/fs/direct-io.c index 60456263a338..62c97ff9e852 100644 --- a/fs/direct-io.c +++ b/fs/direct-io.c @@ -410,6 +410,8 @@ dio_bio_alloc(struct dio *dio, struct dio_submit *sdio, bio->bi_end_io = dio_bio_end_io; if (dio->is_pinned) bio_set_flag(bio, BIO_PAGE_PINNED); + bio->bi_write_hint = file_inode(dio->iocb->ki_filp)->i_write_hint; + sdio->bio = bio; sdio->logical_offset_in_bio = sdio->cur_page_fs_offset; } diff --git a/fs/iomap/buffered-io.c b/fs/iomap/buffered-io.c index 2ad0e287c704..7f3ee4112e2a 100644 --- a/fs/iomap/buffered-io.c +++ b/fs/iomap/buffered-io.c @@ -1689,6 +1689,7 @@ static struct iomap_ioend *iomap_alloc_ioend(struct iomap_writepage_ctx *wpc, GFP_NOFS, &iomap_ioend_bioset); bio->bi_iter.bi_sector = iomap_sector(&wpc->iomap, pos); bio->bi_end_io = iomap_writepage_end_bio; + bio->bi_write_hint = inode->i_write_hint; wbc_init_bio(wbc, bio); ioend = iomap_ioend_from_bio(bio); diff --git a/fs/iomap/direct-io.c b/fs/iomap/direct-io.c index bcd3f8cf5ea4..f3b43d223a46 100644 --- a/fs/iomap/direct-io.c +++ b/fs/iomap/direct-io.c @@ -380,6 +380,7 @@ static loff_t iomap_dio_bio_iter(const struct iomap_iter *iter, fscrypt_set_bio_crypt_ctx(bio, inode, pos >> inode->i_blkbits, GFP_KERNEL); bio->bi_iter.bi_sector = iomap_sector(iomap, pos); + bio->bi_write_hint = inode->i_write_hint; bio->bi_ioprio = dio->iocb->ki_ioprio; bio->bi_private = dio; bio->bi_end_io = iomap_dio_bio_end_io; diff --git a/fs/mpage.c b/fs/mpage.c index 738882e0766d..fa8b99a199fa 100644 --- a/fs/mpage.c +++ b/fs/mpage.c @@ -605,6 +605,7 @@ static int __mpage_writepage(struct folio *folio, struct writeback_control *wbc, GFP_NOFS); bio->bi_iter.bi_sector = first_block << (blkbits - 9); wbc_init_bio(wbc, bio); + bio->bi_write_hint = inode->i_write_hint; } /* diff --git a/include/linux/blk-mq.h b/include/linux/blk-mq.h index 7a8150a5f051..492b0128b5d9 100644 --- a/include/linux/blk-mq.h +++ b/include/linux/blk-mq.h @@ -8,6 +8,7 @@ #include #include #include +#include struct blk_mq_tags; struct blk_flush_queue; @@ -135,6 +136,7 @@ struct request { struct blk_crypto_keyslot *crypt_keyslot; #endif + enum rw_hint write_hint; unsigned short ioprio; enum mq_rq_state state; diff --git a/include/linux/blk_types.h b/include/linux/blk_types.h index f288c94374b3..12d87cef2c03 100644 --- a/include/linux/blk_types.h +++ b/include/linux/blk_types.h @@ -10,6 +10,7 @@ #include #include #include +#include struct bio_set; struct bio; @@ -269,6 +270,7 @@ struct bio { */ unsigned short bi_flags; /* BIO_* below */ unsigned short bi_ioprio; + enum rw_hint bi_write_hint; blk_status_t bi_status; atomic_t __bi_remaining;