From mboxrd@z Thu Jan 1 00:00:00 1970 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 BB69FC433EF for ; Fri, 4 Feb 2022 08:35:07 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id DFA976B0072; Fri, 4 Feb 2022 03:35:06 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id DAA1E6B0073; Fri, 4 Feb 2022 03:35:06 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id C4A6A6B0074; Fri, 4 Feb 2022 03:35:06 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0124.hostedemail.com [216.40.44.124]) by kanga.kvack.org (Postfix) with ESMTP id B1EEB6B0072 for ; Fri, 4 Feb 2022 03:35:06 -0500 (EST) Received: from smtpin26.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay05.hostedemail.com (Postfix) with ESMTP id 67EF2181CAB47 for ; Fri, 4 Feb 2022 08:35:06 +0000 (UTC) X-FDA: 79104437412.26.52A1A34 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by imf10.hostedemail.com (Postfix) with ESMTP id CA302C0005 for ; Fri, 4 Feb 2022 08:35:05 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1643963705; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=ewbjKA3RobEmPNVV+e+ATnicxNeLPCBA7u2EECMhFbw=; b=AqmxeIpLOnirYKYHlclZzv36TeAc2yYv05giHGlacYn2P/O9uBv9G+rqg8XJjw3J2RPMiN JR+oBHLGOzsNjW9k1LVavgXjuu5kYxga/VtCfxqUfeZBbSFRWOe3BY5Llp9CmEz4TgTU3y LqmoT2UpesB/huD8KVNVzNAsxhLaTCc= Received: from mail-wr1-f72.google.com (mail-wr1-f72.google.com [209.85.221.72]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-630-U5KjW0UiOpilY6mTYvaDvg-1; Fri, 04 Feb 2022 03:35:04 -0500 X-MC-Unique: U5KjW0UiOpilY6mTYvaDvg-1 Received: by mail-wr1-f72.google.com with SMTP id x4-20020adfbb44000000b001d83e815683so1678178wrg.8 for ; Fri, 04 Feb 2022 00:35:04 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:message-id:date:mime-version:user-agent :content-language:to:cc:references:from:organization:subject :in-reply-to:content-transfer-encoding; bh=ewbjKA3RobEmPNVV+e+ATnicxNeLPCBA7u2EECMhFbw=; b=xdtLmV8w44gjYTmnoN3ugJCr4XyKCRu9f1MA2SsbT/CedHy+OlUhRCihvBiwGqF+fS mJisqxmTEuOVEWlTU8NoM9Z7PaJgEUGYXlz5O25qUB06IUahI564MTBZRbm2G52A1ze9 r0JWIj79cGr8V5Ba7VQrfU45OMerUWG6wuDx1Cs4nEfAFfENVYZwgZ/q5HXTYvUOpu9S w/CnUt+XSoEU3utbVM8IDIsiwwyAxOeBRILvHbcW/dnoIq2+nRLvvafqv4vb2jr4lbe8 lF2mzCZGOZG4QyJpJFnELYmF/UWg22CBbQIiVsYZ0yz/t7XjcFA5Vhg5AhUjnu+Ew0pB Q6mw== X-Gm-Message-State: AOAM531ll42DEvI7BkHTMox3xQYZTPpNbr3GRYkj332Yj24EN8lTugFP ZfWMx/hf3UGC5EvbGNcNSAaNs+3qIpf2KDxxY4U8jsjI5wIQQUKAHGIUijYSCoehq8Svmf7estR lUYHphk8Ts0c= X-Received: by 2002:a05:600c:3d89:: with SMTP id bi9mr1320790wmb.18.1643963703136; Fri, 04 Feb 2022 00:35:03 -0800 (PST) X-Google-Smtp-Source: ABdhPJzBvUZTIxtC1omsCPEeZauaVUQ6J2EG+VIt2M/0eGF+OUlWBkCIfRYq6sravtSXE+E3rSngbA== X-Received: by 2002:a05:600c:3d89:: with SMTP id bi9mr1320766wmb.18.1643963702801; Fri, 04 Feb 2022 00:35:02 -0800 (PST) Received: from ?IPV6:2003:d8:2f11:9700:838c:3860:6500:5284? (p200300d82f119700838c386065005284.dip0.t-ipconnect.de. [2003:d8:2f11:9700:838c:3860:6500:5284]) by smtp.gmail.com with ESMTPSA id l11sm1428118wry.50.2022.02.04.00.35.01 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Fri, 04 Feb 2022 00:35:02 -0800 (PST) Message-ID: <6a82ea68-6e1e-8f5a-ca89-6744fc896a0b@redhat.com> Date: Fri, 4 Feb 2022 09:35:01 +0100 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101 Thunderbird/91.4.0 To: Mike Kravetz , linux-mm@kvack.org, linux-kernel@vger.kernel.org Cc: Naoya Horiguchi , Axel Rasmussen , Mina Almasry , Michal Hocko , Peter Xu , Andrea Arcangeli , Shuah Khan , Andrew Morton References: <20220202014034.182008-1-mike.kravetz@oracle.com> <20220202014034.182008-2-mike.kravetz@oracle.com> <20571829-9d3d-0b48-817c-b6b15565f651@redhat.com> <7b174c48-d368-43ba-7eab-13719a0d15ef@oracle.com> From: David Hildenbrand Organization: Red Hat Subject: Re: [PATCH v2 1/3] mm: enable MADV_DONTNEED for hugetlb mappings In-Reply-To: <7b174c48-d368-43ba-7eab-13719a0d15ef@oracle.com> X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Language: en-US Content-Type: text/plain; charset=UTF-8 X-Stat-Signature: p8gfp9hojeqiewe995id8derbpqriif7 X-Rspam-User: nil Authentication-Results: imf10.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=AqmxeIpL; spf=none (imf10.hostedemail.com: domain of david@redhat.com has no SPF policy when checking 170.10.133.124) smtp.mailfrom=david@redhat.com; dmarc=pass (policy=none) header.from=redhat.com X-Rspamd-Server: rspam02 X-Rspamd-Queue-Id: CA302C0005 X-HE-Tag: 1643963705-896672 Content-Transfer-Encoding: quoted-printable 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: >>> + /* >>> + * start and size (end - start) must be huge page size aligned >>> + * for hugetlb vmas. >>> + */ >>> + if (is_vm_hugetlb_page(vma)) { >>> + struct hstate *h =3D hstate_vma(vma); >>> + >>> + start =3D ALIGN_DOWN(start, huge_page_size(h)); >>> + end =3D ALIGN(end, huge_page_size(h)); >> >> So you effectively extend the range silently. IIUC, if someone would z= ap >> a 4k range you would implicitly zap a whole 2M page and effectively ze= ro >> out more data than requested. >> >> >> Looking at do_madvise(), we: >> (1) reject start addresses that are not page-aligned >> (2) shrink lengths that are not page-aligned and refuse if it turns 0 >=20 > I believe length is extended (rounded up) by this line: > len =3D PAGE_ALIGN(len_in); Ah, right. I was confused by the "!len" check below that, but the comment explains how this applies to negative values only. >=20 > but, I see your point. >=20 >> The man page documents (1) but doesn't really document (2). >> >> Naturally I'd have assume that we apply the same logic to huge page >> sizes and documenting it in the man page accordingly. >> >> >> Why did you decide to extend the range? I'd assume MADV_REMOVE behaves >> like FALLOC_FL_PUNCH_HOLE: >> "Within the specified range, partial filesystem blocks are zeroed, a= nd >> whole filesystem blocks are removed from the file. After a >> successful call, subsequent reads from this range will return >> zeros." >> So we don't "discard more than requested". >=20 > Well. hugetlbfs does not follow the man page. :( It does not zero > partial blocks. I assume a filesystem block would be a huge page. > Instead it does, >=20 > /* > * For hole punch round up the beginning offset of the hole and > * round down the end. > */ > hole_start =3D round_up(offset, hpage_size); > hole_end =3D round_down(offset + len, hpage_size); Okay, so we skip any zeroing and only free completely covered blocks. We might want to document that behavior. See below. >=20 > So, not only is this patch not following the man page. It is not even > following the existing MADV_REMOVE hugetlb code. Thanks for pointing > that out. Part of my reason for adding this functionality was to make > hugetlb be more like 'normal' memory. I clearly failed. :) >=20 > Related comment about madvise man page for PAGE_SIZE MADV_REMOVE. The = man > page says. >=20 > MADV_REMOVE (since Linux 2.6.16) > Free up a given range of pages and its associated backing= store. > This is equivalent to punching a hole in the correspondin= g byte > range of the backing store (see fallocate(2)). Subsequ= ent ac=E2=80=90 > cesses in the specified address range will see bytes con= taining > zero. >=20 > This may need some clarification. It says it will free pages. We know > madvise only operates on pages (PAGE_ALIGN(len)). Yet, the statement a= bout > equivalent to a fallocate byte range may lead one to believe that lengt= h is > treated the same in madvise and fallocate. Yes >=20 >> I see the following possible alternatives: >> (a) Fail if the range is not aligned >> -> Clear semantics >> (b) Fail if the start is not aligned, shrink the end if required >> -> Same rules as for PAGE_SIZE >> (c) Zero out the requested part >> -> Same semantics as FALLOC_FL_PUNCH_HOLE. >> >> My preference would be a), properly documenting it in the man page. >=20 > However, a) would make hugetlb behave differently than other memory as > len does not need to be aligned. >=20 > I would prefer b) as it is more in line with PAGE_SIZE. But, that does > make it different than MADV_REMOVE hugetlb alignment. >=20 > I thought this was simple. :) It really bugs me that it's under-specified what's supposed to happen when the length is not aligned. BUT: in the posix world, "calling posix_madvise() shall not affect the semantics of access to memory in the specified range". So we don't care too much about if we align up/down, because it wouldn't affect the semantics. Especially for MADV_DONTNEED/MADV_REMOVE as implemented by Linux this is certainly different and the alignment handling matters. So I guess especially for MADV_DONTNEED/MADV_REMOVE we need a clear specification what's supposed to happen if the length falls into the middle of a huge page. We should document alignment handling for madvise() in general I assume. IMHO we should have bailed out right from the start whenever something is not properly aligned, but that ship has sailed. So I agree, maybe we can make at least hugetlb MADV_DONTNEED obey the same (weird) rules as ordinary pages. So b) would mean, requiring start to be hugepage aligned and aligning-up the end. Still feels wrong but at least matches existing semantics. Hugetlb MADV_REMOVE semantics are unfortunate and we should document the exception. --=20 Thanks, David / dhildenb