From patchwork Thu Jul 18 10:59:03 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Oscar Salvador X-Patchwork-Id: 13736393 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 96052C3DA49 for ; Thu, 18 Jul 2024 10:59:40 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 0073E6B009D; Thu, 18 Jul 2024 06:59:28 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id EF6B56B009F; Thu, 18 Jul 2024 06:59:27 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id C6D026B009E; Thu, 18 Jul 2024 06:59:27 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id A45286B009B for ; Thu, 18 Jul 2024 06:59:27 -0400 (EDT) Received: from smtpin23.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id 54060121A47 for ; Thu, 18 Jul 2024 10:59:27 +0000 (UTC) X-FDA: 82352577174.23.F80774F Received: from smtp-out1.suse.de (smtp-out1.suse.de [195.135.223.130]) by imf10.hostedemail.com (Postfix) with ESMTP id 2FBD7C001E for ; Thu, 18 Jul 2024 10:59:24 +0000 (UTC) Authentication-Results: imf10.hostedemail.com; dkim=pass header.d=suse.de header.s=susede2_rsa header.b=i7YlcF6j; dkim=pass header.d=suse.de header.s=susede2_ed25519 header.b=zUWKNZPd; dkim=pass header.d=suse.de header.s=susede2_rsa header.b=dNFZLmEG; dkim=pass header.d=suse.de header.s=susede2_ed25519 header.b=q+V6AbMK; spf=pass (imf10.hostedemail.com: domain of osalvador@suse.de designates 195.135.223.130 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=1721300345; 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=RffRCKVkf4tOMl6I565jAg9VCZIGD0dLIYK/HF2buBQ=; b=3KD0W1WyfapC/WLDnVoWC6/On1wzzXIEdVrtR6RXigHM96REbWCwl5OOHbmd56WvykXpVb vf8YANPxen5JVjCDtPtMJcioZ3Dt0qS0I8KLkLYz5tiu9xg4PIyAT/+yAdcUb5Q7vjN5C5 k6o0LS1Hglx1vQzd0b+/SP/WYfrnRp0= ARC-Authentication-Results: i=1; imf10.hostedemail.com; dkim=pass header.d=suse.de header.s=susede2_rsa header.b=i7YlcF6j; dkim=pass header.d=suse.de header.s=susede2_ed25519 header.b=zUWKNZPd; dkim=pass header.d=suse.de header.s=susede2_rsa header.b=dNFZLmEG; dkim=pass header.d=suse.de header.s=susede2_ed25519 header.b=q+V6AbMK; spf=pass (imf10.hostedemail.com: domain of osalvador@suse.de designates 195.135.223.130 as permitted sender) smtp.mailfrom=osalvador@suse.de; dmarc=pass (policy=none) header.from=suse.de ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1721300345; a=rsa-sha256; cv=none; b=0E9CQEeUtblQNqlVchXFKf9ApyrB1rwEOpQU6SPOI4Ztlki1B5D5jTPSyBqlVZOEb1H6yb rMw9vNM+Ohio1yEEX28Kqr6ydcwHcn+/50p/PsQpGhMsly3kFnIp+3Hf9yUE86vlzaNxCz qYW+Cf+zO6QARj5v3X77eZ8ZtiTgYho= 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-out1.suse.de (Postfix) with ESMTPS id DA41721AD8; Thu, 18 Jul 2024 10:59:23 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1721300364; 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=RffRCKVkf4tOMl6I565jAg9VCZIGD0dLIYK/HF2buBQ=; b=i7YlcF6j7N95/M1bzdjQk/afWpIjGNnzYBM7IKxJRilPCZADsZR+KAOy5MPqVuth0UC3TY VYbeTdWmkl+pGUD0B02lHJKDykyJ4mHqRy2ei6PELxhKkgMVbqk+zBvdeVIxHX5Pmx6PzB /clXhZ28nwy4pf4/mEKapyjVALPXJ4I= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1721300364; 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=RffRCKVkf4tOMl6I565jAg9VCZIGD0dLIYK/HF2buBQ=; b=zUWKNZPd5+IxiEQPxQAclcXFDg9fvY1JH7A4xLgRpX8D9a4jdMse3U81Xed5E26wfiTaTM ueLaKILTzBMa29Ag== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1721300363; 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=RffRCKVkf4tOMl6I565jAg9VCZIGD0dLIYK/HF2buBQ=; b=dNFZLmEGJP9bLQS+FwbrILkK+PuB/MFoTFYzQLTLyte33tBS8Jghb6eq8gGvG6NwrF8iUi UvH8JkYJ4dhwtjDz/cLRYPq4DrVY0Z9JHYS9nNdwt/Qtthx4ZZhNk6PlfIpt1+rn7HWqgQ RSbSjoCT21Fn0hS0n6+/V+B6fd7WXnA= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1721300363; 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=RffRCKVkf4tOMl6I565jAg9VCZIGD0dLIYK/HF2buBQ=; b=q+V6AbMKo0wQpmJoguEfMZf6MSJe6F0vmu3igxL+4bW6YzWJtlwnQuOjWkyvfoA3p9pMU4 3/lzuvOjos2TJhDw== 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 CD9F2137EB; Thu, 18 Jul 2024 10:59:22 +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 2MgSLor1mGbnVAAAD6G6ig (envelope-from ); Thu, 18 Jul 2024 10:59:22 +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 9/9] mm: Consolidate common checks in hugetlb_mmap_check_and_align Date: Thu, 18 Jul 2024 12:59:03 +0200 Message-ID: <20240718105903.19617-10-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-Rspam-User: X-Rspamd-Server: rspam01 X-Rspamd-Queue-Id: 2FBD7C001E X-Stat-Signature: wy463sk5aoksbgpxboumu485p8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a3HjB8maF1hoOpbk+A7v8OF2h9c0NtE1ECZNojtlLyJcrXQKR+lXv6WHNO9S0Akfb6nNf2gd7AnHF+bcbP6eybYHoM2ed3OXzZyuhWkdZOijVaMXMuAkefC//GP+R84R8KzMtLx6DhEiAANprJcnBHrMs03D1fbANYm7bN0vjXCMBCPKNAc1ZUOfBsz+kv5V5CsrtRkSp2nvMLC+bJPDfaUtzga3I+ohmARQYosg== 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: prepare_hugepage_range() performs almost the same checks for all architectures that define it, with the exception of mips and loongarch that also check for overflows. The rest checks for the addr and len to be properly aligned, so we can move that to the generic hugetlb_mmap_check_and_align() function and get rid of a fair amount of duplicated code. Signed-off-by: Oscar Salvador --- arch/loongarch/include/asm/hugetlb.h | 4 ---- arch/mips/include/asm/hugetlb.h | 4 ---- arch/parisc/include/asm/hugetlb.h | 15 --------------- arch/s390/include/asm/hugetlb.h | 17 ----------------- arch/sh/include/asm/hugetlb.h | 15 --------------- fs/hugetlbfs/inode.c | 8 ++++++-- include/asm-generic/hugetlb.h | 7 ------- 7 files changed, 6 insertions(+), 64 deletions(-) diff --git a/arch/loongarch/include/asm/hugetlb.h b/arch/loongarch/include/asm/hugetlb.h index aa44b3fe43dd..107566c98938 100644 --- a/arch/loongarch/include/asm/hugetlb.h +++ b/arch/loongarch/include/asm/hugetlb.h @@ -18,10 +18,6 @@ static inline int prepare_hugepage_range(struct file *file, unsigned long task_size = STACK_TOP; struct hstate *h = hstate_file(file); - if (len & ~huge_page_mask(h)) - return -EINVAL; - if (addr & ~huge_page_mask(h)) - return -EINVAL; if (len > task_size) return -ENOMEM; if (task_size - len < addr) diff --git a/arch/mips/include/asm/hugetlb.h b/arch/mips/include/asm/hugetlb.h index fd69c8808554..6a63d82a8ab3 100644 --- a/arch/mips/include/asm/hugetlb.h +++ b/arch/mips/include/asm/hugetlb.h @@ -19,10 +19,6 @@ static inline int prepare_hugepage_range(struct file *file, unsigned long task_size = STACK_TOP; struct hstate *h = hstate_file(file); - if (len & ~huge_page_mask(h)) - return -EINVAL; - if (addr & ~huge_page_mask(h)) - return -EINVAL; if (len > task_size) return -ENOMEM; if (task_size - len < addr) diff --git a/arch/parisc/include/asm/hugetlb.h b/arch/parisc/include/asm/hugetlb.h index 72daacc472a0..5b3a5429f71b 100644 --- a/arch/parisc/include/asm/hugetlb.h +++ b/arch/parisc/include/asm/hugetlb.h @@ -12,21 +12,6 @@ void set_huge_pte_at(struct mm_struct *mm, unsigned long addr, pte_t huge_ptep_get_and_clear(struct mm_struct *mm, unsigned long addr, pte_t *ptep); -/* - * If the arch doesn't supply something else, assume that hugepage - * size aligned regions are ok without further preparation. - */ -#define __HAVE_ARCH_PREPARE_HUGEPAGE_RANGE -static inline int prepare_hugepage_range(struct file *file, - unsigned long addr, unsigned long len) -{ - if (len & ~HPAGE_MASK) - return -EINVAL; - if (addr & ~HPAGE_MASK) - return -EINVAL; - return 0; -} - #define __HAVE_ARCH_HUGE_PTEP_CLEAR_FLUSH static inline pte_t huge_ptep_clear_flush(struct vm_area_struct *vma, unsigned long addr, pte_t *ptep) diff --git a/arch/s390/include/asm/hugetlb.h b/arch/s390/include/asm/hugetlb.h index f01b101c80b7..d4277e5a27d9 100644 --- a/arch/s390/include/asm/hugetlb.h +++ b/arch/s390/include/asm/hugetlb.h @@ -25,23 +25,6 @@ extern pte_t huge_ptep_get(pte_t *ptep); extern pte_t huge_ptep_get_and_clear(struct mm_struct *mm, unsigned long addr, pte_t *ptep); -/* - * If the arch doesn't supply something else, assume that hugepage - * size aligned regions are ok without further preparation. - */ -#define __HAVE_ARCH_PREPARE_HUGEPAGE_RANGE -static inline int prepare_hugepage_range(struct file *file, - unsigned long addr, unsigned long len) -{ - struct hstate *h = hstate_file(file); - - if (len & ~huge_page_mask(h)) - return -EINVAL; - if (addr & ~huge_page_mask(h)) - return -EINVAL; - return 0; -} - static inline void arch_clear_hugetlb_flags(struct folio *folio) { clear_bit(PG_arch_1, &folio->flags); diff --git a/arch/sh/include/asm/hugetlb.h b/arch/sh/include/asm/hugetlb.h index 75028bd568ba..4a92e6e4d627 100644 --- a/arch/sh/include/asm/hugetlb.h +++ b/arch/sh/include/asm/hugetlb.h @@ -5,21 +5,6 @@ #include #include -/* - * If the arch doesn't supply something else, assume that hugepage - * size aligned regions are ok without further preparation. - */ -#define __HAVE_ARCH_PREPARE_HUGEPAGE_RANGE -static inline int prepare_hugepage_range(struct file *file, - unsigned long addr, unsigned long len) -{ - if (len & ~HPAGE_MASK) - return -EINVAL; - if (addr & ~HPAGE_MASK) - return -EINVAL; - return 0; -} - #define __HAVE_ARCH_HUGE_PTEP_CLEAR_FLUSH static inline pte_t huge_ptep_clear_flush(struct vm_area_struct *vma, unsigned long addr, pte_t *ptep) diff --git a/fs/hugetlbfs/inode.c b/fs/hugetlbfs/inode.c index 4902220adf42..38b8a9a63ba2 100644 --- a/fs/hugetlbfs/inode.c +++ b/fs/hugetlbfs/inode.c @@ -180,8 +180,12 @@ hugetlb_mmap_check_and_align(struct file *file, unsigned long addr, if (len & ~huge_page_mask(h)) return -EINVAL; - if ((flags & MAP_FIXED) && prepare_hugepage_range(file, addr, len)) - return -EINVAL; + if (flags & MAP_FIXED) { + if (addr & ~huge_page_mask(h)) + return -EINVAL; + if (prepare_hugepage_range(file, addr, len)) + return -EINVAL; + } if (addr) addr0 = ALIGN(addr, huge_page_size(h)); diff --git a/include/asm-generic/hugetlb.h b/include/asm-generic/hugetlb.h index 0b89d36a4310..0ca5da28fb01 100644 --- a/include/asm-generic/hugetlb.h +++ b/include/asm-generic/hugetlb.h @@ -123,13 +123,6 @@ static inline int huge_pte_none_mostly(pte_t pte) static inline int prepare_hugepage_range(struct file *file, unsigned long addr, unsigned long len) { - struct hstate *h = hstate_file(file); - - if (len & ~huge_page_mask(h)) - return -EINVAL; - if (addr & ~huge_page_mask(h)) - return -EINVAL; - return 0; } #endif