Received: by 2002:a5b:505:0:0:0:0:0 with SMTP id o5csp1940476ybp; Wed, 9 Oct 2019 23:20:04 -0700 (PDT) X-Google-Smtp-Source: APXvYqwymwAD1sij4pYMMzQk1nUgT2T6mOEiR/yw6mBwfuUenCdXbzZ1WxahqfKGQeAHbnTmr3Xi X-Received: by 2002:a50:8f65:: with SMTP id 92mr6807780edy.9.1570688404434; Wed, 09 Oct 2019 23:20:04 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1570688404; cv=none; d=google.com; s=arc-20160816; b=pVDsvfoW3xWSwNg0n9Sxz92ICeDYnaPGvU7hFTPSQ+dYaplVRMTR3Bbm2Hg1mxvoO9 0nCMy8CEGJ4fVTg1+qBY4+V0EtPInoMp3RXbdnlccqSjLdBRMauxlloO2A3YAOzV2JFi zklN1qRQns70qdHNdg/7fawv5FE6h0PWnphSdlMdjA+SgEkVZ13TruF9+Iwx5yHkM0jJ ZKug5GMLyn4Frgb0j0mFUNNyWwbKIhvKhaaeKuu39iGQZeHVjaGHC4xdQrZ3fVFC8mu2 hB9YufsvjZq1b9z5PWDXRDWSj8l6Gm3d/2nIjZZWXWDxziRtKVKZ544yLRkBVg8GEi1I ymXw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:mime-version:references:in-reply-to :message-id:date:subject:smtp-origin-cluster:cc:to :smtp-origin-hostname:from:smtp-origin-hostprefix:dkim-signature; bh=H5WTiUKRu1qDnKEKpnXoZ0t4lsxmbyz/pzW2AERatVE=; b=foD6pT710StdInC20D98ZZNMaDYwwD0yZ6UoXh5/VH61ygcAM+syff9yMl5ltFh7NT 0Zs6vEGtOmoYGX3YkLs6+uzk0/SWzJHpw57x2NR+LeqxYo1bvxgySAfyMc8tcrHXr0Rw mLqP1YkiftAa4IPaUXqvSv5MeMHbWuxXi9D+ECu/DcXlcPfarZE+ixyQ0+1gdLkmkyP0 uAN7tLWtm+JYDkPwrkiOQFEYzLZv0Tbf5sczcu2FxpGeTAvA9kLsLePquYgs0VHd0nCQ tmO2XkZu3vRojy+BSLzRNomR79/uTNd4BAmJ3F9JNr713OqQrpD5iumIq7aZWOcAKJrg eCXg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@fb.com header.s=facebook header.b=VpkPzN+N; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=fb.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id k7si2544877eja.377.2019.10.09.23.19.40; Wed, 09 Oct 2019 23:20:04 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@fb.com header.s=facebook header.b=VpkPzN+N; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=fb.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1732931AbfJJGTb (ORCPT + 99 others); Thu, 10 Oct 2019 02:19:31 -0400 Received: from mx0b-00082601.pphosted.com ([67.231.153.30]:61238 "EHLO mx0b-00082601.pphosted.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726840AbfJJGT3 (ORCPT ); Thu, 10 Oct 2019 02:19:29 -0400 Received: from pps.filterd (m0109332.ppops.net [127.0.0.1]) by mx0a-00082601.pphosted.com (8.16.0.42/8.16.0.42) with SMTP id x9A69PQv021297 for ; Wed, 9 Oct 2019 23:19:28 -0700 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=fb.com; h=from : to : cc : subject : date : message-id : in-reply-to : references : mime-version : content-type; s=facebook; bh=H5WTiUKRu1qDnKEKpnXoZ0t4lsxmbyz/pzW2AERatVE=; b=VpkPzN+N6TbLnGIfa7Olee2m9RdSMucn9U+I2oiVTekSQbCyQ5wis9JddYAkR1fPojUb cy7qygFYDBAjDzV4Di6+f0vKBhllNkDo7MLAtwhqnEAC7xEoedOz7A/R5uiaeIGMpZ1j +QqPwR5xladfgZJRAZ2ac1IldxAZOhxgqQk= Received: from mail.thefacebook.com (mailout.thefacebook.com [199.201.64.23]) by mx0a-00082601.pphosted.com with ESMTP id 2vht50gvss-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-SHA384 bits=256 verify=NOT) for ; Wed, 09 Oct 2019 23:19:27 -0700 Received: from 2401:db00:2050:5076:face:0:9:0 (2620:10d:c081:10::13) by mail.thefacebook.com (2620:10d:c081:35::130) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA) id 15.1.1713.5; Wed, 9 Oct 2019 23:19:26 -0700 Received: by devbig006.ftw2.facebook.com (Postfix, from userid 4523) id F369962E3559; Wed, 9 Oct 2019 23:19:24 -0700 (PDT) Smtp-Origin-Hostprefix: devbig From: Song Liu Smtp-Origin-Hostname: devbig006.ftw2.facebook.com To: , , CC: , Song Liu , , Peter Zijlstra , Alexei Starovoitov , Daniel Borkmann Smtp-Origin-Cluster: ftw2c04 Subject: [PATCH bpf-next 2/2] bpf/stackmap: fix A-A deadlock in bpf_get_stack() Date: Wed, 9 Oct 2019 23:19:16 -0700 Message-ID: <20191010061916.198761-3-songliubraving@fb.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20191010061916.198761-1-songliubraving@fb.com> References: <20191010061916.198761-1-songliubraving@fb.com> X-FB-Internal: Safe MIME-Version: 1.0 Content-Type: text/plain X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10434:6.0.95,1.0.8 definitions=2019-10-10_03:2019-10-08,2019-10-10 signatures=0 X-Proofpoint-Spam-Details: rule=fb_default_notspam policy=fb_default score=0 bulkscore=0 mlxscore=0 malwarescore=0 spamscore=0 clxscore=1015 adultscore=0 lowpriorityscore=0 mlxlogscore=999 phishscore=0 impostorscore=0 suspectscore=2 priorityscore=1501 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-1908290000 definitions=main-1910100058 X-FB-Internal: deliver Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org bpf stackmap with build-id lookup (BPF_F_STACK_BUILD_ID) can trigger A-A deadlock on rq_lock(): rcu: INFO: rcu_sched detected stalls on CPUs/tasks: [...] Call Trace: try_to_wake_up+0x1ad/0x590 wake_up_q+0x54/0x80 rwsem_wake+0x8a/0xb0 bpf_get_stack+0x13c/0x150 bpf_prog_fbdaf42eded9fe46_on_event+0x5e3/0x1000 bpf_overflow_handler+0x60/0x100 __perf_event_overflow+0x4f/0xf0 perf_swevent_overflow+0x99/0xc0 ___perf_sw_event+0xe7/0x120 __schedule+0x47d/0x620 schedule+0x29/0x90 futex_wait_queue_me+0xb9/0x110 futex_wait+0x139/0x230 do_futex+0x2ac/0xa50 __x64_sys_futex+0x13c/0x180 do_syscall_64+0x42/0x100 entry_SYSCALL_64_after_hwframe+0x44/0xa9 This can be reproduced by: 1. Start a multi-thread program that does parallel mmap() and malloc(); 2. taskset the program to 2 CPUs; 3. Attach bpf program to trace_sched_switch and gather stackmap with build-id, e.g. with trace.py from bcc tools: trace.py -U -p -s t:sched:sched_switch A sample reproducer is attached at the end. Fix this by checking whether rq_lock is already locked. If so, postpone the up_read() to irq_work, just like the in_nmi() case. Fixes: commit 615755a77b24 ("bpf: extend stackmap to save binary_build_id+offset instead of address") Cc: stable@vger.kernel.org # v4.17+ Cc: Peter Zijlstra Cc: Alexei Starovoitov Cc: Daniel Borkmann Reported-by: Tejun Heo Signed-off-by: Song Liu Reproducer: ============================ 8< ============================ char *filename; void *worker(void *p) { void *ptr; int fd; char *pptr; fd = open(filename, O_RDONLY); if (fd < 0) return NULL; while (1) { struct timespec ts = {0, 1000 + rand() % 2000}; ptr = mmap(NULL, 4096 * 64, PROT_READ, MAP_PRIVATE, fd, 0); usleep(1); if (ptr == MAP_FAILED) { printf("failed to mmap\n"); break; } munmap(ptr, 4096 * 64); usleep(1); pptr = malloc(1); usleep(1); pptr[0] = 1; usleep(1); free(pptr); usleep(1); nanosleep(&ts, NULL); } close(fd); return NULL; } int main(int argc, char *argv[]) { void *ptr; int i; pthread_t threads[THREAD_COUNT]; if (argc < 2) return 0; filename = argv[1]; for (i = 0; i < THREAD_COUNT; i++) { if (pthread_create(threads + i, NULL, worker, NULL)) { fprintf(stderr, "Error creating thread\n"); return 0; } } for (i = 0; i < THREAD_COUNT; i++) pthread_join(threads[i], NULL); return 0; } ============================ 8< ============================ --- kernel/bpf/stackmap.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/kernel/bpf/stackmap.c b/kernel/bpf/stackmap.c index 052580c33d26..3b278f6b0c3e 100644 --- a/kernel/bpf/stackmap.c +++ b/kernel/bpf/stackmap.c @@ -287,7 +287,7 @@ static void stack_map_get_build_id_offset(struct bpf_stack_build_id *id_offs, bool irq_work_busy = false; struct stack_map_irq_work *work = NULL; - if (in_nmi()) { + if (in_nmi() || this_rq_is_locked()) { work = this_cpu_ptr(&up_read_work); if (work->irq_work.flags & IRQ_WORK_BUSY) /* cannot queue more up_read, fallback */ -- 2.17.1