From patchwork Thu Jul 18 10:59:00 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Oscar Salvador X-Patchwork-Id: 13736391 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 24F1EC3DA49 for ; Thu, 18 Jul 2024 10:59:34 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 3D6276B0098; Thu, 18 Jul 2024 06:59:24 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 3862A6B0099; Thu, 18 Jul 2024 06:59:24 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 1DA556B009A; Thu, 18 Jul 2024 06:59:24 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id EE4806B0098 for ; Thu, 18 Jul 2024 06:59:23 -0400 (EDT) Received: from smtpin28.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id B4A5B141A02 for ; Thu, 18 Jul 2024 10:59:23 +0000 (UTC) X-FDA: 82352577006.28.B8A60D2 Received: from smtp-out2.suse.de (smtp-out2.suse.de [195.135.223.131]) by imf14.hostedemail.com (Postfix) with ESMTP id 7FEDE100011 for ; Thu, 18 Jul 2024 10:59:21 +0000 (UTC) Authentication-Results: imf14.hostedemail.com; dkim=pass header.d=suse.de header.s=susede2_rsa header.b=z5eI1VcQ; dkim=pass header.d=suse.de header.s=susede2_ed25519 header.b=X4rbEJ+Y; dkim=pass header.d=suse.de header.s=susede2_rsa header.b=z5eI1VcQ; dkim=pass header.d=suse.de header.s=susede2_ed25519 header.b=X4rbEJ+Y; spf=pass (imf14.hostedemail.com: domain of osalvador@suse.de designates 195.135.223.131 as permitted sender) smtp.mailfrom=osalvador@suse.de; dmarc=pass (policy=none) header.from=suse.de ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1721300320; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=6sPGyH5z7l/pm0TX/hh9KWyWWpcRI/yb9hB1nzYK2f0=; b=ttByvhrTeGkqZtNsF+3/MlLUnK69A7NPJ1eTlBBD+e1Wv6M+ZQFQCV4I85fzykgyDJXXrW 1RseJEeK2wzmhFovh6v6CblZMg1oYwz7dlUtZF/WmW6nNoF9MbxpNfratwwebjMZ7HvctP U0/Pzjly6PMYFW5TBP17O/kJqnbXaiQ= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1721300320; a=rsa-sha256; cv=none; b=XiIKbE1v3/N2bK7lRzRvXLi4r9zuvemkRjTE5uiI0LVeDAV9tqoWbPAEqjy7rcvnzRe7/p a1qcttduuh1Nf7avxY5sZeMEr2IabgBw9YSKLE6pODObmq4PrnxedFMR4TFBK9Kctem0+y jQUZPUFUXyrUSgjdrTVh+0JlqqkNs/Q= ARC-Authentication-Results: i=1; imf14.hostedemail.com; dkim=pass header.d=suse.de header.s=susede2_rsa header.b=z5eI1VcQ; dkim=pass header.d=suse.de header.s=susede2_ed25519 header.b=X4rbEJ+Y; dkim=pass header.d=suse.de header.s=susede2_rsa header.b=z5eI1VcQ; dkim=pass header.d=suse.de header.s=susede2_ed25519 header.b=X4rbEJ+Y; spf=pass (imf14.hostedemail.com: domain of osalvador@suse.de designates 195.135.223.131 as permitted sender) smtp.mailfrom=osalvador@suse.de; dmarc=pass (policy=none) header.from=suse.de Received: from imap1.dmz-prg2.suse.org (unknown [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-out2.suse.de (Postfix) with ESMTPS id 407C91FBBE; Thu, 18 Jul 2024 10:59:20 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1721300360; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=6sPGyH5z7l/pm0TX/hh9KWyWWpcRI/yb9hB1nzYK2f0=; b=z5eI1VcQIrnLNx2Z0oK330o9OEByJyUoURwS49Ys44trBlZ/l7ix0vn9EWT1GWuyZGhjUs r2LtEo47jmzw0u57xl9/9M8Q4UxrQIwAjGjEYaGcxEfach1ESHp437POhkkzYPZCF5ZD9y Y2LHEodX4at6FpHiaDavKdKcccmTfqA= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1721300360; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=6sPGyH5z7l/pm0TX/hh9KWyWWpcRI/yb9hB1nzYK2f0=; b=X4rbEJ+Yn1vnB9FjpFiteKUi4ZJqJ/ZU1kpljjScD/+NNRgV18aVF/aYxaHV6hClFIckw8 3Yf5i9Bv93dYvSBg== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1721300360; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=6sPGyH5z7l/pm0TX/hh9KWyWWpcRI/yb9hB1nzYK2f0=; b=z5eI1VcQIrnLNx2Z0oK330o9OEByJyUoURwS49Ys44trBlZ/l7ix0vn9EWT1GWuyZGhjUs r2LtEo47jmzw0u57xl9/9M8Q4UxrQIwAjGjEYaGcxEfach1ESHp437POhkkzYPZCF5ZD9y Y2LHEodX4at6FpHiaDavKdKcccmTfqA= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1721300360; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=6sPGyH5z7l/pm0TX/hh9KWyWWpcRI/yb9hB1nzYK2f0=; b=X4rbEJ+Yn1vnB9FjpFiteKUi4ZJqJ/ZU1kpljjScD/+NNRgV18aVF/aYxaHV6hClFIckw8 3Yf5i9Bv93dYvSBg== 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 2F5E7136F7; Thu, 18 Jul 2024 10:59:19 +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 YEPXB4f1mGbnVAAAD6G6ig (envelope-from ); Thu, 18 Jul 2024 10:59:19 +0000 From: Oscar Salvador To: Andrew Morton Cc: linux-kernel@vger.kernel.org, linux-mm@kvack.org, Peter Xu , Muchun Song , Michal Hocko , Donet Tom , "Kirill A . Shutemov" , Matthew Wilcox , Vlastimil Babka , Oscar Salvador Subject: [PATCH 6/9] mm: Make hugetlb mappings go through mm_get_unmapped_area_vmflags Date: Thu, 18 Jul 2024 12:59:00 +0200 Message-ID: <20240718105903.19617-7-osalvador@suse.de> X-Mailer: git-send-email 2.44.0 In-Reply-To: <20240718105903.19617-1-osalvador@suse.de> References: <20240718105903.19617-1-osalvador@suse.de> MIME-Version: 1.0 X-Stat-Signature: byfsuwuee6aqzr5n18c6qt1jqognx45j X-Rspamd-Que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mOvC2w9DRwlHiRslgzU3HMl5ZovVk+Xzl9hWy2hwgNR5EWJj0qmlO7QIzShbY2oTC4uJF0E4sLrBCqva4eJ8JjcbSN5WaIjJVwYRNowL3uNa/wMTRs8ZqwysC73JNIdww62QuQ3MwLRxpYVTar9b7C2x/ZpLHDFm4ozVyNL96OK8tfAm2eHHbYDBbxZoygxyMJOK8VW4qXNNk9bf0BVzcxRi90F5ttl4v3OEdK8bTBS/Ua0zd+yXmcsRCGxNxfHxSCwZNzpSXCPQ== X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: Hugetlb mappings will no longer be special cased but rather go through the generic mm_get_unmapped_area_vmflags function. For that to happen, let us remove the .get_unmapped_area from hugetlbfs_file_operations struct, and hint __get_unmapped_area that it should not send hugetlb mappings through thp_get_unmapped_area_vmflags but through mm_get_unmapped_area_vmflags. Create also a function called hugetlb_mmap_check_and_align() where a couple of safety checks are being done and the addr is aligned to the huge page size. Otherwise we will have to do this in every single function, which duplicates quite a lot of code. Signed-off-by: Oscar Salvador --- fs/hugetlbfs/inode.c | 22 ++++++++++++++-------- include/linux/hugetlb.h | 8 +++----- mm/mmap.c | 15 ++++++++++++++- 3 files changed, 31 insertions(+), 14 deletions(-) diff --git a/fs/hugetlbfs/inode.c b/fs/hugetlbfs/inode.c index 81dab95f67ed..d02b4bc6c5e9 100644 --- a/fs/hugetlbfs/inode.c +++ b/fs/hugetlbfs/inode.c @@ -257,15 +257,22 @@ generic_hugetlb_get_unmapped_area(struct file *file, unsigned long addr, pgoff, flags); } -#ifndef HAVE_ARCH_HUGETLB_UNMAPPED_AREA -static unsigned long -hugetlb_get_unmapped_area(struct file *file, unsigned long addr, - unsigned long len, unsigned long pgoff, - unsigned long flags) +unsigned long +hugetlb_mmap_check_and_align(struct file *file, unsigned long addr, + unsigned long len, unsigned long flags) { - return generic_hugetlb_get_unmapped_area(file, addr, len, pgoff, flags); + unsigned long addr0 = 0; + struct hstate *h = hstate_file(file); + + if (len & ~huge_page_mask(h)) + return -EINVAL; + if ((flags & MAP_FIXED) && prepare_hugepage_range(file, addr, len)) + return -EINVAL; + if (addr) + addr0 = ALIGN(addr, huge_page_size(h)); + + return addr0; } -#endif /* * Someone wants to read @bytes from a HWPOISON hugetlb @page from @offset. @@ -1302,7 +1309,6 @@ static const struct file_operations hugetlbfs_file_operations = { .read_iter = hugetlbfs_read_iter, .mmap = hugetlbfs_file_mmap, .fsync = noop_fsync, - .get_unmapped_area = hugetlb_get_unmapped_area, .llseek = default_llseek, .fallocate = hugetlbfs_fallocate, .fop_flags = FOP_HUGE_PAGES, diff --git a/include/linux/hugetlb.h b/include/linux/hugetlb.h index 1c7b0b32ff7e..9183ef95dfb6 100644 --- a/include/linux/hugetlb.h +++ b/include/linux/hugetlb.h @@ -555,11 +555,9 @@ static inline struct hstate *hstate_inode(struct inode *i) } #endif /* !CONFIG_HUGETLBFS */ -#ifdef HAVE_ARCH_HUGETLB_UNMAPPED_AREA -unsigned long hugetlb_get_unmapped_area(struct file *file, unsigned long addr, - unsigned long len, unsigned long pgoff, - unsigned long flags); -#endif /* HAVE_ARCH_HUGETLB_UNMAPPED_AREA */ +unsigned long +hugetlb_mmap_check_and_align(struct file *file, unsigned long addr, + unsigned long len, unsigned long flags); unsigned long generic_hugetlb_get_unmapped_area(struct file *file, unsigned long addr, diff --git a/mm/mmap.c b/mm/mmap.c index 09131b705e7b..8130b25b8cf5 100644 --- a/mm/mmap.c +++ b/mm/mmap.c @@ -1860,6 +1860,7 @@ __get_unmapped_area(struct file *file, unsigned long addr, unsigned long len, unsigned long, unsigned long, unsigned long) = NULL; + bool is_hugetlb = false; unsigned long error = arch_mmap_check(addr, len, flags); if (error) return error; @@ -1868,6 +1869,9 @@ __get_unmapped_area(struct file *file, unsigned long addr, unsigned long len, if (len > TASK_SIZE) return -ENOMEM; + if (file && is_file_hugepages(file)) + is_hugetlb = true; + if (file) { if (file->f_op->get_unmapped_area) get_area = file->f_op->get_unmapped_area; @@ -1885,11 +1889,20 @@ __get_unmapped_area(struct file *file, unsigned long addr, unsigned long len, if (get_area) { addr = get_area(file, addr, len, pgoff, flags); - } else if (IS_ENABLED(CONFIG_TRANSPARENT_HUGEPAGE)) { + } else if (IS_ENABLED(CONFIG_TRANSPARENT_HUGEPAGE) && !is_hugetlb) { /* Ensures that larger anonymous mappings are THP aligned. */ addr = thp_get_unmapped_area_vmflags(file, addr, len, pgoff, flags, vm_flags); } else { + /* + * Consolidate hugepages checks in one place, and also align addr + * to hugepage size. + */ + if (is_hugetlb) { + addr = hugetlb_mmap_check_and_align(file, addr, len, flags); + if (IS_ERR_VALUE(addr)) + return addr; + } addr = mm_get_unmapped_area_vmflags(current->mm, file, addr, len, pgoff, flags, vm_flags); }