From nobody Tue Oct 7 00:22:39 2025 Received: from mail-pl1-f201.google.com (mail-pl1-f201.google.com [209.85.214.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 5A05AEEAB for ; Wed, 16 Jul 2025 03:06:03 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1752635165; cv=none; b=MkxRuQH5Y3pEwGCSgiQsfNK0uMJ4f+OES9Y6yU/3xV8y9TOLmA3VOf9A6DEyQqnJJdCASINMIMwYCgr6G+4JdTc22KxbcByZdOFijkmE9WtzimduqljbozUfwYRF4wQrOIGCeWtg1IlkpYUDmncbCFRCiTDWDbzYP1nWOiVWC5Q= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1752635165; c=relaxed/simple; bh=jRnrVoMZdZS4l+fMWfTBazH3LP8URmBuww3v7sYZO+k=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=ee75MRoaDcFHC0mwKvA/HW29wI64zQ9Fkgz/Qv6MDZOmLvu1Ob84+vqwFQs1PsQGtn5grbDsshb5DxIMcpX484XcDQLtoG8jL5YbF2ZWeQDdou8Aba91ZB+6n7PjUNtxOir+w6Y8/s4VNbgy7U3e/QNUvKEXa3OZplsLDJyTCcQ= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--surenb.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=xbg4ULlj; arc=none smtp.client-ip=209.85.214.201 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--surenb.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="xbg4ULlj" Received: by mail-pl1-f201.google.com with SMTP id d9443c01a7336-2369261224bso66851765ad.0 for ; Tue, 15 Jul 2025 20:06:03 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1752635163; x=1753239963; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=w/p/DdbsN4UfikZMzfuS86WplKsUwY/RIZs4YxJfzTQ=; b=xbg4ULljJ81BE+rn2QW+94GL9rlZOFXGbMgmj8ciQ4nbX5qvD1iB9TdQ8VeQvvPIFd V87MVUOlnXs7mYI6idyzol3rdw/jp/g3ePlsqOYgo0ogT1xs2rvaMHFu75iGpgzEu8oD vLfmrCrLZ//5UohNnlcf+g+r9vaYLqC1iai0Sfr8uECSu2AyhhVJ3842MXEo0ALEMJ6Y 5X3q7LuymEyzevrav9tASVlX8nG4A8jz1uE096T0niTP+uSmjp+iTpZk8mpaPvxrZbDB 8OWIrJkAbKl+3z0pQdciBXyk+FHuaBjYhmDuckUHufMiojgk7C1FhUsLBRnaL2hjdH70 zsPA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1752635163; x=1753239963; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=w/p/DdbsN4UfikZMzfuS86WplKsUwY/RIZs4YxJfzTQ=; b=twMeUc1H0aSnLuyOEezCx9ZRz2s2rKN5azev8JKt7WQ+R+88IhMcIoeh9k6OFoBBf9 +sBx1voiX0aSDjzcxWdC6bu7Xsfkm7zpeI1w/Kog463majh6Pz/qJYBPaRM2dBYmuo5B HEqIAy9vWNB+4CqHjxH0KoqfRE2wqnBNewxtdP0nS+pEohCCL1NLG60Z729raidT40yB toq7P2U7fcq/kL0VXPYXCdeQYioZ4WC6zsGQWyFT0NzX0yAIeGRv/KiulxCq8q6ALpCl uevoV1G9P/Gbe2Q01kVaWG1wECQGG0SSheKAGK5ZUxLtmoQiZvenBgQQ8EZx4CU9qvR9 J7Pg== X-Forwarded-Encrypted: i=1; AJvYcCXd5JJrZx4/Bo9fKFHr3gDDZcUVwUWSa/KdyN//OHoVie0LrCZf8t+VbUZl4sxKFJgAHWkgwjVUrb2s0XQ=@vger.kernel.org X-Gm-Message-State: AOJu0YzQ7CSD/DQHVAZ5cflKaSztKmclstm1xyZXlzEVLJT8IQnYWzEJ /Ohsag8B5h5J7dDfIqpjjl76Ct9vVIN5+Q65Fqjir0JYntTBsS+QYCwi+LgvQRVUvwbLWLdw6Ry 1MjRkWw== X-Google-Smtp-Source: AGHT+IFkGF1q6aNwe/z61qYASsZHamHAnhQ/1g6q37bnaz7nWNnHIZmPUI8XdM35nyPrXnVy2DEFTGAWCzc= X-Received: from pjkk15.prod.google.com ([2002:a17:90b:57ef:b0:31c:2fe4:33b8]) (user=surenb job=prod-delivery.src-stubby-dispatcher) by 2002:a17:903:189:b0:234:a139:1216 with SMTP id d9443c01a7336-23e25763d12mr18236815ad.44.1752635162726; Tue, 15 Jul 2025 20:06:02 -0700 (PDT) Date: Tue, 15 Jul 2025 20:05:50 -0700 In-Reply-To: <20250716030557.1547501-1-surenb@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250716030557.1547501-1-surenb@google.com> X-Mailer: git-send-email 2.50.0.727.gbf7dc18ff4-goog Message-ID: <20250716030557.1547501-2-surenb@google.com> Subject: [PATCH v7 1/7] selftests/proc: add /proc/pid/maps tearing from vma split test From: Suren Baghdasaryan To: akpm@linux-foundation.org Cc: Liam.Howlett@oracle.com, lorenzo.stoakes@oracle.com, david@redhat.com, vbabka@suse.cz, peterx@redhat.com, jannh@google.com, hannes@cmpxchg.org, mhocko@kernel.org, paulmck@kernel.org, shuah@kernel.org, adobriyan@gmail.com, brauner@kernel.org, josef@toxicpanda.com, yebin10@huawei.com, linux@weissschuh.net, willy@infradead.org, osalvador@suse.de, andrii@kernel.org, ryan.roberts@arm.com, christophe.leroy@csgroup.eu, tjmercier@google.com, kaleshsingh@google.com, aha310510@gmail.com, linux-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, linux-kselftest@vger.kernel.org, surenb@google.com Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" The /proc/pid/maps file is generated page by page, with the mmap_lock released between pages. This can lead to inconsistent reads if the underlying vmas are concurrently modified. For instance, if a vma split or merge occurs at a page boundary while /proc/pid/maps is being read, the same vma might be seen twice: once before and once after the change. This duplication is considered acceptable for userspace handling. However, observing a "hole" where a vma should be (e.g., due to a vma being replaced and the space temporarily being empty) is unacceptable. Implement a test that: 1. Forks a child process which continuously modifies its address space, specifically targeting a vma at the boundary between two pages. 2. The parent process repeatedly reads the child's /proc/pid/maps. 3. The parent process checks the last vma of the first page and the first vma of the second page for consistency, looking for the effects of vma splits or merges. The test duration is configurable via the -d command-line parameter in seconds to increase the likelihood of catching the race condition. The default test duration is 5 seconds. Example Command: proc-maps-race -d 10 Signed-off-by: Suren Baghdasaryan --- tools/testing/selftests/proc/.gitignore | 1 + tools/testing/selftests/proc/Makefile | 1 + tools/testing/selftests/proc/proc-maps-race.c | 459 ++++++++++++++++++ 3 files changed, 461 insertions(+) create mode 100644 tools/testing/selftests/proc/proc-maps-race.c diff --git a/tools/testing/selftests/proc/.gitignore b/tools/testing/selfte= sts/proc/.gitignore index 973968f45bba..19bb333e2485 100644 --- a/tools/testing/selftests/proc/.gitignore +++ b/tools/testing/selftests/proc/.gitignore @@ -5,6 +5,7 @@ /proc-2-is-kthread /proc-fsconfig-hidepid /proc-loadavg-001 +/proc-maps-race /proc-multiple-procfs /proc-empty-vm /proc-pid-vm diff --git a/tools/testing/selftests/proc/Makefile b/tools/testing/selftest= s/proc/Makefile index b12921b9794b..50aba102201a 100644 --- a/tools/testing/selftests/proc/Makefile +++ b/tools/testing/selftests/proc/Makefile @@ -9,6 +9,7 @@ TEST_GEN_PROGS +=3D fd-002-posix-eq TEST_GEN_PROGS +=3D fd-003-kthread TEST_GEN_PROGS +=3D proc-2-is-kthread TEST_GEN_PROGS +=3D proc-loadavg-001 +TEST_GEN_PROGS +=3D proc-maps-race TEST_GEN_PROGS +=3D proc-empty-vm TEST_GEN_PROGS +=3D proc-pid-vm TEST_GEN_PROGS +=3D proc-self-map-files-001 diff --git a/tools/testing/selftests/proc/proc-maps-race.c b/tools/testing/= selftests/proc/proc-maps-race.c new file mode 100644 index 000000000000..523afd83d34f --- /dev/null +++ b/tools/testing/selftests/proc/proc-maps-race.c @@ -0,0 +1,459 @@ +/* + * Copyright (c) 2025 Suren Baghdasaryan + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ +/* + * Fork a child that concurrently modifies address space while the main + * process is reading /proc/$PID/maps and verifying the results. Address + * space modifications include: + * VMA splitting and merging + * + */ +#undef NDEBUG +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +static unsigned long test_duration_sec =3D 5UL; +static int page_size; + +/* /proc/pid/maps parsing routines */ +struct page_content { + char *data; + ssize_t size; +}; + +#define LINE_MAX_SIZE 256 + +struct line_content { + char text[LINE_MAX_SIZE]; + unsigned long start_addr; + unsigned long end_addr; +}; + +static void read_two_pages(int maps_fd, struct page_content *page1, + struct page_content *page2) +{ + ssize_t bytes_read; + + assert(lseek(maps_fd, 0, SEEK_SET) >=3D 0); + bytes_read =3D read(maps_fd, page1->data, page_size); + assert(bytes_read > 0 && bytes_read < page_size); + page1->size =3D bytes_read; + + bytes_read =3D read(maps_fd, page2->data, page_size); + assert(bytes_read > 0 && bytes_read < page_size); + page2->size =3D bytes_read; +} + +static void copy_first_line(struct page_content *page, char *first_line) +{ + char *pos =3D strchr(page->data, '\n'); + + strncpy(first_line, page->data, pos - page->data); + first_line[pos - page->data] =3D '\0'; +} + +static void copy_last_line(struct page_content *page, char *last_line) +{ + /* Get the last line in the first page */ + const char *end =3D page->data + page->size - 1; + /* skip last newline */ + const char *pos =3D end - 1; + + /* search previous newline */ + while (pos[-1] !=3D '\n') + pos--; + strncpy(last_line, pos, end - pos); + last_line[end - pos] =3D '\0'; +} + +/* Read the last line of the first page and the first line of the second p= age */ +static void read_boundary_lines(int maps_fd, struct page_content *page1, + struct page_content *page2, + struct line_content *last_line, + struct line_content *first_line) +{ + read_two_pages(maps_fd, page1, page2); + + copy_last_line(page1, last_line->text); + copy_first_line(page2, first_line->text); + + assert(sscanf(last_line->text, "%lx-%lx", &last_line->start_addr, + &last_line->end_addr) =3D=3D 2); + assert(sscanf(first_line->text, "%lx-%lx", &first_line->start_addr, + &first_line->end_addr) =3D=3D 2); +} + +/* Thread synchronization routines */ +enum test_state { + INIT, + CHILD_READY, + PARENT_READY, + SETUP_READY, + SETUP_MODIFY_MAPS, + SETUP_MAPS_MODIFIED, + SETUP_RESTORE_MAPS, + SETUP_MAPS_RESTORED, + TEST_READY, + TEST_DONE, +}; + +struct vma_modifier_info; + +typedef void (*vma_modifier_op)(const struct vma_modifier_info *mod_info); +typedef void (*vma_mod_result_check_op)(struct line_content *mod_last_line, + struct line_content *mod_first_line, + struct line_content *restored_last_line, + struct line_content *restored_first_line); + +struct vma_modifier_info { + int vma_count; + void *addr; + int prot; + void *next_addr; + vma_modifier_op vma_modify; + vma_modifier_op vma_restore; + vma_mod_result_check_op vma_mod_check; + pthread_mutex_t sync_lock; + pthread_cond_t sync_cond; + enum test_state curr_state; + bool exit; + void *child_mapped_addr[]; +}; + +static void wait_for_state(struct vma_modifier_info *mod_info, enum test_s= tate state) +{ + pthread_mutex_lock(&mod_info->sync_lock); + while (mod_info->curr_state !=3D state) + pthread_cond_wait(&mod_info->sync_cond, &mod_info->sync_lock); + pthread_mutex_unlock(&mod_info->sync_lock); +} + +static void signal_state(struct vma_modifier_info *mod_info, enum test_sta= te state) +{ + pthread_mutex_lock(&mod_info->sync_lock); + mod_info->curr_state =3D state; + pthread_cond_signal(&mod_info->sync_cond); + pthread_mutex_unlock(&mod_info->sync_lock); +} + +/* VMA modification routines */ +static void *child_vma_modifier(struct vma_modifier_info *mod_info) +{ + int prot =3D PROT_READ | PROT_WRITE; + int i; + + for (i =3D 0; i < mod_info->vma_count; i++) { + mod_info->child_mapped_addr[i] =3D mmap(NULL, page_size * 3, prot, + MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); + assert(mod_info->child_mapped_addr[i] !=3D MAP_FAILED); + /* change protection in adjacent maps to prevent merging */ + prot ^=3D PROT_WRITE; + } + signal_state(mod_info, CHILD_READY); + wait_for_state(mod_info, PARENT_READY); + while (true) { + signal_state(mod_info, SETUP_READY); + wait_for_state(mod_info, SETUP_MODIFY_MAPS); + if (mod_info->exit) + break; + + mod_info->vma_modify(mod_info); + signal_state(mod_info, SETUP_MAPS_MODIFIED); + wait_for_state(mod_info, SETUP_RESTORE_MAPS); + mod_info->vma_restore(mod_info); + signal_state(mod_info, SETUP_MAPS_RESTORED); + + wait_for_state(mod_info, TEST_READY); + while (mod_info->curr_state !=3D TEST_DONE) { + mod_info->vma_modify(mod_info); + mod_info->vma_restore(mod_info); + } + } + for (i =3D 0; i < mod_info->vma_count; i++) + munmap(mod_info->child_mapped_addr[i], page_size * 3); + + return NULL; +} + +static void stop_vma_modifier(struct vma_modifier_info *mod_info) +{ + wait_for_state(mod_info, SETUP_READY); + mod_info->exit =3D true; + signal_state(mod_info, SETUP_MODIFY_MAPS); +} + +static void capture_mod_pattern(int maps_fd, + struct vma_modifier_info *mod_info, + struct page_content *page1, + struct page_content *page2, + struct line_content *last_line, + struct line_content *first_line, + struct line_content *mod_last_line, + struct line_content *mod_first_line, + struct line_content *restored_last_line, + struct line_content *restored_first_line) +{ + signal_state(mod_info, SETUP_MODIFY_MAPS); + wait_for_state(mod_info, SETUP_MAPS_MODIFIED); + + /* Copy last line of the first page and first line of the last page */ + read_boundary_lines(maps_fd, page1, page2, mod_last_line, mod_first_line); + + signal_state(mod_info, SETUP_RESTORE_MAPS); + wait_for_state(mod_info, SETUP_MAPS_RESTORED); + + /* Copy last line of the first page and first line of the last page */ + read_boundary_lines(maps_fd, page1, page2, restored_last_line, restored_f= irst_line); + + mod_info->vma_mod_check(mod_last_line, mod_first_line, + restored_last_line, restored_first_line); + + /* + * The content of these lines after modify+resore should be the same + * as the original. + */ + assert(strcmp(restored_last_line->text, last_line->text) =3D=3D 0); + assert(strcmp(restored_first_line->text, first_line->text) =3D=3D 0); +} + +static inline void split_vma(const struct vma_modifier_info *mod_info) +{ + assert(mmap(mod_info->addr, page_size, mod_info->prot | PROT_EXEC, + MAP_PRIVATE | MAP_ANONYMOUS | MAP_FIXED, + -1, 0) !=3D MAP_FAILED); +} + +static inline void merge_vma(const struct vma_modifier_info *mod_info) +{ + assert(mmap(mod_info->addr, page_size, mod_info->prot, + MAP_PRIVATE | MAP_ANONYMOUS | MAP_FIXED, + -1, 0) !=3D MAP_FAILED); +} + +static inline void check_split_result(struct line_content *mod_last_line, + struct line_content *mod_first_line, + struct line_content *restored_last_line, + struct line_content *restored_first_line) +{ + /* Make sure vmas at the boundaries are changing */ + assert(strcmp(mod_last_line->text, restored_last_line->text) !=3D 0); + assert(strcmp(mod_first_line->text, restored_first_line->text) !=3D 0); +} + +static void test_maps_tearing_from_split(int maps_fd, + struct vma_modifier_info *mod_info, + struct page_content *page1, + struct page_content *page2, + struct line_content *last_line, + struct line_content *first_line) +{ + struct line_content split_last_line; + struct line_content split_first_line; + struct line_content restored_last_line; + struct line_content restored_first_line; + + wait_for_state(mod_info, SETUP_READY); + + /* re-read the file to avoid using stale data from previous test */ + read_boundary_lines(maps_fd, page1, page2, last_line, first_line); + + mod_info->vma_modify =3D split_vma; + mod_info->vma_restore =3D merge_vma; + mod_info->vma_mod_check =3D check_split_result; + + capture_mod_pattern(maps_fd, mod_info, page1, page2, last_line, first_lin= e, + &split_last_line, &split_first_line, + &restored_last_line, &restored_first_line); + + /* Now start concurrent modifications for test_duration_sec */ + signal_state(mod_info, TEST_READY); + + struct line_content new_last_line; + struct line_content new_first_line; + struct timespec start_ts, end_ts; + + clock_gettime(CLOCK_MONOTONIC_COARSE, &start_ts); + do { + bool last_line_changed; + bool first_line_changed; + + read_boundary_lines(maps_fd, page1, page2, &new_last_line, &new_first_li= ne); + + /* Check if we read vmas after split */ + if (!strcmp(new_last_line.text, split_last_line.text)) { + /* + * The vmas should be consistent with split results, + * however if vma was concurrently restored after a + * split, it can be reported twice (first the original + * split one, then the same vma but extended after the + * merge) because we found it as the next vma again. + * In that case new first line will be the same as the + * last restored line. + */ + assert(!strcmp(new_first_line.text, split_first_line.text) || + !strcmp(new_first_line.text, restored_last_line.text)); + } else { + /* The vmas should be consistent with merge results */ + assert(!strcmp(new_last_line.text, restored_last_line.text) && + !strcmp(new_first_line.text, restored_first_line.text)); + } + /* + * First and last lines should change in unison. If the last + * line changed then the first line should change as well and + * vice versa. + */ + last_line_changed =3D strcmp(new_last_line.text, last_line->text) !=3D 0; + first_line_changed =3D strcmp(new_first_line.text, first_line->text) != =3D 0; + assert(last_line_changed =3D=3D first_line_changed); + + clock_gettime(CLOCK_MONOTONIC_COARSE, &end_ts); + } while (end_ts.tv_sec - start_ts.tv_sec < test_duration_sec); + + /* Signal the modifyer thread to stop and wait until it exits */ + signal_state(mod_info, TEST_DONE); +} + +int usage(void) +{ + fprintf(stderr, "Userland /proc/pid/{s}maps race test cases\n"); + fprintf(stderr, " -d: Duration for time-consuming tests\n"); + fprintf(stderr, " -h: Help screen\n"); + exit(-1); +} + +int main(int argc, char **argv) +{ + struct vma_modifier_info *mod_info; + pthread_mutexattr_t mutex_attr; + pthread_condattr_t cond_attr; + int shared_mem_size; + char fname[32]; + int vma_count; + int maps_fd; + int status; + pid_t pid; + int opt; + + while ((opt =3D getopt(argc, argv, "d:h")) !=3D -1) { + if (opt =3D=3D 'd') + test_duration_sec =3D strtoul(optarg, NULL, 0); + else if (opt =3D=3D 'h') + usage(); + } + + page_size =3D sysconf(_SC_PAGESIZE); + /* + * Have to map enough vmas for /proc/pid/maps to contain more than one + * page worth of vmas. Assume at least 32 bytes per line in maps output + */ + vma_count =3D page_size / 32 + 1; + shared_mem_size =3D sizeof(struct vma_modifier_info) + vma_count * sizeof= (void *); + + /* map shared memory for communication with the child process */ + mod_info =3D (struct vma_modifier_info *)mmap(NULL, shared_mem_size, + PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANONYMOUS, -1, 0); + + assert(mod_info !=3D MAP_FAILED); + + /* Initialize shared members */ + pthread_mutexattr_init(&mutex_attr); + pthread_mutexattr_setpshared(&mutex_attr, PTHREAD_PROCESS_SHARED); + assert(!pthread_mutex_init(&mod_info->sync_lock, &mutex_attr)); + pthread_condattr_init(&cond_attr); + pthread_condattr_setpshared(&cond_attr, PTHREAD_PROCESS_SHARED); + assert(!pthread_cond_init(&mod_info->sync_cond, &cond_attr)); + mod_info->vma_count =3D vma_count; + mod_info->curr_state =3D INIT; + mod_info->exit =3D false; + + pid =3D fork(); + if (!pid) { + /* Child process */ + child_vma_modifier(mod_info); + return 0; + } + + sprintf(fname, "/proc/%d/maps", pid); + maps_fd =3D open(fname, O_RDONLY); + assert(maps_fd !=3D -1); + + /* Wait for the child to map the VMAs */ + wait_for_state(mod_info, CHILD_READY); + + /* Read first two pages */ + struct page_content page1; + struct page_content page2; + + page1.data =3D malloc(page_size); + assert(page1.data); + page2.data =3D malloc(page_size); + assert(page2.data); + + struct line_content last_line; + struct line_content first_line; + + read_boundary_lines(maps_fd, &page1, &page2, &last_line, &first_line); + + /* + * Find the addresses corresponding to the last line in the first page + * and the first line in the last page. + */ + mod_info->addr =3D NULL; + mod_info->next_addr =3D NULL; + for (int i =3D 0; i < mod_info->vma_count; i++) { + if (mod_info->child_mapped_addr[i] =3D=3D (void *)last_line.start_addr) { + mod_info->addr =3D mod_info->child_mapped_addr[i]; + mod_info->prot =3D PROT_READ; + /* Even VMAs have write permission */ + if ((i % 2) =3D=3D 0) + mod_info->prot |=3D PROT_WRITE; + } else if (mod_info->child_mapped_addr[i] =3D=3D (void *)first_line.star= t_addr) { + mod_info->next_addr =3D mod_info->child_mapped_addr[i]; + } + + if (mod_info->addr && mod_info->next_addr) + break; + } + assert(mod_info->addr && mod_info->next_addr); + + signal_state(mod_info, PARENT_READY); + + test_maps_tearing_from_split(maps_fd, mod_info, &page1, &page2, + &last_line, &first_line); + + stop_vma_modifier(mod_info); + + free(page2.data); + free(page1.data); + + for (int i =3D 0; i < vma_count; i++) + munmap(mod_info->child_mapped_addr[i], page_size); + close(maps_fd); + waitpid(pid, &status, 0); + munmap(mod_info, shared_mem_size); + + return 0; +} --=20 2.50.0.727.gbf7dc18ff4-goog From nobody Tue Oct 7 00:22:39 2025 Received: from mail-pf1-f202.google.com (mail-pf1-f202.google.com [209.85.210.202]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 63126278E67 for ; Wed, 16 Jul 2025 03:06:05 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1752635166; cv=none; b=Orm7Ttc+1v9N8GzXk2RLdz0rTbnI7FxVDTRTv7oo39UpeHKqNhSN/P2IGqD6Ro4qm4fs79BD0mYp6ZzCjk5Iz2fHsdKbDFpwDY98RARn25FF9q3BEde3msvPzzu+0tXMU4HoMxDYXb074N9Vl4xznUoI+qbt7okznU3XY3EQ/SA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1752635166; c=relaxed/simple; bh=ulewSdFhhtT6XYUvitzcBD75LHyogn8pm3lfZUrI9VI=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=ht9JoCBlq6WSxBbGm99V69z/QCfpJoCJyiQRLE8uf1bKPDguiYtvatPS0MABK7lljC9KPiAuoh56wbXKbeumECsrXtVuoTGI8PnUzuugum2mESBsB9MOcq2wiqn2LIlaJz5fMpGKx6Ai3IXV5YvkLCESFCpP+dMm8f9AVK4L36Q= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--surenb.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=V0dnLROl; arc=none smtp.client-ip=209.85.210.202 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--surenb.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="V0dnLROl" Received: by mail-pf1-f202.google.com with SMTP id d2e1a72fcca58-748efefedb5so5699884b3a.2 for ; Tue, 15 Jul 2025 20:06:05 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1752635165; x=1753239965; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=OwmJhXkBZ0rkJP8ZDJsJXdxfIUoApnDNh42agcrLiOc=; b=V0dnLROlkVW0y8NdwsHaDCXGaMfByNPj+qyzbFf3f/hJ7wzDRX8wtUqJ2gfxQqK3QY H3LEtdB5aHfFR+TXXbUpI+VYyZmduSx5xOY5cxraVsJIjS5oYPKVBmM8u14kF6R6aRHo To4vlHaVbANfdrgitBmYxT8QExlrczbPTwK2bgMEK4rHVCEg9297+XpnzDXP6GlJTCHW 7Up5HwH6m4qpjxLgsPYuoR7a83bTlpmg7q2ZL7EHsjl0jtfIU1sY/lKApoKBLFWzHrmF N3/CVFU6YDgijeb5GF4/p+nHe6MMqqPtqnSxb68Mi54vxPoZ/kiG2rhqVs09IKME/C/f gmKA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1752635165; x=1753239965; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=OwmJhXkBZ0rkJP8ZDJsJXdxfIUoApnDNh42agcrLiOc=; b=SMrg8gtYp+VN02QwvmhXks4Y4JpJmBLou3GF6REslVyHhK2I5hWLluYycMiF+pdvja 6h0xpfun+v+nZIY+qo+cH9JEbUE+eEs7wyDjVwEH/3A51Mjzm6SXwbCD+XfIIGOyQdlT zQ2B8BhoRuzlsmSwZdRYPfy0JJ/oEoxksqKXu2Q94hBbt4+dNkd0lw5+eX/ylvhMI3OA 9Ec/z3skCf0zqWjhgPvBwqpH/iz1daxqfPKYS5W2scui/H/2CUvQj+d14JK3UdZOkK3s Cc0116X/CKLT9re/y9Tj4hxCm3e7WunS0R3N1xkCdMr9kXfJerKeaM/0isjEp99nh58c Lrww== X-Forwarded-Encrypted: i=1; AJvYcCWL7gOuZw1Kiw8xP2apFQzlrVr+fWyLAbChp9DMQU0g3RqPNwY8gW9/2X96n5KwgYh3Lurts0eQgDfisnM=@vger.kernel.org X-Gm-Message-State: AOJu0YwRGM67e3+3xvugPbMR7aP/6M/woaWcZVzU2wa2SB2d48VMtnuV YkdAPOy8qZKrH7qByzWWi1HKtY1fCmPvZAhTXJ20YiHjbJG40IGhhw5pLn3YzGO7XHFaZtt2p5D SQUWb9w== X-Google-Smtp-Source: AGHT+IGzJv3h9zkM0+zyuvpyi7dydp51mDl7cBy864qKJxNT3Tnay4b8zYqfF0oK2BvdPup3ers/Chkr8aM= X-Received: from pgkg9.prod.google.com ([2002:a63:fa49:0:b0:b2f:9d37:5753]) (user=surenb job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a20:160f:b0:21f:751a:dd41 with SMTP id adf61e73a8af0-237d896b159mr2950162637.40.1752635164680; Tue, 15 Jul 2025 20:06:04 -0700 (PDT) Date: Tue, 15 Jul 2025 20:05:51 -0700 In-Reply-To: <20250716030557.1547501-1-surenb@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250716030557.1547501-1-surenb@google.com> X-Mailer: git-send-email 2.50.0.727.gbf7dc18ff4-goog Message-ID: <20250716030557.1547501-3-surenb@google.com> Subject: [PATCH v7 2/7] selftests/proc: extend /proc/pid/maps tearing test to include vma resizing From: Suren Baghdasaryan To: akpm@linux-foundation.org Cc: Liam.Howlett@oracle.com, lorenzo.stoakes@oracle.com, david@redhat.com, vbabka@suse.cz, peterx@redhat.com, jannh@google.com, hannes@cmpxchg.org, mhocko@kernel.org, paulmck@kernel.org, shuah@kernel.org, adobriyan@gmail.com, brauner@kernel.org, josef@toxicpanda.com, yebin10@huawei.com, linux@weissschuh.net, willy@infradead.org, osalvador@suse.de, andrii@kernel.org, ryan.roberts@arm.com, christophe.leroy@csgroup.eu, tjmercier@google.com, kaleshsingh@google.com, aha310510@gmail.com, linux-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, linux-kselftest@vger.kernel.org, surenb@google.com Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Test that /proc/pid/maps does not report unexpected holes in the address space when a vma at the edge of the page is being concurrently remapped. This remapping results in the vma shrinking and expanding from under the reader. We should always see either shrunk or expanded (original) version of the vma. Signed-off-by: Suren Baghdasaryan --- tools/testing/selftests/proc/proc-maps-race.c | 83 +++++++++++++++++++ 1 file changed, 83 insertions(+) diff --git a/tools/testing/selftests/proc/proc-maps-race.c b/tools/testing/= selftests/proc/proc-maps-race.c index 523afd83d34f..10365b4e68e1 100644 --- a/tools/testing/selftests/proc/proc-maps-race.c +++ b/tools/testing/selftests/proc/proc-maps-race.c @@ -336,6 +336,86 @@ static void test_maps_tearing_from_split(int maps_fd, signal_state(mod_info, TEST_DONE); } =20 +static inline void shrink_vma(const struct vma_modifier_info *mod_info) +{ + assert(mremap(mod_info->addr, page_size * 3, page_size, 0) !=3D MAP_FAILE= D); +} + +static inline void expand_vma(const struct vma_modifier_info *mod_info) +{ + assert(mremap(mod_info->addr, page_size, page_size * 3, 0) !=3D MAP_FAILE= D); +} + +static inline void check_shrink_result(struct line_content *mod_last_line, + struct line_content *mod_first_line, + struct line_content *restored_last_line, + struct line_content *restored_first_line) +{ + /* Make sure only the last vma of the first page is changing */ + assert(strcmp(mod_last_line->text, restored_last_line->text) !=3D 0); + assert(strcmp(mod_first_line->text, restored_first_line->text) =3D=3D 0); +} + +static void test_maps_tearing_from_resize(int maps_fd, + struct vma_modifier_info *mod_info, + struct page_content *page1, + struct page_content *page2, + struct line_content *last_line, + struct line_content *first_line) +{ + struct line_content shrunk_last_line; + struct line_content shrunk_first_line; + struct line_content restored_last_line; + struct line_content restored_first_line; + + wait_for_state(mod_info, SETUP_READY); + + /* re-read the file to avoid using stale data from previous test */ + read_boundary_lines(maps_fd, page1, page2, last_line, first_line); + + mod_info->vma_modify =3D shrink_vma; + mod_info->vma_restore =3D expand_vma; + mod_info->vma_mod_check =3D check_shrink_result; + + capture_mod_pattern(maps_fd, mod_info, page1, page2, last_line, first_lin= e, + &shrunk_last_line, &shrunk_first_line, + &restored_last_line, &restored_first_line); + + /* Now start concurrent modifications for test_duration_sec */ + signal_state(mod_info, TEST_READY); + + struct line_content new_last_line; + struct line_content new_first_line; + struct timespec start_ts, end_ts; + + clock_gettime(CLOCK_MONOTONIC_COARSE, &start_ts); + do { + read_boundary_lines(maps_fd, page1, page2, &new_last_line, &new_first_li= ne); + + /* Check if we read vmas after shrinking it */ + if (!strcmp(new_last_line.text, shrunk_last_line.text)) { + /* + * The vmas should be consistent with shrunk results, + * however if the vma was concurrently restored, it + * can be reported twice (first as shrunk one, then + * as restored one) because we found it as the next vma + * again. In that case new first line will be the same + * as the last restored line. + */ + assert(!strcmp(new_first_line.text, shrunk_first_line.text) || + !strcmp(new_first_line.text, restored_last_line.text)); + } else { + /* The vmas should be consistent with the original/resored state */ + assert(!strcmp(new_last_line.text, restored_last_line.text) && + !strcmp(new_first_line.text, restored_first_line.text)); + } + clock_gettime(CLOCK_MONOTONIC_COARSE, &end_ts); + } while (end_ts.tv_sec - start_ts.tv_sec < test_duration_sec); + + /* Signal the modifyer thread to stop and wait until it exits */ + signal_state(mod_info, TEST_DONE); +} + int usage(void) { fprintf(stderr, "Userland /proc/pid/{s}maps race test cases\n"); @@ -444,6 +524,9 @@ int main(int argc, char **argv) test_maps_tearing_from_split(maps_fd, mod_info, &page1, &page2, &last_line, &first_line); =20 + test_maps_tearing_from_resize(maps_fd, mod_info, &page1, &page2, + &last_line, &first_line); + stop_vma_modifier(mod_info); =20 free(page2.data); --=20 2.50.0.727.gbf7dc18ff4-goog From nobody Tue Oct 7 00:22:39 2025 Received: from mail-pf1-f202.google.com (mail-pf1-f202.google.com [209.85.210.202]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id AAE16275118 for ; Wed, 16 Jul 2025 03:06:07 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1752635169; cv=none; b=nv7YO60v1aTXWp1X0uTqANr/qHfvnDULTYoKQUQOQYgSICoBoBy1Ibko2JqbyWhJs6otOJdeSXPQnLMq3CYDsRVryS29eSeXI1d2IiD3VRxB1XnQSPysmMWDR6tDJum+tcrnFWkRsVE4D45KFjQuK8trNAF6GXB3afni8e8Py18= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1752635169; c=relaxed/simple; bh=ZMGFntAQnjjtMoQxDdiUKIBvPxmH73C1FhJ2CMHVp1Y=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=sff1OgBW7k5/lp+KAqVBpIRsv9pKb6yoIjdx1c+xxce1sFDlHnBuZxN4OZUdlgCCNwLEy8guy1b94rLTCC7PXOQVay08wZ+2esSjPw+sa0AcMrRg3rLfwwF2gIjfM83JFUAoyVD+T94e+Lo1J7zH5deyQbvXRcqtvQl0pllThR4= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--surenb.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=UlvXGTil; arc=none smtp.client-ip=209.85.210.202 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--surenb.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="UlvXGTil" Received: by mail-pf1-f202.google.com with SMTP id d2e1a72fcca58-74ad608d60aso5328177b3a.1 for ; Tue, 15 Jul 2025 20:06:07 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1752635167; x=1753239967; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=Li63ssKppcLITl1ITfej/YujyWpoLXNJQdKFDqdnOTk=; b=UlvXGTilHnFbR6Bw0/ZQtPiJApwjvLaRyQRdmJZM2Ne28bVk6pDu6fYOK2wY54rWhq 8yY0vjE4qSMirX/xtCXr/fI8V6ePWRPXCJJHV5fRpo3WO7om3q1K+SphTIJz1aoqXELA mJ6wZ8cxjHC6LhFOLwClEU3s1FyRWBUN8kXyie4xj2BOls1mD0lXayVnzyiRDGIGucPY Fht5ZEjei0Cxo3/RVz6JDIGz+98VLbccpn9S96ie7BObwvKDLRk5wLOqroVGMazrZqTO QOxA3EnrVsqyEGnyyUwHFTnPxq5jDod8Nlfhn3bO4rSEMJdHxmqNOAqnl6yoHGGy7HtT 1j8w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1752635167; x=1753239967; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=Li63ssKppcLITl1ITfej/YujyWpoLXNJQdKFDqdnOTk=; b=bIlqdTOF4TGnXDz7aausPlmOIxBSzH9zKSLQ3OULts4SbM3mNcef4ZcXJsYS1JAQyS 0rcS+khDvl4650SgR/p088SDxROLeByIir2qh+ipm08YbSq+zN5hlIB7FUE9qIuRAcfI aPf6WEYjgdO/QPYqVW5b/7hErCuv1PfhIaA79KvQJqDWnShVVXn9mJNy9yh1nLc4pwdM w32sA0jOmWNktjZDSQIZpnjOcFj8XKSyVT6M8zQfYYFXH95+x1QJEbOWJg9AzyY9MgJR AsJa/aZg/zOPI1cEVPVSVS75DnF8Bqom9hqjExwrCfCnuGiGEFSbYrB/G+FufVIsfSCM sbVA== X-Forwarded-Encrypted: i=1; AJvYcCWmA9ndw9kLB0rNeJA00lNqZPN/L7TXCWNyc3KRaXNMu1Rcowgx2RA5e4t68ddqppcO4pQBFjktHTSKBSQ=@vger.kernel.org X-Gm-Message-State: AOJu0Yw1tr+Tz7wy0n9yEfbICvQDkWZkt8DYFTdjOeMJPk4HlDPggJM7 WMtNJhgdrX65ElfyrD5xhKR93HVqAxqhZhM+f+cEPmbCa6Vg3Gr0pR020dYZqNToZMsh7LNm02J WgNBZYA== X-Google-Smtp-Source: AGHT+IHN4KjxRUYFy2gmozTCR7TK1ofpV5QP2mqF+y0rY8P1k6j/G7f8VA9mG0zvAsbo/1BgyAC1yTm//HE= X-Received: from pfvb10.prod.google.com ([2002:a05:6a00:cca:b0:748:f270:c438]) (user=surenb job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a00:982:b0:74e:c917:3b1a with SMTP id d2e1a72fcca58-75724c805c8mr1488713b3a.24.1752635166933; Tue, 15 Jul 2025 20:06:06 -0700 (PDT) Date: Tue, 15 Jul 2025 20:05:52 -0700 In-Reply-To: <20250716030557.1547501-1-surenb@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250716030557.1547501-1-surenb@google.com> X-Mailer: git-send-email 2.50.0.727.gbf7dc18ff4-goog Message-ID: <20250716030557.1547501-4-surenb@google.com> Subject: [PATCH v7 3/7] selftests/proc: extend /proc/pid/maps tearing test to include vma remapping From: Suren Baghdasaryan To: akpm@linux-foundation.org Cc: Liam.Howlett@oracle.com, lorenzo.stoakes@oracle.com, david@redhat.com, vbabka@suse.cz, peterx@redhat.com, jannh@google.com, hannes@cmpxchg.org, mhocko@kernel.org, paulmck@kernel.org, shuah@kernel.org, adobriyan@gmail.com, brauner@kernel.org, josef@toxicpanda.com, yebin10@huawei.com, linux@weissschuh.net, willy@infradead.org, osalvador@suse.de, andrii@kernel.org, ryan.roberts@arm.com, christophe.leroy@csgroup.eu, tjmercier@google.com, kaleshsingh@google.com, aha310510@gmail.com, linux-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, linux-kselftest@vger.kernel.org, surenb@google.com Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Test that /proc/pid/maps does not report unexpected holes in the address space when we concurrently remap a part of a vma into the middle of another vma. This remapping results in the destination vma being split into three parts and the part in the middle being patched back from, all done concurrently from under the reader. We should always see either original vma or the split one with no holes. Signed-off-by: Suren Baghdasaryan --- tools/testing/selftests/proc/proc-maps-race.c | 92 +++++++++++++++++++ 1 file changed, 92 insertions(+) diff --git a/tools/testing/selftests/proc/proc-maps-race.c b/tools/testing/= selftests/proc/proc-maps-race.c index 10365b4e68e1..764821ffd63d 100644 --- a/tools/testing/selftests/proc/proc-maps-race.c +++ b/tools/testing/selftests/proc/proc-maps-race.c @@ -416,6 +416,95 @@ static void test_maps_tearing_from_resize(int maps_fd, signal_state(mod_info, TEST_DONE); } =20 +static inline void remap_vma(const struct vma_modifier_info *mod_info) +{ + /* + * Remap the last page of the next vma into the middle of the vma. + * This splits the current vma and the first and middle parts (the + * parts at lower addresses) become the last vma objserved in the + * first page and the first vma observed in the last page. + */ + assert(mremap(mod_info->next_addr + page_size * 2, page_size, + page_size, MREMAP_FIXED | MREMAP_MAYMOVE | MREMAP_DONTUNMAP, + mod_info->addr + page_size) !=3D MAP_FAILED); +} + +static inline void patch_vma(const struct vma_modifier_info *mod_info) +{ + assert(!mprotect(mod_info->addr + page_size, page_size, + mod_info->prot)); +} + +static inline void check_remap_result(struct line_content *mod_last_line, + struct line_content *mod_first_line, + struct line_content *restored_last_line, + struct line_content *restored_first_line) +{ + /* Make sure vmas at the boundaries are changing */ + assert(strcmp(mod_last_line->text, restored_last_line->text) !=3D 0); + assert(strcmp(mod_first_line->text, restored_first_line->text) !=3D 0); +} + +static void test_maps_tearing_from_remap(int maps_fd, + struct vma_modifier_info *mod_info, + struct page_content *page1, + struct page_content *page2, + struct line_content *last_line, + struct line_content *first_line) +{ + struct line_content remapped_last_line; + struct line_content remapped_first_line; + struct line_content restored_last_line; + struct line_content restored_first_line; + + wait_for_state(mod_info, SETUP_READY); + + /* re-read the file to avoid using stale data from previous test */ + read_boundary_lines(maps_fd, page1, page2, last_line, first_line); + + mod_info->vma_modify =3D remap_vma; + mod_info->vma_restore =3D patch_vma; + mod_info->vma_mod_check =3D check_remap_result; + + capture_mod_pattern(maps_fd, mod_info, page1, page2, last_line, first_lin= e, + &remapped_last_line, &remapped_first_line, + &restored_last_line, &restored_first_line); + + /* Now start concurrent modifications for test_duration_sec */ + signal_state(mod_info, TEST_READY); + + struct line_content new_last_line; + struct line_content new_first_line; + struct timespec start_ts, end_ts; + + clock_gettime(CLOCK_MONOTONIC_COARSE, &start_ts); + do { + read_boundary_lines(maps_fd, page1, page2, &new_last_line, &new_first_li= ne); + + /* Check if we read vmas after remapping it */ + if (!strcmp(new_last_line.text, remapped_last_line.text)) { + /* + * The vmas should be consistent with remap results, + * however if the vma was concurrently restored, it + * can be reported twice (first as split one, then + * as restored one) because we found it as the next vma + * again. In that case new first line will be the same + * as the last restored line. + */ + assert(!strcmp(new_first_line.text, remapped_first_line.text) || + !strcmp(new_first_line.text, restored_last_line.text)); + } else { + /* The vmas should be consistent with the original/resored state */ + assert(!strcmp(new_last_line.text, restored_last_line.text) && + !strcmp(new_first_line.text, restored_first_line.text)); + } + clock_gettime(CLOCK_MONOTONIC_COARSE, &end_ts); + } while (end_ts.tv_sec - start_ts.tv_sec < test_duration_sec); + + /* Signal the modifyer thread to stop and wait until it exits */ + signal_state(mod_info, TEST_DONE); +} + int usage(void) { fprintf(stderr, "Userland /proc/pid/{s}maps race test cases\n"); @@ -527,6 +616,9 @@ int main(int argc, char **argv) test_maps_tearing_from_resize(maps_fd, mod_info, &page1, &page2, &last_line, &first_line); =20 + test_maps_tearing_from_remap(maps_fd, mod_info, &page1, &page2, + &last_line, &first_line); + stop_vma_modifier(mod_info); =20 free(page2.data); --=20 2.50.0.727.gbf7dc18ff4-goog From nobody Tue Oct 7 00:22:39 2025 Received: from mail-pf1-f201.google.com (mail-pf1-f201.google.com [209.85.210.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id DAFAA27F4F5 for ; Wed, 16 Jul 2025 03:06:09 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1752635171; cv=none; b=utTTCFXB/A6kwfeI61mavZTBXmCxOr6QN8RpN9O1rgwA6se7pxJTY7+YgWsa2LxCrq9zv3ulFzPiT4um1Y0azCUAe+H26Mgs58fD6JzDAGdZhxPjSCBh3HnpeirGNOcBmgN9zK8BGWzdG5luTcZRzkooCziL6QO8nUaWOSmMSPY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1752635171; c=relaxed/simple; bh=6/KDaBp6ZM8MWS9qb0wO08Ku16BqGmytBKM3lNMr5qw=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=pENZ5K5agCdFHnce3Atnv9fbwG3JY3Zvp6n3oWPcsfkh1a8Udugkx+ATUifxiuffPQFfo2S+x7Y4xAd9REECxMUJVg8KMOvsUWd3oSonQHoDR/MJPngcdrG3aYiL9MurLntr70ptyX5sRxLjvt+Btyk4oRZyz6ydkbs6yP8eYT4= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--surenb.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=KaWk0017; arc=none smtp.client-ip=209.85.210.201 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--surenb.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="KaWk0017" Received: by mail-pf1-f201.google.com with SMTP id d2e1a72fcca58-748f13ef248so5810314b3a.3 for ; Tue, 15 Jul 2025 20:06:09 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1752635169; x=1753239969; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=Wz+/Rb5/fWdnaWsOelI+A9jPFxhtbRCP90y3FqomvrU=; b=KaWk0017C6IjSQ4BJrwiv4wom+ZjWM2sI0fgEkOhuC5QsUM8bgbZxakLwDST6UhFS6 qgSbExvo8QTdqHf3grNF3aNkEr3XHc6bt3JqE2o26MeyDSkOF+j3EG6FtJy9yC5XDSL4 iKVEzJo/RRvOoM5eOF/oply6YPUevPaNnaMQVzI1Oq/0H0XsWI/rzfS3jNd002aD/jdo k1ORw5CEOP0KV6o1NnqjBoYHG+TWIEJeB8SZ1WEHqjRRk19omWP7RV6NLM+wiQ6UwzeL 3b0CoPZzux6fD7jRRzF6Vn4d8qtz+8OEIFmJ2Uk+htW5RZSAhAHnjQMIolIQp5IAq0xB jC5w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1752635169; x=1753239969; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=Wz+/Rb5/fWdnaWsOelI+A9jPFxhtbRCP90y3FqomvrU=; b=lg8UWQT7L3PAakyOANTFRuqbLSAr37F/O8utPvrMfEU/vq5t5p7FKt7mC1xo2HVs12 VPfdYcJQYKafAY8cr37hN5L+IinChcMH/4gUMRIXNAZN/HiF6v0kNWlDO4MIWdQwOarS p0BDKe4XQki+zdbDQ4mYD77pZNCKbjNPWrqboR0P2O61WDwoOdIMqEvIeVgECNvgh7Ix 25v8u3SiSSGvyUmS+U3sqS58Tz0mE201GkQxQVPSHdP7xrm9qjtm13NP31+mRknTZtXz cfQsKDU5OxjQ55/chY44MvKgQK+9K8/suuEHwNj5Nyoygj57cRpSymBLzJ98MIsRRce4 6lOw== X-Forwarded-Encrypted: i=1; AJvYcCXWj0lOQ3Yp+WzZFrynJgGQkz4MSFIcj5CzMRrxpW3hfHFit0Yz0kG/j9Xde1jWbMFw2INmrJ7kXH5HVME=@vger.kernel.org X-Gm-Message-State: AOJu0YwHzfZF4BhKIfgiXKwZKJqpYlCSARKup518HcMJw4ftNBY3sH5h 3JuNCHRy1mlW0JkNJOL3eZ4hiINdbw7+fWq3OPEnhBDRATimuShHMmYnhrgRmhQggHHOluM58kx f5uYJmw== X-Google-Smtp-Source: AGHT+IE5zlLJfJSiKDps9oa63lOpF1CpedYQdGtHutsY8b9qvB6aXOWJRFpftXMF7nK7d9tGDEfQnxjduYc= X-Received: from pfbjw38.prod.google.com ([2002:a05:6a00:92a6:b0:748:ec4d:30ec]) (user=surenb job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a20:3d1c:b0:220:e5e:5909 with SMTP id adf61e73a8af0-23813c6df02mr1420419637.20.1752635169133; Tue, 15 Jul 2025 20:06:09 -0700 (PDT) Date: Tue, 15 Jul 2025 20:05:53 -0700 In-Reply-To: <20250716030557.1547501-1-surenb@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250716030557.1547501-1-surenb@google.com> X-Mailer: git-send-email 2.50.0.727.gbf7dc18ff4-goog Message-ID: <20250716030557.1547501-5-surenb@google.com> Subject: [PATCH v7 4/7] selftests/proc: test PROCMAP_QUERY ioctl while vma is concurrently modified From: Suren Baghdasaryan To: akpm@linux-foundation.org Cc: Liam.Howlett@oracle.com, lorenzo.stoakes@oracle.com, david@redhat.com, vbabka@suse.cz, peterx@redhat.com, jannh@google.com, hannes@cmpxchg.org, mhocko@kernel.org, paulmck@kernel.org, shuah@kernel.org, adobriyan@gmail.com, brauner@kernel.org, josef@toxicpanda.com, yebin10@huawei.com, linux@weissschuh.net, willy@infradead.org, osalvador@suse.de, andrii@kernel.org, ryan.roberts@arm.com, christophe.leroy@csgroup.eu, tjmercier@google.com, kaleshsingh@google.com, aha310510@gmail.com, linux-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, linux-kselftest@vger.kernel.org, surenb@google.com Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Extend /proc/pid/maps tearing test to verify PROCMAP_QUERY ioctl operation correctness while the vma is being concurrently modified. Signed-off-by: Suren Baghdasaryan --- tools/testing/selftests/proc/proc-maps-race.c | 62 +++++++++++++++++++ 1 file changed, 62 insertions(+) diff --git a/tools/testing/selftests/proc/proc-maps-race.c b/tools/testing/= selftests/proc/proc-maps-race.c index 764821ffd63d..6acdafdac9db 100644 --- a/tools/testing/selftests/proc/proc-maps-race.c +++ b/tools/testing/selftests/proc/proc-maps-race.c @@ -30,6 +30,8 @@ #include #include #include +#include +#include #include #include #include @@ -239,6 +241,21 @@ static void capture_mod_pattern(int maps_fd, assert(strcmp(restored_first_line->text, first_line->text) =3D=3D 0); } =20 +static void query_addr_at(int maps_fd, void *addr, + unsigned long *vma_start, unsigned long *vma_end) +{ + struct procmap_query q; + + memset(&q, 0, sizeof(q)); + q.size =3D sizeof(q); + /* Find the VMA at the split address */ + q.query_addr =3D (unsigned long long)addr; + q.query_flags =3D 0; + assert(!ioctl(maps_fd, PROCMAP_QUERY, &q)); + *vma_start =3D q.vma_start; + *vma_end =3D q.vma_end; +} + static inline void split_vma(const struct vma_modifier_info *mod_info) { assert(mmap(mod_info->addr, page_size, mod_info->prot | PROT_EXEC, @@ -299,6 +316,8 @@ static void test_maps_tearing_from_split(int maps_fd, do { bool last_line_changed; bool first_line_changed; + unsigned long vma_start; + unsigned long vma_end; =20 read_boundary_lines(maps_fd, page1, page2, &new_last_line, &new_first_li= ne); =20 @@ -329,6 +348,19 @@ static void test_maps_tearing_from_split(int maps_fd, first_line_changed =3D strcmp(new_first_line.text, first_line->text) != =3D 0; assert(last_line_changed =3D=3D first_line_changed); =20 + /* Check if PROCMAP_QUERY ioclt() finds the right VMA */ + query_addr_at(maps_fd, mod_info->addr + page_size, + &vma_start, &vma_end); + /* + * The vma at the split address can be either the same as + * original one (if read before the split) or the same as the + * first line in the second page (if read after the split). + */ + assert((vma_start =3D=3D last_line->start_addr && + vma_end =3D=3D last_line->end_addr) || + (vma_start =3D=3D split_first_line.start_addr && + vma_end =3D=3D split_first_line.end_addr)); + clock_gettime(CLOCK_MONOTONIC_COARSE, &end_ts); } while (end_ts.tv_sec - start_ts.tv_sec < test_duration_sec); =20 @@ -390,6 +422,9 @@ static void test_maps_tearing_from_resize(int maps_fd, =20 clock_gettime(CLOCK_MONOTONIC_COARSE, &start_ts); do { + unsigned long vma_start; + unsigned long vma_end; + read_boundary_lines(maps_fd, page1, page2, &new_last_line, &new_first_li= ne); =20 /* Check if we read vmas after shrinking it */ @@ -409,6 +444,17 @@ static void test_maps_tearing_from_resize(int maps_fd, assert(!strcmp(new_last_line.text, restored_last_line.text) && !strcmp(new_first_line.text, restored_first_line.text)); } + + /* Check if PROCMAP_QUERY ioclt() finds the right VMA */ + query_addr_at(maps_fd, mod_info->addr, &vma_start, &vma_end); + /* + * The vma should stay at the same address and have either the + * original size of 3 pages or 1 page if read after shrinking. + */ + assert(vma_start =3D=3D last_line->start_addr && + (vma_end - vma_start =3D=3D page_size * 3 || + vma_end - vma_start =3D=3D page_size)); + clock_gettime(CLOCK_MONOTONIC_COARSE, &end_ts); } while (end_ts.tv_sec - start_ts.tv_sec < test_duration_sec); =20 @@ -479,6 +525,9 @@ static void test_maps_tearing_from_remap(int maps_fd, =20 clock_gettime(CLOCK_MONOTONIC_COARSE, &start_ts); do { + unsigned long vma_start; + unsigned long vma_end; + read_boundary_lines(maps_fd, page1, page2, &new_last_line, &new_first_li= ne); =20 /* Check if we read vmas after remapping it */ @@ -498,6 +547,19 @@ static void test_maps_tearing_from_remap(int maps_fd, assert(!strcmp(new_last_line.text, restored_last_line.text) && !strcmp(new_first_line.text, restored_first_line.text)); } + + /* Check if PROCMAP_QUERY ioclt() finds the right VMA */ + query_addr_at(maps_fd, mod_info->addr + page_size, &vma_start, &vma_end); + /* + * The vma should either stay at the same address and have the + * original size of 3 pages or we should find the remapped vma + * at the remap destination address with size of 1 page. + */ + assert((vma_start =3D=3D last_line->start_addr && + vma_end - vma_start =3D=3D page_size * 3) || + (vma_start =3D=3D last_line->start_addr + page_size && + vma_end - vma_start =3D=3D page_size)); + clock_gettime(CLOCK_MONOTONIC_COARSE, &end_ts); } while (end_ts.tv_sec - start_ts.tv_sec < test_duration_sec); =20 --=20 2.50.0.727.gbf7dc18ff4-goog From nobody Tue Oct 7 00:22:39 2025 Received: from mail-pj1-f73.google.com (mail-pj1-f73.google.com [209.85.216.73]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id DAB00285CAF for ; Wed, 16 Jul 2025 03:06:11 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.73 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1752635173; cv=none; b=krACjUVFVMKorXEeyK1jnNL9s5HlJ1CsDaWkdevubft5vkFULT1Vo9cu7O7LQOPVfD+AR7hYlLPmB0dDxAOremPt+6OfyqA8KiUwekqwlJOCccoYJ3dXbwwRJ2yHFN3Iaokt6Hl2O7Ljdv4PFDb7Bmpj3zjZAnYsbJRG1dy9i/4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1752635173; c=relaxed/simple; bh=WMdtgrr0/nAUiC/6BwG7uLPF5Tm6Y08FEzL5Ntj1PoA=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=UAPZyr0D8s/3AGXYK2KprN4wMCRADdLrumTIfpZVcjHkDS9oOjLJEKnoWRktuHMhOhg6mDVEqaJPI2jnQ9jmankBMC+Oiigvu76rIQ27c0sS1CIQdkS6q8xSO+rYJ4oxf3+JwjMJXjlylxK30ukNbsAsYwa7nTVoNjj4fceLsG0= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--surenb.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=wyU0Y/Uz; arc=none smtp.client-ip=209.85.216.73 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--surenb.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="wyU0Y/Uz" Received: by mail-pj1-f73.google.com with SMTP id 98e67ed59e1d1-3132c1942a1so9941885a91.2 for ; Tue, 15 Jul 2025 20:06:11 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1752635171; x=1753239971; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=+EEGEPDve4y58hrdp46uj5UK2kmzvt0af85C6yPTlxY=; b=wyU0Y/UzRzmW9FcRJ0tQn3+KYN2lPUo9OSxZizFMPzxRUB7hidCcvTA+RLvImtn7ng 53W0rC1F0Be3psQnXwne0U8QLeexF3Kbsad8fAyTrwvhWqnlzZ/I3wBz7rPnmWU9+20W P0Pu9+H+xiYFZf0EQtNQgp35X5KHyOv3gOmYbKmg+VHeJez33jn7OK6af6S1qziQc6Wq IKtt7AX5LERkf9tOnrldiabdEUNdzMeWTZH56NDGEkmoPA4UFt5Au5QVgXPxC0JLOmuJ jjDSy/1dikeURAKi/UrxeUAaVJr3QQx8fGx4XVAvaRdmuek79Q/EVlWTRtXVkmvS1k5l 4Tig== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1752635171; x=1753239971; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=+EEGEPDve4y58hrdp46uj5UK2kmzvt0af85C6yPTlxY=; b=vqh+jQjEAlZxqwTvsOFrx551kyJDIz29p62DOy4c7eBMhz72SIIUBVs63dzv5B8GR5 v41+JDdD5VuxJ44VMOTGY9FqZrEBy0juduvVyLKNR7shvXPsM3EkrA35gSjCMFEwl7T8 JBA0KuGLtcC55rsRCs9Ab17RfSOKXLEzrFO/Onu6GtVkYnzBH9wLCzEcd2UtSqQZLBW7 VgxNAq+qWBGXbt4Tjpwa74asJGN/OVDxjS/zRgtOb8m2kYVx6zCr4Nq4RxbomEqHIhnJ 18RiAY/G9NHsrCIzatjdqcgHqId93WJowlQgok6utJP4U1P6MbrgnokXJJwjjyBzvO27 M76Q== X-Forwarded-Encrypted: i=1; AJvYcCVyQR+jXhl7jL/YfosBmZwbDRlcFC9OQqgZicG9l96udad7Vm/P7wOq8uXK7MRs1qYDbK7a2Sey91dPrtA=@vger.kernel.org X-Gm-Message-State: AOJu0YyGD+ab3kES0dbUWUlKKPOsKUxC3T83R15jHUczzSwbD06eM4x2 mcTBHP8MsvXe/aVaFYY6HMBaEhF1xLNygxnL/9ekFJiHBKN3FLT/2srvp81Bne/cG49zcsB6r3y a8r850w== X-Google-Smtp-Source: AGHT+IEb/63ti7d4Q2qcu9whEMcfzkbMdhtts3srSHto5sREd8kxF2Vw4VbGHfvo4ixP5EDqUO+ceN5/tWk= X-Received: from pjbsm16.prod.google.com ([2002:a17:90b:2e50:b0:31a:a0b3:5f6]) (user=surenb job=prod-delivery.src-stubby-dispatcher) by 2002:a17:90b:4f49:b0:31a:b92c:d679 with SMTP id 98e67ed59e1d1-31c9f44e3c1mr1397320a91.35.1752635171211; Tue, 15 Jul 2025 20:06:11 -0700 (PDT) Date: Tue, 15 Jul 2025 20:05:54 -0700 In-Reply-To: <20250716030557.1547501-1-surenb@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250716030557.1547501-1-surenb@google.com> X-Mailer: git-send-email 2.50.0.727.gbf7dc18ff4-goog Message-ID: <20250716030557.1547501-6-surenb@google.com> Subject: [PATCH v7 5/7] selftests/proc: add verbose more for tests to facilitate debugging From: Suren Baghdasaryan To: akpm@linux-foundation.org Cc: Liam.Howlett@oracle.com, lorenzo.stoakes@oracle.com, david@redhat.com, vbabka@suse.cz, peterx@redhat.com, jannh@google.com, hannes@cmpxchg.org, mhocko@kernel.org, paulmck@kernel.org, shuah@kernel.org, adobriyan@gmail.com, brauner@kernel.org, josef@toxicpanda.com, yebin10@huawei.com, linux@weissschuh.net, willy@infradead.org, osalvador@suse.de, andrii@kernel.org, ryan.roberts@arm.com, christophe.leroy@csgroup.eu, tjmercier@google.com, kaleshsingh@google.com, aha310510@gmail.com, linux-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, linux-kselftest@vger.kernel.org, surenb@google.com Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Add verbose mode to the proc tests to print debugging information. Usage: proc-pid-vm -v Signed-off-by: Suren Baghdasaryan --- tools/testing/selftests/proc/proc-maps-race.c | 159 ++++++++++++++++-- 1 file changed, 146 insertions(+), 13 deletions(-) diff --git a/tools/testing/selftests/proc/proc-maps-race.c b/tools/testing/= selftests/proc/proc-maps-race.c index 6acdafdac9db..5f912fedd6cf 100644 --- a/tools/testing/selftests/proc/proc-maps-race.c +++ b/tools/testing/selftests/proc/proc-maps-race.c @@ -39,6 +39,7 @@ =20 static unsigned long test_duration_sec =3D 5UL; static int page_size; +static bool verbose; =20 /* /proc/pid/maps parsing routines */ struct page_content { @@ -207,6 +208,99 @@ static void stop_vma_modifier(struct vma_modifier_info= *mod_info) signal_state(mod_info, SETUP_MODIFY_MAPS); } =20 +static void print_first_lines(char *text, int nr) +{ + const char *end =3D text; + + while (nr && (end =3D strchr(end, '\n')) !=3D NULL) { + nr--; + end++; + } + + if (end) { + int offs =3D end - text; + + text[offs] =3D '\0'; + printf(text); + text[offs] =3D '\n'; + printf("\n"); + } else { + printf(text); + } +} + +static void print_last_lines(char *text, int nr) +{ + const char *start =3D text + strlen(text); + + nr++; /* to ignore the last newline */ + while (nr) { + while (start > text && *start !=3D '\n') + start--; + nr--; + start--; + } + printf(start); +} + +static void print_boundaries(const char *title, + struct page_content *page1, + struct page_content *page2) +{ + if (!verbose) + return; + + printf("%s", title); + /* Print 3 boundary lines from each page */ + print_last_lines(page1->data, 3); + printf("-----------------page boundary-----------------\n"); + print_first_lines(page2->data, 3); +} + +static bool print_boundaries_on(bool condition, const char *title, + struct page_content *page1, + struct page_content *page2) +{ + if (verbose && condition) + print_boundaries(title, page1, page2); + + return condition; +} + +static void report_test_start(const char *name) +{ + if (verbose) + printf("=3D=3D=3D=3D %s =3D=3D=3D=3D\n", name); +} + +static struct timespec print_ts; + +static void start_test_loop(struct timespec *ts) +{ + if (verbose) + print_ts.tv_sec =3D ts->tv_sec; +} + +static void end_test_iteration(struct timespec *ts) +{ + if (!verbose) + return; + + /* Update every second */ + if (print_ts.tv_sec =3D=3D ts->tv_sec) + return; + + printf("."); + fflush(stdout); + print_ts.tv_sec =3D ts->tv_sec; +} + +static void end_test_loop(void) +{ + if (verbose) + printf("\n"); +} + static void capture_mod_pattern(int maps_fd, struct vma_modifier_info *mod_info, struct page_content *page1, @@ -218,18 +312,24 @@ static void capture_mod_pattern(int maps_fd, struct line_content *restored_last_line, struct line_content *restored_first_line) { + print_boundaries("Before modification", page1, page2); + signal_state(mod_info, SETUP_MODIFY_MAPS); wait_for_state(mod_info, SETUP_MAPS_MODIFIED); =20 /* Copy last line of the first page and first line of the last page */ read_boundary_lines(maps_fd, page1, page2, mod_last_line, mod_first_line); =20 + print_boundaries("After modification", page1, page2); + signal_state(mod_info, SETUP_RESTORE_MAPS); wait_for_state(mod_info, SETUP_MAPS_RESTORED); =20 /* Copy last line of the first page and first line of the last page */ read_boundary_lines(maps_fd, page1, page2, restored_last_line, restored_f= irst_line); =20 + print_boundaries("After restore", page1, page2); + mod_info->vma_mod_check(mod_last_line, mod_first_line, restored_last_line, restored_first_line); =20 @@ -301,6 +401,7 @@ static void test_maps_tearing_from_split(int maps_fd, mod_info->vma_restore =3D merge_vma; mod_info->vma_mod_check =3D check_split_result; =20 + report_test_start("Tearing from split"); capture_mod_pattern(maps_fd, mod_info, page1, page2, last_line, first_lin= e, &split_last_line, &split_first_line, &restored_last_line, &restored_first_line); @@ -313,6 +414,7 @@ static void test_maps_tearing_from_split(int maps_fd, struct timespec start_ts, end_ts; =20 clock_gettime(CLOCK_MONOTONIC_COARSE, &start_ts); + start_test_loop(&start_ts); do { bool last_line_changed; bool first_line_changed; @@ -332,12 +434,18 @@ static void test_maps_tearing_from_split(int maps_fd, * In that case new first line will be the same as the * last restored line. */ - assert(!strcmp(new_first_line.text, split_first_line.text) || - !strcmp(new_first_line.text, restored_last_line.text)); + assert(!print_boundaries_on( + strcmp(new_first_line.text, split_first_line.text) && + strcmp(new_first_line.text, restored_last_line.text), + "Split result invalid", page1, page2)); } else { /* The vmas should be consistent with merge results */ - assert(!strcmp(new_last_line.text, restored_last_line.text) && - !strcmp(new_first_line.text, restored_first_line.text)); + assert(!print_boundaries_on( + strcmp(new_last_line.text, restored_last_line.text), + "Merge result invalid", page1, page2)); + assert(!print_boundaries_on( + strcmp(new_first_line.text, restored_first_line.text), + "Merge result invalid", page1, page2)); } /* * First and last lines should change in unison. If the last @@ -362,7 +470,9 @@ static void test_maps_tearing_from_split(int maps_fd, vma_end =3D=3D split_first_line.end_addr)); =20 clock_gettime(CLOCK_MONOTONIC_COARSE, &end_ts); + end_test_iteration(&end_ts); } while (end_ts.tv_sec - start_ts.tv_sec < test_duration_sec); + end_test_loop(); =20 /* Signal the modifyer thread to stop and wait until it exits */ signal_state(mod_info, TEST_DONE); @@ -409,6 +519,7 @@ static void test_maps_tearing_from_resize(int maps_fd, mod_info->vma_restore =3D expand_vma; mod_info->vma_mod_check =3D check_shrink_result; =20 + report_test_start("Tearing from resize"); capture_mod_pattern(maps_fd, mod_info, page1, page2, last_line, first_lin= e, &shrunk_last_line, &shrunk_first_line, &restored_last_line, &restored_first_line); @@ -421,6 +532,7 @@ static void test_maps_tearing_from_resize(int maps_fd, struct timespec start_ts, end_ts; =20 clock_gettime(CLOCK_MONOTONIC_COARSE, &start_ts); + start_test_loop(&start_ts); do { unsigned long vma_start; unsigned long vma_end; @@ -437,12 +549,18 @@ static void test_maps_tearing_from_resize(int maps_fd, * again. In that case new first line will be the same * as the last restored line. */ - assert(!strcmp(new_first_line.text, shrunk_first_line.text) || - !strcmp(new_first_line.text, restored_last_line.text)); + assert(!print_boundaries_on( + strcmp(new_first_line.text, shrunk_first_line.text) && + strcmp(new_first_line.text, restored_last_line.text), + "Shrink result invalid", page1, page2)); } else { /* The vmas should be consistent with the original/resored state */ - assert(!strcmp(new_last_line.text, restored_last_line.text) && - !strcmp(new_first_line.text, restored_first_line.text)); + assert(!print_boundaries_on( + strcmp(new_last_line.text, restored_last_line.text), + "Expand result invalid", page1, page2)); + assert(!print_boundaries_on( + strcmp(new_first_line.text, restored_first_line.text), + "Expand result invalid", page1, page2)); } =20 /* Check if PROCMAP_QUERY ioclt() finds the right VMA */ @@ -456,7 +574,9 @@ static void test_maps_tearing_from_resize(int maps_fd, vma_end - vma_start =3D=3D page_size)); =20 clock_gettime(CLOCK_MONOTONIC_COARSE, &end_ts); + end_test_iteration(&end_ts); } while (end_ts.tv_sec - start_ts.tv_sec < test_duration_sec); + end_test_loop(); =20 /* Signal the modifyer thread to stop and wait until it exits */ signal_state(mod_info, TEST_DONE); @@ -512,6 +632,7 @@ static void test_maps_tearing_from_remap(int maps_fd, mod_info->vma_restore =3D patch_vma; mod_info->vma_mod_check =3D check_remap_result; =20 + report_test_start("Tearing from remap"); capture_mod_pattern(maps_fd, mod_info, page1, page2, last_line, first_lin= e, &remapped_last_line, &remapped_first_line, &restored_last_line, &restored_first_line); @@ -524,6 +645,7 @@ static void test_maps_tearing_from_remap(int maps_fd, struct timespec start_ts, end_ts; =20 clock_gettime(CLOCK_MONOTONIC_COARSE, &start_ts); + start_test_loop(&start_ts); do { unsigned long vma_start; unsigned long vma_end; @@ -540,12 +662,18 @@ static void test_maps_tearing_from_remap(int maps_fd, * again. In that case new first line will be the same * as the last restored line. */ - assert(!strcmp(new_first_line.text, remapped_first_line.text) || - !strcmp(new_first_line.text, restored_last_line.text)); + assert(!print_boundaries_on( + strcmp(new_first_line.text, remapped_first_line.text) && + strcmp(new_first_line.text, restored_last_line.text), + "Remap result invalid", page1, page2)); } else { /* The vmas should be consistent with the original/resored state */ - assert(!strcmp(new_last_line.text, restored_last_line.text) && - !strcmp(new_first_line.text, restored_first_line.text)); + assert(!print_boundaries_on( + strcmp(new_last_line.text, restored_last_line.text), + "Remap restore result invalid", page1, page2)); + assert(!print_boundaries_on( + strcmp(new_first_line.text, restored_first_line.text), + "Remap restore result invalid", page1, page2)); } =20 /* Check if PROCMAP_QUERY ioclt() finds the right VMA */ @@ -561,7 +689,9 @@ static void test_maps_tearing_from_remap(int maps_fd, vma_end - vma_start =3D=3D page_size)); =20 clock_gettime(CLOCK_MONOTONIC_COARSE, &end_ts); + end_test_iteration(&end_ts); } while (end_ts.tv_sec - start_ts.tv_sec < test_duration_sec); + end_test_loop(); =20 /* Signal the modifyer thread to stop and wait until it exits */ signal_state(mod_info, TEST_DONE); @@ -571,6 +701,7 @@ int usage(void) { fprintf(stderr, "Userland /proc/pid/{s}maps race test cases\n"); fprintf(stderr, " -d: Duration for time-consuming tests\n"); + fprintf(stderr, " -v: Verbose mode\n"); fprintf(stderr, " -h: Help screen\n"); exit(-1); } @@ -588,9 +719,11 @@ int main(int argc, char **argv) pid_t pid; int opt; =20 - while ((opt =3D getopt(argc, argv, "d:h")) !=3D -1) { + while ((opt =3D getopt(argc, argv, "d:vh")) !=3D -1) { if (opt =3D=3D 'd') test_duration_sec =3D strtoul(optarg, NULL, 0); + else if (opt =3D=3D 'v') + verbose =3D true; else if (opt =3D=3D 'h') usage(); } --=20 2.50.0.727.gbf7dc18ff4-goog From nobody Tue Oct 7 00:22:39 2025 Received: from mail-pg1-f201.google.com (mail-pg1-f201.google.com [209.85.215.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id CF414286D49 for ; Wed, 16 Jul 2025 03:06:13 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.215.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1752635175; cv=none; b=XOkcW7h9McailolUR1f/8M66IhAJxroMxr+hcRSM6gvj+Woo8g1ot5E1b/gvjTiDbWFm3KJ7o6kJP5yrDFbFD06TINREMx+eHJBSH6KC7R+uSiJ+oKpcBFflHSB4qbPNDNZxE8ZwW12nlsygBtZFF0w6ayEjoLWYl+iPA3ZP+Ao= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1752635175; c=relaxed/simple; bh=yvEdgs2nXUWjdMl9gW7ScSBLyyxJCqlOM9mBp2BycIg=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=KaJfwpg4ukVbNw8+2ENVVqSLNVzWHO0sWhFl2VM7OjtrmKdZ1JeNI6iwVzbStRL2LmtdjusND0KvXk/wNkLC0abPN/7GVyYpCR5dpSaD20Y5rjuy2x59wAuw+5cDuC/UHDGijaOjoxnb9LtKGixM9Bad/Ng52l1hd7Gb7zbA/A0= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--surenb.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=P+6l719B; arc=none smtp.client-ip=209.85.215.201 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--surenb.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="P+6l719B" Received: by mail-pg1-f201.google.com with SMTP id 41be03b00d2f7-b362d101243so5110362a12.0 for ; Tue, 15 Jul 2025 20:06:13 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1752635173; x=1753239973; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=JJyiXrcxEaOrLnExvfxF4iaGO8N7lUzqsD5ouhDVNBI=; b=P+6l719BHTkQKALHa9oefQGZyU70dYLR0NIgwJzdpVf+dR/f1xEoAP1/9fkm9+uQNH AjAxSZIh3vqEAPa221rbuTgbf7Dpmt82q4UcNcOG/QmXynirBSevD0pTY2W8F1UvepZO ajJTEmhxAPL59aKZYZCfAi9OVQ+rJ1kD0udEOIfaDDsKuStDf2obYqoPTTQz5loz8oys ZT+L7fQDiyHAT2D08T5GvA9Yw/TSGLTjG55n3/hnUoNT6FHlrs1DDP9Op7AAWKdzOLJ8 sfK8mo94+ikN6d38Jd1TLD/QXAGFMEb8ouxiu34BYf1s5zG4KY/Q/Dlvl3PI/3XuV4X/ 9Rxg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1752635173; x=1753239973; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=JJyiXrcxEaOrLnExvfxF4iaGO8N7lUzqsD5ouhDVNBI=; b=UVlHP0H0SY0OtimzPAM+dAsND6nrMcVpZRLcsh814Gl98aBjy3SuPKqrYw51iVDDXv LSnjaMe4qLHBZYPbUH27USzmmiId3t79uoH+4jBEUH0hbYC1VCn+M6dLbDiUNgcdZUr7 um29bIqWGj8Rsc10fN7GAUpF5MqY6hhHFND9Q4eJt5f3PkSWljRRX9X3iaSFd+X4XUFd qFtlGSbe42LNy6QcWEovcdGsNezfQ8MCMONkMJ1ofEpgb3kJo17n7ImuBmNlq3wB1F53 VT6JU63Uke4NOD00G7f4brF284nqqq37HKkthP/bCxyVuCagpCbWM45SAtTjzzokidJy RNHw== X-Forwarded-Encrypted: i=1; AJvYcCXV7x/UcwJ54VGvc3PVvJA1dUOGY4zoqFaTS/ehy9r1Sq5pvFU1KxeyECWuRzLucNfzHT2Oi5iNKifHPtg=@vger.kernel.org X-Gm-Message-State: AOJu0YwkDtL2hhUUGUhTdYOdC2+r+O3kMjrjTbywvscB2LS0jDPKtT3d KtsXVEdCjMFP4o9+Up6byEAPJeLaHDC6OUlEzcVcQLG0J+hVDZwscS81XvgRUQhZ/1v2gmRWl3P AHlJ3Ow== X-Google-Smtp-Source: AGHT+IFDGwwmBom5R3AeCsNrmsJsGaDmKZ6nwxto47OpXfv54XBWKzi5o3ZDO8E9HG9YurznkyjmKbtvlxI= X-Received: from pjg12.prod.google.com ([2002:a17:90b:3f4c:b0:312:df0e:5f09]) (user=surenb job=prod-delivery.src-stubby-dispatcher) by 2002:a17:90b:1fc3:b0:30c:540b:9ba with SMTP id 98e67ed59e1d1-31c9f44bc2dmr1486650a91.10.1752635173229; Tue, 15 Jul 2025 20:06:13 -0700 (PDT) Date: Tue, 15 Jul 2025 20:05:55 -0700 In-Reply-To: <20250716030557.1547501-1-surenb@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250716030557.1547501-1-surenb@google.com> X-Mailer: git-send-email 2.50.0.727.gbf7dc18ff4-goog Message-ID: <20250716030557.1547501-7-surenb@google.com> Subject: [PATCH v7 6/7] fs/proc/task_mmu: remove conversion of seq_file position to unsigned From: Suren Baghdasaryan To: akpm@linux-foundation.org Cc: Liam.Howlett@oracle.com, lorenzo.stoakes@oracle.com, david@redhat.com, vbabka@suse.cz, peterx@redhat.com, jannh@google.com, hannes@cmpxchg.org, mhocko@kernel.org, paulmck@kernel.org, shuah@kernel.org, adobriyan@gmail.com, brauner@kernel.org, josef@toxicpanda.com, yebin10@huawei.com, linux@weissschuh.net, willy@infradead.org, osalvador@suse.de, andrii@kernel.org, ryan.roberts@arm.com, christophe.leroy@csgroup.eu, tjmercier@google.com, kaleshsingh@google.com, aha310510@gmail.com, linux-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, linux-kselftest@vger.kernel.org, surenb@google.com Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Back in 2.6 era, last_addr used to be stored in seq_file->version variable, which was unsigned long. As a result, sentinels to represent gate vma and end of all vmas used unsigned values. In more recent kernels we don't used seq_file->version anymore and therefore conversion from loff_t into unsigned type is not needed. Similarly, sentinel values don't need to be unsigned. Remove type conversion for set_file position and change sentinel values to signed. Signed-off-by: Suren Baghdasaryan Reviewed-by: Lorenzo Stoakes Reviewed-by: Vlastimil Babka Acked-by: David Hildenbrand --- fs/proc/task_mmu.c | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/fs/proc/task_mmu.c b/fs/proc/task_mmu.c index 751479eb128f..b8bc06d05a72 100644 --- a/fs/proc/task_mmu.c +++ b/fs/proc/task_mmu.c @@ -135,7 +135,7 @@ static struct vm_area_struct *proc_get_vma(struct proc_= maps_private *priv, if (vma) { *ppos =3D vma->vm_start; } else { - *ppos =3D -2UL; + *ppos =3D -2; vma =3D get_gate_vma(priv->mm); } =20 @@ -145,11 +145,11 @@ static struct vm_area_struct *proc_get_vma(struct pro= c_maps_private *priv, static void *m_start(struct seq_file *m, loff_t *ppos) { struct proc_maps_private *priv =3D m->private; - unsigned long last_addr =3D *ppos; + loff_t last_addr =3D *ppos; struct mm_struct *mm; =20 /* See m_next(). Zero at the start or after lseek. */ - if (last_addr =3D=3D -1UL) + if (last_addr =3D=3D -1) return NULL; =20 priv->task =3D get_proc_task(priv->inode); @@ -170,9 +170,9 @@ static void *m_start(struct seq_file *m, loff_t *ppos) return ERR_PTR(-EINTR); } =20 - vma_iter_init(&priv->iter, mm, last_addr); + vma_iter_init(&priv->iter, mm, (unsigned long)last_addr); hold_task_mempolicy(priv); - if (last_addr =3D=3D -2UL) + if (last_addr =3D=3D -2) return get_gate_vma(mm); =20 return proc_get_vma(priv, ppos); @@ -180,8 +180,8 @@ static void *m_start(struct seq_file *m, loff_t *ppos) =20 static void *m_next(struct seq_file *m, void *v, loff_t *ppos) { - if (*ppos =3D=3D -2UL) { - *ppos =3D -1UL; + if (*ppos =3D=3D -2) { + *ppos =3D -1; return NULL; } return proc_get_vma(m->private, ppos); --=20 2.50.0.727.gbf7dc18ff4-goog From nobody Tue Oct 7 00:22:39 2025 Received: from mail-pl1-f202.google.com (mail-pl1-f202.google.com [209.85.214.202]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 35B7828726D for ; Wed, 16 Jul 2025 03:06:16 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1752635178; cv=none; b=Gmq69lZzuP6MYf+qZL0aGU59z+83xFEzamPibq81lHjagti7XV7j51XNySNXz+6kwpXWK0+nX43VsjTpwRwH1DECUVxmhtVzcErWdLRZnZ7HAFx7Df45Hp5YYObHDmhljZgBxWJiwPqCZdcAC9r81LYsnbRrULTuIkZ1NFWHMzU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1752635178; c=relaxed/simple; bh=oYvFfw2em77nggEm2xeU3D+z1Cfni8O5wEzbV1vruOY=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=siukmmPI9q18GZl9hgOhU0hAadW2hdft6qJU2dbNA8mxibJeCF0zHzBAr8cvoePEgty0rkjREwICZaOl7qCPelew/C+Lp/C1jLfrdkXhvUUetZBwF6XBDlYW5yBVGGaA0jKig75keHHpiNXPIn45HJXstOvowCFWyI1e0+eydU8= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--surenb.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=opXiIaSR; arc=none smtp.client-ip=209.85.214.202 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--surenb.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="opXiIaSR" Received: by mail-pl1-f202.google.com with SMTP id d9443c01a7336-235f6b829cfso44280255ad.2 for ; Tue, 15 Jul 2025 20:06:16 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1752635176; x=1753239976; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=OPwtRwrvPyoO56Fp2A3+1i9CxPrsJGWcxqOdpZT86SM=; b=opXiIaSRW9brSk09FHq99BHCJMwHw5D7ECD4RCSOnBv+sb6EICuGewCygsSdMPEdka G27top+pfaSJ70a79koodCqr6Tu+XZC26zBRmYLD7XXcXdudp6CcWi2R2PTexVv0LTZR iZuOdhiJq2HeJpvXYaWbzRuSNZCJuCnmeoeKtXeossrfOJDt18Y7hK4icVJbLheJj29o nSdUAXE8Gr3JKBpGk1aT6TxMAhAzg7rGTdUMtKPSEj1riArFj8xt8+9118e+0b/7II0b +yotOKNF4JBchb20hZAVbKXwH1SgCrtk0KiDAhF4fsbP1pCs2zcAJOMl9HJeULw2EKK4 ALgw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1752635176; x=1753239976; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=OPwtRwrvPyoO56Fp2A3+1i9CxPrsJGWcxqOdpZT86SM=; b=YDcPfS8mtJBv6jVwbCUGmZxXVaybbPZj2E+TcJYY1wHu4QKcfI3q+LqyZmquoDM+B6 WcVXzyhEEemea5zVgJIliQRDCzgAOu7WjZEAbqyZUTx0T6p1xiYNelGwaPQu+W202/O0 0BP+BB/jhqqosGA4vDp8ekcAF5RFun/h/AFclWChCzYOknpGu+0A/DzCuGvuzKrEWTas URc6fBgZ/mjeg+7R0jElZP7ow0z0opYLnaDhoYLoCQlW242nBdgjbvkBsN5F2MlJ0ji1 qS3NAVLJR3i9T2ZOFTJnl2s3rCYI/WHJAgFo2419Li0O+c0dm2aBYMvlaW5YDaJhvgxU 2XaA== X-Forwarded-Encrypted: i=1; AJvYcCUZnYQzPw7cvQvsKfl+4Rxsh1UQwjEYwel/XV4ybaZLnIjplGIIDgbiyEfwZpj45hpleRnHlnFnkdWBo+E=@vger.kernel.org X-Gm-Message-State: AOJu0Yy3VEE5SOlrxuVeunn6k019pYmDe1V0KkDX0CVulQ+bxzeksHu2 8AdHXFnH6QYTkFCChF3PbiYXaAxaKRVFwtUCWW3t3D2fNT/QKwYsUPHZ4y/PJ/2sXVMI4Qu8lX2 TZT9g6Q== X-Google-Smtp-Source: AGHT+IHyXJbEHIgp2+qVGKBtnHRMJI8uJh40Q+DDdPYoHZcLs9Jo1htz2teqIVJkmR2xgXKKUNDYwihzWlM= X-Received: from pjx14.prod.google.com ([2002:a17:90b:568e:b0:311:a4ee:7c3d]) (user=surenb job=prod-delivery.src-stubby-dispatcher) by 2002:a17:903:1245:b0:224:10a2:cae7 with SMTP id d9443c01a7336-23e25770df5mr17655605ad.40.1752635175619; Tue, 15 Jul 2025 20:06:15 -0700 (PDT) Date: Tue, 15 Jul 2025 20:05:56 -0700 In-Reply-To: <20250716030557.1547501-1-surenb@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250716030557.1547501-1-surenb@google.com> X-Mailer: git-send-email 2.50.0.727.gbf7dc18ff4-goog Message-ID: <20250716030557.1547501-8-surenb@google.com> Subject: [PATCH v7 7/7] fs/proc/task_mmu: read proc/pid/maps under per-vma lock From: Suren Baghdasaryan To: akpm@linux-foundation.org Cc: Liam.Howlett@oracle.com, lorenzo.stoakes@oracle.com, david@redhat.com, vbabka@suse.cz, peterx@redhat.com, jannh@google.com, hannes@cmpxchg.org, mhocko@kernel.org, paulmck@kernel.org, shuah@kernel.org, adobriyan@gmail.com, brauner@kernel.org, josef@toxicpanda.com, yebin10@huawei.com, linux@weissschuh.net, willy@infradead.org, osalvador@suse.de, andrii@kernel.org, ryan.roberts@arm.com, christophe.leroy@csgroup.eu, tjmercier@google.com, kaleshsingh@google.com, aha310510@gmail.com, linux-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, linux-kselftest@vger.kernel.org, surenb@google.com Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" With maple_tree supporting vma tree traversal under RCU and per-vma locks, /proc/pid/maps can be read while holding individual vma locks instead of locking the entire address space. A completely lockless approach (walking vma tree under RCU) would be quite complex with the main issue being get_vma_name() using callbacks which might not work correctly with a stable vma copy, requiring original (unstable) vma - see special_mapping_name() for example. When per-vma lock acquisition fails, we take the mmap_lock for reading, lock the vma, release the mmap_lock and continue. This fallback to mmap read lock guarantees the reader to make forward progress even during lock contention. This will interfere with the writer but for a very short time while we are acquiring the per-vma lock and only when there was contention on the vma reader is interested in. We shouldn't see a repeated fallback to mmap read locks in practice, as this require a very unlikely series of lock contentions (for instance due to repeated vma split operations). However even if this did somehow happen, we would still progress. One case requiring special handling is when a vma changes between the time it was found and the time it got locked. A problematic case would be if a vma got shrunk so that its vm_start moved higher in the address space and a new vma was installed at the beginning: reader found: |--------VMA A--------| VMA is modified: |-VMA B-|----VMA A----| reader locks modified VMA A reader reports VMA A: | gap |----VMA A----| This would result in reporting a gap in the address space that does not exist. To prevent this we retry the lookup after locking the vma, however we do that only when we identify a gap and detect that the address space was changed after we found the vma. This change is designed to reduce mmap_lock contention and prevent a process reading /proc/pid/maps files (often a low priority task, such as monitoring/data collection services) from blocking address space updates. Note that this change has a userspace visible disadvantage: it allows for sub-page data tearing as opposed to the previous mechanism where data tearing could happen only between pages of generated output data. Since current userspace considers data tearing between pages to be acceptable, we assume is will be able to handle sub-page data tearing as well. Signed-off-by: Suren Baghdasaryan Reviewed-by: Vlastimil Babka --- fs/proc/internal.h | 5 ++ fs/proc/task_mmu.c | 145 +++++++++++++++++++++++++++++++++++--- include/linux/mmap_lock.h | 11 +++ mm/madvise.c | 3 +- mm/mmap_lock.c | 93 ++++++++++++++++++++++++ 5 files changed, 246 insertions(+), 11 deletions(-) diff --git a/fs/proc/internal.h b/fs/proc/internal.h index 3d48ffe72583..7c235451c5ea 100644 --- a/fs/proc/internal.h +++ b/fs/proc/internal.h @@ -384,6 +384,11 @@ struct proc_maps_private { struct task_struct *task; struct mm_struct *mm; struct vma_iterator iter; + loff_t last_pos; +#ifdef CONFIG_PER_VMA_LOCK + bool mmap_locked; + struct vm_area_struct *locked_vma; +#endif #ifdef CONFIG_NUMA struct mempolicy *task_mempolicy; #endif diff --git a/fs/proc/task_mmu.c b/fs/proc/task_mmu.c index b8bc06d05a72..b15d0ef29896 100644 --- a/fs/proc/task_mmu.c +++ b/fs/proc/task_mmu.c @@ -127,15 +127,134 @@ static void release_task_mempolicy(struct proc_maps_= private *priv) } #endif =20 -static struct vm_area_struct *proc_get_vma(struct proc_maps_private *priv, - loff_t *ppos) +#ifdef CONFIG_PER_VMA_LOCK + +static void unlock_vma(struct proc_maps_private *priv) +{ + if (priv->locked_vma) { + vma_end_read(priv->locked_vma); + priv->locked_vma =3D NULL; + } +} + +static const struct seq_operations proc_pid_maps_op; + +static inline bool lock_vma_range(struct seq_file *m, + struct proc_maps_private *priv) +{ + /* + * smaps and numa_maps perform page table walk, therefore require + * mmap_lock but maps can be read with locking just the vma and + * walking the vma tree under rcu read protection. + */ + if (m->op !=3D &proc_pid_maps_op) { + if (mmap_read_lock_killable(priv->mm)) + return false; + + priv->mmap_locked =3D true; + } else { + rcu_read_lock(); + priv->locked_vma =3D NULL; + priv->mmap_locked =3D false; + } + + return true; +} + +static inline void unlock_vma_range(struct proc_maps_private *priv) +{ + if (priv->mmap_locked) { + mmap_read_unlock(priv->mm); + } else { + unlock_vma(priv); + rcu_read_unlock(); + } +} + +static struct vm_area_struct *get_next_vma(struct proc_maps_private *priv, + loff_t last_pos) +{ + struct vm_area_struct *vma; + + if (priv->mmap_locked) + return vma_next(&priv->iter); + + unlock_vma(priv); + vma =3D lock_next_vma(priv->mm, &priv->iter, last_pos); + if (!IS_ERR_OR_NULL(vma)) + priv->locked_vma =3D vma; + + return vma; +} + +static inline bool fallback_to_mmap_lock(struct proc_maps_private *priv, + loff_t pos) { - struct vm_area_struct *vma =3D vma_next(&priv->iter); + if (priv->mmap_locked) + return false; + + rcu_read_unlock(); + mmap_read_lock(priv->mm); + /* Reinitialize the iterator after taking mmap_lock */ + vma_iter_set(&priv->iter, pos); + priv->mmap_locked =3D true; =20 + return true; +} + +#else /* CONFIG_PER_VMA_LOCK */ + +static inline bool lock_vma_range(struct seq_file *m, + struct proc_maps_private *priv) +{ + return mmap_read_lock_killable(priv->mm) =3D=3D 0; +} + +static inline void unlock_vma_range(struct proc_maps_private *priv) +{ + mmap_read_unlock(priv->mm); +} + +static struct vm_area_struct *get_next_vma(struct proc_maps_private *priv, + loff_t last_pos) +{ + return vma_next(&priv->iter); +} + +static inline bool fallback_to_mmap_lock(struct proc_maps_private *priv, + loff_t pos) +{ + return false; +} + +#endif /* CONFIG_PER_VMA_LOCK */ + +static struct vm_area_struct *proc_get_vma(struct seq_file *m, loff_t *ppo= s) +{ + struct proc_maps_private *priv =3D m->private; + struct vm_area_struct *vma; + +retry: + vma =3D get_next_vma(priv, *ppos); + /* EINTR of EAGAIN is possible */ + if (IS_ERR(vma)) { + if (PTR_ERR(vma) =3D=3D -EAGAIN && fallback_to_mmap_lock(priv, *ppos)) + goto retry; + + return vma; + } + + /* Store previous position to be able to restart if needed */ + priv->last_pos =3D *ppos; if (vma) { - *ppos =3D vma->vm_start; + /* + * Track the end of the reported vma to ensure position changes + * even if previous vma was merged with the next vma and we + * found the extended vma with the same vm_start. + */ + *ppos =3D vma->vm_end; } else { - *ppos =3D -2; + *ppos =3D -2; /* -2 indicates gate vma */ vma =3D get_gate_vma(priv->mm); } =20 @@ -163,28 +282,34 @@ static void *m_start(struct seq_file *m, loff_t *ppos) return NULL; } =20 - if (mmap_read_lock_killable(mm)) { + if (!lock_vma_range(m, priv)) { mmput(mm); put_task_struct(priv->task); priv->task =3D NULL; return ERR_PTR(-EINTR); } =20 + /* + * Reset current position if last_addr was set before + * and it's not a sentinel. + */ + if (last_addr > 0) + *ppos =3D last_addr =3D priv->last_pos; vma_iter_init(&priv->iter, mm, (unsigned long)last_addr); hold_task_mempolicy(priv); if (last_addr =3D=3D -2) return get_gate_vma(mm); =20 - return proc_get_vma(priv, ppos); + return proc_get_vma(m, ppos); } =20 static void *m_next(struct seq_file *m, void *v, loff_t *ppos) { if (*ppos =3D=3D -2) { - *ppos =3D -1; + *ppos =3D -1; /* -1 indicates no more vmas */ return NULL; } - return proc_get_vma(m->private, ppos); + return proc_get_vma(m, ppos); } =20 static void m_stop(struct seq_file *m, void *v) @@ -196,7 +321,7 @@ static void m_stop(struct seq_file *m, void *v) return; =20 release_task_mempolicy(priv); - mmap_read_unlock(mm); + unlock_vma_range(priv); mmput(mm); put_task_struct(priv->task); priv->task =3D NULL; diff --git a/include/linux/mmap_lock.h b/include/linux/mmap_lock.h index 5da384bd0a26..1f4f44951abe 100644 --- a/include/linux/mmap_lock.h +++ b/include/linux/mmap_lock.h @@ -309,6 +309,17 @@ void vma_mark_detached(struct vm_area_struct *vma); struct vm_area_struct *lock_vma_under_rcu(struct mm_struct *mm, unsigned long address); =20 +/* + * Locks next vma pointed by the iterator. Confirms the locked vma has not + * been modified and will retry under mmap_lock protection if modification + * was detected. Should be called from read RCU section. + * Returns either a valid locked VMA, NULL if no more VMAs or -EINTR if the + * process was interrupted. + */ +struct vm_area_struct *lock_next_vma(struct mm_struct *mm, + struct vma_iterator *iter, + unsigned long address); + #else /* CONFIG_PER_VMA_LOCK */ =20 static inline void mm_lock_seqcount_init(struct mm_struct *mm) {} diff --git a/mm/madvise.c b/mm/madvise.c index 1c30031ab035..9de9b7c797c6 100644 --- a/mm/madvise.c +++ b/mm/madvise.c @@ -108,7 +108,8 @@ void anon_vma_name_free(struct kref *kref) =20 struct anon_vma_name *anon_vma_name(struct vm_area_struct *vma) { - mmap_assert_locked(vma->vm_mm); + if (!rwsem_is_locked(&vma->vm_mm->mmap_lock)) + vma_assert_locked(vma); =20 return vma->anon_name; } diff --git a/mm/mmap_lock.c b/mm/mmap_lock.c index 5f725cc67334..729fb7d0dd59 100644 --- a/mm/mmap_lock.c +++ b/mm/mmap_lock.c @@ -178,6 +178,99 @@ struct vm_area_struct *lock_vma_under_rcu(struct mm_st= ruct *mm, count_vm_vma_lock_event(VMA_LOCK_ABORT); return NULL; } + +static struct vm_area_struct *lock_next_vma_under_mmap_lock(struct mm_stru= ct *mm, + struct vma_iterator *vmi, + unsigned long from_addr) +{ + struct vm_area_struct *vma; + int ret; + + ret =3D mmap_read_lock_killable(mm); + if (ret) + return ERR_PTR(ret); + + /* Lookup the vma at the last position again under mmap_read_lock */ + vma_iter_set(vmi, from_addr); + vma =3D vma_next(vmi); + if (vma) { + /* Very unlikely vma->vm_refcnt overflow case */ + if (unlikely(!vma_start_read_locked(vma))) + vma =3D ERR_PTR(-EAGAIN); + } + + mmap_read_unlock(mm); + + return vma; +} + +struct vm_area_struct *lock_next_vma(struct mm_struct *mm, + struct vma_iterator *vmi, + unsigned long from_addr) +{ + struct vm_area_struct *vma; + unsigned int mm_wr_seq; + bool mmap_unlocked; + + RCU_LOCKDEP_WARN(!rcu_read_lock_held(), "no rcu read lock held"); +retry: + /* Start mmap_lock speculation in case we need to verify the vma later */ + mmap_unlocked =3D mmap_lock_speculate_try_begin(mm, &mm_wr_seq); + vma =3D vma_next(vmi); + if (!vma) + return NULL; + + vma =3D vma_start_read(mm, vma); + if (IS_ERR_OR_NULL(vma)) { + /* + * Retry immediately if the vma gets detached from under us. + * Infinite loop should not happen because the vma we find will + * have to be constantly knocked out from under us. + */ + if (PTR_ERR(vma) =3D=3D -EAGAIN) { + /* reset to search from the last address */ + vma_iter_set(vmi, from_addr); + goto retry; + } + + goto fallback; + } + + /* + * Verify the vma we locked belongs to the same address space and it's + * not behind of the last search position. + */ + if (unlikely(vma->vm_mm !=3D mm || from_addr >=3D vma->vm_end)) + goto fallback_unlock; + + /* + * vma can be ahead of the last search position but we need to verify + * it was not shrunk after we found it and another vma has not been + * installed ahead of it. Otherwise we might observe a gap that should + * not be there. + */ + if (from_addr < vma->vm_start) { + /* Verify only if the address space might have changed since vma lookup.= */ + if (!mmap_unlocked || mmap_lock_speculate_retry(mm, mm_wr_seq)) { + vma_iter_set(vmi, from_addr); + if (vma !=3D vma_next(vmi)) + goto fallback_unlock; + } + } + + return vma; + +fallback_unlock: + vma_end_read(vma); +fallback: + rcu_read_unlock(); + vma =3D lock_next_vma_under_mmap_lock(mm, vmi, from_addr); + rcu_read_lock(); + /* Reinitialize the iterator after re-entering rcu read section */ + vma_iter_set(vmi, IS_ERR_OR_NULL(vma) ? from_addr : vma->vm_end); + + return vma; +} #endif /* CONFIG_PER_VMA_LOCK */ =20 #ifdef CONFIG_LOCK_MM_AND_FIND_VMA --=20 2.50.0.727.gbf7dc18ff4-goog