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 22FE5C7618D for ; Thu, 6 Apr 2023 20:00:36 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 9BE9C6B0071; Thu, 6 Apr 2023 16:00:35 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 9480E6B0074; Thu, 6 Apr 2023 16:00:35 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 79C136B0075; Thu, 6 Apr 2023 16:00:35 -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 699866B0071 for ; Thu, 6 Apr 2023 16:00:35 -0400 (EDT) Received: from smtpin09.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id 26089C06D8 for ; Thu, 6 Apr 2023 20:00:35 +0000 (UTC) X-FDA: 80652033630.09.F836D15 Received: from mail-ej1-f52.google.com (mail-ej1-f52.google.com [209.85.218.52]) by imf14.hostedemail.com (Postfix) with ESMTP id 29C12100024 for ; Thu, 6 Apr 2023 20:00:32 +0000 (UTC) Authentication-Results: imf14.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=X4NUYQQ8; spf=pass (imf14.hostedemail.com: domain of emmir@google.com designates 209.85.218.52 as permitted sender) smtp.mailfrom=emmir@google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1680811233; 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-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=nd8CdG3tEyTpIem9NcMLhqsCDudYGnSaMV+QuQk/dn4=; b=aO+3PTwnjwVhPj1ZE919C74gUJbBGoyPnEFvs1lt/wEWGY39UvwuzH86S+yfb1TMSaQouw CBqh8uczYkRrDFUtricI0NPMh518qxBwUUTsYSKUqbSA58f5BEFYet6k72qiv23qzmmtSq 50GMcHTJ3T8H+hcLTjxrHfmws0iHd3E= ARC-Authentication-Results: i=1; imf14.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=X4NUYQQ8; spf=pass (imf14.hostedemail.com: domain of emmir@google.com designates 209.85.218.52 as permitted sender) smtp.mailfrom=emmir@google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1680811233; a=rsa-sha256; cv=none; b=4Qg95pQBTghrcrYrRXUluORkzZ95ezA5K2ugIFyTMziCD4Bv6pFvGGR6PC1NaRaeaairPH USeWv8SeeJao0uVo4LnylsE7o29iHKHi81xOj6DMY7Ti1AIkGNKF5mY7SQfPfc640NT7ED aQS2nzJ1mj6WMM1x/Mwo/Ux7q5x6nbA= Received: by mail-ej1-f52.google.com with SMTP id a640c23a62f3a-9338d479a21so154594366b.1 for ; Thu, 06 Apr 2023 13:00:32 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; t=1680811231; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=nd8CdG3tEyTpIem9NcMLhqsCDudYGnSaMV+QuQk/dn4=; b=X4NUYQQ8tCMLj8AsQ8A5D4O8WA+9or8X/KsR9JSOcXURexcxASRc1DPuMazArWdvdL lUzUodCfxZvgm5B+QFxLSw4orBW/OEc/RN4ERtKalF5HQMavPlwYQo5Q/9DzJZNUpCVM K00Wqe0Ul4ceSlAjKKHwmpP6SdKfH4QI9Z5BJkZaK1io4pl/W48EXr8EmoplS/pe9duQ YQaYhBejj95Vdp/MwjPg+4+aXcOkS8LSYNOJwphruff0KlwSE3bNar6tN5p7UpqB7T2k Dsat8SCV9mzgVh5HaOVfWDb2gvq/bVgDJ7vfIKoqyTd8xTDwGYeFhcgcQkiF11tcLUPe O9pg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1680811231; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=nd8CdG3tEyTpIem9NcMLhqsCDudYGnSaMV+QuQk/dn4=; b=74BXa51HI0oGic4gJ5ZyB8SVtG6IPoL+jrY173pzQ8RFY2kB4h8OL+SAo4nfcgOuuk 0wUSz/4/gqgflb8/xqBRkmCBi9MwNTqn+yGBx64SRBccMDNGD8MNZogKTJUrhIraVS6W np4pRFtCrP2lNsxEQRyjO4Jdm86bjQzXsPmTOfT9HDk09/8r6IglAMNKZ9KPyctsSLv4 a0cinQ2tG2LRCvniflaNKeBEZn2ipGYaGa8DXqETIyTxaltuT6RehYO2mBRtRMoSIgKe GvGtkJ5phDs2WcYoe2R9Gi2QxY4+Xby3aAVF94gM+GRQ4kOixc/RrLmlzGjhun3T7GmA QqZw== X-Gm-Message-State: AAQBX9fDIJ85c15Q83Tctu5c6uh3l/jRZ6Hx/to6NmT2evQUOWs4STpC cRfqH+x5paRH8Z1isy9z7Rl5tsO2dUm5IghzCOtMsA== X-Google-Smtp-Source: AKy350YxLmM7o7NofjNZI1FB3oHxtUqQdNjHp/dOLN89IbILs8acbig6vzBtiMi0F7gjm5VWa5NlgYP2dj9nUNUL3so= X-Received: by 2002:a50:9987:0:b0:504:7094:2b59 with SMTP id m7-20020a509987000000b0050470942b59mr373316edb.7.1680811231205; Thu, 06 Apr 2023 13:00:31 -0700 (PDT) MIME-Version: 1.0 References: <20230406074005.1784728-1-usama.anjum@collabora.com> <20230406074005.1784728-3-usama.anjum@collabora.com> <0351b563-5193-6431-aa9c-c5bf5741b791@collabora.com> In-Reply-To: <0351b563-5193-6431-aa9c-c5bf5741b791@collabora.com> From: =?UTF-8?B?TWljaGHFgiBNaXJvc8WCYXc=?= Date: Thu, 6 Apr 2023 22:00:19 +0200 Message-ID: Subject: Re: [PATCH v12 2/5] fs/proc/task_mmu: Implement IOCTL to get and optionally clear info about PTEs To: Muhammad Usama Anjum , Mike Rapoport Cc: Peter Xu , David Hildenbrand , Andrew Morton , Andrei Vagin , Danylo Mocherniuk , Paul Gofman , Cyrill Gorcunov , Nadav Amit , Alexander Viro , Shuah Khan , Christian Brauner , Yang Shi , Vlastimil Babka , "Liam R . Howlett" , Yun Zhou , Suren Baghdasaryan , Alex Sierra , Matthew Wilcox , Pasha Tatashin , Axel Rasmussen , "Gustavo A . R . Silva" , Dan Williams , linux-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, linux-kselftest@vger.kernel.org, Greg KH , kernel@collabora.com Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Rspamd-Server: rspam05 X-Rspamd-Queue-Id: 29C12100024 X-Stat-Signature: pjdsgcmcf381d6um3drg8k9s8defjjp1 X-Rspam-User: X-HE-Tag: 1680811232-291528 X-HE-Meta: U2FsdGVkX188ujFn6Pdz0o/AOZGY/4VhIf33DA4nXO1DStNCTITcAE69zi2jE5lN8fh0UWpYqyUNsVWBi3C+TVFg0frPG8WrrFd0TzSQttalLfeF02R9JX6efjWKcMGuhxn9EGSgu35Q/uP6qfhBwEIBACgXo9wbwQ8ytvILNKJNf1Hqdu8HloHEsJUeKuc+KxqCeB2BmWEJBodlVErtbfeSW/y6kDrBmKPYwEtWlU16fBzGsrmBeFpmSsbsouGCvj3iVSZZYiynMKvSnohA+mys0JZhYS/uODH5mZ458PEFf27jzb9HJ1T7ZO72W+WfShbyLsP7FtYrnrmIqN407AaeZz6ozZfBoF1F1QfyQg3LUxPC/fGBB20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2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: On Thu, 6 Apr 2023 at 19:58, Muhammad Usama Anjum wrote: > Hello, > > Thank you so much for the review. Do you have any thoughts on the build > error on arc architecture? > https://lore.kernel.org/all/e3c82373-256a-6297-bcb4-5e1179a2cbe2@collabor= a.com Maybe copy HPAGE_* defines from x86 and key on CONFIG_PGTABLE_LEVELS > 2? I don't know much about arc arch, though. > On 4/6/23 8:52=E2=80=AFPM, Micha=C5=82 Miros=C5=82aw wrote: > > On Thu, 6 Apr 2023 at 09:40, Muhammad Usama Anjum > > wrote:> [...] > >> +#define PM_SCAN_BITMAP(wt, file, present, swap) \ > >> + (wt | file << 1 | present << 2 | swap << 3) > > Please parenthesize macro arguments ("(wt)", "(file)", etc.) to not > > have to worry about operator precedence when passed a complex > > expression. > Like this? > #define PM_SCAN_BITMAP(wt, file, present, swap) \ > ((wt) | (file << 1) | (present << 2) | (swap << 3)) The value would be: ( (wt) | ((file) << 1) | ... ) IOW, each parameter should have parentheses around its name. [...] > >> + cur->len +=3D n_pages; > >> + p->found_pages +=3D n_pages; > >> + > >> + if (p->max_pages && (p->found_pages =3D=3D p->max_page= s)) > >> + return PM_SCAN_FOUND_MAX_PAGES; > >> + > >> + return 0; > >> + } > >> + > >> + if (!p->vec_index || ((p->vec_index + 1) < p->vec_len)) { > > > > It looks that `if (p->vec_index < p->vec_len)` is enough here - if we > > have vec_len =3D=3D 0 here, then we'd not fit the entry in the userspac= e > > buffer anyway. Am I missing something? > No. I'd explained it with diagram last time: > https://lore.kernel.org/all/3c8d9ea0-1382-be0c-8dd2-d490eedd3b55@collabor= a.com > > I'll add a concise comment here. So it seems, but I think the code changed a bit and maybe could be simplified now? Since p->vec_len =3D=3D 0 is currently not valid, the field could count only the entries available in p->vec[] -- IOW: not include p->cur in the count. BTW, `if (no space) return -ENOSPC` will avoid additional indentation for the non-merging case. [...] > >> +static inline int pagemap_scan_deposit(struct pagemap_scan_private *p= , > >> + struct page_region __user *vec, > >> + unsigned long *vec_index) > > > > ..._deposit() is used only in single place - please inline. > It is already inline. Sorry. I mean: please paste the code in place of the single call. [...] > >> + /* > >> + * Break huge page into small pages if the WP operatio= n need to > >> + * be performed is on a portion of the huge page or if= max_pages > >> + * pages limit would exceed. > > > > BTW, could the `max_pages` limit be relaxed a bit (in that it would be > > possible to return more pages if they all merge into the last vector > > entry) so that it would not need to split otherwise-matching huge > > page? It would remove the need for this special handling in the kernel > > and splitting the page by this read-only-appearing ioctl? > No, this cannot be done. Otherwise we'll not be able to emulate Windows > syscall GetWriteWatch() which specifies the exact number of pages. Usuall= y > in most of cases, either user will not use THP or not perform the operati= on > on partial huge page. So this part is only there to keep things correct f= or > those users who do use THP and partial pagemap_scan operations. I see that `GetWriteWatch` returns a list of pages not ranges of pages. That makes sense (more or less). (BTW, It could be emulated in userspace by caching the last not-fully-consumed range.) > >> + */ > >> + if (is_written && PM_SCAN_OP_IS_WP(p) && > >> + ((end - start < HPAGE_SIZE) || > >> + (p->max_pages && > >> + (p->max_pages - p->found_pages) < n_pages))) { > >> + > >> + split_huge_pmd(vma, pmd, start); > >> + goto process_smaller_pages; > >> + } > >> + > >> + if (p->max_pages && > >> + p->found_pages + n_pages > p->max_pages) > >> + n_pages =3D p->max_pages - p->found_pages; > >> + > >> + ret =3D pagemap_scan_output(is_written, is_file, is_pr= esent, > >> + is_swap, p, start, n_pages); > >> + if (ret < 0) > >> + return ret; So let's simplify this: if (p->max_pages && n_pages > max_pages - found_pages) n_pages =3D max_pages - found_pages; if (is_written && DO_WP && n_pages !=3D HPAGE_SIZE / PAGE_SIZE) { split_thp(); goto process_smaller_pages; } BTW, THP handling could be extracted to a function that would return -EAGAIN if it has split the page or it wasn't a THP -- and that would mean `goto process_smaller_pages`. > > Why not propagate the error from uffd_wp_range()? > uffd_wp_range() returns status in long variable. We cannot return long in > this function. So intead of type casting long to int and then return I've > used -EINVAL. Would following be more suitable? > > long ret2 =3D uffd_wp_range(vma, start, HPAGE_SIZE, true); > if (ret2 < 0) > return (int)ret2; I think it's ok, since negative values are expected to be error codes. And since you can't overflow int with HPAGE_SIZE pages, then I wouldn't use `ret2` but cast the return and add a comment why it's safe. [...] > >> + start =3D (unsigned long)untagged_addr(arg.start); > >> + vec =3D (struct page_region *)(unsigned long)untagged_addr(arg= .vec); > > > > Is the inner cast needed? > arg.vec remains 64-bit on 32-bit systems. So casting 64bit value directly > to struct page_region pointer errors out. So I've added specific casting = to > unsigned long first before casting to pointers. I see. So to convey the intention, the `arg.start` and `arg.vec` should be casted to unsigned long, not the `untagged_addr()` return values. > >> + ret =3D pagemap_scan_args_valid(&arg, start, vec); > >> + if (ret) > >> + return ret; > >> + > >> + end =3D start + arg.len; > >> + p.max_pages =3D arg.max_pages; > >> + p.found_pages =3D 0; > >> + p.flags =3D arg.flags; > >> + p.required_mask =3D arg.required_mask; > >> + p.anyof_mask =3D arg.anyof_mask; > >> + p.excluded_mask =3D arg.excluded_mask; > >> + p.return_mask =3D arg.return_mask; > >> + p.cur.len =3D 0; > >> + p.cur.start =3D 0; > >> + p.vec =3D NULL; > >> + p.vec_len =3D (PAGEMAP_WALK_SIZE >> PAGE_SHIFT); > > > > Nit: parentheses are not needed here, please remove. > Will do. > > > > >> + > >> + /* > >> + * Allocate smaller buffer to get output from inside the page = walk > >> + * functions and walk page range in PAGEMAP_WALK_SIZE size chu= nks. As > >> + * we want to return output to user in compact form where no t= wo > >> + * consecutive regions should be continuous and have the same = flags. > >> + * So store the latest element in p.cur between different walk= s and > >> + * store the p.cur at the end of the walk to the user buffer. > >> + */ > >> + p.vec =3D kmalloc_array(p.vec_len, sizeof(struct page_region), > >> + GFP_KERNEL); > >> + if (!p.vec) > >> + return -ENOMEM; > >> + > >> + walk_start =3D walk_end =3D start; > >> + while (walk_end < end && !ret) { > > > > The loop will stop if a previous iteration returned ENOSPC (and the > > error will be lost) - is it intended? > It is intentional. -ENOSPC means that the user buffer is full even though > there was more memory to walk over. We don't treat this error. So when > buffer gets full, we stop walking over further as user buffer has gotten > full and return as success. Thanks. What's the difference between -ENOSPC and PM_SCAN_FOUND_MAX_PAGES? They seem to result in the same effect (code flow). [...] > >> --- a/include/linux/userfaultfd_k.h > >> +++ b/include/linux/userfaultfd_k.h > >> @@ -210,6 +210,14 @@ extern bool userfaultfd_wp_async(struct vm_area_s= truct *vma); > >> > >> #else /* CONFIG_USERFAULTFD */ > >> > >> +static inline long uffd_wp_range(struct mm_struct *dst_mm, > >> + struct vm_area_struct *vma, > >> + unsigned long start, unsigned long le= n, > >> + bool enable_wp) > >> +{ > >> + return 0; > >> +} [...] > > Shouldn't this part be in the patch introducing uffd_wp_range()? > We have not added uffd_wp_range() in previous patches. We just need this > stub for this patch for the case when CONFIG_USERFAULTFD isn't enabled. > > I'd this as separate patch before this patch. Mike asked me to merge it > with this patch in order not to break bisectability. >[1] https://lore.kernel.org/all/ZBK+86eMMazwfhdx@kernel.org I would understand the reply [1] to mean that the uffd_wp_range() stub should go in the same patch where uffd_wp_range() is implemented. But uffd_wp_range() is already in (since f369b07c86140) so I don't see how having the stub in a separate commit sequenced before this one could break bisect? Best Regards Micha=C5=82 Miros=C5=82aw