Received: by 2002:a05:6359:6284:b0:131:369:b2a3 with SMTP id se4csp4880302rwb; Tue, 8 Aug 2023 15:50:37 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHZI6MPZcQhvay5uWx1ekohS1YzWd78GSNsbDeQ1h4FOem1mnJrNEJZuUyrisk/XW2XamJ/ X-Received: by 2002:a05:6a20:9187:b0:13f:67b6:b65f with SMTP id v7-20020a056a20918700b0013f67b6b65fmr1000190pzd.54.1691535036680; Tue, 08 Aug 2023 15:50:36 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1691535036; cv=none; d=google.com; s=arc-20160816; b=dIc9ClS9IVS0U5xZo6oeoEG2Q46ZSQJYpip4DjyhWk1Q6WZSpgZPgb7Xsm+iFUx7xE uvBwz0ZPQ0n7XUSm/C+T+Yqinc5XG/Qmgg93EtLczt7a7MyACr8kwenknLzb0pjiXbB1 SqfUJUV2nMwG82AdCahdL5I9ndWaDb0/Neu2HMACeeLekJ2wWpe8UBv/vVsnBVucgQwc HBWtboWxK8zorc6/9zmA4uEqKwOKM5wODLfuwMgurdKYibUPGaJWdiKyzwwvOTuxmEav zmNv24DgDQ3canqg76QiojJ7Mfw41bJcNxpebI5kKGekkcGMxGOt1jB8mZSOuw1ghA7X 08tg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:cc:to:subject :message-id:date:from:in-reply-to:references:mime-version :dkim-signature; bh=shx8xzzcKiMrKveTUHLTR3Q3hB2SO4sAG3AUT0OsTBQ=; fh=Dm7NB/pqHdwyWMzHW4nLpHDDT4PNLGd/EmtucAEwJqU=; b=Hgiss+Asuq3w8UzQCScWQ1apAkWU6wTCQsj46xAqsd4sN0ReZVNbN0Sip3h9No3lFO I5MvfeuAYirvKzM1i1u/KLksM8BiHqphRdlfuC4CYd8Hz7tooAneYwglelSKMrdvF2Ww 9/WdaTJlY2V8SfmE+jsxmf1xAR7grafp/0zrjogVBLQ1S/X0bWIxomb3UxJHgf+gOdmZ hNcEQd0huM+C9MYUmxFrxxPb44fLUBTXI0bZXYJxFfvsNFBHYrqrYwkbaPTpYBMXpGG0 FhPjy6s53Lp1yiogG5udGXT11qqmgovlEugwjOjhL5GYw2SX6oQKks2Kao5OPSxeD2C3 0c2w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20221208 header.b=Tx0pmOfU; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id k25-20020a63ba19000000b00563519244cdsi8013984pgf.625.2023.08.08.15.50.24; Tue, 08 Aug 2023 15:50:36 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20221208 header.b=Tx0pmOfU; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235321AbjHHUct (ORCPT + 99 others); Tue, 8 Aug 2023 16:32:49 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36784 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236464AbjHHUcW (ORCPT ); Tue, 8 Aug 2023 16:32:22 -0400 Received: from mail-yb1-xb2b.google.com (mail-yb1-xb2b.google.com [IPv6:2607:f8b0:4864:20::b2b]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 156D84445C; Tue, 8 Aug 2023 12:56:04 -0700 (PDT) Received: by mail-yb1-xb2b.google.com with SMTP id 3f1490d57ef6-d4364cf8be3so5031061276.1; Tue, 08 Aug 2023 12:56:04 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1691524563; x=1692129363; 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=shx8xzzcKiMrKveTUHLTR3Q3hB2SO4sAG3AUT0OsTBQ=; b=Tx0pmOfUE84GvT8iHHKudjkY6/uU6+P0q4Vq3Fi92Ai4HjE8vjdNm/CgwD/GBUhAi4 H+c93rt+kOWcWN9wMelSq5gRU6D9fVm2AqUDDcLd/f+dt1P2PhYh/nN8/nXgLFdgE/fm LcBdCB1rYzMM+bXvvExqt10//p1OpzjGy4gNa1eo3p/VkYal498m2r6AdeN98nyK3Kxw hQmirUesK5dKLRbM2V2tfuWcSLgFoVbDFR5021fkQR46p5dkVP7IkZQU3rz5bX8GWT7n dzxkxagsG9uSb+QKU8SFvyBWRbBjK+cXAAch3R03s80Er3YOi38saKC4ekEgdE5sSKo/ g2uQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1691524563; x=1692129363; 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=shx8xzzcKiMrKveTUHLTR3Q3hB2SO4sAG3AUT0OsTBQ=; b=GCi4CjS56WiCeHlT9fJYMn8iA3eso+bl947qAkRvWvC1qyNh8GC4cHDe7R41pU4Avx heHeFZwqoVjAxV7rN5+2pQxRFbXMezi16dxkbzrWh7WAPBbmhvrzBn4V8GQ6BSpSXeo/ YmL8EY0835PBOlkpS1AH6Ys9EB+CzdBRwrsj0e+3oeEBHgYOl7b3dR8evNkhqSCy8ffB iU1PzBlS2u4W8pBhQAyHbaVutvazW1K7Vv0K5aAzS9/DNxj/gIeYLrL2SMzDpgmtOYDC BJQogWPcl9P7IgPSsrLta+7IyF8c9hVvRCMlm8pQJRBaUknA71xgVjZ7/MP+OtKLdphY GP3g== X-Gm-Message-State: AOJu0Yybaz52HgZJVaJKA7PsRMRmZMI9/pyshvBZ6tMwJUITfSMDr9Kn oqY1ItM+6QU0rWHNnbtVyrmqlCbjyVBjs2jMpZQ= X-Received: by 2002:a25:5f0d:0:b0:c5d:f2af:5a24 with SMTP id t13-20020a255f0d000000b00c5df2af5a24mr595770ybb.14.1691524562978; Tue, 08 Aug 2023 12:56:02 -0700 (PDT) MIME-Version: 1.0 References: <20230808104309.357852-1-usama.anjum@collabora.com> <20230808104309.357852-3-usama.anjum@collabora.com> <624cfa26-5650-ee0d-8e0a-1d844175bcaf@collabora.com> In-Reply-To: <624cfa26-5650-ee0d-8e0a-1d844175bcaf@collabora.com> From: Andrei Vagin Date: Tue, 8 Aug 2023 12:55:51 -0700 Message-ID: Subject: Re: [PATCH v27 2/6] fs/proc/task_mmu: Implement IOCTL to get and optionally clear info about PTEs To: Muhammad Usama Anjum Cc: Peter Xu , David Hildenbrand , Andrew Morton , =?UTF-8?B?TWljaGHFgiBNaXJvc8WCYXc=?= , Danylo Mocherniuk , Paul Gofman , Cyrill Gorcunov , Mike Rapoport , 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, =?UTF-8?B?TWljaGHFgiBNaXJvc8WCYXc=?= Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM, RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Tue, Aug 8, 2023 at 12:35=E2=80=AFPM Muhammad Usama Anjum wrote: > > On 8/9/23 12:21=E2=80=AFAM, Andrei Vagin wrote: > > On Tue, Aug 8, 2023 at 3:43=E2=80=AFAM Muhammad Usama Anjum > > wrote: > > > > .... > > > >> +static int pagemap_scan_output(unsigned long categories, > >> + struct pagemap_scan_private *p, > >> + unsigned long addr, unsigned long *end) > >> +{ > >> + unsigned long n_pages, total_pages; > >> + int ret =3D 0; > >> + > >> + if (!p->vec_buf) > >> + return 0; > >> + > >> + categories &=3D p->arg.return_mask; > >> + > >> + n_pages =3D (*end - addr) / PAGE_SIZE; > >> + if (check_add_overflow(p->found_pages, n_pages, &total_pages) = || //TODO > > > > Need to fix this TODO. > Sorry, I forgot to remove the "//TODO". As far as I've understood, the la= st > discussion ended in keeping the check_add_overflow(). [1] I'll just remov= e > the TODO. > > https://lore.kernel.org/all/CABb0KFEfmRz+Z_-7GygTL12E5Y254dvoUfWe4uSv9-wO= x+Cs8w@mail.gmail.com > > > > > >> + total_pages > p->arg.max_pages) { > >> + size_t n_too_much =3D total_pages - p->arg.max_pages; > >> + *end -=3D n_too_much * PAGE_SIZE; > >> + n_pages -=3D n_too_much; > >> + ret =3D -ENOSPC; > >> + } > >> + > >> + if (!pagemap_scan_push_range(categories, p, addr, *end)) { > >> + *end =3D addr; > >> + n_pages =3D 0; > >> + ret =3D -ENOSPC; > >> + } > >> + > >> + p->found_pages +=3D n_pages; > >> + if (ret) > >> + p->walk_end_addr =3D *end; > >> + > >> + return ret; > >> +} > >> + > > > > ... > > > >> +static long do_pagemap_scan(struct mm_struct *mm, unsigned long uarg) > >> +{ > >> + struct mmu_notifier_range range; > >> + struct pagemap_scan_private p; > >> + unsigned long walk_start; > >> + size_t n_ranges_out =3D 0; > >> + int ret; > >> + > >> + memset(&p, 0, sizeof(p)); > >> + ret =3D pagemap_scan_get_args(&p.arg, uarg); > >> + if (ret) > >> + return ret; > >> + > >> + p.masks_of_interest =3D MASKS_OF_INTEREST(p.arg); > >> + ret =3D pagemap_scan_init_bounce_buffer(&p); > >> + if (ret) > >> + return ret; > >> + > >> + /* Protection change for the range is going to happen. */ > >> + if (p.arg.flags & PM_SCAN_WP_MATCHING) { > >> + mmu_notifier_range_init(&range, MMU_NOTIFY_PROTECTION_= VMA, 0, > >> + mm, p.arg.start, p.arg.end); > >> + mmu_notifier_invalidate_range_start(&range); > >> + } > >> + > >> + walk_start =3D p.arg.start; > >> + for (; walk_start < p.arg.end; walk_start =3D p.arg.walk_end) = { > >> + int n_out; > >> + > >> + if (fatal_signal_pending(current)) { > >> + ret =3D -EINTR; > >> + break; > >> + } > >> + > >> + ret =3D mmap_read_lock_killable(mm); > >> + if (ret) > >> + break; > >> + ret =3D walk_page_range(mm, walk_start, p.arg.end, > >> + &pagemap_scan_ops, &p); > >> + mmap_read_unlock(mm); > >> + > >> + n_out =3D pagemap_scan_flush_buffer(&p); > >> + if (n_out < 0) > >> + ret =3D n_out; > >> + else > >> + n_ranges_out +=3D n_out; > >> + > >> + if (ret !=3D -ENOSPC || p.arg.vec_len - 1 =3D=3D 0 || > >> + p.found_pages =3D=3D p.arg.max_pages) { > >> + p.walk_end_addr =3D p.arg.end; > > > > You should not change p.walk_end_addr If ret is ENOSPC. Pls add a test > > case to check this. > Yeah, I'm not setting walk_end_addr if ret is ENOSPC. > > I'm setting walk_end_addr only when ret =3D 0. I'd added this as a result= of > a test case in my local test application. I can look at adding some tests > in pagemap_ioctl.c kselftest as well. I am not sure that I understand what you mean here. ENOSPC can be returned when the vec array is full and in this case, walk_end_addr should be the address when it stops scanning. > > > > >> + break; > >> + } > >> + } > >> + > >> + if (p.cur_buf.start !=3D p.cur_buf.end) { > >> + if (copy_to_user(p.vec_out, &p.cur_buf, sizeof(p.cur_b= uf))) > >> + ret =3D -EFAULT; > >> + else > >> + ++n_ranges_out; > >> + } > >> + > >> + /* ENOSPC signifies early stop (buffer full) from the walk. */ > >> + if (!ret || ret =3D=3D -ENOSPC) > >> + ret =3D n_ranges_out; > >> + > >> + p.arg.walk_end =3D p.walk_end_addr ? p.walk_end_addr : walk_st= art; > >> + if (pagemap_scan_writeback_args(&p.arg, uarg)) > >> + ret =3D -EFAULT; > >> + > >> + if (p.arg.flags & PM_SCAN_WP_MATCHING) > >> + mmu_notifier_invalidate_range_end(&range); > >> + > >> + kfree(p.vec_buf); > >> + return ret; > >> +} > > > > Thanks, > > Andrei > > -- > BR, > Muhammad Usama Anjum