Received: by 2002:a25:31c3:0:0:0:0:0 with SMTP id x186csp825178ybx; Thu, 7 Nov 2019 03:16:30 -0800 (PST) X-Google-Smtp-Source: APXvYqyuNAjlfs16n6UfGK7zqzTdF1OYp/fLG41U005C4DNnK0XB5rBhK0F9901TiA9hr8me9XvV X-Received: by 2002:a17:906:25cb:: with SMTP id n11mr1668095ejb.239.1573125390209; Thu, 07 Nov 2019 03:16:30 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1573125390; cv=none; d=google.com; s=arc-20160816; b=N9DOnJ8V2iAX0Bi1lHU3HE6SXdKCJKYVtcsYzjwBVnAyAopkc5yIf6fp5eqnf7dIi9 YUVUKhcLEelVwoTe+8fdFBqZ+/FWfefeKVcEs/oK5U5XvayaKsqowqd25qPZ+E5tBabd T3KeE7A+7ZE+HrIArh18Fk2KHwp3eRQtkhXCkagLpUPw+tS/4iwPm5iZuX/xYw1dpl68 2B8To/I5qLu51+T387O/ioFYVIvConWmSYQJtm15M7TftzqNfKOuc0Rj1QQdLBBOBdCq dSHqvaftnxjO6xSYqdxDm+J1aELWHwPvUfjedFrOiQImQQoIXD6auH9z8SzOHIKRW+1L Vyrg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:cc:to:subject :message-id:date:from:in-reply-to:references:mime-version :dkim-signature; bh=lkhKlP+nRmdiHSeBByl2uzBydDfCxHRYE22JZBDwszc=; b=Iz/R6YHcB9cc/8shkFCxlfyjqXD+I4tcBHKl1EHABB0JrLFM+q62ZARRgmyxGjDgcD DPVQU4HAyZ4/3NJY6VsKqo5mEbUr+/UJF51YjxMzEZgIRk14FaaxnDQ4nPtdqxSDD9JB 0ZGMylCGet8WaIVpYOJNOhmmyr0+lhiWcpzIP/0xouyShpDIRRUIb0+zFByBl0WKpuZJ mjZm4mx0tPcTjXIpRD2OXZWnwCWShDdU2V5yes5lXw4Z25pe/g2poi9YLJQk+OwX4wZ6 5uehytWpZnf0DOTHuTlBGyq7RdNrWw5JgEbjHe+I56q8STJjouBa++Y6kmteN0lOD3QH WA8g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b="gI9Ei2/E"; 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=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id e23si1105425eda.248.2019.11.07.03.16.06; Thu, 07 Nov 2019 03:16:30 -0800 (PST) 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=@google.com header.s=20161025 header.b="gI9Ei2/E"; 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=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2388093AbfKGLPV (ORCPT + 99 others); Thu, 7 Nov 2019 06:15:21 -0500 Received: from mail-wr1-f68.google.com ([209.85.221.68]:38874 "EHLO mail-wr1-f68.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727528AbfKGLPU (ORCPT ); Thu, 7 Nov 2019 06:15:20 -0500 Received: by mail-wr1-f68.google.com with SMTP id j15so2556189wrw.5 for ; Thu, 07 Nov 2019 03:15:16 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc:content-transfer-encoding; bh=lkhKlP+nRmdiHSeBByl2uzBydDfCxHRYE22JZBDwszc=; b=gI9Ei2/E5vg3K70OQbtvyLQFKNCmpvb02EzBVRHawvo509ZV+i6q/NuFzM512blCCw w8FivJLQ5+sD4Ra7JGG0QBRBuIXkqRGvlu7nUHq/1/r0Ctb95RbMva/tTYnOfc2m4+bA DGor7b9uEfdoemUrrH8DPT4lzzaIekGREGHXI7zuCcfSW7Yf7GtXVVDw8TQBIgM0ro6J RD6hFqkl1hT65pOS0RUCZXqYFWjtDyk0FEHuEpeYtglM2Br8ox9jY1KV86BGABMINWhb 0PtfO7a2lKsxYBooRqTroJiMScICvc06lhMJBwprgyF889dZsiXge/vY4Xf2sSaiFHey z5mg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc:content-transfer-encoding; bh=lkhKlP+nRmdiHSeBByl2uzBydDfCxHRYE22JZBDwszc=; b=AgEZiKmm9F2y10rj9axP6pLCn7oQGfKS17zxonUlQPtx9yYkna3faMFIpNNL9f7zMh TEPr/aXzv+ZovR0uCedcp2e4+YqtKb8rGrkHAm8QNk3ortGOEvaNW2PECxv95KVXsA1G Ahi7F29FfiWzF5Lg0B1JLiIOmn9jumZ3b8sPbLs5Auwq3nmxsz41iNiGk6PRzI1bPiCm 3aim+c7Es8GDY/13f1xDBgPtzsmIfisabldL7fvdE89RdRuq0nFgXqvQI731Qyj6wpbg ZfNalpJkC8PTvNDIbT0PTRExn7oG/TVJ3dUE/KybWih2k2yM2Z4oQXtJrnWZFNNrKOEK 2UuA== X-Gm-Message-State: APjAAAUJplLV9vf8wJ6SKO1hLVrISdOUrlGnuQJUo+UdjvJ4gDm8Kvz0 CMOSpLOYoaDPG/4B4g0Y/cjnLBh5800t6dWZDD9SjQ== X-Received: by 2002:a5d:4ecd:: with SMTP id s13mr2419196wrv.216.1573125315730; Thu, 07 Nov 2019 03:15:15 -0800 (PST) MIME-Version: 1.0 References: <20191106222208.26815-1-labbott@redhat.com> In-Reply-To: <20191106222208.26815-1-labbott@redhat.com> From: Alexander Potapenko Date: Thu, 7 Nov 2019 12:15:04 +0100 Message-ID: Subject: Re: [PATCH] mm: slub: Really fix slab walking for init_on_free To: Laura Abbott Cc: Andrew Morton , Networking , Linux Memory Management List , LKML , "David S. Miller" , Kees Cook , clipos@ssi.gouv.fr, Vlastimil Babka , Thibaut Sautereau Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Wed, Nov 6, 2019 at 11:22 PM Laura Abbott wrote: > > Commit 1b7e816fc80e ("mm: slub: Fix slab walking for init_on_free") > fixed one problem with the slab walking but missed a key detail: > When walking the list, the head and tail pointers need to be updated > since we end up reversing the list as a result. Without doing this, > bulk free is broken. One way this is exposed is a NULL pointer with > slub_debug=3DF: Thanks for the fix! Is it possible to reproduce the problem on a small test case that could be added to lib/test_meminit.c? > =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D > BUG skbuff_head_cache (Tainted: G T): Object already free > -------------------------------------------------------------------------= ---- > > INFO: Slab 0x000000000d2d2f8f objects=3D16 used=3D3 fp=3D0x00000000643090= 71 flags=3D0x3fff00000000201 > BUG: kernel NULL pointer dereference, address: 0000000000000000 > PGD 0 P4D 0 > Oops: 0000 [#1] PREEMPT SMP PTI > CPU: 0 PID: 0 Comm: swapper/0 Tainted: G B T 5.3.8 #1 > Hardware name: QEMU Standard PC (Q35 + ICH9, 2009), BIOS 0.0.0 02/06/2015 > RIP: 0010:print_trailer+0x70/0x1d5 > Code: 28 4d 8b 4d 00 4d 8b 45 20 81 e2 ff 7f 00 00 e8 86 ce ef ff 8b 4b 2= 0 48 89 ea 48 89 ee 4c 29 e2 48 c7 c7 90 6f d4 89 48 01 e9 <48> 33 09 48 33= 8b 70 01 00 00 e8 61 ce ef ff f6 43 09 04 74 35 8b > RSP: 0018:ffffbf7680003d58 EFLAGS: 00010046 > RAX: 000000000000005d RBX: ffffa3d2bb08e540 RCX: 0000000000000000 > RDX: 00005c2d8fdc2000 RSI: 0000000000000000 RDI: ffffffff89d46f90 > RBP: 0000000000000000 R08: 0000000000000242 R09: 000000000000006c > R10: 0000000000000000 R11: 0000000000000030 R12: ffffa3d27023e000 > R13: fffff11080c08f80 R14: ffffa3d2bb047a80 R15: 0000000000000002 > FS: 0000000000000000(0000) GS:ffffa3d2be400000(0000) knlGS:0000000000000= 000 > CS: 0010 DS: 0000 ES: 0000 CR0: 0000000080050033 > CR2: 0000000000000000 CR3: 000000007a6c4000 CR4: 00000000000006f0 > Call Trace: > > free_debug_processing.cold.37+0xc9/0x149 > ? __kfree_skb_flush+0x30/0x40 > ? __kfree_skb_flush+0x30/0x40 > __slab_free+0x22a/0x3d0 > ? tcp_wfree+0x2a/0x140 > ? __sock_wfree+0x1b/0x30 > kmem_cache_free_bulk+0x415/0x420 > ? __kfree_skb_flush+0x30/0x40 > __kfree_skb_flush+0x30/0x40 > net_rx_action+0x2dd/0x480 > __do_softirq+0xf0/0x246 > irq_exit+0x93/0xb0 > do_IRQ+0xa0/0x110 > common_interrupt+0xf/0xf > > > Given we're now almost identical to the existing debugging > code which correctly walks the list, combine with that. > > Link: https://lkml.kernel.org/r/20191104170303.GA50361@gandi.net > Reported-by: Thibaut Sautereau > Fixes: 1b7e816fc80e ("mm: slub: Fix slab walking for init_on_free") > Signed-off-by: Laura Abbott Acked-by: Alexander Potapenko > --- > mm/slub.c | 39 +++++++++------------------------------ > 1 file changed, 9 insertions(+), 30 deletions(-) > > diff --git a/mm/slub.c b/mm/slub.c > index dac41cf0b94a..d2445dd1c7ed 100644 > --- a/mm/slub.c > +++ b/mm/slub.c > @@ -1432,12 +1432,15 @@ static inline bool slab_free_freelist_hook(struct= kmem_cache *s, > void *old_tail =3D *tail ? *tail : *head; > int rsize; > > - if (slab_want_init_on_free(s)) { > - void *p =3D NULL; > + /* Head and tail of the reconstructed freelist */ > + *head =3D NULL; > + *tail =3D NULL; > > - do { > - object =3D next; > - next =3D get_freepointer(s, object); > + do { > + object =3D next; > + next =3D get_freepointer(s, object); > + > + if (slab_want_init_on_free(s)) { > /* > * Clear the object and the metadata, but don't t= ouch > * the redzone. > @@ -1447,29 +1450,8 @@ static inline bool slab_free_freelist_hook(struct = kmem_cache *s, > : 0; > memset((char *)object + s->inuse, 0, > s->size - s->inuse - rsize); > - set_freepointer(s, object, p); > - p =3D object; > - } while (object !=3D old_tail); > - } > - > -/* > - * Compiler cannot detect this function can be removed if slab_free_hook= () > - * evaluates to nothing. Thus, catch all relevant config debug options = here. > - */ > -#if defined(CONFIG_LOCKDEP) || \ > - defined(CONFIG_DEBUG_KMEMLEAK) || \ > - defined(CONFIG_DEBUG_OBJECTS_FREE) || \ > - defined(CONFIG_KASAN) > > - next =3D *head; > - > - /* Head and tail of the reconstructed freelist */ > - *head =3D NULL; > - *tail =3D NULL; > - > - do { > - object =3D next; > - next =3D get_freepointer(s, object); > + } > /* If object's reuse doesn't have to be delayed */ > if (!slab_free_hook(s, object)) { > /* Move object to the new freelist */ > @@ -1484,9 +1466,6 @@ static inline bool slab_free_freelist_hook(struct k= mem_cache *s, > *tail =3D NULL; > > return *head !=3D NULL; > -#else > - return true; > -#endif > } > > static void *setup_object(struct kmem_cache *s, struct page *page, > -- > 2.21.0 > --=20 Alexander Potapenko Software Engineer Google Germany GmbH Erika-Mann-Stra=C3=9Fe, 33 80636 M=C3=BCnchen Gesch=C3=A4ftsf=C3=BChrer: Paul Manicle, Halimah DeLaine Prado Registergericht und -nummer: Hamburg, HRB 86891 Sitz der Gesellschaft: Hamburg