Received: by 2002:a05:7412:bbc7:b0:fc:a2b0:25d7 with SMTP id kh7csp572279rdb; Thu, 1 Feb 2024 18:21:25 -0800 (PST) X-Google-Smtp-Source: AGHT+IFqggkzec403hWZQOhPEuG7YIb5fIR4OCn+c5XlBWeju0JIAiWCN3Mig39HHGinYrb/k6BC X-Received: by 2002:a0c:d98e:0:b0:68c:7a2f:1c0b with SMTP id y14-20020a0cd98e000000b0068c7a2f1c0bmr564115qvj.62.1706840484918; Thu, 01 Feb 2024 18:21:24 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1706840484; cv=pass; d=google.com; s=arc-20160816; b=cd8TudqRKXXzz4Kt8sE6JJQdS/BeKshaUnHPgbk+wQKyBKNr/BcPXoJppe+6mMJuFN +MvAYHiZqCQqUHWswyksLgfWfGfTWOkHIjZ4xg039qeuazktvXtGCryYbXxqk9DOwmtc Y51/EqNxxmAcUDfMgk3RyQO9a6C6PwPpxtUZNHOGI7pUM//KRGGUfEEAKo+q8KBYIFOO w5TZJdETsnylDyfxNtmHKcyOcnZjpFuE0JUqSPNWMlJg9BcvGolsy9bR523R7A287HwH aCdFboODR7y6oaYaMyLs6RtzO2mnrX47akgEdjijyIp/HSZD+Lo6WL9lau0OJ4hg9kgT uKKw== 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=/AFNq394fKdD6rutt1qh+bLin+C/DGw45kLcKx0YqTM=; fh=LMgQXFErBlufmQVlqCjk6me9qAfHWwo5wmaHfhaiUU0=; b=wezRNadw/UqL7UGG0tXWbcaDtqfeaMtB/KoqMrDPju15JdXdLovs8QANE8fL43FZFy rcR/qv8Mo4awzExNRc07G+RFV2aeB8XocF8KYMbSMsYk310hNII2T7+6Rk8WAqrJHf0Q gJn6ng55j0TGA7FlYH9al20RmBk+zKDTjoXxROAf3tRFMGFq0Okg5ptKZTzQaGYcKHk3 +1nm0FCYJtdDuj1YEt6pn2e/dgNNQyBxBhuTYmyQXHvl2owpKhjnOHZfpiEldybFQ4WI KpcgBltG34AW2+GgNGJQungxl8htbdrHvT01F+BBYDRTIG8nc9Hgspxs/ToixKAnrYLr PiGQ==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=crslRzn8; arc=pass (i=1 spf=pass spfdomain=redhat.com dkim=pass dkdomain=redhat.com dmarc=pass fromdomain=redhat.com); spf=pass (google.com: domain of linux-kernel+bounces-49086-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-49086-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com X-Forwarded-Encrypted: i=1; AJvYcCUJQ00cS9xcwH1r4XGWS7Gqct+FBxg6+pb3yIBwIjnoR0fa7Epksi568bYTg/URdynqqTiMppx9FBD3+AYVHUlSYPe270EM5B4FV4+qFA== Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [147.75.199.223]) by mx.google.com with ESMTPS id w20-20020a056214013400b00684638ab22dsi959488qvs.456.2024.02.01.18.21.24 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 01 Feb 2024 18:21:24 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-49086-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) client-ip=147.75.199.223; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=crslRzn8; arc=pass (i=1 spf=pass spfdomain=redhat.com dkim=pass dkdomain=redhat.com dmarc=pass fromdomain=redhat.com); spf=pass (google.com: domain of linux-kernel+bounces-49086-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-49086-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.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 960BD1C22BA1 for ; Fri, 2 Feb 2024 02:21:24 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id E0D1CC13E; Fri, 2 Feb 2024 02:21:06 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="crslRzn8" Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) (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 6D2CBBA48 for ; Fri, 2 Feb 2024 02:21:04 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.133.124 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706840466; cv=none; b=VZBLKbkl3tYHNB8i4FUaVgY4NNp01khhxignMOnREX5eFc0NEuIKX4JdY2/s1tTfDxD0a9J2dBtPyrdk0Ksn5aixtAYXgF+vWFNVV1yZkUJrsid2Y9UE/SLV7/K+i71hwnbC8adO8LvaMq+xm5bM9eeOxV40tFkEv2i6biJI3aI= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706840466; c=relaxed/simple; bh=EQIwbZHsGuMlZyQ+7R9MpEihML0bmDWknH+HvCa+3QI=; h=From:To:Cc:Subject:Date:Message-ID:MIME-Version; b=UppPgccqvHjFKFxSGS06bxYVQKon2wdLrOdn8mWLiCf6pmzeqQF14b9NrNbsLkDj7IJf45cD9yqWKXV4+I/13cLnDee9T3UavzT9rsBmbfQOAUNTwO0sIXECNpPGdPzabniQsAxaDkgesLMYsexVfoYSbEfeRehnkjJ76/AZ77A= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=crslRzn8; arc=none smtp.client-ip=170.10.133.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1706840463; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding; bh=/AFNq394fKdD6rutt1qh+bLin+C/DGw45kLcKx0YqTM=; b=crslRzn8TIDEYKtqWxAUJMegEew8Yoh1yPbu4l2l56NrRQa1fg3iIIXBCl49gSRezmOi7w q27LNdKljx+9HCt3D8RtXuGGnZpjeZ/laBf2Z4IVc1RsJeKxMvMeIJxTAk311wCYvBHyBX jiZeMRMZY9bpdSJcgLKWhKoDxfY3Qe8= Received: from mimecast-mx02.redhat.com (mx-ext.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-124-aBo1uFCGPYOOyb7-_Gf_gw-1; Thu, 01 Feb 2024 21:20:58 -0500 X-MC-Unique: aBo1uFCGPYOOyb7-_Gf_gw-1 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.rdu2.redhat.com [10.11.54.3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 975C729AC00A; Fri, 2 Feb 2024 02:20:57 +0000 (UTC) Received: from localhost (unknown [10.72.116.16]) by smtp.corp.redhat.com (Postfix) with ESMTP id ABCD01121306; Fri, 2 Feb 2024 02:20:56 +0000 (UTC) From: Ming Lei To: Andrew Morton , linux-mm@kvack.org, linux-fsdevel@vger.kernel.org Cc: linux-kernel@vger.kernel.org, Ming Lei , David Hildenbrand , Matthew Wilcox , Alexander Viro , Christian Brauner , Don Dutile , Rafael Aquini , Dave Chinner , Mike Snitzer Subject: [PATCH] mm/madvise: set ra_pages as device max request size during ADV_POPULATE_READ Date: Fri, 2 Feb 2024 10:20:29 +0800 Message-ID: <20240202022029.1903629-1-ming.lei@redhat.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.3 madvise(MADV_POPULATE_READ) tries to populate all page tables in the specific range, so it is usually sequential IO if VMA is backed by file. Set ra_pages as device max request size for the involved readahead in the ADV_POPULATE_READ, this way reduces latency of madvise(MADV_POPULATE_READ) to 1/10 when running madvise(MADV_POPULATE_READ) over one 1GB file with usual(default) 128KB of read_ahead_kb. Cc: David Hildenbrand Cc: Matthew Wilcox Cc: Alexander Viro Cc: Christian Brauner Cc: Don Dutile Cc: Rafael Aquini Cc: Dave Chinner Cc: Mike Snitzer Cc: Andrew Morton Signed-off-by: Ming Lei --- mm/madvise.c | 52 +++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 51 insertions(+), 1 deletion(-) diff --git a/mm/madvise.c b/mm/madvise.c index 912155a94ed5..db5452c8abdd 100644 --- a/mm/madvise.c +++ b/mm/madvise.c @@ -900,6 +900,37 @@ static long madvise_dontneed_free(struct vm_area_struct *vma, return -EINVAL; } +static void madvise_restore_ra_win(struct file **file, unsigned int ra_pages) +{ + if (*file) { + struct file *f = *file; + + f->f_ra.ra_pages = ra_pages; + fput(f); + *file = NULL; + } +} + +static struct file *madvise_override_ra_win(struct file *f, + unsigned long start, unsigned long end, + unsigned int *old_ra_pages) +{ + unsigned int io_pages; + + if (!f || !f->f_mapping || !f->f_mapping->host) + return NULL; + + io_pages = inode_to_bdi(f->f_mapping->host)->io_pages; + if (((end - start) >> PAGE_SHIFT) < io_pages) + return NULL; + + f = get_file(f); + *old_ra_pages = f->f_ra.ra_pages; + f->f_ra.ra_pages = io_pages; + + return f; +} + static long madvise_populate(struct vm_area_struct *vma, struct vm_area_struct **prev, unsigned long start, unsigned long end, @@ -908,9 +939,21 @@ static long madvise_populate(struct vm_area_struct *vma, const bool write = behavior == MADV_POPULATE_WRITE; struct mm_struct *mm = vma->vm_mm; unsigned long tmp_end; + unsigned int ra_pages; + struct file *file; int locked = 1; long pages; + /* + * In case of file backing mapping, increase readahead window + * for reducing the whole populate latency, and restore it + * after the populate is done + */ + if (behavior == MADV_POPULATE_READ) + file = madvise_override_ra_win(vma->vm_file, start, end, + &ra_pages); + else + file = NULL; *prev = vma; while (start < end) { @@ -920,8 +963,10 @@ static long madvise_populate(struct vm_area_struct *vma, */ if (!vma || start >= vma->vm_end) { vma = vma_lookup(mm, start); - if (!vma) + if (!vma) { + madvise_restore_ra_win(&file, ra_pages); return -ENOMEM; + } } tmp_end = min_t(unsigned long, end, vma->vm_end); @@ -935,6 +980,9 @@ static long madvise_populate(struct vm_area_struct *vma, vma = NULL; } if (pages < 0) { + /* restore ra pages back in case of any failure */ + madvise_restore_ra_win(&file, ra_pages); + switch (pages) { case -EINTR: return -EINTR; @@ -954,6 +1002,8 @@ static long madvise_populate(struct vm_area_struct *vma, } start += pages * PAGE_SIZE; } + + madvise_restore_ra_win(&file, ra_pages); return 0; } -- 2.41.0